michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: /** michael@0: * Verifies that hidden, pre-loaded newtabs don't allow background captures, and michael@0: * when unhidden, do allow background captures. michael@0: */ michael@0: michael@0: const CAPTURE_PREF = "browser.pagethumbnails.capturing_disabled"; michael@0: michael@0: function runTests() { michael@0: let imports = {}; michael@0: Cu.import("resource://gre/modules/PageThumbs.jsm", imports); michael@0: Cu.import("resource:///modules/BrowserNewTabPreloader.jsm", imports); michael@0: michael@0: // Disable captures. michael@0: let originalDisabledState = Services.prefs.getBoolPref(CAPTURE_PREF); michael@0: Services.prefs.setBoolPref(CAPTURE_PREF, true); michael@0: michael@0: // Make sure the thumbnail doesn't exist yet. michael@0: let siteName = "newtab_background_captures"; michael@0: let url = "http://example.com/#" + siteName; michael@0: let path = imports.PageThumbsStorage.getFilePathForURL(url); michael@0: let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); michael@0: file.initWithPath(path); michael@0: try { michael@0: file.remove(false); michael@0: } michael@0: catch (err) {} michael@0: michael@0: // Add a top site. michael@0: yield setLinks(siteName); michael@0: michael@0: // We need a handle to a hidden, pre-loaded newtab so we can verify that it michael@0: // doesn't allow background captures. Add a newtab, which triggers creation michael@0: // of a hidden newtab, and then keep calling BrowserNewTabPreloader.newTab michael@0: // until it returns true, meaning that it swapped the passed-in tab's docshell michael@0: // for the hidden newtab docshell. michael@0: let tab = gWindow.gBrowser.addTab("about:blank"); michael@0: yield addNewTabPageTab(); michael@0: let swapWaitCount = 0; michael@0: let swapped = imports.BrowserNewTabPreloader.newTab(tab); michael@0: while (!swapped) { michael@0: if (++swapWaitCount == 10) { michael@0: ok(false, "Timed out waiting for newtab docshell swap."); michael@0: return; michael@0: } michael@0: // Give the hidden newtab some time to finish loading. michael@0: yield wait(2000); michael@0: info("Checking newtab swap " + swapWaitCount); michael@0: swapped = imports.BrowserNewTabPreloader.newTab(tab); michael@0: } michael@0: michael@0: // The tab's docshell is now the previously hidden newtab docshell. michael@0: let doc = tab.linkedBrowser.contentDocument; michael@0: isnot(doc.documentElement.getAttribute("allow-background-captures"), "true", michael@0: "Pre-loaded docshell just synchronously swapped, so background " + michael@0: "captures should not be allowed yet"); michael@0: michael@0: // Enable captures. michael@0: Services.prefs.setBoolPref(CAPTURE_PREF, false); michael@0: michael@0: // Now that the newtab is visible, its allow-background-captures attribute michael@0: // should be set eventually. michael@0: let allowBackgroundCaptures = false; michael@0: let mutationObserver = new MutationObserver(() => { michael@0: mutationObserver.disconnect(); michael@0: allowBackgroundCaptures = true; michael@0: is(doc.documentElement.getAttribute("allow-background-captures"), "true", michael@0: "allow-background-captures should now be true"); michael@0: info("Waiting for thumbnail to be created after observing " + michael@0: "allow-background-captures change"); michael@0: }); michael@0: mutationObserver.observe(doc.documentElement, { michael@0: attributes: true, michael@0: attributeFilter: ["allow-background-captures"], michael@0: }); michael@0: michael@0: // And the allow-background-captures change should trigger the thumbnail michael@0: // capture. michael@0: Services.obs.addObserver(function onCreate(subj, topic, data) { michael@0: if (data != url) michael@0: return; michael@0: ok(allowBackgroundCaptures, michael@0: "page-thumbnail:create should be observed after " + michael@0: "allow-background-captures was set"); michael@0: Services.obs.removeObserver(onCreate, "page-thumbnail:create"); michael@0: // Test finished! michael@0: Services.prefs.setBoolPref(CAPTURE_PREF, originalDisabledState); michael@0: file.remove(false); michael@0: TestRunner.next(); michael@0: }, "page-thumbnail:create", false); michael@0: michael@0: info("Waiting for allow-background-captures change"); michael@0: yield true; michael@0: } michael@0: michael@0: function wait(ms) { michael@0: setTimeout(TestRunner.next, ms); michael@0: }