OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** @fileoverview Suite of tests for site-list. */ | 5 /** @fileoverview Suite of tests for site-list. */ |
6 cr.define('site_list', function() { | 6 cr.define('site_list', function() { |
7 function registerTests() { | 7 function registerTests() { |
8 suite('SiteList', function() { | 8 suite('SiteList', function() { |
9 /** | 9 /** |
10 * A site list element created before each test. | 10 * A site list element created before each test. |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
185 // Initialize a site-list before each test. | 185 // Initialize a site-list before each test. |
186 setup(function() { | 186 setup(function() { |
187 browserProxy = new TestSiteSettingsPrefsBrowserProxy(); | 187 browserProxy = new TestSiteSettingsPrefsBrowserProxy(); |
188 settings.SiteSettingsPrefsBrowserProxyImpl.instance_ = browserProxy; | 188 settings.SiteSettingsPrefsBrowserProxyImpl.instance_ = browserProxy; |
189 PolymerTest.clearBody(); | 189 PolymerTest.clearBody(); |
190 testElement = document.createElement('settings-site-list'); | 190 testElement = document.createElement('settings-site-list'); |
191 document.body.appendChild(testElement); | 191 document.body.appendChild(testElement); |
192 }); | 192 }); |
193 | 193 |
194 /** | 194 /** |
195 * Returns a promise that resolves once the site list has been updated. | |
196 * @param {function()} action is executed after the listener is set up. | |
197 * @return {!Promise} a Promise fulfilled when the selected item changes. | |
198 */ | |
199 function runAndResolveWhenSitesChanged(action) { | |
200 return new Promise(function(resolve, reject) { | |
201 var handler = function() { | |
202 testElement.removeEventListener('sites-changed', handler); | |
203 resolve(); | |
204 }; | |
205 testElement.addEventListener('sites-changed', handler); | |
206 action(); | |
207 }); | |
208 } | |
209 | |
210 /** | |
211 * Asserts if a menu action is incorrectly hidden. | 195 * Asserts if a menu action is incorrectly hidden. |
212 * @param {!HTMLElement} parentElement The parent node to start looking | 196 * @param {!HTMLElement} parentElement The parent node to start looking |
213 * in. | 197 * in. |
214 * @param {string} textForHiddenAction Text content of the only node that | 198 * @param {string} textForHiddenAction Text content of the only node that |
215 * should be hidden. | 199 * should be hidden. |
216 */ | 200 */ |
217 function assertMenuActionHidden(parentElement, textForHiddenAction) { | 201 function assertMenuActionHidden(parentElement, textForHiddenAction) { |
218 var actions = parentElement.$.listContainer.items; | 202 var actions = parentElement.$.listContainer.items; |
219 for (var i = 0; i < actions.length; ++i) { | 203 for (var i = 0; i < actions.length; ++i) { |
220 var content = actions[i].textContent.trim(); | 204 var content = actions[i].textContent.trim(); |
221 if (content == textForHiddenAction) | 205 if (content == textForHiddenAction) |
dpapad
2016/03/31 17:37:07
Nit: This can be shortened as follows
actions.for
Finnur
2016/04/04 13:59:24
I discovered this function doesn't work as expecte
| |
222 assertTrue(actions[i].hidden); | 206 assertTrue(actions[i].hidden); |
223 else | 207 else |
224 assertFalse(actions[i].hidden); | 208 assertFalse(actions[i].hidden); |
225 } | 209 } |
226 } | 210 } |
227 | 211 |
228 /** | 212 /** |
229 * Configures the test element as a location category. | 213 * Configures the test element as a location category. |
230 * @param {settings.PermissionValues} subtype Type of list to use: ALLOW | 214 * @param {settings.PermissionValues} subtype Type of list to use: ALLOW |
231 * or BLOCK. | 215 * or BLOCK. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 test('getExceptionList API used', function() { | 249 test('getExceptionList API used', function() { |
266 setupLocationCategory(settings.PermissionValues.ALLOW, prefsEmpty); | 250 setupLocationCategory(settings.PermissionValues.ALLOW, prefsEmpty); |
267 return browserProxy.whenCalled('getExceptionList').then( | 251 return browserProxy.whenCalled('getExceptionList').then( |
268 function(contentType) { | 252 function(contentType) { |
269 assertEquals( | 253 assertEquals( |
270 settings.ContentSettingsTypes.GEOLOCATION, contentType); | 254 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
271 }); | 255 }); |
272 }); | 256 }); |
273 | 257 |
274 test('Empty list', function() { | 258 test('Empty list', function() { |
275 return runAndResolveWhenSitesChanged(function() { | 259 setupLocationCategory(settings.PermissionValues.ALLOW, prefsEmpty); |
276 setupLocationCategory(settings.PermissionValues.ALLOW, prefsEmpty); | 260 return browserProxy.whenCalled('getExceptionList').then( |
277 }).then(function() { | 261 function(contentType) { |
278 assertEquals(0, testElement.sites.length); | 262 assertEquals( |
279 | 263 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
280 assertTrue(testElement.isAllowList_()); | 264 |
281 assertFalse(testElement.showSiteList_(testElement.sites, true)); | 265 assertEquals(0, testElement.sites.length); |
282 assertFalse(testElement.showSiteList_(testElement.sites, false)); | 266 |
283 assertEquals('Allow - 0', testElement.computeSiteListHeader_( | 267 assertEquals( |
284 testElement.sites, true)); | 268 settings.PermissionValues.ALLOW, testElement.categorySubtype); |
285 assertEquals('Exceptions - 0', testElement.computeSiteListHeader_( | 269 assertEquals('Allow - 0', testElement.$.header.innerText); |
286 testElement.sites, false)); | 270 |
287 }.bind(this)); | 271 // Site list should not show, no matter what category default is set |
272 // to. | |
273 assertTrue(testElement.$.category.hidden); | |
274 browserProxy.resetResolver('getExceptionList'); | |
275 testElement.categoryEnabled = false; | |
276 return browserProxy.whenCalled('getExceptionList').then( | |
277 function(contentType) { | |
278 assertTrue(testElement.$.category.hidden); | |
279 assertEquals('Exceptions - 0', testElement.$.header.innerText); | |
280 }); | |
281 }); | |
288 }); | 282 }); |
289 | 283 |
290 test('initial ALLOW state is correct', function() { | 284 test('initial ALLOW state is correct', function() { |
291 return runAndResolveWhenSitesChanged(function() { | 285 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); |
292 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); | 286 return browserProxy.whenCalled('getExceptionList').then( |
293 }).then(function() { | 287 function(contentType) { |
294 assertEquals(2, testElement.sites.length); | 288 assertEquals( |
295 assertEquals('https://bar-allow.com:443', testElement.sites[0].origin) ; | 289 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
296 assertTrue(testElement.isAllowList_()); | 290 |
297 assertMenuActionHidden(testElement, 'Allow'); | 291 assertEquals(2, testElement.sites.length); |
298 // Site list should show, no matter what category default is set to. | 292 assertEquals(prefs.exceptions.geolocation[1].origin, |
299 assertTrue(testElement.showSiteList_(testElement.sites, true)); | 293 testElement.sites[0].origin); |
300 assertTrue(testElement.showSiteList_(testElement.sites, false)); | 294 assertEquals( |
301 assertEquals('Exceptions - 2', testElement.computeSiteListHeader_( | 295 settings.PermissionValues.ALLOW, testElement.categorySubtype); |
302 testElement.sites, false)); | 296 assertMenuActionHidden(testElement, 'Allow'); |
303 assertEquals('Allow - 2', testElement.computeSiteListHeader_( | 297 assertEquals('Allow - 2', testElement.$.header.innerText); |
304 testElement.sites, true)); | 298 |
305 }.bind(this)); | 299 // Site list should show, no matter what category default is set to. |
300 assertFalse(testElement.$.category.hidden); | |
301 browserProxy.resetResolver('getExceptionList'); | |
302 testElement.categoryEnabled = false; | |
303 return browserProxy.whenCalled('getExceptionList').then( | |
304 function(contentType) { | |
305 assertFalse(testElement.$.category.hidden); | |
306 assertEquals('Exceptions - 2', testElement.$.header.innerText); | |
307 }); | |
308 }); | |
306 }); | 309 }); |
307 | 310 |
308 test('initial BLOCK state is correct', function() { | 311 test('initial BLOCK state is correct', function() { |
309 return runAndResolveWhenSitesChanged(function() { | 312 setupLocationCategory(settings.PermissionValues.BLOCK, prefs); |
310 setupLocationCategory(settings.PermissionValues.BLOCK, prefs); | 313 return browserProxy.whenCalled('getExceptionList').then( |
311 }).then(function() { | 314 function(contentType) { |
312 assertEquals(2, testElement.sites.length); | 315 assertEquals( |
313 assertEquals('https://bar-block.com:443', testElement.sites[0].origin) ; | 316 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
314 | 317 |
315 assertFalse(testElement.isAllowList_()); | 318 assertEquals(2, testElement.sites.length); |
316 assertMenuActionHidden(testElement, 'Block'); | 319 assertEquals(prefs.exceptions.geolocation[3].origin, |
317 // Site list should only show when category default is enabled. | 320 testElement.sites[0].origin); |
318 assertFalse(testElement.showSiteList_(testElement.sites, false)); | 321 |
319 assertTrue(testElement.showSiteList_(testElement.sites, true)); | 322 assertEquals( |
320 assertEquals('Block - 2', testElement.computeSiteListHeader_( | 323 settings.PermissionValues.BLOCK, testElement.categorySubtype); |
321 testElement.sites, true)); | 324 assertMenuActionHidden(testElement, 'Block'); |
322 }.bind(this)); | 325 assertEquals('Block - 2', testElement.$.header.innerText); |
326 | |
327 // Site list should only show when category default is enabled. | |
328 assertFalse(testElement.$.category.hidden); | |
329 browserProxy.resetResolver('getExceptionList'); | |
330 testElement.categoryEnabled = false; | |
331 return browserProxy.whenCalled('getExceptionList').then( | |
332 function(contentType) { | |
333 assertTrue(testElement.$.category.hidden); | |
334 }); | |
335 }); | |
323 }); | 336 }); |
324 | 337 |
325 test('list items shown and clickable when data is present', function() { | 338 test('list items shown and clickable when data is present', function() { |
326 return runAndResolveWhenSitesChanged(function() { | 339 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); |
327 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); | 340 return browserProxy.whenCalled('getExceptionList').then( |
328 }).then(function() { | 341 function(contentType) { |
329 // Required for firstItem to be found below. | 342 assertEquals( |
330 Polymer.dom.flush(); | 343 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
331 | 344 |
332 // Validate that the sites gets populated from pre-canned prefs. | 345 // Required for firstItem to be found below. |
333 assertEquals(2, testElement.sites.length); | 346 Polymer.dom.flush(); |
334 assertEquals('https://bar-allow.com:443', testElement.sites[0].origin) ; | 347 |
335 assertEquals(undefined, testElement.selectedOrigin); | 348 // Validate that the sites gets populated from pre-canned prefs. |
336 | 349 assertEquals(2, testElement.sites.length); |
337 // Validate that the sites are shown in UI and can be selected. | 350 assertEquals(prefs.exceptions.geolocation[1].origin, |
338 var firstItem = testElement.$.listContainer.items[0]; | 351 testElement.sites[0].origin); |
339 var clickable = firstItem.querySelector('.flex paper-item-body'); | 352 assertEquals(undefined, testElement.selectedOrigin); |
340 assertNotEquals(undefined, clickable); | 353 |
341 MockInteractions.tap(clickable); | 354 // Validate that the sites are shown in UI and can be selected. |
342 assertEquals( | 355 var firstItem = testElement.$.listContainer.items[0]; |
343 'https://bar-allow.com:443', testElement.selectedSite.origin); | 356 var clickable = firstItem.querySelector('.flex paper-item-body'); |
344 }.bind(this)); | 357 assertNotEquals(undefined, clickable); |
358 MockInteractions.tap(clickable); | |
359 assertEquals(prefs.exceptions.geolocation[1].origin, | |
360 testElement.selectedSite.origin); | |
361 }); | |
345 }); | 362 }); |
346 | 363 |
347 test('Block list open when Allow list is empty', function() { | 364 test('Block list open when Allow list is empty', function() { |
348 return runAndResolveWhenSitesChanged(function() { | 365 // Prefs: One item in Block list, nothing in Allow list. |
349 // Prefs: One item in Block list, nothing in Allow list. | 366 setupLocationCategory(settings.PermissionValues.BLOCK, |
350 setupLocationCategory(settings.PermissionValues.BLOCK, | 367 prefsOneDisabled); |
351 prefsOneDisabled); | 368 return browserProxy.whenCalled('getExceptionList').then( |
352 }).then(function() { | 369 function(contentType) { |
353 assertFalse(testElement.$.category.hidden); | 370 assertEquals( |
354 assertTrue(testElement.$.category.opened); | 371 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
355 assertNotEquals(0, testElement.$.listContainer.offsetHeight); | 372 |
356 }.bind(this)); | 373 assertFalse(testElement.$.category.hidden); |
374 assertTrue(testElement.$.category.opened); | |
375 assertNotEquals(0, testElement.$.listContainer.offsetHeight); | |
376 }); | |
357 }); | 377 }); |
358 | 378 |
359 test('Block list closed when Allow list is not empty', function() { | 379 test('Block list closed when Allow list is not empty', function() { |
360 return runAndResolveWhenSitesChanged(function() { | 380 // Prefs: Items in both Block and Allow list. |
361 // Prefs: Items in both Block and Allow list. | 381 setupLocationCategory(settings.PermissionValues.BLOCK, prefs); |
362 setupLocationCategory(settings.PermissionValues.BLOCK, prefs); | 382 return browserProxy.whenCalled('getExceptionList').then( |
363 }).then(function() { | 383 function(contentType) { |
364 assertFalse(testElement.$.category.hidden); | 384 assertEquals( |
365 assertFalse(testElement.$.category.opened); | 385 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
366 assertEquals(0, testElement.$.listContainer.offsetHeight); | 386 |
367 }.bind(this)); | 387 assertFalse(testElement.$.category.hidden); |
388 assertFalse(testElement.$.category.opened); | |
389 assertEquals(0, testElement.$.listContainer.offsetHeight); | |
390 }); | |
368 }); | 391 }); |
369 | 392 |
370 test('Allow list is always open (Block list empty)', function() { | 393 test('Allow list is always open (Block list empty)', function() { |
371 return runAndResolveWhenSitesChanged(function() { | 394 // Prefs: One item in Allow list, nothing in Block list. |
372 // Prefs: One item in Allow list, nothing in Block list. | 395 setupLocationCategory( |
373 setupLocationCategory( | 396 settings.PermissionValues.ALLOW, prefsOneEnabled); |
374 settings.PermissionValues.ALLOW, prefsOneEnabled); | 397 return browserProxy.whenCalled('getExceptionList').then( |
375 }).then(function() { | 398 function(contentType) { |
376 assertFalse(testElement.$.category.hidden); | 399 assertEquals( |
377 assertTrue(testElement.$.category.opened); | 400 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
378 assertNotEquals(0, testElement.$.listContainer.offsetHeight); | 401 |
379 }.bind(this)); | 402 assertFalse(testElement.$.category.hidden); |
403 assertTrue(testElement.$.category.opened); | |
404 assertNotEquals(0, testElement.$.listContainer.offsetHeight); | |
405 }); | |
380 }); | 406 }); |
381 | 407 |
382 test('Allow list is always open (Block list non-empty)', function() { | 408 test('Allow list is always open (Block list non-empty)', function() { |
383 return runAndResolveWhenSitesChanged(function() { | 409 // Prefs: Items in both Block and Allow list. |
384 // Prefs: Items in both Block and Allow list. | 410 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); |
385 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); | 411 return browserProxy.whenCalled('getExceptionList').then( |
386 }).then(function() { | 412 function(contentType) { |
387 assertFalse(testElement.$.category.hidden); | 413 assertEquals( |
388 assertTrue(testElement.$.category.opened); | 414 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
389 assertNotEquals(0, testElement.$.listContainer.offsetHeight); | 415 |
390 }.bind(this)); | 416 assertFalse(testElement.$.category.hidden); |
417 assertTrue(testElement.$.category.opened); | |
418 assertNotEquals(0, testElement.$.listContainer.offsetHeight); | |
419 }); | |
391 }); | 420 }); |
392 | 421 |
393 test('Block list hidden when empty', function() { | 422 test('Block list hidden when empty', function() { |
394 return runAndResolveWhenSitesChanged(function() { | 423 // Prefs: One item in Allow list, nothing in Block list. |
395 // Prefs: One item in Allow list, nothing in Block list. | 424 setupLocationCategory( |
396 setupLocationCategory( | 425 settings.PermissionValues.BLOCK, prefsOneEnabled); |
397 settings.PermissionValues.BLOCK, prefsOneEnabled); | 426 return browserProxy.whenCalled('getExceptionList').then( |
398 }).then(function() { | 427 function(contentType) { |
399 assertTrue(testElement.$.category.hidden); | 428 assertEquals( |
400 }.bind(this)); | 429 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
430 | |
431 assertTrue(testElement.$.category.hidden); | |
432 }); | |
401 }); | 433 }); |
402 | 434 |
403 test('Allow list hidden when empty', function() { | 435 test('Allow list hidden when empty', function() { |
404 return runAndResolveWhenSitesChanged(function() { | 436 // Prefs: One item in Block list, nothing in Allow list. |
405 // Prefs: One item in Block list, nothing in Allow list. | 437 setupLocationCategory(settings.PermissionValues.ALLOW, |
406 setupLocationCategory(settings.PermissionValues.ALLOW, | 438 prefsOneDisabled); |
407 prefsOneDisabled); | 439 return browserProxy.whenCalled('getExceptionList').then( |
408 }).then(function() { | 440 function(contentType) { |
409 assertTrue(testElement.$.category.hidden); | 441 assertEquals( |
410 }.bind(this)); | 442 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
411 }); | 443 |
412 | 444 assertTrue(testElement.$.category.hidden); |
413 test('All sites category', function() { | 445 }); |
414 return runAndResolveWhenSitesChanged(function() { | 446 }); |
415 // Prefs: Multiple and overlapping sites. | 447 |
416 setupAllSitesCategory(prefsVarious); | 448 test('All sites category', function(done) { |
417 }).then(function() { | 449 // Prefs: Multiple and overlapping sites. |
418 // Required for firstItem to be found below. | 450 setupAllSitesCategory(prefsVarious); |
419 Polymer.dom.flush(); | 451 |
420 | 452 browserProxy.whenCalled('getExceptionList').then( |
421 assertFalse(testElement.$.category.hidden); | 453 function(contentType) { |
422 // Validate that the sites gets populated from pre-canned prefs. If | 454 testElement.async(function() { |
423 // this fails with 5 instead of the expected 3, then the de-duping of | 455 // All Sites calls getExceptionList for all categories, starting |
424 // sites is not working for site_list. | 456 // with Cookies. |
425 assertEquals(3, testElement.sites.length); | 457 assertEquals(settings.ContentSettingsTypes.COOKIES, contentType); |
426 assertEquals('https://bar.com', testElement.sites[0].origin); | 458 |
427 assertEquals('https://foo.com', testElement.sites[1].origin); | 459 // Required for firstItem to be found below. |
428 assertEquals('https://google.com', testElement.sites[2].origin); | 460 Polymer.dom.flush(); |
429 assertEquals(undefined, testElement.selectedOrigin); | 461 |
430 | 462 assertTrue(testElement.$.category.opened); |
431 // Validate that the sites are shown in UI and can be selected. | 463 assertFalse(testElement.$.category.hidden); |
432 var firstItem = testElement.$.listContainer.items[1]; | 464 // Validate that the sites gets populated from pre-canned prefs. |
433 var clickable = firstItem.querySelector('.flex paper-item-body'); | 465 // If this fails with 5 instead of the expected 3, then the |
434 assertNotEquals(undefined, clickable); | 466 // de-duping of sites is not working for site_list. |
435 MockInteractions.tap(clickable); | 467 assertEquals(3, testElement.sites.length); |
436 assertEquals('https://foo.com', testElement.selectedSite.origin); | 468 assertEquals(prefsVarious.exceptions.geolocation[1].origin, |
437 }.bind(this)); | 469 testElement.sites[0].origin); |
470 assertEquals(prefsVarious.exceptions.geolocation[0].origin, | |
471 testElement.sites[1].origin); | |
472 assertEquals(prefsVarious.exceptions.notifications[0].origin, | |
473 testElement.sites[2].origin); | |
474 assertEquals(undefined, testElement.selectedOrigin); | |
475 | |
476 // Validate that the sites are shown in UI and can be selected. | |
477 var firstItem = testElement.$.listContainer.items[1]; | |
478 var clickable = firstItem.querySelector('.flex paper-item-body'); | |
479 assertNotEquals(undefined, clickable); | |
480 MockInteractions.tap(clickable); | |
481 assertEquals(prefsVarious.exceptions.geolocation[0].origin, | |
482 testElement.selectedSite.origin); | |
483 | |
484 done(); | |
485 }); | |
486 }); | |
438 }); | 487 }); |
439 | 488 |
440 test('Mixed schemes (present and absent)', function() { | 489 test('Mixed schemes (present and absent)', function() { |
441 return runAndResolveWhenSitesChanged(function() { | 490 // Prefs: One item with scheme and one without. |
442 // Prefs: One item with scheme and one without. | 491 setupLocationCategory(settings.PermissionValues.ALLOW, |
443 setupLocationCategory(settings.PermissionValues.ALLOW, | 492 prefsMixedSchemes); |
444 prefsMixedSchemes); | 493 return browserProxy.whenCalled('getExceptionList').then( |
445 }).then(function() { | 494 function(contentType) { |
446 // No further checks needed. If this fails, it will hang the test. | 495 // No further checks needed. If this fails, it will hang the test. |
447 }.bind(this)); | 496 }); |
448 }); | 497 }); |
449 }); | 498 }); |
450 } | 499 } |
451 return { | 500 return { |
452 registerTests: registerTests, | 501 registerTests: registerTests, |
453 }; | 502 }; |
454 }); | 503 }); |
OLD | NEW |