summaryrefslogtreecommitdiffstats
path: root/dom/base/test/chrome/window_groupedSHistory.xul
blob: aafa991ba40f081091c8da912e1785fa26e7bd61 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
                 type="text/css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1276553
-->
<window title="Mozilla Bug 1276553"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" onload="run();">

  <!-- test code goes here -->
  <script type="application/javascript">
  <![CDATA[

  const {interfaces: Ci, classes: Cc, results: Cr, utils: Cu} = Components;
  Cu.import("resource://testing-common/TestUtils.jsm");
  Cu.import("resource://testing-common/ContentTask.jsm");
  Cu.import("resource://testing-common/BrowserTestUtils.jsm");
  Cu.import("resource://gre/modules/Task.jsm");
  ContentTask.setTestScope(window.opener.wrappedJSObject);

  let imports = ['SimpleTest', 'SpecialPowers', 'ok', 'is', 'info'];
  for (let name of imports) {
    window[name] = window.opener.wrappedJSObject[name];
  }

  /** Test for Bug 1276553 **/
  function run() {
    new Promise(resolve => SpecialPowers.pushPrefEnv(
      {'set' : [[ 'browser.groupedhistory.enabled', true ]]}, resolve))
    .then(() => test(false))
    .then(() => test(true))
    .then(() => {
      window.close();
      SimpleTest.finish();
    });
  }

  function test(remote) {
    let act, bg1, bg2;
    return Promise.resolve()

    // create first browser with 1 entry (which will always be the active one)
    .then(() => info('TEST-INFO | test create active browser, remote=' + remote))
    .then(() => createBrowser('pen', remote))
    .then(b => act = b)
    .then(() => verifyBrowser(act, 'pen'       /* title */,
                                    0          /* index */,
                                    1          /* length */,
                                    false      /* canGoBack */,
                                    false      /* canGoForward */,
                                    false      /* partial */ ))

    // create background browser 1 with 1 entry
    .then(() => info('TEST-INFO | test create background browser 1, remote=' + remote))
    .then(() => createBrowser('pineapple', remote))
    .then(b => bg1 = b)
    .then(() => verifyBrowser(bg1, 'pineapple' /* title */,
                                    0          /* index */,
                                    1          /* length */,
                                    false      /* canGoBack */,
                                    false      /* canGoForward */,
                                    false      /* partial */ ))

     // create background browser 2 with 2 entries
    .then(() => info('TEST-INFO | test create background browser 2, remote=' + remote))
    .then(() => createBrowser('apple', remote))
    .then(b => bg2 = b)
    .then(() => verifyBrowser(bg2, 'apple'     /* title */,
                                    0          /* index */,
                                    1          /* length */,
                                    false      /* canGoBack */,
                                    false      /* canGoForward */,
                                    false      /* partial */ ))
    .then(() => loadURI(bg2, getDummyHtml('pencil')))
    .then(() => verifyBrowser(bg2, 'pencil'    /* title */,
                                    1          /* index */,
                                    2          /* length */,
                                    true       /* canGoBack */,
                                    false      /* canGoForward */,
                                    false      /* partial */ ))

    // merge to 2 entries pen-pineapple
    .then(() => info('TEST-INFO | test merge history, remote=' + remote))
    .then(() => mergeHistory(act, bg1))
    .then(() => verifyBrowser(act, 'pineapple' /* title */,
                                    0          /* index */,
                                    1          /* length */,
                                    true       /* canGoBack */,
                                    false      /* canGoForward */,
                                    true       /* partial */,
                                    1          /* offset */,
                                    2          /* globalLength */ ))

    // merge to 4 entries pen-pineapple-apple-pencil
    .then(() => mergeHistory(act, bg2))
    .then(() => verifyBrowser(act, 'pencil'    /* title */,
                                    1          /* index */,
                                    2          /* length */,
                                    true       /* canGoBack */,
                                    false      /* canGoForward */,
                                    true       /* partial */,
                                    2          /* offset */,
                                    4          /* globalLength */ ))

    // test go back
    .then(() => info('TEST-INFO | test history go back, remote=' + remote))
    .then(() => wrapHistoryNavFn(act, act.goBack.bind(act)))
    .then(() => verifyBrowser(act, 'apple'     /* title */,
                                    0          /* index */,
                                    2          /* length */,
                                    true       /* canGoBack */,
                                    true       /* canGoForward */,
                                    true       /* partial */,
                                    2          /* offset */,
                                    4          /* globalLength */ ))
    // XXX The 2nd pageshow comes from reload as current index of the active
    // partial history remains the same
    .then(() => wrapHistoryNavFn(act, act.goBack.bind(act), true))
    .then(() => verifyBrowser(act, 'pineapple' /* title */,
                                    0          /* index */,
                                    1          /* length */,
                                    true       /* canGoBack */,
                                    true       /* canGoForward */,
                                    true       /* partial */,
                                    1          /* offset */,
                                    4          /* globalLength */ ))
    .then(() => wrapHistoryNavFn(act, act.goBack.bind(act), true))
    .then(() => verifyBrowser(act, 'pen'       /* title */,
                                    0          /* index */,
                                    1          /* length */,
                                    false      /* canGoBack */,
                                    true       /* canGoForward */,
                                    true       /* partial */,
                                    0          /* offset */,
                                    4          /* globalLength */ ))

    // test go forward
    .then(() => info('TEST-INFO | test history go forward, remote=' + remote))
    .then(() => wrapHistoryNavFn(act, act.goForward.bind(act), true))
    .then(() => verifyBrowser(act, 'pineapple' /* title */,
                                    0          /* index */,
                                    1          /* length */,
                                    true       /* canGoBack */,
                                    true       /* canGoForward */,
                                    true       /* partial */,
                                    1          /* offset */,
                                    4          /* globalLength */ ))
    .then(() => wrapHistoryNavFn(act, act.goForward.bind(act), true))
    .then(() => verifyBrowser(act, 'apple'     /* title */,
                                    0          /* index */,
                                    2          /* length */,
                                    true       /* canGoBack */,
                                    true       /* canGoForward */,
                                    true       /* partial */,
                                    2          /* offset */,
                                    4          /* globalLength */ ))
    .then(() => wrapHistoryNavFn(act, act.goForward.bind(act)))
    .then(() => verifyBrowser(act, 'pencil'    /* title */,
                                    1          /* index */,
                                    2          /* length */,
                                    true       /* canGoBack */,
                                    false      /* canGoForward */,
                                    true       /* partial */,
                                    2          /* offset */,
                                    4          /* globalLength */ ))

    // test goto index
    .then(() => info('TEST-INFO | test history goto index, remote=' + remote))
    .then(() => wrapHistoryNavFn(act, act.gotoIndex.bind(act, 0), true))
    .then(() => verifyBrowser(act, 'pen'       /* title */,
                                    0          /* index */,
                                    1          /* length */,
                                    false      /* canGoBack */,
                                    true       /* canGoForward */,
                                    true       /* partial */,
                                    0          /* offset */,
                                    4          /* globalLength */ ))
    // expect 2 pageshow since we're also changing mIndex of the partial history
    .then(() => wrapHistoryNavFn(act, act.gotoIndex.bind(act, 2), true, 2))
    .then(() => verifyBrowser(act, 'apple'     /* title */,
                                    0          /* index */,
                                    2          /* length */,
                                    true       /* canGoBack */,
                                    true       /* canGoForward */,
                                    true       /* partial */,
                                    2          /* offset */,
                                    4          /* globalLength */ ))
    .then(() => wrapHistoryNavFn(act, act.gotoIndex.bind(act, 1), true))
    .then(() => verifyBrowser(act, 'pineapple' /* title */,
                                    0          /* index */,
                                    1          /* length */,
                                    true       /* canGoBack */,
                                    true       /* canGoForward */,
                                    true       /* partial */,
                                    1          /* offset */,
                                    4          /* globalLength */ ))
    // expect 2 pageshow since we're also changing mIndex of the partial history
    .then(() => wrapHistoryNavFn(act, act.gotoIndex.bind(act, 3), true, 2))
    .then(() => verifyBrowser(act, 'pencil'    /* title */,
                                    1          /* index */,
                                    2          /* length */,
                                    true       /* canGoBack */,
                                    false      /* canGoForward */,
                                    true       /* partial */,
                                    2          /* offset */,
                                    4          /* globalLength */ ))

    // test history change to 3 entries pen-pineapple-banana
    .then(() => info('TEST-INFO | test history change, remote=' + remote))
    .then(() => wrapHistoryNavFn(act, act.gotoIndex.bind(act, 1), true))
    .then(() => verifyBrowser(act, 'pineapple' /* title */,
                                    0          /* index */,
                                    1          /* length */,
                                    true       /* canGoBack */,
                                    true       /* canGoForward */,
                                    true       /* partial */,
                                    1          /* offset */,
                                    4          /* globalLength */ ))
    .then(() => loadURI(act, getDummyHtml('banana')))
    .then(() => verifyBrowser(act, 'banana'    /* title */,
                                    1          /* index */,
                                    2          /* length */,
                                    true       /* canGoBack */,
                                    false      /* canGoForward */,
                                    true       /* partial */,
                                    1          /* offset */,
                                    3          /* globalLength */ ))
  }

  function getDummyHtml(title) {
    return 'data:text/html;charset=UTF-8,' +
     '<html><head><title>' + title + '</title></head></html>'
  }

  function createBrowser(title, remote) {
    let browser = document.createElement('browser');
    browser.setAttribute('type', 'content');
    browser.setAttribute('remote', remote);
    browser.setAttribute('src', getDummyHtml(title));
    document.getElementById('stack').appendChild(browser);
    return BrowserTestUtils.browserLoaded(browser)
           .then(() => {
             browser.messageManager.loadFrameScript('data:,' +
               'addEventListener("pageshow", () => sendAsyncMessage("test:pageshow", null), false);' +
               'addEventListener("pagehide", () => sendAsyncMessage("test:pagehide", null), false);',
               true);
           })
           .then(() => {
             // a trick to ensure webProgress object is created for e10s case
             ok(browser.webProgress, 'check browser.webProgress exists');
             return browser;
           });
  }

  function loadURI(browser, uri) {
    let promise = BrowserTestUtils.browserLoaded(browser, false, uri);
    browser.loadURI(uri);
    return promise;
  }

  function mergeHistory(b1, b2) {
    let promises = [];
    let pagehide1, pagehide2;

    // For swapping there should be a pagehide followed by a pageshow.
    promises.push(BrowserTestUtils.waitForMessage(b1.messageManager, 'test:pagehide', msg => pagehide1 = true));
    promises.push(BrowserTestUtils.waitForMessage(b2.messageManager, 'test:pagehide', msg => pagehide2 = true));
    promises.push(BrowserTestUtils.waitForMessage(b1.messageManager, 'test:pageshow', msg => pagehide1));
    promises.push(BrowserTestUtils.waitForMessage(b2.messageManager, 'test:pageshow', msg => pagehide2));

    // For swapping remote browsers, we'll also receive Content:LocationChange
    if (b1.isRemoteBrowser) {
      promises.push(BrowserTestUtils.waitForMessage(b1.messageManager, 'Content:LocationChange'));
    }

    promises.push(Promise.resolve().then(() => {
      let f1 = b1.QueryInterface(Components.interfaces.nsIFrameLoaderOwner).frameLoader;
      let f2 = b2.QueryInterface(Components.interfaces.nsIFrameLoaderOwner).frameLoader;
      f1.appendPartialSessionHistoryAndSwap(f2);
    }));

    return Promise.all(promises);
  }

  function wrapHistoryNavFn(browser, navFn, expectSwap = false, expectPageshowCount = 1) {
    let promises = [];
    let pagehide = false;
    let pageshowCount = 0;

    if (expectSwap) {
      // For swapping there should be a pagehide followed by a pageshow.
      promises.push(BrowserTestUtils.waitForMessage(browser.messageManager,
        'test:pagehide', msg => pagehide = true));

      // For swapping remote browsers, we'll also receive Content:LocationChange
      if (browser.isRemoteBrowser) {
        promises.push(BrowserTestUtils.waitForMessage(browser.messageManager,
          'Content:LocationChange'));
      }
    }
    promises.push(BrowserTestUtils.waitForMessage(browser.messageManager,
      'test:pageshow', msg => {
        // Only count events after pagehide for swapping case.
        if (!expectSwap || pagehide) {
          return !--expectPageshowCount;
        }
        return false;
      }));
    promises.push(Task.spawn(navFn));

    return Promise.all(promises);
  }

  function verifyBrowser(browser, title, index, length, canGoBack, canGoForward,
                         partial, offset = 0, globalLength = length) {
    is(browser.canGoBack, canGoBack, 'check browser.canGoBack');
    is(browser.canGoForward, canGoForward, 'check browser.canGoForward');
    if (partial) {
      let frameLoader = browser.QueryInterface(Components.interfaces.nsIFrameLoaderOwner).frameLoader;
      is(frameLoader.groupedSessionHistory.count, globalLength, 'check groupedSHistory.count');
    }

    return ContentTask.spawn(browser,
                             { title, index, length, canGoBack, canGoForward, partial, offset, globalLength },
                             ({ title, index, length, canGoBack, canGoForward, partial, offset, globalLength }) => {
      let webNav = docShell.QueryInterface(Ci.nsIWebNavigation);
      let shistory = webNav.sessionHistory;
      is(content.document.title, title, 'check title');
      is(webNav.canGoBack, canGoBack, 'check webNav.canGoBack');
      is(webNav.canGoForward, canGoForward, 'check webNav.canGoForward');
      is(shistory.index, index, 'check shistory.index');
      is(shistory.count, length, 'check shistory.count');
      is(shistory.isPartial, partial, 'check shistory.isPartial');
      is(shistory.globalIndexOffset, offset, 'check shistory.globalIndexOffset');
      is(shistory.globalCount, globalLength, 'check shistory.globalCount');
    });
  }

  ]]>
  </script>
  <stack id="stack" flex="1" />
</window>