browser/base/content/test/newtab/browser_newtab_background_captures.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.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 /**
michael@0 5 * Verifies that hidden, pre-loaded newtabs don't allow background captures, and
michael@0 6 * when unhidden, do allow background captures.
michael@0 7 */
michael@0 8
michael@0 9 const CAPTURE_PREF = "browser.pagethumbnails.capturing_disabled";
michael@0 10
michael@0 11 function runTests() {
michael@0 12 let imports = {};
michael@0 13 Cu.import("resource://gre/modules/PageThumbs.jsm", imports);
michael@0 14 Cu.import("resource:///modules/BrowserNewTabPreloader.jsm", imports);
michael@0 15
michael@0 16 // Disable captures.
michael@0 17 let originalDisabledState = Services.prefs.getBoolPref(CAPTURE_PREF);
michael@0 18 Services.prefs.setBoolPref(CAPTURE_PREF, true);
michael@0 19
michael@0 20 // Make sure the thumbnail doesn't exist yet.
michael@0 21 let siteName = "newtab_background_captures";
michael@0 22 let url = "http://example.com/#" + siteName;
michael@0 23 let path = imports.PageThumbsStorage.getFilePathForURL(url);
michael@0 24 let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
michael@0 25 file.initWithPath(path);
michael@0 26 try {
michael@0 27 file.remove(false);
michael@0 28 }
michael@0 29 catch (err) {}
michael@0 30
michael@0 31 // Add a top site.
michael@0 32 yield setLinks(siteName);
michael@0 33
michael@0 34 // We need a handle to a hidden, pre-loaded newtab so we can verify that it
michael@0 35 // doesn't allow background captures. Add a newtab, which triggers creation
michael@0 36 // of a hidden newtab, and then keep calling BrowserNewTabPreloader.newTab
michael@0 37 // until it returns true, meaning that it swapped the passed-in tab's docshell
michael@0 38 // for the hidden newtab docshell.
michael@0 39 let tab = gWindow.gBrowser.addTab("about:blank");
michael@0 40 yield addNewTabPageTab();
michael@0 41 let swapWaitCount = 0;
michael@0 42 let swapped = imports.BrowserNewTabPreloader.newTab(tab);
michael@0 43 while (!swapped) {
michael@0 44 if (++swapWaitCount == 10) {
michael@0 45 ok(false, "Timed out waiting for newtab docshell swap.");
michael@0 46 return;
michael@0 47 }
michael@0 48 // Give the hidden newtab some time to finish loading.
michael@0 49 yield wait(2000);
michael@0 50 info("Checking newtab swap " + swapWaitCount);
michael@0 51 swapped = imports.BrowserNewTabPreloader.newTab(tab);
michael@0 52 }
michael@0 53
michael@0 54 // The tab's docshell is now the previously hidden newtab docshell.
michael@0 55 let doc = tab.linkedBrowser.contentDocument;
michael@0 56 isnot(doc.documentElement.getAttribute("allow-background-captures"), "true",
michael@0 57 "Pre-loaded docshell just synchronously swapped, so background " +
michael@0 58 "captures should not be allowed yet");
michael@0 59
michael@0 60 // Enable captures.
michael@0 61 Services.prefs.setBoolPref(CAPTURE_PREF, false);
michael@0 62
michael@0 63 // Now that the newtab is visible, its allow-background-captures attribute
michael@0 64 // should be set eventually.
michael@0 65 let allowBackgroundCaptures = false;
michael@0 66 let mutationObserver = new MutationObserver(() => {
michael@0 67 mutationObserver.disconnect();
michael@0 68 allowBackgroundCaptures = true;
michael@0 69 is(doc.documentElement.getAttribute("allow-background-captures"), "true",
michael@0 70 "allow-background-captures should now be true");
michael@0 71 info("Waiting for thumbnail to be created after observing " +
michael@0 72 "allow-background-captures change");
michael@0 73 });
michael@0 74 mutationObserver.observe(doc.documentElement, {
michael@0 75 attributes: true,
michael@0 76 attributeFilter: ["allow-background-captures"],
michael@0 77 });
michael@0 78
michael@0 79 // And the allow-background-captures change should trigger the thumbnail
michael@0 80 // capture.
michael@0 81 Services.obs.addObserver(function onCreate(subj, topic, data) {
michael@0 82 if (data != url)
michael@0 83 return;
michael@0 84 ok(allowBackgroundCaptures,
michael@0 85 "page-thumbnail:create should be observed after " +
michael@0 86 "allow-background-captures was set");
michael@0 87 Services.obs.removeObserver(onCreate, "page-thumbnail:create");
michael@0 88 // Test finished!
michael@0 89 Services.prefs.setBoolPref(CAPTURE_PREF, originalDisabledState);
michael@0 90 file.remove(false);
michael@0 91 TestRunner.next();
michael@0 92 }, "page-thumbnail:create", false);
michael@0 93
michael@0 94 info("Waiting for allow-background-captures change");
michael@0 95 yield true;
michael@0 96 }
michael@0 97
michael@0 98 function wait(ms) {
michael@0 99 setTimeout(TestRunner.next, ms);
michael@0 100 }

mercurial