summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/idbrequest-onupgradeneeded.htm
blob: c156dba619abb15f9d2c9c45cb7a3cf34863dce2 (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
<!doctype html>
<meta charset=utf-8>
<title>IndexedDB: UpgradeNeeded Tests</title>
<meta name="help" href="https://w3c.github.io/IndexedDB/#commit-transaction">
<meta name="help" href="https://w3c.github.io/IndexedDB/#dom-idbfactory-open">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

function upgradeneeded_test(upgrade_func, success_func, error_func, description) {
  async_test(function(t) {
    var dbName = 'db' + self.location.pathname + '-' + description;
    var delete_request = indexedDB.deleteDatabase(dbName);
    delete_request.onerror = t.unreached_func('deleteDatabase should not fail');
    delete_request.onsuccess = t.step_func(function() {
      var open_request = indexedDB.open(dbName);

      open_request.onupgradeneeded = t.step_func(function() {
        t.add_cleanup(function() {
          if (open_request.result) {
            open_request.result.close(),
            indexedDB.deleteDatabase(dbName);
          }
        });
        upgrade_func(t, open_request);
      });
      open_request.onsuccess = t.step_func(function() {
        success_func(t, open_request);
      });
      if (error_func) {
        open_request.onerror = function() { error_func(t, open_request); };
      } else {
        open_request.onerror = t.unreached_func('open failed');
      }
    });
  }, description);
}

(function() {
  var order = [];
  upgradeneeded_test(
    function upgrade(t, request) {
      order.push('Upgrade');
      var db = request.result;
      var deleteRequest = indexedDB.deleteDatabase(db.name);
      deleteRequest.onsuccess = t.step_func(function() {
        assert_array_equals(order, ['Upgrade', 'Open Success']);
        t.done();
      });
      deleteRequest.onerror = t.unreached_func('delete failed');
    },
    function success(t, request) {
      var db = request.result;
      db.close();
      order.push('Open Success');
    },
    null,
    'indexedDB.delete called from upgradeneeded handler'
  );
}());

(function() {
  var order = [];
  upgradeneeded_test(
    function upgrade(t, request) {
      order.push('Upgrade');
      request.transaction.abort();
      order.push('Upgrade Transaction Aborted');
      var db = request.result;
      var deleteRequest = indexedDB.deleteDatabase(db.name);
      deleteRequest.onsuccess = t.step_func(function() {
        assert_array_equals(
            order, ['Upgrade', 'Upgrade Transaction Aborted', 'Open Error']);
        t.done();
      });
      deleteRequest.onerror = t.unreached_func('delete failed');
    },
    function success(t, request) {
      t.unreached_func('open should not succeed');
    },
    function error_func(t, request) {
      assert_array_equals(order, ['Upgrade', 'Upgrade Transaction Aborted']);
      order.push('Open Error');
    },
    'Abort transaction before deleting database in upgradeneeded handler'
  );
}());

(function() {
  var order = [];
  upgradeneeded_test(
    function upgrade(t, request) {
      order.push('Upgrade');
      var db = request.result;
      var deleteRequest = indexedDB.deleteDatabase(db.name);
      request.transaction.abort();
      order.push('Upgrade Transaction Aborted');
      deleteRequest.onsuccess = t.step_func(function() {
        assert_array_equals(
            order, ['Upgrade', 'Upgrade Transaction Aborted', 'Open Error']);
        t.done();
      });
      deleteRequest.onerror = t.unreached_func('delete failed');
    },
    function success(t, request) {
      t.unreached_func('open should not succeed');
    },
    function error_func(t, request) {
      assert_array_equals(order, ['Upgrade', 'Upgrade Transaction Aborted']);
      order.push('Open Error');
    },
    'Abort transaction after deleting database in upgradeneeded event handler'
  );
}());

(function() {
  var order = [];
  upgradeneeded_test(
    function upgrade(t, request) {
      order.push('Upgrade');
      var db = request.result;
      db.createObjectStore('store');
      request.transaction.oncomplete = t.step_func(function() {
        order.push('Upgrade transaction complete');
        var txn = db.transaction('store', 'readwrite', {durability: 'relaxed'});
        var store = txn.objectStore('store');
        store.put('value', 'key');
        txn.oncomplete = t.step_func(function() {
          assert_array_equals(
              order,
              ['Upgrade', 'Upgrade transaction complete', 'Open Success']);
          t.done();
        });
        txn.onerror = t.unreached_func('error on transaction');
        txn.onabort = t.unreached_func('aborting transaction');
      });
    },
    function success(t, request) {
      order.push('Open Success');
    },
    null,
    'transaction oncomplete ordering relative to open request onsuccess'
  );
}());

</script>