summaryrefslogtreecommitdiffstats
path: root/LayoutTests/storage/indexeddb/objectstore-autoincrement.html
blob: 0c0e9f598f0ba97b2b16a695d6b247dcf0bfb5e5 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<html>
<head>
<link rel="stylesheet" href="../../fast/js/resources/js-test-style.css">
<script src="../../fast/js/resources/js-test-pre.js"></script>
<script src="../../fast/js/resources/js-test-post-function.js"></script>
<script src="resources/shared.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>

description("Test IndexedDB's IDBObjectStore auto-increment feature.");
if (window.layoutTestController)
    layoutTestController.waitUntilDone();

function test()
{
    result = evalAndLog("webkitIndexedDB.open('Address Book')");
    verifyResult(result);
    result.onsuccess = openSuccess;
    result.onerror = unexpectedErrorCallback;
}

function openSuccess()
{
    debug("openSuccess():");
    verifySuccessEvent(event);
    window.db = evalAndLog("db = event.result");

    result = evalAndLog("db.setVersion('new version')");
    verifyResult(result);
    result.onsuccess = setVersionSuccess;
    result.onerror = unexpectedErrorCallback;
}

function setVersionSuccess()
{
    debug("setVersionSuccess():");
    verifySuccessEvent(event);
    window.trans = evalAndLog("trans = event.result");
    shouldBeTrue("trans !== null");
    trans.onabort = unexpectedAbortCallback;
    trans.oncomplete = setVersionCompleted;

    deleteAllObjectStores(db, createObjectStore);
}

function createObjectStore()
{
    debug("createObjectStore():");
    window.store = evalAndLog("store = db.createObjectStore('StoreWithKeyPath', {keyPath: 'id', autoIncrement: true})");
    evalAndLog("db.createObjectStore('StoreWithAutoIncrement', {autoIncrement: true})");
    evalAndLog("db.createObjectStore('PlainOldStore', {autoIncrement: false})");
    var storeNames = evalAndLog("storeNames = db.objectStoreNames");

    shouldBeEqualToString("store.name", "StoreWithKeyPath");
    shouldBe("store.keyPath", "'id'");
    shouldBe("storeNames.contains('StoreWithKeyPath')", "true");
    shouldBe("storeNames.contains('StoreWithAutoIncrement')", "true");
    shouldBe("storeNames.contains('PlainOldStore')", "true");
    shouldBe("storeNames.length", "3");

    // Let the setVersion transaction complete.
}

function setVersionCompleted()
{
    debug("setVersionCompleted():");

    window.trans = evalAndLog("trans = db.transaction([], webkitIDBTransaction.READ_WRITE)");
    trans.onabort = unexpectedAbortCallback;
    trans.oncomplete = done;

    window.store = evalAndLog("store = trans.objectStore('StoreWithKeyPath')");

    debug("Insert into object store with auto increment and key path, with key in the object.");
    result = evalAndLog("store.add({name: 'Jeffersson', number: '7010', id: 3})");
    result.onsuccess = addJefferssonSuccess;
    result.onerror = unexpectedErrorCallback;
}

function addJefferssonSuccess()
{
    debug("addJefferssonSuccess():");
    verifySuccessEvent(event);
    shouldBe("event.result", "3");

    debug("Insert into object store with auto increment and key path, without key in the object.");
    result = evalAndLog("store.add({name: 'Lincoln', number: '7012'})");
    result.onsuccess = unexpectedSuccessCallback;
    result.onerror = addLincolnError;
}

function addLincolnError()
{
    debug("addLincolnError():");
    verifyErrorEvent(event);
    // FIXME: This should be implemented, but we make it an error for now.
    shouldBe("event.code", "webkitIDBDatabaseException.UNKNOWN_ERR");

    evalAndLog("event.preventDefault()");

    window.store = evalAndLog("store = trans.objectStore('StoreWithAutoIncrement')");
    debug("Insert into object store with key gen using explicit key");
    result = evalAndLog("store.add({name: 'Lincoln', number: '7012'}, 5)");
    result.onsuccess = addLincolnSuccess;
}

function addLincolnSuccess()
{
    debug("addLincolnSuccess():");
    verifySuccessEvent(event);
    shouldBe("event.result", "5");

    result = evalAndLog("store.get(5)");
    result.onsuccess = getLincolnSuccess;
    result.onerror = unexpectedErrorCallback;
}

function getLincolnSuccess()
{
    debug("getLincolnSuccess():");
    verifySuccessEvent(event);
    shouldBeEqualToString("event.result.name", "Lincoln");
    shouldBeEqualToString("event.result.number", "7012");

    result = evalAndLog("store.put({name: 'Abraham', number: '2107'})");
    result.onsuccess = putAbrahamSuccess;
    result.onerror = unexpectedErrorCallback;
}

function putAbrahamSuccess()
{
    debug("putAbrahamSuccess():");
    verifySuccessEvent(event);
    shouldBe("event.result", "6");

    result = evalAndLog("store.get(6)");
    result.onsuccess = getAbrahamSuccess;
    result.onerror = unexpectedErrorCallback;
}

function getAbrahamSuccess()
{
    debug("getAbrahamSuccess():");
    verifySuccessEvent(event);
    shouldBeEqualToString("event.result.name", "Abraham");
    shouldBeEqualToString("event.result.number", "2107");

    window.store = evalAndLog("store = trans.objectStore('PlainOldStore')");
    debug("Try adding with no key to object store without auto increment.");
    result = evalAndLog("store.add({name: 'Adam'})");
    result.onsuccess = unexpectedSuccessCallback;
    result.onerror = addAdamError;
}

function addAdamError()
{
    debug("addAdamError():");
    verifyErrorEvent(event);
    shouldBe("event.code", "webkitIDBDatabaseException.DATA_ERR");

    evalAndLog("event.preventDefault()");

    result = evalAndLog("store.add({name: 'Adam'}, 1)");
    result.onsuccess = addAdamSuccess;
    result.onerror = unexpectedErrorCallback;
}

function addAdamSuccess()
{
    debug("addAdamSuccess():");
    verifySuccessEvent(event);
    shouldBe("event.result", "1");
}

test();

var successfullyParsed = true;

</script>
</body>
</html>