browser/components/sessionstore/test/browser_490040.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 function test() {
     6   /** Test for Bug 490040 **/
     8   waitForExplicitFinish();
    10   function testWithState(aState) {
    11     // Ensure we can store the window if needed.
    12     let curClosedWindowCount = ss.getClosedWindowCount();
    13     gPrefService.setIntPref("browser.sessionstore.max_windows_undo",
    14                             curClosedWindowCount + 1);
    16     var origWin;
    17     function windowObserver(aSubject, aTopic, aData) {
    18       let theWin = aSubject.QueryInterface(Ci.nsIDOMWindow);
    19       if (origWin && theWin != origWin)
    20         return;
    22       switch (aTopic) {
    23         case "domwindowopened":
    24           origWin = theWin;
    25           theWin.addEventListener("load", function () {
    26             theWin.removeEventListener("load", arguments.callee, false);
    27             executeSoon(function () {
    28               // Close the window as soon as the first tab loads, or
    29               // immediately if there are no tabs.
    30               if (aState.windowState.windows[0].tabs[0].entries.length) {
    31                 theWin.gBrowser.addEventListener("load", function() {
    32                   theWin.gBrowser.removeEventListener("load",
    33                                                       arguments.callee, true);
    34                   theWin.close();
    35                 }, true);
    36               } else {
    37                 executeSoon(function () {
    38                   theWin.close();
    39                 });
    40               }
    41               ss.setWindowState(theWin, JSON.stringify(aState.windowState),
    42                                 true);
    43             });
    44           }, false);
    45           break;
    47         case "domwindowclosed":
    48           Services.ww.unregisterNotification(windowObserver);
    49           // Use executeSoon to ensure this happens after SS observer.
    50           executeSoon(function () {
    51             is(ss.getClosedWindowCount(),
    52                curClosedWindowCount + (aState.shouldBeAdded ? 1 : 0),
    53                "That window should " + (aState.shouldBeAdded ? "" : "not ") +
    54                "be restorable");
    55             executeSoon(runNextTest);
    56           });
    57           break;
    58       }
    59     }
    60     Services.ww.registerNotification(windowObserver);
    61     Services.ww.openWindow(null,
    62                            location,
    63                            "_blank",
    64                            "chrome,all,dialog=no",
    65                            null);
    66   }
    68   // Only windows with open tabs are restorable. Windows where a lone tab is
    69   // detached may have _closedTabs, but is left with just an empty tab.
    70   let states = [
    71     {
    72       shouldBeAdded: true,
    73       windowState: {
    74         windows: [{
    75           tabs: [{ entries: [{ url: "http://example.com", title: "example.com" }] }],
    76           selected: 1,
    77           _closedTabs: []
    78         }]
    79       }
    80     },
    81     {
    82       shouldBeAdded: false,
    83       windowState: {
    84         windows: [{
    85           tabs: [{ entries: [] }],
    86           _closedTabs: []
    87         }]
    88       }
    89     },
    90     {
    91       shouldBeAdded: false,
    92       windowState: {
    93         windows: [{
    94           tabs: [{ entries: [] }],
    95           _closedTabs: [{ state: { entries: [{ url: "http://example.com", index: 1 }] } }]
    96         }]
    97       }
    98     },
    99     {
   100       shouldBeAdded: false,
   101       windowState: {
   102         windows: [{
   103           tabs: [{ entries: [] }],
   104           _closedTabs: [],
   105           extData: { keyname: "pi != " + Math.random() }
   106         }]
   107       }
   108     }
   109   ];
   111   function runNextTest() {
   112     if (states.length) {
   113       let state = states.shift();
   114       testWithState(state);
   115     }
   116     else {
   117       gPrefService.clearUserPref("browser.sessionstore.max_windows_undo");
   118       finish();
   119     }
   120   }
   121   runNextTest();
   122 }

mercurial