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