#include #include using namespace extra2d; // ============================================================================ // TimerModule 基础测试 // ============================================================================ TEST(TimerModule, BasicProperties) { TimerModule timer; // 测试基本属性 EXPECT_STREQ("Timer", timer.name()); EXPECT_EQ(ModuleType::Core, timer.type()); EXPECT_EQ(Pri::System, timer.priority()); } TEST(TimerModule, InitShutdown) { TimerModule timer; // 测试初始化和关闭 EXPECT_TRUE(timer.init()); timer.shutdown(); } TEST(TimerModule, TimeScale) { TimerModule timer; // 测试时间缩放默认值 EXPECT_FLOAT_EQ(1.0f, timer.getTimeScale()); // 测试设置时间缩放 timer.setTimeScale(2.0f); EXPECT_FLOAT_EQ(2.0f, timer.getTimeScale()); timer.setTimeScale(0.5f); EXPECT_FLOAT_EQ(0.5f, timer.getTimeScale()); // 测试边界值 timer.setTimeScale(0.0f); EXPECT_FLOAT_EQ(0.0f, timer.getTimeScale()); } // ============================================================================ // 一次性定时器测试 // ============================================================================ TEST(TimerModule, ScheduleOnce) { TimerModule timer; timer.init(); bool called = false; TimerId id = timer.scheduleOnce(0.1f, [&called]() { called = true; }); EXPECT_NE(INVALID_TIMER_ID, id); EXPECT_EQ(1, timer.getActiveCount()); // 模拟时间流逝(未到达触发时间) timer.update(0.05f); EXPECT_FALSE(called); EXPECT_EQ(1, timer.getActiveCount()); // 模拟时间流逝(到达触发时间) timer.update(0.05f); EXPECT_TRUE(called); EXPECT_EQ(0, timer.getActiveCount()); timer.shutdown(); } TEST(TimerModule, ScheduleOnceImmediate) { TimerModule timer; timer.init(); bool called = false; TimerId id = timer.scheduleOnce(0.0f, [&called]() { called = true; }); EXPECT_NE(INVALID_TIMER_ID, id); // 立即触发 timer.update(0.0f); EXPECT_TRUE(called); timer.shutdown(); } TEST(TimerModule, CancelTimer) { TimerModule timer; timer.init(); bool called = false; TimerId id = timer.scheduleOnce(0.1f, [&called]() { called = true; }); // 取消定时器 timer.cancel(id); // 模拟时间流逝 timer.update(0.2f); EXPECT_FALSE(called); EXPECT_EQ(0, timer.getActiveCount()); timer.shutdown(); } TEST(TimerModule, CancelInvalidTimer) { TimerModule timer; timer.init(); // 取消不存在的定时器不应崩溃 EXPECT_NO_THROW(timer.cancel(99999)); timer.shutdown(); } // ============================================================================ // 重复定时器测试 // ============================================================================ TEST(TimerModule, ScheduleRepeat) { TimerModule timer; timer.init(); int callCount = 0; TimerId id = timer.scheduleRepeat(0.1f, 3, [&callCount]() { callCount++; }); EXPECT_NE(INVALID_TIMER_ID, id); // 第一次触发 timer.update(0.1f); EXPECT_EQ(1, callCount); // 第二次触发 timer.update(0.1f); EXPECT_EQ(2, callCount); // 第三次触发 timer.update(0.1f); EXPECT_EQ(3, callCount); // 定时器应已自动移除 EXPECT_EQ(0, timer.getActiveCount()); timer.shutdown(); } TEST(TimerModule, ScheduleRepeatInfinite) { TimerModule timer; timer.init(); int callCount = 0; TimerId id = timer.scheduleRepeat(0.1f, 0, [&callCount]() { callCount++; }); // 触发多次 for (int i = 0; i < 10; i++) { timer.update(0.1f); } EXPECT_EQ(10, callCount); EXPECT_EQ(1, timer.getActiveCount()); // 取消无限定时器 timer.cancel(id); EXPECT_EQ(0, timer.getActiveCount()); timer.shutdown(); } // ============================================================================ // 每帧更新定时器测试 // ============================================================================ TEST(TimerModule, ScheduleUpdate) { TimerModule timer; timer.init(); float totalDt = 0.0f; int callCount = 0; TimerId id = timer.scheduleUpdate([&](float dt) { totalDt += dt; callCount++; }); EXPECT_NE(INVALID_TIMER_ID, id); // 模拟多帧 timer.update(0.016f); timer.update(0.016f); timer.update(0.016f); EXPECT_EQ(3, callCount); EXPECT_FLOAT_EQ(0.048f, totalDt); // 取消更新定时器 timer.cancel(id); EXPECT_EQ(0, timer.getActiveCount()); timer.shutdown(); } // ============================================================================ // 暂停和恢复测试 // ============================================================================ TEST(TimerModule, PauseResume) { TimerModule timer; timer.init(); int callCount = 0; TimerId id = timer.scheduleRepeat(0.1f, 0, [&callCount]() { callCount++; }); // 正常触发一次 timer.update(0.1f); EXPECT_EQ(1, callCount); // 暂停 timer.pause(id); timer.update(0.1f); EXPECT_EQ(1, callCount); // 不应增加 // 恢复 timer.resume(id); timer.update(0.1f); EXPECT_EQ(2, callCount); timer.shutdown(); } TEST(TimerModule, PauseInvalidTimer) { TimerModule timer; timer.init(); // 暂停不存在的定时器不应崩溃 EXPECT_NO_THROW(timer.pause(99999)); EXPECT_NO_THROW(timer.resume(99999)); timer.shutdown(); } // ============================================================================ // 时间缩放测试 // ============================================================================ TEST(TimerModule, TimeScaleEffect) { TimerModule timer; timer.init(); int callCount = 0; timer.scheduleRepeat(1.0f, 0, [&callCount]() { callCount++; }); // 设置2倍速 timer.setTimeScale(2.0f); // 实际经过0.5秒,但效果相当于1秒 timer.update(0.5f); EXPECT_EQ(1, callCount); timer.shutdown(); } TEST(TimerModule, TimeScaleZero) { TimerModule timer; timer.init(); int callCount = 0; timer.scheduleRepeat(0.1f, 0, [&callCount]() { callCount++; }); // 暂停时间 timer.setTimeScale(0.0f); // 无论经过多少时间,都不会触发 timer.update(1.0f); timer.update(1.0f); EXPECT_EQ(0, callCount); // 恢复时间 timer.setTimeScale(1.0f); timer.update(0.1f); EXPECT_EQ(1, callCount); timer.shutdown(); } // ============================================================================ // 多定时器测试 // ============================================================================ TEST(TimerModule, MultipleTimers) { TimerModule timer; timer.init(); int count1 = 0, count2 = 0, count3 = 0; TimerId id1 = timer.scheduleOnce(0.1f, [&count1]() { count1++; }); TimerId id2 = timer.scheduleOnce(0.2f, [&count2]() { count2++; }); TimerId id3 = timer.scheduleOnce(0.3f, [&count3]() { count3++; }); EXPECT_EQ(3, timer.getActiveCount()); // 经过0.15秒 timer.update(0.15f); EXPECT_EQ(1, count1); EXPECT_EQ(0, count2); EXPECT_EQ(0, count3); EXPECT_EQ(2, timer.getActiveCount()); // 再经过0.1秒 timer.update(0.1f); EXPECT_EQ(1, count1); EXPECT_EQ(1, count2); EXPECT_EQ(0, count3); EXPECT_EQ(1, timer.getActiveCount()); // 再经过0.1秒 timer.update(0.1f); EXPECT_EQ(1, count1); EXPECT_EQ(1, count2); EXPECT_EQ(1, count3); EXPECT_EQ(0, timer.getActiveCount()); timer.shutdown(); } TEST(TimerModule, CancelAll) { TimerModule timer; timer.init(); int count = 0; timer.scheduleOnce(0.1f, [&count]() { count++; }); timer.scheduleOnce(0.2f, [&count]() { count++; }); timer.scheduleOnce(0.3f, [&count]() { count++; }); EXPECT_EQ(3, timer.getActiveCount()); // 取消所有 timer.cancelAll(); EXPECT_EQ(0, timer.getActiveCount()); // 确认都不会触发 timer.update(0.5f); EXPECT_EQ(0, count); timer.shutdown(); } // ============================================================================ // 边界条件测试 // ============================================================================ TEST(TimerModule, SelfCancelInCallback) { TimerModule timer; timer.init(); TimerId id = INVALID_TIMER_ID; bool called = false; // 在回调中取消自己 id = timer.scheduleOnce(0.1f, [&]() { called = true; timer.cancel(id); }); timer.update(0.1f); EXPECT_TRUE(called); timer.shutdown(); } TEST(TimerModule, ScheduleInCallback) { TimerModule timer; timer.init(); bool secondCalled = false; // 在回调中创建新定时器 timer.scheduleOnce(0.1f, [&]() { timer.scheduleOnce(0.1f, [&]() { secondCalled = true; }); }); timer.update(0.1f); EXPECT_FALSE(secondCalled); timer.update(0.1f); EXPECT_TRUE(secondCalled); timer.shutdown(); }