browser/metro/base/tests/mochitest/browser_topsites.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* Any copyright is dedicated to the Public Domain.
michael@0 4 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 5
michael@0 6 "use strict";
michael@0 7
michael@0 8 //////////////////////////////////////////////////////////////////////////
michael@0 9 // Test helpers
michael@0 10
michael@0 11 let TopSitesTestHelper = {
michael@0 12 get grid() {
michael@0 13 return Browser.selectedBrowser.contentDocument.getElementById("start-topsites-grid");
michael@0 14 },
michael@0 15 get document() {
michael@0 16 return Browser.selectedBrowser.contentDocument;
michael@0 17 },
michael@0 18 setup: function() {
michael@0 19 return Task.spawn(function(){
michael@0 20 if (BrowserUI.isStartTabVisible)
michael@0 21 return;
michael@0 22
michael@0 23 yield addTab("about:start");
michael@0 24
michael@0 25 yield waitForCondition(() => BrowserUI.isStartTabVisible);
michael@0 26 });
michael@0 27 },
michael@0 28 mockLinks: function th_mockLinks(aLinks) {
michael@0 29 // create link objects where index. corresponds to grid position
michael@0 30 // falsy values are set to null
michael@0 31 let links = (typeof aLinks == "string") ?
michael@0 32 aLinks.split(/\s*,\s*/) : aLinks;
michael@0 33
michael@0 34 links = links.map(function (id) {
michael@0 35 return (id) ? {url: "http://example.com/#" + id, title: id} : null;
michael@0 36 });
michael@0 37 return links;
michael@0 38 },
michael@0 39 siteFromNode: function th_siteFromNode(aNode) {
michael@0 40 return {
michael@0 41 url: aNode.getAttribute("value"),
michael@0 42 title: aNode.getAttribute("label")
michael@0 43 }
michael@0 44 },
michael@0 45 clearHistory: function th_clearHistory() {
michael@0 46 PlacesUtils.history.removeAllPages();
michael@0 47 },
michael@0 48 fillHistory: function th_fillHistory(aLinks) {
michael@0 49 return Task.spawn(function(){
michael@0 50 let numLinks = aLinks.length;
michael@0 51 let transitionLink = Ci.nsINavHistoryService.TRANSITION_LINK;
michael@0 52
michael@0 53 let updateDeferred = Promise.defer();
michael@0 54
michael@0 55 for (let link of aLinks.reverse()) {
michael@0 56 let place = {
michael@0 57 uri: Util.makeURI(link.url),
michael@0 58 title: link.title,
michael@0 59 visits: [{visitDate: Date.now() * 1000, transitionType: transitionLink}]
michael@0 60 };
michael@0 61 try {
michael@0 62 PlacesUtils.asyncHistory.updatePlaces(place, {
michael@0 63 handleError: function (aError) {
michael@0 64 ok(false, "couldn't add visit to history");
michael@0 65 throw new Task.Result(aError);
michael@0 66 },
michael@0 67 handleResult: function () {},
michael@0 68 handleCompletion: function () {
michael@0 69 if(--numLinks <= 0) {
michael@0 70 updateDeferred.resolve(true);
michael@0 71 }
michael@0 72 }
michael@0 73 });
michael@0 74 } catch(e) {
michael@0 75 ok(false, "because: " + e);
michael@0 76 }
michael@0 77 }
michael@0 78 return updateDeferred.promise;
michael@0 79 });
michael@0 80 },
michael@0 81
michael@0 82 /**
michael@0 83 * Allows to specify the list of pinned links (that have a fixed position in
michael@0 84 * the grid.
michael@0 85 * @param aLinksPattern the pattern (see below)
michael@0 86 *
michael@0 87 * Example: setPinnedLinks("foo,,bar")
michael@0 88 * Result: 'http://example.com/#foo' is pinned in the first cell. 'http://example.com/#bar' is
michael@0 89 * pinned in the third cell.
michael@0 90 */
michael@0 91 setPinnedLinks: function th_setPinnedLinks(aLinks) {
michael@0 92 let links = TopSitesTestHelper.mockLinks(aLinks);
michael@0 93
michael@0 94 // (we trust that NewTabUtils works, and test our consumption of it)
michael@0 95 // clear all existing pins
michael@0 96 Array.forEach(NewTabUtils.pinnedLinks.links, function(aLink){
michael@0 97 if(aLink)
michael@0 98 NewTabUtils.pinnedLinks.unpin(aLink);
michael@0 99 });
michael@0 100
michael@0 101 links.forEach(function(aLink, aIndex){
michael@0 102 if(aLink) {
michael@0 103 NewTabUtils.pinnedLinks.pin(aLink, aIndex);
michael@0 104 }
michael@0 105 });
michael@0 106 NewTabUtils.pinnedLinks.save();
michael@0 107 },
michael@0 108
michael@0 109 /**
michael@0 110 * Allows to provide a list of links that is used to construct the grid.
michael@0 111 * @param aLinksPattern the pattern (see below)
michael@0 112 * @param aPinnedLinksPattern the pattern (see below)
michael@0 113 *
michael@0 114 * Example: setLinks("dougal,florence,zebedee")
michael@0 115 * Result: [{url: "http://example.com/#dougal", title: "dougal"},
michael@0 116 * {url: "http://example.com/#florence", title: "florence"}
michael@0 117 * {url: "http://example.com/#zebedee", title: "zebedee"}]
michael@0 118 * Example: setLinks("dougal,florence,zebedee","dougal,,zebedee")
michael@0 119 * Result: http://example.com/#dougal is pinned at index 0, http://example.com/#florence at index 2
michael@0 120 */
michael@0 121
michael@0 122 setLinks: function th_setLinks(aLinks, aPinnedLinks) {
michael@0 123 let links = TopSitesTestHelper.mockLinks(aLinks);
michael@0 124 if (links.filter(function(aLink){
michael@0 125 return !aLink;
michael@0 126 }).length) {
michael@0 127 throw new Error("null link objects in setLinks");
michael@0 128 }
michael@0 129
michael@0 130 return Task.spawn(function() {
michael@0 131 TopSitesTestHelper.clearHistory();
michael@0 132
michael@0 133 yield Task.spawn(TopSitesTestHelper.fillHistory(links));
michael@0 134
michael@0 135 if(aPinnedLinks) {
michael@0 136 TopSitesTestHelper.setPinnedLinks(aPinnedLinks);
michael@0 137 }
michael@0 138
michael@0 139 // reset the TopSites state, have it update its cache with the new data fillHistory put there
michael@0 140 yield TopSites.prepareCache(true);
michael@0 141 });
michael@0 142 },
michael@0 143
michael@0 144 updatePagesAndWait: function th_updatePagesAndWait() {
michael@0 145 let deferredUpdate = Promise.defer();
michael@0 146 let updater = {
michael@0 147 update: function() {
michael@0 148 NewTabUtils.allPages.unregister(updater);
michael@0 149 deferredUpdate.resolve(true);
michael@0 150 }
michael@0 151 };
michael@0 152 NewTabUtils.allPages.register(updater);
michael@0 153 setTimeout(function() {
michael@0 154 NewTabUtils.allPages.update();
michael@0 155 }, 0);
michael@0 156 return deferredUpdate.promise;
michael@0 157 },
michael@0 158
michael@0 159 tearDown: function th_tearDown() {
michael@0 160 TopSitesTestHelper.clearHistory();
michael@0 161 }
michael@0 162 };
michael@0 163
michael@0 164
michael@0 165 //////////////////////////////////////////////////////////////////////////
michael@0 166
michael@0 167
michael@0 168 function test() {
michael@0 169 registerCleanupFunction(TopSitesTestHelper.tearDown);
michael@0 170 runTests();
michael@0 171 }
michael@0 172
michael@0 173 gTests.push({
michael@0 174 desc: "TopSites dependencies",
michael@0 175 run: function() {
michael@0 176 ok(NewTabUtils, "NewTabUtils is truthy");
michael@0 177 ok(TopSites, "TopSites is truthy");
michael@0 178 }
michael@0 179 });
michael@0 180
michael@0 181 gTests.push({
michael@0 182 desc: "load and display top sites",
michael@0 183 setUp: function() {
michael@0 184 yield TopSitesTestHelper.setup();
michael@0 185 let grid = TopSitesTestHelper.grid;
michael@0 186
michael@0 187 // setup - set history to known state
michael@0 188 yield TopSitesTestHelper.setLinks("brian,dougal,dylan,ermintrude,florence,moose,sgtsam,train,zebedee,zeebad");
michael@0 189
michael@0 190 let arrangedPromise = waitForEvent(grid, "arranged");
michael@0 191 yield TopSitesTestHelper.updatePagesAndWait();
michael@0 192 // pause until the update has fired and the view is finishd updating
michael@0 193 yield arrangedPromise;
michael@0 194 },
michael@0 195 run: function() {
michael@0 196 let grid = TopSitesTestHelper.grid;
michael@0 197 let items = grid.items;
michael@0 198 is(items.length, 8, "should be 8 topsites"); // i.e. not 10
michael@0 199 if(items.length) {
michael@0 200 let firstitem = items[0];
michael@0 201 is(
michael@0 202 firstitem.getAttribute("label"),
michael@0 203 "brian",
michael@0 204 "first item label should be 'brian': " + firstitem.getAttribute("label")
michael@0 205 );
michael@0 206 is(
michael@0 207 firstitem.getAttribute("value"),
michael@0 208 "http://example.com/#brian",
michael@0 209 "first item url should be 'http://example.com/#brian': " + firstitem.getAttribute("url")
michael@0 210 );
michael@0 211 }
michael@0 212 }
michael@0 213 });
michael@0 214
michael@0 215 gTests.push({
michael@0 216 desc: "pinned sites",
michael@0 217 pins: "dangermouse,zebedee,,,dougal",
michael@0 218 setUp: function() {
michael@0 219 yield TopSitesTestHelper.setup();
michael@0 220 // setup - set history to known state
michael@0 221 yield TopSitesTestHelper.setLinks(
michael@0 222 "brian,dougal,dylan,ermintrude,florence,moose,sgtsam,train,zebedee,zeebad",
michael@0 223 this.pins
michael@0 224 );
michael@0 225 // pause until the update has fired and the view is finishd updating
michael@0 226 let arrangedPromise = waitForEvent(TopSitesTestHelper.grid, "arranged");
michael@0 227 yield TopSitesTestHelper.updatePagesAndWait();
michael@0 228 yield arrangedPromise;
michael@0 229 },
michael@0 230 run: function() {
michael@0 231 // test that pinned state of each site as rendered matches our expectations
michael@0 232 let pins = this.pins.split(",");
michael@0 233 let items = TopSitesTestHelper.grid.items;
michael@0 234 is(items.length, 8, "should be 8 topsites in the grid");
michael@0 235
michael@0 236 is(TopSitesTestHelper.document.querySelectorAll("#start-topsites-grid > [pinned]").length, 3, "should be 3 children with 'pinned' attribute");
michael@0 237 try {
michael@0 238 Array.forEach(items, function(aItem, aIndex){
michael@0 239 // pinned state should agree with the pins array
michael@0 240 is(
michael@0 241 aItem.hasAttribute("pinned"), !!pins[aIndex],
michael@0 242 "site at index " + aIndex + " was " +aItem.hasAttribute("pinned")
michael@0 243 +", should agree with " + !!pins[aIndex]
michael@0 244 );
michael@0 245 if (pins[aIndex]) {
michael@0 246 is(
michael@0 247 aItem.getAttribute("label"),
michael@0 248 pins[aIndex],
michael@0 249 "pinned site has correct label: " + pins[aIndex] +"=="+ aItem.getAttribute("label")
michael@0 250 );
michael@0 251 }
michael@0 252 }, this);
michael@0 253 } catch(e) {
michael@0 254 ok(false, this.desc + ": Test of pinned state on items failed");
michael@0 255 info("because: " + e.message + "\n" + e.stack);
michael@0 256 }
michael@0 257 }
michael@0 258 });
michael@0 259
michael@0 260 gTests.push({
michael@0 261 desc: "pin site",
michael@0 262 setUp: function() {
michael@0 263 yield TopSitesTestHelper.setup();
michael@0 264 // setup - set history to known state
michael@0 265 yield TopSitesTestHelper.setLinks("sgtsam,train,zebedee,zeebad", []); // nothing initially pinned
michael@0 266 // pause until the update has fired and the view is finishd updating
michael@0 267 let arrangedPromise = waitForEvent(TopSitesTestHelper.grid, "arranged");
michael@0 268 yield TopSitesTestHelper.updatePagesAndWait();
michael@0 269 yield arrangedPromise;
michael@0 270 },
michael@0 271 run: function() {
michael@0 272 // pin a site
michael@0 273 // test that site is pinned as expected
michael@0 274 // and that sites fill positions around it
michael@0 275 let grid = TopSitesTestHelper.grid,
michael@0 276 items = grid.items;
michael@0 277 is(items.length, 4, this.desc + ": should be 4 topsites");
michael@0 278
michael@0 279 let tile = grid.items[2],
michael@0 280 url = tile.getAttribute("value"),
michael@0 281 title = tile.getAttribute("label");
michael@0 282
michael@0 283 info(this.desc + ": pinning site at index 2");
michael@0 284 TopSites.pinSites([{
michael@0 285 url: url,
michael@0 286 title: title
michael@0 287 }], [2]);
michael@0 288
michael@0 289 // pinning shouldn't require re-arranging - just wait for isUpdating flag to flip
michael@0 290 yield waitForCondition(function(){
michael@0 291 return !grid.controller.isUpdating;
michael@0 292 });
michael@0 293
michael@0 294 let thirdTile = grid.items[2];
michael@0 295 ok( thirdTile.hasAttribute("pinned"), thirdTile.getAttribute("value")+ " should look pinned" );
michael@0 296
michael@0 297 // visit some more sites
michael@0 298 yield TopSitesTestHelper.fillHistory( TopSitesTestHelper.mockLinks("brian,dougal,dylan,ermintrude,florence,moose") );
michael@0 299
michael@0 300 // force flush and repopulation of links cache
michael@0 301 yield TopSites.prepareCache(true);
michael@0 302 // pause until the update has fired and the view is finishd updating
michael@0 303 let arrangedPromise = waitForEvent(grid, "arranged");
michael@0 304 yield TopSitesTestHelper.updatePagesAndWait();
michael@0 305 yield arrangedPromise;
michael@0 306
michael@0 307 // check zebedee is still pinned at index 2
michael@0 308 is( items[2].getAttribute("label"), "zebedee", "Pinned site remained at its index" );
michael@0 309 ok( items[2].hasAttribute("pinned"), "3rd site should still look pinned" );
michael@0 310 }
michael@0 311 });
michael@0 312
michael@0 313 gTests.push({
michael@0 314 desc: "unpin site",
michael@0 315 pins: ",zebedee",
michael@0 316 setUp: function() {
michael@0 317 yield TopSitesTestHelper.setup();
michael@0 318 // setup - set history to known state
michael@0 319 yield TopSitesTestHelper.setLinks(
michael@0 320 "brian,dougal,dylan,ermintrude,florence,moose,sgtsam,train,zebedee,zeebad",
michael@0 321 this.pins
michael@0 322 );
michael@0 323 // pause until the update has fired and the view is finishd updating
michael@0 324 let arrangedPromise = waitForEvent(TopSitesTestHelper.grid, "arranged");
michael@0 325 yield TopSitesTestHelper.updatePagesAndWait();
michael@0 326 yield arrangedPromise;
michael@0 327 },
michael@0 328 run: function() {
michael@0 329 // unpin a pinned site
michael@0 330 // test that sites are unpinned as expected
michael@0 331 let grid = TopSitesTestHelper.grid,
michael@0 332 items = grid.items;
michael@0 333 is(items.length, 8, this.desc + ": should be 8 topsites");
michael@0 334 let site = {
michael@0 335 url: items[1].getAttribute("value"),
michael@0 336 title: items[1].getAttribute("label")
michael@0 337 };
michael@0 338 // verify assumptions before unpinning this site
michael@0 339 ok( NewTabUtils.pinnedLinks.isPinned(site), "2nd item is pinned" );
michael@0 340 ok( items[1].hasAttribute("pinned"), "2nd item has pinned attribute" );
michael@0 341
michael@0 342 // unpinning shouldn't require re-arranging - just wait for isUpdating flag to flip
michael@0 343 TopSites.unpinSites([site]);
michael@0 344
michael@0 345 yield waitForCondition(function(){
michael@0 346 return !grid.controller.isUpdating;
michael@0 347 });
michael@0 348
michael@0 349 let secondTile = grid.items[1];
michael@0 350 ok( !secondTile.hasAttribute("pinned"), "2nd item should no longer be marked as pinned" );
michael@0 351 ok( !NewTabUtils.pinnedLinks.isPinned(site), "2nd item should no longer be pinned" );
michael@0 352 }
michael@0 353 });
michael@0 354
michael@0 355 gTests.push({
michael@0 356 desc: "block/unblock sites",
michael@0 357 setUp: function() {
michael@0 358 yield TopSitesTestHelper.setup();
michael@0 359 // setup - set topsites to known state
michael@0 360 yield TopSitesTestHelper.setLinks(
michael@0 361 "brian,dougal,dylan,ermintrude,florence,moose,sgtsam,train,zebedee,zeebad,basic,coral",
michael@0 362 ",dougal"
michael@0 363 );
michael@0 364 // pause until the update has fired and the view is finishd updating
michael@0 365 let arrangedPromise = waitForEvent(TopSitesTestHelper.grid, "arranged");
michael@0 366 yield TopSitesTestHelper.updatePagesAndWait();
michael@0 367 yield arrangedPromise;
michael@0 368 },
michael@0 369 run: function() {
michael@0 370 try {
michael@0 371 // block a site
michael@0 372 // test that sites are removed from the grid as expected
michael@0 373 let grid = TopSitesTestHelper.grid,
michael@0 374 items = grid.items;
michael@0 375 is(items.length, 8, this.desc + ": should be 8 topsites");
michael@0 376
michael@0 377 let brianSite = TopSitesTestHelper.siteFromNode(items[0]);
michael@0 378 let dougalSite = TopSitesTestHelper.siteFromNode(items[1]);
michael@0 379 let dylanSite = TopSitesTestHelper.siteFromNode(items[2]);
michael@0 380
michael@0 381 let arrangedPromise = waitForEvent(grid, "arranged");
michael@0 382 // we'll block brian (he's not pinned)
michael@0 383 TopSites.hideSites([brianSite]);
michael@0 384 // pause until the update has fired and the view is finished updating
michael@0 385 yield arrangedPromise;
michael@0 386
michael@0 387 // verify brian is blocked and removed from the grid
michael@0 388 ok( (new Site(brianSite)).blocked, "Site has truthy blocked property" );
michael@0 389 ok( NewTabUtils.blockedLinks.isBlocked(brianSite), "Site was blocked" );
michael@0 390 is( grid.querySelectorAll("[value='"+brianSite.url+"']").length, 0, "Blocked site was removed from grid");
michael@0 391
michael@0 392 // make sure the empty slot was backfilled
michael@0 393 is(items.length, 8, this.desc + ": should be 8 topsites");
michael@0 394
michael@0 395 arrangedPromise = waitForEvent(grid, "arranged");
michael@0 396 // block dougal,dylan. dougal is currently pinned at index 1
michael@0 397 TopSites.hideSites([dougalSite, dylanSite]);
michael@0 398 // pause until the update has fired and the view is finished updating
michael@0 399 yield arrangedPromise;
michael@0 400
michael@0 401 // verify dougal is blocked and removed from the grid
michael@0 402 ok( (new Site(dougalSite)).blocked, "Site has truthy blocked property" );
michael@0 403 ok( NewTabUtils.blockedLinks.isBlocked(dougalSite), "Site was blocked" );
michael@0 404 ok( !NewTabUtils.pinnedLinks.isPinned(dougalSite), "Blocked Site is no longer pinned" );
michael@0 405 is( grid.querySelectorAll("[value='"+dougalSite.url+"']").length, 0, "Blocked site was removed from grid");
michael@0 406
michael@0 407 // verify dylan is blocked and removed from the grid
michael@0 408 ok( (new Site(dylanSite)).blocked, "Site has truthy blocked property" );
michael@0 409 ok( NewTabUtils.blockedLinks.isBlocked(dylanSite), "Site was blocked" );
michael@0 410 ok( !NewTabUtils.pinnedLinks.isPinned(dylanSite), "Blocked Site is no longer pinned" );
michael@0 411 is( grid.querySelectorAll("[value='"+dylanSite.url+"']").length, 0, "Blocked site was removed from grid");
michael@0 412
michael@0 413 // make sure the empty slots were backfilled
michael@0 414 is(items.length, 8, this.desc + ": should be 8 topsites");
michael@0 415
michael@0 416 arrangedPromise = waitForEvent(grid, "arranged");
michael@0 417 TopSites.restoreSites([brianSite, dougalSite, dylanSite]);
michael@0 418 // pause until the update has fired and the view is finished updating
michael@0 419 yield arrangedPromise;
michael@0 420
michael@0 421 // verify brian, dougal and dyland are unblocked and back in the grid
michael@0 422 ok( !NewTabUtils.blockedLinks.isBlocked(brianSite), "site was unblocked" );
michael@0 423 is( grid.querySelectorAll("[value='"+brianSite.url+"']").length, 1, "Unblocked site is back in the grid");
michael@0 424
michael@0 425 ok( !NewTabUtils.blockedLinks.isBlocked(dougalSite), "site was unblocked" );
michael@0 426 is( grid.querySelectorAll("[value='"+dougalSite.url+"']").length, 1, "Unblocked site is back in the grid");
michael@0 427 // ..and that a previously pinned site is re-pinned after being blocked, then restored
michael@0 428 ok( NewTabUtils.pinnedLinks.isPinned(dougalSite), "Restoring previously pinned site makes it pinned again" );
michael@0 429 is( grid.items[1].getAttribute("value"), dougalSite.url, "Blocked Site restored to pinned index" );
michael@0 430
michael@0 431 ok( !NewTabUtils.blockedLinks.isBlocked(dylanSite), "site was unblocked" );
michael@0 432 is( grid.querySelectorAll("[value='"+dylanSite.url+"']").length, 1, "Unblocked site is back in the grid");
michael@0 433
michael@0 434 } catch(ex) {
michael@0 435
michael@0 436 ok(false, this.desc+": Caught exception in test: " + ex);
michael@0 437 info("trace: " + ex.stack);
michael@0 438 }
michael@0 439 }
michael@0 440 });
michael@0 441
michael@0 442 gTests.push({
michael@0 443 desc: "delete and restore site tiles",
michael@0 444 pins: "brian",
michael@0 445 setUp: function() {
michael@0 446 yield TopSitesTestHelper.setup();
michael@0 447 // setup - set history to known state
michael@0 448 yield TopSitesTestHelper.setLinks(
michael@0 449 "brian,dougal,dylan,ermintrude",
michael@0 450 this.pins
michael@0 451 );
michael@0 452 // pause until the update has fired and the view is finishd updating
michael@0 453 let arrangedPromise = waitForEvent(TopSitesTestHelper.grid, "arranged");
michael@0 454 yield TopSitesTestHelper.updatePagesAndWait();
michael@0 455 yield arrangedPromise;
michael@0 456 },
michael@0 457 run: function() {
michael@0 458 // delete a both pinned and unpinned sites
michael@0 459 // test that sites are removed from the grid
michael@0 460 let grid = TopSitesTestHelper.grid,
michael@0 461 items = grid.items;
michael@0 462 is(items.length, 4, this.desc + ": should be 4 topsites");
michael@0 463
michael@0 464 let brianTile = grid.querySelector('richgriditem[value$="brian"]');
michael@0 465 let dougalTile = grid.querySelector('richgriditem[value$="dougal"]')
michael@0 466
michael@0 467 // verify assumptions before deleting sites
michael@0 468 ok( brianTile, "Tile for Brian was created");
michael@0 469 ok( dougalTile, "Tile for Dougal was created");
michael@0 470
michael@0 471 let brianSite = TopSitesTestHelper.siteFromNode(brianTile);
michael@0 472 let dougalSite = TopSitesTestHelper.siteFromNode(dougalTile);
michael@0 473 ok( NewTabUtils.pinnedLinks.isPinned( brianSite ), "Brian tile is pinned" );
michael@0 474
michael@0 475 // select the 2 tiles
michael@0 476 grid.toggleItemSelection(brianTile);
michael@0 477 grid.toggleItemSelection(dougalTile);
michael@0 478 is(grid.selectedItems.length, 2, "2 tiles were selected");
michael@0 479
michael@0 480 // pause until the update has fired and the view is finishd updating
michael@0 481 let arrangedPromise = waitForEvent(grid, "arranged");
michael@0 482
michael@0 483 // raise a mock context-action event to trigger deletion of the selection
michael@0 484 let event = document.createEvent("Events");
michael@0 485 event.action = "delete";
michael@0 486 event.initEvent("context-action", true, true); // is cancelable
michael@0 487 grid.dispatchEvent(event);
michael@0 488
michael@0 489 yield arrangedPromise;
michael@0 490
michael@0 491 // those sites are blocked and their tiles have been removed from the grid?
michael@0 492 ok( !grid.querySelector('richgriditem[value="'+brianSite.value+'"]'));
michael@0 493 ok( !grid.querySelector('richgriditem[value="'+dougalSite.value+'"]'));
michael@0 494 ok( NewTabUtils.blockedLinks.isBlocked(brianSite), "Brian site was blocked" );
michael@0 495 ok( NewTabUtils.blockedLinks.isBlocked(dougalSite), "Dougal site was blocked" );
michael@0 496 // with the tiles deleted, selection should be empty
michael@0 497 is( grid.selectedItems.length, 0, "Gris selection is empty after deletion" );
michael@0 498
michael@0 499 // raise a mock context-action event to trigger restore
michael@0 500 arrangedPromise = waitForEvent(grid, "arranged");
michael@0 501 event = document.createEvent("Events");
michael@0 502 event.action = "restore";
michael@0 503 event.initEvent("context-action", true, true); // is cancelable
michael@0 504 grid.dispatchEvent(event);
michael@0 505
michael@0 506 yield arrangedPromise;
michael@0 507 brianTile = grid.querySelector('richgriditem[value$="brian"]');
michael@0 508 dougalTile = grid.querySelector('richgriditem[value$="dougal"]');
michael@0 509
michael@0 510 // those tiles have been restored to the grid?
michael@0 511 ok( brianTile, "First tile was restored to the grid" );
michael@0 512 ok( dougalTile, "2nd tile was restored to the grid" );
michael@0 513
michael@0 514 is(grid.selectedItems.length, 2, "2 tiles are still selected");
michael@0 515 is( grid.selectedItems[0], brianTile, "Brian is still selected" );
michael@0 516 is( grid.selectedItems[1], dougalTile, "Dougal is still selected" );
michael@0 517 ok( NewTabUtils.pinnedLinks.isPinned( brianSite ), "Brian tile is still pinned" );
michael@0 518 ok( !NewTabUtils.blockedLinks.isBlocked(brianSite), "Brian site was unblocked" );
michael@0 519 ok( !NewTabUtils.blockedLinks.isBlocked(dougalSite), "Dougal site was unblocked" );
michael@0 520
michael@0 521 }
michael@0 522 });

mercurial