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 listItem = parentElement.$.listContainer.items[0]; |
219 for (var i = 0; i < actions.length; ++i) { | 203 var menuItems = |
220 var content = actions[i].textContent.trim(); | 204 listItem.querySelectorAll('paper-menu-button paper-item'); |
221 if (content == textForHiddenAction) | 205 assertNotEquals(0, menuItems.length); |
222 assertTrue(actions[i].hidden); | 206 |
223 else | 207 var found = false; |
224 assertFalse(actions[i].hidden); | 208 menuItems.forEach(function(item) { |
225 } | 209 var text = item.textContent.trim(); |
| 210 if (text == textForHiddenAction) |
| 211 found = true; |
| 212 assertEquals(text == textForHiddenAction, item.hidden); |
| 213 }); |
| 214 assertTrue(found); |
226 } | 215 } |
227 | 216 |
228 /** | 217 /** |
229 * Configures the test element as a location category. | 218 * Configures the test element as a location category. |
230 * @param {settings.PermissionValues} subtype Type of list to use: ALLOW | 219 * @param {settings.PermissionValues} subtype Type of list to use: ALLOW |
231 * or BLOCK. | 220 * or BLOCK. |
232 * @param Array<dictionary> prefs The prefs to use. | 221 * @param Array<dictionary> prefs The prefs to use. |
233 */ | 222 */ |
234 function setupLocationCategory(subtype, prefs) { | 223 function setupLocationCategory(subtype, prefs) { |
235 browserProxy.setPrefs(prefs); | 224 browserProxy.setPrefs(prefs); |
(...skipping 29 matching lines...) Expand all Loading... |
265 test('getExceptionList API used', function() { | 254 test('getExceptionList API used', function() { |
266 setupLocationCategory(settings.PermissionValues.ALLOW, prefsEmpty); | 255 setupLocationCategory(settings.PermissionValues.ALLOW, prefsEmpty); |
267 return browserProxy.whenCalled('getExceptionList').then( | 256 return browserProxy.whenCalled('getExceptionList').then( |
268 function(contentType) { | 257 function(contentType) { |
269 assertEquals( | 258 assertEquals( |
270 settings.ContentSettingsTypes.GEOLOCATION, contentType); | 259 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
271 }); | 260 }); |
272 }); | 261 }); |
273 | 262 |
274 test('Empty list', function() { | 263 test('Empty list', function() { |
275 return runAndResolveWhenSitesChanged(function() { | 264 setupLocationCategory(settings.PermissionValues.ALLOW, prefsEmpty); |
276 setupLocationCategory(settings.PermissionValues.ALLOW, prefsEmpty); | 265 return browserProxy.whenCalled('getExceptionList').then( |
277 }).then(function() { | 266 function(contentType) { |
278 assertEquals(0, testElement.sites.length); | 267 assertEquals( |
279 | 268 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
280 assertTrue(testElement.isAllowList_()); | 269 |
281 assertFalse(testElement.showSiteList_(testElement.sites, true)); | 270 assertEquals(0, testElement.sites.length); |
282 assertFalse(testElement.showSiteList_(testElement.sites, false)); | 271 |
283 assertEquals('Allow - 0', testElement.computeSiteListHeader_( | 272 assertEquals( |
284 testElement.sites, true)); | 273 settings.PermissionValues.ALLOW, testElement.categorySubtype); |
285 assertEquals('Exceptions - 0', testElement.computeSiteListHeader_( | 274 assertEquals('Allow - 0', testElement.$.header.innerText); |
286 testElement.sites, false)); | 275 |
287 }.bind(this)); | 276 // Site list should not show, no matter what category default is set |
| 277 // to. |
| 278 assertTrue(testElement.$.category.hidden); |
| 279 browserProxy.resetResolver('getExceptionList'); |
| 280 testElement.categoryEnabled = false; |
| 281 return browserProxy.whenCalled('getExceptionList').then( |
| 282 function(contentType) { |
| 283 assertTrue(testElement.$.category.hidden); |
| 284 assertEquals('Exceptions - 0', testElement.$.header.innerText); |
| 285 }); |
| 286 }); |
288 }); | 287 }); |
289 | 288 |
290 test('initial ALLOW state is correct', function() { | 289 test('initial ALLOW state is correct', function() { |
291 return runAndResolveWhenSitesChanged(function() { | 290 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); |
292 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); | 291 return browserProxy.whenCalled('getExceptionList').then( |
293 }).then(function() { | 292 function(contentType) { |
294 assertEquals(2, testElement.sites.length); | 293 assertEquals( |
295 assertEquals('https://bar-allow.com:443', testElement.sites[0].origin)
; | 294 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
296 assertTrue(testElement.isAllowList_()); | 295 |
297 assertMenuActionHidden(testElement, 'Allow'); | 296 assertEquals(2, testElement.sites.length); |
298 // Site list should show, no matter what category default is set to. | 297 assertEquals(prefs.exceptions.geolocation[1].origin, |
299 assertTrue(testElement.showSiteList_(testElement.sites, true)); | 298 testElement.sites[0].origin); |
300 assertTrue(testElement.showSiteList_(testElement.sites, false)); | 299 assertEquals( |
301 assertEquals('Exceptions - 2', testElement.computeSiteListHeader_( | 300 settings.PermissionValues.ALLOW, testElement.categorySubtype); |
302 testElement.sites, false)); | 301 Polymer.dom.flush(); // Populates action menu. |
303 assertEquals('Allow - 2', testElement.computeSiteListHeader_( | 302 assertMenuActionHidden(testElement, 'Allow'); |
304 testElement.sites, true)); | 303 assertEquals('Allow - 2', testElement.$.header.innerText); |
305 }.bind(this)); | 304 |
| 305 // Site list should show, no matter what category default is set to. |
| 306 assertFalse(testElement.$.category.hidden); |
| 307 browserProxy.resetResolver('getExceptionList'); |
| 308 testElement.categoryEnabled = false; |
| 309 return browserProxy.whenCalled('getExceptionList').then( |
| 310 function(contentType) { |
| 311 assertFalse(testElement.$.category.hidden); |
| 312 assertEquals('Exceptions - 2', testElement.$.header.innerText); |
| 313 }); |
| 314 }); |
306 }); | 315 }); |
307 | 316 |
308 test('initial BLOCK state is correct', function() { | 317 test('initial BLOCK state is correct', function() { |
309 return runAndResolveWhenSitesChanged(function() { | 318 setupLocationCategory(settings.PermissionValues.BLOCK, prefs); |
310 setupLocationCategory(settings.PermissionValues.BLOCK, prefs); | 319 return browserProxy.whenCalled('getExceptionList').then( |
311 }).then(function() { | 320 function(contentType) { |
312 assertEquals(2, testElement.sites.length); | 321 assertEquals( |
313 assertEquals('https://bar-block.com:443', testElement.sites[0].origin)
; | 322 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
314 | 323 |
315 assertFalse(testElement.isAllowList_()); | 324 assertEquals(2, testElement.sites.length); |
316 assertMenuActionHidden(testElement, 'Block'); | 325 assertEquals(prefs.exceptions.geolocation[3].origin, |
317 // Site list should only show when category default is enabled. | 326 testElement.sites[0].origin); |
318 assertFalse(testElement.showSiteList_(testElement.sites, false)); | 327 |
319 assertTrue(testElement.showSiteList_(testElement.sites, true)); | 328 assertEquals( |
320 assertEquals('Block - 2', testElement.computeSiteListHeader_( | 329 settings.PermissionValues.BLOCK, testElement.categorySubtype); |
321 testElement.sites, true)); | 330 Polymer.dom.flush(); // Populates action menu. |
322 }.bind(this)); | 331 assertMenuActionHidden(testElement, 'Block'); |
| 332 assertEquals('Block - 2', testElement.$.header.innerText); |
| 333 |
| 334 // Site list should only show when category default is enabled. |
| 335 assertFalse(testElement.$.category.hidden); |
| 336 browserProxy.resetResolver('getExceptionList'); |
| 337 testElement.categoryEnabled = false; |
| 338 return browserProxy.whenCalled('getExceptionList').then( |
| 339 function(contentType) { |
| 340 assertTrue(testElement.$.category.hidden); |
| 341 }); |
| 342 }); |
323 }); | 343 }); |
324 | 344 |
325 test('list items shown and clickable when data is present', function() { | 345 test('list items shown and clickable when data is present', function() { |
326 return runAndResolveWhenSitesChanged(function() { | 346 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); |
327 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); | 347 return browserProxy.whenCalled('getExceptionList').then( |
328 }).then(function() { | 348 function(contentType) { |
329 // Required for firstItem to be found below. | 349 assertEquals( |
330 Polymer.dom.flush(); | 350 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
331 | 351 |
332 // Validate that the sites gets populated from pre-canned prefs. | 352 // Required for firstItem to be found below. |
333 assertEquals(2, testElement.sites.length); | 353 Polymer.dom.flush(); |
334 assertEquals('https://bar-allow.com:443', testElement.sites[0].origin)
; | 354 |
335 assertEquals(undefined, testElement.selectedOrigin); | 355 // Validate that the sites gets populated from pre-canned prefs. |
336 | 356 assertEquals(2, testElement.sites.length); |
337 // Validate that the sites are shown in UI and can be selected. | 357 assertEquals(prefs.exceptions.geolocation[1].origin, |
338 var firstItem = testElement.$.listContainer.items[0]; | 358 testElement.sites[0].origin); |
339 var clickable = firstItem.querySelector('.flex paper-item-body'); | 359 assertEquals(undefined, testElement.selectedOrigin); |
340 assertNotEquals(undefined, clickable); | 360 |
341 MockInteractions.tap(clickable); | 361 // Validate that the sites are shown in UI and can be selected. |
342 assertEquals( | 362 var firstItem = testElement.$.listContainer.items[0]; |
343 'https://bar-allow.com:443', testElement.selectedSite.origin); | 363 var clickable = firstItem.querySelector('.flex paper-item-body'); |
344 }.bind(this)); | 364 assertNotEquals(undefined, clickable); |
| 365 MockInteractions.tap(clickable); |
| 366 assertEquals(prefs.exceptions.geolocation[1].origin, |
| 367 testElement.selectedSite.origin); |
| 368 }); |
345 }); | 369 }); |
346 | 370 |
347 test('Block list open when Allow list is empty', function() { | 371 test('Block list open when Allow list is empty', function() { |
348 return runAndResolveWhenSitesChanged(function() { | 372 // Prefs: One item in Block list, nothing in Allow list. |
349 // Prefs: One item in Block list, nothing in Allow list. | 373 setupLocationCategory(settings.PermissionValues.BLOCK, |
350 setupLocationCategory(settings.PermissionValues.BLOCK, | 374 prefsOneDisabled); |
351 prefsOneDisabled); | 375 return browserProxy.whenCalled('getExceptionList').then( |
352 }).then(function() { | 376 function(contentType) { |
353 assertFalse(testElement.$.category.hidden); | 377 assertEquals( |
354 assertTrue(testElement.$.category.opened); | 378 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
355 assertNotEquals(0, testElement.$.listContainer.offsetHeight); | 379 |
356 }.bind(this)); | 380 assertFalse(testElement.$.category.hidden); |
| 381 assertTrue(testElement.$.category.opened); |
| 382 assertNotEquals(0, testElement.$.listContainer.offsetHeight); |
| 383 }); |
357 }); | 384 }); |
358 | 385 |
359 test('Block list closed when Allow list is not empty', function() { | 386 test('Block list closed when Allow list is not empty', function() { |
360 return runAndResolveWhenSitesChanged(function() { | 387 // Prefs: Items in both Block and Allow list. |
361 // Prefs: Items in both Block and Allow list. | 388 setupLocationCategory(settings.PermissionValues.BLOCK, prefs); |
362 setupLocationCategory(settings.PermissionValues.BLOCK, prefs); | 389 return browserProxy.whenCalled('getExceptionList').then( |
363 }).then(function() { | 390 function(contentType) { |
364 assertFalse(testElement.$.category.hidden); | 391 assertEquals( |
365 assertFalse(testElement.$.category.opened); | 392 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
366 assertEquals(0, testElement.$.listContainer.offsetHeight); | 393 |
367 }.bind(this)); | 394 assertFalse(testElement.$.category.hidden); |
| 395 assertFalse(testElement.$.category.opened); |
| 396 assertEquals(0, testElement.$.listContainer.offsetHeight); |
| 397 }); |
368 }); | 398 }); |
369 | 399 |
370 test('Allow list is always open (Block list empty)', function() { | 400 test('Allow list is always open (Block list empty)', function() { |
371 return runAndResolveWhenSitesChanged(function() { | 401 // Prefs: One item in Allow list, nothing in Block list. |
372 // Prefs: One item in Allow list, nothing in Block list. | 402 setupLocationCategory( |
373 setupLocationCategory( | 403 settings.PermissionValues.ALLOW, prefsOneEnabled); |
374 settings.PermissionValues.ALLOW, prefsOneEnabled); | 404 return browserProxy.whenCalled('getExceptionList').then( |
375 }).then(function() { | 405 function(contentType) { |
376 assertFalse(testElement.$.category.hidden); | 406 assertEquals( |
377 assertTrue(testElement.$.category.opened); | 407 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
378 assertNotEquals(0, testElement.$.listContainer.offsetHeight); | 408 |
379 }.bind(this)); | 409 assertFalse(testElement.$.category.hidden); |
| 410 assertTrue(testElement.$.category.opened); |
| 411 assertNotEquals(0, testElement.$.listContainer.offsetHeight); |
| 412 }); |
380 }); | 413 }); |
381 | 414 |
382 test('Allow list is always open (Block list non-empty)', function() { | 415 test('Allow list is always open (Block list non-empty)', function() { |
383 return runAndResolveWhenSitesChanged(function() { | 416 // Prefs: Items in both Block and Allow list. |
384 // Prefs: Items in both Block and Allow list. | 417 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); |
385 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); | 418 return browserProxy.whenCalled('getExceptionList').then( |
386 }).then(function() { | 419 function(contentType) { |
387 assertFalse(testElement.$.category.hidden); | 420 assertEquals( |
388 assertTrue(testElement.$.category.opened); | 421 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
389 assertNotEquals(0, testElement.$.listContainer.offsetHeight); | 422 |
390 }.bind(this)); | 423 assertFalse(testElement.$.category.hidden); |
| 424 assertTrue(testElement.$.category.opened); |
| 425 assertNotEquals(0, testElement.$.listContainer.offsetHeight); |
| 426 }); |
391 }); | 427 }); |
392 | 428 |
393 test('Block list hidden when empty', function() { | 429 test('Block list hidden when empty', function() { |
394 return runAndResolveWhenSitesChanged(function() { | 430 // Prefs: One item in Allow list, nothing in Block list. |
395 // Prefs: One item in Allow list, nothing in Block list. | 431 setupLocationCategory( |
396 setupLocationCategory( | 432 settings.PermissionValues.BLOCK, prefsOneEnabled); |
397 settings.PermissionValues.BLOCK, prefsOneEnabled); | 433 return browserProxy.whenCalled('getExceptionList').then( |
398 }).then(function() { | 434 function(contentType) { |
399 assertTrue(testElement.$.category.hidden); | 435 assertEquals( |
400 }.bind(this)); | 436 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
| 437 |
| 438 assertTrue(testElement.$.category.hidden); |
| 439 }); |
401 }); | 440 }); |
402 | 441 |
403 test('Allow list hidden when empty', function() { | 442 test('Allow list hidden when empty', function() { |
404 return runAndResolveWhenSitesChanged(function() { | 443 // Prefs: One item in Block list, nothing in Allow list. |
405 // Prefs: One item in Block list, nothing in Allow list. | 444 setupLocationCategory(settings.PermissionValues.ALLOW, |
406 setupLocationCategory(settings.PermissionValues.ALLOW, | 445 prefsOneDisabled); |
407 prefsOneDisabled); | 446 return browserProxy.whenCalled('getExceptionList').then( |
408 }).then(function() { | 447 function(contentType) { |
409 assertTrue(testElement.$.category.hidden); | 448 assertEquals( |
410 }.bind(this)); | 449 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
411 }); | 450 |
412 | 451 assertTrue(testElement.$.category.hidden); |
413 test('All sites category', function() { | 452 }); |
414 return runAndResolveWhenSitesChanged(function() { | 453 }); |
415 // Prefs: Multiple and overlapping sites. | 454 |
416 setupAllSitesCategory(prefsVarious); | 455 test('All sites category', function(done) { |
417 }).then(function() { | 456 // Prefs: Multiple and overlapping sites. |
418 // Required for firstItem to be found below. | 457 setupAllSitesCategory(prefsVarious); |
419 Polymer.dom.flush(); | 458 |
420 | 459 browserProxy.whenCalled('getExceptionList').then( |
421 assertFalse(testElement.$.category.hidden); | 460 function(contentType) { |
422 // Validate that the sites gets populated from pre-canned prefs. If | 461 testElement.async(function() { |
423 // this fails with 5 instead of the expected 3, then the de-duping of | 462 // All Sites calls getExceptionList for all categories, starting |
424 // sites is not working for site_list. | 463 // with Cookies. |
425 assertEquals(3, testElement.sites.length); | 464 assertEquals(settings.ContentSettingsTypes.COOKIES, contentType); |
426 assertEquals('https://bar.com', testElement.sites[0].origin); | 465 |
427 assertEquals('https://foo.com', testElement.sites[1].origin); | 466 // Required for firstItem to be found below. |
428 assertEquals('https://google.com', testElement.sites[2].origin); | 467 Polymer.dom.flush(); |
429 assertEquals(undefined, testElement.selectedOrigin); | 468 |
430 | 469 assertTrue(testElement.$.category.opened); |
431 // Validate that the sites are shown in UI and can be selected. | 470 assertFalse(testElement.$.category.hidden); |
432 var firstItem = testElement.$.listContainer.items[1]; | 471 // Validate that the sites gets populated from pre-canned prefs. |
433 var clickable = firstItem.querySelector('.flex paper-item-body'); | 472 // If this fails with 5 instead of the expected 3, then the |
434 assertNotEquals(undefined, clickable); | 473 // de-duping of sites is not working for site_list. |
435 MockInteractions.tap(clickable); | 474 assertEquals(3, testElement.sites.length); |
436 assertEquals('https://foo.com', testElement.selectedSite.origin); | 475 assertEquals(prefsVarious.exceptions.geolocation[1].origin, |
437 }.bind(this)); | 476 testElement.sites[0].origin); |
| 477 assertEquals(prefsVarious.exceptions.geolocation[0].origin, |
| 478 testElement.sites[1].origin); |
| 479 assertEquals(prefsVarious.exceptions.notifications[0].origin, |
| 480 testElement.sites[2].origin); |
| 481 assertEquals(undefined, testElement.selectedOrigin); |
| 482 |
| 483 // Validate that the sites are shown in UI and can be selected. |
| 484 var firstItem = testElement.$.listContainer.items[1]; |
| 485 var clickable = firstItem.querySelector('.flex paper-item-body'); |
| 486 assertNotEquals(undefined, clickable); |
| 487 MockInteractions.tap(clickable); |
| 488 assertEquals(prefsVarious.exceptions.geolocation[0].origin, |
| 489 testElement.selectedSite.origin); |
| 490 |
| 491 done(); |
| 492 }); |
| 493 }); |
438 }); | 494 }); |
439 | 495 |
440 test('Mixed schemes (present and absent)', function() { | 496 test('Mixed schemes (present and absent)', function() { |
441 return runAndResolveWhenSitesChanged(function() { | 497 // Prefs: One item with scheme and one without. |
442 // Prefs: One item with scheme and one without. | 498 setupLocationCategory(settings.PermissionValues.ALLOW, |
443 setupLocationCategory(settings.PermissionValues.ALLOW, | 499 prefsMixedSchemes); |
444 prefsMixedSchemes); | 500 return browserProxy.whenCalled('getExceptionList').then( |
445 }).then(function() { | 501 function(contentType) { |
446 // No further checks needed. If this fails, it will hang the test. | 502 // No further checks needed. If this fails, it will hang the test. |
447 }.bind(this)); | 503 }); |
448 }); | 504 }); |
449 }); | 505 }); |
450 } | 506 } |
451 return { | 507 return { |
452 registerTests: registerTests, | 508 registerTests: registerTests, |
453 }; | 509 }; |
454 }); | 510 }); |
OLD | NEW |