Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / storage / websql / hash-change-with-xhr.js
blob4c9603b59fc4baf16197a969746b9532b3f07afd
1 var DB_UPDATE_INTERVAL = 100;
2 var SEND_XHR_INTERVAL = 100;
3 var BACK_INTERVAL = 100;
4 var CREATE_HEALTH_TABLE = 'CREATE TABLE IF NOT EXISTS health (key VARCHAR(16) PRIMARY KEY);';
5 var UPDATE_DATA = 'REPLACE INTO health VALUES("health-check-key");';
7 var db = openDatabaseWithSuffix('bug25710', '1.0', 'LayoutTest for bug 25710', 102400);
8 var backIterations;
9 var xhrFctIntervalId;
10 var backFctIntervalId;
11 var successCheckIntervalId;
12 var dbFctIntervalId;
13 var successes;
14 var databaseUpdates;
15 var stoppedIntervals;
17 function stopIntervals()
19 stoppedIntervals = true;
20 clearInterval(dbFctIntervalId);
21 clearInterval(xhrFctIntervalId);
22 clearInterval(backFctIntervalId);
25 function stopTest(message)
27 if (!stoppedIntervals)
28 stopIntervals();
30 log(message);
32 if (window.testRunner)
33 testRunner.notifyDone();
36 function updateDatabase()
38 databaseUpdates++;
39 db.transaction(function(transaction) {
40 transaction.executeSql(UPDATE_DATA, [], function() {}, errorHandler);
41 }, errorHandler, function() {
42 successes++;
43 });
46 function checkForSuccess()
48 if (successes == databaseUpdates) {
49 stopTest('Test Complete, SUCCESS');
50 clearInterval(successCheckIntervalId);
54 function errorHandler(tx, error)
56 log('DB error, code: ' + error.code + ', msg: ' + error.message);
57 stopTest('Test Complete, FAILED');
60 function sendXhr()
62 xhr = new XMLHttpRequest();
63 xhr.open('GET', location.href, true);
64 xhr.send('');
67 function invokeBack()
69 backIterations--;
70 if (backIterations) {
71 history.back();
72 } else {
73 stopIntervals();
74 // Allow a little time for all the database transactions to complete now we've stopped making them.
75 successCheckIntervalId = setInterval(checkForSuccess, 250);
76 // If we don't finish before this time, then we consider the test failed.
77 setTimeout(function() { stopTest('Timed out waiting for transactions to complete. FAILED'); }, 20000);
82 function runTest()
84 // Location changes need to happen outside the onload handler to generate history entries.
85 setTimeout(runTestsInner, 0);
88 function runTestsInner()
90 backIterations = 10;
91 consecutiveFailures = 0;
92 successes = 0;
93 databaseUpdates = 0;
94 stoppedIntervals = false;
96 // Create some hashes so we can call history.back().
97 log('Changing the hash to create history entries.');
98 for (var i = 0; i < backIterations; i++) {
99 setLocationHash(i);
102 // Init the database.
103 db.transaction(function(transaction) {
104 transaction.executeSql(CREATE_HEALTH_TABLE, [], function() {}, errorHandler);
105 }, errorHandler, function() {
106 // Give a little for the database to 'warm up' before making xhr requests
107 // and calling history.back().
108 setTimeout(function() {
109 log('Db is warmed up');
111 // NOTE: If we don't make any xhr requests, then the test
112 // successfully passes (comment this line out).
113 xhrFctIntervalId = setInterval(sendXhr, SEND_XHR_INTERVAL);
114 backFctIntervalId = setInterval(invokeBack, BACK_INTERVAL);
115 dbFctIntervalId = setInterval(updateDatabase, DB_UPDATE_INTERVAL);
116 }, 500);