browser/base/content/test/newtab/browser_newtab_background_captures.js

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

mercurial