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_util | 7 import fieldtrial_util |
8 import os | 8 import os |
9 import tempfile | 9 import tempfile |
10 | 10 |
11 | 11 |
12 class FieldTrialUtilUnittest(unittest.TestCase): | 12 class FieldTrialUtilUnittest(unittest.TestCase): |
13 | 13 |
14 def runGenerateArgs(self, config, platform): | 14 def runGenerateArgs(self, config): |
15 result = None | 15 result = None |
16 with tempfile.NamedTemporaryFile('w', delete=False) as base_file: | 16 with tempfile.NamedTemporaryFile('w', delete=False) as base_file: |
17 try: | 17 try: |
18 base_file.write(config) | 18 base_file.write(config) |
19 base_file.close() | 19 base_file.close() |
20 result = fieldtrial_util.GenerateArgs(base_file.name, platform) | 20 result = fieldtrial_util.GenerateArgs(base_file.name) |
21 finally: | 21 finally: |
22 os.unlink(base_file.name) | 22 os.unlink(base_file.name) |
23 return result | 23 return result |
24 | 24 |
25 def test_GenArgsEmptyPaths(self): | 25 def test_GenArgsEmptyPaths(self): |
26 args = fieldtrial_util.GenerateArgs('', 'linux') | 26 args = fieldtrial_util.GenerateArgs('') |
27 self.assertEqual([], args) | 27 self.assertEqual([], args) |
28 | 28 |
29 def test_GenArgsOneConfig(self): | 29 def test_GenArgsOneConfig(self): |
30 config = '''{ | 30 config = '''{ |
31 "BrowserBlackList": [ | 31 "BrowserBlackList": [ |
| 32 { "group_name": "Enabled" } |
| 33 ], |
| 34 "c": [ |
32 { | 35 { |
33 "platforms": ["win"], | 36 "group_name": "d.", |
34 "experiments": [{"name": "Enabled"}] | 37 "params": {"url": "http://www.google.com"}, |
| 38 "enable_features": ["x"], |
| 39 "disable_features": ["y"] |
35 } | 40 } |
36 ], | 41 ], |
37 "SimpleParams": [ | 42 "SimpleParams": [ |
38 { | 43 { |
39 "platforms": ["win"], | 44 "group_name": "Default", |
40 "experiments": [ | 45 "params": {"id": "abc"}, |
41 { | 46 "enable_features": ["a", "b"] |
42 "name": "Default", | |
43 "params": {"id": "abc"}, | |
44 "enable_features": ["a", "b"] | |
45 } | |
46 ] | |
47 } | |
48 ], | |
49 "c": [ | |
50 { | |
51 "platforms": ["win"], | |
52 "experiments": [ | |
53 { | |
54 "name": "d.", | |
55 "params": {"url": "http://www.google.com"}, | |
56 "enable_features": ["x"], | |
57 "disable_features": ["y"] | |
58 } | |
59 ] | |
60 } | 47 } |
61 ] | 48 ] |
62 }''' | 49 }''' |
63 result = self.runGenerateArgs(config, 'win') | 50 result = self.runGenerateArgs(config) |
64 self.assertEqual(['--force-fieldtrials=' | 51 self.assertEqual(['--force-fieldtrials=' |
65 'BrowserBlackList/Enabled/SimpleParams/Default/c/d.', | 52 'BrowserBlackList/Enabled/c/d./SimpleParams/Default', |
66 '--force-fieldtrial-params=' | 53 '--force-fieldtrial-params=' |
67 'SimpleParams.Default:id/abc,' | 54 'c.d%2E:url/http%3A%2F%2Fwww%2Egoogle%2Ecom,' |
68 'c.d%2E:url/http%3A%2F%2Fwww%2Egoogle%2Ecom', | 55 'SimpleParams.Default:id/abc', |
69 '--enable-features=a,b,x', | 56 '--enable-features=x,a,b', |
70 '--disable-features=y'], result) | 57 '--disable-features=y'], result) |
71 | 58 |
72 def test_DuplicateEnableFeatures(self): | 59 def test_DuplicateEnableFeatures(self): |
73 config = '''{ | 60 config = '''{ |
74 "X": [ | 61 "X": [ |
75 { | 62 { |
76 "platforms": ["win"], | 63 "group_name": "x", |
77 "experiments": [ | 64 "enable_features": ["x"] |
78 { | |
79 "name": "x", | |
80 "enable_features": ["x"] | |
81 } | |
82 ] | |
83 } | 65 } |
84 ], | 66 ], |
85 "Y": [ | 67 "Y": [ |
86 { | 68 { |
87 "platforms": ["win"], | 69 "group_name": "Default", |
88 "experiments": [ | 70 "enable_features": ["x", "y"] |
89 { | |
90 "name": "Default", | |
91 "enable_features": ["x", "y"] | |
92 } | |
93 ] | |
94 } | 71 } |
95 ] | 72 ] |
96 }''' | 73 }''' |
97 with self.assertRaises(Exception) as raised: | 74 with self.assertRaises(Exception) as raised: |
98 self.runGenerateArgs(config, 'win') | 75 self.runGenerateArgs(config) |
99 self.assertEqual('Duplicate feature(s) in enable_features: x', | 76 self.assertEqual('Duplicate feature(s) in enable_features: x', |
100 str(raised.exception)) | 77 str(raised.exception)) |
101 | 78 |
102 def test_DuplicateDisableFeatures(self): | 79 def test_DuplicateDisableFeatures(self): |
103 config = '''{ | 80 config = '''{ |
104 "X": [ | 81 "X": [ |
105 { | 82 { |
106 "platforms": ["win"], | 83 "group_name": "x", |
107 "experiments": [ | 84 "enable_features": ["y", "z"] |
108 { | |
109 "name": "x", | |
110 "enable_features": ["y", "z"] | |
111 } | |
112 ] | |
113 } | 85 } |
114 ], | 86 ], |
115 "Y": [ | 87 "Y": [ |
116 { | 88 { |
117 "platforms": ["win"], | 89 "group_name": "Default", |
118 "experiments": [ | 90 "enable_features": ["z", "x", "y"] |
119 { | |
120 "name": "Default", | |
121 "enable_features": ["z", "x", "y"] | |
122 } | |
123 ] | |
124 } | 91 } |
125 ] | 92 ] |
126 }''' | 93 }''' |
127 with self.assertRaises(Exception) as raised: | 94 with self.assertRaises(Exception) as raised: |
128 self.runGenerateArgs(config, 'win') | 95 self.runGenerateArgs(config) |
129 self.assertEqual('Duplicate feature(s) in enable_features: y, z', | 96 self.assertEqual('Duplicate feature(s) in enable_features: y, z', |
130 str(raised.exception)) | 97 str(raised.exception)) |
131 | 98 |
132 | 99 |
133 def test_DuplicateEnableDisable(self): | 100 def test_DuplicateEnableDisable(self): |
134 config = '''{ | 101 config = '''{ |
135 "X": [ | 102 "X": [ |
136 { | 103 { |
137 "platforms": ["win"], | 104 "group_name": "x", |
138 "experiments": [ | 105 "enable_features": ["x"] |
139 { | |
140 "name": "x", | |
141 "enable_features": ["x"] | |
142 } | |
143 ] | |
144 } | 106 } |
145 ], | 107 ], |
146 "Y": [ | 108 "Y": [ |
147 { | 109 { |
148 "platforms": ["win"], | 110 "group_name": "Default", |
149 "experiments": [ | 111 "disable_features": ["x", "y"] |
150 { | |
151 "name": "Default", | |
152 "disable_features": ["x", "y"] | |
153 } | |
154 ] | |
155 } | 112 } |
156 ] | 113 ] |
157 }''' | 114 }''' |
158 with self.assertRaises(Exception) as raised: | 115 with self.assertRaises(Exception) as raised: |
159 self.runGenerateArgs(config, 'win') | 116 self.runGenerateArgs(config) |
160 self.assertEqual('Conflicting features set as both enabled and disabled: x', | 117 self.assertEqual('Conflicting features set as both enabled and disabled: x', |
161 str(raised.exception)) | 118 str(raised.exception)) |
162 | 119 |
163 if __name__ == '__main__': | 120 if __name__ == '__main__': |
164 unittest.main() | 121 unittest.main() |
OLD | NEW |