1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/
7 // ClientID fails without...
10 const { TestUtils } = ChromeUtils.importESModule(
11 "resource://testing-common/TestUtils.sys.mjs"
13 const { ClientID } = ChromeUtils.importESModule(
14 "resource://gre/modules/ClientID.sys.mjs"
16 const { Discovery } = ChromeUtils.importESModule(
17 "resource:///modules/Discovery.sys.mjs"
19 const { ContextualIdentityService } = ChromeUtils.importESModule(
20 "resource://gre/modules/ContextualIdentityService.sys.mjs"
23 const TAAR_COOKIE_NAME = "taarId";
25 add_task(async function test_discovery() {
26 let uri = Services.io.newURI("https://example.com/foobar");
28 // Ensure the prefs we need
29 Services.prefs.setBoolPref("browser.discovery.enabled", true);
30 Services.prefs.setBoolPref("browser.discovery.containers.enabled", true);
31 Services.prefs.setBoolPref("datareporting.healthreport.uploadEnabled", true);
32 Services.prefs.setCharPref("browser.discovery.sites", uri.host);
34 registerCleanupFunction(() => {
35 Services.prefs.clearUserPref("browser.discovery.enabled");
36 Services.prefs.clearUserPref("browser.discovery.containers.enabled");
37 Services.prefs.clearUserPref("browser.discovery.sites");
38 Services.prefs.clearUserPref("datareporting.healthreport.uploadEnabled");
41 // This is normally initialized by telemetry, force id creation. This results
42 // in Discovery setting the cookie.
43 await ClientID.getClientID();
44 await Discovery.update();
47 Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {}),
51 !Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {
54 "no private cookie exists"
56 ContextualIdentityService.getPublicIdentities().forEach(identity => {
57 let { userContextId } = identity;
59 Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {
67 // Test the addition of a new container.
68 let changed = TestUtils.topicObserved("cookie-changed", subject => {
70 .QueryInterface(Ci.nsICookieNotification)
71 .cookie.QueryInterface(Ci.nsICookie);
72 equal(cookie.name, TAAR_COOKIE_NAME, "taar cookie exists");
73 equal(cookie.host, uri.host, "cookie exists for host");
75 cookie.originAttributes.userContextId,
76 container.userContextId,
77 "cookie userContextId is correct"
81 let container = ContextualIdentityService.create(
89 Discovery.enabled = false;
90 // Wait for the update to remove the cookie.
91 await TestUtils.waitForCondition(() => {
92 return !Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {});
95 ContextualIdentityService.getPublicIdentities().forEach(identity => {
96 let { userContextId } = identity;
98 !Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {
105 // turn off containers
106 Services.prefs.setBoolPref("browser.discovery.containers.enabled", false);
108 Discovery.enabled = true;
109 await TestUtils.waitForCondition(() => {
110 return Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {});
112 // make sure we did not set cookies on containers
113 ContextualIdentityService.getPublicIdentities().forEach(identity => {
114 let { userContextId } = identity;
116 !Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {
123 // Make sure clientId changes update discovery
124 changed = TestUtils.topicObserved("cookie-changed", subject => {
125 let notification = subject.QueryInterface(Ci.nsICookieNotification);
126 if (notification.action != Ci.nsICookieNotification.COOKIE_ADDED) {
129 let cookie = notification.cookie.QueryInterface(Ci.nsICookie);
130 equal(cookie.name, TAAR_COOKIE_NAME, "taar cookie exists");
131 equal(cookie.host, uri.host, "cookie exists for host");
134 await ClientID.removeClientID();
135 await ClientID.getClientID();
138 // Make sure disabling telemetry disables discovery.
139 Services.prefs.setBoolPref("datareporting.healthreport.uploadEnabled", false);
140 await TestUtils.waitForCondition(() => {
141 return !Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {});