summaryrefslogtreecommitdiffstats
path: root/LayoutTests/storage/hash-change-with-xhr.html
blob: 9cbf276c1feb5fd449ce7b3a839a89dc8c3a573c (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
<html> 
<head> 
<title>Hash Change with an Open XHR should not stop database transactions</title>

<script type="text/javascript"> 
 
var DB_UPDATE_INTERVAL = 100;
var SEND_XHR_INTERVAL = 100;
var BACK_INTERVAL = 100;
var CREATE_HEALTH_TABLE = 'CREATE TABLE IF NOT EXISTS health (key VARCHAR(16) PRIMARY KEY);';
var UPDATE_DATA = 'REPLACE INTO health VALUES("health-check-key");';
 
var db = window.openDatabase('bug25710', '1.0', 'LayoutTest for bug 25710', 102400);
var backIterations;
var msgDiv;
var xhrFctIntervalId;
var backFctIntervalId;
var successCheckIntervalId;
var dbFctIntervalId;
var successes;
var databaseUpdates;
var stoppedIntervals;
 
function log(msg)
{
    var newMsg = document.createElement('div');
    newMsg.innerText = msg;
    msgDiv.appendChild(newMsg);
}

function stopIntervals()
{
    stoppedIntervals = true;
    window.clearInterval(dbFctIntervalId);
    window.clearInterval(xhrFctIntervalId);
    window.clearInterval(backFctIntervalId);
}

function stopTest(message)
{
    if (!stoppedIntervals)
        stopIntervals();

    log(message);

    if (window.layoutTestController)
        layoutTestController.notifyDone();
}
    
function updateDatabase()
{
    databaseUpdates++;  
    db.transaction(function(transaction) {
        transaction.executeSql(UPDATE_DATA, [], function() {}, errorHandler);
    }, errorHandler, function() {
        successes++;
    });
}

function checkForSuccess()
{
    if (successes == databaseUpdates) {
        stopTest('Test Complete, SUCCESS');
        window.clearInterval(successCheckIntervalId);
    }
}
 
function errorHandler(tx, error)
{
    log('DB error, code: ' + error.code + ', msg: ' + error.message);
    stopTest('Test Complete, FAILED');
}
 
function sendXhr()
{
    xhr = new XMLHttpRequest();
    xhr.open('GET', location.href, true);
    xhr.send('');
}
 
function invokeBack()
{
    backIterations--;
    if (backIterations) {
        history.back();
    } else {
        stopIntervals();
        // Allow a little time for all the database transactions to complete now we've stopped making them.
        successCheckIntervalId = window.setInterval(checkForSuccess, 250);
        // If we don't finish before this time, then we consider the test failed.
        window.setTimeout(function() { stopTest('Timed out waiting for transactions to complete. FAILED'); }, 20000);
        
    }
}

function runTest()
{
    if (window.layoutTestController) {
        layoutTestController.dumpAsText();
        layoutTestController.waitUntilDone();
    }
  
    msgDiv = document.getElementById('msgs');

    msgDiv.innerHTML = '';
    backIterations = 10;
    consecutiveFailures = 0;
    successes = 0;
    databaseUpdates = 0;
    stoppedIntervals = false;

    // Create some hashes so we can call history.back().
    log('Changing the hash to create history entries.');
    for (var i = 0; i < backIterations; i++) {
        location.hash = i;
    }

    // Init the database.
    db.transaction(function(transaction) {
        transaction.executeSql(CREATE_HEALTH_TABLE, [], function() {}, errorHandler);
    }, errorHandler, function() {
        // Give a little for the database to 'warm up' before making xhr requests
        // and calling history.back().
        window.setTimeout(function() {
            log('Db is warmed up');

            // NOTE: If we don't make any xhr requests, then the test
            // successfully passes (comment this line out).
            xhrFctIntervalId = window.setInterval(sendXhr, SEND_XHR_INTERVAL);
            backFctIntervalId = window.setInterval(invokeBack, BACK_INTERVAL);
            dbFctIntervalId = window.setInterval(updateDatabase, DB_UPDATE_INTERVAL);
        }, 500);
    });
}
</script> 
</head> 
<body onload="runTest()"> 
<div id="msgs"></div> 
</body> 
</html>