Chromium Code Reviews| 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(); |
| (...skipping 44 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 assertTrue(testElement.isAllowList_()); |
|
dpapad
2016/03/29 21:46:33
Lines 267-273 all call private functions. This is
Finnur
2016/03/30 11:22:43
Done.
| |
| 284 testElement.sites, true)); | 268 assertFalse(testElement.showSiteList_(testElement.sites, true)); |
| 285 assertEquals('Exceptions - 0', testElement.computeSiteListHeader_( | 269 assertFalse(testElement.showSiteList_(testElement.sites, false)); |
| 286 testElement.sites, false)); | 270 assertEquals('Allow - 0', testElement.computeSiteListHeader_( |
| 287 }.bind(this)); | 271 testElement.sites, true)); |
| 272 assertEquals('Exceptions - 0', testElement.computeSiteListHeader_( | |
| 273 testElement.sites, false)); | |
| 274 }); | |
| 288 }); | 275 }); |
| 289 | 276 |
| 290 test('initial ALLOW state is correct', function() { | 277 test('initial ALLOW state is correct', function() { |
| 291 return runAndResolveWhenSitesChanged(function() { | 278 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); |
| 292 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); | 279 return browserProxy.whenCalled('getExceptionList').then( |
| 293 }).then(function() { | 280 function(contentType) { |
| 294 assertEquals(2, testElement.sites.length); | 281 assertEquals( |
| 295 assertEquals('https://bar-allow.com:443', testElement.sites[0].origin) ; | 282 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
| 296 assertTrue(testElement.isAllowList_()); | 283 |
| 297 assertMenuActionHidden(testElement, 'Allow'); | 284 assertEquals(2, testElement.sites.length); |
| 298 // Site list should show, no matter what category default is set to. | 285 assertEquals( |
| 299 assertTrue(testElement.showSiteList_(testElement.sites, true)); | 286 'https://bar-allow.com:443', testElement.sites[0].origin); |
|
dpapad
2016/03/29 21:46:33
Can we avoid repeating these string literals? Mayb
Finnur
2016/03/30 11:22:43
Done.
| |
| 300 assertTrue(testElement.showSiteList_(testElement.sites, false)); | 287 assertTrue(testElement.isAllowList_()); |
| 301 assertEquals('Exceptions - 2', testElement.computeSiteListHeader_( | 288 assertMenuActionHidden(testElement, 'Allow'); |
| 302 testElement.sites, false)); | 289 // Site list should show, no matter what category default is set to. |
| 303 assertEquals('Allow - 2', testElement.computeSiteListHeader_( | 290 assertTrue(testElement.showSiteList_(testElement.sites, true)); |
| 304 testElement.sites, true)); | 291 assertTrue(testElement.showSiteList_(testElement.sites, false)); |
| 305 }.bind(this)); | 292 assertEquals('Exceptions - 2', testElement.computeSiteListHeader_( |
| 293 testElement.sites, false)); | |
| 294 assertEquals('Allow - 2', testElement.computeSiteListHeader_( | |
| 295 testElement.sites, true)); | |
| 296 }); | |
| 306 }); | 297 }); |
| 307 | 298 |
| 308 test('initial BLOCK state is correct', function() { | 299 test('initial BLOCK state is correct', function() { |
| 309 return runAndResolveWhenSitesChanged(function() { | 300 setupLocationCategory(settings.PermissionValues.BLOCK, prefs); |
| 310 setupLocationCategory(settings.PermissionValues.BLOCK, prefs); | 301 return browserProxy.whenCalled('getExceptionList').then( |
| 311 }).then(function() { | 302 function(contentType) { |
| 312 assertEquals(2, testElement.sites.length); | 303 assertEquals( |
| 313 assertEquals('https://bar-block.com:443', testElement.sites[0].origin) ; | 304 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
| 314 | 305 |
| 315 assertFalse(testElement.isAllowList_()); | 306 assertEquals(2, testElement.sites.length); |
| 316 assertMenuActionHidden(testElement, 'Block'); | 307 assertEquals('https://bar-block.com:443', testElement.sites[0].origi n); |
| 317 // Site list should only show when category default is enabled. | 308 |
| 318 assertFalse(testElement.showSiteList_(testElement.sites, false)); | 309 assertFalse(testElement.isAllowList_()); |
| 319 assertTrue(testElement.showSiteList_(testElement.sites, true)); | 310 assertMenuActionHidden(testElement, 'Block'); |
| 320 assertEquals('Block - 2', testElement.computeSiteListHeader_( | 311 // Site list should only show when category default is enabled. |
| 321 testElement.sites, true)); | 312 assertFalse(testElement.showSiteList_(testElement.sites, false)); |
| 322 }.bind(this)); | 313 assertTrue(testElement.showSiteList_(testElement.sites, true)); |
| 314 assertEquals('Block - 2', testElement.computeSiteListHeader_( | |
| 315 testElement.sites, true)); | |
| 316 }); | |
| 323 }); | 317 }); |
| 324 | 318 |
| 325 test('list items shown and clickable when data is present', function() { | 319 test('list items shown and clickable when data is present', function() { |
| 326 return runAndResolveWhenSitesChanged(function() { | 320 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); |
| 327 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); | 321 return browserProxy.whenCalled('getExceptionList').then( |
| 328 }).then(function() { | 322 function(contentType) { |
| 329 // Required for firstItem to be found below. | 323 assertEquals( |
| 330 Polymer.dom.flush(); | 324 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
| 331 | 325 |
| 332 // Validate that the sites gets populated from pre-canned prefs. | 326 // Required for firstItem to be found below. |
| 333 assertEquals(2, testElement.sites.length); | 327 Polymer.dom.flush(); |
| 334 assertEquals('https://bar-allow.com:443', testElement.sites[0].origin) ; | 328 |
| 335 assertEquals(undefined, testElement.selectedOrigin); | 329 // Validate that the sites gets populated from pre-canned prefs. |
| 336 | 330 assertEquals(2, testElement.sites.length); |
| 337 // Validate that the sites are shown in UI and can be selected. | 331 assertEquals('https://bar-allow.com:443', testElement.sites[0].origi n); |
| 338 var firstItem = testElement.$.listContainer.items[0]; | 332 assertEquals(undefined, testElement.selectedOrigin); |
| 339 var clickable = firstItem.querySelector('.flex paper-item-body'); | 333 |
| 340 assertNotEquals(undefined, clickable); | 334 // Validate that the sites are shown in UI and can be selected. |
| 341 MockInteractions.tap(clickable); | 335 var firstItem = testElement.$.listContainer.items[0]; |
| 342 assertEquals( | 336 var clickable = firstItem.querySelector('.flex paper-item-body'); |
| 343 'https://bar-allow.com:443', testElement.selectedSite.origin); | 337 assertNotEquals(undefined, clickable); |
| 344 }.bind(this)); | 338 MockInteractions.tap(clickable); |
| 339 assertEquals( | |
| 340 'https://bar-allow.com:443', testElement.selectedSite.origin); | |
| 341 }); | |
| 345 }); | 342 }); |
| 346 | 343 |
| 347 test('Block list open when Allow list is empty', function() { | 344 test('Block list open when Allow list is empty', function() { |
| 348 return runAndResolveWhenSitesChanged(function() { | 345 // Prefs: One item in Block list, nothing in Allow list. |
| 349 // Prefs: One item in Block list, nothing in Allow list. | 346 setupLocationCategory(settings.PermissionValues.BLOCK, |
| 350 setupLocationCategory(settings.PermissionValues.BLOCK, | 347 prefsOneDisabled); |
| 351 prefsOneDisabled); | 348 return browserProxy.whenCalled('getExceptionList').then( |
| 352 }).then(function() { | 349 function(contentType) { |
| 353 assertFalse(testElement.$.category.hidden); | 350 assertEquals( |
| 354 assertTrue(testElement.$.category.opened); | 351 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
| 355 assertNotEquals(0, testElement.$.listContainer.offsetHeight); | 352 |
| 356 }.bind(this)); | 353 assertFalse(testElement.$.category.hidden); |
| 354 assertTrue(testElement.$.category.opened); | |
| 355 assertNotEquals(0, testElement.$.listContainer.offsetHeight); | |
| 356 }); | |
| 357 }); | 357 }); |
| 358 | 358 |
| 359 test('Block list closed when Allow list is not empty', function() { | 359 test('Block list closed when Allow list is not empty', function() { |
| 360 return runAndResolveWhenSitesChanged(function() { | 360 // Prefs: Items in both Block and Allow list. |
| 361 // Prefs: Items in both Block and Allow list. | 361 setupLocationCategory(settings.PermissionValues.BLOCK, prefs); |
| 362 setupLocationCategory(settings.PermissionValues.BLOCK, prefs); | 362 return browserProxy.whenCalled('getExceptionList').then( |
| 363 }).then(function() { | 363 function(contentType) { |
| 364 assertFalse(testElement.$.category.hidden); | 364 assertEquals( |
| 365 assertFalse(testElement.$.category.opened); | 365 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
| 366 assertEquals(0, testElement.$.listContainer.offsetHeight); | 366 |
| 367 }.bind(this)); | 367 assertFalse(testElement.$.category.hidden); |
| 368 assertFalse(testElement.$.category.opened); | |
| 369 assertEquals(0, testElement.$.listContainer.offsetHeight); | |
| 370 }); | |
| 368 }); | 371 }); |
| 369 | 372 |
| 370 test('Allow list is always open (Block list empty)', function() { | 373 test('Allow list is always open (Block list empty)', function() { |
| 371 return runAndResolveWhenSitesChanged(function() { | 374 // Prefs: One item in Allow list, nothing in Block list. |
| 372 // Prefs: One item in Allow list, nothing in Block list. | 375 setupLocationCategory( |
| 373 setupLocationCategory( | 376 settings.PermissionValues.ALLOW, prefsOneEnabled); |
| 374 settings.PermissionValues.ALLOW, prefsOneEnabled); | 377 return browserProxy.whenCalled('getExceptionList').then( |
| 375 }).then(function() { | 378 function(contentType) { |
| 376 assertFalse(testElement.$.category.hidden); | 379 assertEquals( |
| 377 assertTrue(testElement.$.category.opened); | 380 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
| 378 assertNotEquals(0, testElement.$.listContainer.offsetHeight); | 381 |
| 379 }.bind(this)); | 382 assertFalse(testElement.$.category.hidden); |
| 383 assertTrue(testElement.$.category.opened); | |
| 384 assertNotEquals(0, testElement.$.listContainer.offsetHeight); | |
| 385 }); | |
| 380 }); | 386 }); |
| 381 | 387 |
| 382 test('Allow list is always open (Block list non-empty)', function() { | 388 test('Allow list is always open (Block list non-empty)', function() { |
| 383 return runAndResolveWhenSitesChanged(function() { | 389 // Prefs: Items in both Block and Allow list. |
| 384 // Prefs: Items in both Block and Allow list. | 390 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); |
| 385 setupLocationCategory(settings.PermissionValues.ALLOW, prefs); | 391 return browserProxy.whenCalled('getExceptionList').then( |
| 386 }).then(function() { | 392 function(contentType) { |
| 387 assertFalse(testElement.$.category.hidden); | 393 assertEquals( |
| 388 assertTrue(testElement.$.category.opened); | 394 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
| 389 assertNotEquals(0, testElement.$.listContainer.offsetHeight); | 395 |
| 390 }.bind(this)); | 396 assertFalse(testElement.$.category.hidden); |
| 397 assertTrue(testElement.$.category.opened); | |
| 398 assertNotEquals(0, testElement.$.listContainer.offsetHeight); | |
| 399 }); | |
| 391 }); | 400 }); |
| 392 | 401 |
| 393 test('Block list hidden when empty', function() { | 402 test('Block list hidden when empty', function() { |
| 394 return runAndResolveWhenSitesChanged(function() { | 403 // Prefs: One item in Allow list, nothing in Block list. |
| 395 // Prefs: One item in Allow list, nothing in Block list. | 404 setupLocationCategory( |
| 396 setupLocationCategory( | 405 settings.PermissionValues.BLOCK, prefsOneEnabled); |
| 397 settings.PermissionValues.BLOCK, prefsOneEnabled); | 406 return browserProxy.whenCalled('getExceptionList').then( |
| 398 }).then(function() { | 407 function(contentType) { |
| 399 assertTrue(testElement.$.category.hidden); | 408 assertEquals( |
| 400 }.bind(this)); | 409 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
| 410 | |
| 411 assertTrue(testElement.$.category.hidden); | |
| 412 }); | |
| 401 }); | 413 }); |
| 402 | 414 |
| 403 test('Allow list hidden when empty', function() { | 415 test('Allow list hidden when empty', function() { |
| 404 return runAndResolveWhenSitesChanged(function() { | 416 // Prefs: One item in Block list, nothing in Allow list. |
| 405 // Prefs: One item in Block list, nothing in Allow list. | 417 setupLocationCategory(settings.PermissionValues.ALLOW, |
| 406 setupLocationCategory(settings.PermissionValues.ALLOW, | 418 prefsOneDisabled); |
| 407 prefsOneDisabled); | 419 return browserProxy.whenCalled('getExceptionList').then( |
| 408 }).then(function() { | 420 function(contentType) { |
| 409 assertTrue(testElement.$.category.hidden); | 421 assertEquals( |
| 410 }.bind(this)); | 422 settings.ContentSettingsTypes.GEOLOCATION, contentType); |
| 411 }); | 423 |
| 412 | 424 assertTrue(testElement.$.category.hidden); |
| 413 test('All sites category', function() { | 425 }); |
| 414 return runAndResolveWhenSitesChanged(function() { | 426 }); |
| 415 // Prefs: Multiple and overlapping sites. | 427 |
| 416 setupAllSitesCategory(prefsVarious); | 428 test('All sites category', function(done) { |
| 417 }).then(function() { | 429 // Prefs: Multiple and overlapping sites. |
| 418 // Required for firstItem to be found below. | 430 setupAllSitesCategory(prefsVarious); |
| 419 Polymer.dom.flush(); | 431 |
| 420 | 432 return browserProxy.whenCalled('getExceptionList').then( |
|
dpapad
2016/03/29 21:46:33
Since you are using done(), there is no need to re
Finnur
2016/03/30 11:22:43
Done (no pun intended).
| |
| 421 assertFalse(testElement.$.category.hidden); | 433 function(contentType) { |
| 422 // Validate that the sites gets populated from pre-canned prefs. If | 434 testElement.async(function() { |
| 423 // this fails with 5 instead of the expected 3, then the de-duping of | 435 // All Sites calls getExceptionList for all categories, starting |
| 424 // sites is not working for site_list. | 436 // with Cookies. |
| 425 assertEquals(3, testElement.sites.length); | 437 assertEquals(settings.ContentSettingsTypes.COOKIES, contentType); |
| 426 assertEquals('https://bar.com', testElement.sites[0].origin); | 438 |
| 427 assertEquals('https://foo.com', testElement.sites[1].origin); | 439 // Required for firstItem to be found below. |
| 428 assertEquals('https://google.com', testElement.sites[2].origin); | 440 Polymer.dom.flush(); |
| 429 assertEquals(undefined, testElement.selectedOrigin); | 441 |
| 430 | 442 assertTrue(testElement.$.category.opened); |
| 431 // Validate that the sites are shown in UI and can be selected. | 443 assertFalse(testElement.$.category.hidden); |
| 432 var firstItem = testElement.$.listContainer.items[1]; | 444 // Validate that the sites gets populated from pre-canned prefs. |
| 433 var clickable = firstItem.querySelector('.flex paper-item-body'); | 445 // If this fails with 5 instead of the expected 3, then the |
| 434 assertNotEquals(undefined, clickable); | 446 // de-duping of sites is not working for site_list. |
| 435 MockInteractions.tap(clickable); | 447 assertEquals(3, testElement.sites.length); |
| 436 assertEquals('https://foo.com', testElement.selectedSite.origin); | 448 assertEquals('https://bar.com', testElement.sites[0].origin); |
| 437 }.bind(this)); | 449 assertEquals('https://foo.com', testElement.sites[1].origin); |
| 450 assertEquals('https://google.com', testElement.sites[2].origin); | |
| 451 assertEquals(undefined, testElement.selectedOrigin); | |
| 452 | |
| 453 // Validate that the sites are shown in UI and can be selected. | |
| 454 var firstItem = testElement.$.listContainer.items[1]; | |
| 455 var clickable = firstItem.querySelector('.flex paper-item-body'); | |
| 456 assertNotEquals(undefined, clickable); | |
| 457 MockInteractions.tap(clickable); | |
| 458 assertEquals('https://foo.com', testElement.selectedSite.origin); | |
| 459 | |
| 460 done(); | |
| 461 }); | |
| 462 }); | |
| 438 }); | 463 }); |
| 439 | 464 |
| 440 test('Mixed schemes (present and absent)', function() { | 465 test('Mixed schemes (present and absent)', function() { |
| 441 return runAndResolveWhenSitesChanged(function() { | 466 // Prefs: One item with scheme and one without. |
| 442 // Prefs: One item with scheme and one without. | 467 setupLocationCategory(settings.PermissionValues.ALLOW, |
| 443 setupLocationCategory(settings.PermissionValues.ALLOW, | 468 prefsMixedSchemes); |
| 444 prefsMixedSchemes); | 469 return browserProxy.whenCalled('getExceptionList').then( |
| 445 }).then(function() { | 470 function(contentType) { |
| 446 // No further checks needed. If this fails, it will hang the test. | 471 // No further checks needed. If this fails, it will hang the test. |
| 447 }.bind(this)); | 472 }); |
| 448 }); | 473 }); |
| 449 }); | 474 }); |
| 450 } | 475 } |
| 451 return { | 476 return { |
| 452 registerTests: registerTests, | 477 registerTests: registerTests, |
| 453 }; | 478 }; |
| 454 }); | 479 }); |
| OLD | NEW |