OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <title>IndexedDB: IDBCursor continuePrimaryKey() method</title> | 2 <title>IndexedDB: IDBCursor continuePrimaryKey() method</title> |
3 <script src="../../resources/testharness.js"></script> | 3 <script src="../../resources/testharness.js"></script> |
4 <script src="../../resources/testharnessreport.js"></script> | 4 <script src="../../resources/testharnessreport.js"></script> |
5 <script> | 5 <script> |
6 | 6 |
7 async_test(function(t) { | 7 async_test(function(t) { |
8 var dbname = document.location + '-' + t.name; | 8 var dbname = document.location + '-' + t.name; |
9 var del = indexedDB.deleteDatabase(dbname); | 9 var del = indexedDB.deleteDatabase(dbname); |
10 del.onerror = t.unreached_func('deleteDatabase should succeed'); | 10 del.onerror = t.unreached_func('deleteDatabase should succeed'); |
11 var open = indexedDB.open(dbname); | 11 var open = indexedDB.open(dbname); |
12 open.onerror = t.unreached_func('open should succeed'); | 12 open.onerror = t.unreached_func('open should succeed'); |
13 | 13 |
14 open.onupgradeneeded = t.step_func(function() { | 14 open.onupgradeneeded = t.step_func(function() { |
15 var db = open.result; | 15 var db = open.result; |
16 var store = db.createObjectStore('store'); | 16 var store = db.createObjectStore('store'); |
17 store.put('a', 1).onerror = t.unreached_func('put should not fail'); | 17 store.put('a', 1).onerror = t.unreached_func('put should not fail'); |
18 var request = store.openCursor(); | 18 var request = store.openCursor(); |
19 request.onerror = t.unreached_func('openCursor should not fail'); | 19 request.onerror = t.unreached_func('openCursor should not fail'); |
20 request.onsuccess = function() { | 20 request.onsuccess = t.step_func(function() { |
21 var cursor = request.result; | 21 var cursor = request.result; |
22 assert_class_string(cursor, 'IDBCursorWithValue', | 22 assert_class_string(cursor, 'IDBCursorWithValue', |
23 'result should be a cursor'); | 23 'result should be a cursor'); |
24 | 24 |
25 assert_throws('InvalidAccessError', function() { | 25 assert_throws('InvalidAccessError', function() { |
26 cursor.continuePrimaryKey(2, 2); | 26 cursor.continuePrimaryKey(2, 2); |
27 }, 'continuePrimaryKey() should throw if source is not an index'); | 27 }, 'continuePrimaryKey() should throw if source is not an index'); |
28 }; | 28 }); |
29 }); | 29 }); |
30 | 30 |
31 open.onsuccess = t.step_func(function() { | 31 open.onsuccess = t.step_func(function() { |
32 var db = open.result; | 32 var db = open.result; |
33 db.close(); | 33 db.close(); |
34 t.done(); | 34 t.done(); |
35 }); | 35 }); |
36 | 36 |
37 }, 'IDBCursor continuePrimaryKey() on object store cursor'); | 37 }, 'IDBCursor continuePrimaryKey() on object store cursor'); |
38 | 38 |
| 39 [ |
| 40 { |
| 41 direction: 'nextunique', |
| 42 expected_key: 1, expected_primaryKey: 'a', |
| 43 continue_key: 2, continue_primaryKey: 'a' |
| 44 }, |
| 45 { |
| 46 direction: 'prevunique', |
| 47 expected_key: 3, expected_primaryKey: 'a', |
| 48 continue_key: 2, continue_primaryKey: 'a' |
| 49 } |
| 50 ].forEach(function(testcase) { |
| 51 async_test(function(t) { |
| 52 var dbname = document.location + '-' + t.name; |
| 53 var del = indexedDB.deleteDatabase(dbname); |
| 54 del.onerror = t.unreached_func('deleteDatabase should succeed'); |
| 55 var open = indexedDB.open(dbname); |
| 56 open.onerror = t.unreached_func('open should succeed'); |
| 57 |
| 58 open.onupgradeneeded = t.step_func(function() { |
| 59 var db = open.result; |
| 60 var store = db.createObjectStore('store', {keyPath: 'pk'}); |
| 61 var index = store.createIndex('index', 'ik', {multiEntry: true}); |
| 62 store.put({pk: 'a', ik: [1,2,3]}).onerror = |
| 63 t.unreached_func('put should not fail'); |
| 64 store.put({pk: 'b', ik: [1,2,3]}).onerror = |
| 65 t.unreached_func('put should not fail'); |
| 66 var request = index.openKeyCursor(null, testcase.direction); |
| 67 request.onerror = t.unreached_func('openCursor should not fail'); |
| 68 request.onsuccess = t.step_func(function() { |
| 69 var cursor = request.result; |
| 70 assert_class_string(cursor, 'IDBCursor', |
| 71 'result should be a cursor'); |
| 72 assert_equals(cursor.direction, testcase.direction, |
| 73 'direction should be as specified'); |
| 74 assert_equals(cursor.key, testcase.expected_key, |
| 75 'key should match'); |
| 76 assert_equals(cursor.primaryKey, testcase.expected_primaryKey, |
| 77 'primaryKey should match'); |
| 78 |
| 79 assert_throws('InvalidAccessError', function() { |
| 80 cursor.continuePrimaryKey( |
| 81 testcase.continue_key, |
| 82 testcase.continue_primaryKey); |
| 83 }, 'continuePrimaryKey() should throw if direction is unique'); |
| 84 }); |
| 85 }); |
| 86 |
| 87 open.onsuccess = t.step_func(function() { |
| 88 var db = open.result; |
| 89 db.close(); |
| 90 t.done(); |
| 91 }); |
| 92 |
| 93 }, 'IDBCursor continuePrimaryKey() on "' + testcase.direction + '" cursor'); |
| 94 }); |
| 95 |
39 </script> | 96 </script> |
OLD | NEW |