diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-15 03:34:42 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-15 03:34:42 +0000 |
commit | da4c7e7ed675c3bf405668739c3012d140856109 (patch) | |
tree | cdd868dba063fecba609a1d819de271f0d51b23e /toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests | |
parent | Adding upstream version 125.0.3. (diff) | |
download | firefox-da4c7e7ed675c3bf405668739c3012d140856109.tar.xz firefox-da4c7e7ed675c3bf405668739c3012d140856109.zip |
Adding upstream version 126.0.upstream/126.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests')
8 files changed, 140 insertions, 53 deletions
diff --git a/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_external_types.js b/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_external_types.js index e2f985cde7..699dcfe6f3 100644 --- a/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_external_types.js +++ b/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_external_types.js @@ -6,10 +6,10 @@ const ExternalTypes = ChromeUtils.importESModule( ); add_task(async function () { - const line = new ExternalTypes.Line( - new ExternalTypes.Point(0, 0, "p1"), - new ExternalTypes.Point(2, 1, "p2") - ); + const line = new ExternalTypes.Line({ + start: await new ExternalTypes.Point({ coordX: 0, coordY: 0 }), + end: await new ExternalTypes.Point({ coordX: 2, coordY: 1 }), + }); Assert.equal(await ExternalTypes.gradient(line), 0.5); Assert.equal(await ExternalTypes.gradient(null), 0.0); diff --git a/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_geometry.js b/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_geometry.js index cdb552e4ef..1028cd37c2 100644 --- a/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_geometry.js +++ b/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_geometry.js @@ -6,24 +6,16 @@ const Geometry = ChromeUtils.importESModule( ); add_task(async function () { - const ln1 = new Geometry.Line( - new Geometry.Point({ coord_x: 0, coord_y: 0 }), - new Geometry.Point({ coord_x: 1, coord_y: 2 }) - ); - const ln2 = new Geometry.Line( - new Geometry.Point({ coord_x: 1, coord_y: 1 }), - new Geometry.Point({ coord_x: 2, coord_y: 2 }) - ); - const origin = new Geometry.Point({ coord_x: 0, coord_y: 0 }); - Assert.ok( - (await Geometry.intersection({ start: ln1, end: ln2 })).equals(origin) - ); - Assert.deepEqual( - await Geometry.intersection({ start: ln1, end: ln2 }), - origin - ); - Assert.strictEqual( - await Geometry.intersection({ start: ln1, end: ln1 }), - null - ); + const ln1 = new Geometry.Line({ + start: new Geometry.Point({ coordX: 0, coordY: 0 }), + end: new Geometry.Point({ coordX: 1, coordY: 2 }), + }); + const ln2 = new Geometry.Line({ + start: new Geometry.Point({ coordX: 1, coordY: 1 }), + end: new Geometry.Point({ coordX: 2, coordY: 2 }), + }); + const origin = new Geometry.Point({ coordX: 0, coordY: 0 }); + Assert.ok((await Geometry.intersection(ln1, ln2)).equals(origin)); + Assert.deepEqual(await Geometry.intersection(ln1, ln2), origin); + Assert.strictEqual(await Geometry.intersection(ln1, ln1), null); }); diff --git a/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_refcounts.js b/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_refcounts.js new file mode 100644 index 0000000000..790d981104 --- /dev/null +++ b/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_refcounts.js @@ -0,0 +1,57 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +const { getSingleton, getJsRefcount } = ChromeUtils.importESModule( + "resource://gre/modules/RustRefcounts.sys.mjs" +); + +// Test refcounts when we call methods. +// +// Each method call requires that we clone the Arc pointer on the JS side, then pass it to Rust +// which will consumer the reference. Make sure we get this right + +function createObjectAndCallMethods() { + const obj = getSingleton(); + obj.method(); +} + +add_test(() => { + // Create an object that we'll keep around. If the ref count ends up being low, we don't want + // to reduce it below 0, since the Rust code may catch that and clamp it + const obj = getSingleton(); + createObjectAndCallMethods(); + Cu.forceGC(); + Cu.forceCC(); + do_test_pending(); + do_timeout(500, () => { + Assert.equal(getJsRefcount(), 1); + // Use `obj` to avoid unused warnings and try to ensure that JS doesn't destroy it early + obj.method(); + do_test_finished(); + run_next_test(); + }); +}); + +// Test refcounts when creating/destroying objects +function createAndDeleteObjects() { + [getSingleton(), getSingleton(), getSingleton()]; +} + +add_test(() => { + const obj = getSingleton(); + createAndDeleteObjects(); + Cu.forceGC(); + Cu.forceCC(); + do_timeout(500, () => { + Assert.equal(getJsRefcount(), 1); + obj.method(); + do_test_finished(); + run_next_test(); + }); +}); + +// As we implement more UniFFI features we should probably add refcount tests for it. +// Some features that should probably have tests: +// - Async methods +// - UniFFI builtin trait methods like 'to_string' +// - Rust trait objects diff --git a/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_rondpoint.js b/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_rondpoint.js index 8c673b2e92..4ad76074f9 100644 --- a/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_rondpoint.js +++ b/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_rondpoint.js @@ -22,7 +22,12 @@ const { OptionneurDictionnaire, } = Rondpoint; add_task(async function () { - const dico = new Dictionnaire(Enumeration.DEUX, true, 0, 1235); + const dico = new Dictionnaire({ + un: Enumeration.DEUX, + deux: true, + petitNombre: 0, + grosNombre: 1235, + }); const copyDico = await copieDictionnaire(dico); Assert.deepEqual(dico, copyDico); @@ -127,13 +132,29 @@ add_task(async function () { ); await affirmAllerRetour( - [-1, 0, 1].map(n => new DictionnaireNombresSignes(n, n, n, n)), + [-1, 0, 1].map( + n => + new DictionnaireNombresSignes({ + petitNombre: n, + courtNombre: n, + nombreSimple: n, + grosNombre: n, + }) + ), rt.identiqueNombresSignes.bind(rt), (a, b) => Assert.deepEqual(a, b) ); await affirmAllerRetour( - [0, 1].map(n => new DictionnaireNombres(n, n, n, n)), + [0, 1].map( + n => + new DictionnaireNombres({ + petitNombre: n, + courtNombre: n, + nombreSimple: n, + grosNombre: n, + }) + ), rt.identiqueNombres.bind(rt), (a, b) => Assert.deepEqual(a, b) ); diff --git a/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_sprites.js b/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_sprites.js index 3feb2fd34d..e1f29cd9c8 100644 --- a/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_sprites.js +++ b/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_sprites.js @@ -9,20 +9,26 @@ add_task(async function () { Assert.ok(Sprites.Sprite); const sempty = await Sprites.Sprite.init(null); - Assert.deepEqual(await sempty.getPosition(), new Sprites.Point(0, 0)); + Assert.deepEqual( + await sempty.getPosition(), + new Sprites.Point({ x: 0, y: 0 }) + ); - const s = await Sprites.Sprite.init(new Sprites.Point(0, 1)); - Assert.deepEqual(await s.getPosition(), new Sprites.Point(0, 1)); + const s = await Sprites.Sprite.init(new Sprites.Point({ x: 0, y: 1 })); + Assert.deepEqual(await s.getPosition(), new Sprites.Point({ x: 0, y: 1 })); - s.moveTo(new Sprites.Point(1, 2)); - Assert.deepEqual(await s.getPosition(), new Sprites.Point(1, 2)); + s.moveTo(new Sprites.Point({ x: 1, y: 2 })); + Assert.deepEqual(await s.getPosition(), new Sprites.Point({ x: 1, y: 2 })); - s.moveBy(new Sprites.Vector(-4, 2)); - Assert.deepEqual(await s.getPosition(), new Sprites.Point(-3, 4)); + s.moveBy(new Sprites.Vector({ dx: -4, dy: 2 })); + Assert.deepEqual(await s.getPosition(), new Sprites.Point({ x: -3, y: 4 })); const srel = await Sprites.Sprite.newRelativeTo( - new Sprites.Point(0, 1), - new Sprites.Vector(1, 1.5) + new Sprites.Point({ x: 0, y: 1 }), + new Sprites.Vector({ dx: 1, dy: 1.5 }) + ); + Assert.deepEqual( + await srel.getPosition(), + new Sprites.Point({ x: 1, y: 2.5 }) ); - Assert.deepEqual(await srel.getPosition(), new Sprites.Point(1, 2.5)); }); diff --git a/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_todolist.js b/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_todolist.js index dac26d2be1..2cbd43304c 100644 --- a/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_todolist.js +++ b/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_todolist.js @@ -6,7 +6,9 @@ const { TodoList, TodoEntry, getDefaultList, setDefaultList } = add_task(async function () { const todo = await TodoList.init(); - const entry = new TodoEntry("Write bindings for strings in records"); + const entry = new TodoEntry({ + text: "Write bindings for strings in records", + }); await todo.addItem("Write JS bindings"); Assert.equal(await todo.getLast(), "Write JS bindings"); @@ -30,9 +32,9 @@ add_task(async function () { "Test Ünicode hàndling without an entry can't believe I didn't test this at first 🤣" ); - const entry2 = new TodoEntry( - "Test Ünicode hàndling in an entry can't believe I didn't test this at first 🤣" - ); + const entry2 = new TodoEntry({ + text: "Test Ünicode hàndling in an entry can't believe I didn't test this at first 🤣", + }); await todo.addEntry(entry2); Assert.equal( (await todo.getLastEntry()).text, diff --git a/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_type_checking.js b/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_type_checking.js index bfeb07c82b..7a5e04cea1 100644 --- a/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_type_checking.js +++ b/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/test_type_checking.js @@ -42,6 +42,12 @@ add_task(async function testObjectPointers() { /Bad pointer type/, "getEntries() with wrong pointer type" ); + + await Assert.rejects( + TodoList.setDefaultList(1), // expecting an object + /Object is not a 'TodoList' instance/, + "attempting to lift the wrong object type" + ); }); add_task(async function testEnumTypeCheck() { @@ -68,18 +74,6 @@ add_task(async function testRecordTypeCheck() { UniFFITypeError, "gradient with non-Line object should throw" ); - - await Assert.rejects( - Geometry.gradient({ - start: { - coordX: 0.0, - coordY: 0.0, - }, - // missing the end field - }), - /ln.end/, // Ensure exception message includes the argument name - "gradient with Line object with missing end field should throw" - ); }); add_task(async function testOptionTypeCheck() { diff --git a/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/xpcshell.toml b/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/xpcshell.toml index 76814ff199..801e95382f 100644 --- a/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/xpcshell.toml +++ b/toolkit/components/uniffi-bindgen-gecko-js/fixtures/tests/xpcshell/xpcshell.toml @@ -2,18 +2,33 @@ ["test_arithmetic.js"] +# I think this can be re-enabled when we implement https://bugzilla.mozilla.org/show_bug.cgi?id=1888668 +lineno = "3" + ["test_callbacks.js"] +disabled = "Temporarily disabled until we can re-enable callback support" +lineno = "8" ["test_custom_types.js"] +lineno = "12" ["test_external_types.js"] +lineno = "15" ["test_geometry.js"] +lineno = "18" + +["test_refcounts.js"] +lineno = "21" ["test_rondpoint.js"] +lineno = "24" ["test_sprites.js"] +lineno = "27" ["test_todolist.js"] +lineno = "30" ["test_type_checking.js"] +lineno = "33" |