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 } |
| 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))) { |
| 109 result.push(root); |
| 110 } |
| 111 if (selector.startsWith('#') && root.id == selector.slice(1)) |
| 112 result.push(root); |
| 113 for (var i = 0; i < root.children.length; ++i) |
| 114 result = result.concat(this.queryObjectDeep_(selector, root.children[i])); |
| 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()) { |
| 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 |
| 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) |
| 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"'); |
| 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); |
| 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 |