summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/browsers/history/the-history-interface/002.html
blob: eb0c15aab896969b0d8f6561860bbf55c46301f3 (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
<!doctype html>
<html>
    <head>
        <title>history.replaceState tests</title>
        <script type="text/javascript" src="/resources/testharness.js"></script>
        <script type="text/javascript" src="/resources/testharnessreport.js"></script>
        <script type="text/javascript">
//does not test for firing of popstate onload, because this was dropped from the specification on 25 March 2011
//covers history.state after load, in accordance with the specification draft from 25 March 2011
//history.state before load is tested in 006 and 007
//does not test for structured cloning of FileList, File or Blob interfaces, as these require manual file selection

//**This test assumes that assignments to location.hash will be synchronous - this is how all browsers implement it.
//The spec (as of 25 March 2011) disagrees.

var histlength, atstep = 0, lasttimer;
setup({explicit_done:true}); //tests should take under 6 seconds + execution time

window.onload = function () {
    if( location.protocol == 'file:' ) {
        document.getElementsByTagName('p')[0].innerHTML = 'ERROR: This test cannot be run from file: (URL resolving will not work). It must be loaded over HTTP.';
        return;
    } else if( location.protocol == 'https:' ) {
        document.getElementsByTagName('p')[0].innerHTML += '<br>WARNING: Browsers may intentionally fail to update history.length when pages are loaded over HTTPS, as a privacy restriction. If possible, load this page over HTTP.';
    }
    //use a timeout, because some browsers intentionally do not add history entries for URL changes in the onload thread
    setTimeout(testinit,100);
};
function testinit() {
    atstep = 1;
    histlength = history.length;
    iframe = document.getElementsByTagName('iframe')[0].src = 'blank2.html';
    //reportload will now be called by the onload handler for the iframe
}
function reportload() {
    var iframe = document.getElementsByTagName('iframe')[0], hashchng = false;
    var canvassup = false, cloneobj;

    function tests1() {
        test(function () { assert_equals( history.length, histlength + 1, 'make sure that you loaded the test in a new tab/window' ); }, 'history.length should update when loading pages in an iframe');
        histlength = history.length;
        iframe.contentWindow.location.hash = 'test'; //should be synchronous **SEE COMMENT AT TOP OF FILE
        test(function () {
            assert_equals( history.length, histlength + 1, 'make sure that you loaded the test in a new tab/window' );
        }, 'history.length should update when setting location.hash');
        test(function () { assert_true( !!history.replaceState, 'critical test; ignore any failures after this' ); }, 'history.replaceState must exist'); //assert_exists does not allow prototype inheritance
        test(function () { assert_true( !!iframe.contentWindow.history.replaceState, 'critical test; ignore any failures after this' ); }, 'history.replaceState must exist within iframes');
        test(function () {
            assert_equals( iframe.contentWindow.history.state, null );
        }, 'initial history.state should be null');
        iframe.contentWindow.location.hash = 'test2';
        history.back();
        setTimeout(tests2,50); //.go is queued to end of thread
    }
    function tests2() {
        test(function () {
            histlength = history.length;
            iframe.contentWindow.history.replaceState('','');
            assert_equals( history.length, histlength );
        }, 'history.length should not update when replacing a state with no URL');
        test(function () {
            assert_equals( iframe.contentWindow.history.state, '' );
        }, 'history.state should update after a state is pushed');
        test(function () {
            assert_equals( iframe.contentWindow.location.hash.replace(/^#/,''), 'test' );
        }, 'hash should not change when replaceState is called without a URL');
        test(function () {
            histlength = history.length;
            iframe.contentWindow.history.replaceState('','','#test3');
            assert_equals( history.length, histlength );
        }, 'history.length should not update when replacing a state with a URL');
        test(function () {
            assert_equals( iframe.contentWindow.location.hash.replace(/^#/,''), 'test3' );
        }, 'hash should change when replaceState is called with a URL');
        history.go(-1);
        setTimeout(tests3,50); //.go is queued to end of thread
    }
    function tests3() {
        test(function () {
            assert_equals( iframe.contentWindow.location.hash.replace(/^#/,''), '' );
        }, 'replaceState must replace the existing state and not add an extra one');
        history.go(2);
        setTimeout(tests4,50); //.go is queued to end of thread
    }
    function tests4() {
        test(function () {
            assert_equals( iframe.contentWindow.location.hash.replace(/^#/,''), 'test2' );
        }, 'replaceState must replace the existing state without altering the forward history');
        test(function () {
            assert_throws('SECURITY_ERR',function () { history.replaceState('','','//exa mple'); });
        }, 'replaceState must not be allowed to create invalid URLs');
        test(function () {
            assert_throws('SECURITY_ERR',function () { history.replaceState('','','http://www.example.com/'); });
        }, 'replaceState must not be allowed to create cross-origin URLs');
        test(function () {
            assert_throws('SECURITY_ERR',function () { history.replaceState('','','about:blank'); });
        }, 'replaceState must not be allowed to create cross-origin URLs (about:blank)');
        test(function () {
            assert_throws('SECURITY_ERR',function () { history.replaceState('','','data:text/html,'); });
        }, 'replaceState must not be allowed to create cross-origin URLs (data:URI)');
        test(function () {
            assert_throws('SECURITY_ERR',function () { iframe.contentWindow.history.replaceState('','','http://www.example.com/'); },iframe.contentWindow);
        }, 'security errors are expected to be thrown in the context of the document that owns the history object');
        test(function () {
            //avoids browsers running .go synchronously when only a hash change is involved
            iframe.contentWindow.history.replaceState('','','/testing_ignore_me_404#test4');
            assert_equals( iframe.contentWindow.location.pathname, '/testing_ignore_me_404' );
        }, 'replaceState must be able to set location.pathname');
        test(function () {
            var newURL = location.href.replace(/\/[^\/]*$/)+'/testing_ignore_me_404/';
            iframe.contentWindow.history.replaceState('','',newURL);
            assert_equals( iframe.contentWindow.location.href, newURL );
        }, 'replaceState must be able to set absolute URLs to the same host');
        //begin setup for "[must not] remove any tasks queued by the history traversal task source"
        iframe.contentWindow.history.go(-1); //must be queued so the next command takes place *beforehand*
        try {
            //must not remove the queued navigation in the same browsing context
            iframe.contentWindow.history.replaceState('','',iframe.contentWindow.location.pathname+'#test5');
        } catch(unsuperr2) {}
        //allow the browser to run the .go
        setTimeout(tests5,50);
    }
    function tests5() {
        test(function () {
            assert_equals( iframe.contentWindow.location.hash.replace(/^#/,''), 'test3' );
        }, 'replaceState must not remove any tasks queued by the history traversal task source');
        //Safari 5.0.3 fails here - it navigates *this* document to the *iframe's* location, instead of just navigating the iframe
        history.go(1);
        setTimeout(tests6,50); //.go is queued to end of thread
    }
    function tests6() {
        test(function () {
            assert_equals( iframe.contentWindow.location.hash.replace(/^#/,''), 'test5' );
        }, '.go must queue a task with the history traversal task source (run asynchronously)');
        //end "[must not] remove any tasks queued by the history traversal task source"
        window.addEventListener('hashchange',function () { hashchng = true; },false);
        try {
            //push a state that changes the hash
            iframe.contentWindow.history.replaceState('','',iframe.contentWindow.location.pathname+'#test6');
        } catch(unsuperr) {}
        setTimeout(tests7,50); //allow the hashchange event to process, if the browser has mistakenly fired it
    }
    function tests7() {
        test(function () {
            assert_false( hashchng );
        }, 'replaceState must not fire hashchange events');
        test(function () {
            assert_throws( 'DATA_CLONE_ERR', function () {
                history.replaceState({dummy:function () {}},'');
            } );
        }, 'replaceState must not be able to use a function as data');
        test(function () {
            assert_throws( 'DATA_CLONE_ERR', function () {
                history.replaceState({dummy:window},'');
            } );
        }, 'replaceState must not be able to use a DOM node as data');
        test(function () {
            try { a.b = c; } catch(errdata) {
                assert_throws( 'DATA_CLONE_ERR', function () {
                    history.replaceState({dummy:errdata},'');
                } );
            }
        }, 'replaceState must not be able to use an error object as data');
        test(function () {
            assert_throws( 'DATA_CLONE_ERR', function () {
                iframe.contentWindow.history.replaceState(document,'');
            }, iframe.contentWindow );
        }, 'security errors are expected to be thrown in the context of the document that owns the history object (2)');
        cloneobj = {
            nulldata: null,
            udefdata: window.undefined,
            booldata: true,
            numdata: 1,
            strdata: 'string data',
            boolobj: new Boolean(true),
            numobj: new Number(1),
            strobj: new String('string data'),
            datedata: new Date(),
            regdata: /a/g,
            arrdata: [1]
        };
        cloneobj.regdata.lastIndex = 1;
        cloneobj.looped = cloneobj;
        //test the ImageData type, if the browser supports it
        var canvas = document.createElement('canvas');
        if( canvas.getContext && ( canvas = canvas.getContext('2d') ) && canvas.createImageData ) {
            canvassup = true;
            cloneobj.imgdata = canvas.createImageData(1,1);
        }
        test(function () {
            try {
                iframe.contentWindow.history.replaceState(cloneobj,'new title');
            } catch(e) {
                cloneobj.looped = null;
                //try again because this object is needed for future tests
                iframe.contentWindow.history.replaceState(cloneobj,'new title');
                //rethrow so the browser gets a FAIL for not coping with the circular reference; "internal structured cloning algorithm" step 1
                throw(e);
            }
        }, 'replaceState must be able to make structured clones of complex objects');
        test(function () {
            assert_equals( iframe.contentWindow.history.state && iframe.contentWindow.history.state.strdata, 'string data' );
        }, 'history.state should also reference a clone of the original object');
        test(function () {
            assert_false( cloneobj === iframe.contentWindow.history.state );
        }, 'history.state should be a clone of the original object, not a reference to it');
        history.go(-1);
        setTimeout(tests8,50); //.go is queued to end of thread
    }
    function tests8() {
        var eventtime = setTimeout(function () { tests9(false); },500); //should be cleared by the event handler long before it has a chance to fire
        iframe.contentWindow.addEventListener('popstate',function (e) { clearTimeout(eventtime); tests9(true,e); },false);
        history.forward();
    }
    function tests9(hasFired,ev) {
        test(function () {
            assert_true( hasFired );
        }, 'popstate event should fire when navigation occurs');
        test(function () {
            assert_true( !!ev && typeof(ev.state) != 'undefined', 'state information was not passed' );
            assert_true( !!ev.state, 'state information does not contain the expected value - browser is probably stuck in the wrong history position' );
            assert_equals( ev.state.nulldata, null, 'state null data was not correct' );
            assert_equals( ev.state.udefdata, window.undefined, 'state undefined data was not correct' );
            assert_true( ev.state.booldata, 'state boolean data was not correct' );
            assert_equals( ev.state.numdata, 1, 'state numeric data was not correct' );
            assert_equals( ev.state.strdata, 'string data', 'state string data was not correct' );
            assert_true( !!ev.state.datedata.getTime, 'state date data was not correct' );
            assert_exists( ev.state, 'regdata', 'state regex data was not correct' );
            assert_equals( ev.state.regdata.source, 'a', 'state regex pattern data was not correct' );
            assert_true( ev.state.regdata.global, 'state regex flag data was not correct' );
            assert_equals( ev.state.regdata.lastIndex, 0, 'state regex lastIndex data was not correct' );
            assert_equals( ev.state.arrdata.length, 1, 'state array data was not correct' );
            assert_true( ev.state.boolobj.valueOf(), 'state boolean data was not correct' );
            assert_equals( ev.state.numobj.valueOf(), 1, 'state numeric data was not correct' );
            assert_equals( ev.state.strobj.valueOf(), 'string data', 'state string data was not correct' );
            if( canvassup ) {
                assert_equals( ev.state.imgdata.width, 1, 'state ImageData was not correct' );
            }
        }, 'popstate event should pass the state data');
        test(function () {
            assert_equals( ev.state.looped, ev.state );
        }, 'state data should cope with circular object references');
        test(function () {
            assert_false( cloneobj === ev.state );
        }, 'state data should be a clone of the original object, not a reference to it');
        test(function () {
            assert_equals( iframe.contentWindow.history.state && iframe.contentWindow.history.state.strdata, 'string data' );
        }, 'history.state should also reference a clone of the original object (2)');
        test(function () {
            assert_false( cloneobj === iframe.contentWindow.history.state );
        }, 'history.state should be a clone of the original object, not a reference to it (2)');
        test(function () {
            assert_false( iframe.contentWindow.history.state === ev.state );
        }, 'history.state should be a separate clone of the object, not a reference to the object passed to the event handler');
        try {
            iframe.contentWindow.persistval = true;
            iframe.contentWindow.history.replaceState('','', location.href.replace(/\/[^\/]*$/,'/blank3.html') );
        } catch(unsuperr) {}
        //it's already cached, so this should be very fast if the browser mistakenly loads it
        //it should not need to load at all, since it's just a pushed state
        setTimeout(tests10,1000);
    }
    function tests10() {
        test(function () {
            assert_true( iframe.contentWindow.persistval && !iframe.contentWindow.forreal );
        }, 'replaceState should not actually load the new URL');
        atstep = 3;
        iframe.contentWindow.location.reload(); //load the real URL
        lasttimer = setTimeout(function () { tests11(false); },3000); //should be cleared by the onload handler long before it has a chance to fire
    }
    function tests11(passed) {
        test(function () {
            assert_true( passed, 'expected a load event to fire when reloading the URL from cache, gave up waiting after 3 seconds' );
        }, 'reloading a replaced state should actually load the new URL');
        //try to make browsers behave when reloading so that the correct URL is recovered - does not always work
        iframe.contentWindow.location.href = location.href.replace(/\/[^\/]*$/,'/blank.html');
        done();
    }

    if( atstep == 1 ) {
        //blank2 has loaded
        atstep = 2;
        //use a timeout, because some browsers intentionally do not add history entries for URL changes in an onload thread
        setTimeout(tests1,100);
    } else if( atstep == 3 ) {
        //blank3 should now have loaded after the .reload() command
        atstep = 4;
        clearTimeout(lasttimer);
        tests11(true);
    }
}



        </script>
    </head>
    <body>

        <noscript><p>Enable JavaScript and reload</p></noscript>
        <p>WARNING: This test should always be loaded in a new tab/window, to avoid browsers attempting to recover the state of frames, and history length. Do not reload the test.</p>
        <div id="log">Running test...</div>
        <p><iframe onload="reportload();" src="blank.html"></iframe></p>
        <p><iframe src="blank.html"></iframe></p>
        <p><iframe src="blank2.html"></iframe></p>
        <p><iframe src="blank3.html"></iframe></p>

    </body>
</html>