summaryrefslogtreecommitdiffstats
path: root/browser/modules/test/browser/browser_UsageTelemetry_uniqueOriginsVisitedInPast24Hours.js
blob: ec5da648822d3b958e1efe6e18aa009b7ac1eb7f (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
/* eslint-disable mozilla/no-arbitrary-setTimeout */
/* 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";

ChromeUtils.defineModuleGetter(
  this,
  "URICountListener",
  "resource:///modules/BrowserUsageTelemetry.jsm"
);

add_task(async function test_uniqueDomainsVisitedInPast24Hours() {
  // By default, proxies don't apply to 127.0.0.1. We need them to for this test, though:
  await SpecialPowers.pushPrefEnv({
    set: [["network.proxy.allow_hijacking_localhost", true]],
  });
  registerCleanupFunction(async () => {
    info("Cleaning up");
    URICountListener.resetUniqueDomainsVisitedInPast24Hours();
  });

  URICountListener.resetUniqueDomainsVisitedInPast24Hours();
  let startingCount = URICountListener.uniqueDomainsVisitedInPast24Hours;
  is(
    startingCount,
    0,
    "We should have no domains recorded in the history right after resetting"
  );

  // Add a new window and then some tabs in it.
  let win = await BrowserTestUtils.openNewBrowserWindow();
  await BrowserTestUtils.openNewForegroundTab(
    win.gBrowser,
    "http://example.com"
  );

  await BrowserTestUtils.openNewForegroundTab(
    win.gBrowser,
    "http://test1.example.com"
  );
  is(
    URICountListener.uniqueDomainsVisitedInPast24Hours,
    startingCount + 1,
    "test1.example.com should only count as a unique visit if example.com wasn't visited before"
  );

  await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "http://127.0.0.1");
  is(
    URICountListener.uniqueDomainsVisitedInPast24Hours,
    startingCount + 1,
    "127.0.0.1 should not count as a unique visit"
  );

  // Set the expiry time to 4 seconds. The value should be reasonably short
  // for testing, but long enough so that waiting for openNewForegroundTab
  // does not cause the expiry timeout to run.
  await SpecialPowers.pushPrefEnv({
    set: [["browser.engagement.recent_visited_origins.expiry", 4]],
  });

  // http://www.exämple.test
  await BrowserTestUtils.openNewForegroundTab(
    win.gBrowser,
    "http://xn--exmple-cua.test"
  );
  is(
    URICountListener.uniqueDomainsVisitedInPast24Hours,
    startingCount + 2,
    "www.exämple.test should count as a unique visit"
  );

  let countBefore = URICountListener.uniqueDomainsVisitedInPast24Hours;

  // If expiration does not work correctly, the following will time out.
  await BrowserTestUtils.waitForCondition(() => {
    return (
      URICountListener.uniqueDomainsVisitedInPast24Hours == countBefore - 1
    );
  }, 250);

  let countAfter = URICountListener.uniqueDomainsVisitedInPast24Hours;
  is(countAfter, countBefore - 1, "The expiry should work correctly");

  BrowserTestUtils.removeTab(win.gBrowser.selectedTab);
  BrowserTestUtils.removeTab(win.gBrowser.selectedTab);
  await BrowserTestUtils.closeWindow(win);
});