summaryrefslogtreecommitdiffstats
path: root/mobile/android/tests/browser/chrome/test_offline_page.html
blob: e1b723266923c4b32bf6ff41dea5119499536fec (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1089190
Migrated from Robocop: https://bugzilla.mozilla.org/show_bug.cgi?id=1184186
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1089190</title>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
  <script type="application/javascript" src="head.js"></script>
  <script type="application/javascript;version=1.7">

  "use strict";

  const { classes: Cc, interfaces: Ci, utils: Cu } = Components;

  Cu.import("resource://gre/modules/Services.jsm");
  Cu.import("resource://gre/modules/Messaging.jsm");
  Cu.import("resource://gre/modules/Task.jsm");

  // Provide a helper to yield until we are sure the offline state has changed
  function promiseOffline(isOffline) {
    return new Promise((resolve, reject) => {
      function observe(subject, topic, data) {
        info("Received topic: " + topic);
        Services.obs.removeObserver(observe, "network:offline-status-changed");
        resolve();
      }
      Services.obs.addObserver(observe, "network:offline-status-changed", false);
      Services.io.offline = isOffline;
    });
  }

  // The chrome window
  let chromeWin;

  // Track the <browser> where the tests are happening
  let browser;

  // The proxy setting
  let proxyPrefValue;

  const kUniqueURI = Services.io.newURI("http://mochi.test:8888/chrome/mobile/android/tests/browser/chrome/video_controls.html", null, null);

  add_task(function* test_offline() {
    // 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.
    Cc["@mozilla.org/netwerk/cache-storage-service;1"].getService(Ci.nsICacheStorageService).clear();

    chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
    let BrowserApp = chromeWin.BrowserApp;

    // Add a new tab with a blank page so we can better control the real page load and the offline state
    browser = BrowserApp.addTab("about:blank", { selected: true, parentId: BrowserApp.selectedTab.id }).browser;

    SimpleTest.registerCleanupFunction(function() {
      BrowserApp.closeTab(BrowserApp.getTabForBrowser(browser));
      Services.prefs.setIntPref("network.proxy.type", proxyPrefValue);
      Services.io.offline = false;
    });

    // Go offline, expecting the error page.
    yield promiseOffline(true);

    // Load our test web page
    browser.loadURI(kUniqueURI.spec, null, null)
    yield promiseBrowserEvent(browser, "DOMContentLoaded");

    // This is an error page.
    is(browser.contentDocument.documentURI.substring(0, 27), "about:neterror?e=netOffline", "Document URI is the error page.");

    // But location bar should show the original request.
    is(browser.contentWindow.location.href, kUniqueURI.spec, "Docshell URI is the original URI.");

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

    // Go online and try to load the page again
    yield promiseOffline(false);

    ok(browser.contentDocument.getElementById("errorTryAgain"), "The error page has got a #errorTryAgain element");

    // Click "Try Again" button to start the page load
    browser.contentDocument.getElementById("errorTryAgain").click();
    yield promiseBrowserEvent(browser, "DOMContentLoaded");

    // This is not an error page.
    is(browser.contentDocument.documentURI, kUniqueURI.spec, "Document URI is not the offline-error page, but the original URI.");
  });

  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1089190">Mozilla Bug 1089190</a>
<br>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1184186">Migrated from Robocop testOfflinePage</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>