1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/
5 registerCleanupFunction(function() {
6 // Ensure we don't pollute prefs for next tests.
8 Services.prefs.clearUserPref("network.cookies.cookieBehavior");
11 Services.prefs.clearUserPref("network.cookie.lifetimePolicy");
18 desc: "Check that clearing cookies does not clear storage",
21 Cc["@mozilla.org/dom/storagemanager;1"]
22 .getService(Ci.nsIObserver)
23 .observe(null, "cookie-changed", "cleared");
27 let storage = getStorage();
28 isnot(storage.getItem("snippets-last-update"), null);
29 executeSoon(runNextTest);
34 desc: "Check default snippets are shown",
40 let doc = gBrowser.selectedTab.linkedBrowser.contentDocument;
41 let snippetsElt = doc.getElementById("snippets");
42 ok(snippetsElt, "Found snippets element")
43 is(snippetsElt.getElementsByTagName("span").length, 1,
44 "A default snippet is visible.");
45 executeSoon(runNextTest);
50 desc: "Check default snippets are shown if snippets are invalid xml",
53 let storage = getStorage();
54 // This must be some incorrect xhtml code.
55 storage.setItem("snippets", "<p><b></p></b>");
59 let doc = gBrowser.selectedTab.linkedBrowser.contentDocument;
61 let snippetsElt = doc.getElementById("snippets");
62 ok(snippetsElt, "Found snippets element");
63 is(snippetsElt.getElementsByTagName("span").length, 1,
64 "A default snippet is visible.");
65 let storage = getStorage();
66 storage.removeItem("snippets");
67 executeSoon(runNextTest);
71 desc: "Check that search engine logo has alt text",
77 let doc = gBrowser.selectedTab.linkedBrowser.contentDocument;
79 let searchEngineLogoElt = doc.getElementById("searchEngineLogo");
80 ok(searchEngineLogoElt, "Found search engine logo");
82 let altText = searchEngineLogoElt.alt;
83 ok(typeof altText == "string" && altText.length > 0,
84 "Search engine logo's alt text is a nonempty string");
86 isnot(altText, "undefined",
87 "Search engine logo's alt text shouldn't be the string 'undefined'");
89 executeSoon(runNextTest);
93 desc: "Check that performing a search fires a search event.",
94 setup: function () { },
96 let doc = gBrowser.contentDocument;
98 doc.addEventListener("AboutHomeSearchEvent", function onSearch(e) {
99 is(e.detail, doc.documentElement.getAttribute("searchEngineName"), "Detail is search engine name");
102 executeSoon(runNextTest);
105 doc.getElementById("searchText").value = "it works";
106 doc.getElementById("searchSubmit").click();
110 desc: "Check that performing a search records to Firefox Health Report.",
111 setup: function () { },
114 let cm = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
115 cm.getCategoryEntry("healthreport-js-provider", "SearchesProvider");
117 // Health Report disabled, or no SearchesProvider.
122 let doc = gBrowser.contentDocument;
124 // We rely on the listener in browser.js being installed and fired before
125 // this one. If this ever changes, we should add an executeSoon() or similar.
126 doc.addEventListener("AboutHomeSearchEvent", function onSearch(e) {
127 executeSoon(gBrowser.stop.bind(gBrowser));
128 let reporter = Components.classes["@mozilla.org/datareporting/service;1"]
132 ok(reporter, "Health Reporter instance available.");
134 reporter.onInit().then(function onInit() {
135 let provider = reporter.getProvider("org.mozilla.searches");
136 ok(provider, "Searches provider is available.");
138 let engineName = doc.documentElement.getAttribute("searchEngineName").toLowerCase();
140 let m = provider.getMeasurement("counts", 1);
141 m.getValues().then(function onValues(data) {
142 let now = new Date();
143 ok(data.days.hasDay(now), "Have data for today.");
145 let day = data.days.getDay(now);
146 let field = engineName + ".abouthome";
147 ok(day.has(field), "Have data for about home on this engine.");
149 // Note the search from the previous test.
150 is(day.get(field), 2, "Have searches recorded.");
152 executeSoon(runNextTest);
158 doc.getElementById("searchText").value = "a search";
159 doc.getElementById("searchSubmit").click();
166 waitForExplicitFinish();
168 // Ensure that by default we don't try to check for remote snippets since that
169 // could be tricky due to network bustages or slowness.
170 let storage = getStorage();
171 storage.setItem("snippets-last-update", Date.now());
172 storage.removeItem("snippets");
174 executeSoon(runNextTest);
177 function runNextTest()
179 while (gBrowser.tabs.length > 1) {
180 gBrowser.removeCurrentTab();
184 let test = gTests.shift();
187 let tab = gBrowser.selectedTab = gBrowser.addTab("about:home");
188 tab.linkedBrowser.addEventListener("load", function load(event) {
189 tab.linkedBrowser.removeEventListener("load", load, true);
191 let observer = new MutationObserver(function (mutations) {
192 for (let mutation of mutations) {
193 if (mutation.attributeName == "searchEngineURL") {
194 observer.disconnect();
195 executeSoon(test.run);
200 let docElt = tab.linkedBrowser.contentDocument.documentElement;
201 observer.observe(docElt, { attributes: true });
209 function getStorage()
211 let aboutHomeURI = Services.io.newURI("moz-safe-about:home", null, null);
212 let principal = Components.classes["@mozilla.org/scriptsecuritymanager;1"].
213 getService(Components.interfaces.nsIScriptSecurityManager).
214 getNoAppCodebasePrincipal(Services.io.newURI("about:home", null, null));
215 let dsm = Components.classes["@mozilla.org/dom/storagemanager;1"].
216 getService(Components.interfaces.nsIDOMStorageManager);
217 return dsm.getLocalStorageForPrincipal(principal, "");