summaryrefslogtreecommitdiffstats
path: root/docshell/test/mochitest/test_bug529119-2.html
blob: a8bd57d4f7ebdc11a422441aa2fe1e36f202663a (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
<!DOCTYPE HTML>
<html>
<head>
<title>Test bug 529119</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />

<script class="testbody" type="text/javascript">

SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("untriaged");

var workingURL = "http://mochi.test:8888/tests/docshell/test/mochitest/bug529119-window.html";
// eslint-disable-next-line @microsoft/sdl/no-insecure-url
var faultyURL = "http://some-nonexistent-domain-27489274c892748217cn2384.test/";

var w = null;
var phase = 0;
var isWindowLoaded = false;
// Token that represents which page we currently have loaded.
var token = 0;

function delay(msec) {
  return new Promise(resolve => setTimeout(resolve, msec));
}

async function assignToken(tokenToAssign) {
  await SpecialPowers.spawn(w, [tokenToAssign],
                            newToken => { this.content.token = newToken });
}

// Returns when a new page is loaded and returns whether that page is an
// error page.
async function pollForPage(win) {
  while (true) {
    try {
      // When we do our navigation, there may be an interstitial about:blank
      // page if the navigation involves a process switch.  That about:blank
      // will exist between the new process's docshell being created and the
      // actual page that's being loaded loading (which can happen async from
      // the docshell creation).  We want to avoid treating the initial
      // about:blank as a new page.
      //
      // We could conceivably expose Document::IsInitialDocument() as a
      // ChromeOnly thing and use it here, but let's just filter out all
      // about:blank, since we don't expect any in this test.
      var haveNewPage = await SpecialPowers.spawn(w, [token],
        currentToken => this.content.token != currentToken &&
                        this.content.location.href != "about:blank");

      if (haveNewPage) {
        ++token;
        assignToken(token);

        // In this test, error pages are non-same-origin with us, and non-error
        // pages are same-origin.
        let haveErrorPage = false;
        try {
          win.document.title;
        } catch (ex) {
          haveErrorPage = true;
        }
        return haveErrorPage;
      }
    } catch (e) {
      // Something went wrong; just keep waiting.
    }

    await delay(100);
  }
}

async function windowLoaded() {
  // The code under here should only be run once
  // The test popup window workingURL was already opened
  if (isWindowLoaded)
    return;
  isWindowLoaded = true;

  assignToken(token);

  /* 2. We have successfully loaded a page, now go to a faulty URL */
  // XXX The test fails when we change the location synchronously
  window.setTimeout(function() {
    w.location.href = faultyURL;
  }, 0);

  ok(await pollForPage(w), "Waiting for error page succeeded");
  /* 3. now, while we are on the error page, navigate back */
  try {
    // We need the SpecialPowers bit, because this is a cross-origin window
    // and we normally can't touch .history on those.
    await SpecialPowers.spawn(w, [], () => this.content.history.back());
  } catch (ex) {
    ok(false, "w.history.back() threw " + ex);
  }

  ok(!await pollForPage(w), "Waiting for original page succeeded");
  /* 4-finish, check we are back at the original page */
  is(await SpecialPowers.spawn(w, [], () => this.content.location.href),
     workingURL,
     "Is on the previous page");
  w.close();
  SimpleTest.finish();
}

function startTest() {
  /* 1. load a URL that leads to an error page */
  w = window.open(workingURL);
}

</script>
</head>
<body onload="startTest();">
</body>
</html>