File: test_callback_interfaces.js

package info (click to toggle)
firefox 145.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,653,528 kB
  • sloc: cpp: 7,594,999; javascript: 6,459,658; ansic: 3,752,909; python: 1,403,455; xml: 629,809; asm: 438,679; java: 186,421; sh: 67,287; makefile: 19,169; objc: 13,086; perl: 12,982; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (84 lines) | stat: -rw-r--r-- 2,465 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

const {
  invokeTestCallbackInterfaceNoop,
  invokeTestCallbackInterfaceGetValue,
  invokeTestCallbackInterfaceSetValue,
  TestCallbackInterface,
  UniffiSkipJsTypeCheck,
  UnitTestObjs,
} = ChromeUtils.importESModule(
  "moz-src:///toolkit/components/uniffi-bindgen-gecko-js/tests/generated/RustUniffiBindingsTests.sys.mjs"
);

/**
 *
 */
class Callback extends TestCallbackInterface {
  constructor(value) {
    super();
    this.value = value;
  }

  noop() {}

  getValue() {
    return this.value;
  }

  setValue(value) {
    this.value = value;
  }
}

// Construct a callback interface to pass to rust
const cbi = new Callback(42);
// Before we pass it to Rust `hasRegisteredCallbacks` should return fals
Assert.equal(
  UnitTestObjs.uniffiCallbackHandlerUniffiBindingsTestsTestCallbackInterface.hasRegisteredCallbacks(),
  false
);
// Test calling callback interface methods, which we can only do indirectly.
// Each of these Rust functions inputs a callback interface, calls a method on it, then returns the result.
invokeTestCallbackInterfaceNoop(cbi);
Assert.equal(invokeTestCallbackInterfaceGetValue(cbi), 42);
invokeTestCallbackInterfaceSetValue(cbi, 43);
Assert.equal(invokeTestCallbackInterfaceGetValue(cbi), 43);

// Test lowering failures when invoking the callback interfaces.
// The main test is that we don't leak a callback interface handle when doing this.
// Even though the Rust call doesn't go through, `hasRegisteredCallbacks` should still return false
// at the end of this test.

const invalidU32Value = 2 ** 48;
try {
  invokeTestCallbackInterfaceSetValue(cbi, invalidU32Value);
} catch {
  // Errors are expected
}

// Test a trickier case of lower failing.
// This one uses `UniffiSkipJsTypeCheck` to force the JS type checking to pass and make the failure
// happen in the C++ layer.
try {
  invokeTestCallbackInterfaceSetValue(
    cbi,
    new UniffiSkipJsTypeCheck(invalidU32Value)
  );
} catch {
  // Errors are expected
}

// eslint-disable-next-line no-delete-var
delete cbi;
// Wait a bit, then check that all callbacks have been cleaned up.
// Cleanup happens in a scheduled call, so wait a bit before checking
do_test_pending();
do_timeout(100, () => {
  Assert.equal(
    UnitTestObjs.uniffiCallbackHandlerUniffiBindingsTestsTestCallbackInterface.hasRegisteredCallbacks(),
    false
  );
  do_test_finished();
});