summaryrefslogtreecommitdiffstats
path: root/browser/modules/test/unit/test_HomePage_ignore.js
blob: d62d55738ae4fe61a8fa75aab0707935365c8664 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

"use strict";

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

ChromeUtils.defineESModuleGetters(this, {
  TelemetryTestUtils: "resource://testing-common/TelemetryTestUtils.sys.mjs",
});

XPCOMUtils.defineLazyModuleGetters(this, {
  HomePage: "resource:///modules/HomePage.jsm",
  RemoteSettings: "resource://services-settings/remote-settings.js",
  sinon: "resource://testing-common/Sinon.jsm",
});

const HOMEPAGE_IGNORELIST = "homepage-urls";

/**
 * Provides a basic set of remote settings for use in tests.
 */
async function setupRemoteSettings() {
  const settings = await RemoteSettings("hijack-blocklists");
  sinon.stub(settings, "get").returns([
    {
      id: HOMEPAGE_IGNORELIST,
      matches: ["ignore=me", "ignoreCASE=ME"],
      _status: "synced",
    },
  ]);
}

add_task(async function setup() {
  await setupRemoteSettings();
});

add_task(async function test_initWithIgnoredPageCausesReset() {
  // Set the preference direct as the set() would block us.
  Services.prefs.setStringPref(
    "browser.startup.homepage",
    "http://bad/?ignore=me"
  );
  Assert.ok(HomePage.overridden, "Should have overriden the homepage");

  await HomePage.delayedStartup();

  Assert.ok(
    !HomePage.overridden,
    "Should no longer be overriding the homepage."
  );
  Assert.equal(
    HomePage.get(),
    HomePage.getDefault(),
    "Should have reset to the default preference"
  );

  TelemetryTestUtils.assertEvents(
    [{ object: "ignore", value: "saved_reset" }],
    {
      category: "homepage",
      method: "preference",
    }
  );
});

add_task(async function test_updateIgnoreListCausesReset() {
  Services.prefs.setStringPref(
    "browser.startup.homepage",
    "http://bad/?new=ignore"
  );
  Assert.ok(HomePage.overridden, "Should have overriden the homepage");

  // Simulate an ignore list update.
  await RemoteSettings("hijack-blocklists").emit("sync", {
    data: {
      current: [
        {
          id: HOMEPAGE_IGNORELIST,
          schema: 1553857697843,
          last_modified: 1553859483588,
          matches: ["ignore=me", "ignoreCASE=ME", "new=ignore"],
        },
      ],
    },
  });

  Assert.ok(
    !HomePage.overridden,
    "Should no longer be overriding the homepage."
  );
  Assert.equal(
    HomePage.get(),
    HomePage.getDefault(),
    "Should have reset to the default preference"
  );
  TelemetryTestUtils.assertEvents(
    [{ object: "ignore", value: "saved_reset" }],
    {
      category: "homepage",
      method: "preference",
    }
  );
});

async function testSetIgnoredUrl(url) {
  Assert.ok(!HomePage.overriden, "Should not be overriding the homepage");

  await HomePage.set(url);

  Assert.equal(
    HomePage.get(),
    HomePage.getDefault(),
    "Should still have the default homepage."
  );
  Assert.ok(!HomePage.overriden, "Should not be overriding the homepage.");
  TelemetryTestUtils.assertEvents(
    [{ object: "ignore", value: "set_blocked" }],
    {
      category: "homepage",
      method: "preference",
    }
  );
}

add_task(async function test_setIgnoredUrl() {
  await testSetIgnoredUrl("http://bad/?ignore=me");
});

add_task(async function test_setIgnoredUrl_case() {
  await testSetIgnoredUrl("http://bad/?Ignorecase=me");
});