]> git.proxmox.com Git - mirror_novnc.git/commitdiff
Approximate comparison of JPEG result
authorPierre Ossman <ossman@cendio.se>
Fri, 4 Sep 2020 14:48:44 +0000 (16:48 +0200)
committerPierre Ossman <ossman@cendio.se>
Fri, 4 Sep 2020 14:48:44 +0000 (16:48 +0200)
The browsers have various rounding errors so we need to compare the
output data only approximately and not exactly.

tests/assertions.js
tests/test.tight.js

index 05957c083614bd44a6a5b0454fd8e7bcc5a0047b..bab932c5f3da638296965aa55545e977685f88b1 100644 (file)
@@ -1,6 +1,9 @@
 // noVNC specific assertions
 chai.use(function (_chai, utils) {
-    _chai.Assertion.addMethod('displayed', function (targetData) {
+    function _equal(a, b) {
+        return a === b;
+    }
+    _chai.Assertion.addMethod('displayed', function (targetData, cmp=_equal) {
         const obj = this._obj;
         const ctx = obj._target.getContext('2d');
         const dataCl = ctx.getImageData(0, 0, obj._target.width, obj._target.height).data;
@@ -10,7 +13,7 @@ chai.use(function (_chai, utils) {
         new chai.Assertion(len).to.be.equal(targetData.length, "unexpected display size");
         let same = true;
         for (let i = 0; i < len; i++) {
-            if (data[i] != targetData[i]) {
+            if (!cmp(data[i], targetData[i])) {
                 same = false;
                 break;
             }
index d89cf57e2d84f28761adf9ffe6ef5768db9cbf64..4aa47d70dbc3d66793ba7faf7835e5302c75d615 100644 (file)
@@ -365,17 +365,22 @@ describe('Tight Decoder', function () {
 
         testDecodeRect(decoder, 0, 0, 4, 4, data, display, 24);
 
-        // We got some rounding errors when we compressed things,
-        // hence not perfect 0xff/0x00 values
         let targetData = new Uint8Array([
-            0xfe, 0x00, 0x00, 255, 0xfe, 0x00, 0x00, 255, 0x00, 0xff, 0x01, 255, 0x00, 0xff, 0x01, 255,
-            0xfe, 0x00, 0x00, 255, 0xfd, 0x00, 0x00, 255, 0x00, 0xff, 0x01, 255, 0x01, 0xff, 0x02, 255,
-            0x00, 0xff, 0x01, 255, 0x00, 0xff, 0x01, 255, 0xfe, 0x00, 0x00, 255, 0xfe, 0x00, 0x00, 255,
-            0x00, 0xff, 0x01, 255, 0x01, 0xff, 0x00, 255, 0xfe, 0x00, 0x00, 255, 0xfd, 0x01, 0x00, 255
+            0xff, 0x00, 0x00, 255, 0xff, 0x00, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
+            0xff, 0x00, 0x00, 255, 0xff, 0x00, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
+            0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0xff, 0x00, 0x00, 255, 0xff, 0x00, 0x00, 255,
+            0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0xff, 0x00, 0x00, 255, 0xff, 0x00, 0x00, 255
         ]);
 
+        // Browsers have rounding errors, so we need an approximate
+        // comparing function
+        function almost(a, b) {
+            let diff = Math.abs(a - b);
+            return diff < 5;
+        }
+
         display.onflush = () => {
-            expect(display).to.have.displayed(targetData);
+            expect(display).to.have.displayed(targetData, almost);
             done();
         };
         display.flush();