[go: nahoru, domu]

blob: 0ff70ac2923cbc31a98a2e308742b3cf36456c2d [file] [log] [blame]
Avi Drissmandfd880852022-09-15 20:11:091# Copyright 2015 The Chromium Authors
danduong970b79a8a2015-07-11 01:04:192# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import unittest
6
7import fieldtrial_to_struct
8import os
9
10
11class FieldTrialToStruct(unittest.TestCase):
12
Nikunj Bhagatf958b792019-03-20 19:13:3013 def FullRelativePath(self, relative_path):
14 base_path = os.path.dirname(__file__)
15 if not base_path:
16 # Handle test being run from the current directory.
17 base_path = '.'
18 return base_path + relative_path
19
danduong970b79a8a2015-07-11 01:04:1920 def test_FieldTrialToDescription(self):
21 config = {
robliao6df95232016-08-23 19:56:5022 'Trial1': [
danduong970b79a8a2015-07-11 01:04:1923 {
Paul Millerad77b7892018-07-11 20:07:4324 'platforms': ['windows'],
robliao74ce2fa2016-09-21 18:33:4925 'experiments': [
26 {
27 'name': 'Group1',
28 'params': {
29 'x': '1',
30 'y': '2'
31 },
32 'enable_features': ['A', 'B'],
33 'disable_features': ['C']
34 },
35 {
36 'name': 'Group2',
37 'params': {
38 'x': '3',
39 'y': '4'
40 },
41 'enable_features': ['D', 'E'],
42 'disable_features': ['F']
43 },
44 ]
danduong970b79a8a2015-07-11 01:04:1945 }
46 ],
robliao74ce2fa2016-09-21 18:33:4947 'Trial2': [
48 {
Paul Millerad77b7892018-07-11 20:07:4349 'platforms': ['windows'],
robliao74ce2fa2016-09-21 18:33:4950 'experiments': [{'name': 'OtherGroup'}]
51 }
robliao16ac6bb2016-10-18 17:45:4652 ],
53 'TrialWithForcingFlag': [
54 {
Paul Millerad77b7892018-07-11 20:07:4355 'platforms': ['windows'],
robliao16ac6bb2016-10-18 17:45:4656 'experiments': [
57 {
58 'name': 'ForcedGroup',
59 'forcing_flag': "my-forcing-flag"
60 }
61 ]
62 }
robliao74ce2fa2016-09-21 18:33:4963 ]
danduong970b79a8a2015-07-11 01:04:1964 }
Stephen Martinis154877b2020-08-05 18:04:3865 result = fieldtrial_to_struct._FieldTrialConfigToDescription(
Haiyang Pan02dcc472023-11-02 18:01:5466 config, ['windows'])
danduong970b79a8a2015-07-11 01:04:1967 expected = {
68 'elements': {
69 'kFieldTrialConfig': {
robliao045eddd2016-09-27 17:13:1370 'studies': [
danduong970b79a8a2015-07-11 01:04:1971 {
robliao6df95232016-08-23 19:56:5072 'name': 'Trial1',
robliao045eddd2016-09-27 17:13:1373 'experiments': [
robliao6df95232016-08-23 19:56:5074 {
75 'name': 'Group1',
Paul Millerf8f07de12018-07-09 00:11:3076 'platforms': ['Study::PLATFORM_WINDOWS'],
robliao6df95232016-08-23 19:56:5077 'params': [
78 {'key': 'x', 'value': '1'},
79 {'key': 'y', 'value': '2'}
80 ],
81 'enable_features': ['A', 'B'],
Nikunj Bhagatf958b792019-03-20 19:13:3082 'disable_features': ['C'],
Weilun Shi3dc06742019-04-15 12:36:2583 'form_factors': [],
robliao6df95232016-08-23 19:56:5084 },
85 {
86 'name': 'Group2',
Paul Millerf8f07de12018-07-09 00:11:3087 'platforms': ['Study::PLATFORM_WINDOWS'],
robliao6df95232016-08-23 19:56:5088 'params': [
89 {'key': 'x', 'value': '3'},
90 {'key': 'y', 'value': '4'}
91 ],
92 'enable_features': ['D', 'E'],
Nikunj Bhagatf958b792019-03-20 19:13:3093 'disable_features': ['F'],
Weilun Shi3dc06742019-04-15 12:36:2594 'form_factors': [],
robliao6df95232016-08-23 19:56:5095 },
asvitkinecc1cd6072015-11-12 15:09:5596 ],
danduong970b79a8a2015-07-11 01:04:1997 },
98 {
robliao6df95232016-08-23 19:56:5099 'name': 'Trial2',
Paul Millerf8f07de12018-07-09 00:11:30100 'experiments': [
101 {
102 'name': 'OtherGroup',
103 'platforms': ['Study::PLATFORM_WINDOWS'],
Weilun Shi3dc06742019-04-15 12:36:25104 'form_factors': [],
Paul Millerf8f07de12018-07-09 00:11:30105 }
106 ]
robliao6df95232016-08-23 19:56:50107 },
robliao16ac6bb2016-10-18 17:45:46108 {
109 'name': 'TrialWithForcingFlag',
110 'experiments': [
111 {
112 'name': 'ForcedGroup',
Paul Millerf8f07de12018-07-09 00:11:30113 'platforms': ['Study::PLATFORM_WINDOWS'],
Nikunj Bhagatf958b792019-03-20 19:13:30114 'forcing_flag': "my-forcing-flag",
Weilun Shi3dc06742019-04-15 12:36:25115 'form_factors': [],
robliao16ac6bb2016-10-18 17:45:46116 }
117 ]
118 },
danduong970b79a8a2015-07-11 01:04:19119 ]
120 }
121 }
122 }
asvitkinecc1cd6072015-11-12 15:09:55123 self.maxDiff = None
danduong970b79a8a2015-07-11 01:04:19124 self.assertEqual(expected, result)
125
robliao74ce2fa2016-09-21 18:33:49126 _MULTIPLE_PLATFORM_CONFIG = {
127 'Trial1': [
128 {
Paul Millerad77b7892018-07-11 20:07:43129 'platforms': ['windows', 'ios'],
Weilun Shi603551b32020-01-09 01:56:30130 'is_low_end_device': 'true',
robliao74ce2fa2016-09-21 18:33:49131 'experiments': [
132 {
133 'name': 'Group1',
134 'params': {
135 'x': '1',
136 'y': '2'
137 },
138 'enable_features': ['A', 'B'],
139 'disable_features': ['C']
140 },
141 {
142 'name': 'Group2',
143 'params': {
144 'x': '3',
145 'y': '4'
146 },
147 'enable_features': ['D', 'E'],
148 'disable_features': ['F']
149 }
150 ]
151 },
152 {
153 'platforms': ['ios'],
154 'experiments': [
155 {
156 'name': 'IOSOnly'
157 }
158 ]
159 },
160 ],
161 'Trial2': [
162 {
Paul Millerad77b7892018-07-11 20:07:43163 'platforms': ['windows', 'mac'],
robliao74ce2fa2016-09-21 18:33:49164 'experiments': [{'name': 'OtherGroup'}]
165 }
166 ]
167 }
168
169 def test_FieldTrialToDescriptionMultipleSinglePlatformMultipleTrial(self):
170 result = fieldtrial_to_struct._FieldTrialConfigToDescription(
Haiyang Pan02dcc472023-11-02 18:01:54171 self._MULTIPLE_PLATFORM_CONFIG, ['ios'])
robliao74ce2fa2016-09-21 18:33:49172 expected = {
173 'elements': {
174 'kFieldTrialConfig': {
robliao045eddd2016-09-27 17:13:13175 'studies': [
robliao74ce2fa2016-09-21 18:33:49176 {
177 'name': 'Trial1',
robliao045eddd2016-09-27 17:13:13178 'experiments': [
robliao74ce2fa2016-09-21 18:33:49179 {
180 'name': 'Group1',
Paul Millerf8f07de12018-07-09 00:11:30181 'platforms': ['Study::PLATFORM_IOS'],
robliao74ce2fa2016-09-21 18:33:49182 'params': [
183 {'key': 'x', 'value': '1'},
184 {'key': 'y', 'value': '2'}
185 ],
186 'enable_features': ['A', 'B'],
Nikunj Bhagatf958b792019-03-20 19:13:30187 'disable_features': ['C'],
Weilun Shi603551b32020-01-09 01:56:30188 'is_low_end_device': 'true',
Weilun Shi3dc06742019-04-15 12:36:25189 'form_factors': [],
robliao74ce2fa2016-09-21 18:33:49190 },
191 {
192 'name': 'Group2',
Paul Millerf8f07de12018-07-09 00:11:30193 'platforms': ['Study::PLATFORM_IOS'],
robliao74ce2fa2016-09-21 18:33:49194 'params': [
195 {'key': 'x', 'value': '3'},
196 {'key': 'y', 'value': '4'}
197 ],
198 'enable_features': ['D', 'E'],
Nikunj Bhagatf958b792019-03-20 19:13:30199 'disable_features': ['F'],
Weilun Shi603551b32020-01-09 01:56:30200 'is_low_end_device': 'true',
Weilun Shi3dc06742019-04-15 12:36:25201 'form_factors': [],
robliao74ce2fa2016-09-21 18:33:49202 },
203 {
Paul Millerf8f07de12018-07-09 00:11:30204 'name': 'IOSOnly',
205 'platforms': ['Study::PLATFORM_IOS'],
Weilun Shi3dc06742019-04-15 12:36:25206 'form_factors': [],
robliao74ce2fa2016-09-21 18:33:49207 },
208 ],
209 },
210 ]
211 }
212 }
213 }
214 self.maxDiff = None
215 self.assertEqual(expected, result)
216
217 def test_FieldTrialToDescriptionMultipleSinglePlatformSingleTrial(self):
218 result = fieldtrial_to_struct._FieldTrialConfigToDescription(
Haiyang Pan02dcc472023-11-02 18:01:54219 self._MULTIPLE_PLATFORM_CONFIG, ['mac'])
robliao74ce2fa2016-09-21 18:33:49220 expected = {
221 'elements': {
222 'kFieldTrialConfig': {
robliao045eddd2016-09-27 17:13:13223 'studies': [
robliao74ce2fa2016-09-21 18:33:49224 {
225 'name': 'Trial2',
robliao045eddd2016-09-27 17:13:13226 'experiments': [
robliao74ce2fa2016-09-21 18:33:49227 {
228 'name': 'OtherGroup',
Paul Millerf8f07de12018-07-09 00:11:30229 'platforms': ['Study::PLATFORM_MAC'],
Weilun Shi3dc06742019-04-15 12:36:25230 'form_factors': [],
231 },
232 ],
233 },
234 ]
235 }
236 }
237 }
238 self.maxDiff = None
239 self.assertEqual(expected, result)
240
241 _MULTIPLE_FORM_FACTORS_CONFIG = {
242 'Trial1': [
243 {
244 'platforms': ['windows'],
245 'form_factors': ['desktop', 'phone'],
246 'experiments': [{'name': 'Group1'}]
247 }
248 ],
249 'Trial2': [
250 {
251 'platforms': ['windows'],
252 'form_factors': ['tablet'],
253 'experiments': [{'name': 'OtherGroup'}]
254 }
255 ]
256 }
257
258 def test_FieldTrialToDescriptionMultipleFormFactorsTrial(self):
259 result = fieldtrial_to_struct._FieldTrialConfigToDescription(
Haiyang Pan02dcc472023-11-02 18:01:54260 self._MULTIPLE_FORM_FACTORS_CONFIG, ['windows'])
Weilun Shi3dc06742019-04-15 12:36:25261 expected = {
262 'elements': {
263 'kFieldTrialConfig': {
264 'studies': [
265 {
266 'name': 'Trial1',
267 'experiments': [
268 {
269 'name': 'Group1',
270 'platforms': ['Study::PLATFORM_WINDOWS'],
Weilun Shi3dc06742019-04-15 12:36:25271 'form_factors': ['Study::DESKTOP', 'Study::PHONE'],
272 },
273 ],
274 },
275 {
276 'name': 'Trial2',
277 'experiments': [
278 {
279 'name': 'OtherGroup',
280 'platforms': ['Study::PLATFORM_WINDOWS'],
Weilun Shi3dc06742019-04-15 12:36:25281 'form_factors': ['Study::TABLET'],
robliao74ce2fa2016-09-21 18:33:49282 },
283 ],
284 },
285 ]
286 }
287 }
288 }
289 self.maxDiff = None
290 self.assertEqual(expected, result)
291
Weilun Shi54785ae2019-05-02 03:53:45292 _MULTIPLE_OVERRIDE_UI_STRING_CONFIG = {
293 'Trial1': [
294 {
295 'platforms': ['windows'],
296 'experiments': [
297 {
298 'name': 'Group1',
299 'override_ui_strings': {
300 'IDS_NEW_TAB_TITLE': 'test1',
301 'IDS_SAD_TAB_TITLE': 'test2',
302 },
303 },
304 ]
305 }
306 ],
307 'Trial2': [
308 {
309 'platforms': ['windows'],
310 'experiments': [
311 {
312 'name': 'Group2',
313 'override_ui_strings': {
314 'IDS_DEFAULT_TAB_TITLE': 'test3',
315 },
316 }
317 ]
318 }
319 ]
320 }
321
322 def test_FieldTrialToDescriptionMultipleOverrideUIStringTrial(self):
323 result = fieldtrial_to_struct._FieldTrialConfigToDescription(
Haiyang Pan02dcc472023-11-02 18:01:54324 self._MULTIPLE_OVERRIDE_UI_STRING_CONFIG, ['windows'])
Weilun Shi54785ae2019-05-02 03:53:45325 expected = {
326 'elements': {
327 'kFieldTrialConfig': {
328 'studies': [
329 {
330 'name': 'Trial1',
331 'experiments': [
332 {
333 'name': 'Group1',
334 'platforms': ['Study::PLATFORM_WINDOWS'],
335 'override_ui_string': [
336 {
337 'name_hash':
338 4045341670,
339 'value': 'test1'
340 },
341 {
342 'name_hash':
343 1173727369,
344 'value': 'test2'
345 },
346 ],
Weilun Shi54785ae2019-05-02 03:53:45347 'form_factors': [],
348 },
349 ],
350 },
351 {
352 'name': 'Trial2',
353 'experiments': [
354 {
355 'name': 'Group2',
356 'platforms': ['Study::PLATFORM_WINDOWS'],
357 'override_ui_string': [
358 {
359 'name_hash':
360 3477264953,
361 'value': 'test3'
362 },
363 ],
Weilun Shi54785ae2019-05-02 03:53:45364 'form_factors': [],
365 },
366 ],
367 }
368 ]
369 }
370 }
371 }
372 self.maxDiff = None
373 self.assertEqual(expected, result)
374
danduong970b79a8a2015-07-11 01:04:19375 def test_FieldTrialToStructMain(self):
Nikunj Bhagatf958b792019-03-20 19:13:30376
377 schema = self.FullRelativePath(
Alexei Svitkineac3352b2017-10-06 13:46:30378 '/../../components/variations/field_trial_config/'
379 'field_trial_testing_config_schema.json')
Nikunj Bhagatf958b792019-03-20 19:13:30380 unittest_data_dir = self.FullRelativePath('/unittest_data/')
robliao6df95232016-08-23 19:56:50381 test_output_filename = 'test_output'
danduong970b79a8a2015-07-11 01:04:19382 fieldtrial_to_struct.main([
383 '--schema=' + schema,
robliao6df95232016-08-23 19:56:50384 '--output=' + test_output_filename,
Paul Millerad77b7892018-07-11 20:07:43385 '--platform=windows',
danduong970b79a8a2015-07-11 01:04:19386 '--year=2015',
Alexei Svitkineac3352b2017-10-06 13:46:30387 unittest_data_dir + 'test_config.json'
danduong970b79a8a2015-07-11 01:04:19388 ])
robliao6df95232016-08-23 19:56:50389 header_filename = test_output_filename + '.h'
danduong970b79a8a2015-07-11 01:04:19390 with open(header_filename, 'r') as header:
391 test_header = header.read()
Alexei Svitkineac3352b2017-10-06 13:46:30392 with open(unittest_data_dir + 'expected_output.h', 'r') as expected:
danduong970b79a8a2015-07-11 01:04:19393 expected_header = expected.read()
394 self.assertEqual(expected_header, test_header)
395 os.unlink(header_filename)
396
robliao6df95232016-08-23 19:56:50397 cc_filename = test_output_filename + '.cc'
danduong970b79a8a2015-07-11 01:04:19398 with open(cc_filename, 'r') as cc:
399 test_cc = cc.read()
Alexei Svitkineac3352b2017-10-06 13:46:30400 with open(unittest_data_dir + 'expected_output.cc', 'r') as expected:
danduong970b79a8a2015-07-11 01:04:19401 expected_cc = expected.read()
402 self.assertEqual(expected_cc, test_cc)
403 os.unlink(cc_filename)
404
405if __name__ == '__main__':
406 unittest.main()