* -------------------------------------------------------------------------
* This file is part of the MindStudio project.
* Copyright (c) 2025 Huawei Technologies Co.,Ltd.
*
* MindStudio is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* -------------------------------------------------------------------------
*/
import { RenderEngine } from './index';
const RENDER_FREQUENCY = 16;
describe('RenderEngine', () => {
let renderEngine: RenderEngine;
beforeEach(() => {
jest.useFakeTimers();
renderEngine = new RenderEngine();
});
afterEach(() => {
jest.useRealTimers();
});
it('should start in running state', () => {
expect((renderEngine as any)._status).toBe('running');
});
it('should add and execute a once task', () => {
const mockAction = jest.fn();
const renderID = renderEngine.addTask(mockAction, 'once');
expect((renderEngine as any)._renderTasks.has(renderID)).toBe(true);
expect((renderEngine as any)._renderTasks.get(renderID).status).toBe('pending');
jest.advanceTimersByTime(RENDER_FREQUENCY);
expect(mockAction).toHaveBeenCalledTimes(1);
expect((renderEngine as any)._renderTasks.get(renderID).status).toBe('fullfilled');
});
it('should add and execute an always task', () => {
const mockAction = jest.fn();
const renderID = renderEngine.addTask(mockAction, 'always');
expect((renderEngine as any)._renderTasks.has(renderID)).toBe(true);
expect((renderEngine as any)._renderTasks.get(renderID).status).toBe('pending');
jest.advanceTimersByTime(RENDER_FREQUENCY);
expect(mockAction).toHaveBeenCalledTimes(1);
expect((renderEngine as any)._renderTasks.get(renderID).status).toBe('pending');
});
it('should delete a task', () => {
const mockAction = jest.fn();
const renderID = renderEngine.addTask(mockAction, 'always');
expect((renderEngine as any)._renderTasks.has(renderID)).toBe(true);
renderEngine.deleteTask(renderID);
expect((renderEngine as any)._renderTasks.has(renderID)).toBe(false);
});
it('should stop and start the engine', () => {
renderEngine.stop();
expect((renderEngine as any)._status).toBe('waiting');
renderEngine.start();
expect((renderEngine as any)._status).toBe('running');
});
it('should not execute tasks when stopped', () => {
const mockAction = jest.fn();
renderEngine.addTask(mockAction, 'always');
renderEngine.stop();
jest.advanceTimersByTime(RENDER_FREQUENCY);
expect(mockAction).toHaveBeenCalledTimes(1);
jest.advanceTimersByTime(RENDER_FREQUENCY);
expect(mockAction).toHaveBeenCalledTimes(1);
});
it('should execute tasks periodically when running', () => {
const mockAction = jest.fn();
renderEngine.addTask(mockAction, 'always');
jest.advanceTimersByTime(RENDER_FREQUENCY);
expect(mockAction).toHaveBeenCalledTimes(1);
jest.advanceTimersByTime(RENDER_FREQUENCY);
expect(mockAction).toHaveBeenCalledTimes(2);
});
});