1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 var testDate
= new Date("February 24, 1955 12:00:00");
7 function getByDateSuccess()
9 debug('Data retrieved by date key');
11 shouldBe("event.target.result", "'foo'");
15 function recordNotFound()
17 debug('Removed data can no longer be found');
18 shouldBe("event.target.result", "undefined");
20 debug('Retrieving an index');
21 shouldBe("objectStore.index('fname_index').name", "'fname_index'");
23 debug('Removing an index');
25 objectStore
.deleteIndex('fname_index');
30 var request
= transaction
.objectStore('stuff').get(testDate
);
31 request
.onsuccess
= getByDateSuccess
;
32 request
.onerror
= unexpectedErrorCallback
;
35 function removeSuccess()
37 debug('Data removed');
39 var request
= objectStore
.get(1);
40 request
.onsuccess
= recordNotFound
;
41 request
.onerror
= unexpectedSuccessCallback
;
46 debug('Data retrieved');
48 shouldBe("event.target.result.fname", "'John'");
49 shouldBe("event.target.result.lname", "'Doe'");
50 shouldBe("event.target.result.id", "1");
52 var request
= objectStore
.delete(1);
53 request
.onsuccess
= removeSuccess
;
54 request
.onerror
= unexpectedErrorCallback
;
57 function moreDataAddedSuccess()
59 debug('More data added');
61 var request
= objectStore
.get(1);
62 request
.onsuccess
= getSuccess
;
63 request
.onerror
= unexpectedErrorCallback
;
66 function addWithSameKeyFailed()
68 debug('Adding a record with same key failed');
69 shouldBe("event.target.error.name", "'ConstraintError'");
70 event
.preventDefault();
72 var request
= transaction
.objectStore('stuff').add('foo', testDate
);
73 request
.onsuccess
= moreDataAddedSuccess
;
74 request
.onerror
= unexpectedErrorCallback
;
77 function dataAddedSuccess()
81 debug('Try to add employee with same id');
82 var request
= objectStore
.add({fname
: "Tom", lname
: "Jones", id
: 1});
83 request
.onsuccess
= unexpectedSuccessCallback
;
84 request
.onerror
= addWithSameKeyFailed
;
87 function populateObjectStore()
89 window
.transaction
= event
.target
.transaction
;
90 db
= event
.target
.result
;
91 transaction
.onabort
= unexpectedAbortCallback
;
92 debug('Populating object store');
93 deleteAllObjectStores(db
);
94 db
.createObjectStore('stuff');
95 window
.objectStore
= db
.createObjectStore('employees', {keyPath
: 'id'});
96 shouldBe("objectStore.name", "'employees'");
97 shouldBe("objectStore.keyPath", "'id'");
99 objectStore
.createIndex('fname_index', 'fname');
100 objectStore
.createIndex('lname_index', 'fname');
101 debug('Created indexes');
102 shouldBe("objectStore.indexNames[0]", "'fname_index'");
103 shouldBe("objectStore.indexNames[1]", "'lname_index'");
105 var request
= objectStore
.add({fname
: "John", lname
: "Doe", id
: 1});
106 request
.onsuccess
= dataAddedSuccess
;
107 request
.onerror
= unexpectedErrorCallback
;
111 indexedDBTest(populateObjectStore
);