summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/resources/reading-autoincrement-common.js
blob: 45c8ffef923870909cb9b0e2af128081e5bce41a (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
// Returns the "name" property written to the object with the given ID.
function nameForId(id) {
  return `Object ${id}`;
}

// Initial database setup used by all the reading-autoincrement tests.
async function setupAutoincrementDatabase(testCase) {
  const database = await createDatabase(testCase, database => {
    const store = database.createObjectStore(
        'store', { autoIncrement: true, keyPath: 'id' });
    store.createIndex('by_name', 'name', { unique: true });
    store.createIndex('by_id', 'id', { unique: true });

    // Cover writing from the initial upgrade transaction.
    for (let i = 1; i <= 16; ++i) {
      if (i % 2 == 0) {
        store.put({name: nameForId(i), id: i});
      } else {
        store.put({name: nameForId(i)});
      }
    }
  });

  // Cover writing from a subsequent transaction.
  const transaction = database.transaction(['store'], 'readwrite');
  const store = transaction.objectStore('store');
  for (let i = 17; i <= 32; ++i) {
    if (i % 2 == 0) {
      store.put({name: nameForId(i), id: i});
    } else {
      store.put({name: nameForId(i)});
    }
  }
  await promiseForTransaction(testCase, transaction);

  return database;
}

// Returns the IDs used by the object store, sorted as strings.
//
// This is used to determine the correct order of records when retrieved from an
// index that uses stringified IDs.
function idsSortedByStringCompare() {
  const stringIds = [];
  for (let i = 1; i <= 32; ++i)
    stringIds.push(i);
  stringIds.sort((a, b) => indexedDB.cmp(`${a}`, `${b}`));
  return stringIds;
}

async function iterateCursor(testCase, cursorRequest, callback) {
  // This uses requestWatcher() directly instead of using promiseForRequest()
  // inside the loop to avoid creating multiple EventWatcher instances. In turn,
  // this avoids ending up with O(N) listeners for the request and O(N^2)
  // dispatched events.
  const eventWatcher = requestWatcher(testCase, cursorRequest);
  while (true) {
    const event = await eventWatcher.wait_for('success');
    const cursor = event.target.result;
    if (cursor === null)
      return;
    callback(cursor);
    cursor.continue();
  }
}

// Returns equivalent information to getAllKeys() by iterating a cursor.
//
// Returns an array with one dictionary per entry in the source. The dictionary
// has the properties "key" and "primaryKey".
async function getAllKeysViaCursor(testCase, cursorSource) {
  const results = [];
  await iterateCursor(testCase, cursorSource.openKeyCursor(), cursor => {
    results.push({ key: cursor.key, primaryKey: cursor.primaryKey });
  });
  return results;
}

// Returns equivalent information to getAll() by iterating a cursor.
//
// Returns an array with one dictionary per entry in the source. The dictionary
// has the properties "key", "primaryKey" and "value".
async function getAllViaCursor(testCase, cursorSource) {
  const results = [];
  await iterateCursor(testCase, cursorSource.openCursor(), cursor => {
    results.push({
      key: cursor.key,
      primaryKey: cursor.primaryKey,
      value: cursor.value,
    });
  });
  return results;
}