1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/sessionstore/test/browser_frametree.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,131 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +"use strict"; 1.8 + 1.9 +const URL = ROOT + "browser_frametree_sample.html"; 1.10 +const URL_FRAMESET = ROOT + "browser_frametree_sample_frameset.html"; 1.11 + 1.12 +/** 1.13 + * This ensures that loading a page normally, aborting a page load, reloading 1.14 + * a page, navigating using the bfcache, and ignoring frames that were 1.15 + * created dynamically work as expect. We expect the frame tree to be reset 1.16 + * when a page starts loading and we also expect a valid frame tree to exist 1.17 + * when it has stopped loading. 1.18 + */ 1.19 +add_task(function test_frametree() { 1.20 + const FRAME_TREE_SINGLE = { href: URL }; 1.21 + const FRAME_TREE_FRAMESET = { 1.22 + href: URL_FRAMESET, 1.23 + children: [{href: URL}, {href: URL}, {href: URL}] 1.24 + }; 1.25 + 1.26 + // Create a tab with a single frame. 1.27 + let tab = gBrowser.addTab(URL); 1.28 + let browser = tab.linkedBrowser; 1.29 + yield promiseNewFrameTree(browser); 1.30 + yield checkFrameTree(browser, FRAME_TREE_SINGLE, 1.31 + "loading a page resets and creates the frame tree correctly"); 1.32 + 1.33 + // Load the frameset and create two frames dynamically, the first on 1.34 + // DOMContentLoaded and the second on load. 1.35 + yield sendMessage(browser, "ss-test:createDynamicFrames", {id: "frames", url: URL}); 1.36 + browser.loadURI(URL_FRAMESET); 1.37 + yield promiseNewFrameTree(browser); 1.38 + yield checkFrameTree(browser, FRAME_TREE_FRAMESET, 1.39 + "dynamic frames created on or after the load event are ignored"); 1.40 + 1.41 + // Go back to the previous single-frame page. There will be no load event as 1.42 + // the page is still in the bfcache. We thus make sure this type of navigation 1.43 + // resets the frame tree. 1.44 + browser.goBack(); 1.45 + yield promiseNewFrameTree(browser); 1.46 + yield checkFrameTree(browser, FRAME_TREE_SINGLE, 1.47 + "loading from bfache resets and creates the frame tree correctly"); 1.48 + 1.49 + // Load the frameset again but abort the load early. 1.50 + // The frame tree should still be reset and created. 1.51 + browser.loadURI(URL_FRAMESET); 1.52 + executeSoon(() => browser.stop()); 1.53 + yield promiseNewFrameTree(browser); 1.54 + 1.55 + // Load the frameset and check the tree again. 1.56 + yield sendMessage(browser, "ss-test:createDynamicFrames", {id: "frames", url: URL}); 1.57 + browser.loadURI(URL_FRAMESET); 1.58 + yield promiseNewFrameTree(browser); 1.59 + yield checkFrameTree(browser, FRAME_TREE_FRAMESET, 1.60 + "reloading a page resets and creates the frame tree correctly"); 1.61 + 1.62 + // Cleanup. 1.63 + gBrowser.removeTab(tab); 1.64 +}); 1.65 + 1.66 +/** 1.67 + * This test ensures that we ignore frames that were created dynamically at or 1.68 + * after the load event. SessionStore can't handle these and will not restore 1.69 + * or collect any data for them. 1.70 + */ 1.71 +add_task(function test_frametree_dynamic() { 1.72 + // The frame tree as expected. The first two frames are static 1.73 + // and the third one was created on DOMContentLoaded. 1.74 + const FRAME_TREE = { 1.75 + href: URL_FRAMESET, 1.76 + children: [{href: URL}, {href: URL}, {href: URL}] 1.77 + }; 1.78 + const FRAME_TREE_REMOVED = { 1.79 + href: URL_FRAMESET, 1.80 + children: [{href: URL}, {href: URL}] 1.81 + }; 1.82 + 1.83 + // Add an empty tab for a start. 1.84 + let tab = gBrowser.addTab("about:blank"); 1.85 + let browser = tab.linkedBrowser; 1.86 + yield promiseBrowserLoaded(browser); 1.87 + 1.88 + // Create dynamic frames on "DOMContentLoaded" and on "load". 1.89 + yield sendMessage(browser, "ss-test:createDynamicFrames", {id: "frames", url: URL}); 1.90 + browser.loadURI(URL_FRAMESET); 1.91 + yield promiseNewFrameTree(browser); 1.92 + 1.93 + // Check that the frame tree does not contain the frame created on "load". 1.94 + // The two static frames and the one created on DOMContentLoaded must be in 1.95 + // the tree. 1.96 + yield checkFrameTree(browser, FRAME_TREE, 1.97 + "frame tree contains first four frames"); 1.98 + 1.99 + // Remove the last frame in the frameset. 1.100 + yield sendMessage(browser, "ss-test:removeLastFrame", {id: "frames"}); 1.101 + // Check that the frame tree didn't change. 1.102 + yield checkFrameTree(browser, FRAME_TREE, 1.103 + "frame tree contains first four frames"); 1.104 + 1.105 + // Remove the last frame in the frameset. 1.106 + yield sendMessage(browser, "ss-test:removeLastFrame", {id: "frames"}); 1.107 + // Check that the frame tree excludes the removed frame. 1.108 + yield checkFrameTree(browser, FRAME_TREE_REMOVED, 1.109 + "frame tree contains first three frames"); 1.110 + 1.111 + // Cleanup. 1.112 + gBrowser.removeTab(tab); 1.113 +}); 1.114 + 1.115 +/** 1.116 + * Checks whether the current frame hierarchy of a given |browser| matches the 1.117 + * |expected| frame hierarchy. 1.118 + */ 1.119 +function checkFrameTree(browser, expected, msg) { 1.120 + return sendMessage(browser, "ss-test:mapFrameTree").then(tree => { 1.121 + is(JSON.stringify(tree), JSON.stringify(expected), msg); 1.122 + }); 1.123 +} 1.124 + 1.125 +/** 1.126 + * Returns a promise that will be resolved when the given |browser| has loaded 1.127 + * and we received messages saying that its frame tree has been reset and 1.128 + * recollected. 1.129 + */ 1.130 +function promiseNewFrameTree(browser) { 1.131 + let reset = promiseContentMessage(browser, "ss-test:onFrameTreeCollected"); 1.132 + let collect = promiseContentMessage(browser, "ss-test:onFrameTreeCollected"); 1.133 + return Promise.all([reset, collect]); 1.134 +}