/** Part of Beyond. Collina Systems proprietary module. */ import { expect, test, describe } from "@odoo/hoot"; import { click, queryAll, queryFirst } from "@odoo/hoot-dom"; import { animationFrame } from "@odoo/hoot-mock"; import { defineModels, fields, models, mountView, patchWithCleanup, } from "@web/../tests/web_test_helpers"; import { browser } from "@web/core/browser/browser"; /** * Unit tests for beyond_image_camera. * * The one that matters most is the secure-context one: production has no TLS * yet, so navigator.mediaDevices does not exist on the branch tablets and the * widget must not offer a button that would throw. That is the case a person * cannot easily check by hand — dev runs on localhost, where the camera * always works. */ describe.current.tags("desktop"); class ResPartner extends models.Model { _name = "res.partner"; name = fields.Char(); image_1920 = fields.Binary(); _records = [{ id: 1, name: "Alejandro Mcdonald", image_1920: "FOTO==" }]; } defineModels([ResPartner]); const VISTA = /* xml */ `
`; /** Fakes a camera: one track whose stop() we can watch. */ function camaraFalsa({ camaras = 1 } = {}) { const pista = { stop: () => (pista.detenida = true), detenida: false, kind: "video" }; const stream = { getTracks: () => [pista] }; patchWithCleanup(browser, { navigator: { ...browser.navigator, mediaDevices: { getUserMedia: async () => stream, enumerateDevices: async () => Array.from({ length: camaras }, () => ({ kind: "videoinput" })), }, }, }); return pista; } /** Takes the camera away, the way an http:// page does. */ function sinCamara() { patchWithCleanup(browser, { navigator: { ...browser.navigator, mediaDevices: undefined }, }); } test("el menú de la foto se abre al pasar el mouse y se cierra al salir", async () => { camaraFalsa(); await mountView({ type: "form", resModel: "res.partner", resId: 1, arch: VISTA }); expect(".beyond_foto_menu").toHaveCount(0); await queryFirst(".beyond_foto").dispatchEvent(new MouseEvent("mouseenter")); await animationFrame(); expect(".beyond_foto_menu").toHaveCount(1); await queryFirst(".beyond_foto").dispatchEvent(new MouseEvent("mouseleave")); await animationFrame(); expect(".beyond_foto_menu").toHaveCount(0); }); test("un toque fija el menú y otro lo suelta (en táctil no hay hover)", async () => { camaraFalsa(); await mountView({ type: "form", resModel: "res.partner", resId: 1, arch: VISTA }); await click(".beyond_foto"); await animationFrame(); expect(".beyond_foto_menu").toHaveCount(1); // Fijado: salir con el mouse ya no lo cierra. await queryFirst(".beyond_foto").dispatchEvent(new MouseEvent("mouseleave")); await animationFrame(); expect(".beyond_foto_menu").toHaveCount(1); await click(".beyond_foto"); await animationFrame(); expect(".beyond_foto_menu").toHaveCount(0); }); test("sin cámara no se ofrece «Tomar fotografía», y sí «Subir archivo»", async () => { // Es el caso de las tablets de sucursal mientras producción no tenga TLS: // fuera de contexto seguro el navegador ni siquiera define mediaDevices. sinCamara(); await mountView({ type: "form", resModel: "res.partner", resId: 1, arch: VISTA }); await click(".beyond_foto"); await animationFrame(); const opciones = queryAll(".beyond_foto_opcion").map((b) => b.textContent.trim()); expect(opciones).not.toInclude("Tomar fotografía"); expect(opciones).toInclude("Subir archivo"); }); test("con cámara sí se ofrece «Tomar fotografía»", async () => { camaraFalsa(); await mountView({ type: "form", resModel: "res.partner", resId: 1, arch: VISTA }); await click(".beyond_foto"); await animationFrame(); const opciones = queryAll(".beyond_foto_opcion").map((b) => b.textContent.trim()); expect(opciones).toInclude("Tomar fotografía"); }); test("«Subir archivo» pide la cámara trasera, no la frontal", async () => { // Coherencia con el diálogo propio: el caso de uso es el mostrador // fotografiando al cliente, no una selfie. sinCamara(); await mountView({ type: "form", resModel: "res.partner", resId: 1, arch: VISTA }); await click(".beyond_foto"); await animationFrame(); expect("input[type=file]").toHaveAttribute("capture", "environment"); }); test("«Eliminar» limpia el campo", async () => { camaraFalsa(); await mountView({ type: "form", resModel: "res.partner", resId: 1, arch: VISTA }); await click(".beyond_foto"); await animationFrame(); await click(".beyond_foto_borrar"); await animationFrame(); // Sin imagen no hay nada que borrar: la opción desaparece. expect(".beyond_foto_borrar").toHaveCount(0); }); test("cerrar el diálogo apaga la cámara", async () => { // Una cámara que se queda encendida deja el testigo del aparato prendido // y parece que el local está grabando. const pista = camaraFalsa(); await mountView({ type: "form", resModel: "res.partner", resId: 1, arch: VISTA }); await click(".beyond_foto"); await animationFrame(); await click(".beyond_foto_opcion"); // «Tomar fotografía» await animationFrame(); await animationFrame(); expect(".beyond_camara_video").toHaveCount(1); expect(pista.detenida).toBe(false); await click(".beyond_camara_dialogo .btn-secondary:last-child"); // Cancelar await animationFrame(); expect(pista.detenida).toBe(true); }); test("con una sola cámara no se ofrece cambiarla", async () => { camaraFalsa({ camaras: 1 }); await mountView({ type: "form", resModel: "res.partner", resId: 1, arch: VISTA }); await click(".beyond_foto"); await animationFrame(); await click(".beyond_foto_opcion"); await animationFrame(); await animationFrame(); const botones = queryAll(".beyond_camara_dialogo .modal-footer .btn").map((b) => b.textContent.trim() ); expect(botones).not.toInclude("Cambiar de cámara"); });