summaryrefslogtreecommitdiffstats
path: root/browser/tools/mozscreenshots/mozscreenshots/extension/configurations/ControlCenter.jsm
blob: 1201ed69235ecf237fc8128295bcf8ac2f248121 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

this.EXPORTED_SYMBOLS = ["ControlCenter"];

const {classes: Cc, interfaces: Ci, utils: Cu} = Components;

Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Task.jsm");
Cu.import("resource://gre/modules/Timer.jsm");
Cu.import("resource://testing-common/BrowserTestUtils.jsm");
Cu.import("resource:///modules/SitePermissions.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");

let {UrlClassifierTestUtils} = Cu.import("resource://testing-common/UrlClassifierTestUtils.jsm", {});

const RESOURCE_PATH = "extensions/mozscreenshots/browser/chrome/mozscreenshots/lib/controlCenter";
const HTTP_PAGE = "http://example.com/";
const HTTPS_PAGE = "https://example.com/";
const PERMISSIONS_PAGE = "https://test1.example.com/";
const HTTP_PASSWORD_PAGE = `http://test2.example.org/${RESOURCE_PATH}/password.html`;
const MIXED_CONTENT_URL = `https://example.com/${RESOURCE_PATH}/mixed.html`;
const MIXED_ACTIVE_CONTENT_URL = `https://example.com/${RESOURCE_PATH}/mixed_active.html`;
const MIXED_PASSIVE_CONTENT_URL = `https://example.com/${RESOURCE_PATH}/mixed_passive.html`;
const TRACKING_PAGE = `http://tracking.example.org/${RESOURCE_PATH}/tracking.html`;

this.ControlCenter = {
  init(libDir) { },

  configurations: {
    about: {
      applyConfig: Task.async(function* () {
        yield loadPage("about:home");
        yield openIdentityPopup();
      }),
    },

    localFile: {
      applyConfig: Task.async(function* () {
        let channel = NetUtil.newChannel({
            uri: "chrome://mozscreenshots/content/lib/mozscreenshots.html",
            loadUsingSystemPrincipal: true
        });
        channel = channel.QueryInterface(Ci.nsIFileChannel);
        let browserWindow = Services.wm.getMostRecentWindow("navigator:browser");
        let gBrowser = browserWindow.gBrowser;
        BrowserTestUtils.loadURI(gBrowser.selectedBrowser, channel.file.path);
        yield BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
        yield openIdentityPopup();
      }),
    },

    http: {
      applyConfig: Task.async(function* () {
        yield loadPage(HTTP_PAGE);
        yield openIdentityPopup();
      }),
    },

    httpSubView: {
      applyConfig: Task.async(function* () {
        yield loadPage(HTTP_PAGE);
        yield openIdentityPopup(true);
      }),
    },

    https: {
      applyConfig: Task.async(function* () {
        yield loadPage(HTTPS_PAGE);
        yield openIdentityPopup();
      }),
    },

    httpsSubView: {
      applyConfig: Task.async(function* () {
        yield loadPage(HTTPS_PAGE);
        yield openIdentityPopup(true);
      }),
    },

    singlePermission: {
      applyConfig: Task.async(function* () {
        let uri = Services.io.newURI(PERMISSIONS_PAGE, null, null)
        SitePermissions.set(uri, "camera", SitePermissions.ALLOW);

        yield loadPage(PERMISSIONS_PAGE);
        yield openIdentityPopup();
      }),
    },

    allPermissions: {
      applyConfig: Task.async(function* () {
        // there are 3 possible non-default permission states, so we alternate between them
        let states = [SitePermissions.ALLOW, SitePermissions.BLOCK, SitePermissions.SESSION];
        let uri = Services.io.newURI(PERMISSIONS_PAGE, null, null)
        SitePermissions.listPermissions().forEach(function (permission, index) {
          SitePermissions.set(uri, permission, states[index % 3]);
        });

        yield loadPage(PERMISSIONS_PAGE);
        yield openIdentityPopup();
      }),
    },

    mixed: {
      applyConfig: Task.async(function* () {
        yield loadPage(MIXED_CONTENT_URL);
        yield openIdentityPopup();
      }),
    },

    mixedSubView: {
      applyConfig: Task.async(function* () {
        yield loadPage(MIXED_CONTENT_URL);
        yield openIdentityPopup(true);
      }),
    },

    mixedPassive: {
      applyConfig: Task.async(function* () {
        yield loadPage(MIXED_PASSIVE_CONTENT_URL);
        yield openIdentityPopup();
      }),
    },

    mixedPassiveSubView: {
      applyConfig: Task.async(function* () {
        yield loadPage(MIXED_PASSIVE_CONTENT_URL);
        yield openIdentityPopup(true);
      }),
    },

    mixedActive: {
      applyConfig: Task.async(function* () {
        yield loadPage(MIXED_ACTIVE_CONTENT_URL);
        yield openIdentityPopup();
      }),
    },

    mixedActiveSubView: {
      applyConfig: Task.async(function* () {
        yield loadPage(MIXED_ACTIVE_CONTENT_URL);
        yield openIdentityPopup(true);
      }),
    },

    mixedActiveUnblocked: {
      applyConfig: Task.async(function* () {
        let browserWindow = Services.wm.getMostRecentWindow("navigator:browser");
        let gBrowser = browserWindow.gBrowser;
        yield loadPage(MIXED_ACTIVE_CONTENT_URL);
        gBrowser.ownerGlobal.gIdentityHandler.disableMixedContentProtection();
        yield BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser, false, MIXED_ACTIVE_CONTENT_URL);
        yield openIdentityPopup();
      }),
    },

    mixedActiveUnblockedSubView: {
      applyConfig: Task.async(function* () {
        let browserWindow = Services.wm.getMostRecentWindow("navigator:browser");
        let gBrowser = browserWindow.gBrowser;
        yield loadPage(MIXED_ACTIVE_CONTENT_URL);
        gBrowser.ownerGlobal.gIdentityHandler.disableMixedContentProtection();
        yield BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser, false, MIXED_ACTIVE_CONTENT_URL);
        yield openIdentityPopup(true);
      }),
    },

    httpPassword: {
      applyConfig: Task.async(function* () {
        yield loadPage(HTTP_PASSWORD_PAGE);
        yield openIdentityPopup();
      }),
    },

    httpPasswordSubView: {
      applyConfig: Task.async(function* () {
        yield loadPage(HTTP_PASSWORD_PAGE);
        yield openIdentityPopup(true);
      }),
    },

    trackingProtectionNoElements: {
      applyConfig: Task.async(function* () {
        Services.prefs.setBoolPref("privacy.trackingprotection.enabled", true);

        yield loadPage(HTTP_PAGE);
        yield openIdentityPopup();
      }),
    },

    trackingProtectionEnabled: {
      applyConfig: Task.async(function* () {
        Services.prefs.setBoolPref("privacy.trackingprotection.enabled", true);
        yield UrlClassifierTestUtils.addTestTrackers();

        yield loadPage(TRACKING_PAGE);
        yield openIdentityPopup();
      }),
    },

    trackingProtectionDisabled: {
      applyConfig: Task.async(function* () {
        let browserWindow = Services.wm.getMostRecentWindow("navigator:browser");
        let gBrowser = browserWindow.gBrowser;
        Services.prefs.setBoolPref("privacy.trackingprotection.enabled", true);
        yield UrlClassifierTestUtils.addTestTrackers();

        yield loadPage(TRACKING_PAGE);
        yield openIdentityPopup();
        // unblock the page
        gBrowser.ownerGlobal.document.querySelector("#tracking-action-unblock").click();
        yield BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser, false, TRACKING_PAGE);
        yield openIdentityPopup();
      }),
    },
  },
};

function* loadPage(url) {
  let browserWindow = Services.wm.getMostRecentWindow("navigator:browser");
  let gBrowser = browserWindow.gBrowser;
  BrowserTestUtils.loadURI(gBrowser.selectedBrowser, url);
  yield BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser, false, url);
}

function* openIdentityPopup(expand) {
  let browserWindow = Services.wm.getMostRecentWindow("navigator:browser");
  let gBrowser = browserWindow.gBrowser;
  let { gIdentityHandler } = gBrowser.ownerGlobal;
  gIdentityHandler._identityPopup.hidePopup();
  gIdentityHandler._identityBox.querySelector("#identity-icon").click();
  if (expand) {
    // give some time for opening to avoid weird style issues
    yield new Promise((c) => setTimeout(c, 500));
    gIdentityHandler._identityPopup.querySelector("#identity-popup-security-expander").click();
  }
}