summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/unit/test_blocked_order.js
blob: 1c70853f4f00a383e39021cf2aaa40ccc08f2f7c (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
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

var testGenerator = testSteps();

function testSteps()
{
  const databaseName =
    ("window" in this) ? window.location.pathname : "Test";
  const databaseCount = 10;

  // Test 1: Make sure basic versionchange events work and that they don't
  //         trigger blocked events.
  info("Opening " + databaseCount + " databases with version 1");

  let databases = [];

  for (let i = 0; i < databaseCount; i++) {
    let thisIndex = i;

    info("Opening database " + thisIndex);

    let request = indexedDB.open(databaseName, 1);
    request.onerror = errorHandler;
    request.onblocked = errorHandler;
    request.onsuccess = grabEventAndContinueHandler;

    let event = yield undefined;

    is(event.type, "success", "Got success event");

    let db = request.result;
    is(db.version, 1, "Got version 1");

    db.onversionchange = function(event) {
      info("Closing database " + thisIndex);
      db.close();

      databases.splice(databases.indexOf(db), 1);
    };

    databases.push(db);
  }

  is(databases.length, databaseCount, "Created all databases with version 1");

  info("Opening database with version 2");

  let request = indexedDB.open(databaseName, 2);
  request.onerror = errorHandler;
  request.onsuccess = grabEventAndContinueHandler;

  request.onblocked = function(event) {
    ok(false, "Should not receive a blocked event");
  };

  let event = yield undefined;

  is(event.type, "success", "Got success event");
  is(databases.length, 0, "All databases with version 1 were closed");

  let db = request.result;
  is(db.version, 2, "Got version 2");

  info("Deleting database with version 2");
  db.close();

  request = indexedDB.deleteDatabase(databaseName);
  request.onerror = errorHandler;
  request.onsuccess = grabEventAndContinueHandler;

  event = yield undefined;

  is(event.type, "success", "Got success event");

  // Test 2: Make sure blocked events aren't delivered until all versionchange
  //         events have been delivered.
  info("Opening " + databaseCount + " databases with version 1");

  for (let i = 0; i < databaseCount; i++) {
    let thisIndex = i;

    info("Opening database " + thisIndex);

    let request = indexedDB.open(databaseName, 1);
    request.onerror = errorHandler;
    request.onblocked = errorHandler;
    request.onsuccess = grabEventAndContinueHandler;

    let event = yield undefined;

    is(event.type, "success", "Got success event");

    let db = request.result;
    is(db.version, 1, "Got version 1");

    db.onversionchange = function(event) {
      if (thisIndex == (databaseCount - 1)) {
        info("Closing all databases with version 1");

        for (let j = 0; j < databases.length; j++) {
          databases[j].close();
        }

        databases = [];
        info("Done closing all databases with version 1");
      } else {
        info("Not closing database " + thisIndex);
      }
    };

    databases.push(db);
  }

  is(databases.length, databaseCount, "Created all databases with version 1");

  info("Opening database with version 2");

  request = indexedDB.open(databaseName, 2);
  request.onerror = errorHandler;
  request.onsuccess = grabEventAndContinueHandler;

  request.onblocked = function(event) {
    ok(false, "Should not receive a blocked event");
  };

  event = yield undefined;

  is(event.type, "success", "Got success event");
  is(databases.length, 0, "All databases with version 1 were closed");

  db = request.result;
  is(db.version, 2, "Got version 2");

  info("Deleting database with version 2");
  db.close();

  request = indexedDB.deleteDatabase(databaseName);
  request.onerror = errorHandler;
  request.onsuccess = grabEventAndContinueHandler;

  event = yield undefined;

  is(event.type, "success", "Got success event");

  // Test 3: A blocked database left in that state should not hang shutdown.
  info("Opening 1 database with version 1");

  request = indexedDB.open(databaseName, 1);
  request.onerror = errorHandler;
  request.onblocked = errorHandler;
  request.onsuccess = grabEventAndContinueHandler;

  event = yield undefined;

  is(event.type, "success", "Got success event");

  db = request.result;
  is(db.version, 1, "Got version 1");

  info("Opening database with version 2");

  request = indexedDB.open(databaseName, 2);
  request.onerror = function(e) {
    e.preventDefault();
  };
  request.onsuccess = errorHandler;

  request.onblocked = grabEventAndContinueHandler;

  event = yield undefined;
  ok(true, "Got blocked");
  // Just allow this to remain blocked ...

  finishTest();
  yield undefined;
}