OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import unittest | 5 import unittest |
6 | 6 |
7 import fieldtrial_to_struct | 7 import fieldtrial_to_struct |
8 import os | 8 import os |
9 | 9 |
10 | 10 |
11 class FieldTrialToStruct(unittest.TestCase): | 11 class FieldTrialToStruct(unittest.TestCase): |
12 | 12 |
13 def test_FieldTrialToDescription(self): | 13 def test_FieldTrialToDescription(self): |
14 config = { | 14 config = { |
15 'Trial1': [ | 15 'Trial1': [ |
16 { | 16 { |
17 'group_name': 'Group1', | 17 'platforms': ['win'], |
18 'params': { | 18 'experiments': [ |
19 'x': '1', | 19 { |
20 'y': '2' | 20 'name': 'Group1', |
21 }, | 21 'params': { |
22 'enable_features': ['A', 'B'], | 22 'x': '1', |
23 'disable_features': ['C'] | 23 'y': '2' |
24 }, | 24 }, |
25 { | 25 'enable_features': ['A', 'B'], |
26 'group_name': 'Group2', | 26 'disable_features': ['C'] |
27 'params': { | 27 }, |
28 'x': '3', | 28 { |
29 'y': '4' | 29 'name': 'Group2', |
30 }, | 30 'params': { |
31 'enable_features': ['D', 'E'], | 31 'x': '3', |
32 'disable_features': ['F'] | 32 'y': '4' |
| 33 }, |
| 34 'enable_features': ['D', 'E'], |
| 35 'disable_features': ['F'] |
| 36 }, |
| 37 ] |
33 } | 38 } |
34 ], | 39 ], |
35 'Trial2': [{'group_name': 'OtherGroup'}] | 40 'Trial2': [ |
| 41 { |
| 42 'platforms': ['win'], |
| 43 'experiments': [{'name': 'OtherGroup'}] |
| 44 } |
| 45 ] |
36 } | 46 } |
37 result = fieldtrial_to_struct._FieldTrialConfigToDescription(config) | 47 result = fieldtrial_to_struct._FieldTrialConfigToDescription(config, 'win') |
38 expected = { | 48 expected = { |
39 'elements': { | 49 'elements': { |
40 'kFieldTrialConfig': { | 50 'kFieldTrialConfig': { |
41 'trials': [ | 51 'trials': [ |
42 { | 52 { |
43 'name': 'Trial1', | 53 'name': 'Trial1', |
44 'groups': [ | 54 'groups': [ |
45 { | 55 { |
46 'name': 'Group1', | 56 'name': 'Group1', |
47 'params': [ | 57 'params': [ |
(...skipping 18 matching lines...) Expand all Loading... |
66 'name': 'Trial2', | 76 'name': 'Trial2', |
67 'groups': [{'name': 'OtherGroup'}] | 77 'groups': [{'name': 'OtherGroup'}] |
68 }, | 78 }, |
69 ] | 79 ] |
70 } | 80 } |
71 } | 81 } |
72 } | 82 } |
73 self.maxDiff = None | 83 self.maxDiff = None |
74 self.assertEqual(expected, result) | 84 self.assertEqual(expected, result) |
75 | 85 |
| 86 _MULTIPLE_PLATFORM_CONFIG = { |
| 87 'Trial1': [ |
| 88 { |
| 89 'platforms': ['win', 'ios'], |
| 90 'experiments': [ |
| 91 { |
| 92 'name': 'Group1', |
| 93 'params': { |
| 94 'x': '1', |
| 95 'y': '2' |
| 96 }, |
| 97 'enable_features': ['A', 'B'], |
| 98 'disable_features': ['C'] |
| 99 }, |
| 100 { |
| 101 'name': 'Group2', |
| 102 'params': { |
| 103 'x': '3', |
| 104 'y': '4' |
| 105 }, |
| 106 'enable_features': ['D', 'E'], |
| 107 'disable_features': ['F'] |
| 108 } |
| 109 ] |
| 110 }, |
| 111 { |
| 112 'platforms': ['ios'], |
| 113 'experiments': [ |
| 114 { |
| 115 'name': 'IOSOnly' |
| 116 } |
| 117 ] |
| 118 }, |
| 119 ], |
| 120 'Trial2': [ |
| 121 { |
| 122 'platforms': ['win', 'mac'], |
| 123 'experiments': [{'name': 'OtherGroup'}] |
| 124 } |
| 125 ] |
| 126 } |
| 127 |
| 128 def test_FieldTrialToDescriptionMultipleSinglePlatformMultipleTrial(self): |
| 129 result = fieldtrial_to_struct._FieldTrialConfigToDescription( |
| 130 self._MULTIPLE_PLATFORM_CONFIG, 'ios') |
| 131 expected = { |
| 132 'elements': { |
| 133 'kFieldTrialConfig': { |
| 134 'trials': [ |
| 135 { |
| 136 'name': 'Trial1', |
| 137 'groups': [ |
| 138 { |
| 139 'name': 'Group1', |
| 140 'params': [ |
| 141 {'key': 'x', 'value': '1'}, |
| 142 {'key': 'y', 'value': '2'} |
| 143 ], |
| 144 'enable_features': ['A', 'B'], |
| 145 'disable_features': ['C'] |
| 146 }, |
| 147 { |
| 148 'name': 'Group2', |
| 149 'params': [ |
| 150 {'key': 'x', 'value': '3'}, |
| 151 {'key': 'y', 'value': '4'} |
| 152 ], |
| 153 'enable_features': ['D', 'E'], |
| 154 'disable_features': ['F'] |
| 155 }, |
| 156 { |
| 157 'name': 'IOSOnly' |
| 158 }, |
| 159 ], |
| 160 }, |
| 161 ] |
| 162 } |
| 163 } |
| 164 } |
| 165 self.maxDiff = None |
| 166 self.assertEqual(expected, result) |
| 167 |
| 168 def test_FieldTrialToDescriptionMultipleSinglePlatformSingleTrial(self): |
| 169 result = fieldtrial_to_struct._FieldTrialConfigToDescription( |
| 170 self._MULTIPLE_PLATFORM_CONFIG, 'mac') |
| 171 expected = { |
| 172 'elements': { |
| 173 'kFieldTrialConfig': { |
| 174 'trials': [ |
| 175 { |
| 176 'name': 'Trial2', |
| 177 'groups': [ |
| 178 { |
| 179 'name': 'OtherGroup', |
| 180 }, |
| 181 ], |
| 182 }, |
| 183 ] |
| 184 } |
| 185 } |
| 186 } |
| 187 self.maxDiff = None |
| 188 self.assertEqual(expected, result) |
| 189 |
76 def test_FieldTrialToStructMain(self): | 190 def test_FieldTrialToStructMain(self): |
77 schema = ( | 191 schema = ( |
78 '../../chrome/common/variations/fieldtrial_testing_config_schema.json') | 192 '../../chrome/common/variations/fieldtrial_testing_config_schema.json') |
79 test_output_filename = 'test_output' | 193 test_output_filename = 'test_output' |
80 fieldtrial_to_struct.main([ | 194 fieldtrial_to_struct.main([ |
81 '--schema=' + schema, | 195 '--schema=' + schema, |
82 '--output=' + test_output_filename, | 196 '--output=' + test_output_filename, |
| 197 '--platform=win', |
83 '--year=2015', | 198 '--year=2015', |
84 'unittest_data/test_config.json' | 199 'unittest_data/test_config.json' |
85 ]) | 200 ]) |
86 header_filename = test_output_filename + '.h' | 201 header_filename = test_output_filename + '.h' |
87 with open(header_filename, 'r') as header: | 202 with open(header_filename, 'r') as header: |
88 test_header = header.read() | 203 test_header = header.read() |
89 with open('unittest_data/expected_output.h', 'r') as expected: | 204 with open('unittest_data/expected_output.h', 'r') as expected: |
90 expected_header = expected.read() | 205 expected_header = expected.read() |
91 self.assertEqual(expected_header, test_header) | 206 self.assertEqual(expected_header, test_header) |
92 os.unlink(header_filename) | 207 os.unlink(header_filename) |
93 | 208 |
94 cc_filename = test_output_filename + '.cc' | 209 cc_filename = test_output_filename + '.cc' |
95 with open(cc_filename, 'r') as cc: | 210 with open(cc_filename, 'r') as cc: |
96 test_cc = cc.read() | 211 test_cc = cc.read() |
97 with open('unittest_data/expected_output.cc', 'r') as expected: | 212 with open('unittest_data/expected_output.cc', 'r') as expected: |
98 expected_cc = expected.read() | 213 expected_cc = expected.read() |
99 self.assertEqual(expected_cc, test_cc) | 214 self.assertEqual(expected_cc, test_cc) |
100 os.unlink(cc_filename) | 215 os.unlink(cc_filename) |
101 | 216 |
102 if __name__ == '__main__': | 217 if __name__ == '__main__': |
103 unittest.main() | 218 unittest.main() |
OLD | NEW |