1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/newtab/browser_newtab_background_captures.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,100 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +/** 1.8 + * Verifies that hidden, pre-loaded newtabs don't allow background captures, and 1.9 + * when unhidden, do allow background captures. 1.10 + */ 1.11 + 1.12 +const CAPTURE_PREF = "browser.pagethumbnails.capturing_disabled"; 1.13 + 1.14 +function runTests() { 1.15 + let imports = {}; 1.16 + Cu.import("resource://gre/modules/PageThumbs.jsm", imports); 1.17 + Cu.import("resource:///modules/BrowserNewTabPreloader.jsm", imports); 1.18 + 1.19 + // Disable captures. 1.20 + let originalDisabledState = Services.prefs.getBoolPref(CAPTURE_PREF); 1.21 + Services.prefs.setBoolPref(CAPTURE_PREF, true); 1.22 + 1.23 + // Make sure the thumbnail doesn't exist yet. 1.24 + let siteName = "newtab_background_captures"; 1.25 + let url = "http://example.com/#" + siteName; 1.26 + let path = imports.PageThumbsStorage.getFilePathForURL(url); 1.27 + let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); 1.28 + file.initWithPath(path); 1.29 + try { 1.30 + file.remove(false); 1.31 + } 1.32 + catch (err) {} 1.33 + 1.34 + // Add a top site. 1.35 + yield setLinks(siteName); 1.36 + 1.37 + // We need a handle to a hidden, pre-loaded newtab so we can verify that it 1.38 + // doesn't allow background captures. Add a newtab, which triggers creation 1.39 + // of a hidden newtab, and then keep calling BrowserNewTabPreloader.newTab 1.40 + // until it returns true, meaning that it swapped the passed-in tab's docshell 1.41 + // for the hidden newtab docshell. 1.42 + let tab = gWindow.gBrowser.addTab("about:blank"); 1.43 + yield addNewTabPageTab(); 1.44 + let swapWaitCount = 0; 1.45 + let swapped = imports.BrowserNewTabPreloader.newTab(tab); 1.46 + while (!swapped) { 1.47 + if (++swapWaitCount == 10) { 1.48 + ok(false, "Timed out waiting for newtab docshell swap."); 1.49 + return; 1.50 + } 1.51 + // Give the hidden newtab some time to finish loading. 1.52 + yield wait(2000); 1.53 + info("Checking newtab swap " + swapWaitCount); 1.54 + swapped = imports.BrowserNewTabPreloader.newTab(tab); 1.55 + } 1.56 + 1.57 + // The tab's docshell is now the previously hidden newtab docshell. 1.58 + let doc = tab.linkedBrowser.contentDocument; 1.59 + isnot(doc.documentElement.getAttribute("allow-background-captures"), "true", 1.60 + "Pre-loaded docshell just synchronously swapped, so background " + 1.61 + "captures should not be allowed yet"); 1.62 + 1.63 + // Enable captures. 1.64 + Services.prefs.setBoolPref(CAPTURE_PREF, false); 1.65 + 1.66 + // Now that the newtab is visible, its allow-background-captures attribute 1.67 + // should be set eventually. 1.68 + let allowBackgroundCaptures = false; 1.69 + let mutationObserver = new MutationObserver(() => { 1.70 + mutationObserver.disconnect(); 1.71 + allowBackgroundCaptures = true; 1.72 + is(doc.documentElement.getAttribute("allow-background-captures"), "true", 1.73 + "allow-background-captures should now be true"); 1.74 + info("Waiting for thumbnail to be created after observing " + 1.75 + "allow-background-captures change"); 1.76 + }); 1.77 + mutationObserver.observe(doc.documentElement, { 1.78 + attributes: true, 1.79 + attributeFilter: ["allow-background-captures"], 1.80 + }); 1.81 + 1.82 + // And the allow-background-captures change should trigger the thumbnail 1.83 + // capture. 1.84 + Services.obs.addObserver(function onCreate(subj, topic, data) { 1.85 + if (data != url) 1.86 + return; 1.87 + ok(allowBackgroundCaptures, 1.88 + "page-thumbnail:create should be observed after " + 1.89 + "allow-background-captures was set"); 1.90 + Services.obs.removeObserver(onCreate, "page-thumbnail:create"); 1.91 + // Test finished! 1.92 + Services.prefs.setBoolPref(CAPTURE_PREF, originalDisabledState); 1.93 + file.remove(false); 1.94 + TestRunner.next(); 1.95 + }, "page-thumbnail:create", false); 1.96 + 1.97 + info("Waiting for allow-background-captures change"); 1.98 + yield true; 1.99 +} 1.100 + 1.101 +function wait(ms) { 1.102 + setTimeout(TestRunner.next, ms); 1.103 +}