diff --git a/test/unit/src/rendering/renderers/webGl/WebGlRenderer.test.js b/test/unit/src/rendering/renderers/webGl/WebGlRenderer.test.js index d5dc41b3..f2647776 100644 --- a/test/unit/src/rendering/renderers/webGl/WebGlRenderer.test.js +++ b/test/unit/src/rendering/renderers/webGl/WebGlRenderer.test.js @@ -1,9 +1,11 @@ import { assertEquals, assertRejects, assertStrictEquals } from "std/testing/asserts.ts"; import { CustomMaterialData, Entity, Material, MaterialMap, Mesh, ShaderSource, VertexState, WebGlMaterialConfig, WebGlMaterialMapType, WebGlRenderer, WebGlRendererError } from "../../../../../../src/mod.js"; -import { assertHasSingleContext, runWithWebGlMocksAsync, setWebGlContextSupported } from "./shared/webGlMocks.js"; +import { assertHasSingleWebGlContext, runWithWebGlMocksAsync, setWebGlContextSupported } from "./shared/webGlMocks.js"; import { assertIsType, testTypes } from "../../../../shared/typeAssertions.js"; import { createCam, createCubeEntity, createVertexState } from "../shared/sceneUtil.js"; import { assertLogEntryEquals, assertLogEquals } from "./shared/WebGlCommandLog.js"; +import { assertHasSingle2dRenderingContext } from "./shared/RenderingContext2d.js"; +import { assertSpyCall, assertSpyCalls, spy } from "std/testing/mock.ts"; async function basicRendererSetup() { const renderer = new WebGlRenderer(); @@ -15,7 +17,7 @@ async function basicRendererSetup() { const scene = new Entity(); scene.add(cam); - const context = assertHasSingleContext(); + const context = assertHasSingleWebGlContext(); return { renderer, domTarget, camComponent, scene, ...context }; } @@ -88,10 +90,25 @@ Deno.test({ const domTarget = renderer.createDomTarget(); + const context2d = assertHasSingle2dRenderingContext(); + const { commandLog, canvas: webGlCanvas } = assertHasSingleWebGlContext(); + + const clearRectSpy = spy(context2d, "clearRect"); + const drawImageSpy = spy(context2d, "drawImage"); + const { camComponent } = createCam(); domTarget.render(camComponent); - const { commandLog } = assertHasSingleContext(); + assertSpyCalls(clearRectSpy, 1); + assertSpyCall(clearRectSpy, 0, { + args: [0, 0, 300, 150], + }); + assertSpyCalls(drawImageSpy, 1); + assertEquals(drawImageSpy.calls[0].args.length, 3); + assertStrictEquals(drawImageSpy.calls[0].args[0], webGlCanvas); + assertEquals(drawImageSpy.calls[0].args[1], 0); + assertEquals(drawImageSpy.calls[0].args[2], 0); + commandLog.assertCount(5); commandLog.assertLogEquals([ @@ -127,7 +144,7 @@ Deno.test({ const renderer = new WebGlRenderer(); await renderer.init(); - const { commandLog, canvas } = assertHasSingleContext(); + const { commandLog, canvas } = assertHasSingleWebGlContext(); const domTargetA = renderer.createDomTarget(); domTargetA.resize(100, 200); diff --git a/test/unit/src/rendering/renderers/webGl/shared/RenderingContext2d.js b/test/unit/src/rendering/renderers/webGl/shared/RenderingContext2d.js index 66842578..8168c79c 100644 --- a/test/unit/src/rendering/renderers/webGl/shared/RenderingContext2d.js +++ b/test/unit/src/rendering/renderers/webGl/shared/RenderingContext2d.js @@ -1,3 +1,5 @@ +import { assertEquals } from "std/testing/asserts.ts"; + export class RenderingContext2d { /** * @param {CanvasImageSource} image @@ -5,12 +7,27 @@ export class RenderingContext2d { * @param {number} dy */ drawImage(image, dx, dy) {} + + clearRect() {} } +/** @type {RenderingContext2d[]} */ +let created2dRenderingContexts = []; + /** * @param {{width: number, height: number}} canvas */ export function create2dRenderingContext(canvas) { const context = new RenderingContext2d(); + created2dRenderingContexts.push(context); return context; } + +export function assertHasSingle2dRenderingContext() { + assertEquals(created2dRenderingContexts.length, 1, 'Expected to have exactly one canvas with getContext("2d")'); + return created2dRenderingContexts[0]; +} + +export function clearCreated2dRenderingContexts() { + created2dRenderingContexts = []; +} diff --git a/test/unit/src/rendering/renderers/webGl/shared/webGlMocks.js b/test/unit/src/rendering/renderers/webGl/shared/webGlMocks.js index 1ed45fd4..7873b717 100644 --- a/test/unit/src/rendering/renderers/webGl/shared/webGlMocks.js +++ b/test/unit/src/rendering/renderers/webGl/shared/webGlMocks.js @@ -1,4 +1,4 @@ -import { create2dRenderingContext } from "./RenderingContext2d.js"; +import { clearCreated2dRenderingContexts, create2dRenderingContext } from "./RenderingContext2d.js"; import { createWebGlRenderingContext } from "./WebGlRenderingContext.js"; const oldDocument = globalThis.document; @@ -54,6 +54,7 @@ export function installWebGlMocks() { export function uninstallWebGlMocks() { globalThis.document = oldDocument; installed = false; + clearCreated2dRenderingContexts(); } /** @@ -86,7 +87,7 @@ export function set2dContextSupported(supported) { context2dSupported = supported; } -export function assertHasSingleContext() { +export function assertHasSingleWebGlContext() { if (createdContexts.length != 1) { throw new Error("Expected exactly one webgl context to be created"); }