Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable loading of texture assets in unit tests #6928

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
522 changes: 515 additions & 7 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@
"@babel/core": "^7.24.4",
"@babel/eslint-parser": "^7.25.1",
"@babel/preset-env": "^7.24.4",
"@playcanvas/canvas-mock": "^1.0.1",
"@playcanvas/eslint-config": "^2.0.2",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-strip": "^3.0.4",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/pluginutils": "^5.1.0",
"c8": "^10.1.2",
"canvas": "^2.11.2",
"chai": "^5.1.0",
"eslint": "^9.10.0",
"fflate": "^0.8.2",
Expand Down
2 changes: 1 addition & 1 deletion test/core/math/quat.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ describe('Quat', function () {
[1, 2, 3],
[10, 10, 0]
].forEach(([x, y, z]) => {
it(`sets the quaternion from ${x}°, ${y}°, ${z}°`, () => {
it(`sets the quaternion from ${x}°, ${y}°, ${z}°`, function () {
const q1 = new Quat();
const q2 = new Quat();
const q3 = new Quat();
Expand Down
7 changes: 5 additions & 2 deletions test/fixtures.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import globalJsdom from 'global-jsdom';
import handler from 'serve-handler';
import http from 'http';
import XMLHttpRequest from 'xhr2';

import 'global-jsdom/register';

let server;

export const mochaGlobalSetup = () => {
globalJsdom(undefined, {
resources: 'usable'
});

// Provide a polyfill for XMLHttpRequest required by the engine
global.XMLHttpRequest = XMLHttpRequest;

Expand Down
8 changes: 4 additions & 4 deletions test/framework/anim/controller/anim-controller.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import { AnimCurve } from '../../../../src/framework/anim/evaluator/anim-curve.j
import { INTERPOLATION_LINEAR } from '../../../../src/framework/anim/constants.js';
import { ANIM_LESS_THAN } from '../../../../src/framework/anim/controller/constants.js';
import { NullGraphicsDevice } from '../../../../src/platform/graphics/null/null-graphics-device.js';
import { HTMLCanvasElement } from '@playcanvas/canvas-mock';
import { createCanvas } from 'canvas';
import { expect } from 'chai';

describe('AnimController', function () {

let app;
let controller;

beforeEach(() => {
const canvas = new HTMLCanvasElement(500, 500);
beforeEach(function () {
const canvas = createCanvas(500, 500);
app = new Application(canvas, { graphicsDevice: new NullGraphicsDevice(canvas) });
const states = [
{
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('AnimController', function () {
controller.assignAnimation('Other State 2', new AnimTrack('otherState2Track', 4, inputs, outputs, curves), 1, true);
});

afterEach(() => {
afterEach(function () {
app.destroy();
});

Expand Down
2 changes: 1 addition & 1 deletion test/framework/anim/evaluator/anim-clip.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { expect } from 'chai';
describe('AnimClip', function () {
let animClip;

beforeEach(() => {
beforeEach(function () {
const curves = [new AnimCurve(['path/to/entity'], 0, 0, INTERPOLATION_LINEAR)];
const inputs = [new AnimData(1, [0, 1, 2])];
const outputs = [new AnimData(3, [0, 0, 0, 1, 2, 3, 2, 4, 6])];
Expand Down
6 changes: 3 additions & 3 deletions test/framework/anim/evaluator/anim-evaluator.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import { DefaultAnimBinder } from '../../../../src/framework/anim/binder/default
import { GraphNode } from '../../../../src/scene/graph-node.js';
import { NullGraphicsDevice } from '../../../../src/platform/graphics/null/null-graphics-device.js';

import { HTMLCanvasElement } from '@playcanvas/canvas-mock';
import { createCanvas } from 'canvas';

import { expect } from 'chai';

describe('AnimEvaluator', function () {

it('AnimEvaluator: update with clip blending', function () {
const canvas = new HTMLCanvasElement(500, 500);
const canvas = createCanvas(500, 500);
const app = new Application(canvas, { graphicsDevice: new NullGraphicsDevice(canvas) });

// build the graph to be animated
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('AnimEvaluator', function () {
});

it('AnimEvaluator: update without clip blending', function () {
const canvas = new HTMLCanvasElement(500, 500);
const canvas = createCanvas(500, 500);
const app = new Application(canvas, { graphicsDevice: new NullGraphicsDevice(canvas) });

// build the graph to be animated
Expand Down
6 changes: 3 additions & 3 deletions test/framework/application.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ScriptRegistry } from '../../src/framework/script/script-registry.js';
import { XrManager } from '../../src/framework/xr/xr-manager.js';
import { NullGraphicsDevice } from '../../src/platform/graphics/null/null-graphics-device.js';

import { HTMLCanvasElement } from '@playcanvas/canvas-mock';
import { createCanvas } from 'canvas';

import { expect } from 'chai';

Expand All @@ -23,7 +23,7 @@ describe('Application', function () {
describe('#constructor', function () {

it('support no options', function () {
const canvas = new HTMLCanvasElement(500, 500);
const canvas = createCanvas(500, 500);
const app = new Application(canvas, { graphicsDevice: new NullGraphicsDevice(canvas) });

expect(app.assets).to.be.instanceOf(AssetRegistry);
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('Application', function () {
describe('#destroy', function () {

it('destroys the application', function () {
const canvas = new HTMLCanvasElement(500, 500);
const canvas = createCanvas(500, 500);
const app = new Application(canvas, { graphicsDevice: new NullGraphicsDevice(canvas) });

app.destroy();
Expand Down
8 changes: 4 additions & 4 deletions test/framework/asset/asset-list-loader.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AssetListLoader } from '../../../src/framework/asset/asset-list-loader.
import { Asset } from '../../../src/framework/asset/asset.js';
import { NullGraphicsDevice } from '../../../src/platform/graphics/null/null-graphics-device.js';

import { HTMLCanvasElement } from '@playcanvas/canvas-mock';
import { createCanvas } from 'canvas';

import { expect } from 'chai';

Expand All @@ -12,12 +12,12 @@ describe('AssetListLoader', function () {
let app;
const assetPath = 'http://localhost:3000/test/test-assets/';

beforeEach(() => {
const canvas = new HTMLCanvasElement(500, 500);
beforeEach(function () {
const canvas = createCanvas(500, 500);
app = new Application(canvas, { graphicsDevice: new NullGraphicsDevice(canvas) });
});

afterEach(() => {
afterEach(function () {
app.destroy();
});

Expand Down
8 changes: 4 additions & 4 deletions test/framework/asset/asset-localized.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import { Application } from '../../../src/framework/application.js';
import { Asset } from '../../../src/framework/asset/asset.js';
import { NullGraphicsDevice } from '../../../src/platform/graphics/null/null-graphics-device.js';

import { HTMLCanvasElement } from '@playcanvas/canvas-mock';
import { createCanvas } from 'canvas';

import { expect } from 'chai';

describe('LocalizedAsset', function () {

let app;

beforeEach(() => {
const canvas = new HTMLCanvasElement(500, 500);
beforeEach(function () {
const canvas = createCanvas(500, 500);
app = new Application(canvas, { graphicsDevice: new NullGraphicsDevice(canvas) });
});

afterEach(() => {
afterEach(function () {
app.destroy();
});

Expand Down
18 changes: 14 additions & 4 deletions test/framework/asset/asset-registry.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { Asset } from '../../../src/framework/asset/asset.js';
import { AssetRegistry } from '../../../src/framework/asset/asset-registry.js';
import { GlbContainerResource } from '../../../src/framework/parsers/glb-container-resource.js';
import { ResourceLoader } from '../../../src/framework/handlers/loader.js';
import { Texture } from '../../../src/platform/graphics/texture.js';
import { http, Http } from '../../../src/platform/net/http.js';
import { NullGraphicsDevice } from '../../../src/platform/graphics/null/null-graphics-device.js';

import { HTMLCanvasElement } from '@playcanvas/canvas-mock';
import { createCanvas } from 'canvas';

import { expect } from 'chai';
import { restore, spy } from 'sinon';
Expand All @@ -16,14 +17,14 @@ describe('AssetRegistry', function () {
let app;
let retryDelay;

beforeEach(() => {
beforeEach(function () {
retryDelay = Http.retryDelay;
Http.retryDelay = 1;
const canvas = new HTMLCanvasElement(500, 500);
const canvas = createCanvas(500, 500);
app = new Application(canvas, { graphicsDevice: new NullGraphicsDevice(canvas) });
});

afterEach(() => {
afterEach(function () {
app.destroy();
Http.retryDelay = retryDelay;
restore();
Expand Down Expand Up @@ -320,6 +321,15 @@ describe('AssetRegistry', function () {
});
});

it('loads texture assets', (done) => {
app.assets.loadFromUrl(`${assetPath}test.png`, 'texture', (err, asset) => {
expect(err).to.be.null;
expect(asset).to.be.instanceof(Asset);
expect(asset.resource).to.be.instanceof(Texture);
done();
});
});

});

describe('#remove', function () {
Expand Down
8 changes: 4 additions & 4 deletions test/framework/asset/asset.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { Application } from '../../../src/framework/application.js';
import { Asset } from '../../../src/framework/asset/asset.js';
import { NullGraphicsDevice } from '../../../src/platform/graphics/null/null-graphics-device.js';

import { HTMLCanvasElement } from '@playcanvas/canvas-mock';
import { createCanvas } from 'canvas';

import { expect } from 'chai';

describe('Asset', function () {

let app;

beforeEach(() => {
const canvas = new HTMLCanvasElement(500, 500);
beforeEach(function () {
const canvas = createCanvas(500, 500);
app = new Application(canvas, { graphicsDevice: new NullGraphicsDevice(canvas) });
});

afterEach(() => {
afterEach(function () {
app.destroy();
});

Expand Down
8 changes: 4 additions & 4 deletions test/framework/components/element/component.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { Application } from '../../../../src/framework/application.js';
import { Entity } from '../../../../src/framework/entity.js';
import { NullGraphicsDevice } from '../../../../src/platform/graphics/null/null-graphics-device.js';

import { HTMLCanvasElement } from '@playcanvas/canvas-mock';
import { createCanvas } from 'canvas';

import { expect } from 'chai';

describe('ElementComponent', function () {
let app;

beforeEach(() => {
const canvas = new HTMLCanvasElement(500, 500);
beforeEach(function () {
const canvas = createCanvas(500, 500);
app = new Application(canvas, { graphicsDevice: new NullGraphicsDevice(canvas) });
});

afterEach(() => {
afterEach(function () {
app.destroy();
});

Expand Down
5 changes: 3 additions & 2 deletions test/framework/components/element/draworder.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import { Entity } from '../../../../src/framework/entity.js';
import { NullGraphicsDevice } from '../../../../src/platform/graphics/null/null-graphics-device.js';
import { ScreenComponent } from '../../../../src/framework/components/screen/component.js';

import { HTMLCanvasElement } from '@playcanvas/canvas-mock';
import { createCanvas } from 'canvas';

import { expect } from 'chai';

describe('ElementComponent Draw Order', function () {
let app;

beforeEach(function () {
const canvas = new HTMLCanvasElement(500, 500);
const canvas = createCanvas(500, 500);
canvas.getBoundingClientRect = () => ({ left: 0, top: 0, width: 500, height: 500 });
app = new Application(canvas, { graphicsDevice: new NullGraphicsDevice(canvas) });
});

Expand Down
5 changes: 3 additions & 2 deletions test/framework/components/element/maks.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import { Application } from '../../../../src/framework/application.js';
import { Entity } from '../../../../src/framework/entity.js';
import { NullGraphicsDevice } from '../../../../src/platform/graphics/null/null-graphics-device.js';

import { HTMLCanvasElement } from '@playcanvas/canvas-mock';
import { createCanvas } from 'canvas';

import { expect } from 'chai';

describe('ElementComponent Masks', function () {
let app;

beforeEach(function () {
const canvas = new HTMLCanvasElement(500, 500);
const canvas = createCanvas(500, 500);
canvas.getBoundingClientRect = () => ({ left: 0, top: 0, width: 500, height: 500 });
app = new Application(canvas, { graphicsDevice: new NullGraphicsDevice(canvas) });
});

Expand Down
8 changes: 4 additions & 4 deletions test/framework/components/layout-group/component.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Application } from '../../../../src/framework/application.js';
import { Entity } from '../../../../src/framework/entity.js';
import { NullGraphicsDevice } from '../../../../src/platform/graphics/null/null-graphics-device.js';

import { HTMLCanvasElement } from '@playcanvas/canvas-mock';
import { createCanvas } from 'canvas';

import { expect } from 'chai';
import { restore, spy, stub } from 'sinon';
Expand Down Expand Up @@ -31,8 +31,8 @@ describe('LayoutGroupComponent', function () {
return entity;
};

beforeEach(() => {
const canvas = new HTMLCanvasElement(500, 500);
beforeEach(function () {
const canvas = createCanvas(500, 500);
app = new Application(canvas, { graphicsDevice: new NullGraphicsDevice(canvas) });
system = app.systems.layoutgroup;

Expand All @@ -51,7 +51,7 @@ describe('LayoutGroupComponent', function () {
spy(entity0_0_0.layoutgroup, 'reflow');
});

afterEach(() => {
afterEach(function () {
restore();
app.destroy();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Vec2 } from '../../../../src/core/math/vec2.js';
import { Vec4 } from '../../../../src/core/math/vec4.js';
import { NullGraphicsDevice } from '../../../../src/platform/graphics/null/null-graphics-device.js';

import { HTMLCanvasElement } from '@playcanvas/canvas-mock';
import { createCanvas } from 'canvas';

import { expect } from 'chai';

Expand Down Expand Up @@ -61,8 +61,8 @@ describe('LayoutCalculator', function () {
});
};

beforeEach(() => {
const canvas = new HTMLCanvasElement(500, 500);
beforeEach(function () {
const canvas = createCanvas(500, 500);
app = new Application(canvas, { graphicsDevice: new NullGraphicsDevice(canvas) });
calculator = new LayoutCalculator();

Expand Down Expand Up @@ -242,7 +242,7 @@ describe('LayoutCalculator', function () {
]);
});

afterEach(() => {
afterEach(function () {
app.destroy();
});

Expand Down
Loading