summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/indexeddb.https.html
blob: be9be4968f7b2d0cbc1f18f88b06b7acb6ad096f (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
<!DOCTYPE html>
<title>Service Worker: Indexed DB</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
function readDB() {
  return new Promise(function(resolve, reject) {
      var openRequest = indexedDB.open('db');

      openRequest.onerror = reject;
      openRequest.onsuccess = function() {
          var db = openRequest.result;
          var tx = db.transaction('store');
          var store = tx.objectStore('store');
          var getRequest = store.get('key');

          getRequest.onerror = function() {
              db.close();
              reject(getRequest.error);
            };
          getRequest.onsuccess = function() {
              db.close();
              resolve(getRequest.result);
            };
        };
    });
}

function send(worker, action) {
  return new Promise(function(resolve, reject) {
      var messageChannel = new MessageChannel();
      messageChannel.port1.onmessage = function(event) {
          if (event.data.type === 'error') {
            reject(event.data.reason);
          }

          resolve();
        };

      worker.postMessage(
        {action: action, port: messageChannel.port2},
        [messageChannel.port2]);
    });
}

promise_test(function(t) {
    var scope = 'resources/blank.html';

    return service_worker_unregister_and_register(
        t, 'resources/indexeddb-worker.js', scope)
      .then(function(registration) {
          var worker = registration.installing;

          promise_test(function() {
              return registration.unregister();
            }, 'clean up: registration');

          return send(worker, 'create')
            .then(function() {
                promise_test(function() {
                  return new Promise(function(resolve, reject) {
                        var delete_request = indexedDB.deleteDatabase('db');

                        delete_request.onsuccess = resolve;
                        delete_request.onerror = reject;
                      });
                    }, 'clean up: database');
              })
            .then(readDB)
            .then(function(value) {
                assert_equals(
                  value, 'value',
                  'The get() result should match what the worker put().');
              });
        });
  }, 'Verify Indexed DB operation in a Service Worker');
</script>