Skip to content

Commit

Permalink
IDB WPTs: Combine idbcursor_advance_index tests. Run them on workers
Browse files Browse the repository at this point in the history
This change combines various idbcursor_advance_index WPTs into a single
file and extends them to also run in dedicated, shared, and service
worker environments.

Bug: 41455766
Change-Id: I521221cb7d77aa95af2a74a7476d7c4a2a95394f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5581404
Reviewed-by: Evan Stade <estade@chromium.org>
Commit-Queue: Rahul Singh <rahsin@microsoft.com>
Cr-Commit-Position: refs/heads/main@{#1313697}
  • Loading branch information
rahulsingh-msft authored and chromium-wpt-export-bot committed Jun 12, 2024
1 parent 4692fbf commit 1b7e0b2
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 357 deletions.
258 changes: 258 additions & 0 deletions IndexedDB/idbcursor_advance_index.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
// META: global=window,worker
// META: title=IDBCursor.advance()
// META: script=resources/support.js
// @author Microsoft <https://www.microsoft.com>
// @author Odin Hørthe Omdal <mailto:odinho@opera.com>
// @author Intel <http://www.intel.com>

'use strict';

function createObjectStoreWithIndexAndPopulate(db, records) {
let objStore = db.createObjectStore("test", { keyPath: "pKey" });
objStore.createIndex("index", "iKey");
for (let i = 0; i < records.length; i++) {
objStore.add(records[i]);
}
return objStore;
}

function setOnUpgradeNeeded(dbObj, records) {
return function (event) {
dbObj.db = event.target.result;
createObjectStoreWithIndexAndPopulate(dbObj.db, records);
};
}

async_test(t => {
let dbObj = {};
let count = 0;
const records = [{ pKey: "primaryKey_0", iKey: "indexKey_0" },
{ pKey: "primaryKey_1", iKey: "indexKey_1" },
{ pKey: "primaryKey_2", iKey: "indexKey_2" },
{ pKey: "primaryKey_3", iKey: "indexKey_3" }
];

let open_rq = createdb(t);
open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);

open_rq.onsuccess = function (e) {
let cursor_rq = dbObj.db.transaction("test", "readonly", { durability: 'relaxed' })
.objectStore("test")
.index("index")
.openCursor();

cursor_rq.onsuccess = t.step_func(function (e) {
let cursor = e.target.result;
assert_true(cursor instanceof IDBCursor);

switch (count) {
case 0:
count += 3;
cursor.advance(3);
break;
case 3:
let record = cursor.value;
assert_equals(record.pKey, records[count].pKey, "record.pKey");
assert_equals(record.iKey, records[count].iKey, "record.iKey");
t.done();
break;
default:
assert_unreached("unexpected count");
break;
}
});
};
}, "index - iterate cursor number of times specified by count");

async_test(t => {
let dbObj = {};
const records = [
{ pKey: "primaryKey_0", iKey: "indexKey_0" },
{ pKey: "primaryKey_1", iKey: "indexKey_1" }
];

let open_rq = createdb(t);
open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);

open_rq.onsuccess = function (e) {
let cursor_rq = dbObj.db.transaction("test", "readonly", { durability: 'relaxed' })
.objectStore("test")
.index("index")
.openCursor();

cursor_rq.onsuccess = t.step_func(function (e) {
let cursor = e.target.result;

assert_true(cursor != null, "cursor exist");
assert_throws_js(TypeError,
function () { cursor.advance(-1); });

t.done();
});
};
}, "attempt to pass a count parameter that is not a number");

async_test(t => {
let dbObj = {};
const records = [
{ pKey: "primaryKey_0", iKey: "indexKey_0" },
{ pKey: "primaryKey_1", iKey: "indexKey_1" }
];

let open_rq = createdb(t);
open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);

open_rq.onsuccess = function (e) {
let cursor_rq = dbObj.db.transaction("test", "readonly", { durability: 'relaxed' })
.objectStore("test")
.index("index")
.openCursor(undefined, "next");

cursor_rq.onsuccess = t.step_func(function (e) {
let cursor = e.target.result;

assert_true(cursor != null, "cursor exist");
assert_throws_js(TypeError,
function () { cursor.advance(-1); });

t.done();
});
};
}, "index - attempt to advance backwards");

async_test(t => {
let dbObj = {};
let count = 0;
const records = [
{ pKey: "primaryKey_0", iKey: "indexKey_0" },
{ pKey: "primaryKey_1", iKey: "indexKey_1" },
{ pKey: "primaryKey_1-2", iKey: "indexKey_1" }
];
const expected = [
{ pKey: "primaryKey_0", iKey: "indexKey_0" },
{ pKey: "primaryKey_1-2", iKey: "indexKey_1" }
];

let open_rq = createdb(t);
open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);

open_rq.onsuccess = function (e) {
let cursor_rq = dbObj.db.transaction("test", "readonly", { durability: 'relaxed' })
.objectStore("test")
.index("index")
.openCursor();

cursor_rq.onsuccess = t.step_func(function (e) {
let cursor = e.target.result;
if (!cursor) {
assert_equals(count, expected.length, "cursor run count")
t.done()
}

let record = cursor.value;
assert_equals(record.pKey, expected[count].pKey, "primary key");
assert_equals(record.iKey, expected[count].iKey, "index key");

cursor.advance(2);
count++;
});
};
}, "index - iterate to the next record");

async_test(t => {
let db;
const records = [
{ pKey: "primaryKey_0", iKey: "indexKey_0" },
{ pKey: "primaryKey_1", iKey: "indexKey_1" }
];

let open_rq = createdb(t);
open_rq.onupgradeneeded = function (event) {
db = event.target.result;
let objStore = createObjectStoreWithIndexAndPopulate(db, records);
let rq = objStore.index("index").openCursor();
rq.onsuccess = t.step_func(function (event) {
let cursor = event.target.result;
assert_true(cursor instanceof IDBCursor);
assert_throws_js(TypeError,
function () { cursor.advance(0); });

t.done();
});
}
}, "Calling advance() with count argument 0 should throw TypeError.");

async_test(t => {
let db;
const records = [
{ pKey: "primaryKey_0", iKey: "indexKey_0" },
{ pKey: "primaryKey_1", iKey: "indexKey_1" }
];

let open_rq = createdb(t);
open_rq.onupgradeneeded = function (event) {
db = event.target.result;
let objStore = createObjectStoreWithIndexAndPopulate(db, records);
let rq = objStore.index("index").openCursor();
rq.onsuccess = t.step_func(function (event) {
let cursor = event.target.result;
assert_true(cursor instanceof IDBCursor);

event.target.transaction.abort();
assert_throws_dom("TransactionInactiveError",
function () { cursor.advance(1); });

t.done();
});
}
}, "Calling advance() should throws an exception TransactionInactiveError when the transaction is not active.");

async_test(t => {
let db;
const records = [
{ pKey: "primaryKey_0", iKey: "indexKey_0" },
{ pKey: "primaryKey_1", iKey: "indexKey_1" }
];

let open_rq = createdb(t);
open_rq.onupgradeneeded = function (event) {
db = event.target.result;
let objStore = createObjectStoreWithIndexAndPopulate(db, records);
let rq = objStore.index("index").openCursor();
rq.onsuccess = t.step_func(function (event) {
let cursor = event.target.result;
assert_true(cursor instanceof IDBCursor);

cursor.advance(1);
assert_throws_dom("InvalidStateError",
function () { cursor.advance(1); });

t.done();
});
}
}, "Calling advance() should throw DOMException when the cursor is currently being iterated.");

async_test(t => {
let db;
const records = [
{ pKey: "primaryKey_0", iKey: "indexKey_0" },
{ pKey: "primaryKey_1", iKey: "indexKey_1" }
];

let open_rq = createdb(t);
open_rq.onupgradeneeded = function (event) {
db = event.target.result;
let objStore = createObjectStoreWithIndexAndPopulate(db, records);
let rq = objStore.index("index").openCursor();
rq.onsuccess = t.step_func(function (event) {
let cursor = event.target.result;
assert_true(cursor instanceof IDBCursor, "cursor exist");

db.deleteObjectStore("test");
assert_throws_dom("InvalidStateError",
function () { cursor.advance(1); });

t.done();
});
}
}, "If the cursor's source or effective object store has been deleted, the implementation MUST throw a DOMException of type InvalidStateError");
57 changes: 0 additions & 57 deletions IndexedDB/idbcursor_advance_index.htm

This file was deleted.

47 changes: 0 additions & 47 deletions IndexedDB/idbcursor_advance_index2.htm

This file was deleted.

Loading

0 comments on commit 1b7e0b2

Please sign in to comment.