browser/components/sessionstore/test/browser_frame_history.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 /**
     6  Ensure that frameset history works properly when restoring a tab,
     7  provided that the frameset is static.
     8  */
    10 // Loading a toplevel frameset
    11 add_task(function() {
    12   let testURL = getRootDirectory(gTestPath) + "browser_frame_history_index.html";
    13   let tab = gBrowser.addTab(testURL);
    14   gBrowser.selectedTab = tab;
    16   info("Opening a page with three frames, 4 loads should take place");
    17   yield waitForLoadsInBrowser(tab.linkedBrowser, 4);
    19   let browser_b = tab.linkedBrowser.contentDocument.getElementsByTagName("frame")[1];
    20   let document_b = browser_b.contentDocument;
    21   let links = document_b.getElementsByTagName("a");
    23   // We're going to click on the first link, so listen for another load event
    24   info("Clicking on link 1, 1 load should take place");
    25   let promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
    26   EventUtils.sendMouseEvent({type:"click"}, links[0], browser_b.contentWindow);
    27   yield promise;
    29   info("Clicking on link 2, 1 load should take place");
    30   promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
    31   EventUtils.sendMouseEvent({type:"click"}, links[1], browser_b.contentWindow);
    32   yield promise;
    34   info("Close then un-close page, 4 loads should take place");
    35   gBrowser.removeTab(tab);
    36   let newTab = ss.undoCloseTab(window, 0);
    37   yield waitForLoadsInBrowser(newTab.linkedBrowser, 4);
    39   info("Go back in time, 1 load should take place");
    40   gBrowser.goBack();
    41   yield waitForLoadsInBrowser(newTab.linkedBrowser, 1);
    43   let expectedURLEnds = ["a.html", "b.html", "c1.html"];
    44   let frames = newTab.linkedBrowser.contentDocument.getElementsByTagName("frame");
    45   for (let i = 0; i < frames.length; i++) {
    46     is(frames[i].contentDocument.location,
    47        getRootDirectory(gTestPath) + "browser_frame_history_" + expectedURLEnds[i],
    48        "frame " + i + " has the right url");
    49   }
    50   gBrowser.removeTab(newTab);
    51 });
    53 // Loading the frameset inside an iframe
    54 add_task(function() {
    55   let testURL = getRootDirectory(gTestPath) + "browser_frame_history_index2.html";
    56   let tab = gBrowser.addTab(testURL);
    57   gBrowser.selectedTab = tab;
    59   info("iframe: Opening a page with an iframe containing three frames, 5 loads should take place");
    60   yield waitForLoadsInBrowser(tab.linkedBrowser, 5);
    62   let browser_b = tab.linkedBrowser.contentDocument.
    63     getElementById("iframe").contentDocument.
    64     getElementsByTagName("frame")[1];
    65   let document_b = browser_b.contentDocument;
    66   let links = document_b.getElementsByTagName("a");
    68   // We're going to click on the first link, so listen for another load event
    69   info("iframe: Clicking on link 1, 1 load should take place");
    70   let promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
    71   EventUtils.sendMouseEvent({type:"click"}, links[0], browser_b.contentWindow);
    72   yield promise;
    74   info("iframe: Clicking on link 2, 1 load should take place");
    75   promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
    76   EventUtils.sendMouseEvent({type:"click"}, links[1], browser_b.contentWindow);
    77   yield promise;
    79   info("iframe: Close then un-close page, 5 loads should take place");
    80   gBrowser.removeTab(tab);
    81   let newTab = ss.undoCloseTab(window, 0);
    82   yield waitForLoadsInBrowser(newTab.linkedBrowser, 5);
    84   info("iframe: Go back in time, 1 load should take place");
    85   gBrowser.goBack();
    86   yield waitForLoadsInBrowser(newTab.linkedBrowser, 1);
    88   let expectedURLEnds = ["a.html", "b.html", "c1.html"];
    89   let frames = newTab.linkedBrowser.contentDocument.
    90     getElementById("iframe").contentDocument.
    91         getElementsByTagName("frame");
    92   for (let i = 0; i < frames.length; i++) {
    93     is(frames[i].contentDocument.location,
    94        getRootDirectory(gTestPath) + "browser_frame_history_" + expectedURLEnds[i],
    95        "frame " + i + " has the right url");
    96   }
    97   gBrowser.removeTab(newTab);
    98 });
   100 // Now, test that we don't record history if the iframe is added dynamically
   101 add_task(function() {
   102   // Start with an empty history
   103     let blankState = JSON.stringify({
   104       windows: [{
   105         tabs: [{ entries: [{ url: "about:blank" }] }],
   106         _closedTabs: []
   107       }],
   108       _closedWindows: []
   109     });
   110     ss.setBrowserState(blankState);
   112   let testURL = getRootDirectory(gTestPath) + "browser_frame_history_index_blank.html";
   113   let tab = gBrowser.addTab(testURL);
   114   gBrowser.selectedTab = tab;
   115   yield waitForLoadsInBrowser(tab.linkedBrowser, 1);
   117   info("dynamic: Opening a page with an iframe containing three frames, 4 dynamic loads should take place");
   118   let doc = tab.linkedBrowser.contentDocument;
   119   let iframe = doc.createElement("iframe");
   120   iframe.id = "iframe";
   121   iframe.src="browser_frame_history_index.html";
   122   doc.body.appendChild(iframe);
   123   yield waitForLoadsInBrowser(tab.linkedBrowser, 4);
   125   let browser_b = tab.linkedBrowser.contentDocument.
   126     getElementById("iframe").contentDocument.
   127     getElementsByTagName("frame")[1];
   128   let document_b = browser_b.contentDocument;
   129   let links = document_b.getElementsByTagName("a");
   131   // We're going to click on the first link, so listen for another load event
   132   info("dynamic: Clicking on link 1, 1 load should take place");
   133   let promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
   134   EventUtils.sendMouseEvent({type:"click"}, links[0], browser_b.contentWindow);
   135   yield promise;
   137   info("dynamic: Clicking on link 2, 1 load should take place");
   138   promise = waitForLoadsInBrowser(tab.linkedBrowser, 1);
   139   EventUtils.sendMouseEvent({type:"click"}, links[1], browser_b.contentWindow);
   140   yield promise;
   142   info("Check in the state that we have not stored this history");
   143   let state = ss.getBrowserState();
   144   info(JSON.stringify(JSON.parse(state), null, "\t"));
   145   is(state.indexOf("c1.html"), -1, "History entry was not stored in the session state");;
   146   gBrowser.removeTab(tab);
   147 });
   149 // helper functions
   150 function waitForLoadsInBrowser(aBrowser, aLoadCount) {
   151   let deferred = Promise.defer();
   152   let loadCount = 0;
   153   aBrowser.addEventListener("load", function(aEvent) {
   154     if (++loadCount < aLoadCount) {
   155       info("Got " + loadCount + " loads, waiting until we have " + aLoadCount);
   156       return;
   157     }
   159     aBrowser.removeEventListener("load", arguments.callee, true);
   160     deferred.resolve();
   161   }, true);
   162   return deferred.promise;
   163 }
   165 function timeout(delay, task) {
   166   let deferred = Promise.defer();
   167   setTimeout(() => deferred.resolve(true), delay);
   168   task.then(() => deferred.resolve(false), deferred.reject);
   169   return deferred.promise;
   170 }

mercurial