Andrew Top | 61a8495 | 2019-04-30 15:07:33 -0700 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <title>IDBCursor direction - object store with keyrange</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 direction is "prev", let found record be the last record in records which satisfy all of the following requirements'> |
| 7 | <link rel=assert title="If range is defined, the record's key is in range."> |
| 8 | <script src="/resources/testharness.js"></script> |
| 9 | <script src="/resources/testharnessreport.js"></script> |
| 10 | <script src="support.js"></script> |
| 11 | |
| 12 | <script> |
| 13 | var records = [ 1337, "Alice", "Bob", "Greg", "Åke", ["Anne"] ]; |
| 14 | var directions = ["next", "prev", "nextunique", "prevunique"]; |
| 15 | var tests = {}; |
| 16 | |
| 17 | directions.forEach(function(dir) { |
| 18 | tests[dir] = async_test(document.title + ' - ' + dir); |
| 19 | }); |
| 20 | |
| 21 | var open_rq = indexedDB.open("testdb-" + new Date().getTime() + Math.random()); |
| 22 | |
| 23 | open_rq.onupgradeneeded = function(e) { |
| 24 | var objStore = e.target.result.createObjectStore("test"); |
| 25 | |
| 26 | for (var i = 0; i < records.length; i++) |
| 27 | objStore.add(records[i], records[i]); |
| 28 | }; |
| 29 | |
| 30 | open_rq.onsuccess = function(e) { |
| 31 | var db = e.target.result; |
| 32 | db.onerror = fail_helper("db.onerror"); |
| 33 | |
| 34 | |
| 35 | // The tests |
| 36 | testdir('next', ['Alice', 'Bob', 'Greg']); |
| 37 | testdir('prev', ['Greg', 'Bob', 'Alice']); |
| 38 | testdir('nextunique', ['Alice', 'Bob', 'Greg']); |
| 39 | testdir('prevunique', ['Greg', 'Bob', 'Alice']); |
| 40 | |
| 41 | |
| 42 | // Test function |
| 43 | function testdir(dir, expect) { |
| 44 | var count = 0; |
| 45 | var t = tests[dir]; |
| 46 | var rq = db.transaction("test").objectStore("test").openCursor(IDBKeyRange.bound("AA", "ZZ"), dir); |
| 47 | rq.onsuccess = t.step_func(function(e) { |
| 48 | var cursor = e.target.result; |
| 49 | if (!cursor) { |
| 50 | assert_equals(count, expect.length, "cursor runs"); |
| 51 | t.done(); |
| 52 | } |
| 53 | assert_equals(cursor.value, expect[count], "cursor.value"); |
| 54 | count++; |
| 55 | cursor.continue(); |
| 56 | }); |
| 57 | rq.onerror = t.step_func(function(e) { |
| 58 | e.preventDefault(); |
| 59 | e.stopPropagation(); |
| 60 | assert_unreached("rq.onerror - " + e.message); |
| 61 | }); |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | // Fail handling |
| 66 | function fail_helper(name) { |
| 67 | return function() { |
| 68 | directions.forEach(function(dir) { |
| 69 | tests[dir].step(function() { assert_unreached(name); }); |
| 70 | }); |
| 71 | }; |
| 72 | } |
| 73 | open_rq.onblocked = fail_helper('open_rq.onblocked'); |
| 74 | open_rq.onerror = fail_helper('open_rq.onerror'); |
| 75 | </script> |
| 76 | |
| 77 | <div id=log> </div> |