]>
git.proxmox.com Git - mirror_novnc.git/blob - tests/assertions.js
1 // Assertions that make it easier to use sinon
2 import sinonChai
from '../node_modules/sinon-chai/lib/sinon-chai.js';
5 // noVNC specific assertions
6 chai
.use(function (_chai
, utils
) {
7 _chai
.Assertion
.addMethod('displayed', function (target_data
) {
9 const ctx
= obj
._target
.getContext('2d');
10 const data_cl
= ctx
.getImageData(0, 0, obj
._target
.width
, obj
._target
.height
).data
;
11 // NB(directxman12): PhantomJS 1.x doesn't implement Uint8ClampedArray, so work around that
12 const data
= new Uint8Array(data_cl
);
13 const len
= data_cl
.length
;
14 new chai
.Assertion(len
).to
.be
.equal(target_data
.length
, "unexpected display size");
16 for (let i
= 0; i
< len
; i
++) {
17 if (data
[i
] != target_data
[i
]) {
23 // eslint-disable-next-line no-console
24 console
.log("expected data: %o, actual data: %o", target_data
, data
);
27 "expected #{this} to have displayed the image #{exp}, but instead it displayed #{act}",
28 "expected #{this} not to have displayed the image #{act}",
33 _chai
.Assertion
.addMethod('sent', function (target_data
) {
34 const obj
= this._obj
;
36 const res
= { _websocket
: obj
._websocket
, rQi
: obj
._rQi
, _rQ
: new Uint8Array(obj
._rQ
.buffer
, 0, obj
._rQlen
),
37 _sQ
: new Uint8Array(obj
._sQ
.buffer
, 0, obj
._sQlen
) };
41 const data
= obj
._websocket
._get_sent_data();
43 if (data
.length
!= target_data
.length
) {
46 for (let i
= 0; i
< data
.length
; i
++) {
47 if (data
[i
] != target_data
[i
]) {
54 // eslint-disable-next-line no-console
55 console
.log("expected data: %o, actual data: %o", target_data
, data
);
58 "expected #{this} to have sent the data #{exp}, but it actually sent #{act}",
59 "expected #{this} not to have sent the data #{act}",
60 Array
.prototype.slice
.call(target_data
),
61 Array
.prototype.slice
.call(data
));
64 _chai
.Assertion
.addProperty('array', function () {
65 utils
.flag(this, 'array', true);
68 _chai
.Assertion
.overwriteMethod('equal', function (_super
) {
69 return function assertArrayEqual(target
) {
70 if (utils
.flag(this, 'array')) {
71 const obj
= this._obj
;
75 if (utils
.flag(this, 'deep')) {
76 for (let i
= 0; i
< obj
.length
; i
++) {
77 if (!utils
.eql(obj
[i
], target
[i
])) {
84 "expected #{this} to have elements deeply equal to #{exp}",
85 "expected #{this} not to have elements deeply equal to #{exp}",
86 Array
.prototype.slice
.call(target
));
88 for (let i
= 0; i
< obj
.length
; i
++) {
89 if (obj
[i
] != target
[i
]) {
96 "expected #{this} to have elements equal to #{exp}",
97 "expected #{this} not to have elements equal to #{exp}",
98 Array
.prototype.slice
.call(target
));
101 _super
.apply(this, arguments
);