Skip to content

Commit

Permalink
modify unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
zxx43 committed Sep 13, 2023
1 parent 97d6c65 commit decd412
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 24 deletions.
28 changes: 15 additions & 13 deletions cocos/tween/tween-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,19 @@ export class TweenAction extends ActionInterval {
prop.end.w = value.w;
}
prop.type = 'rect';
} else if (_t instanceof Quat) {
if (prop.start == null) {
prop.start = new Quat(); prop.current = new Quat(); prop.end = new Quat();
}
prop.start.set(_t);
prop.current.set(_t);
if (relative) {
Quat.multiply(prop.end, _t, value);
} else {
prop.end.set(value);
}
prop.type = 'quat';
warn('Quaternion only support slerp interpolation method.');
} else if (typeof _t === 'object') {
if (_t instanceof Vec2) {
if (prop.start == null) {
Expand All @@ -234,19 +247,14 @@ export class TweenAction extends ActionInterval {
prop.start = new Size(); prop.current = new Size(); prop.end = new Size();
}
prop.type = 'size';
} else if (_t instanceof Quat) {
if (prop.start == null) {
prop.start = new Quat(); prop.current = new Quat(); prop.end = new Quat();
}
prop.type = 'quat';
} else if (prop.start == null) {
prop.start = {}; prop.current = {}; prop.end = {};
}

for (const k in value) {
// filtering if it not a number
// eslint-disable-next-line no-restricted-globals, @typescript-eslint/no-unsafe-argument
if (isNaN(_t[k])) continue;
// eslint-disable-next-line no-restricted-globals
if (isNaN(_t[k] as number)) continue;
prop.start[k] = _t[k];
prop.current[k] = _t[k];
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
Expand Down Expand Up @@ -309,12 +317,6 @@ export class TweenAction extends ActionInterval {
Quat.slerp(prop.current, start, end, time as number);
} else {
for (const k in start) {
// if (value[k].easing) {
// time = value[k].easing(t);
// }
// if (value[k].progress) {
// interpolation = value[k].easing(t);
// }
prop.current[k] = interpolation(start[k], end[k], prop.current[k], time);
}
}
Expand Down
71 changes: 60 additions & 11 deletions tests/tween/tween.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,128 +44,177 @@ test('destroySelf', function () {
});

test('type color', function () {
const sys = new TweenSystem();
(TweenSystem.instance as any) = sys;
director.registerSystem(TweenSystem.ID, sys, System.Priority.MEDIUM);

const shadow = new Shadows();
shadow.shadowColor.set(200, 100, 50, 250);
const target = Color.TRANSPARENT;
const tweenact = tween(shadow).to(1, {shadowColor: target});
tweenact.start();

for (let i = 0; i < 100; ++i) {
game.step();
}
// @ts-expect-error access private property
const action = tweenact._actions[0] as TweenAction;
action.update(1.0);
// @ts-expect-error access private property
const props = action._props;
for (const property in props) {
const prop = props[property];
expect(prop.current instanceof Color).toBeTruthy();
expect(Color.equals(prop.current, target)).toBeTruthy();
}
director.unregisterSystem(sys);
});

test('type quat', function () {
const sys = new TweenSystem();
(TweenSystem.instance as any) = sys;
director.registerSystem(TweenSystem.ID, sys, System.Priority.MEDIUM);

const node = new Node();
const target = new Quat(1, 1, 1, 1);
const tweenact = tween(node).to(1, {rotation: target});
tweenact.start();

for (let i = 0; i < 100; ++i) {
game.step();
}
// @ts-expect-error access private property
const action = tweenact._actions[0] as TweenAction;
action.update(1.0);
// @ts-expect-error access private property
const props = action._props;
for (const property in props) {
const prop = props[property];
expect(prop.current instanceof Quat).toBeTruthy();
expect(Quat.equals(prop.current, target)).toBeTruthy();
}
director.unregisterSystem(sys);
});

test('type rect', function () {
const sys = new TweenSystem();
(TweenSystem.instance as any) = sys;
director.registerSystem(TweenSystem.ID, sys, System.Priority.MEDIUM);

const camera = new CameraComponent();
const target = new Rect(1, 1, 10, 20);
const tweenact = tween(camera).to(1, {rect: target});
tweenact.start();

for (let i = 0; i < 100; ++i) {
game.step();
}
// @ts-expect-error access private property
const action = tweenact._actions[0] as TweenAction;
action.update(1.0);
// @ts-expect-error access private property
const props = action._props;
for (const property in props) {
const prop = props[property];
expect(prop.current instanceof Rect).toBeTruthy();
expect(Rect.equals(prop.current, target)).toBeTruthy();
}
}
director.unregisterSystem(sys);
});

test('type size', function () {
const sys = new TweenSystem();
(TweenSystem.instance as any) = sys;
director.registerSystem(TweenSystem.ID, sys, System.Priority.MEDIUM);

const rect = new Rect();
const target = new Size(800, 600);
const tweenact = tween(rect).to(1, {size: target});
tweenact.start();

for (let i = 0; i < 100; ++i) {
game.step();
}
// @ts-expect-error access private property
const action = tweenact._actions[0] as TweenAction;
action.update(1.0);
// @ts-expect-error access private property
const props = action._props;
for (const property in props) {
const prop = props[property];
expect(prop.current instanceof Size).toBeTruthy();
expect(Rect.equals(prop.current, target)).toBeTruthy();
}
}
director.unregisterSystem(sys);
});

test('type vec2', function () {
const sys = new TweenSystem();
(TweenSystem.instance as any) = sys;
director.registerSystem(TweenSystem.ID, sys, System.Priority.MEDIUM);

const rect = new Rect();
const target = new Vec2(20, 10);
const tweenact = tween(rect).to(1, {origin: target});
tweenact.start();

for (let i = 0; i < 100; ++i) {
game.step();
}
// @ts-expect-error access private property
const action = tweenact._actions[0] as TweenAction;
action.update(1.0);
// @ts-expect-error access private property
const props = action._props;
for (const property in props) {
const prop = props[property];
expect(prop.current instanceof Vec2).toBeTruthy();
expect(Vec2.equals(prop.current, target)).toBeTruthy();
}
}
director.unregisterSystem(sys);
});

test('type vec3', function () {
const sys = new TweenSystem();
(TweenSystem.instance as any) = sys;
director.registerSystem(TweenSystem.ID, sys, System.Priority.MEDIUM);

const node = new Node();
const target = new Vec3(10, 20, 30);
const tweenact = tween(node).to(1, {position: target});
tweenact.start();

for (let i = 0; i < 100; ++i) {
game.step();
}
// @ts-expect-error access private property
const action = tweenact._actions[0] as TweenAction;
action.update(1.0);
// @ts-expect-error access private property
const props = action._props;
for (const property in props) {
const prop = props[property];
expect(prop.current instanceof Vec3).toBeTruthy();
expect(Vec3.equals(prop.current, target)).toBeTruthy();
}
director.unregisterSystem(sys);
});

test('type vec4', function () {
const sys = new TweenSystem();
(TweenSystem.instance as any) = sys;
director.registerSystem(TweenSystem.ID, sys, System.Priority.MEDIUM);

const shadowLayer = new CSMShadowLayer(4);
const target = new Vec4(10, 20, 30, 40);
const tweenact = tween(shadowLayer).to(1, {csmAtlas: target});
tweenact.start();

for (let i = 0; i < 100; ++i) {
game.step();
}
// @ts-expect-error access private property
const action = tweenact._actions[0] as TweenAction;
action.update(1.0);
// @ts-expect-error access private property
const props = action._props;
for (const property in props) {
const prop = props[property];
expect(prop.current instanceof Vec4).toBeTruthy();
expect(Vec4.equals(prop.current, target)).toBeTruthy();
}
}
director.unregisterSystem(sys);
});

0 comments on commit decd412

Please sign in to comment.