OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /** @const {string} Path to source root. */ | |
6 var ROOT_PATH = '../../../../'; | |
7 | |
8 // Polymer BrowserTest fixture. | |
9 GEN_INCLUDE( | |
10 [ROOT_PATH + 'chrome/test/data/webui/polymer_browser_test_base.js']); | |
11 | |
Dan Beam
2015/10/10 01:04:19
one less \n
fhorschig
2015/10/13 16:29:11
Done.
| |
12 | |
13 /** @type {Object} * @const */ | |
14 var GROUP = { | |
15 ADMIN: 'fullAdminAccess', | |
16 SECURITY: 'systemSecurity' | |
17 }; | |
18 | |
19 /** @type {Array.string} @const */ | |
20 var GROUP_NAMES = Object.keys(GROUP).map(key => GROUP[key]); | |
21 | |
22 /** @type {Object} @const */ | |
23 var POLICY = { | |
24 SAMLE_A: { | |
Dan Beam
2015/10/10 01:04:19
is this supposed to be SAMPLE_{A,B}?
fhorschig
2015/10/13 16:29:11
Thanks!
| |
25 NAME: 'RandomPolicy', | |
26 TAGS: [GROUP.ADMIN, GROUP.SECURITY] | |
27 }, | |
28 SAMLE_B: { | |
29 NAME: 'AnotherPolicy', | |
30 TAGS: [GROUP.ADMIN] | |
31 } | |
32 }; | |
33 | |
34 /** | |
35 * Test fixture for policy ui WebUI testing. | |
36 * @constructor | |
37 * @extends {PolymerTest} | |
38 */ | |
39 function PolicyUiMaterialWebUITest() { | |
40 } | |
41 | |
42 PolicyUiMaterialWebUITest.prototype = { | |
43 __proto__: PolymerTest.prototype, | |
44 | |
45 /** @override */ | |
46 browsePreload: 'chrome://md-policy', | |
47 | |
48 /** @override */ | |
49 runAccessibilityChecks: true, | |
50 | |
51 /** @override */ | |
52 accessibilityIssuesAreErrors: true, | |
53 | |
54 /** | |
55 * Generate a real C++ class; don't typedef. | |
56 * @type {?string} | |
57 * @override | |
58 */ | |
59 typedefCppFixture: null, | |
60 | |
61 /** | |
62 * Set extra libraries relative to source root. | |
63 * @override | |
64 */ | |
65 extraLibraries: PolymerTest.getLibraries(ROOT_PATH), | |
66 | |
67 /** | |
68 * Skipped tags when looking for Objects in DOM. | |
69 * @type {Array.string} | |
70 * @const | |
71 */ | |
72 IGNORED_TAGS: ['DOM-MODULE', 'SCRIPT', 'STYLE', 'TEMPLATE', 'HEAD'], | |
73 | |
74 /** | |
75 * Creates object as send by chromium. Can include only a subset of policies. | |
76 * @param {Array.Object=} policies Defaults to all policies. | |
Dan Beam
2015/10/10 01:04:19
opt_policies
fhorschig
2015/10/13 16:29:11
Done.
| |
77 * @return {Object} Name object with policies within 'chromePolicyNames' key. | |
78 */ | |
79 createPolicyNames: function(policies) { | |
80 policies = policies || Object.keys(POLICY).map(key => POLICY[key]); | |
81 var data = { chromePolicyNames : {} }; | |
Dan Beam
2015/10/10 01:04:19
{chromePolicyNames: {}} (no spaces)
fhorschig
2015/10/13 16:29:12
Done.
| |
82 for (var i = policies.length - 1; i >= 0; i--) { | |
83 data.chromePolicyNames[policies[i].NAME] = policies[i].TAGS; | |
84 }; | |
Dan Beam
2015/10/10 01:04:19
}; -> }
fhorschig
2015/10/13 16:29:11
Removed.
| |
85 return data; | |
86 }, | |
87 | |
88 // TODO(fhorschig): Remove as soon as some querying tool handles shadow DOM. | |
89 /** | |
90 * Selects #id, .class or tag while ignoring dom-module and searching | |
91 * shadow-dom. | |
92 * @private | |
93 * @param {string} selector | |
94 * @param {Element=} root Defaults to document | |
Dan Beam
2015/10/10 01:04:19
opt_root
fhorschig
2015/10/13 16:29:12
Done.
| |
95 * @return {Array.Element} list of found objects | |
96 */ | |
97 queryObjectDeep_: function(selector, root) { | |
Dan Beam
2015/10/10 01:04:19
why not just actually use /deep/?
fhorschig
2015/10/13 16:29:11
It's deprecated [1] and searched DOM-MODULES (whic
| |
98 root = root || document; | |
99 if (this.IGNORED_TAGS.indexOf(root.tagName) != -1) { | |
100 return []; | |
101 } | |
Dan Beam
2015/10/10 01:04:19
no curlies
fhorschig
2015/10/13 16:29:12
Done.
| |
102 var result = []; | |
103 if (root.tagName == selector.toUpperCase()) { | |
104 result.push(root); | |
105 } | |
106 if (selector.startsWith('.') && | |
107 root.classList && | |
108 root.classList.contains(selector.slice(1))) { | |
Dan Beam
2015/10/10 01:04:19
root.matches && root.matches(selector)?
fhorschig
2015/10/13 16:29:12
Nice!
| |
109 result.push(root); | |
110 } | |
111 if (selector.startsWith('#') && root.id == selector.slice(1)) | |
112 result.push(root); | |
Dan Beam
2015/10/10 01:04:19
would also be handled by matches
fhorschig
2015/10/13 16:29:12
Exactly what I was looking for.
| |
113 for (var i = 0; i < root.children.length; ++i) | |
114 result = result.concat(this.queryObjectDeep_(selector, root.children[i])); | |
Dan Beam
2015/10/10 01:04:19
nit: \n
fhorschig
2015/10/13 16:29:12
Done.
| |
115 if (root.shadowRoot) { | |
116 for (var i = 0; i < root.shadowRoot.children.length; ++i) { | |
117 result = result.concat(this.queryObjectDeep_( | |
118 selector, root.shadowRoot.children[i])); | |
119 } | |
120 } | |
121 return result; | |
122 }, | |
123 | |
124 /** | |
125 * Selects #id, .class or tag while ignoring dom-module and searching | |
126 * shadow-dom. Allows nested selectors. | |
127 * @param {string} selector | |
128 * @param {Element=} root Defaults to document | |
129 * @return {Array.Element} list of found objects | |
130 */ | |
131 listOf: function(selector, root) { | |
132 var selectors = selector.split(' '); | |
133 var containers = [root || document]; | |
134 var newContainers = []; | |
135 while (selector = selectors.shift()) { | |
Dan Beam
2015/10/10 01:04:19
nit: selector.split(' ').forEach(function() { ...
fhorschig
2015/10/13 16:29:12
Done.
| |
136 newContainers = []; | |
137 for (var i = 0; i < containers.length; ++i) { | |
138 newContainers = newContainers.concat( | |
139 this.queryObjectDeep_(selector, containers[i])); | |
140 } | |
141 containers = newContainers; | |
142 } | |
143 return containers; | |
144 }, | |
145 | |
146 /** | |
147 * Tries to find at least one of the element matching the selector. | |
148 * @param {string} selector | |
149 * @param {Element=} root Defaults to document | |
Dan Beam
2015/10/10 01:04:19
opt_root
fhorschig
2015/10/13 16:29:12
Done.
| |
150 * @return {Array.Element} list of found objects | |
151 */ | |
152 exists: function(selector, root) { | |
153 return this.listOf(selector, root).length > 0; | |
154 }, | |
155 | |
156 /** | |
157 * Tries to find a group by name within the first POLICY-UI tag. | |
158 * @param {string} name Name of the group | |
159 * @return {?Element} Group with name matching |name|. | |
160 */ | |
161 group: function(name) { | |
162 var ui = this.listOf('policy-ui')[0]; | |
163 for (var i = ui.$.groups.children.length - 1; i >= 0; i--) { | |
164 if (ui.$.groups.children[i].riskTag == name) | |
Dan Beam
2015/10/10 01:04:19
if you use reflectToAttribute, you can just
query
fhorschig
2015/10/13 16:29:12
Good idea.
| |
165 return ui.$.groups.children[i]; | |
166 } | |
167 return null; | |
168 }, | |
169 | |
170 /** | |
171 * Returns if there is a group with the given |groupName| whose content | |
172 * contains the given |text|. | |
173 * @param {string} groupName | |
174 * @param {string} text | |
175 * @return {boolean} Returns false when there is no group named |groupName| or | |
176 * if the group doesn't contain the string. | |
177 */ | |
178 groupContainsText: function(groupName, text) { | |
179 var group = this.group(groupName); | |
180 if (!group) | |
181 return false; | |
182 return group.$.content.textContent.indexOf(text) != 1; | |
183 } | |
184 }; | |
185 | |
186 GEN('#include "chrome/test/data/webui/policy_ui_material.h"'); | |
Dan Beam
2015/10/10 01:04:19
move to top
fhorschig
2015/10/13 16:29:12
Done.
| |
187 | |
188 // Test some basic assumptions about the print preview WebUI. | |
189 TEST_F('PolicyUiMaterialWebUITest', 'TestPolicyLayout', function() { | |
190 var createPolicyNames = | |
191 this.createPolicyNames.bind(PolicyUiMaterialWebUITest.prototype); | |
192 var exists = this.exists.bind(PolicyUiMaterialWebUITest.prototype); | |
Dan Beam
2015/10/10 01:04:19
can't you just bind(this)?
fhorschig
2015/10/13 16:29:12
Done.
| |
193 var groupContainsText = | |
194 this.groupContainsText.bind(PolicyUiMaterialWebUITest.prototype); | |
195 var listOf = this.listOf.bind(PolicyUiMaterialWebUITest.prototype); | |
196 | |
197 suite('Layout', function() { | |
198 test('has one PolicyUi', function() { | |
199 expectTrue(exists('policy-ui')); | |
200 }); | |
201 | |
202 test('PolicyUi has paper-cards', function() { | |
203 expectTrue(exists('policy-ui paper-card')); | |
204 }); | |
205 | |
206 test('PolicyUi contains introduction', function() { | |
207 expectTrue(exists('policy-ui #introduction')); | |
208 }); | |
209 }); | |
210 | |
211 suite('Functionality', function() { | |
212 setup(function() { | |
213 this.policyUi = listOf('policy-ui')[0]; | |
214 }); | |
215 | |
216 test('PolicyUi loads introduction title', function() { | |
217 expectTrue(this.policyUi.$.introduction.heading != ''); | |
218 }); | |
219 | |
220 test('PolicyUi loads at least one group', function() { | |
221 this.policyUi.setPolicyGroups(GROUP_NAMES); | |
222 this.policyUi.setPolicyNames(createPolicyNames([POLICY.SAMLE_A])); | |
223 expectTrue(exists('policy-ui #groups policy-group')); | |
224 }); | |
225 | |
226 test('PolicyUi provides same methods as internal page', function() { | |
227 expectTrue(typeof this.policyUi.setPolicyGroups === 'function'); | |
228 expectTrue(typeof this.policyUi.setPolicyNames === 'function'); | |
229 expectTrue(typeof this.policyUi.setPolicyValues === 'function'); | |
230 expectTrue(typeof this.policyUi.setStatus === 'function'); | |
231 }); | |
232 | |
233 test('PolicyUi clears groups when names are loaded', function() { | |
234 this.policyUi.setPolicyGroups(GROUP_NAMES); | |
235 this.policyUi.setPolicyNames(createPolicyNames()); | |
236 | |
237 expectTrue(listOf('policy-ui #groups policy-group').length == 2); | |
238 | |
239 this.policyUi.setPolicyGroups([POLICY.SAMLE_B.TAGS[0]]); | |
240 this.policyUi.setPolicyNames(createPolicyNames([POLICY.SAMLE_B])); | |
241 | |
242 expectTrue(listOf('policy-ui #groups policy-group').length == 1); | |
243 }); | |
244 | |
245 test('policies with multiple tags are in multiple groups', function() { | |
246 this.policyUi.setPolicyGroups(GROUP_NAMES); | |
247 this.policyUi.setPolicyNames(createPolicyNames()); | |
248 expectTrue(groupContainsText(GROUP.ADMIN, POLICY.SAMLE_A.NAME)); | |
249 expectTrue(groupContainsText(GROUP.SECURITY, POLICY.SAMLE_A.NAME)); | |
250 }); | |
251 }); | |
252 | |
253 mocha.run(); | |
254 }); | |
OLD | NEW |