| OLD | NEW |
| (Empty) |
| 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 | |
| 6 '''Unit tests for grit.format.policy_templates.writers.json_writer''' | |
| 7 | |
| 8 | |
| 9 import os | |
| 10 import sys | |
| 11 if __name__ == '__main__': | |
| 12 sys.path.append(os.path.join(os.path.dirname(__file__), '../../../..')) | |
| 13 | |
| 14 import unittest | |
| 15 | |
| 16 from grit.format.policy_templates.writers import writer_unittest_common | |
| 17 | |
| 18 | |
| 19 TEMPLATE_HEADER="""\ | |
| 20 // Policy template for Linux. | |
| 21 // Uncomment the policies you wish to activate and change their values to | |
| 22 // something useful for your case. The provided values are for reference only | |
| 23 // and do not provide meaningful defaults! | |
| 24 { | |
| 25 """ | |
| 26 | |
| 27 TEMPLATE_HEADER_WITH_VERSION="""\ | |
| 28 // chromium version: 39.0.0.0 | |
| 29 // Policy template for Linux. | |
| 30 // Uncomment the policies you wish to activate and change their values to | |
| 31 // something useful for your case. The provided values are for reference only | |
| 32 // and do not provide meaningful defaults! | |
| 33 { | |
| 34 """ | |
| 35 | |
| 36 | |
| 37 HEADER_DELIMETER="""\ | |
| 38 //------------------------------------------------------------------------- | |
| 39 """ | |
| 40 | |
| 41 | |
| 42 class JsonWriterUnittest(writer_unittest_common.WriterUnittestCommon): | |
| 43 '''Unit tests for JsonWriter.''' | |
| 44 | |
| 45 def CompareOutputs(self, output, expected_output): | |
| 46 '''Compares the output of the json_writer with its expected output. | |
| 47 | |
| 48 Args: | |
| 49 output: The output of the json writer as returned by grit. | |
| 50 expected_output: The expected output. | |
| 51 | |
| 52 Raises: | |
| 53 AssertionError: if the two strings are not equivalent. | |
| 54 ''' | |
| 55 self.assertEquals( | |
| 56 output.strip(), | |
| 57 expected_output.strip()) | |
| 58 | |
| 59 def testEmpty(self): | |
| 60 # Test the handling of an empty policy list. | |
| 61 grd = self.PrepareTest( | |
| 62 '{' | |
| 63 ' "policy_definitions": [],' | |
| 64 ' "placeholders": [],' | |
| 65 ' "messages": {},' | |
| 66 '}') | |
| 67 output = self.GetOutput(grd, 'fr', {'_chromium': '1'}, 'json', 'en') | |
| 68 expected_output = TEMPLATE_HEADER + '}' | |
| 69 self.CompareOutputs(output, expected_output) | |
| 70 | |
| 71 def testEmptyWithVersion(self): | |
| 72 # Test the handling of an empty policy list. | |
| 73 grd = self.PrepareTest( | |
| 74 '{' | |
| 75 ' "policy_definitions": [],' | |
| 76 ' "placeholders": [],' | |
| 77 ' "messages": {},' | |
| 78 '}') | |
| 79 output = self.GetOutput( | |
| 80 grd, 'fr', {'_chromium': '1', 'version':'39.0.0.0'}, 'json', 'en') | |
| 81 expected_output = TEMPLATE_HEADER_WITH_VERSION + '}' | |
| 82 self.CompareOutputs(output, expected_output) | |
| 83 | |
| 84 def testMainPolicy(self): | |
| 85 # Tests a policy group with a single policy of type 'main'. | |
| 86 grd = self.PrepareTest( | |
| 87 '{' | |
| 88 ' "policy_definitions": [' | |
| 89 ' {' | |
| 90 ' "name": "MainPolicy",' | |
| 91 ' "type": "main",' | |
| 92 ' "caption": "Example Main Policy",' | |
| 93 ' "desc": "Example Main Policy",' | |
| 94 ' "supported_on": ["chrome.linux:8-"],' | |
| 95 ' "example_value": True' | |
| 96 ' },' | |
| 97 ' ],' | |
| 98 ' "placeholders": [],' | |
| 99 ' "messages": {},' | |
| 100 '}') | |
| 101 output = self.GetOutput(grd, 'fr', {'_google_chrome' : '1'}, 'json', 'en') | |
| 102 expected_output = ( | |
| 103 TEMPLATE_HEADER + | |
| 104 ' // Example Main Policy\n' + | |
| 105 HEADER_DELIMETER + | |
| 106 ' // Example Main Policy\n\n' | |
| 107 ' //"MainPolicy": true\n\n' | |
| 108 '}') | |
| 109 self.CompareOutputs(output, expected_output) | |
| 110 | |
| 111 def testRecommendedOnlyPolicy(self): | |
| 112 # Tests a policy group with a single policy of type 'main'. | |
| 113 grd = self.PrepareTest( | |
| 114 '{' | |
| 115 ' "policy_definitions": [' | |
| 116 ' {' | |
| 117 ' "name": "MainPolicy",' | |
| 118 ' "type": "main",' | |
| 119 ' "caption": "Example Main Policy",' | |
| 120 ' "desc": "Example Main Policy",' | |
| 121 ' "features": {' | |
| 122 ' "can_be_recommended": True,' | |
| 123 ' "can_be_mandatory": False' | |
| 124 ' },' | |
| 125 ' "supported_on": ["chrome.linux:8-"],' | |
| 126 ' "example_value": True' | |
| 127 ' },' | |
| 128 ' ],' | |
| 129 ' "placeholders": [],' | |
| 130 ' "messages": {},' | |
| 131 '}') | |
| 132 output = self.GetOutput(grd, 'fr', {'_google_chrome' : '1'}, 'json', 'en') | |
| 133 expected_output = ( | |
| 134 TEMPLATE_HEADER + | |
| 135 ' // Note: this policy is supported only in recommended mode.\n' + | |
| 136 ' // The JSON file should be placed in' + | |
| 137 ' /etc/opt/chrome/policies/recommended.\n' + | |
| 138 ' // Example Main Policy\n' + | |
| 139 HEADER_DELIMETER + | |
| 140 ' // Example Main Policy\n\n' | |
| 141 ' //"MainPolicy": true\n\n' | |
| 142 '}') | |
| 143 self.CompareOutputs(output, expected_output) | |
| 144 | |
| 145 def testStringPolicy(self): | |
| 146 # Tests a policy group with a single policy of type 'string'. | |
| 147 grd = self.PrepareTest( | |
| 148 '{' | |
| 149 ' "policy_definitions": [' | |
| 150 ' {' | |
| 151 ' "name": "StringPolicy",' | |
| 152 ' "type": "string",' | |
| 153 ' "caption": "Example String Policy",' | |
| 154 ' "desc": "Example String Policy",' | |
| 155 ' "supported_on": ["chrome.linux:8-"],' | |
| 156 ' "example_value": "hello, world!"' | |
| 157 ' },' | |
| 158 ' ],' | |
| 159 ' "placeholders": [],' | |
| 160 ' "messages": {},' | |
| 161 '}') | |
| 162 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en') | |
| 163 expected_output = ( | |
| 164 TEMPLATE_HEADER + | |
| 165 ' // Example String Policy\n' + | |
| 166 HEADER_DELIMETER + | |
| 167 ' // Example String Policy\n\n' | |
| 168 ' //"StringPolicy": "hello, world!"\n\n' | |
| 169 '}') | |
| 170 self.CompareOutputs(output, expected_output) | |
| 171 | |
| 172 def testIntPolicy(self): | |
| 173 # Tests a policy group with a single policy of type 'string'. | |
| 174 grd = self.PrepareTest( | |
| 175 '{' | |
| 176 ' "policy_definitions": [' | |
| 177 ' {' | |
| 178 ' "name": "IntPolicy",' | |
| 179 ' "type": "int",' | |
| 180 ' "caption": "Example Int Policy",' | |
| 181 ' "desc": "Example Int Policy",' | |
| 182 ' "supported_on": ["chrome.linux:8-"],' | |
| 183 ' "example_value": 15' | |
| 184 ' },' | |
| 185 ' ],' | |
| 186 ' "placeholders": [],' | |
| 187 ' "messages": {},' | |
| 188 '}') | |
| 189 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en') | |
| 190 expected_output = ( | |
| 191 TEMPLATE_HEADER + | |
| 192 ' // Example Int Policy\n' + | |
| 193 HEADER_DELIMETER + | |
| 194 ' // Example Int Policy\n\n' | |
| 195 ' //"IntPolicy": 15\n\n' | |
| 196 '}') | |
| 197 self.CompareOutputs(output, expected_output) | |
| 198 | |
| 199 def testIntEnumPolicy(self): | |
| 200 # Tests a policy group with a single policy of type 'int-enum'. | |
| 201 grd = self.PrepareTest( | |
| 202 '{' | |
| 203 ' "policy_definitions": [' | |
| 204 ' {' | |
| 205 ' "name": "EnumPolicy",' | |
| 206 ' "type": "int-enum",' | |
| 207 ' "caption": "Example Int Enum",' | |
| 208 ' "desc": "Example Int Enum",' | |
| 209 ' "items": [' | |
| 210 ' {"name": "ProxyServerDisabled", "value": 0, "caption": ""},' | |
| 211 ' {"name": "ProxyServerAutoDetect", "value": 1, "caption": ""},' | |
| 212 ' ],' | |
| 213 ' "supported_on": ["chrome.linux:8-"],' | |
| 214 ' "example_value": 1' | |
| 215 ' },' | |
| 216 ' ],' | |
| 217 ' "placeholders": [],' | |
| 218 ' "messages": {},' | |
| 219 '}') | |
| 220 output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'json', 'en') | |
| 221 expected_output = ( | |
| 222 TEMPLATE_HEADER + | |
| 223 ' // Example Int Enum\n' + | |
| 224 HEADER_DELIMETER + | |
| 225 ' // Example Int Enum\n\n' | |
| 226 ' //"EnumPolicy": 1\n\n' | |
| 227 '}') | |
| 228 self.CompareOutputs(output, expected_output) | |
| 229 | |
| 230 def testStringEnumPolicy(self): | |
| 231 # Tests a policy group with a single policy of type 'string-enum'. | |
| 232 grd = self.PrepareTest( | |
| 233 '{' | |
| 234 ' "policy_definitions": [' | |
| 235 ' {' | |
| 236 ' "name": "EnumPolicy",' | |
| 237 ' "type": "string-enum",' | |
| 238 ' "caption": "Example String Enum",' | |
| 239 ' "desc": "Example String Enum",' | |
| 240 ' "items": [' | |
| 241 ' {"name": "ProxyServerDisabled", "value": "one",' | |
| 242 ' "caption": ""},' | |
| 243 ' {"name": "ProxyServerAutoDetect", "value": "two",' | |
| 244 ' "caption": ""},' | |
| 245 ' ],' | |
| 246 ' "supported_on": ["chrome.linux:8-"],' | |
| 247 ' "example_value": "one"' | |
| 248 ' },' | |
| 249 ' ],' | |
| 250 ' "placeholders": [],' | |
| 251 ' "messages": {},' | |
| 252 '}') | |
| 253 output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'json', 'en') | |
| 254 expected_output = ( | |
| 255 TEMPLATE_HEADER + | |
| 256 ' // Example String Enum\n' + | |
| 257 HEADER_DELIMETER + | |
| 258 ' // Example String Enum\n\n' | |
| 259 ' //"EnumPolicy": "one"\n\n' | |
| 260 '}') | |
| 261 self.CompareOutputs(output, expected_output) | |
| 262 | |
| 263 def testListPolicy(self): | |
| 264 # Tests a policy group with a single policy of type 'list'. | |
| 265 grd = self.PrepareTest( | |
| 266 '{' | |
| 267 ' "policy_definitions": [' | |
| 268 ' {' | |
| 269 ' "name": "ListPolicy",' | |
| 270 ' "type": "list",' | |
| 271 ' "caption": "Example List",' | |
| 272 ' "desc": "Example List",' | |
| 273 ' "supported_on": ["chrome.linux:8-"],' | |
| 274 ' "example_value": ["foo", "bar"]' | |
| 275 ' },' | |
| 276 ' ],' | |
| 277 ' "placeholders": [],' | |
| 278 ' "messages": {},' | |
| 279 '}') | |
| 280 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en') | |
| 281 expected_output = ( | |
| 282 TEMPLATE_HEADER + | |
| 283 ' // Example List\n' + | |
| 284 HEADER_DELIMETER + | |
| 285 ' // Example List\n\n' | |
| 286 ' //"ListPolicy": ["foo", "bar"]\n\n' | |
| 287 '}') | |
| 288 self.CompareOutputs(output, expected_output) | |
| 289 | |
| 290 def testStringEnumListPolicy(self): | |
| 291 # Tests a policy group with a single policy of type 'string-enum-list'. | |
| 292 grd = self.PrepareTest( | |
| 293 '{' | |
| 294 ' "policy_definitions": [' | |
| 295 ' {' | |
| 296 ' "name": "ListPolicy",' | |
| 297 ' "type": "string-enum-list",' | |
| 298 ' "caption": "Example List",' | |
| 299 ' "desc": "Example List",' | |
| 300 ' "items": [' | |
| 301 ' {"name": "ProxyServerDisabled", "value": "one",' | |
| 302 ' "caption": ""},' | |
| 303 ' {"name": "ProxyServerAutoDetect", "value": "two",' | |
| 304 ' "caption": ""},' | |
| 305 ' ],' | |
| 306 ' "supported_on": ["chrome.linux:8-"],' | |
| 307 ' "example_value": ["one", "two"]' | |
| 308 ' },' | |
| 309 ' ],' | |
| 310 ' "placeholders": [],' | |
| 311 ' "messages": {},' | |
| 312 '}') | |
| 313 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en') | |
| 314 expected_output = ( | |
| 315 TEMPLATE_HEADER + | |
| 316 ' // Example List\n' + | |
| 317 HEADER_DELIMETER + | |
| 318 ' // Example List\n\n' | |
| 319 ' //"ListPolicy": ["one", "two"]\n\n' | |
| 320 '}') | |
| 321 self.CompareOutputs(output, expected_output) | |
| 322 | |
| 323 def testDictionaryPolicy(self): | |
| 324 # Tests a policy group with a single policy of type 'dict'. | |
| 325 example = { | |
| 326 'bool': True, | |
| 327 'dict': { | |
| 328 'a': 1, | |
| 329 'b': 2, | |
| 330 }, | |
| 331 'int': 10, | |
| 332 'list': [1, 2, 3], | |
| 333 'string': 'abc', | |
| 334 } | |
| 335 grd = self.PrepareTest( | |
| 336 '{' | |
| 337 ' "policy_definitions": [' | |
| 338 ' {' | |
| 339 ' "name": "DictionaryPolicy",' | |
| 340 ' "type": "dict",' | |
| 341 ' "caption": "Example Dictionary Policy",' | |
| 342 ' "desc": "Example Dictionary Policy",' | |
| 343 ' "supported_on": ["chrome.linux:8-"],' | |
| 344 ' "example_value": ' + str(example) + | |
| 345 ' },' | |
| 346 ' ],' | |
| 347 ' "placeholders": [],' | |
| 348 ' "messages": {},' | |
| 349 '}') | |
| 350 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en') | |
| 351 expected_output = ( | |
| 352 TEMPLATE_HEADER + | |
| 353 ' // Example Dictionary Policy\n' + | |
| 354 HEADER_DELIMETER + | |
| 355 ' // Example Dictionary Policy\n\n' | |
| 356 ' //"DictionaryPolicy": {"bool": true, "dict": {"a": 1, ' | |
| 357 '"b": 2}, "int": 10, "list": [1, 2, 3], "string": "abc"}\n\n' | |
| 358 '}') | |
| 359 self.CompareOutputs(output, expected_output) | |
| 360 | |
| 361 def testNonSupportedPolicy(self): | |
| 362 # Tests a policy that is not supported on Linux, so it shouldn't | |
| 363 # be included in the JSON file. | |
| 364 grd = self.PrepareTest( | |
| 365 '{' | |
| 366 ' "policy_definitions": [' | |
| 367 ' {' | |
| 368 ' "name": "NonLinuxPolicy",' | |
| 369 ' "type": "list",' | |
| 370 ' "caption": "",' | |
| 371 ' "desc": "",' | |
| 372 ' "supported_on": ["chrome.mac:8-"],' | |
| 373 ' "example_value": ["a"]' | |
| 374 ' },' | |
| 375 ' ],' | |
| 376 ' "placeholders": [],' | |
| 377 ' "messages": {},' | |
| 378 '}') | |
| 379 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en') | |
| 380 expected_output = TEMPLATE_HEADER + '}' | |
| 381 self.CompareOutputs(output, expected_output) | |
| 382 | |
| 383 def testPolicyGroup(self): | |
| 384 # Tests a policy group that has more than one policies. | |
| 385 grd = self.PrepareTest( | |
| 386 '{' | |
| 387 ' "policy_definitions": [' | |
| 388 ' {' | |
| 389 ' "name": "Group1",' | |
| 390 ' "type": "group",' | |
| 391 ' "caption": "",' | |
| 392 ' "desc": "",' | |
| 393 ' "policies": [{' | |
| 394 ' "name": "Policy1",' | |
| 395 ' "type": "list",' | |
| 396 ' "caption": "Policy One",' | |
| 397 ' "desc": "Policy One",' | |
| 398 ' "supported_on": ["chrome.linux:8-"],' | |
| 399 ' "example_value": ["a", "b"]' | |
| 400 ' },{' | |
| 401 ' "name": "Policy2",' | |
| 402 ' "type": "string",' | |
| 403 ' "caption": "Policy Two",' | |
| 404 ' "desc": "Policy Two",' | |
| 405 ' "supported_on": ["chrome.linux:8-"],' | |
| 406 ' "example_value": "c"' | |
| 407 ' }],' | |
| 408 ' },' | |
| 409 ' ],' | |
| 410 ' "placeholders": [],' | |
| 411 ' "messages": {},' | |
| 412 '}') | |
| 413 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en') | |
| 414 expected_output = ( | |
| 415 TEMPLATE_HEADER + | |
| 416 ' // Policy One\n' + | |
| 417 HEADER_DELIMETER + | |
| 418 ' // Policy One\n\n' | |
| 419 ' //"Policy1": ["a", "b"],\n\n' | |
| 420 ' // Policy Two\n' + | |
| 421 HEADER_DELIMETER + | |
| 422 ' // Policy Two\n\n' | |
| 423 ' //"Policy2": "c"\n\n' | |
| 424 '}') | |
| 425 self.CompareOutputs(output, expected_output) | |
| 426 | |
| 427 | |
| 428 if __name__ == '__main__': | |
| 429 unittest.main() | |
| OLD | NEW |