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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
/* global $, cockpit, QUnit, ArrayBuffer, Uint8Array */
QUnit.test("basic", function (assert) {
let done = assert.async();
assert.expect(4);
var channel = cockpit.channel({ "payload": "echo" });
var pass = 0;
$(channel).on("control", function(ev, options) {
if (pass === 0) {
assert.equal(options.command, "ready", "got ready");
pass += 1;
} else {
assert.equal(options.command, "done", "got done");
channel.close();
$(channel).off();
done();
}
});
$(channel).on("message", function(ev, payload) {
assert.strictEqual(payload, "the payload", "got the right payload");
channel.control({ command: "done" });
});
assert.strictEqual(channel.binary, false, "not binary");
channel.send("the payload");
});
QUnit.test("binary empty", function (assert) {
let done = assert.async();
assert.expect(2);
var channel = cockpit.channel({
"payload": "echo",
"binary": true
});
$(channel).on("message", function(ev, payload) {
if (window.Uint8Array)
assert.ok(payload instanceof window.Uint8Array, "got a byte array");
else
assert.ok($.isArray(payload), "got a byte array");
assert.strictEqual(payload.length, 0, "got the right payload");
$(channel).off();
done();
});
channel.send("");
});
QUnit.test("binary", function (assert) {
let done = assert.async();
assert.expect(3);
var channel = cockpit.channel({ "payload": "echo", "binary": true });
$(channel).on("message", function(ev, payload) {
if (window.Uint8Array)
assert.ok(payload instanceof window.Uint8Array, "got a byte array");
else
assert.ok($.isArray(payload), "got a byte array");
var array = [];
for (var i = 0; i < payload.length; i++)
array.push(payload[i]);
assert.deepEqual(array, [ 0, 1, 2, 3, 4, 5, 6, 7 ], "got back right data");
channel.close();
$(channel).off();
done();
});
var i, buffer;
if (window.ArrayBuffer) {
buffer = new ArrayBuffer(8);
var view = new Uint8Array(buffer);
for (i = 0; i < 8; i++)
view[i] = i;
} else {
buffer = new Array(8);
for (i = 0; i < 8; i++)
buffer[i] = i;
}
assert.strictEqual(channel.binary, true, "binary set");
channel.send(buffer);
});
QUnit.test("fence", function (assert) {
let done = assert.async();
assert.expect(2);
var before = cockpit.channel({ "payload": "echo" });
before.addEventListener("message", onMessage);
var fence = cockpit.channel({ "payload": "echo", "group": "fence" });
fence.addEventListener("message", onMessage);
var after = cockpit.channel({ "payload": "echo" });
after.addEventListener("message", onMessage);
var received = [ ];
function onMessage(ev, payload) {
received.push(payload);
if (received.length == 3) {
assert.deepEqual(received, [ "1", "2", "3", ], "got back before and fence data");
fence.close();
} else if (received.length == 5) {
assert.deepEqual(received, [ "1", "2", "3", "4", "5" ], "got back data in right order");
before.close();
after.close();
done();
}
}
/* We send messages in this order, but they should echoed in numeric order */
before.send("1");
after.send("4");
after.send("5");
fence.send("2");
fence.send("3");
});
QUnit.start();
|