Skip to content

Commit

Permalink
add(core) enforceWebGL2 tests
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gervang <chris@gervang.com>
  • Loading branch information
chrisgervang committed Apr 5, 2024
1 parent 13dca9f commit 17d50e8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/core/src/adapter/luma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions modules/core/test/adapter/luma.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit 17d50e8

Please sign in to comment.