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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/metro/base/tests/mochitest/browser_prefs_ui.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,212 @@
     1.4 +// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +"use strict";
    1.10 +
    1.11 +const Ci = Components.interfaces;
    1.12 +const Cm = Components.manager;
    1.13 +const Cc = Components.classes;
    1.14 +
    1.15 +const CONTRACT_ID = "@mozilla.org/xre/runtime;1";
    1.16 +
    1.17 +function MockAppInfo() {
    1.18 +}
    1.19 +
    1.20 +MockAppInfo.prototype = {
    1.21 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULRuntime]),
    1.22 +}
    1.23 +
    1.24 +let newFactory = {
    1.25 +  createInstance: function(aOuter, aIID) {
    1.26 +    if (aOuter)
    1.27 +      throw Components.results.NS_ERROR_NO_AGGREGATION;
    1.28 +    return new MockAppInfo().QueryInterface(aIID);
    1.29 +  },
    1.30 +  lockFactory: function(aLock) {
    1.31 +    throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
    1.32 +  },
    1.33 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory])
    1.34 +};
    1.35 +
    1.36 +var SanitizeHelper = {
    1.37 +  _originalSanitizer: null,
    1.38 +
    1.39 +  MockSanitizer: {
    1.40 +    clearCalled: [],
    1.41 +    clearItem: function clearItem(aItemName) {
    1.42 +      info("Clear item called for: " + aItemName);
    1.43 +      this.clearCalled.push(aItemName);
    1.44 +    }
    1.45 +  },
    1.46 +
    1.47 +  setUp: function setUp() {
    1.48 +    SanitizeHelper._originalSanitizer = SanitizeUI._sanitizer;
    1.49 +    SanitizeUI._sanitizer = SanitizeHelper.MockSanitizer;
    1.50 +
    1.51 +    let registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
    1.52 +    this.gAppInfoClassID = registrar.contractIDToCID(CONTRACT_ID);
    1.53 +    this.gIncOldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory);
    1.54 +    registrar.unregisterFactory(this.gAppInfoClassID, this.gIncOldFactory);
    1.55 +    let components = [MockAppInfo];
    1.56 +    registrar.registerFactory(this.gAppInfoClassID, "", CONTRACT_ID, newFactory);
    1.57 +    this.gIncOldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory);
    1.58 +
    1.59 +    this.oldPrompt = Services.prompt;
    1.60 +  },
    1.61 +
    1.62 +  tearDown: function tearDown() {
    1.63 +    SanitizeUI._sanitizer = SanitizeHelper._originalSanitizer;
    1.64 +
    1.65 +    if (this.gIncOldFactory) {
    1.66 +      var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
    1.67 +      registrar.unregisterFactory(this.gAppInfoClassID, newFactory);
    1.68 +      registrar.registerFactory(this.gAppInfoClassID, "", CONTRACT_ID, this.gIncOldFactory);
    1.69 +    }
    1.70 +    this.gIncOldFactory = null;
    1.71 +
    1.72 +    Services.prompt = this.oldPrompt;
    1.73 +  },
    1.74 +};
    1.75 +
    1.76 +function getAllSelected() {
    1.77 +  return document.getElementById("prefs-privdata").querySelectorAll(
    1.78 +    "#prefs-privdata-history[checked], " +
    1.79 +      "#prefs-privdata-other[checked] + #prefs-privdata-subitems .privdata-subitem-item[checked]");
    1.80 +}
    1.81 +
    1.82 +gTests.push({
    1.83 +  setUp: SanitizeHelper.setUp,
    1.84 +  tearDown: SanitizeHelper.tearDown,
    1.85 +  desc: "Test sanitizer UI",
    1.86 +  run: function testSanitizeUI() {
    1.87 +    // We want to be able to simulate that a specific button
    1.88 +    // of the 'clear private data' prompt was pressed.
    1.89 +    Services.prompt = {
    1.90 +      confirmEx: function() {
    1.91 +        return this.retVal;
    1.92 +      }
    1.93 +    };
    1.94 +
    1.95 +    // Show options flyout
    1.96 +    let promise = waitForEvent(FlyoutPanelsUI.PrefsFlyoutPanel._topmostElement, "PopupChanged", 2000);
    1.97 +    FlyoutPanelsUI.show('PrefsFlyoutPanel');
    1.98 +    yield promise;
    1.99 +
   1.100 +    // Make sure it's opened
   1.101 +    yield waitForEvent(FlyoutPanelsUI.PrefsFlyoutPanel._topmostElement, "transitionend", 1000);
   1.102 +
   1.103 +    SanitizeHelper.setUp();
   1.104 +
   1.105 +    // Test initial state
   1.106 +    let allSelected = getAllSelected();
   1.107 +    // Only  history should be selected
   1.108 +    ok(allSelected.length === 1 && allSelected[0].getAttribute("itemName") === "history", "History is initially selected.");
   1.109 +
   1.110 +    let othersCheck = document.getElementById("prefs-privdata-other");
   1.111 +    othersCheck.setAttribute("checked", "true");
   1.112 +
   1.113 +    let othersSubitems = document.getElementById("prefs-privdata-subitems");
   1.114 +    yield waitForCondition(function (){
   1.115 +      return othersSubitems.style.display !== "none";
   1.116 +    }, 500);
   1.117 +
   1.118 +    allSelected = getAllSelected();
   1.119 +    // History and all checkboxes under othersSubitems should be selected
   1.120 +    ok(allSelected.length === 1 + othersSubitems.querySelectorAll("checkbox").length,
   1.121 +      "All checkboxes are selected.");
   1.122 +
   1.123 +    // Select only downloads and passwords
   1.124 +    let callItems = ["downloads", "passwords"];
   1.125 +    for (let checkbox of allSelected) {
   1.126 +      if (callItems.indexOf(checkbox.getAttribute("itemName")) === -1) {
   1.127 +        checkbox.removeAttribute("checked");
   1.128 +      }
   1.129 +    }
   1.130 +
   1.131 +    // Simulate clicking "button 1", cancel.
   1.132 +    Services.prompt.retVal = 1;
   1.133 +    let clearButton = document.getElementById("prefs-clear-data");
   1.134 +    clearButton.doCommand();
   1.135 +    ok(SanitizeHelper.MockSanitizer.clearCalled.length == 0, "Nothing was cleared");
   1.136 +
   1.137 +    // We will simulate that "button 0" (which should be the clear button)
   1.138 +    // was pressed
   1.139 +    Services.prompt.retVal = 0;
   1.140 +    clearButton.doCommand();
   1.141 +
   1.142 +    let clearNotificationDeck = document.getElementById("clear-notification");
   1.143 +    let clearNotificationDone = document.getElementById("clear-notification-done");
   1.144 +
   1.145 +    // Wait until command is done.
   1.146 +    yield waitForCondition(function (){
   1.147 +      return clearNotificationDeck.selectedPanel == clearNotificationDone;
   1.148 +    }, 1000);
   1.149 +
   1.150 +    ok(SanitizeHelper.MockSanitizer.clearCalled.length === callItems.length, "All expected items were called");
   1.151 +
   1.152 +    SanitizeHelper.MockSanitizer.clearCalled.forEach(function(item) {
   1.153 +      ok(callItems.indexOf(item) >= 0, "Sanitized " + item);
   1.154 +    });
   1.155 +
   1.156 +    // hide options flyout
   1.157 +    let promise = waitForEvent(FlyoutPanelsUI.PrefsFlyoutPanel._topmostElement, "PopupChanged", 2000);
   1.158 +    FlyoutPanelsUI.hide();
   1.159 +    yield promise;
   1.160 +  }
   1.161 +});
   1.162 +
   1.163 +function checkDNTPrefs(aExpectedEnabled, aExpectedValue) {
   1.164 +  let currentEnabled = Services.prefs.getBoolPref("privacy.donottrackheader.enabled");
   1.165 +  let currentValue = Services.prefs.getIntPref("privacy.donottrackheader.value");
   1.166 +
   1.167 +  let enabledTestMsg = "testing privacy.donottrackheader.enabled, expected "
   1.168 +    + aExpectedEnabled + " got " + currentEnabled;
   1.169 +
   1.170 +  ok(aExpectedEnabled === currentEnabled, enabledTestMsg);
   1.171 +
   1.172 +  let valueTestMsg = "testing privacy.donottrackheader.value, expected "
   1.173 +    + aExpectedValue + " got " + currentValue;
   1.174 +
   1.175 +  ok(aExpectedValue === currentValue, valueTestMsg);
   1.176 +}
   1.177 +
   1.178 +gTests.push({
   1.179 +  desc: "Test do not track settings",
   1.180 +  run: function testDNT() {
   1.181 +    let noTrack = document.getElementById("prefs-dnt-notrack");
   1.182 +    let noPref = document.getElementById("prefs-dnt-nopref");
   1.183 +    let okTrack = document.getElementById("prefs-dnt-oktrack");
   1.184 +
   1.185 +    // Show options flyout
   1.186 +    let promise = waitForEvent(FlyoutPanelsUI.PrefsFlyoutPanel._topmostElement, "PopupChanged", 2000);
   1.187 +    FlyoutPanelsUI.show('PrefsFlyoutPanel');
   1.188 +    yield promise;
   1.189 +
   1.190 +    noPref.click();
   1.191 +    // See https://mxr.mozilla.org/mozilla-central/source/modules/libpref/src/init/all.js?rev=0aab2bb76b45#755
   1.192 +    // -1 - tell sites nothing about preferences
   1.193 +    yield waitForCondition(() => Services.prefs.getIntPref("privacy.donottrackheader.value") === -1);
   1.194 +    checkDNTPrefs(false, -1);
   1.195 +
   1.196 +    noTrack.click();
   1.197 +    // 1 - tell sites tracking is unacceptable
   1.198 +    yield waitForCondition(() => Services.prefs.getIntPref("privacy.donottrackheader.value") === 1);
   1.199 +    checkDNTPrefs(true, 1);
   1.200 +
   1.201 +    okTrack.click();
   1.202 +    // 0 - tell sites tracking is acceptable
   1.203 +    yield waitForCondition(() => Services.prefs.getIntPref("privacy.donottrackheader.value") === 0);
   1.204 +    checkDNTPrefs(true, 0);
   1.205 +
   1.206 +    // hide options flyout
   1.207 +    let promise = waitForEvent(FlyoutPanelsUI.PrefsFlyoutPanel._topmostElement, "PopupChanged", 2000);
   1.208 +    FlyoutPanelsUI.hide();
   1.209 +    yield promise;
   1.210 +  }
   1.211 +});
   1.212 +
   1.213 +function test() {
   1.214 +  runTests();
   1.215 +}

mercurial