OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 /** @fileoverview Suite of tests for site-settings-category. */ | |
6 cr.define('site_settings_category', function() { | |
7 function registerTests() { | |
8 suite('SiteSettingsCategory', function() { | |
9 /** | |
10 * A site settings category created before each test. | |
11 * @type {SiteSettingsCategory} | |
12 */ | |
13 var testElement; | |
14 | |
15 /** | |
16 * The mock proxy object to use during test. | |
17 * @type {TestSiteSettingsPrefsBrowserProxy} | |
18 */ | |
19 var browserProxy = null; | |
20 | |
21 /** | |
22 * An example pref where the location category is disabled. | |
23 * @type {SiteSettingsPref} | |
24 */ | |
25 var prefsLocationDisabled = { | |
26 defaults: { | |
27 geolocation: { | |
28 setting: 'block', | |
29 }, | |
30 }, | |
31 exceptions: { | |
32 geolocation: [], | |
33 }, | |
34 }; | |
35 | |
36 /** | |
37 * An example pref where the location category is enabled. | |
38 * @type {SiteSettingsPref} | |
39 */ | |
40 var prefsLocationEnabled = { | |
41 defaults: { | |
42 geolocation: { | |
43 setting: 'allow', | |
44 }, | |
45 }, | |
46 exceptions: { | |
47 geolocation: [], | |
48 }, | |
49 }; | |
50 | |
51 /** | |
52 * An example pref where the Flash category is set on detect mode. | |
53 */ | |
54 var prefsFlashDetect = { | |
55 defaults: { | |
56 plugins: { | |
57 setting: 'detect_important_content', | |
58 }, | |
59 }, | |
60 exceptions: { | |
61 plugins: [], | |
62 }, | |
63 }; | |
64 | |
65 /** | |
66 * An example pref where the Cookies category is set to delete when | |
67 * session ends. | |
68 */ | |
69 var prefsCookesSessionOnly = { | |
70 defaults: { | |
71 cookies: { | |
72 setting: 'session_only', | |
73 }, | |
74 }, | |
75 exceptions: { | |
76 cookies: [], | |
77 }, | |
78 }; | |
79 | |
80 // Import necessary html before running suite. | |
81 suiteSetup(function() { | |
82 return PolymerTest.importHtml( | |
83 'chrome://md-settings/site_settings/site_settings_category.html'); | |
84 }); | |
85 | |
86 // Initialize a site-settings-category before each test. | |
87 setup(function() { | |
88 browserProxy = new TestSiteSettingsPrefsBrowserProxy(); | |
89 settings.SiteSettingsPrefsBrowserProxyImpl.instance_ = browserProxy; | |
90 PolymerTest.clearBody(); | |
91 testElement = document.createElement('site-settings-category'); | |
92 document.body.appendChild(testElement); | |
93 }); | |
94 | |
95 test('getDefaultValueForContentType API used', function() { | |
96 testElement.category = settings.ContentSettingsTypes.GEOLOCATION; | |
97 return browserProxy.whenCalled('getDefaultValueForContentType').then( | |
98 function(contentType) { | |
99 assertEquals( | |
100 settings.ContentSettingsTypes.GEOLOCATION, contentType); | |
101 }); | |
102 }); | |
103 | |
104 function testCategoryEnabled(testElement, enabled) { | |
105 browserProxy.reset(); | |
106 browserProxy.setPrefs( | |
107 enabled ? prefsLocationEnabled : prefsLocationDisabled); | |
108 | |
109 testElement.category = settings.ContentSettingsTypes.GEOLOCATION; | |
110 return browserProxy.whenCalled('getDefaultValueForContentType').then( | |
111 function(contentType) { | |
112 assertEquals( | |
113 settings.ContentSettingsTypes.GEOLOCATION, contentType); | |
114 assertEquals(enabled, testElement.categoryEnabled); | |
115 MockInteractions.tap(testElement.$.toggle); | |
116 return browserProxy.whenCalled('setDefaultValueForContentType'); | |
117 }).then(function(args) { | |
118 assertEquals( | |
119 settings.ContentSettingsTypes.GEOLOCATION, args[0]); | |
120 assertEquals( | |
121 enabled ? settings.PermissionValues.BLOCK : | |
122 settings.PermissionValues.ASK, | |
123 args[1]); | |
124 assertNotEquals(enabled, testElement.categoryEnabled); | |
125 }); | |
126 } | |
127 | |
128 test('categoryEnabled correctly represents prefs (enabled)', function() { | |
129 return testCategoryEnabled(testElement, true); | |
130 }); | |
131 | |
132 test('categoryEnabled correctly represents prefs (disabled)', function() { | |
133 return testCategoryEnabled(testElement, false); | |
134 }); | |
135 | |
136 test('basic category tests', function() { | |
137 for (var key in settings.ContentSettingsTypes) { | |
138 var category = settings.ContentSettingsTypes[key]; | |
139 | |
140 // A quick testing note on a few special categories... | |
141 | |
142 // The USB Devices category has no global toggle -- and therefore no | |
143 // Category Desc (and has a special way of storing its data). | |
144 | |
145 // The Protocol Handlers is a special category in that is does not | |
146 // store its data like the rest, but it has a global default toggle | |
147 // and therefore can be tested like the rest below. | |
148 | |
149 // Test category text ids and descriptions for those categories that | |
150 // have those. | |
151 if (category != settings.ContentSettingsTypes.USB_DEVICES && | |
152 category != settings.ContentSettingsTypes.ZOOM_LEVELS) { | |
153 assertNotEquals('', testElement.computeCategoryDesc( | |
154 category, settings.PermissionValues.ALLOW, true)); | |
155 assertNotEquals('', testElement.computeCategoryDesc( | |
156 category, settings.PermissionValues.ALLOW, false)); | |
157 assertNotEquals('', testElement.computeCategoryDesc( | |
158 category, settings.PermissionValues.BLOCK, true)); | |
159 assertNotEquals('', testElement.computeCategoryDesc( | |
160 category, settings.PermissionValues.BLOCK, false)); | |
161 | |
162 // Test additional tri-state values: | |
163 if (category == settings.ContentSettingsTypes.PLUGINS) { | |
164 assertNotEquals('', testElement.computeCategoryDesc( | |
165 category, settings.PermissionValues.IMPORTANT_CONTENT, true)); | |
166 assertNotEquals('', testElement.computeCategoryDesc( | |
167 category, settings.PermissionValues.IMPORTANT_CONTENT, | |
168 false)); | |
169 } else if (category == settings.ContentSettingsTypes.COOKIES) { | |
170 assertNotEquals('', testElement.computeCategoryDesc( | |
171 category, settings.PermissionValues.SESSION_ONLY, true)); | |
172 assertNotEquals('', testElement.computeCategoryDesc( | |
173 category, settings.PermissionValues.SESSION_ONLY, false)); | |
174 } | |
175 } | |
176 } | |
177 }); | |
178 | |
179 function testTristateCategory(prefs, category, thirdState, | |
180 secondaryToggleId) { | |
181 browserProxy.setPrefs(prefs); | |
182 | |
183 testElement.category = category; | |
184 var secondaryToggle = null; | |
185 | |
186 return browserProxy.whenCalled('getDefaultValueForContentType').then( | |
187 function(contentType) { | |
188 Polymer.dom.flush(); | |
189 | |
190 secondaryToggle = testElement.$$(secondaryToggleId); | |
191 assertTrue(!!secondaryToggle); | |
192 | |
193 assertEquals(category, contentType); | |
194 assertTrue(testElement.categoryEnabled); | |
195 assertFalse(secondaryToggle.disabled); | |
196 assertTrue(secondaryToggle.checked); | |
197 | |
198 MockInteractions.tap(testElement.$.toggle); | |
199 return browserProxy.whenCalled('setDefaultValueForContentType'); | |
200 }).then(function(args) { | |
201 // Check THIRD_STATE => BLOCK transition succeeded. | |
202 Polymer.dom.flush(); | |
203 | |
204 assertEquals(category, args[0]); | |
205 assertEquals(settings.PermissionValues.BLOCK, args[1]); | |
206 assertFalse(testElement.categoryEnabled); | |
207 assertTrue(secondaryToggle.disabled); | |
208 assertTrue(secondaryToggle.checked); | |
209 | |
210 browserProxy.resetResolver('setDefaultValueForContentType'); | |
211 MockInteractions.tap(testElement.$.toggle); | |
212 return browserProxy.whenCalled('setDefaultValueForContentType'); | |
213 }).then(function(args) { | |
214 // Check BLOCK => THIRD_STATE transition succeeded. | |
215 Polymer.dom.flush(); | |
216 | |
217 assertEquals(category, args[0]); | |
218 assertEquals(thirdState, args[1]); | |
219 assertTrue(testElement.categoryEnabled); | |
220 assertFalse(secondaryToggle.disabled); | |
221 assertTrue(secondaryToggle.checked); | |
222 | |
223 browserProxy.resetResolver('setDefaultValueForContentType'); | |
224 MockInteractions.tap(secondaryToggle); | |
225 return browserProxy.whenCalled('setDefaultValueForContentType'); | |
226 }).then(function(args) { | |
227 // Check THIRD_STATE => ALLOW transition succeeded. | |
228 Polymer.dom.flush(); | |
229 | |
230 assertEquals(category, args[0]); | |
231 assertEquals( | |
232 settings.PermissionValues.ALLOW, args[1]); | |
233 assertTrue(testElement.categoryEnabled); | |
234 assertFalse(secondaryToggle.disabled); | |
235 assertFalse(secondaryToggle.checked); | |
236 | |
237 browserProxy.resetResolver('setDefaultValueForContentType'); | |
238 MockInteractions.tap(testElement.$.toggle); | |
239 return browserProxy.whenCalled('setDefaultValueForContentType'); | |
240 }).then(function(args) { | |
241 // Check ALLOW => BLOCK transition succeeded. | |
242 Polymer.dom.flush(); | |
243 | |
244 assertEquals(category, args[0]); | |
245 assertEquals(settings.PermissionValues.BLOCK, args[1]); | |
246 assertFalse(testElement.categoryEnabled); | |
247 assertTrue(secondaryToggle.disabled); | |
248 assertFalse(secondaryToggle.checked); | |
249 | |
250 browserProxy.resetResolver('setDefaultValueForContentType'); | |
251 MockInteractions.tap(testElement.$.toggle); | |
252 return browserProxy.whenCalled('setDefaultValueForContentType'); | |
253 }).then(function(args) { | |
254 // Check BLOCK => ALLOW transition succeeded. | |
255 Polymer.dom.flush(); | |
256 | |
257 assertEquals(category, args[0]); | |
258 assertEquals(settings.PermissionValues.ALLOW, args[1]); | |
259 assertTrue(testElement.categoryEnabled); | |
260 assertFalse(secondaryToggle.disabled); | |
261 assertFalse(secondaryToggle.checked); | |
262 | |
263 browserProxy.resetResolver('setDefaultValueForContentType'); | |
264 MockInteractions.tap(secondaryToggle); | |
265 return browserProxy.whenCalled('setDefaultValueForContentType'); | |
266 }).then(function(args) { | |
267 // Check ALLOW => THIRD_STATE transition succeeded. | |
268 Polymer.dom.flush(); | |
269 | |
270 assertEquals(category, args[0]); | |
271 assertEquals(thirdState, args[1]); | |
272 assertTrue(testElement.categoryEnabled); | |
273 assertFalse(secondaryToggle.disabled); | |
274 assertTrue(secondaryToggle.checked); | |
275 }); | |
276 } | |
277 | |
278 test('test special tri-state Flash category', function() { | |
279 return testTristateCategory( | |
280 prefsFlashDetect, settings.ContentSettingsTypes.PLUGINS, | |
281 settings.PermissionValues.IMPORTANT_CONTENT, '#flashAskToggle'); | |
282 }); | |
283 | |
284 test('test special tri-state Cookies category', function() { | |
285 return testTristateCategory( | |
286 prefsCookesSessionOnly, settings.ContentSettingsTypes.COOKIES, | |
287 settings.PermissionValues.SESSION_ONLY, '#sessionOnlyToggle'); | |
288 }); | |
289 }); | |
290 } | |
291 | |
292 return { | |
293 registerTests: registerTests, | |
294 }; | |
295 }); | |
OLD | NEW |