summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/browser/browser_bug680727.js
blob: 313adb3c7526561321fe86f0ea97161728429aaf (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/* Ensure that clicking the button in the Offline mode neterror page updates
   global history. See bug 680727. */
/* TEST_PATH=toolkit/components/places/tests/browser/browser_bug680727.js make -C $(OBJDIR) mochitest-browser-chrome */

const kUniqueURI = Services.io.newURI("http://mochi.test:8888/#bug_680727");
var proxyPrefValue;
var ourTab;

function test() {
  waitForExplicitFinish();

  // Tests always connect to localhost, and per bug 87717, localhost is now
  // reachable in offline mode.  To avoid this, disable any proxy.
  proxyPrefValue = Services.prefs.getIntPref("network.proxy.type");
  Services.prefs.setIntPref("network.proxy.type", 0);

  // Clear network cache.
  Services.cache2.clear();

  // Go offline, expecting the error page.
  Services.io.offline = true;

  BrowserTestUtils.openNewForegroundTab(gBrowser).then(tab => {
    ourTab = tab;
    BrowserTestUtils.browserLoaded(
      ourTab.linkedBrowser,
      false,
      null,
      true
    ).then(errorListener);
    BrowserTestUtils.startLoadingURIString(
      ourTab.linkedBrowser,
      kUniqueURI.spec
    );
  });
}

// ------------------------------------------------------------------------------
// listen to loading the neterror page. (offline mode)
function errorListener() {
  ok(Services.io.offline, "Services.io.offline is true.");

  // This is an error page.
  SpecialPowers.spawn(ourTab.linkedBrowser, [kUniqueURI.spec], function (uri) {
    Assert.equal(
      content.document.documentURI.substring(0, 27),
      "about:neterror?e=netOffline",
      "Document URI is the error page."
    );

    // But location bar should show the original request.
    Assert.equal(
      content.location.href,
      uri,
      "Docshell URI is the original URI."
    );
  }).then(() => {
    // Global history does not record URI of a failed request.
    PlacesTestUtils.promiseAsyncUpdates().then(() => {
      PlacesUtils.history.hasVisits(kUniqueURI).then(isVisited => {
        errorAsyncListener(kUniqueURI, isVisited);
      });
    });
  });
}

function errorAsyncListener(aURI, aIsVisited) {
  ok(
    kUniqueURI.equals(aURI) && !aIsVisited,
    "The neterror page is not listed in global history."
  );

  Services.prefs.setIntPref("network.proxy.type", proxyPrefValue);

  // Now press the "Try Again" button, with offline mode off.
  Services.io.offline = false;

  BrowserTestUtils.browserLoaded(ourTab.linkedBrowser, false, null, true).then(
    reloadListener
  );

  SpecialPowers.spawn(ourTab.linkedBrowser, [], function () {
    Assert.ok(
      content.document.querySelector("#netErrorButtonContainer > .try-again"),
      "The error page has got a .try-again element"
    );
    content.document
      .querySelector("#netErrorButtonContainer > .try-again")
      .click();
  });
}

// ------------------------------------------------------------------------------
// listen to reload of neterror.
function reloadListener() {
  // This listener catches "DOMContentLoaded" on being called
  // nsIWPL::onLocationChange(...). That is right *AFTER*
  // IHistory::VisitURI(...) is called.
  ok(!Services.io.offline, "Services.io.offline is false.");

  SpecialPowers.spawn(ourTab.linkedBrowser, [kUniqueURI.spec], function (uri) {
    // This is not an error page.
    Assert.equal(
      content.document.documentURI,
      uri,
      "Document URI is not the offline-error page, but the original URI."
    );
  }).then(() => {
    // Check if global history remembers the successfully-requested URI.
    PlacesTestUtils.promiseAsyncUpdates().then(() => {
      PlacesUtils.history.hasVisits(kUniqueURI).then(isVisited => {
        reloadAsyncListener(kUniqueURI, isVisited);
      });
    });
  });
}

function reloadAsyncListener(aURI, aIsVisited) {
  ok(kUniqueURI.equals(aURI) && aIsVisited, "We have visited the URI.");
  PlacesUtils.history.clear().then(finish);
}

registerCleanupFunction(async function () {
  Services.prefs.setIntPref("network.proxy.type", proxyPrefValue);
  Services.io.offline = false;
  BrowserTestUtils.removeTab(ourTab);
});