more test WPTs

This commit is contained in:
Florian Dold 2021-02-16 15:07:38 +01:00
parent 579c9da58b
commit f4bce10cda
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
2 changed files with 187 additions and 0 deletions

View File

@ -0,0 +1,83 @@
import test from "ava";
import { BridgeIDBRequest } from "..";
import { EventTarget, IDBDatabase } from "../idbtypes";
import { createdb } from "./wptsupport";
// Bubbling and capturing of request events
test("WPT request_bubble-and-capture.htm", async (t) => {
await new Promise<void>((resolve, reject) => {
var events: any[] = [];
var open_rq = createdb(t);
open_rq.onupgradeneeded = function (e: any) {
var db = e.target.result;
var txn = e.target.transaction;
var store = db.createObjectStore("s");
var rq1 = store.add("", 1);
var rq2 = store.add("", 1);
db.onerror = function () {};
log_request(" db", db);
log_request("txn", txn);
log_request("rq1", rq1);
log_request("rq2", rq2);
// Don't let it get to abort
db.addEventListener(
"error",
function (e: any) {
e.preventDefault();
},
false,
);
};
open_rq.onsuccess = function (e) {
log("open_rq.success")(e);
t.deepEqual(
events,
[
"capture db.success",
"capture txn.success",
"capture rq1.success",
"bubble rq1.success",
"capture db.error: ConstraintError",
"capture txn.error: ConstraintError",
"capture rq2.error: ConstraintError",
"bubble rq2.error: ConstraintError",
"bubble txn.error: ConstraintError",
"bubble db.error: ConstraintError",
"open_rq.success",
],
"events",
);
resolve();
};
function log_request(type: any, obj: EventTarget) {
obj.addEventListener(
"success",
log("capture " + type + ".success"),
true,
);
obj.addEventListener(
"success",
log("bubble " + type + ".success"),
false,
);
obj.addEventListener("error", log("capture " + type + ".error"), true);
obj.addEventListener("error", log("bubble " + type + ".error"), false);
}
function log(msg: any) {
return function (e: any) {
if (e && e.target && e.target.error)
events.push(msg + ": " + e.target.error.name);
else events.push(msg);
};
}
});
t.pass();
});

View File

@ -0,0 +1,104 @@
import test from "ava";
import { createdb } from "./wptsupport";
// Transactions have a request queue
test("transaction-requestqueue.htm", async (t) => {
await new Promise<void>((resolve, reject) => {
var db: any;
let keys = { txn: [], txn2: [] };
let open_rq = createdb(t);
open_rq.onupgradeneeded = function (e: any) {
var i, os;
db = e.target.result;
for (i = 1; i < 6; i++) {
os = db.createObjectStore("os" + i, {
autoIncrement: true,
keyPath: "k",
});
os.add({ os: "os" + i });
os.put({ os: "os" + i, k: i });
os.add({ os: "os" + i });
}
};
open_rq.onsuccess = function (e) {
var txn = db.transaction(["os2", "os1", "os3", "os5"]);
txn.objectStore("os1").openCursor().onsuccess = reg("txn");
txn.objectStore("os3").openCursor().onsuccess = reg("txn");
txn.objectStore("os1").get(2).onsuccess = reg("txn");
txn.objectStore("os2").get(3).onsuccess = reg("txn");
var txn2 = db.transaction(["os4", "os3", "os1", "os5"]);
var os4 = txn2.objectStore("os4");
for (var i = 0; i < 3; i++) {
os4.openCursor().onsuccess = reg("txn2");
os4.get(5).onsuccess = reg("txn2");
os4.get(4).onsuccess = reg("txn2");
txn.objectStore("os2").get(1).onsuccess = reg("txn");
txn2.objectStore("os3").get(1).onsuccess = reg("txn2");
}
txn2.objectStore("os1").get(2).onsuccess = reg("txn2");
txn.objectStore("os1").openCursor(null, "prev").onsuccess = reg("txn");
os4.openCursor(null, "prev").onsuccess = reg("txn2");
txn.oncomplete = finish;
txn2.oncomplete = finish;
};
function reg(n: string) {
return function (e: any) {
var v = e.target.result;
if (v.value) v = v.value;
(keys as any)[n].push(v.os + ": " + v.k);
};
}
var finish_to_go = 2;
function finish() {
if (--finish_to_go) return;
t.deepEqual(
keys["txn"],
[
"os1: 1",
"os3: 1",
"os1: 2",
"os2: 3",
"os2: 1",
"os2: 1",
"os2: 1",
"os1: 2",
],
"transaction keys",
);
t.deepEqual(
keys["txn2"],
[
"os4: 1",
"os4: 5",
"os4: 4",
"os3: 1",
"os4: 1",
"os4: 5",
"os4: 4",
"os3: 1",
"os4: 1",
"os4: 5",
"os4: 4",
"os3: 1",
"os1: 2",
"os4: 5",
],
"transaction 2 keys",
);
resolve();
}
});
t.pass();
});