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 | |
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: { | |
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. | |
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 : {} }; | |
82 for (var i = policies.length - 1; i >= 0; i--) { | |
83 data.chromePolicyNames[policies[i].NAME] = policies[i].TAGS; | |
84 }; | |
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 | |
95 * @return {Array.Element} list of found objects | |
96 */ | |
97 queryObjectDeep_: function(selector, root) { | |
98 root = root || document; | |
99 if (this.IGNORED_TAGS.indexOf(root.tagName) != -1) | |
100 return []; | |
101 var result = []; | |
102 if (root.tagName == selector.toUpperCase()) | |
103 result.push(root); | |
104 if (selector.startsWith('.') && | |
105 root.classList && | |
106 root.classList.contains(selector.slice(1))) | |
107 result.push(root); | |
stevenjb
2015/10/07 17:09:33
{}
fhorschig
2015/10/08 09:35:46
Done.
| |
108 if (selector.startsWith('#') && | |
109 root.id == selector.slice(1)) | |
stevenjb
2015/10/07 17:09:33
one line
fhorschig
2015/10/08 09:35:46
Done.
| |
110 result.push(root); | |
111 for (var i = 0; i < root.children.length; ++i) | |
112 result = result.concat(this.queryObjectDeep_(selector, root.children[i])); | |
113 if (root.shadowRoot) { | |
114 for (var i = 0; i < root.shadowRoot.children.length; ++i) | |
115 result = result.concat(this.queryObjectDeep_( | |
116 selector, root.shadowRoot.children[i])); | |
stevenjb
2015/10/07 17:09:33
{}
fhorschig
2015/10/08 09:35:46
Done.
| |
117 } | |
118 return result; | |
119 }, | |
120 | |
121 /** | |
122 * Selects #id, .class or tag while ignoring dom-module and searching | |
123 * shadow-dom. Allows nested selectors. | |
124 * @param {string} selector | |
125 * @param {Element=} root Defaults to document | |
126 * @return {Array.Element} list of found objects | |
127 */ | |
128 listOf: function(selector, root) { | |
129 var selectors = selector.split(' '); | |
130 var containers = [root || document]; | |
131 var newContainers = []; | |
132 while (selector = selectors.shift()) { | |
133 newContainers = []; | |
134 for (var i = 0; i < containers.length; ++i) { | |
135 newContainers = newContainers.concat( | |
136 this.queryObjectDeep_(selector, containers[i])); | |
137 } | |
138 containers = newContainers; | |
139 } | |
140 return containers; | |
141 }, | |
142 | |
143 /** | |
144 * Tries to find at least one of the element matching the selector. | |
145 * @param {string} selector | |
146 * @param {Element=} root Defaults to document | |
147 * @return {Array.Element} list of found objects | |
148 */ | |
149 exists: function(selector, root) { | |
150 return this.listOf(selector, root).length > 0; | |
151 }, | |
152 | |
153 /** | |
154 * Tries to find a group by name within the first POLICY-UI tag. | |
155 * @param {string} name Name of the group | |
156 * @return {?Element} Group with name matching |name|. | |
157 */ | |
158 group: function(name) { | |
159 var ui = this.listOf('policy-ui')[0]; | |
160 for (var i = ui.tagGroups.length - 1; i >= 0; i--) | |
161 if (ui.tagGroups[i].name == name) | |
162 return ui.tagGroups[i].element; | |
stevenjb
2015/10/07 17:09:33
P{
fhorschig
2015/10/08 09:35:46
Done.
| |
163 return null; | |
164 }, | |
165 | |
166 /** | |
167 * Returns if there is a group with the given |groupName| whose content | |
168 * contains the given |text|. | |
169 * @param {string} groupName | |
170 * @param {text} text | |
stevenjb
2015/10/07 17:09:33
This is confusing... where is |text| typedef'd? If
fhorschig
2015/10/08 09:35:46
Sorry, I meant string.
| |
171 * @return {boolean} Returns false when there is no group named |groupName| or | |
172 * if the group doesn't contain the string. | |
173 */ | |
174 groupContainsText: function(groupName, text) { | |
175 var group = this.group(groupName); | |
176 if (!group) | |
177 return false; | |
178 return group.$.content.textContent.indexOf('RandomPolicy') != 1; | |
179 } | |
180 }; | |
181 | |
182 GEN('#include "chrome/test/data/webui/policy_ui_material.h"'); | |
183 | |
184 // Test some basic assumptions about the print preview WebUI. | |
185 TEST_F('PolicyUiMaterialWebUITest', 'TestPolicyLayout', function() { | |
186 var createPolicyNames = | |
187 this.createPolicyNames.bind(PolicyUiMaterialWebUITest.prototype); | |
188 var exists = this.exists.bind(PolicyUiMaterialWebUITest.prototype); | |
189 var groupContainsText = | |
190 this.groupContainsText.bind(PolicyUiMaterialWebUITest.prototype); | |
191 var listOf = this.listOf.bind(PolicyUiMaterialWebUITest.prototype); | |
192 | |
193 suite('Layout', function() { | |
194 test('has one PolicyUi', function() { | |
195 expectTrue(exists('policy-ui')); | |
196 }); | |
197 | |
198 test('PolicyUi has paper-cards', function() { | |
199 expectTrue(exists('policy-ui paper-card')); | |
200 }); | |
201 | |
202 test('PolicyUi contains introduction', function() { | |
203 expectTrue(exists('policy-ui #introduction')); | |
204 }); | |
205 }); | |
206 | |
207 suite('Functionality', function() { | |
208 setup(function() { | |
209 this.policyUi = listOf('policy-ui')[0]; | |
210 }); | |
211 | |
212 test('PolicyUi loads introduction title', function() { | |
213 expectTrue(this.policyUi.$.introduction.heading != ''); | |
214 }); | |
215 | |
216 test('PolicyUi loads at least one group', function() { | |
217 this.policyUi.setPolicyGroups(GROUP_NAMES); | |
218 this.policyUi.setPolicyNames(createPolicyNames([POLICY.SAMLE_A])); | |
219 expectTrue(exists('policy-ui #groups policy-group')); | |
220 }); | |
221 | |
222 test('PolicyUi provides same methods as internal page', function() { | |
223 expectTrue(typeof this.policyUi.setPolicyGroups === 'function'); | |
224 expectTrue(typeof this.policyUi.setPolicyNames === 'function'); | |
225 expectTrue(typeof this.policyUi.setPolicyValues === 'function'); | |
226 expectTrue(typeof this.policyUi.setStatus === 'function'); | |
227 }); | |
228 | |
229 test('PolicyUi clears groups when names are loaded', function() { | |
230 this.policyUi.setPolicyGroups(GROUP_NAMES); | |
231 this.policyUi.setPolicyNames(createPolicyNames()); | |
232 | |
233 expectTrue(listOf('policy-ui #groups policy-group').length == 2); | |
234 | |
235 this.policyUi.setPolicyGroups([POLICY.SAMLE_B.TAGS[0]]); | |
236 this.policyUi.setPolicyNames(createPolicyNames([POLICY.SAMLE_B])); | |
237 | |
238 expectTrue(listOf('policy-ui #groups policy-group').length == 1); | |
239 }); | |
240 | |
241 test('policies with multiple tags are in multiple groups', function() { | |
242 this.policyUi.setPolicyGroups(GROUP_NAMES); | |
243 this.policyUi.setPolicyNames(createPolicyNames()); | |
244 expectTrue(groupContainsText(GROUP.ADMIN, POLICY.SAMLE_A.NAME)); | |
245 expectTrue(groupContainsText(GROUP.SECURITY, POLICY.SAMLE_A.NAME)); | |
246 }); | |
247 }); | |
248 | |
249 mocha.run(); | |
250 }); | |
OLD | NEW |