OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2011 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 |
| 6 import os |
| 7 import sys |
| 8 if __name__ == '__main__': |
| 9 sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../..')) |
| 10 |
| 11 import unittest |
| 12 |
| 13 from grit.format.policy_templates import policy_template_generator |
| 14 from grit.format.policy_templates.writers import mock_writer |
| 15 from grit.format.policy_templates.writers import template_writer |
| 16 |
| 17 |
| 18 class PolicyTemplateGeneratorUnittest(unittest.TestCase): |
| 19 '''Unit tests for policy_template_generator.py.''' |
| 20 |
| 21 def do_test(self, policy_data, writer): |
| 22 '''Executes a test case. |
| 23 |
| 24 Creates and invokes an instance of PolicyTemplateGenerator with |
| 25 the given arguments. |
| 26 |
| 27 Notice: Plain comments are used in test methods instead of docstrings, |
| 28 so that method names do not get overridden by the docstrings in the |
| 29 test output. |
| 30 |
| 31 Args: |
| 32 policy_data: The list of policies and groups as it would be |
| 33 loaded from policy_templates.json. |
| 34 writer: A writer used for this test. It is usually derived from |
| 35 mock_writer.MockWriter. |
| 36 ''' |
| 37 writer.tester = self |
| 38 config = { |
| 39 'app_name': '_app_name', |
| 40 'frame_name': '_frame_name', |
| 41 'os_name': '_os_name', |
| 42 } |
| 43 if not 'messages' in policy_data: |
| 44 policy_data['messages'] = {} |
| 45 if not 'placeholders' in policy_data: |
| 46 policy_data['placeholders'] = [] |
| 47 if not 'policy_definitions' in policy_data: |
| 48 policy_data['policy_definitions'] = [] |
| 49 policy_generator = policy_template_generator.PolicyTemplateGenerator( |
| 50 config, |
| 51 policy_data) |
| 52 res = policy_generator.GetTemplateText(writer) |
| 53 writer.Test() |
| 54 return res |
| 55 |
| 56 def testSequence(self): |
| 57 # Test the sequence of invoking the basic PolicyWriter operations, |
| 58 # in case of empty input data structures. |
| 59 class LocalMockWriter(mock_writer.MockWriter): |
| 60 def __init__(self): |
| 61 self.log = 'init;' |
| 62 def Init(self): |
| 63 self.log += 'prepare;' |
| 64 def BeginTemplate(self): |
| 65 self.log += 'begin;' |
| 66 def EndTemplate(self): |
| 67 self.log += 'end;' |
| 68 def GetTemplateText(self): |
| 69 self.log += 'get_text;' |
| 70 return 'writer_result_string' |
| 71 def Test(self): |
| 72 self.tester.assertEquals(self.log, |
| 73 'init;prepare;begin;end;get_text;') |
| 74 result = self.do_test({}, LocalMockWriter()) |
| 75 self.assertEquals(result, 'writer_result_string') |
| 76 |
| 77 def testEmptyGroups(self): |
| 78 # Test that empty policy groups are not passed to the writer. |
| 79 policies_mock = { |
| 80 'policy_definitions': [ |
| 81 {'name': 'Group1', 'type': 'group', 'policies': [], |
| 82 'desc': '', 'caption': ''}, |
| 83 {'name': 'Group2', 'type': 'group', 'policies': [], |
| 84 'desc': '', 'caption': ''}, |
| 85 {'name': 'Group3', 'type': 'group', 'policies': [], |
| 86 'desc': '', 'caption': ''}, |
| 87 ] |
| 88 } |
| 89 class LocalMockWriter(mock_writer.MockWriter): |
| 90 def __init__(self): |
| 91 self.log = '' |
| 92 def BeginPolicyGroup(self, group): |
| 93 self.log += '[' |
| 94 def EndPolicyGroup(self): |
| 95 self.log += ']' |
| 96 def Test(self): |
| 97 self.tester.assertEquals(self.log, '') |
| 98 self.do_test(policies_mock, LocalMockWriter()) |
| 99 |
| 100 def testGroups(self): |
| 101 # Test that policy groups are passed to the writer in the correct order. |
| 102 policies_mock = { |
| 103 'policy_definitions': [ |
| 104 { |
| 105 'name': 'Group1', 'type': 'group', |
| 106 'caption': '', 'desc': '', |
| 107 'policies': [{'name': 'TAG1', 'type': 'mock', 'supported_on': [], |
| 108 'caption': '', 'desc': ''}] |
| 109 }, |
| 110 { |
| 111 'name': 'Group2', 'type': 'group', |
| 112 'caption': '', 'desc': '', |
| 113 'policies': [{'name': 'TAG2', 'type': 'mock', 'supported_on': [], |
| 114 'caption': '', 'desc': ''}] |
| 115 }, |
| 116 { |
| 117 'name': 'Group3', 'type': 'group', |
| 118 'caption': '', 'desc': '', |
| 119 'policies': [{'name': 'TAG3', 'type': 'mock', 'supported_on': [], |
| 120 'caption': '', 'desc': ''}] |
| 121 }, |
| 122 ] |
| 123 } |
| 124 class LocalMockWriter(mock_writer.MockWriter): |
| 125 def __init__(self): |
| 126 self.log = '' |
| 127 def BeginPolicyGroup(self, group): |
| 128 self.log += '[' + group['policies'][0]['name'] |
| 129 def EndPolicyGroup(self): |
| 130 self.log += ']' |
| 131 def Test(self): |
| 132 self.tester.assertEquals(self.log, '[TAG1][TAG2][TAG3]') |
| 133 self.do_test(policies_mock, LocalMockWriter()) |
| 134 |
| 135 def testPolicies(self): |
| 136 # Test that policies are passed to the writer in the correct order. |
| 137 policy_defs_mock = { |
| 138 'policy_definitions': [ |
| 139 { |
| 140 'name': 'Group1', |
| 141 'type': 'group', |
| 142 'caption': '', |
| 143 'desc': '', |
| 144 'policies': [ |
| 145 {'name': 'Group1Policy1', 'type': 'string', 'supported_on': [], |
| 146 'caption': '', 'desc': ''}, |
| 147 {'name': 'Group1Policy2', 'type': 'string', 'supported_on': [], |
| 148 'caption': '', 'desc': ''}, |
| 149 ] |
| 150 }, |
| 151 { |
| 152 'name': 'Group2', |
| 153 'type': 'group', |
| 154 'caption': '', |
| 155 'desc': '', |
| 156 'policies': [ |
| 157 {'name': 'Group2Policy3', 'type': 'string', 'supported_on': [], |
| 158 'caption': '', 'desc': ''}, |
| 159 ] |
| 160 } |
| 161 ] |
| 162 } |
| 163 class LocalMockWriter(mock_writer.MockWriter): |
| 164 def __init__(self): |
| 165 self.policy_name = None |
| 166 self.policy_list = [] |
| 167 def BeginPolicyGroup(self, group): |
| 168 self.group = group; |
| 169 def EndPolicyGroup(self): |
| 170 self.group = None |
| 171 def WritePolicy(self, policy): |
| 172 self.tester.assertEquals(policy['name'][0:6], self.group['name']) |
| 173 self.policy_list.append(policy['name']) |
| 174 def Test(self): |
| 175 self.tester.assertEquals( |
| 176 self.policy_list, |
| 177 ['Group1Policy1', 'Group1Policy2', 'Group2Policy3']) |
| 178 self.do_test( policy_defs_mock, LocalMockWriter()) |
| 179 |
| 180 def testPolicyTexts(self): |
| 181 # Test that GUI messages of policies all get placeholders replaced. |
| 182 policy_data_mock = { |
| 183 'policy_definitions': [ |
| 184 { |
| 185 'name': 'Group1', |
| 186 'type': 'group', |
| 187 'desc': '', |
| 188 'caption': '', |
| 189 'policies': [ |
| 190 { |
| 191 'name': 'Policy1', |
| 192 'caption': '1. app_name -- $1', |
| 193 'label': '2. os_name -- $2', |
| 194 'desc': '3. frame_name -- $3', |
| 195 'type': 'string', |
| 196 'supported_on': [] |
| 197 }, |
| 198 ] |
| 199 } |
| 200 ] |
| 201 } |
| 202 class LocalMockWriter(mock_writer.MockWriter): |
| 203 def WritePolicy(self, policy): |
| 204 if policy['name'] == 'Policy1': |
| 205 self.tester.assertEquals(policy['caption'], |
| 206 '1. app_name -- _app_name') |
| 207 self.tester.assertEquals(policy['label'], |
| 208 '2. os_name -- _os_name') |
| 209 self.tester.assertEquals(policy['desc'], |
| 210 '3. frame_name -- _frame_name') |
| 211 elif policy['name'] == 'Group1': |
| 212 pass |
| 213 else: |
| 214 self.tester.fail() |
| 215 self.do_test(policy_data_mock, LocalMockWriter()) |
| 216 |
| 217 def testIntEnumTexts(self): |
| 218 # Test that GUI messages are assigned correctly to int-enums |
| 219 # (aka dropdown menus). |
| 220 policy_defs_mock = { |
| 221 'policy_definitions': [{ |
| 222 'name': 'Policy1', |
| 223 'type': 'int-enum', |
| 224 'caption': '', 'desc': '', |
| 225 'supported_on': [], |
| 226 'items': [ |
| 227 {'name': 'item1', 'value': 0, 'caption': 'string1', 'desc': ''}, |
| 228 {'name': 'item2', 'value': 1, 'caption': 'string2', 'desc': ''}, |
| 229 {'name': 'item3', 'value': 3, 'caption': 'string3', 'desc': ''}, |
| 230 ] |
| 231 }] |
| 232 } |
| 233 |
| 234 class LocalMockWriter(mock_writer.MockWriter): |
| 235 def WritePolicy(self, policy): |
| 236 self.tester.assertEquals(policy['items'][0]['caption'], 'string1') |
| 237 self.tester.assertEquals(policy['items'][1]['caption'], 'string2') |
| 238 self.tester.assertEquals(policy['items'][2]['caption'], 'string3') |
| 239 self.do_test(policy_defs_mock, LocalMockWriter()) |
| 240 |
| 241 def testStringEnumTexts(self): |
| 242 # Test that GUI messages are assigned correctly to string-enums |
| 243 # (aka dropdown menus). |
| 244 policy_data_mock = { |
| 245 'policy_definitions': [{ |
| 246 'name': 'Policy1', |
| 247 'type': 'string-enum', |
| 248 'caption': '', 'desc': '', |
| 249 'supported_on': [], |
| 250 'items': [ |
| 251 {'name': 'item1', 'value': 'one', 'caption': 'string1', 'desc': ''}, |
| 252 {'name': 'item2', 'value': 'two', 'caption': 'string2', 'desc': ''}, |
| 253 {'name': 'item3', 'value': 'three', 'caption': 'string3', 'desc': ''}, |
| 254 ] |
| 255 }] |
| 256 } |
| 257 class LocalMockWriter(mock_writer.MockWriter): |
| 258 def WritePolicy(self, policy): |
| 259 self.tester.assertEquals(policy['items'][0]['caption'], 'string1') |
| 260 self.tester.assertEquals(policy['items'][1]['caption'], 'string2') |
| 261 self.tester.assertEquals(policy['items'][2]['caption'], 'string3') |
| 262 self.do_test(policy_data_mock, LocalMockWriter()) |
| 263 |
| 264 def testPolicyFiltering(self): |
| 265 # Test that policies are filtered correctly based on their annotations. |
| 266 policy_data_mock = { |
| 267 'policy_definitions': [ |
| 268 { |
| 269 'name': 'Group1', |
| 270 'type': 'group', |
| 271 'caption': '', |
| 272 'desc': '', |
| 273 'policies': [ |
| 274 { |
| 275 'name': 'Group1Policy1', |
| 276 'type': 'string', |
| 277 'caption': '', |
| 278 'desc': '', |
| 279 'supported_on': [ |
| 280 'chrome.aaa:8-', 'chrome.bbb:8-', 'chrome.ccc:8-' |
| 281 ] |
| 282 }, |
| 283 { |
| 284 'name': 'Group1Policy2', |
| 285 'type': 'string', |
| 286 'caption': '', |
| 287 'desc': '', |
| 288 'supported_on': ['chrome.ddd:8-'] |
| 289 }, |
| 290 ] |
| 291 }, { |
| 292 'name': 'Group2', |
| 293 'type': 'group', |
| 294 'caption': '', |
| 295 'desc': '', |
| 296 'policies': [ |
| 297 { |
| 298 'name': 'Group2Policy3', |
| 299 'type': 'string', |
| 300 'caption': '', |
| 301 'desc': '', |
| 302 'supported_on': ['chrome.eee:8-'] |
| 303 }, |
| 304 ] |
| 305 }, { |
| 306 'name': 'SinglePolicy', |
| 307 'type': 'int', |
| 308 'caption': '', |
| 309 'desc': '', |
| 310 'supported_on': ['chrome.eee:8-'] |
| 311 } |
| 312 ] |
| 313 } |
| 314 # This writer accumulates the list of policies it is asked to write. |
| 315 # This list is stored in the result_list member variable and can |
| 316 # be used later for assertions. |
| 317 class LocalMockWriter(mock_writer.MockWriter): |
| 318 def __init__(self, platforms): |
| 319 self.platforms = platforms |
| 320 self.policy_name = None |
| 321 self.result_list = [] |
| 322 def BeginPolicyGroup(self, group): |
| 323 self.group = group; |
| 324 self.result_list.append('begin_' + group['name']) |
| 325 def EndPolicyGroup(self): |
| 326 self.result_list.append('end_group') |
| 327 self.group = None |
| 328 def WritePolicy(self, policy): |
| 329 self.result_list.append(policy['name']) |
| 330 def IsPolicySupported(self, policy): |
| 331 # Call the original (non-mock) implementation of this method. |
| 332 return template_writer.TemplateWriter.IsPolicySupported(self, policy) |
| 333 |
| 334 local_mock_writer = LocalMockWriter(['eee']) |
| 335 self.do_test(policy_data_mock, local_mock_writer) |
| 336 # Test that only policies of platform 'eee' were written: |
| 337 self.assertEquals( |
| 338 local_mock_writer.result_list, |
| 339 ['begin_Group2', 'Group2Policy3', 'end_group', 'SinglePolicy']) |
| 340 |
| 341 local_mock_writer = LocalMockWriter(['ddd', 'bbb']) |
| 342 self.do_test(policy_data_mock, local_mock_writer) |
| 343 # Test that only policies of platforms 'ddd' and 'bbb' were written: |
| 344 self.assertEquals( |
| 345 local_mock_writer.result_list, |
| 346 ['begin_Group1', 'Group1Policy1', 'Group1Policy2', 'end_group']) |
| 347 |
| 348 def testSortingInvoked(self): |
| 349 # Tests that policy-sorting happens before passing policies to the writer. |
| 350 policy_data = { |
| 351 'policy_definitions': [ |
| 352 {'name': 'zp', 'type': 'string', 'supported_on': [], |
| 353 'caption': '', 'desc': ''}, |
| 354 {'name': 'ap', 'type': 'string', 'supported_on': [], |
| 355 'caption': '', 'desc': ''}, |
| 356 ] |
| 357 } |
| 358 class LocalMockWriter(mock_writer.MockWriter): |
| 359 def __init__(self): |
| 360 self.result_list = [] |
| 361 def WritePolicy(self, policy): |
| 362 self.result_list.append(policy['name']) |
| 363 def Test(self): |
| 364 self.tester.assertEquals( |
| 365 self.result_list, |
| 366 ['ap', 'zp']) |
| 367 self.do_test(policy_data, LocalMockWriter()) |
| 368 |
| 369 |
| 370 if __name__ == '__main__': |
| 371 unittest.main() |
OLD | NEW |