summaryrefslogtreecommitdiff
path: root/docshell/test/test_bug590573.html
blob: e218140ea6a01d6baa9a6f1aeb2ffc2002f37c0e (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=590573
-->
<head>
  <title>Test for Bug 590573</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=590573">Mozilla Bug 590573</a>

<script type='application/javascript;version=1.7'>
SimpleTest.waitForExplicitFinish();

// Listen to the first callback, since this indicates that the page loaded.
var page1LoadCallbackEnabled = true;
function page1Load()
{
  if (page1LoadCallbackEnabled) {
    page1LoadCallbackEnabled = false;
    dump('Got page1 load.\n');
    pageLoad();
  }
  else {
    dump('Ignoring page1 load.\n');
  }
}

var page1PopstateCallbackEnabled = false;
function page1Popstate()
{
  if (page1PopstateCallbackEnabled) {
    page1PopstateCallbackEnabled = false;
    dump('Got page1 popstate.\n');
    pageLoad();
  }
  else {
    dump('Ignoring page1 popstate.\n');
  }
}

var page1PageShowCallbackEnabled = false;
function page1PageShow()
{
  if (page1PageShowCallbackEnabled) {
    page1PageShowCallbackEnabled = false;
    dump('Got page1 pageshow.\n');
    pageLoad();
  }
  else {
    dump('Ignoring page1 pageshow.\n');
  }
}

var page2LoadCallbackEnabled = false;
function page2Load()
{
  if (page2LoadCallbackEnabled) {
    page2LoadCallbackEnabled = false;
    dump('Got page2 popstate.\n');
    pageLoad();
  }
  else {
    dump('Ignoring page2 popstate.\n');
  }
}

var page2PopstateCallbackEnabled = false;
function page2Popstate()
{
  if (page2PopstateCallbackEnabled) {
    page2PopstateCallbackEnabled = false;
    dump('Got page2 popstate.\n');
    pageLoad();
  }
  else {
    dump('Ignoring page2 popstate.\n');
  }
}

var page2PageShowCallbackEnabled = false;
function page2PageShow()
{
  if (page2PageShowCallbackEnabled) {
    page2PageShowCallbackEnabled = false;
    dump('Got page2 pageshow.\n');
    pageLoad();
  }
  else {
    dump('Ignoring page2 pageshow.\n');
  }
}

function dumpSHistory(theWindow)
{
  let sh = SpecialPowers.wrap(theWindow).QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor)
                    .getInterface(SpecialPowers.Ci.nsIWebNavigation)
                    .sessionHistory;
  if (!sh) {
    dump(" window has no shistory.\n");
    return;
  }

  dump(" count: " + sh.count + "\n");
  dump(" index: " + sh.index + "\n");
  dump(" requestedIndex: " + sh.requestedIndex + "\n");

  for (let i = 0; i < sh.count; i++) {
    let shentry = sh.getEntryAtIndex(i, false);
    dump(" " + i + ": " + shentry.URI.spec + '\n');
    shentry.QueryInterface(SpecialPowers.Ci.nsISHContainer);
    for (let j = 0; j < shentry.childCount; j++) {
      let child = shentry.GetChildAt(j);
      dump("   child " + j + ": " + child.URI.spec + '\n');
    }
  }

  return sh;
}

var popup = window.open('file_bug590573_1.html');

var gTestContinuation = null;
var loads = 0;
function pageLoad()
{
  loads++;
  dump('pageLoad(loads=' + loads + ', page location=' + popup.location + ')\n');

  dumpSHistory(window);

  if (!gTestContinuation) {
    gTestContinuation = testBody();
  }
  var ret = gTestContinuation.next();
  if (ret.done) {
    SimpleTest.finish();
  }
}

function* testBody()
{
  is(popup.scrollY, 0, "test 1");
  popup.scroll(0, 100);

  popup.history.pushState('', '', '?pushed');
  is(Math.round(popup.scrollY), 100, "test 2");
  popup.scroll(0, 200); // set state-2's position to 200

  popup.history.back();
  is(Math.round(popup.scrollY), 100, "test 3");
  popup.scroll(0, 150); // set original page's position to 150

  popup.history.forward();
  is(Math.round(popup.scrollY), 200, "test 4");

  popup.history.back();
  is(Math.round(popup.scrollY), 150, "test 5");

  popup.history.forward();
  is(Math.round(popup.scrollY), 200, "test 6");

  // At this point, the history looks like:
  //   PATH                         POSITION
  //   file_bug590573_1.html        150       <-- oldest
  //   file_bug590573_1.html?pushed 200       <-- newest, current

  // Now test that the scroll position is persisted when we have real
  // navigations involved.  First, we need to spin the event loop so that the
  // navigation doesn't replace our current history entry.

  setTimeout(pageLoad, 0);
  yield;

  page2LoadCallbackEnabled = true;
  popup.location = 'file_bug590573_2.html';
  yield;

  ok(popup.location.href.match('file_bug590573_2.html$'),
     "Location was " + popup.location +
     " but should end with file_bug590573_2.html");

  is(popup.scrollY, 0, "test 7");
  popup.scroll(0, 300);

  // We need to spin the event loop again before we go back, otherwise the
  // scroll positions don't get updated properly.
  setTimeout(pageLoad, 0);
  yield;

  page1PageShowCallbackEnabled = true;
  popup.history.back();
  yield;

  // Spin the event loop again so that we get the right scroll positions.
  setTimeout(pageLoad, 0);
  yield;

  is(popup.location.search, "?pushed");
  ok(popup.document.getElementById('div1'), 'page should have div1.');

  is(Math.round(popup.scrollY), 200, "test 8");

  popup.history.back();
  is(Math.round(popup.scrollY), 150, "test 9");
  popup.history.forward();

  is(Math.round(popup.scrollY), 200, "test 10");

  // Spin one last time...
  setTimeout(pageLoad, 0);
  yield;

  page2PageShowCallbackEnabled = true;
  popup.history.forward();
  yield;

  // Bug 821821, on Android tegras we get 299 instead of 300 sometimes
  if (popup.scrollY >= 299 && popup.scrollY <= 300) {
    is(1, 1, "test 11");
  } else {
    is(1, 0, "test 11, got " + popup.scrollY + " for popup.scrollY instead of 299|300");
  }
  popup.close();
}
</script>

</body>
</html>