OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import unittest |
| 6 import model |
| 7 import json |
| 8 |
| 9 class TestModel(unittest.TestCase): |
| 10 def setUp(self): |
| 11 self.model = model.Model() |
| 12 self.permissions_json = json.loads(open('test/permissions.json').read()) |
| 13 self.model.add_namespace(self.permissions_json[0], 'extensions', |
| 14 'path/to/permissions.json', '_api') |
| 15 self.permissions = self.model.namespaces.get('permissions') |
| 16 self.windows_json = json.loads(open('test/windows.json').read()) |
| 17 self.model.add_namespace(self.windows_json[0], 'extensions', |
| 18 'path/to/window.json', '_api') |
| 19 self.windows = self.model.namespaces.get('windows') |
| 20 self.tabs_json = json.loads(open('test/tabs.json').read()) |
| 21 self.model.add_namespace(self.tabs_json[0], 'extensions', |
| 22 'path/to/tabs.json', '_api') |
| 23 self.tabs = self.model.namespaces.get('tabs') |
| 24 |
| 25 def test_namespaces(self): |
| 26 self.assertEquals(len(self.model.namespaces), 3) |
| 27 self.assertTrue(self.permissions) |
| 28 |
| 29 def test_namespace_nocompile(self): |
| 30 self.permissions_json[0]['namespace'] = 'something' |
| 31 del self.permissions_json[0]['compile'] |
| 32 self.model.add_namespace(self.permissions_json[0], 'extensions', |
| 33 'path/to/something.json', '_api') |
| 34 self.assertEquals(len(self.model.namespaces), 3) |
| 35 |
| 36 def test_filename(self): |
| 37 self.assertEquals(self.permissions.filename, 'permissions_api') |
| 38 |
| 39 def test_hasFunctions(self): |
| 40 self.assertEquals(sorted(self.permissions.functions.keys()), |
| 41 ["contains", "getAll", "remove", "request"]) |
| 42 |
| 43 def test_function_no_callback(self): |
| 44 del (self.permissions_json[0]['functions'][0]['parameters'][0]) |
| 45 self.assertRaises(KeyError, self.model.add_namespace, |
| 46 self.permissions_json[0], 'extensions', 'path/to/something.json', |
| 47 '_api') |
| 48 |
| 49 def test_function_nocompile(self): |
| 50 # tabs.json has 2 functions marked as nocompile (connect, sendRequest) |
| 51 self.assertEquals(sorted(self.tabs.functions.keys()), |
| 52 ["captureVisibleTab", "create", "detectLanguage", |
| 53 "executeScript", "get", "getAllInWindow", "getCurrent", |
| 54 "getSelected", "highlight", "insertCSS", "move", "query", "reload", |
| 55 "remove", "update"]) |
| 56 |
| 57 def test_hasTypes(self): |
| 58 self.assertEquals(self.tabs.types.keys(), ['Tab']) |
| 59 self.assertEquals(self.permissions.types.keys(), ['Permissions']) |
| 60 self.assertEquals(self.windows.types.keys(), ['Window']) |
| 61 |
| 62 def test_hasProperties(self): |
| 63 self.assertEquals(sorted(self.tabs.types['Tab'].properties.keys()), |
| 64 ["active", "favIconUrl", "highlighted", "id", "incognito", "index", |
| 65 "pinned", "selected", "status", "title", "url", "windowId"]) |
| 66 |
| 67 def test_properties(self): |
| 68 string_prop = self.tabs.types['Tab'].properties['status'] |
| 69 self.assertEquals(string_prop.type, model.PropertyType.FUNDAMENTAL) |
| 70 integer_prop = self.tabs.types['Tab'].properties['id'] |
| 71 self.assertEquals(integer_prop.type, model.PropertyType.FUNDAMENTAL) |
| 72 array_prop = self.windows.types['Window'].properties['tabs'] |
| 73 self.assertEquals(array_prop.type, model.PropertyType.ARRAY) |
| 74 self.assertEquals(array_prop.item_type.type, model.PropertyType.REF) |
| 75 self.assertEquals(array_prop.item_type.json_type, 'Tab') |
| 76 object_prop = self.tabs.functions['query'].params[0] |
| 77 self.assertEquals(object_prop.type, model.PropertyType.OBJECT) |
| 78 self.assertEquals(sorted(object_prop.properties.keys()), |
| 79 ["active", "highlighted", "pinned", "status", "title", "url", |
| 80 "windowId", "windowType"]) |
| 81 # TODO(calamity): test choices |
| 82 |
| 83 def test_property_not_implemented(self): |
| 84 (self.permissions_json[0]['types'][0] |
| 85 ['properties']['permissions']['type']) = 'something' |
| 86 self.assertRaises(NotImplementedError, self.model.add_namespace, |
| 87 self.permissions_json[0], 'extensions', 'path/to/something.json', |
| 88 '_api') |
| 89 |
| 90 def test_description(self): |
| 91 self.assertFalse( |
| 92 self.permissions.functions['contains'].params[0].description) |
| 93 self.assertEquals( |
| 94 self.permissions.functions['contains'].callback.param.description, |
| 95 'True if the extension has the specified permissions.') |
| 96 |
| 97 if __name__ == '__main__': |
| 98 unittest.main() |
OLD | NEW |