summaryrefslogtreecommitdiffstats
path: root/toolkit/components/contentprefs/tests/browser/browser_remoteContentPrefs.js
blob: 00c845e488d7f9bcb1e0ae437050c97d755106c5 (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
"use strict";

const childFrameURL = Services.io.newURI(
  "data:text/html,<!DOCTYPE HTML><html><body></body></html>"
);

async function runTestsForFrame(browser, isPrivate) {
  let loadContext = Cu.createLoadContext();
  let privateLoadContext = Cu.createPrivateLoadContext();

  await SpecialPowers.spawn(browser, [], async () => {
    var cps = Cc["@mozilla.org/content-pref/service;1"].getService(
      Ci.nsIContentPrefService2
    );
    Assert.ok(cps !== null, "got the content pref service");

    await new Promise(resolve => {
      cps.setGlobal("testing", 42, null, {
        handleCompletion(reason) {
          Assert.equal(reason, 0, "set a pref?");
          resolve();
        },
      });
    });

    let numResults = 0;
    await new Promise(resolve => {
      cps.getGlobal("testing", null, {
        handleResult(pref) {
          numResults++;
          Assert.equal(pref.name, "testing", "pref has the right name");
          Assert.equal(pref.value, 42, "pref has the right value");
        },

        handleCompletion(reason) {
          Assert.equal(reason, 0, "get a pref?");
          Assert.equal(numResults, 1, "got the right number of prefs");
          resolve();
        },
      });
    });
  });

  await SpecialPowers.spawn(browser, [isPrivate], async isFramePrivate => {
    var cps = Cc["@mozilla.org/content-pref/service;1"].getService(
      Ci.nsIContentPrefService2
    );

    if (!content._result) {
      content._result = [];
    }

    let observer;
    cps.addObserverForName(
      "testName",
      (observer = {
        onContentPrefSet(group, name, value, isPrivate) {
          Assert.equal(group, null, "group should be null");
          Assert.equal(name, "testName", "should only see testName");
          Assert.equal(value, 42, "value should be correct");
          Assert.equal(isPrivate, isFramePrivate, "privacy should match");

          content._result.push("set");
        },

        onContentPrefRemoved(group, name, isPrivate) {
          Assert.equal(group, null, "group should be null");
          Assert.equal(name, "testName", "name should match");
          Assert.equal(isPrivate, isFramePrivate, "privacy should match");

          content._result.push("removed");
        },
      })
    );
    content._observer = observer;
  });

  for (let expectedResponse of ["set", "removed"]) {
    var cps = Cc["@mozilla.org/content-pref/service;1"].getService(
      Ci.nsIContentPrefService2
    );

    if (expectedResponse == "set") {
      cps.setGlobal(
        "testName",
        42,
        isPrivate ? privateLoadContext : loadContext
      );
    } else {
      cps.removeGlobal(
        "testName",
        isPrivate ? privateLoadContext : loadContext
      );
    }

    let response = await SpecialPowers.spawn(
      browser,
      [expectedResponse],
      async expected => {
        var cps = Cc["@mozilla.org/content-pref/service;1"].getService(
          Ci.nsIContentPrefService2
        );
        await ContentTaskUtils.waitForCondition(
          () => content._result.length == 1,
          "expecting one notification"
        );

        if (expected == "removed") {
          cps.removeObserverForName("testName", content._observer);
        }

        Assert.equal(
          content._result.length,
          1,
          "correct number of notifications"
        );
        let result = content._result[0];
        content._result = [];
        return result;
      }
    );

    is(response, expectedResponse, "got correct observer notification");
  }

  await SpecialPowers.spawn(browser, [], async () => {
    var cps = Cc["@mozilla.org/content-pref/service;1"].getService(
      Ci.nsIContentPrefService2
    );

    await new Promise(resolve => {
      cps.setGlobal("testName", 42, null, {
        handleCompletion(reason) {
          Assert.equal(reason, 0, "set a pref");
          cps.set("http://mochi.test", "testpref", "str", null, {
            /* eslint no-shadow: 0 */
            handleCompletion(reason) {
              Assert.equal(reason, 0, "set a pref");
              resolve();
            },
          });
        },
      });
    });

    await new Promise(resolve => {
      cps.removeByDomain("http://mochi.test", null, {
        handleCompletion(reason) {
          Assert.equal(reason, 0, "remove succeeded");
          cps.getByDomainAndName("http://mochi.test", "testpref", null, {
            handleResult() {
              throw new Error("got removed pref in test3");
            },
            handleCompletion() {
              resolve();
            },
            handleError(rv) {
              throw new Error(`got a pref error ${rv}`);
            },
          });
        },
      });
    });
  });

  await SpecialPowers.spawn(browser, [], async () => {
    var cps = Cc["@mozilla.org/content-pref/service;1"].getService(
      Ci.nsIContentPrefService2
    );

    let event = await new Promise((resolve, reject) => {
      let prefObserver = {
        onContentPrefSet(group, name, value, isPrivate) {
          resolve({ group, name, value, isPrivate });
        },
        onContentPrefRemoved(group, name, isPrivate) {
          reject("got unexpected notification");
        },
      };

      cps.addObserverForName("test", prefObserver);

      let privateLoadContext = Cu.createPrivateLoadContext();
      cps.set("http://mochi.test", "test", 42, privateLoadContext);

      cps.addObserverForName("test", prefObserver);
    });

    Assert.equal(event.name, "test", "got the right event");
    Assert.equal(event.isPrivate, true, "the event was for an isPrivate pref");
  });

  await SpecialPowers.spawn(browser, [], async () => {
    var cps = Cc["@mozilla.org/content-pref/service;1"].getService(
      Ci.nsIContentPrefService2
    );

    let results = [];
    await new Promise(resolve => {
      cps.getByDomainAndName("http://mochi.test", "test", null, {
        handleResult(pref) {
          info("received handleResult");
          results.push(pref);
        },
        handleCompletion(reason) {
          resolve();
        },
        handleError(rv) {
          ok(false, `failed to get pref ${rv}`);
          throw new Error("got unexpected error");
        },
      });
    });

    Assert.equal(results.length, 0, "should not have seen the pb pref");
  });
}

async function runTest(isPrivate) {
  /* globals Services */
  info("testing with isPrivate=" + isPrivate);

  let newWindow = await BrowserTestUtils.openNewBrowserWindow({
    private: isPrivate,
  });

  const system = Services.scriptSecurityManager.getSystemPrincipal();

  let browser = newWindow.gBrowser.selectedBrowser;
  let loadedPromise = BrowserTestUtils.browserLoaded(browser);
  browser.loadURI(childFrameURL, { triggeringPrincipal: system });
  await loadedPromise;

  await runTestsForFrame(browser, isPrivate);

  newWindow.close();
}

add_task(async function () {
  await runTest(false);
  await runTest(true);
});