summaryrefslogtreecommitdiffstats
path: root/toolkit/components/contentprefs/tests/mochitest/test_remoteContentPrefs.html
blob: d14e85a2556700c5b58c4ee7b1a15faaac6ee35e (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for nsIContentPrefService2 in child processes</title>
  <script type="application/javascript"
          src="/tests/SimpleTest/SimpleTest.js">
  </script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

  <script type="application/javascript;version=1.8">
    "use strict";

    SimpleTest.waitForExplicitFinish();

    const childFrameURL =
      "data:text/html,<!DOCTYPE HTML><html><body></body></html>";

    function childFrameScript(isFramePrivate) {
      "use strict";

      function Tester(resultArray) {
        this.results = [];
      }

      Tester.prototype.is =
        function(a, b, note) {
          this.results.push([a === b, note + " (" + a + ", " + b + ")"]);
        };
      Tester.prototype.ok =
        function(b, note) {
          this.results.push([b != false, note]);
        };

      var cps = Components.classes["@mozilla.org/content-pref/service;1"]
                          .getService(Components.interfaces.nsIContentPrefService2);

      let test = null;
      function* test1(message) {
        let tester = new Tester();

        tester.ok(cps !== null, "got the content pref service");

        cps.setGlobal("testing", 42, null, {
          handleCompletion: function(reason) {
            tester.is(reason, 0, "set a pref?");
            test.next();
          }
        });

        yield;

        let numResults = 0;
        cps.getGlobal("testing", null, {
          handleResult: function(pref) {
            numResults++;
            tester.is(pref.name, "testing", "pref has the right name");
            tester.is(pref.value, 42, "pref has the right value");
          },

          handleCompletion: function(reason) {
            tester.is(reason, 0, "get a pref?");
            tester.is(numResults, 1, "got the right number of prefs");
            tester.is(test.next().done, true, "done with test1");
            message.target.sendAsyncMessage("testRemoteContentPrefs:test1Finished",
                                            { results: tester.results });
          }
        });

        yield;
      }

      function* test2(message) {
        let tester = new Tester();

        let observer;
        let removed = false;
        cps.addObserverForName("testName", observer = {
          onContentPrefSet: function(group, name, value, isPrivate) {
            if (removed) {
              message.target.sendAsyncMessage("testRemoteContentPrefs:fail",
                                              { reason: "unexpected notification" });
            }
            tester.is(group, null, "group should be null");
            tester.is(name, "testName", "should only see testName");
            tester.is(value, 42, "value should be correct");
            tester.is(isPrivate, isFramePrivate, "privacy should match");

            message.target.sendAsyncMessage("testRemoteContentPrefs:test2poke2", {})
          },

          onContentPrefRemoved: function(group, name, isPrivate) {
            tester.is(group, null, "group should be null");
            tester.is(name, "testName");
            tester.is(isPrivate, isFramePrivate, "privacy should match");
            tester.is(test.next().done, true, "should be done with test2");

            cps.removeObserverForName("testName", observer);
            removed = true;

            message.target.sendAsyncMessage("testRemoteContentPrefs:test2Finished",
                                            { results: tester.results });
          }
        });

        message.target.sendAsyncMessage("testRemoteContentPrefs:test2poke", {});
        yield;
      }

      function* test3(message) {
        let tester = new Tester();

        cps.setGlobal("testName", 42, null, {
          handleCompletion: function(reason) {
            tester.is(reason, 0, "set a pref");
            cps.set("http://mochi.test", "testpref", "str", null, {
              handleCompletion: function(reason) {
                tester.is(reason, 0, "set a pref");
                test.next();
              }
            });
          }
        });

        yield;

        cps.removeByDomain("http://mochi.test", null, {
          handleCompletion: function(reason) {
            tester.is(reason, 0, "remove succeeded");
            cps.getByDomainAndName("http://mochi.test", "testpref", null, {
              handleResult: function() {
                message.target.sendAsyncMessage("testRemoteContentPrefs:fail",
                                                { reason: "got removed pref in test3" });
              },
              handleCompletion: function() {
                test.next();
              }
            });
          }
        });

        yield;

        message.target.sendAsyncMessage("testRemoteContentPrefs:test3Finished",
                                        { results: tester.results });
      }

      function* test4(message) {
        let tester = new Tester();

        let prefObserver = {
          onContentPrefSet: function(group, name, value, isPrivate) {
            test.next({ group: group, name: name, value: value, isPrivate: isPrivate });
          },
          onContentPrefRemoved: function(group, name, isPrivate) {
            test.next({ group: group, name: name, isPrivate: isPrivate });
          }
        };

        addMessageListener("testRemoteContentPrefs:prefResults", (msg) => {
          test.next(msg.data.results);
        });

        cps.addObserverForName("test", prefObserver);

        cps.set("http://mochi.test", "test", 42, { usePrivateBrowsing: true });
        let event = yield;
        tester.is(event.name, "test");
        tester.is(event.isPrivate, true);

        message.target.sendAsyncMessage("testRemoteContentPrefs:getPref",
                                        { group: "http://mochi.test", name: "test" });

        let results = yield;
        tester.is(results.length, 0, "should not have seen the pb pref");

        message.target.sendAsyncMessage("testRemoteContentPrefs:test4Finished",
                                        { results: tester.results });
      }

      addMessageListener("testRemoteContentPrefs:test1", function(message) {
        test = test1(message);
        test.next();
      });
      addMessageListener("testRemoteContentPrefs:test2", function(message) {
        test = test2(message);
        test.next();
      });
      addMessageListener("testRemoteContentPrefs:test3", function(message) {
        test = test3(message);
        test.next();
      });
      addMessageListener("testRemoteContentPrefs:test4", function(message) {
        test = test4(message);
        test.next();
      });
    }

    function processResults(results) {
      for (let i of results) {
        ok(...i);
      }
    }

    let test;
    function* testStructure(mm, isPrivate, callback) {
      let lastResult;

      function testDone(msg) {
        test.next(msg.data);
      }

      mm.addMessageListener("testRemoteContentPrefs:test1Finished", testDone);
      mm.addMessageListener("testRemoteContentPrefs:test2Finished", testDone);
      mm.addMessageListener("testRemoteContentPrefs:test3Finished", testDone);
      mm.addMessageListener("testRemoteContentPrefs:test4Finished", testDone);

      mm.addMessageListener("testRemoteContentPrefs:fail", function(msg) {
        ok(false, msg.data.reason);
      });

      mm.sendAsyncMessage("testRemoteContentPrefs:test1", {});
      lastResult = yield;
      processResults(lastResult.results);

      var cps = SpecialPowers.Cc["@mozilla.org/content-pref/service;1"]
                             .getService(SpecialPowers.Ci.nsIContentPrefService2);
      mm.sendAsyncMessage("testRemoteContentPrefs:test2", {});
      mm.addMessageListener("testRemoteContentPrefs:test2poke", function() {
        cps.setGlobal("testName", 42, {usePrivateBrowsing: isPrivate});
      });
      mm.addMessageListener("testRemoteContentPrefs:test2poke2", function() {
        cps.removeGlobal("testName", {usePrivateBrowsing: isPrivate});
      });

      lastResult = yield;
      processResults(lastResult.results);

      mm.sendAsyncMessage("testRemoteContentPrefs:test3", {});
      lastResult = yield;
      processResults(lastResult.results);

      mm.addMessageListener("testRemoteContentPrefs:getPref", function(msg) {
        let results = [];
        cps.getByDomainAndName(msg.data.group, msg.data.name, null, {
          handleResult: function(pref) {
            results.push(pref);
          },
          handleCompletion: function(reason) {
            mm.sendAsyncMessage("testRemoteContentPrefs:prefResults",
                                { results: results });
          }
        });
      });

      mm.sendAsyncMessage("testRemoteContentPrefs:test4", {});
      lastResult = yield;
      processResults(lastResult.results);

      document.getElementById('iframe').remove();
      setTimeout(callback, 0);
    }

    function runTest(isPrivate, callback) {
      info("testing with isPrivate=" + isPrivate);
      let iframe = document.createElement("iframe");
      SpecialPowers.wrap(iframe).mozbrowser = true;
      if (isPrivate) {
        SpecialPowers.wrap(iframe).mozprivatebrowsing = true;
      }
      iframe.id = "iframe";
      iframe.src = childFrameURL;

      iframe.addEventListener("mozbrowserloadend", function() {
        info("Got iframe load event.");
        let mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
        mm.loadFrameScript("data:,(" + childFrameScript.toString() + ")(" + isPrivate + ");",
                           false);

        test = testStructure(mm, isPrivate, callback);
        test.next();
      });

      document.body.appendChild(iframe);
    }

    function runTests() {
      info("Browser prefs set.");
      runTest(false, function() {
        runTest(true, function() {
          SimpleTest.finish();
        });
      });
    }

    addEventListener("load", function() {
      info("Got load event.");

      SpecialPowers.addPermission("browser", true, document);
      SpecialPowers.pushPrefEnv({
        "set": [
          ["dom.ipc.browser_frames.oop_by_default", true],
          ["dom.mozBrowserFramesEnabled", true],
          ["browser.pagethumbnails.capturing_disabled", true]
        ]
      }, runTests);
    });
  </script>
</body>
</html>