browser/metro/base/tests/mochitest/browser_prefs_ui.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial