1var DB_UPDATE_INTERVAL = 100;
2var SEND_XHR_INTERVAL = 100;
3var BACK_INTERVAL = 100;
4var CREATE_HEALTH_TABLE = 'CREATE TABLE IF NOT EXISTS health (key VARCHAR(16) PRIMARY KEY);';
5var UPDATE_DATA = 'REPLACE INTO health VALUES("health-check-key");';
6
7var db = openDatabaseWithSuffix('bug25710', '1.0', 'LayoutTest for bug 25710', 102400);
8var backIterations;
9var xhrFctIntervalId;
10var backFctIntervalId;
11var successCheckIntervalId;
12var dbFctIntervalId;
13var successes;
14var databaseUpdates;
15var stoppedIntervals;
16
17function stopIntervals()
18{
19    stoppedIntervals = true;
20    clearInterval(dbFctIntervalId);
21    clearInterval(xhrFctIntervalId);
22    clearInterval(backFctIntervalId);
23}
24
25function stopTest(message)
26{
27    if (!stoppedIntervals)
28        stopIntervals();
29
30    log(message);
31
32    if (window.layoutTestController)
33        layoutTestController.notifyDone();
34}
35
36function updateDatabase()
37{
38    databaseUpdates++;
39    db.transaction(function(transaction) {
40        transaction.executeSql(UPDATE_DATA, [], function() {}, errorHandler);
41    }, errorHandler, function() {
42        successes++;
43    });
44}
45
46function checkForSuccess()
47{
48    if (successes == databaseUpdates) {
49        stopTest('Test Complete, SUCCESS');
50        clearInterval(successCheckIntervalId);
51    }
52}
53
54function errorHandler(tx, error)
55{
56    log('DB error, code: ' + error.code + ', msg: ' + error.message);
57    stopTest('Test Complete, FAILED');
58}
59
60function sendXhr()
61{
62    xhr = new XMLHttpRequest();
63    xhr.open('GET', location.href, true);
64    xhr.send('');
65}
66
67function invokeBack()
68{
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);
78
79    }
80}
81
82function runTest()
83{
84    // Location changes need to happen outside the onload handler to generate history entries.
85    setTimeout(runTestsInner, 0);
86}
87
88function runTestsInner()
89{
90    backIterations = 10;
91    consecutiveFailures = 0;
92    successes = 0;
93    databaseUpdates = 0;
94    stoppedIntervals = false;
95
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);
100    }
101
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');
110
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);
117    });
118}
119