michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const Ci = Components.interfaces; michael@0: const Cm = Components.manager; michael@0: const Cc = Components.classes; michael@0: michael@0: const CONTRACT_ID = "@mozilla.org/xre/runtime;1"; michael@0: michael@0: function MockAppInfo() { michael@0: } michael@0: michael@0: MockAppInfo.prototype = { michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULRuntime]), michael@0: } michael@0: michael@0: let newFactory = { michael@0: createInstance: function(aOuter, aIID) { michael@0: if (aOuter) michael@0: throw Components.results.NS_ERROR_NO_AGGREGATION; michael@0: return new MockAppInfo().QueryInterface(aIID); michael@0: }, michael@0: lockFactory: function(aLock) { michael@0: throw Components.results.NS_ERROR_NOT_IMPLEMENTED; michael@0: }, michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory]) michael@0: }; michael@0: michael@0: var SanitizeHelper = { michael@0: _originalSanitizer: null, michael@0: michael@0: MockSanitizer: { michael@0: clearCalled: [], michael@0: clearItem: function clearItem(aItemName) { michael@0: info("Clear item called for: " + aItemName); michael@0: this.clearCalled.push(aItemName); michael@0: } michael@0: }, michael@0: michael@0: setUp: function setUp() { michael@0: SanitizeHelper._originalSanitizer = SanitizeUI._sanitizer; michael@0: SanitizeUI._sanitizer = SanitizeHelper.MockSanitizer; michael@0: michael@0: let registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); michael@0: this.gAppInfoClassID = registrar.contractIDToCID(CONTRACT_ID); michael@0: this.gIncOldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory); michael@0: registrar.unregisterFactory(this.gAppInfoClassID, this.gIncOldFactory); michael@0: let components = [MockAppInfo]; michael@0: registrar.registerFactory(this.gAppInfoClassID, "", CONTRACT_ID, newFactory); michael@0: this.gIncOldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory); michael@0: michael@0: this.oldPrompt = Services.prompt; michael@0: }, michael@0: michael@0: tearDown: function tearDown() { michael@0: SanitizeUI._sanitizer = SanitizeHelper._originalSanitizer; michael@0: michael@0: if (this.gIncOldFactory) { michael@0: var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); michael@0: registrar.unregisterFactory(this.gAppInfoClassID, newFactory); michael@0: registrar.registerFactory(this.gAppInfoClassID, "", CONTRACT_ID, this.gIncOldFactory); michael@0: } michael@0: this.gIncOldFactory = null; michael@0: michael@0: Services.prompt = this.oldPrompt; michael@0: }, michael@0: }; michael@0: michael@0: function getAllSelected() { michael@0: return document.getElementById("prefs-privdata").querySelectorAll( michael@0: "#prefs-privdata-history[checked], " + michael@0: "#prefs-privdata-other[checked] + #prefs-privdata-subitems .privdata-subitem-item[checked]"); michael@0: } michael@0: michael@0: gTests.push({ michael@0: setUp: SanitizeHelper.setUp, michael@0: tearDown: SanitizeHelper.tearDown, michael@0: desc: "Test sanitizer UI", michael@0: run: function testSanitizeUI() { michael@0: // We want to be able to simulate that a specific button michael@0: // of the 'clear private data' prompt was pressed. michael@0: Services.prompt = { michael@0: confirmEx: function() { michael@0: return this.retVal; michael@0: } michael@0: }; michael@0: michael@0: // Show options flyout michael@0: let promise = waitForEvent(FlyoutPanelsUI.PrefsFlyoutPanel._topmostElement, "PopupChanged", 2000); michael@0: FlyoutPanelsUI.show('PrefsFlyoutPanel'); michael@0: yield promise; michael@0: michael@0: // Make sure it's opened michael@0: yield waitForEvent(FlyoutPanelsUI.PrefsFlyoutPanel._topmostElement, "transitionend", 1000); michael@0: michael@0: SanitizeHelper.setUp(); michael@0: michael@0: // Test initial state michael@0: let allSelected = getAllSelected(); michael@0: // Only history should be selected michael@0: ok(allSelected.length === 1 && allSelected[0].getAttribute("itemName") === "history", "History is initially selected."); michael@0: michael@0: let othersCheck = document.getElementById("prefs-privdata-other"); michael@0: othersCheck.setAttribute("checked", "true"); michael@0: michael@0: let othersSubitems = document.getElementById("prefs-privdata-subitems"); michael@0: yield waitForCondition(function (){ michael@0: return othersSubitems.style.display !== "none"; michael@0: }, 500); michael@0: michael@0: allSelected = getAllSelected(); michael@0: // History and all checkboxes under othersSubitems should be selected michael@0: ok(allSelected.length === 1 + othersSubitems.querySelectorAll("checkbox").length, michael@0: "All checkboxes are selected."); michael@0: michael@0: // Select only downloads and passwords michael@0: let callItems = ["downloads", "passwords"]; michael@0: for (let checkbox of allSelected) { michael@0: if (callItems.indexOf(checkbox.getAttribute("itemName")) === -1) { michael@0: checkbox.removeAttribute("checked"); michael@0: } michael@0: } michael@0: michael@0: // Simulate clicking "button 1", cancel. michael@0: Services.prompt.retVal = 1; michael@0: let clearButton = document.getElementById("prefs-clear-data"); michael@0: clearButton.doCommand(); michael@0: ok(SanitizeHelper.MockSanitizer.clearCalled.length == 0, "Nothing was cleared"); michael@0: michael@0: // We will simulate that "button 0" (which should be the clear button) michael@0: // was pressed michael@0: Services.prompt.retVal = 0; michael@0: clearButton.doCommand(); michael@0: michael@0: let clearNotificationDeck = document.getElementById("clear-notification"); michael@0: let clearNotificationDone = document.getElementById("clear-notification-done"); michael@0: michael@0: // Wait until command is done. michael@0: yield waitForCondition(function (){ michael@0: return clearNotificationDeck.selectedPanel == clearNotificationDone; michael@0: }, 1000); michael@0: michael@0: ok(SanitizeHelper.MockSanitizer.clearCalled.length === callItems.length, "All expected items were called"); michael@0: michael@0: SanitizeHelper.MockSanitizer.clearCalled.forEach(function(item) { michael@0: ok(callItems.indexOf(item) >= 0, "Sanitized " + item); michael@0: }); michael@0: michael@0: // hide options flyout michael@0: let promise = waitForEvent(FlyoutPanelsUI.PrefsFlyoutPanel._topmostElement, "PopupChanged", 2000); michael@0: FlyoutPanelsUI.hide(); michael@0: yield promise; michael@0: } michael@0: }); michael@0: michael@0: function checkDNTPrefs(aExpectedEnabled, aExpectedValue) { michael@0: let currentEnabled = Services.prefs.getBoolPref("privacy.donottrackheader.enabled"); michael@0: let currentValue = Services.prefs.getIntPref("privacy.donottrackheader.value"); michael@0: michael@0: let enabledTestMsg = "testing privacy.donottrackheader.enabled, expected " michael@0: + aExpectedEnabled + " got " + currentEnabled; michael@0: michael@0: ok(aExpectedEnabled === currentEnabled, enabledTestMsg); michael@0: michael@0: let valueTestMsg = "testing privacy.donottrackheader.value, expected " michael@0: + aExpectedValue + " got " + currentValue; michael@0: michael@0: ok(aExpectedValue === currentValue, valueTestMsg); michael@0: } michael@0: michael@0: gTests.push({ michael@0: desc: "Test do not track settings", michael@0: run: function testDNT() { michael@0: let noTrack = document.getElementById("prefs-dnt-notrack"); michael@0: let noPref = document.getElementById("prefs-dnt-nopref"); michael@0: let okTrack = document.getElementById("prefs-dnt-oktrack"); michael@0: michael@0: // Show options flyout michael@0: let promise = waitForEvent(FlyoutPanelsUI.PrefsFlyoutPanel._topmostElement, "PopupChanged", 2000); michael@0: FlyoutPanelsUI.show('PrefsFlyoutPanel'); michael@0: yield promise; michael@0: michael@0: noPref.click(); michael@0: // See https://mxr.mozilla.org/mozilla-central/source/modules/libpref/src/init/all.js?rev=0aab2bb76b45#755 michael@0: // -1 - tell sites nothing about preferences michael@0: yield waitForCondition(() => Services.prefs.getIntPref("privacy.donottrackheader.value") === -1); michael@0: checkDNTPrefs(false, -1); michael@0: michael@0: noTrack.click(); michael@0: // 1 - tell sites tracking is unacceptable michael@0: yield waitForCondition(() => Services.prefs.getIntPref("privacy.donottrackheader.value") === 1); michael@0: checkDNTPrefs(true, 1); michael@0: michael@0: okTrack.click(); michael@0: // 0 - tell sites tracking is acceptable michael@0: yield waitForCondition(() => Services.prefs.getIntPref("privacy.donottrackheader.value") === 0); michael@0: checkDNTPrefs(true, 0); michael@0: michael@0: // hide options flyout michael@0: let promise = waitForEvent(FlyoutPanelsUI.PrefsFlyoutPanel._topmostElement, "PopupChanged", 2000); michael@0: FlyoutPanelsUI.hide(); michael@0: yield promise; michael@0: } michael@0: }); michael@0: michael@0: function test() { michael@0: runTests(); michael@0: }