summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/siteIdentity/browser_geolocation_indicator.js
blob: 078b7ab97545acb5c7a5e5dfa9de49da1b616ff8 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

requestLongerTimeout(2);

const { PermissionUI } = ChromeUtils.importESModule(
  "resource:///modules/PermissionUI.sys.mjs"
);

const { PermissionTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PermissionTestUtils.sys.mjs"
);

const CP = Cc["@mozilla.org/content-pref/service;1"].getService(
  Ci.nsIContentPrefService2
);

const EXAMPLE_PAGE_URL = "https://example.com";
const EXAMPLE_PAGE_URI = Services.io.newURI(EXAMPLE_PAGE_URL);
const EXAMPLE_PAGE_PRINCIPAL =
  Services.scriptSecurityManager.createContentPrincipal(EXAMPLE_PAGE_URI, {});
const GEO_CONTENT_PREF_KEY = "permissions.geoLocation.lastAccess";
const POLL_INTERVAL_FALSE_STATE = 50;

async function testGeoSharingIconVisible(state = true) {
  let sharingIcon = document.getElementById("geo-sharing-icon");
  ok(sharingIcon, "Geo sharing icon exists");

  try {
    await TestUtils.waitForCondition(
      () => sharingIcon.hasAttribute("sharing") === true,
      "Waiting for geo sharing icon visibility state",
      // If we wait for sharing icon to *not* show, waitForCondition will always timeout on correct state.
      // In these cases we want to reduce the wait time from 5 seconds to 2.5 seconds to prevent test duration timeouts
      !state ? POLL_INTERVAL_FALSE_STATE : undefined
    );
  } catch (e) {
    ok(!state, "Geo sharing icon not showing");
    return;
  }
  ok(state, "Geo sharing icon showing");
}

async function checkForDOMElement(state, id) {
  info(`Testing state ${state} of element  ${id}`);
  let el;
  try {
    await TestUtils.waitForCondition(
      () => {
        el = document.getElementById(id);
        return el != null;
      },
      `Waiting for ${id}`,
      !state ? POLL_INTERVAL_FALSE_STATE : undefined
    );
  } catch (e) {
    ok(!state, `${id} has correct state`);
    return el;
  }
  ok(state, `${id} has correct state`);

  return el;
}

async function testPermissionPopupGeoContainer(
  containerVisible,
  timestampVisible
) {
  // The container holds the timestamp element, therefore we can't have a
  // visible timestamp without the container.
  if (timestampVisible && !containerVisible) {
    ok(false, "Can't have timestamp without container");
  }

  // Only call openPermissionPopup if popup is closed, otherwise it does not resolve
  if (!gPermissionPanel._identityPermissionBox.hasAttribute("open")) {
    await openPermissionPopup();
  }

  let checkContainer = checkForDOMElement(
    containerVisible,
    "permission-popup-geo-container"
  );

  if (containerVisible && timestampVisible) {
    // Wait for the geo container to be fully populated.
    // The time label is computed async.
    let container = await checkContainer;
    await TestUtils.waitForCondition(
      () => container.childElementCount == 2,
      "permission-popup-geo-container should have two elements."
    );
    is(
      container.childNodes[0].classList[0],
      "permission-popup-permission-item",
      "Geo container should have permission item."
    );
    is(
      container.childNodes[1].id,
      "geo-access-indicator-item",
      "Geo container should have indicator item."
    );
  }
  let checkAccessIndicator = checkForDOMElement(
    timestampVisible,
    "geo-access-indicator-item"
  );

  return Promise.all([checkContainer, checkAccessIndicator]);
}

function openExamplePage(tabbrowser = gBrowser) {
  return BrowserTestUtils.openNewForegroundTab(tabbrowser, EXAMPLE_PAGE_URL);
}

function requestGeoLocation(browser) {
  return SpecialPowers.spawn(browser, [], () => {
    return new Promise(resolve => {
      content.navigator.geolocation.getCurrentPosition(
        () => resolve(true),
        error => resolve(error.code !== 1) // PERMISSION_DENIED = 1
      );
    });
  });
}

function answerGeoLocationPopup(allow, remember = false) {
  let notification = PopupNotifications.getNotification("geolocation");
  ok(
    PopupNotifications.isPanelOpen && notification,
    "Geolocation notification is open"
  );

  let rememberCheck = PopupNotifications.panel.querySelector(
    ".popup-notification-checkbox"
  );
  rememberCheck.checked = remember;

  let popupHidden = BrowserTestUtils.waitForEvent(
    PopupNotifications.panel,
    "popuphidden"
  );
  if (allow) {
    let allowBtn = PopupNotifications.panel.querySelector(
      ".popup-notification-primary-button"
    );
    allowBtn.click();
  } else {
    let denyBtn = PopupNotifications.panel.querySelector(
      ".popup-notification-secondary-button"
    );
    denyBtn.click();
  }
  return popupHidden;
}

function setGeoLastAccess(browser, state) {
  return new Promise(resolve => {
    let host = browser.currentURI.host;
    let handler = {
      handleCompletion: () => resolve(),
    };

    if (!state) {
      CP.removeByDomainAndName(
        host,
        GEO_CONTENT_PREF_KEY,
        browser.loadContext,
        handler
      );
      return;
    }
    CP.set(
      host,
      GEO_CONTENT_PREF_KEY,
      new Date().toString(),
      browser.loadContext,
      handler
    );
  });
}

async function testGeoLocationLastAccessSet(browser) {
  let timestamp = await new Promise(resolve => {
    let lastAccess = null;
    CP.getByDomainAndName(
      gBrowser.currentURI.spec,
      GEO_CONTENT_PREF_KEY,
      browser.loadContext,
      {
        handleResult(pref) {
          lastAccess = pref.value;
        },
        handleCompletion() {
          resolve(lastAccess);
        },
      }
    );
  });

  ok(timestamp != null, "Geo last access timestamp set");

  let parseSuccess = true;
  try {
    timestamp = new Date(timestamp);
  } catch (e) {
    parseSuccess = false;
  }
  ok(
    parseSuccess && !isNaN(timestamp),
    "Geo last access timestamp is valid Date"
  );
}

async function cleanup(tab) {
  await setGeoLastAccess(tab.linkedBrowser, false);
  SitePermissions.removeFromPrincipal(
    tab.linkedBrowser.contentPrincipal,
    "geo",
    tab.linkedBrowser
  );
  gBrowser.resetBrowserSharing(tab.linkedBrowser);
  BrowserTestUtils.removeTab(tab);
}

async function testIndicatorGeoSharingState(active) {
  let tab = await openExamplePage();
  gBrowser.updateBrowserSharing(tab.linkedBrowser, { geo: active });
  await testGeoSharingIconVisible(active);

  await cleanup(tab);
}

async function testIndicatorExplicitAllow(persistent) {
  let tab = await openExamplePage();

  let popupShown = BrowserTestUtils.waitForEvent(
    PopupNotifications.panel,
    "popupshown"
  );
  info("Requesting geolocation");
  let request = requestGeoLocation(tab.linkedBrowser);
  await popupShown;
  info("Allowing geolocation via popup");
  answerGeoLocationPopup(true, persistent);
  await request;

  await Promise.all([
    testGeoSharingIconVisible(true),
    testPermissionPopupGeoContainer(true, true),
    testGeoLocationLastAccessSet(tab.linkedBrowser),
  ]);

  await cleanup(tab);
}

// Indicator and permission popup entry shown after explicit PermissionUI geolocation allow
add_task(function test_indicator_and_timestamp_after_explicit_allow() {
  return testIndicatorExplicitAllow(false);
});
add_task(function test_indicator_and_timestamp_after_explicit_allow_remember() {
  return testIndicatorExplicitAllow(true);
});

// Indicator and permission popup entry shown after auto PermissionUI geolocation allow
add_task(async function test_indicator_and_timestamp_after_implicit_allow() {
  PermissionTestUtils.add(
    EXAMPLE_PAGE_URI,
    "geo",
    Services.perms.ALLOW_ACTION,
    Services.perms.EXPIRE_NEVER
  );
  let tab = await openExamplePage();
  let result = await requestGeoLocation(tab.linkedBrowser);
  ok(result, "Request should be allowed");

  await Promise.all([
    testGeoSharingIconVisible(true),
    testPermissionPopupGeoContainer(true, true),
    testGeoLocationLastAccessSet(tab.linkedBrowser),
  ]);

  await cleanup(tab);
});

// Indicator shown when manually setting sharing state to true
add_task(function test_indicator_sharing_state_active() {
  return testIndicatorGeoSharingState(true);
});

// Indicator not shown when manually setting sharing state to false
add_task(function test_indicator_sharing_state_inactive() {
  return testIndicatorGeoSharingState(false);
});

// Permission popup shows permission if geo permission is set to persistent allow
add_task(async function test_permission_popup_permission_scope_permanent() {
  PermissionTestUtils.add(
    EXAMPLE_PAGE_URI,
    "geo",
    Services.perms.ALLOW_ACTION,
    Services.perms.EXPIRE_NEVER
  );
  let tab = await openExamplePage();

  await testPermissionPopupGeoContainer(true, false); // Expect permission to be visible, but not lastAccess indicator

  await cleanup(tab);
});

// Sharing state set, but no permission
add_task(async function test_permission_popup_permission_sharing_state() {
  let tab = await openExamplePage();
  gBrowser.updateBrowserSharing(tab.linkedBrowser, { geo: true });
  await testPermissionPopupGeoContainer(true, false);

  await cleanup(tab);
});

// Permission popup has correct state if sharing state and last geo access timestamp are set
add_task(
  async function test_permission_popup_permission_sharing_state_timestamp() {
    let tab = await openExamplePage();
    gBrowser.updateBrowserSharing(tab.linkedBrowser, { geo: true });
    await setGeoLastAccess(tab.linkedBrowser, true);

    await testPermissionPopupGeoContainer(true, true);

    await cleanup(tab);
  }
);

// Clicking permission clear button clears permission and resets geo sharing state
add_task(async function test_permission_popup_permission_clear() {
  PermissionTestUtils.add(
    EXAMPLE_PAGE_URI,
    "geo",
    Services.perms.ALLOW_ACTION,
    Services.perms.EXPIRE_NEVER
  );
  let tab = await openExamplePage();
  gBrowser.updateBrowserSharing(tab.linkedBrowser, { geo: true });

  await openPermissionPopup();

  let clearButton = document.querySelector(
    "#permission-popup-geo-container button"
  );
  ok(clearButton, "Clear button is visible");
  clearButton.click();

  await Promise.all([
    testGeoSharingIconVisible(false),
    testPermissionPopupGeoContainer(false, false),
    TestUtils.waitForCondition(() => {
      let sharingState = tab._sharingState;
      return (
        sharingState == null ||
        sharingState.geo == null ||
        sharingState.geo === false
      );
    }, "Waiting for geo sharing state to reset"),
  ]);
  await cleanup(tab);
});

/**
 * Tests that we only show the last access label once when the sharing
 * state is updated multiple times while the popup is open.
 */
add_task(async function test_permission_no_duplicate_last_access_label() {
  let tab = await openExamplePage();
  await setGeoLastAccess(tab.linkedBrowser, true);
  await openPermissionPopup();
  gBrowser.updateBrowserSharing(tab.linkedBrowser, { geo: true });
  gBrowser.updateBrowserSharing(tab.linkedBrowser, { geo: true });
  await testPermissionPopupGeoContainer(true, true);
  await cleanup(tab);
});