From 17d50e804f8b98e66dc96ae98c565e32956c24e2 Mon Sep 17 00:00:00 2001 From: Chris Gervang Date: Fri, 5 Apr 2024 10:25:02 -0700 Subject: [PATCH] add(core) enforceWebGL2 tests Signed-off-by: Chris Gervang --- modules/core/src/adapter/luma.ts | 1 + modules/core/test/adapter/luma.spec.ts | 48 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/modules/core/src/adapter/luma.ts b/modules/core/src/adapter/luma.ts index 78acac7b7f..4a58497f42 100644 --- a/modules/core/src/adapter/luma.ts +++ b/modules/core/src/adapter/luma.ts @@ -156,6 +156,7 @@ export class luma { // Reset the original getContext function prototype.getContext = prototype.originalGetContext; prototype.originalGetContext = undefined; + return; } // Store the original getContext function diff --git a/modules/core/test/adapter/luma.spec.ts b/modules/core/test/adapter/luma.spec.ts index 72f42bcb55..8588d4b8a5 100644 --- a/modules/core/test/adapter/luma.spec.ts +++ b/modules/core/test/adapter/luma.spec.ts @@ -30,3 +30,51 @@ test('luma#registerDevices', async t => { t.equal(device.info.renderer, 'none', 'info.renderer ok'); t.end(); }); + +// To suppress @typescript-eslint/unbound-method +interface TestHTMLCanvasElement { + getContext: (contextId: any, options?: unknown) => string; + originalGetContext?: (contextId: any, options?: unknown) => unknown; +} + +test('luma#enforceWebGL2', async t => { + const prototype = HTMLCanvasElement.prototype as unknown as TestHTMLCanvasElement; + + // Setup mock getContext + const originalGetContext = prototype.getContext; + prototype.getContext = function (contextId: any, options?: unknown) { + return contextId; + }; + // Revert mock test completes. + t.teardown(() => { + prototype.getContext = originalGetContext; + }); + + t.equal(prototype.getContext('webgl'), 'webgl', 'getContext webgl ok'); + t.equal( + prototype.getContext('experimental-webgl'), + 'experimental-webgl', + 'getContext experimental-webgl ok' + ); + t.equal(prototype.getContext('webgl2'), 'webgl2', 'getContext webgl2 ok'); + + luma.enforceWebGL2(); + + t.true(prototype.originalGetContext, 'originalGetContext ok'); + t.equal(prototype.getContext('webgl'), 'webgl2', 'getContext enforce webgl2 ok'); + t.equal(prototype.getContext('experimental-webgl'), 'webgl2', 'getContext enforce webgl2 ok'); + t.equal(prototype.getContext('webgl2'), 'webgl2', 'getContext webgl2 ok'); + + luma.enforceWebGL2(false); + + t.false(prototype.originalGetContext, 'originalGetContext ok'); + t.equal(prototype.getContext('webgl'), 'webgl', 'getContext revert webgl ok'); + t.equal( + prototype.getContext('experimental-webgl'), + 'experimental-webgl', + 'getContext revert experimental-webgl ok' + ); + t.equal(prototype.getContext('webgl2'), 'webgl2', 'getContext webgl2 ok'); + + t.end(); +});