aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/tests/browser_context.mjs (2487B)


      1 import assert from 'node:assert/strict';
      2 import fs from 'node:fs';
      3 import vm from 'node:vm';
      4 import test from 'node:test';
      5 
      6 function graphics(proxied = false) {
      7   const canvas = new EventTarget();
      8   let lost = false, notifications = 0, framebuffers = 0, queries = 0, library;
      9   const context = {defaultFbo: proxied ? {} : null, GLctx: {canvas, isContextLost: () => lost}};
     10   vm.runInNewContext(fs.readFileSync(new URL('../runtime/browser_host.js', import.meta.url), 'utf8'), {
     11     GL: {currentContext: proxied ? 7 : context, currentContextIsProxied: proxied,
     12       getContext: handle => !proxied && handle === 7 ? context : undefined,
     13       createOffscreenFramebuffer: () => ++framebuffers},
     14     AOInitializeGraphics: context => library.$AOInitializeGraphics(context),
     15     _ao_graphics_initialize_main: handle => {
     16       assert.equal(handle, 7);
     17       library.$AOInitializeGraphics(context);
     18     },
     19     _ao_graphics_context_lost_main: handle => {
     20       assert.equal(handle, 7);
     21       ++queries;
     22       return lost;
     23     },
     24     _browser_graphics_lost: () => ++notifications,
     25     addToLibrary: value => { library = value; }
     26   });
     27   library.ao_graphics_initialize();
     28   return {
     29     library,
     30     notifications: () => notifications,
     31     framebuffers: () => framebuffers,
     32     queries: () => queries,
     33     lose() {
     34       lost = true;
     35       const event = new Event('webglcontextlost', {cancelable: true});
     36       canvas.dispatchEvent(event);
     37       return event;
     38     },
     39     restore() { lost = false; canvas.dispatchEvent(new Event('webglcontextrestored')); }
     40   };
     41 }
     42 
     43 test('context loss allows restoration and notifies the game on every loss', () => {
     44   const game = graphics();
     45 
     46   const losses = [];
     47   for (let index = 0; index < 3; ++index) {
     48     losses.push(game.lose());
     49     game.restore();
     50   }
     51 
     52   assert.ok(losses.every(event => event.defaultPrevented));
     53   assert.equal(game.notifications(), 3);
     54   assert.equal(game.library.ao_graphics_wait, undefined);
     55 });
     56 
     57 test('proxied context loss reaches the page and rebuilds its presentation framebuffer', () => {
     58   const game = graphics(true);
     59 
     60   const event = game.lose();
     61   const lost = game.library.emscripten_is_webgl_context_lost(7);
     62   game.restore();
     63   const restored = game.library.emscripten_is_webgl_context_lost(7);
     64 
     65   assert.equal(event.defaultPrevented, true);
     66   assert.equal(game.notifications(), 1);
     67   assert.equal(lost, true);
     68   assert.equal(restored, false);
     69   assert.equal(game.framebuffers(), 1);
     70   assert.equal(game.queries(), 2);
     71 });