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

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

function* testSteps() {
  const name = this.window ? window.location.pathname : "Splendid Test";
  const objectStoreInfo = [
    { name: "foo", options: { keyPath: "id" }, location: 1 },
    { name: "bar", options: { keyPath: "id" }, location: 0 },
  ];
  const indexInfo = [
    { name: "foo", keyPath: "value", location: 1 },
    { name: "bar", keyPath: "value", location: 0 },
  ];

  let request = indexedDB.open(name, 1);
  request.onerror = errorHandler;
  request.onupgradeneeded = grabEventAndContinueHandler;
  request.onsuccess = unexpectedSuccessHandler;
  let event = yield undefined;
  let db = event.target.result;

  for (let i = 0; i < objectStoreInfo.length; i++) {
    let info = objectStoreInfo[i];
    let objectStore = info.hasOwnProperty("options")
      ? db.createObjectStore(info.name, info.options)
      : db.createObjectStore(info.name);

    // Test index creation, and that it ends up in indexNames.
    for (let j = 0; j < indexInfo.length; j++) {
      let info = indexInfo[j];
      info.hasOwnProperty("options")
        ? objectStore.createIndex(info.name, info.keyPath, info.options)
        : objectStore.createIndex(info.name, info.keyPath);
    }
  }

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

  event = yield undefined;

  let objectStoreNames = [];
  for (let i = 0; i < objectStoreInfo.length; i++) {
    let info = objectStoreInfo[i];
    objectStoreNames.push(info.name);

    is(
      db.objectStoreNames[info.location],
      info.name,
      "Got objectStore name in the right location"
    );

    let trans = db.transaction(info.name);
    let objectStore = trans.objectStore(info.name);
    for (let j = 0; j < indexInfo.length; j++) {
      let info = indexInfo[j];
      is(
        objectStore.indexNames[info.location],
        info.name,
        "Got index name in the right location"
      );
    }
  }

  let trans = db.transaction(objectStoreNames);
  for (let i = 0; i < objectStoreInfo.length; i++) {
    let info = objectStoreInfo[i];

    is(
      trans.objectStoreNames[info.location],
      info.name,
      "Got objectStore name in the right location"
    );
  }

  db.close();

  request = indexedDB.open(name, 1);
  request.onerror = errorHandler;
  request.onsuccess = grabEventAndContinueHandler;
  request.onupgradeneeded = unexpectedSuccessHandler;
  event = yield undefined;

  db = event.target.result;

  objectStoreNames = [];
  for (let i = 0; i < objectStoreInfo.length; i++) {
    let info = objectStoreInfo[i];
    objectStoreNames.push(info.name);

    is(
      db.objectStoreNames[info.location],
      info.name,
      "Got objectStore name in the right location"
    );

    let trans = db.transaction(info.name);
    let objectStore = trans.objectStore(info.name);
    for (let j = 0; j < indexInfo.length; j++) {
      let info = indexInfo[j];
      is(
        objectStore.indexNames[info.location],
        info.name,
        "Got index name in the right location"
      );
    }
  }

  trans = db.transaction(objectStoreNames);
  for (let i = 0; i < objectStoreInfo.length; i++) {
    let info = objectStoreInfo[i];

    is(
      trans.objectStoreNames[info.location],
      info.name,
      "Got objectStore name in the right location"
    );
  }

  db.close();

  finishTest();
}