Skip to content

Commit

Permalink
Fix unit test and compile bug (#746)
Browse files Browse the repository at this point in the history
* fix: component unit test
  • Loading branch information
GuoLei1990 authored Apr 20, 2022
1 parent baf16cb commit 0a938c6
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 45 deletions.
5 changes: 1 addition & 4 deletions packages/core/src/Script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,7 @@ export class Script extends Component {
_handlingInValid(): void {
const componentsManager = this.engine._componentsManager;
// Use "xxIndex !== -1" to project.
// Developer maybe call onDisable it maybe it still not in script queue, for example write "entity.isActive = false" in onWake().
if (this._onStartIndex !== -1) {
componentsManager.removeOnStartScript(this);
}
// Maybe call onDisable it is still not in script queue, for example write "entity.isActive = false" in onWake().
if (this._onUpdateIndex !== -1) {
componentsManager.removeOnUpdateScript(this);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/env-probe/Probe.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Camera } from "../Camera";
import { Layer } from "../Layer";
import { Script } from "../Script";
import { Texture, Texture2D, TextureCube, TextureFormat } from "../texture";
import { RenderBufferDepthFormat, Texture, Texture2D, TextureCube, TextureFormat } from "../texture";
import { RenderTarget } from "../texture/RenderTarget";

/**
Expand Down Expand Up @@ -77,7 +77,7 @@ export abstract class Probe extends Script {
this.width,
this.height,
this._isCube ? new TextureCube(this.engine, this.width) : new Texture2D(this.engine, this.width, this.height),
TextureFormat.Depth,
RenderBufferDepthFormat.Depth,
this.antiAliasing
);

Expand All @@ -86,7 +86,7 @@ export abstract class Probe extends Script {
this.width,
this.height,
this._isCube ? new TextureCube(this.engine, this.width) : new Texture2D(this.engine, this.width, this.height),
TextureFormat.Depth,
RenderBufferDepthFormat.Depth,
this.antiAliasing
);

Expand Down
18 changes: 8 additions & 10 deletions packages/core/tests/Component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,10 @@ describe("Component", () => {
//@ts-ignore
entity.parent = scene.getRootEntity();
TestComponent.prototype._onDisable = jest.fn();
TestComponent.prototype._onInActive = jest.fn();
TestComponent.prototype._onDestroy = jest.fn();
const component = entity.addComponent(TestComponent);
component.destroy();
expect(component._onDisable).toHaveBeenCalledTimes(1);
expect(component._onInActive).toHaveBeenCalledTimes(1);
expect(component._onDestroy).toHaveBeenCalledTimes(1);
});
});
Expand Down Expand Up @@ -138,23 +136,23 @@ describe("Component", () => {
});

describe("active", () => {
it("onActive", () => {
it("onEnable", () => {
const entity = new Entity(engine, "entity");
//@ts-ignore
entity.parent = scene.getRootEntity();
TestComponent.prototype._onActive = jest.fn();
TestComponent.prototype._onEnable = jest.fn();
const component = entity.addComponent(TestComponent);
expect(component._onActive).toHaveBeenCalledTimes(1);
expect(component._onEnable).toHaveBeenCalledTimes(1);
});

it("onInActive", () => {
it("onDisable", () => {
const entity = new Entity(engine, "entity");
//@ts-ignore
entity.parent = scene.getRootEntity();
TestComponent.prototype._onInActive = jest.fn();
TestComponent.prototype._onDisable = jest.fn();
const component = entity.addComponent(TestComponent);
entity.isActive = false;
expect(component._onInActive).toHaveBeenCalledTimes(1);
expect(component._onDisable).toHaveBeenCalledTimes(1);
});

it("inActiveHierarchy", () => {
Expand All @@ -163,10 +161,10 @@ describe("Component", () => {
parent.parent = scene.getRootEntity();
const child = new Entity(engine, "child");
child.parent = parent;
TestComponent.prototype._onInActive = jest.fn();
TestComponent.prototype._onDisable = jest.fn();
const component = child.addComponent(TestComponent);
parent.isActive = false;
expect(component._onInActive).toHaveBeenCalledTimes(1);
expect(component._onDisable).toHaveBeenCalledTimes(1);
});
});
});
28 changes: 9 additions & 19 deletions packages/core/tests/script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@ describe("Script", () => {
expect(component.onDisable).toHaveBeenCalledTimes(1);
});

it("_onInActive", () => {
class TheScript extends Script {}
const entity = new Entity(engine, "entity");
entity.parent = scene.getRootEntity();
TheScript.prototype._onInActive = jest.fn();
const component = entity.addComponent(TheScript);
component.destroy();
expect(component._onInActive).toHaveBeenCalledTimes(1);
});

it("_onDestroy", () => {
class TheScript extends Script {}
const entity = new Entity(engine, "entity");
Expand Down Expand Up @@ -107,23 +97,23 @@ describe("Script", () => {
});

describe("active", () => {
it("onActive", () => {
it("onEnable", () => {
class TheScript extends Script {}
const entity = new Entity(engine, "entity");
entity.parent = scene.getRootEntity();
TheScript.prototype._onActive = jest.fn();
TheScript.prototype._onEnable = jest.fn();
const component = entity.addComponent(TheScript);
expect(component._onActive).toHaveBeenCalledTimes(1);
expect(component._onEnable).toHaveBeenCalledTimes(1);
});

it("onInActive", () => {
it("onDisable", () => {
class TheScript extends Script {}
const entity = new Entity(engine, "entity");
entity.parent = scene.getRootEntity();
TheScript.prototype._onInActive = jest.fn();
TheScript.prototype._onDisable = jest.fn();
const component = entity.addComponent(TheScript);
entity.isActive = false;
expect(component._onInActive).toHaveBeenCalledTimes(1);
expect(component._onDisable).toHaveBeenCalledTimes(1);
});

it("inActiveHierarchy", () => {
Expand All @@ -132,10 +122,10 @@ describe("Script", () => {
parent.parent = scene.getRootEntity();
const child = new Entity(engine, "child");
child.parent = parent;
TheScript.prototype._onInActive = jest.fn();
TheScript.prototype._onDisable = jest.fn();
const component = child.addComponent(TheScript);
parent.isActive = false;
expect(component._onInActive).toHaveBeenCalledTimes(1);
expect(component._onDisable).toHaveBeenCalledTimes(1);
});
});

Expand Down Expand Up @@ -281,7 +271,7 @@ describe("Script", () => {
const root = scene.getRootEntity();
const component = root.addComponent(TheScript);
component.destroy();
engine._componentsManager.callComponentDestroy();
engine._componentsManager.handlingInvalidScripts();
expect(component.onDestroy).toHaveBeenCalledTimes(1);
});
});
Expand Down
6 changes: 3 additions & 3 deletions packages/core/tests/texture/RenderTarget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// @todo: jest `_depth instanceof RenderDepthTexture` in `GLRenderTarget.ts` always return `false`, so test with depthTexture in renderTarget is ignored.

import { WebGLEngine } from "../../../rhi-webgl/src/WebGLEngine";
import { Texture2D, RenderTarget, TextureFormat,TextureCube } from "../../src/texture";
import { RenderBufferDepthFormat, RenderTarget, Texture2D } from "../../src/texture";

describe("RenderTarget", () => {
const width = 1024;
Expand Down Expand Up @@ -72,14 +72,14 @@ describe("RenderTarget", () => {
it("创建失败-不支持高精度深度缓冲", () => {
expect(() => {
rhi.canIUse.mockReturnValueOnce(false);
new RenderTarget(engine, width, height, renderColorTexture, TextureFormat.Depth32);
new RenderTarget(engine, width, height, renderColorTexture, RenderBufferDepthFormat.Depth32);
}).toThrow();
});

it("创建失败-不支持高精度深度模版缓冲", () => {
expect(() => {
rhi.canIUse.mockReturnValueOnce(false);
new RenderTarget(engine, width, height, renderColorTexture, TextureFormat.Depth32Stencil8);
new RenderTarget(engine, width, height, renderColorTexture, RenderBufferDepthFormat.Depth32Stencil8);
}).toThrow();
});

Expand Down
5 changes: 1 addition & 4 deletions packages/math/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@
"files": [
"dist/**/*",
"types/**/*"
],
"devDependencies": {
"@oasis-engine/design": "0.6.7"
}
]
}
8 changes: 6 additions & 2 deletions packages/rhi-webgl/src/GLTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export class GLTexture implements IPlatformTexture {
* @internal
*/
static _getRenderBufferDepthFormatDetail(
format: TextureFormat,
format: TextureFormat | RenderBufferDepthFormat,
gl: WebGLRenderingContext & WebGL2RenderingContext,
isWebGL2: boolean
): TextureFormatDetail {
Expand Down Expand Up @@ -325,7 +325,11 @@ export class GLTexture implements IPlatformTexture {
/**
* @internal
*/
static _supportRenderBufferDepthFormat(format: TextureFormat, rhi: WebGLRenderer, isTexture: boolean): boolean {
static _supportRenderBufferDepthFormat(
format: TextureFormat | RenderBufferDepthFormat,
rhi: WebGLRenderer,
isTexture: boolean
): boolean {
const isWebGL2: boolean = rhi.isWebGL2;
let isSupported = true;

Expand Down

0 comments on commit 0a938c6

Please sign in to comment.