Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: grit/format/policy_templates/policy_template_generator_unittest.py

Issue 1442863002: Remove contents of grit's SVN repository. (Closed) Base URL: http://grit-i18n.googlecode.com/svn/trunk/
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
7 import os
8 import sys
9 if __name__ == '__main__':
10 sys.path.append(os.path.join(os.path.dirname(__file__), '../../..'))
11
12 import unittest
13
14 from grit.format.policy_templates import policy_template_generator
15 from grit.format.policy_templates.writers import mock_writer
16 from grit.format.policy_templates.writers import template_writer
17
18
19 class 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 testStringEnumTexts(self):
266 # Test that GUI messages are assigned correctly to string-enums
267 # (aka dropdown menus).
268 policy_data_mock = {
269 'policy_definitions': [{
270 'name': 'Policy1',
271 'type': 'string-enum-list',
272 'caption': '', 'desc': '',
273 'supported_on': [],
274 'items': [
275 {'name': 'item1', 'value': 'one', 'caption': 'string1', 'desc': ''},
276 {'name': 'item2', 'value': 'two', 'caption': 'string2', 'desc': ''},
277 {'name': 'item3', 'value': 'three', 'caption': 'string3', 'desc': ''},
278 ]
279 }]
280 }
281 class LocalMockWriter(mock_writer.MockWriter):
282 def WritePolicy(self, policy):
283 self.tester.assertEquals(policy['items'][0]['caption'], 'string1')
284 self.tester.assertEquals(policy['items'][1]['caption'], 'string2')
285 self.tester.assertEquals(policy['items'][2]['caption'], 'string3')
286 self.do_test(policy_data_mock, LocalMockWriter())
287
288 def testPolicyFiltering(self):
289 # Test that policies are filtered correctly based on their annotations.
290 policy_data_mock = {
291 'policy_definitions': [
292 {
293 'name': 'Group1',
294 'type': 'group',
295 'caption': '',
296 'desc': '',
297 'policies': [
298 {
299 'name': 'Group1Policy1',
300 'type': 'string',
301 'caption': '',
302 'desc': '',
303 'supported_on': [
304 'chrome.aaa:8-', 'chrome.bbb:8-', 'chrome.ccc:8-'
305 ]
306 },
307 {
308 'name': 'Group1Policy2',
309 'type': 'string',
310 'caption': '',
311 'desc': '',
312 'supported_on': ['chrome.ddd:8-']
313 },
314 ]
315 }, {
316 'name': 'Group2',
317 'type': 'group',
318 'caption': '',
319 'desc': '',
320 'policies': [
321 {
322 'name': 'Group2Policy3',
323 'type': 'string',
324 'caption': '',
325 'desc': '',
326 'supported_on': ['chrome.eee:8-']
327 },
328 ]
329 }, {
330 'name': 'SinglePolicy',
331 'type': 'int',
332 'caption': '',
333 'desc': '',
334 'supported_on': ['chrome.eee:8-']
335 }
336 ]
337 }
338 # This writer accumulates the list of policies it is asked to write.
339 # This list is stored in the result_list member variable and can
340 # be used later for assertions.
341 class LocalMockWriter(mock_writer.MockWriter):
342 def __init__(self, platforms):
343 self.platforms = platforms
344 self.policy_name = None
345 self.result_list = []
346 def BeginPolicyGroup(self, group):
347 self.group = group;
348 self.result_list.append('begin_' + group['name'])
349 def EndPolicyGroup(self):
350 self.result_list.append('end_group')
351 self.group = None
352 def WritePolicy(self, policy):
353 self.result_list.append(policy['name'])
354 def IsPolicySupported(self, policy):
355 # Call the original (non-mock) implementation of this method.
356 return template_writer.TemplateWriter.IsPolicySupported(self, policy)
357
358 local_mock_writer = LocalMockWriter(['eee'])
359 self.do_test(policy_data_mock, local_mock_writer)
360 # Test that only policies of platform 'eee' were written:
361 self.assertEquals(
362 local_mock_writer.result_list,
363 ['begin_Group2', 'Group2Policy3', 'end_group', 'SinglePolicy'])
364
365 local_mock_writer = LocalMockWriter(['ddd', 'bbb'])
366 self.do_test(policy_data_mock, local_mock_writer)
367 # Test that only policies of platforms 'ddd' and 'bbb' were written:
368 self.assertEquals(
369 local_mock_writer.result_list,
370 ['begin_Group1', 'Group1Policy1', 'Group1Policy2', 'end_group'])
371
372 def testSortingInvoked(self):
373 # Tests that policy-sorting happens before passing policies to the writer.
374 policy_data = {
375 'policy_definitions': [
376 {'name': 'zp', 'type': 'string', 'supported_on': [],
377 'caption': '', 'desc': ''},
378 {'name': 'ap', 'type': 'string', 'supported_on': [],
379 'caption': '', 'desc': ''},
380 ]
381 }
382 class LocalMockWriter(mock_writer.MockWriter):
383 def __init__(self):
384 self.result_list = []
385 def WritePolicy(self, policy):
386 self.result_list.append(policy['name'])
387 def Test(self):
388 self.tester.assertEquals(
389 self.result_list,
390 ['ap', 'zp'])
391 self.do_test(policy_data, LocalMockWriter())
392
393
394 if __name__ == '__main__':
395 unittest.main()
OLDNEW
« no previous file with comments | « grit/format/policy_templates/policy_template_generator.py ('k') | grit/format/policy_templates/template_formatter.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698