summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/browser/browser_visituri_privatebrowsing_perwindowpb.js
blob: abde69a7d99e1ad0a7f3afa4a93d1e9b2c7f4274 (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
/* 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/. */

function test() {
  // initialization
  waitForExplicitFinish();
  let windowsToClose = [];
  let initialURL =
    "http://example.com/tests/toolkit/components/places/tests/browser/begin.html";
  let finalURL =
    "http://example.com/tests/toolkit/components/places/tests/browser/final.html";
  let observer = null;
  let enumerator = null;
  let currentObserver = null;
  let uri = null;

  function doTest(aIsPrivateMode, aWindow, aTestURI, aCallback) {
    observer = {
      observe: function(aSubject, aTopic, aData) {
        // The uri-visit-saved topic should only work when on normal mode.
        if (aTopic == "uri-visit-saved") {
          // Remove the observers set on per window private mode and normal
          // mode.
          enumerator = aWindow.Services.obs.enumerateObservers("uri-visit-saved");
          while (enumerator.hasMoreElements()) {
            currentObserver = enumerator.getNext();
            aWindow.Services.obs.removeObserver(currentObserver, "uri-visit-saved");
          }

          // The expected visit should be the finalURL because private mode
          // should not register a visit with the initialURL.
          uri = aSubject.QueryInterface(Ci.nsIURI);
          is(uri.spec, finalURL, "Check received expected visit");
        }
      }
    };

    aWindow.Services.obs.addObserver(observer, "uri-visit-saved", false);

    BrowserTestUtils.browserLoaded(aWindow.gBrowser.selectedBrowser).then(aCallback);
    aWindow.gBrowser.selectedBrowser.loadURI(aTestURI);
  }

  function testOnWindow(aOptions, aCallback) {
    whenNewWindowLoaded(aOptions, function(aWin) {
      windowsToClose.push(aWin);
      // execute should only be called when need, like when you are opening
      // web pages on the test. If calling executeSoon() is not necesary, then
      // call whenNewWindowLoaded() instead of testOnWindow() on your test.
      executeSoon(() => aCallback(aWin));
    });
  }

   // This function is called after calling finish() on the test.
  registerCleanupFunction(function() {
    windowsToClose.forEach(function(aWin) {
      aWin.close();
    });
  });

  // test first when on private mode
  testOnWindow({private: true}, function(aWin) {
    doTest(true, aWin, initialURL, function() {
      // then test when not on private mode
      testOnWindow({}, function(aWin2) {
        doTest(false, aWin2, finalURL, function () {
          PlacesTestUtils.clearHistory().then(finish);
        });
      });
    });
  });
}