PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/json_schema_compiler/model_test.py

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