Skip to content

Commit

Permalink
test: 添加 copy message plugin 测试
Browse files Browse the repository at this point in the history
test: 添加c onsole message plugin 测试
test: 添加 visibility message plugin 测试
refactor: 将 DBaseLive2dOptions 构造移动到 live2d 构造中
  • Loading branch information
wonder-light committed Mar 20, 2024
1 parent 00668c5 commit ba86645
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 22 deletions.
17 changes: 8 additions & 9 deletions lib/controller/live2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,14 @@ export class ULive2dController {
* @throws {Error} live2d options is null
*/
constructor(options) {
FHelp.isNotValid(options) && (options = defaultOptions);
this._data = options;
this._data = options = new DBaseLive2dOptions(FHelp.mergeAll(defaultOptions, options));
this._event = new EventEmitter();
this._ref = {};
// 控制器
this._stage = new UBaseStageController(this, options.selector);
this._model = new UBaseModelController(this, options.models);
this._tips = new UBaseTipsController(this, options.tips);
this._plugins = options.plugins?.filter(p => p && p.install) ?? [];
this._plugins = options.plugins.filter(p => p && p.install);
// 创建 app 实例
if (FHelp.isNotValid(PIXI?.Application)) {
throw Error('PIXI is not import');
Expand Down Expand Up @@ -170,10 +169,7 @@ export class ULive2dController {
* @static
*/
static create(options = null) {
return new ULive2dController(FHelp.mergeAll(
new DBaseLive2dOptions(defaultOptions),
options
));
return new ULive2dController(options);
}

/**
Expand All @@ -183,7 +179,10 @@ export class ULive2dController {
*/
installPlugin(...plugins) {
// 插件名称集合
const names = this.plugins.reduce((names, current) => names[current.name] = current.name, {});
const names = this.plugins.reduce((names, current) => {
names[current.name] = current.name;
return names;
}, {});
// 筛选出有效插件,并且按照优先级从高到低排列及执行
plugins = plugins.filter(plugin => FHelp.is(FBasePlugin, plugin) && !names[plugin.name])
.sort((prev, next) => next.priority - prev.priority);
Expand All @@ -203,7 +202,7 @@ export class ULive2dController {
for (const plugin of plugins) {
const index = this.plugins.indexOf(plugin);
if (index >= 0) {
plugin.uninstall(this);
plugin.uninstall?.(this);
this.plugins.splice(index, 1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/controller/stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class UBaseStageController extends UBaseController {
// 包装器
this.wrapper.style.backgroundColor = this.live2d.model.backgroundColor;
// 模型的过度时间
if (this.live2dData.transitionTime > 0) {
if (this.live2dData.transitionTime !== 500) {
this.canvas.style.setProperty('--live2d-duration', `${ this.live2dData.transitionTime }ms`);
}
// 提示框
Expand Down
1 change: 0 additions & 1 deletion lib/plugins/message/event/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export class FConsoleMessagePlugin extends FEventMessagePlugin {
// window.innerWidth 是html的展示的宽度, 如果没有打开控制台, innerWidth + scrollWidth = outerWidth
// 通知控制台已经打开
const open = this.isOpen();
console.log(this._open, open, window.outerWidth - window.innerWidth, window.outerHeight - window.innerHeight);
if (!this._open && open) {
await super.notify();
}
Expand Down
5 changes: 4 additions & 1 deletion test/jest/controller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const createLive2d = (options = null) => {
global.PIXI = PIXI;
global.ILive2DModel = ILive2DModel;
expect(() => live2d = new ULive2dController(options)).not.toThrow();
live2d.stage.canvas.style.setProperty('--live2d-duration', '1ms')
live2d.stage.wrapper.style.setProperty('--live2d-duration', '1ms')
live2d.stage.wrapper.style.setProperty('--live2d-tips-duration', '1ms')
// 先推进 10s 触发 startFade, 然后再调用 stopFade
jest.advanceTimersByTime(10000);
expect(() => live2d.tips.stopFade()).not.toThrow();
Expand Down Expand Up @@ -125,7 +128,7 @@ describe('UBaseStageController 单元测试', () => {
wlLive2d.event.emit(EEvent.init);
expect(initFun).toHaveBeenCalled();
expect(live2d).toHaveBeenCalledTimes(1);
expect(live2dData).toHaveBeenCalledTimes(5);
expect(live2dData).toHaveBeenCalledTimes(4);
expect(ref).toHaveBeenCalledTimes(1);
wlLive2d.data.fixed = false;
wlLive2d.data.transitionTime = 0;
Expand Down
72 changes: 64 additions & 8 deletions test/jest/plugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ULive2dController } from '../../lib/controller/index.js';
import { DBaseMessage } from '../../lib/models/index.js';
import { FBasePlugin, FBaseSwitchPlugin, FCapturePlugin, FDragPlugin, FHourMessagePlugin, FInfoPlugin, FMotionMessagePlugin, FNullMessagePlugin, FQuitPlugin, FSeasonsMessagePlugin, FSentenceMessagePlugin, FSwitchModulePlugin, FSwitchTexturePlugin, FTipsDragPlugin } from '../../lib/plugins/index.js';
import { FBasePlugin, FBaseSwitchPlugin, FCapturePlugin, FConsoleMessagePlugin, FCopyMessagePlugin, FDragPlugin, FHourMessagePlugin, FInfoPlugin, FMotionMessagePlugin, FNullMessagePlugin, FQuitPlugin, FSeasonsMessagePlugin, FSentenceMessagePlugin, FSwitchModulePlugin, FSwitchTexturePlugin, FTipsDragPlugin, FVisibilityMessagePlugin } from '../../lib/plugins/index.js';
import { EEvent, FHelp } from '../../lib/utils/index.js';
import val from './const/variable.js';

Expand Down Expand Up @@ -59,8 +59,9 @@ describe('plugins 测试', () => {
// 停止循环
live2d.tips.stopFade();
// 设置储持续时间
live2d.stage.canvas.style.setProperty('--live2d-duration', '1ms');
live2d.stage.wrapper.style.setProperty('--live2d-duration', '1ms');
live2d.stage.canvas.style.removeProperty('--live2d-duration');
live2d.stage.wrapper.style.setProperty('--live2d-tips-duration', '1ms');
// 卸载插件
live2d.uninstallPlugin(...live2d.plugins);
jest.runAllTimers();
Expand Down Expand Up @@ -111,13 +112,13 @@ describe('plugins 测试', () => {
expect(() => testPlugin(plugin, baseEnable)).not.toThrow();
expect(() => live2d.installPlugin(plugin)).not.toThrow();
// 测试拖拽
plugin._element.dispatchEvent(new MouseEvent('mousedown'));
let mouse = new MouseEvent('mousemove');
mouse.movementX = mouse.movementY = 12;
let mouse = new MouseEvent('mousedown', { screenX: 0, screenY: 0 });
plugin._element.dispatchEvent(mouse);
mouse.movementX = mouse.movementY = -12;
mouse = new MouseEvent('mousemove', { screenX: 12, screenY: 12 });
plugin._element.dispatchEvent(mouse);
mouse.movementX = mouse.movementY = 0;
mouse = new MouseEvent('mousemove', { screenX: -12, screenY: -12 });
plugin._element.dispatchEvent(mouse);
mouse = new MouseEvent('mousemove', { screenX: 0, screenY: 0 });
plugin._element.dispatchEvent(mouse);
plugin._element.dispatchEvent(new MouseEvent('mouseup'));
plugin._element.dispatchEvent(new TouchEvent('touchstart', { targetTouches: [plugin._element] }));
Expand Down Expand Up @@ -192,7 +193,7 @@ describe('plugins 测试', () => {
])('测试 $name', async ({ classes }) => {
const baseEnable = jest.spyOn(classes.prototype, 'isEnable', null).mockImplementation(() => enable);
let plugin = new classes;
let messages = Array.from({ length: 10 }, () => new DBaseMessage({ type: plugin['_type'] }));
let messages = Array.from({ length: 10 }, (v, k) => new DBaseMessage({ type: plugin['_type'], priority: k % 2 === 0 ? 2 : 10 }));
let message = messages[0];
live2d.tips.messages.splice(0, live2d.tips.messages.length);
live2d.tips.messages.push(...messages);
Expand Down Expand Up @@ -247,4 +248,59 @@ describe('plugins 测试', () => {
expect(plugin.isEnable()).toBeTrue();
expect(() => live2d.uninstallPlugin(plugin)).not.toThrow();
});

test('测试 FCopyMessagePlugin', () => {
const baseEnable = jest.spyOn(FCopyMessagePlugin.prototype, 'isEnable', null).mockImplementation(() => enable);
let plugin = new FCopyMessagePlugin;
let messages = Array.from({ length: 1 }).map((v, k) => new DBaseMessage({
type: 'event',
event: 'copy',
text: '你都复制了些什么呀,转载要记得加上出处哦!'
}));
live2d.tips.addMessage(...messages);
expect(() => live2d.installPlugin(plugin)).not.toThrow();
window.dispatchEvent(new Event('copy'));
jest.runAllTimers();
messages[0].text = ['你都复制了些什么呀,转载要记得加上出处哦!'];
window.dispatchEvent(new Event('copy'));
jest.runAllTimers();
let message = new DBaseMessage({
type: 'event'
});
expect(plugin.isType(message)).toBeFalse();
message.event = 'copy';
expect(plugin.isType(message)).toBeTrue();
expect(() => live2d.uninstallPlugin(plugin)).not.toThrow();
live2d.tips.removeMessage(...messages);
});

test('测试 FConsoleMessagePlugin', () => {
const baseEnable = jest.spyOn(FConsoleMessagePlugin.prototype, 'isEnable', null).mockImplementation(() => enable);
let plugin = new FConsoleMessagePlugin;
expect(() => testPlugin(plugin, baseEnable)).not.toThrow();
expect(() => live2d.installPlugin(plugin)).not.toThrow();
window.innerHeight = window.outerHeight / 2;
window.dispatchEvent(new Event('resize'));
jest.runAllTimers();
expect(() => live2d.uninstallPlugin(plugin)).not.toThrow();
});

test('测试 FVisibilityMessagePlugin', () => {
let plugin = new FVisibilityMessagePlugin;
const call = () => {
// 淡出然后淡入
live2d.stage.wrapper.classList.remove('live2d-opacity-1');
live2d.stage.wrapper.classList.add('live2d-opacity-0');
jest.runAllTimers();
live2d.stage.wrapper.classList.remove('live2d-opacity-0');
live2d.stage.wrapper.classList.add('live2d-opacity-1');
jest.runAllTimers();
};
expect(() => live2d.installPlugin(plugin)).not.toThrow();
// 首次
call();
// 隐藏再显示
call();
expect(() => live2d.uninstallPlugin(plugin)).not.toThrow();
});
});
4 changes: 2 additions & 2 deletions test/jest/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ describe('utils/help.js - FHelp - 单元测试', () => {
}*/
for (const str of ['none', 'floor', 'ceil']) {
expect(FHelp.random()).toBeWithin(0, 1.1);
expect(FHelp.random(10, 0, str)).toBeWithin(0, 11);
expect(FHelp.random(10, 0, str)).toBeWithin(10, 11);
expect(FHelp.random(0.56, 0.56, str)).toBeWithin(0, 1.1);
expect(FHelp.random(0, 10, str)).toBeWithin(10, 11);
expect(FHelp.random(0, 10, str)).toBeWithin(0, 11);
}
});

Expand Down

0 comments on commit ba86645

Please sign in to comment.