blob: 39afcb24d86d412ee673fa7f0f56aa9a2a50385f [file] [log] [blame]
Andrew Top61a84952019-04-30 15:07:33 -07001<!DOCTYPE html>
2<title>IDBCursor direction - index</title>
3<link rel="author" href="mailto:odinho@opera.com" title="Odin Hørthe Omdal">
4<link rel=help href="http://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html#cursor-iteration-operation">
5<link rel=assert title='If direction is "next", let found record be the first record in records which satisfy all of the following requirements'>
6<link rel=assert title="If position is defined, and source is an object store, the record's key is greater than position.">
7<link rel=assert title='If direction is "prev", let found record be the last record in records which satisfy all of the following requirements'>
8<link rel=assert title="If position is defined, and source is an object store, the record's key is less than position.">
9<link rel=assert title="Set cursor's position to found record's key. If source is an index, set cursor's object store position to found record's value.">
10<link rel=assert title="Set cursor's key to found record's key.">
11<script src="/resources/testharness.js"></script>
12<script src="/resources/testharnessreport.js"></script>
13<script src="support.js"></script>
14
15<script>
16 var records = [ "Alice", "Bob", "Bob", "Greg" ];
17 var directions = ["next", "prev", "nextunique", "prevunique"];
18 var tests = {};
19
20 directions.forEach(function(dir) {
21 tests[dir] = async_test(document.title + ' - ' + dir);
22 });
23
24 var open_rq = indexedDB.open("testdb-" + new Date().getTime() + Math.random());
25
26 open_rq.onupgradeneeded = function(e) {
27 var objStore = e.target.result.createObjectStore("test");
28 objStore.createIndex("idx", "name");
29
30 for (var i = 0; i < records.length; i++)
31 objStore.add({ name: records[i] }, i);
32 };
33
34 open_rq.onsuccess = function(e) {
35 var db = e.target.result;
36 db.onerror = fail_helper("db.onerror");
37
38
39 // The tests
40 testdir('next', ['Alice:0', 'Bob:1', 'Bob:2', 'Greg:3']);
41 testdir('prev', ['Greg:3', 'Bob:2', 'Bob:1', 'Alice:0']);
42 testdir('nextunique', ['Alice:0', 'Bob:1', 'Greg:3']);
43 testdir('prevunique', ['Greg:3', 'Bob:1', 'Alice:0']);
44
45
46 // Test function
47 function testdir(dir, expect) {
48 var count = 0;
49 var t = tests[dir];
50 var rq = db.transaction("test").objectStore("test").index("idx").openCursor(undefined, dir);
51 rq.onsuccess = t.step_func(function(e) {
52 var cursor = e.target.result;
53 if (!cursor) {
54 assert_equals(count, expect.length, "cursor runs");
55 t.done();
56 }
57 assert_equals(cursor.value.name + ":" + cursor.primaryKey, expect[count], "cursor.value");
58 count++;
59 cursor.continue();
60 });
61 rq.onerror = t.step_func(function(e) {
62 e.preventDefault();
63 e.stopPropagation();
64 assert_unreached("rq.onerror - " + e.message);
65 });
66 }
67 };
68
69 // Fail handling
70 function fail_helper(name) {
71 return function() {
72 directions.forEach(function(dir) {
73 tests[dir].step(function() { assert_unreached(name); });
74 });
75 };
76 }
77 open_rq.onblocked = fail_helper('open_rq.onblocked');
78 open_rq.onerror = fail_helper('open_rq.onerror');
79</script>
80
81<div id=log> </div>