mirror of
https://github.com/bringout/oca-ocb-core.git
synced 2026-04-20 10:52:02 +02:00
vanilla 19.0
This commit is contained in:
parent
991d2234ca
commit
d1963a3c3a
3066 changed files with 1651266 additions and 922560 deletions
|
|
@ -0,0 +1,137 @@
|
|||
import { expect, test } from "@odoo/hoot";
|
||||
import { Deferred } from "@odoo/hoot-mock";
|
||||
import {
|
||||
contains,
|
||||
makeMockEnv,
|
||||
mountWithCleanup,
|
||||
patchWithCleanup,
|
||||
} from "@web/../tests/web_test_helpers";
|
||||
|
||||
import { browser } from "@web/core/browser/browser";
|
||||
import { scanBarcode } from "@web/core/barcode/barcode_dialog";
|
||||
import { BarcodeVideoScanner } from "@web/core/barcode/barcode_video_scanner";
|
||||
import { WebClient } from "@web/webclient/webclient";
|
||||
|
||||
/* global ZXing */
|
||||
|
||||
test("Barcode scanner crop overlay", async () => {
|
||||
const env = await makeMockEnv();
|
||||
await mountWithCleanup(WebClient, { env });
|
||||
|
||||
const firstBarcodeValue = "Odoo";
|
||||
const secondBarcodeValue = "OCDTEST";
|
||||
|
||||
let barcodeToGenerate = firstBarcodeValue;
|
||||
let videoReady = new Deferred();
|
||||
|
||||
function mockUserMedia() {
|
||||
const canvas = document.createElement("canvas");
|
||||
const ctx = canvas.getContext("2d");
|
||||
const stream = canvas.captureStream();
|
||||
|
||||
const multiFormatWriter = new ZXing.MultiFormatWriter();
|
||||
const bitMatrix = multiFormatWriter.encode(
|
||||
barcodeToGenerate,
|
||||
ZXing.BarcodeFormat.QR_CODE,
|
||||
250,
|
||||
250,
|
||||
null
|
||||
);
|
||||
canvas.width = bitMatrix.width;
|
||||
canvas.height = bitMatrix.height;
|
||||
ctx.strokeStyle = "black";
|
||||
ctx.fillStyle = "white";
|
||||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||
for (let x = 0; x < bitMatrix.width; x++) {
|
||||
for (let y = 0; y < bitMatrix.height; y++) {
|
||||
if (bitMatrix.get(x, y)) {
|
||||
ctx.beginPath();
|
||||
ctx.rect(x, y, 1, 1);
|
||||
ctx.stroke();
|
||||
}
|
||||
}
|
||||
}
|
||||
return stream;
|
||||
}
|
||||
// simulate an environment with a camera/webcam
|
||||
patchWithCleanup(browser.navigator, {
|
||||
mediaDevices: {
|
||||
getUserMedia: mockUserMedia,
|
||||
},
|
||||
});
|
||||
|
||||
patchWithCleanup(BarcodeVideoScanner.prototype, {
|
||||
async isVideoReady() {
|
||||
await super.isVideoReady(...arguments);
|
||||
videoReady.resolve();
|
||||
},
|
||||
onResize(overlayInfo) {
|
||||
expect.step(overlayInfo);
|
||||
return super.onResize(...arguments);
|
||||
},
|
||||
});
|
||||
|
||||
const firstBarcodeFound = scanBarcode(env);
|
||||
await videoReady;
|
||||
await contains(".o_crop_icon").dragAndDrop(".o_crop_container", {
|
||||
relative: true,
|
||||
position: {
|
||||
x: 0,
|
||||
y: 0,
|
||||
},
|
||||
});
|
||||
|
||||
const firstValueScanned = await firstBarcodeFound;
|
||||
expect(firstValueScanned).toBe(firstBarcodeValue, {
|
||||
message: `The detected barcode (${firstValueScanned}) should be the same as generated (${firstBarcodeValue})`,
|
||||
});
|
||||
|
||||
// Do another scan barcode to the test position of the overlay saved in the locale storage
|
||||
// Reset all values for the second test
|
||||
barcodeToGenerate = secondBarcodeValue;
|
||||
videoReady = new Deferred();
|
||||
|
||||
const secondBarcodeFound = scanBarcode(env);
|
||||
await videoReady;
|
||||
const secondValueScanned = await secondBarcodeFound;
|
||||
expect(secondValueScanned).toBe(secondBarcodeValue, {
|
||||
message: `The detected barcode (${secondValueScanned}) should be the same as generated (${secondBarcodeValue})`,
|
||||
});
|
||||
|
||||
expect.verifySteps([
|
||||
{ x: 25, y: 100, width: 200, height: 50 },
|
||||
{ x: 0, y: 0, width: 250, height: 250 },
|
||||
{ x: 0, y: 0, width: 250, height: 250 },
|
||||
]);
|
||||
});
|
||||
|
||||
test("BarcodeVideoScanner onReady props", async () => {
|
||||
function mockUserMedia() {
|
||||
const canvas = document.createElement("canvas");
|
||||
const ctx = canvas.getContext("2d");
|
||||
const stream = canvas.captureStream();
|
||||
canvas.width = 250;
|
||||
canvas.height = 250;
|
||||
ctx.strokeStyle = "black";
|
||||
ctx.fillStyle = "white";
|
||||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||
return stream;
|
||||
}
|
||||
// Simulate an environment with a camera/webcam.
|
||||
patchWithCleanup(browser.navigator, {
|
||||
mediaDevices: {
|
||||
getUserMedia: mockUserMedia,
|
||||
},
|
||||
});
|
||||
const resolvedOnReadyPromise = new Promise((resolve) => {
|
||||
mountWithCleanup(BarcodeVideoScanner, {
|
||||
props: {
|
||||
facingMode: "environment",
|
||||
onReady: () => resolve(true),
|
||||
onResult: () => {},
|
||||
onError: () => {},
|
||||
},
|
||||
});
|
||||
});
|
||||
expect(await resolvedOnReadyPromise).toBe(true);
|
||||
});
|
||||
|
|
@ -1,133 +0,0 @@
|
|||
/** @odoo-module **/
|
||||
/* global ZXing */
|
||||
|
||||
import { browser } from "@web/core/browser/browser";
|
||||
import {
|
||||
makeDeferred,
|
||||
nextTick,
|
||||
patchWithCleanup,
|
||||
triggerEvent,
|
||||
} from "@web/../tests/helpers/utils";
|
||||
|
||||
import { scanBarcode, BarcodeDialog } from "@web/webclient/barcode/barcode_scanner";
|
||||
|
||||
QUnit.module("Barcode scanner", {});
|
||||
|
||||
QUnit.test("Barcode scanner crop overlay", async (assert) => {
|
||||
const firstBarcodeValue = "Odoo";
|
||||
const secondBarcodeValue = "O-CMD-TEST";
|
||||
|
||||
let barcodeToGenerate = firstBarcodeValue;
|
||||
let videoReady = makeDeferred();
|
||||
|
||||
function mockUserMedia() {
|
||||
const canvas = document.createElement("canvas");
|
||||
const ctx = canvas.getContext("2d");
|
||||
const stream = canvas.captureStream();
|
||||
|
||||
const multiFormatWriter = new ZXing.MultiFormatWriter();
|
||||
const bitMatrix = multiFormatWriter.encode(
|
||||
barcodeToGenerate,
|
||||
ZXing.BarcodeFormat.QR_CODE,
|
||||
250,
|
||||
250,
|
||||
null
|
||||
);
|
||||
canvas.width = bitMatrix.width;
|
||||
canvas.height = bitMatrix.height;
|
||||
for (let x = 0; x < bitMatrix.width; x++) {
|
||||
for (let y = 0; y < bitMatrix.height; y++) {
|
||||
if (bitMatrix.get(x, y)) {
|
||||
ctx.beginPath();
|
||||
ctx.rect(x, y, 1, 1);
|
||||
ctx.stroke();
|
||||
}
|
||||
}
|
||||
}
|
||||
return stream;
|
||||
}
|
||||
// simulate an environment with a camera/webcam
|
||||
patchWithCleanup(
|
||||
browser,
|
||||
Object.assign({}, browser, {
|
||||
navigator: {
|
||||
mediaDevices: {
|
||||
getUserMedia: mockUserMedia,
|
||||
},
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
patchWithCleanup(BarcodeDialog.prototype, {
|
||||
async isVideoReady() {
|
||||
return this._super(...arguments).then(() => {
|
||||
videoReady.resolve();
|
||||
});
|
||||
},
|
||||
onResize(overlayInfo) {
|
||||
assert.step(JSON.stringify(overlayInfo));
|
||||
return this._super(...arguments);
|
||||
},
|
||||
});
|
||||
|
||||
const firstBarcodeFound = scanBarcode();
|
||||
await videoReady;
|
||||
// Needed due to the change on the props in the Crop component
|
||||
await nextTick();
|
||||
const cropIconSelector = ".o_crop_icon";
|
||||
const cropIcon = document.querySelector(cropIconSelector);
|
||||
const cropOverlay = document.querySelector(".o_crop_overlay");
|
||||
const cropContainer = document.querySelector(".o_crop_container");
|
||||
const cropIconPosition = cropIcon.getBoundingClientRect();
|
||||
const cropOverlayPosition = cropOverlay.getBoundingClientRect();
|
||||
await triggerEvent(cropContainer, cropIconSelector, "touchstart", {
|
||||
touches: [
|
||||
{
|
||||
identifier: 0,
|
||||
clientX: cropIconPosition.x + cropIconPosition.width / 2,
|
||||
clientY: cropIconPosition.y + cropIconPosition.height / 2,
|
||||
target: cropIcon,
|
||||
},
|
||||
],
|
||||
});
|
||||
await triggerEvent(cropContainer, cropIconSelector, "touchmove", {
|
||||
touches: [
|
||||
{
|
||||
identifier: 0,
|
||||
clientX: cropOverlayPosition.right,
|
||||
clientY: cropOverlayPosition.bottom,
|
||||
target: cropIcon,
|
||||
},
|
||||
],
|
||||
});
|
||||
await triggerEvent(cropContainer, cropIconSelector, "touchend", {});
|
||||
const firstValueScanned = await firstBarcodeFound;
|
||||
assert.strictEqual(
|
||||
firstValueScanned,
|
||||
firstBarcodeValue,
|
||||
`The detected barcode should be the same as generated (${firstBarcodeValue})`
|
||||
);
|
||||
|
||||
// Do another scan barcode to the test position of the overlay saved in the locale storage
|
||||
// Reset all values for the second test
|
||||
barcodeToGenerate = secondBarcodeValue;
|
||||
videoReady = makeDeferred();
|
||||
|
||||
const secondBarcodeFound = scanBarcode();
|
||||
await videoReady;
|
||||
const secondValueScanned = await secondBarcodeFound;
|
||||
assert.strictEqual(
|
||||
secondValueScanned,
|
||||
secondBarcodeValue,
|
||||
`The detected barcode should be the same as generated (${secondBarcodeValue})`
|
||||
);
|
||||
|
||||
assert.verifySteps(
|
||||
[
|
||||
JSON.stringify({ x: 25, y: 100, width: 200, height: 50 }),
|
||||
JSON.stringify({ x: 0, y: 0, width: 250, height: 250 }),
|
||||
JSON.stringify({ x: 0, y: 0, width: 250, height: 250 }),
|
||||
],
|
||||
"We should haves three resize event; one for the default position, another one for the all frame and the last one must be the same as the saved second position"
|
||||
);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue