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

/* exported testGenerator */
var testGenerator = testSteps();

function* testSteps() {
  const dbName = this.window
    ? window.location.pathname
    : "test_objectStore_getAllKeys";
  const dbVersion = 1;
  const objectStoreName = "foo";
  const keyCount = 200;

  let request = indexedDB.open(dbName, dbVersion);
  request.onerror = errorHandler;
  request.onupgradeneeded = grabEventAndContinueHandler;
  request.onsuccess = unexpectedSuccessHandler;

  let event = yield undefined;

  info("Creating database");

  let db = event.target.result;
  let objectStore = db.createObjectStore(objectStoreName);
  for (let i = 0; i < keyCount; i++) {
    objectStore.add(true, i);
  }

  request.onupgradeneeded = unexpectedSuccessHandler;
  request.onsuccess = grabEventAndContinueHandler;

  event = yield undefined;

  db = event.target.result;
  objectStore = db.transaction(objectStoreName).objectStore(objectStoreName);

  info("Getting all keys");
  objectStore.getAllKeys().onsuccess = grabEventAndContinueHandler;
  event = yield undefined;

  ok(Array.isArray(event.target.result), "Got an array result");
  is(event.target.result.length, keyCount, "Got correct array length");

  let match = true;
  for (let i = 0; i < keyCount; i++) {
    if (event.target.result[i] != i) {
      match = false;
      break;
    }
  }
  ok(match, "Got correct keys");

  info("Getting all keys with key range");
  let keyRange = IDBKeyRange.bound(10, 20, false, true);
  objectStore.getAllKeys(keyRange).onsuccess = grabEventAndContinueHandler;
  event = yield undefined;

  ok(Array.isArray(event.target.result), "Got an array result");
  is(event.target.result.length, 10, "Got correct array length");

  match = true;
  for (let i = 10; i < 20; i++) {
    if (event.target.result[i - 10] != i) {
      match = false;
      break;
    }
  }
  ok(match, "Got correct keys");

  info("Getting all keys with unmatched key range");
  keyRange = IDBKeyRange.bound(10000, 200000);
  objectStore.getAllKeys(keyRange).onsuccess = grabEventAndContinueHandler;
  event = yield undefined;

  ok(Array.isArray(event.target.result), "Got an array result");
  is(event.target.result.length, 0, "Got correct array length");

  info("Getting all keys with limit");
  objectStore.getAllKeys(null, 5).onsuccess = grabEventAndContinueHandler;
  event = yield undefined;

  ok(Array.isArray(event.target.result), "Got an array result");
  is(event.target.result.length, 5, "Got correct array length");

  match = true;
  for (let i = 0; i < 5; i++) {
    if (event.target.result[i] != i) {
      match = false;
      break;
    }
  }
  ok(match, "Got correct keys");

  info("Getting all keys with key range and limit");
  keyRange = IDBKeyRange.bound(10, 20, false, true);
  objectStore.getAllKeys(keyRange, 5).onsuccess = grabEventAndContinueHandler;
  event = yield undefined;

  ok(Array.isArray(event.target.result), "Got an array result");
  is(event.target.result.length, 5, "Got correct array length");

  match = true;
  for (let i = 10; i < 15; i++) {
    if (event.target.result[i - 10] != i) {
      match = false;
      break;
    }
  }
  ok(match, "Got correct keys");

  info("Getting all keys with unmatched key range and limit");
  keyRange = IDBKeyRange.bound(10000, 200000);
  objectStore.getAllKeys(keyRange, 5).onsuccess = grabEventAndContinueHandler;
  event = yield undefined;

  ok(Array.isArray(event.target.result), "Got an array result");
  is(event.target.result.length, 0, "Got correct array length");

  finishTest();
}