1#!/usr/bin/env python 2# Copyright (c) 2012 The Chromium Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6from json_schema import CachedLoad 7from idl_schema import Load 8from model import Platforms 9import model 10import unittest 11 12class ModelTest(unittest.TestCase): 13 def setUp(self): 14 self.model = model.Model() 15 self.permissions_json = CachedLoad('test/permissions.json') 16 self.model.AddNamespace(self.permissions_json[0], 17 'path/to/permissions.json') 18 self.permissions = self.model.namespaces.get('permissions') 19 self.windows_json = CachedLoad('test/windows.json') 20 self.model.AddNamespace(self.windows_json[0], 21 'path/to/window.json') 22 self.windows = self.model.namespaces.get('windows') 23 self.tabs_json = CachedLoad('test/tabs.json') 24 self.model.AddNamespace(self.tabs_json[0], 25 'path/to/tabs.json') 26 self.tabs = self.model.namespaces.get('tabs') 27 self.idl_chromeos = Load('test/idl_namespace_chromeos.idl') 28 self.model.AddNamespace(self.idl_chromeos[0], 29 'path/to/idl_namespace_chromeos.idl') 30 self.idl_namespace_chromeos = self.model.namespaces.get( 31 'idl_namespace_chromeos') 32 self.idl_all_platforms = Load('test/idl_namespace_all_platforms.idl') 33 self.model.AddNamespace(self.idl_all_platforms[0], 34 'path/to/idl_namespace_all_platforms.idl') 35 self.idl_namespace_all_platforms = self.model.namespaces.get( 36 'idl_namespace_all_platforms') 37 self.idl_non_specific_platforms = Load( 38 'test/idl_namespace_non_specific_platforms.idl') 39 self.model.AddNamespace(self.idl_non_specific_platforms[0], 40 'path/to/idl_namespace_non_specific_platforms.idl') 41 self.idl_namespace_non_specific_platforms = self.model.namespaces.get( 42 'idl_namespace_non_specific_platforms') 43 44 def testNamespaces(self): 45 self.assertEquals(6, len(self.model.namespaces)) 46 self.assertTrue(self.permissions) 47 48 def testHasFunctions(self): 49 self.assertEquals(["contains", "getAll", "remove", "request"], 50 sorted(self.permissions.functions.keys())) 51 52 def testHasTypes(self): 53 self.assertEquals(['Tab'], self.tabs.types.keys()) 54 self.assertEquals(['Permissions'], self.permissions.types.keys()) 55 self.assertEquals(['Window'], self.windows.types.keys()) 56 57 def testHasProperties(self): 58 self.assertEquals(["active", "favIconUrl", "highlighted", "id", 59 "incognito", "index", "pinned", "selected", "status", "title", "url", 60 "windowId"], 61 sorted(self.tabs.types['Tab'].properties.keys())) 62 63 def testProperties(self): 64 string_prop = self.tabs.types['Tab'].properties['status'] 65 self.assertEquals(model.PropertyType.STRING, 66 string_prop.type_.property_type) 67 integer_prop = self.tabs.types['Tab'].properties['id'] 68 self.assertEquals(model.PropertyType.INTEGER, 69 integer_prop.type_.property_type) 70 array_prop = self.windows.types['Window'].properties['tabs'] 71 self.assertEquals(model.PropertyType.ARRAY, 72 array_prop.type_.property_type) 73 self.assertEquals(model.PropertyType.REF, 74 array_prop.type_.item_type.property_type) 75 self.assertEquals('tabs.Tab', array_prop.type_.item_type.ref_type) 76 object_prop = self.tabs.functions['query'].params[0] 77 self.assertEquals(model.PropertyType.OBJECT, 78 object_prop.type_.property_type) 79 self.assertEquals( 80 ["active", "highlighted", "pinned", "status", "title", "url", 81 "windowId", "windowType"], 82 sorted(object_prop.type_.properties.keys())) 83 84 def testChoices(self): 85 self.assertEquals(model.PropertyType.CHOICES, 86 self.tabs.functions['move'].params[0].type_.property_type) 87 88 def testPropertyNotImplemented(self): 89 (self.permissions_json[0]['types'][0] 90 ['properties']['permissions']['type']) = 'something' 91 self.assertRaises(model.ParseException, self.model.AddNamespace, 92 self.permissions_json[0], 'path/to/something.json') 93 94 def testDescription(self): 95 self.assertFalse( 96 self.permissions.functions['contains'].params[0].description) 97 self.assertEquals('True if the extension has the specified permissions.', 98 self.permissions.functions['contains'].callback.params[0].description) 99 100 def testPropertyUnixName(self): 101 param = self.tabs.functions['move'].params[0] 102 self.assertEquals('tab_ids', param.unix_name) 103 104 def testUnixName(self): 105 expectations = { 106 'foo': 'foo', 107 'fooBar': 'foo_bar', 108 'fooBarBaz': 'foo_bar_baz', 109 'fooBARBaz': 'foo_bar_baz', 110 'fooBAR': 'foo_bar', 111 'FOO': 'foo', 112 'FOOBar': 'foo_bar', 113 'foo.bar': 'foo_bar', 114 'foo.BAR': 'foo_bar', 115 'foo.barBAZ': 'foo_bar_baz', 116 'foo_Bar_Baz_box': 'foo_bar_baz_box', 117 } 118 for name in expectations: 119 self.assertEquals(expectations[name], model.UnixName(name)) 120 121 def testCamelName(self): 122 expectations = { 123 'foo': 'foo', 124 'fooBar': 'fooBar', 125 'foo_bar_baz': 'fooBarBaz', 126 'FOO_BAR': 'FOOBar', 127 'FOO_bar': 'FOOBar', 128 '_bar': 'Bar', 129 '_bar_baz': 'BarBaz', 130 'bar_': 'bar', 131 'bar_baz_': 'barBaz', 132 } 133 for testcase, expected in expectations.iteritems(): 134 self.assertEquals(expected, model.CamelName(testcase)) 135 136 def testPlatforms(self): 137 self.assertEqual([Platforms.CHROMEOS], 138 self.idl_namespace_chromeos.platforms) 139 self.assertEqual( 140 [Platforms.CHROMEOS, Platforms.CHROMEOS_TOUCH, Platforms.LINUX, 141 Platforms.MAC, Platforms.WIN], 142 self.idl_namespace_all_platforms.platforms) 143 self.assertEqual(None, 144 self.idl_namespace_non_specific_platforms.platforms) 145 146if __name__ == '__main__': 147 unittest.main() 148