Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 "use strict";
8 const Ci = Components.interfaces;
9 const Cm = Components.manager;
10 const Cc = Components.classes;
12 const CONTRACT_ID = "@mozilla.org/xre/runtime;1";
14 function MockAppInfo() {
15 }
17 MockAppInfo.prototype = {
18 QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULRuntime]),
19 }
21 let newFactory = {
22 createInstance: function(aOuter, aIID) {
23 if (aOuter)
24 throw Components.results.NS_ERROR_NO_AGGREGATION;
25 return new MockAppInfo().QueryInterface(aIID);
26 },
27 lockFactory: function(aLock) {
28 throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
29 },
30 QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory])
31 };
33 var SanitizeHelper = {
34 _originalSanitizer: null,
36 MockSanitizer: {
37 clearCalled: [],
38 clearItem: function clearItem(aItemName) {
39 info("Clear item called for: " + aItemName);
40 this.clearCalled.push(aItemName);
41 }
42 },
44 setUp: function setUp() {
45 SanitizeHelper._originalSanitizer = SanitizeUI._sanitizer;
46 SanitizeUI._sanitizer = SanitizeHelper.MockSanitizer;
48 let registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
49 this.gAppInfoClassID = registrar.contractIDToCID(CONTRACT_ID);
50 this.gIncOldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory);
51 registrar.unregisterFactory(this.gAppInfoClassID, this.gIncOldFactory);
52 let components = [MockAppInfo];
53 registrar.registerFactory(this.gAppInfoClassID, "", CONTRACT_ID, newFactory);
54 this.gIncOldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory);
56 this.oldPrompt = Services.prompt;
57 },
59 tearDown: function tearDown() {
60 SanitizeUI._sanitizer = SanitizeHelper._originalSanitizer;
62 if (this.gIncOldFactory) {
63 var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
64 registrar.unregisterFactory(this.gAppInfoClassID, newFactory);
65 registrar.registerFactory(this.gAppInfoClassID, "", CONTRACT_ID, this.gIncOldFactory);
66 }
67 this.gIncOldFactory = null;
69 Services.prompt = this.oldPrompt;
70 },
71 };
73 function getAllSelected() {
74 return document.getElementById("prefs-privdata").querySelectorAll(
75 "#prefs-privdata-history[checked], " +
76 "#prefs-privdata-other[checked] + #prefs-privdata-subitems .privdata-subitem-item[checked]");
77 }
79 gTests.push({
80 setUp: SanitizeHelper.setUp,
81 tearDown: SanitizeHelper.tearDown,
82 desc: "Test sanitizer UI",
83 run: function testSanitizeUI() {
84 // We want to be able to simulate that a specific button
85 // of the 'clear private data' prompt was pressed.
86 Services.prompt = {
87 confirmEx: function() {
88 return this.retVal;
89 }
90 };
92 // Show options flyout
93 let promise = waitForEvent(FlyoutPanelsUI.PrefsFlyoutPanel._topmostElement, "PopupChanged", 2000);
94 FlyoutPanelsUI.show('PrefsFlyoutPanel');
95 yield promise;
97 // Make sure it's opened
98 yield waitForEvent(FlyoutPanelsUI.PrefsFlyoutPanel._topmostElement, "transitionend", 1000);
100 SanitizeHelper.setUp();
102 // Test initial state
103 let allSelected = getAllSelected();
104 // Only history should be selected
105 ok(allSelected.length === 1 && allSelected[0].getAttribute("itemName") === "history", "History is initially selected.");
107 let othersCheck = document.getElementById("prefs-privdata-other");
108 othersCheck.setAttribute("checked", "true");
110 let othersSubitems = document.getElementById("prefs-privdata-subitems");
111 yield waitForCondition(function (){
112 return othersSubitems.style.display !== "none";
113 }, 500);
115 allSelected = getAllSelected();
116 // History and all checkboxes under othersSubitems should be selected
117 ok(allSelected.length === 1 + othersSubitems.querySelectorAll("checkbox").length,
118 "All checkboxes are selected.");
120 // Select only downloads and passwords
121 let callItems = ["downloads", "passwords"];
122 for (let checkbox of allSelected) {
123 if (callItems.indexOf(checkbox.getAttribute("itemName")) === -1) {
124 checkbox.removeAttribute("checked");
125 }
126 }
128 // Simulate clicking "button 1", cancel.
129 Services.prompt.retVal = 1;
130 let clearButton = document.getElementById("prefs-clear-data");
131 clearButton.doCommand();
132 ok(SanitizeHelper.MockSanitizer.clearCalled.length == 0, "Nothing was cleared");
134 // We will simulate that "button 0" (which should be the clear button)
135 // was pressed
136 Services.prompt.retVal = 0;
137 clearButton.doCommand();
139 let clearNotificationDeck = document.getElementById("clear-notification");
140 let clearNotificationDone = document.getElementById("clear-notification-done");
142 // Wait until command is done.
143 yield waitForCondition(function (){
144 return clearNotificationDeck.selectedPanel == clearNotificationDone;
145 }, 1000);
147 ok(SanitizeHelper.MockSanitizer.clearCalled.length === callItems.length, "All expected items were called");
149 SanitizeHelper.MockSanitizer.clearCalled.forEach(function(item) {
150 ok(callItems.indexOf(item) >= 0, "Sanitized " + item);
151 });
153 // hide options flyout
154 let promise = waitForEvent(FlyoutPanelsUI.PrefsFlyoutPanel._topmostElement, "PopupChanged", 2000);
155 FlyoutPanelsUI.hide();
156 yield promise;
157 }
158 });
160 function checkDNTPrefs(aExpectedEnabled, aExpectedValue) {
161 let currentEnabled = Services.prefs.getBoolPref("privacy.donottrackheader.enabled");
162 let currentValue = Services.prefs.getIntPref("privacy.donottrackheader.value");
164 let enabledTestMsg = "testing privacy.donottrackheader.enabled, expected "
165 + aExpectedEnabled + " got " + currentEnabled;
167 ok(aExpectedEnabled === currentEnabled, enabledTestMsg);
169 let valueTestMsg = "testing privacy.donottrackheader.value, expected "
170 + aExpectedValue + " got " + currentValue;
172 ok(aExpectedValue === currentValue, valueTestMsg);
173 }
175 gTests.push({
176 desc: "Test do not track settings",
177 run: function testDNT() {
178 let noTrack = document.getElementById("prefs-dnt-notrack");
179 let noPref = document.getElementById("prefs-dnt-nopref");
180 let okTrack = document.getElementById("prefs-dnt-oktrack");
182 // Show options flyout
183 let promise = waitForEvent(FlyoutPanelsUI.PrefsFlyoutPanel._topmostElement, "PopupChanged", 2000);
184 FlyoutPanelsUI.show('PrefsFlyoutPanel');
185 yield promise;
187 noPref.click();
188 // See https://mxr.mozilla.org/mozilla-central/source/modules/libpref/src/init/all.js?rev=0aab2bb76b45#755
189 // -1 - tell sites nothing about preferences
190 yield waitForCondition(() => Services.prefs.getIntPref("privacy.donottrackheader.value") === -1);
191 checkDNTPrefs(false, -1);
193 noTrack.click();
194 // 1 - tell sites tracking is unacceptable
195 yield waitForCondition(() => Services.prefs.getIntPref("privacy.donottrackheader.value") === 1);
196 checkDNTPrefs(true, 1);
198 okTrack.click();
199 // 0 - tell sites tracking is acceptable
200 yield waitForCondition(() => Services.prefs.getIntPref("privacy.donottrackheader.value") === 0);
201 checkDNTPrefs(true, 0);
203 // hide options flyout
204 let promise = waitForEvent(FlyoutPanelsUI.PrefsFlyoutPanel._topmostElement, "PopupChanged", 2000);
205 FlyoutPanelsUI.hide();
206 yield promise;
207 }
208 });
210 function test() {
211 runTests();
212 }