OLD | NEW |
---|---|
(Empty) | |
1 /** @const {string} Path to source root. */ | |
Dan Beam
2015/10/14 21:44:00
needs a copyright
fhorschig
2015/10/15 17:38:29
Done.
| |
2 var ROOT_PATH = '../../../../'; | |
3 | |
4 // Polymer BrowserTest fixture. | |
5 GEN_INCLUDE( | |
6 [ROOT_PATH + 'chrome/test/data/webui/polymer_browser_test_base.js']); | |
7 | |
8 GEN('#include "base/macros.h"'); | |
Dan Beam
2015/10/14 21:44:00
why is this needed here?
fhorschig
2015/10/15 17:38:30
Sorry, just added this on the wrong place.
| |
9 GEN('#include "chrome/test/data/webui/policy_ui_material.h"'); | |
10 | |
11 /** @type {Object} * @const */ | |
12 var GROUP = { | |
13 ADMIN: 'fullAdminAccess', | |
14 SECURITY: 'systemSecurity' | |
15 }; | |
16 | |
17 /** @type {Array.string} @const */ | |
Dan Beam
2015/10/14 21:44:00
!Array<string>
fhorschig
2015/10/15 17:38:29
Done.
| |
18 var GROUP_NAMES = Object.keys(GROUP).map(key => GROUP[key]); | |
19 | |
20 /** @type {Object} @const */ | |
21 var POLICY = { | |
22 SAMPLE_A: { | |
23 NAME: 'RandomPolicy', | |
24 TAGS: [GROUP.ADMIN, GROUP.SECURITY] | |
25 }, | |
26 SAMPLE_B: { | |
27 NAME: 'AnotherPolicy', | |
28 TAGS: [GROUP.ADMIN] | |
29 } | |
30 }; | |
31 | |
32 /** | |
33 * Test fixture for policy ui WebUI testing. | |
34 * @constructor | |
35 * @extends {PolymerTest} | |
36 */ | |
37 function PolicyUiMaterialWebUITest() { | |
38 } | |
39 | |
40 PolicyUiMaterialWebUITest.prototype = { | |
41 __proto__: PolymerTest.prototype, | |
42 | |
43 /** @override */ | |
44 browsePreload: 'chrome://md-policy', | |
45 | |
46 /** @override */ | |
47 runAccessibilityChecks: true, | |
48 | |
49 /** @override */ | |
50 accessibilityIssuesAreErrors: true, | |
51 | |
52 /** | |
53 * Generate a real C++ class; don't typedef. | |
Dan Beam
2015/10/14 21:44:00
the CppFixture is always a C++ class, no?
I think
fhorschig
2015/10/15 17:38:29
Done.
| |
54 * @type {?string} | |
55 * @override | |
56 */ | |
57 typedefCppFixture: null, | |
58 | |
59 /** | |
60 * Set extra libraries relative to source root. | |
61 * @override | |
62 */ | |
63 extraLibraries: PolymerTest.getLibraries(ROOT_PATH), | |
64 | |
65 /** | |
66 * Skipped tags when looking for Objects in DOM. | |
67 * @type {Array.string} | |
Dan Beam
2015/10/14 21:44:00
!Array<string>
fhorschig
2015/10/15 17:38:29
Done.
| |
68 * @const | |
69 */ | |
70 IGNORED_TAGS: ['DOM-MODULE', 'SCRIPT', 'STYLE', 'TEMPLATE', 'HEAD'], | |
Dan Beam
2015/10/14 21:44:00
can you make this private and static (i.e. PolicyU
fhorschig
2015/10/15 17:38:29
Done.
| |
71 | |
72 /** | |
73 * Creates object as send by chromium. Can include only a subset of policies. | |
74 * @param {Array.Object=} opt_policies Defaults to all policies. | |
75 * @return {Object} Name object with policies within 'chromePolicyNames' key. | |
76 */ | |
77 createPolicyNames: function(opt_policies) { | |
78 opt_policies = opt_policies || Object.keys(POLICY).map(key => POLICY[key]); | |
79 var data = {chromePolicyNames: {}}; | |
80 for (var i = opt_policies.length - 1; i >= 0; i--) | |
81 data.chromePolicyNames[opt_policies[i].NAME] = opt_policies[i].TAGS; | |
82 return data; | |
83 }, | |
84 | |
85 // TODO(fhorschig): Remove as soon as some querying tool handles shadow DOM. | |
86 /** | |
87 * Selects #id, .class or tag while ignoring dom-module and searching | |
88 * shadow-dom. | |
89 * @private | |
90 * @param {string} selector | |
91 * @param {Element=} opt_root Defaults to document | |
92 * @return {Array.Element} list of found objects | |
93 */ | |
94 queryObjectDeep_: function(selector, opt_root) { | |
95 opt_root = opt_root || document; | |
96 if (this.IGNORED_TAGS.indexOf(opt_root.tagName) != -1) | |
97 return []; | |
98 var result = []; | |
99 if (opt_root.matches && opt_root.matches(selector)) | |
esprehn
2015/10/14 21:35:34
This is going to be massively slower than just cal
fhorschig
2015/10/15 17:38:29
Changed the way I test, so I don't need to deep se
| |
100 result.push(opt_root); | |
101 for (var i = 0; i < opt_root.children.length; ++i) { | |
102 result = result.concat(this.queryObjectDeep_( | |
103 selector, opt_root.children[i])); | |
104 } | |
105 if (opt_root.shadowRoot) { | |
106 for (var i = 0; i < opt_root.shadowRoot.children.length; ++i) { | |
esprehn
2015/10/14 21:35:34
don't use .children (ever), use .firstElementChild
fhorschig
2015/10/15 17:38:29
Done.
| |
107 result = result.concat(this.queryObjectDeep_( | |
108 selector, opt_root.shadowRoot.children[i])); | |
109 } | |
110 } | |
111 return result; | |
112 }, | |
113 | |
114 /** | |
115 * Selects #id, .class or tag while ignoring dom-module and searching | |
116 * shadow-dom. Allows nested selectors. | |
117 * @param {string} selector | |
118 * @param {Element=} opt_root Defaults to document | |
119 * @return {Array.Element} list of found objects | |
120 */ | |
121 listOf: function(selector, opt_root) { | |
122 var containers = [opt_root || document]; | |
123 var newContainers = []; | |
124 selector.split(' ').forEach((function(sel) { | |
esprehn
2015/10/14 21:35:34
This doesn't make sense, selectors aren't separate
fhorschig
2015/10/15 17:38:29
This is absolutely true. Deleted. Currently, I onl
| |
125 newContainers = []; | |
126 for (var i = 0; i < containers.length; ++i) { | |
127 newContainers = newContainers.concat( | |
128 this.queryObjectDeep_(sel, containers[i])); | |
129 } | |
130 containers = newContainers; | |
131 }).bind(this)); | |
132 return containers; | |
133 }, | |
134 | |
135 /** | |
136 * Tries to find at least one of the element matching the selector. | |
137 * @param {string} selector | |
138 * @param {Element=} opt_root Defaults to document | |
139 * @return {Array.Element} list of found objects | |
140 */ | |
141 exists: function(selector, opt_root) { | |
142 return this.listOf(selector, opt_root).length > 0; | |
esprehn
2015/10/14 21:35:34
this is super slow, you're querying for *all* to c
fhorschig
2015/10/15 17:38:30
querySelector handles this now. Should be fine.
| |
143 }, | |
144 | |
145 /** | |
146 * Tries to find a group by name within the first POLICY-UI tag. | |
147 * @param {string} name Name of the group | |
148 * @return {?Element} Group with name matching |name|. | |
149 */ | |
150 group: function(name) { | |
151 var ui = this.listOf('policy-ui')[0]; | |
esprehn
2015/10/14 21:35:34
again you query for *all* to get the first one, do
fhorschig
2015/10/15 17:38:29
Now it's querySelector, again. Thanks for pointing
| |
152 return ui.$.groups.querySelector('[risk-tag=' + name + ']'); | |
153 }, | |
154 | |
155 /** | |
156 * Returns if there is a group with the given |groupName| whose content | |
157 * contains the given |text|. | |
158 * @param {string} groupName | |
159 * @param {string} text | |
160 * @return {boolean} Returns false when there is no group named |groupName| or | |
161 * if the group doesn't contain the string. | |
162 */ | |
163 groupContainsText: function(groupName, text) { | |
164 var group = this.group(groupName); | |
165 if (!group) | |
166 return false; | |
167 return group.$.content.textContent.indexOf(text) != 1; | |
168 } | |
169 }; | |
170 | |
171 // Test some basic assumptions about the print preview WebUI. | |
172 TEST_F('PolicyUiMaterialWebUITest', 'TestPolicyLayout', function() { | |
173 var createPolicyNames = this.createPolicyNames.bind(this); | |
174 var exists = this.exists.bind(this); | |
175 var groupContainsText = this.groupContainsText.bind(this); | |
176 var listOf = this.listOf.bind(this); | |
177 | |
178 suite('Layout', function() { | |
179 test('has one PolicyUi', function() { | |
180 expectTrue(exists('policy-ui')); | |
181 }); | |
182 | |
183 test('PolicyUi has paper-cards', function() { | |
184 expectTrue(exists('policy-ui paper-card')); | |
185 }); | |
186 | |
187 test('PolicyUi contains introduction', function() { | |
188 expectTrue(exists('policy-ui #introduction')); | |
189 }); | |
190 }); | |
191 | |
192 suite('Functionality', function() { | |
193 setup(function() { | |
194 this.policyUi = listOf('policy-ui')[0]; | |
195 }); | |
196 | |
197 test('PolicyUi loads introduction title', function() { | |
198 expectTrue(this.policyUi.$.introduction.heading != ''); | |
199 }); | |
200 | |
201 test('PolicyUi loads at least one group', function() { | |
202 this.policyUi.setPolicyGroups(GROUP_NAMES); | |
203 this.policyUi.setPolicyNames(createPolicyNames([POLICY.SAMPLE_A])); | |
204 expectTrue(exists('policy-ui #groups policy-group')); | |
205 }); | |
206 | |
207 test('PolicyUi provides same methods as internal page', function() { | |
208 expectTrue(typeof this.policyUi.setPolicyGroups === 'function'); | |
209 expectTrue(typeof this.policyUi.setPolicyNames === 'function'); | |
210 expectTrue(typeof this.policyUi.setPolicyValues === 'function'); | |
211 expectTrue(typeof this.policyUi.setStatus === 'function'); | |
212 }); | |
213 | |
214 test('PolicyUi clears groups when names are loaded', function() { | |
215 this.policyUi.setPolicyGroups(GROUP_NAMES); | |
216 this.policyUi.setPolicyNames(createPolicyNames()); | |
217 | |
218 expectTrue(listOf('policy-ui #groups policy-group').length == 2); | |
219 | |
220 this.policyUi.setPolicyGroups([POLICY.SAMPLE_B.TAGS[0]]); | |
221 this.policyUi.setPolicyNames(createPolicyNames([POLICY.SAMPLE_B])); | |
222 | |
223 expectTrue(listOf('policy-ui #groups policy-group').length == 1); | |
224 }); | |
225 | |
226 test('policies with multiple tags are in multiple groups', function() { | |
227 this.policyUi.setPolicyGroups(GROUP_NAMES); | |
228 this.policyUi.setPolicyNames(createPolicyNames()); | |
229 expectTrue(groupContainsText(GROUP.ADMIN, POLICY.SAMPLE_A.NAME)); | |
230 expectTrue(groupContainsText(GROUP.SECURITY, POLICY.SAMPLE_A.NAME)); | |
231 }); | |
232 }); | |
233 | |
234 mocha.run(); | |
235 }); | |
OLD | NEW |