Debian Codegraph @

pkg/base1/test-dbus-framed.js

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 import cockpit from "cockpit";
3 import QUnit from "qunit-tests";
4
5 /* This is *NOT* a development guide, we take lots of shortcuts here */
6
7 /* Top level window */
8 function parent_window(assert) {
9 const done = assert.async();
10 assert.expect(4);
11 window.assert = assert; // for the child frame
12
13 document.getElementById("qunit-header").textContent = "Cockpit Parent Frame";
14 window.name = "cockpit1";
15 let initialized = false;
16 let frame;
17
18 cockpit.transport.filter((message, channel, control) => { // eslint-disable-line array-callback-return
19 if (initialized) {
20 /* Inject an unknown message that gets sent
21 * before the reply
22 */
23 const pos = message.indexOf('\n');
24 if (pos > -1) {
25 const json = JSON.parse(message.substring(pos));
26 if (json.reply) {
27 frame.postMessage(message.substring(0, pos) + '\n{"unknown":"unknown"}',
28 cockpit.transport.origin);
29 }
30 }
31 frame.postMessage(message, cockpit.transport.origin);
32 }
33 });
34
35 window.addEventListener("message", event => {
36 const message = event.data;
37 if (message.length === 0) {
38 done();
39 } else if (message.indexOf('"init"') !== -1) {
40 initialized = true;
41 frame.postMessage("\n" + JSON.stringify({
42 command: "init",
43 version: 1,
44 a: "b",
45 host: "localhost",
46 "channel-seed": cockpit.transport.options["channel-seed"] + "1!",
47 }), cockpit.transport.origin);
48 } else {
49 const ret = cockpit.transport.inject(message);
50 if (!ret) console.error("inject failed");
51 }
52 }, false);
53
54 /* This keeps coming up in tests ... how to open the transport */
55 const chan = cockpit.channel({ payload: "resource2" });
56 chan.addEventListener("close", () => {
57 assert.equal(cockpit.transport.host, "localhost", "parent cockpit.transport.host");
58 const iframe = document.createElement("iframe");
59 iframe.setAttribute("name", "cockpit1:blah");
60 iframe.setAttribute("src", window.location.href + "?sub");
61 document.body.appendChild(iframe);
62 frame = window.frames["cockpit1:blah"];
63 });
64 }
65
66 function child_frame() {
67 const assert = window.parent.assert;
68
69 const dbus = cockpit.dbus("com.redhat.Cockpit.DBusTests.Test", { bus: "session" });
70 const promise = dbus.call("/otree/frobber", "com.redhat.Cockpit.DBusTests.Frobber",
71 "HelloWorld", ["Browser-side JS"])
72 .then(reply => {
73 assert.equal(reply[0], "Word! You said `Browser-side JS'. I'm Skeleton, btw!", "reply");
74 dbus.close();
75 })
76 .always(() => {
77 assert.equal(promise.state(), "resolved", "finished successfully");
78 });
79 dbus.addEventListener("close", (ev, options) => {
80 assert.notOk(options.problem, "close cleanly");
81 cockpit.transport.close();
82 });
83 }
84
85 if (window.parent === window) {
86 QUnit.test("framed", parent_window);
87 QUnit.start();
88 } else {
89 child_frame();
90 }