@@ -108,6 +108,7 @@ TEST_F(LooksBlocksTest, RegisterBlocks)
108108 // Blocks
109109 EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " looks_sayforsecs" , &LooksBlocks::compileSayForSecs));
110110 EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " looks_say" , &LooksBlocks::compileSay));
111+ EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " looks_thinkforsecs" , &LooksBlocks::compileThinkForSecs));
111112 EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " looks_show" , &LooksBlocks::compileShow));
112113 EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " looks_hide" , &LooksBlocks::compileHide));
113114 EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " looks_changeeffectby" , &LooksBlocks::compileChangeEffectBy));
@@ -370,6 +371,166 @@ TEST_F(LooksBlocksTest, SayImpl)
370371 ASSERT_EQ (target.bubbleText (), " test" );
371372}
372373
374+ TEST_F (LooksBlocksTest, ThinkForSecs)
375+ {
376+ Compiler compiler (&m_engineMock);
377+
378+ // think "Hmm..." for 3.5 seconds
379+ auto block = std::make_shared<Block>(" a" , " looks_thinkforsecs" );
380+ addValueInput (block, " MESSAGE" , LooksBlocks::MESSAGE, " Hmm..." );
381+ addValueInput (block, " SECS" , LooksBlocks::SECS, 3.5 );
382+
383+ EXPECT_CALL (m_engineMock, functionIndex (&LooksBlocks::startThinkForSecs)).WillOnce (Return (0 ));
384+ EXPECT_CALL (m_engineMock, functionIndex (&LooksBlocks::thinkForSecs)).WillOnce (Return (1 ));
385+
386+ compiler.init ();
387+ compiler.setBlock (block);
388+ LooksBlocks::compileThinkForSecs (&compiler);
389+ compiler.end ();
390+
391+ ASSERT_EQ (compiler.bytecode (), std::vector<unsigned int >({ vm::OP_START, vm::OP_CONST, 0 , vm::OP_CONST, 1 , vm::OP_EXEC, 0 , vm::OP_EXEC, 1 , vm::OP_HALT }));
392+ ASSERT_EQ (compiler.constValues (), std::vector<Value>({ " Hmm..." , 3.5 }));
393+ }
394+
395+ TEST_F (LooksBlocksTest, ThinkForSecsImpl)
396+ {
397+ static unsigned int bytecode1[] = { vm::OP_START, vm::OP_CONST, 0 , vm::OP_CONST, 1 , vm::OP_EXEC, 0 , vm::OP_EXEC, 1 , vm::OP_HALT };
398+ // static unsigned int bytecode2[] = { vm::OP_START, vm::OP_CONST, 2, vm::OP_EXEC, 2, vm::OP_HALT };
399+ static unsigned int bytecode3[] = { vm::OP_START, vm::OP_CONST, 2 , vm::OP_CONST, 1 , vm::OP_EXEC, 0 , vm::OP_EXEC, 1 , vm::OP_HALT };
400+ static BlockFunc functions[] = { &LooksBlocks::startThinkForSecs, &LooksBlocks::thinkForSecs /* , &LooksBlocks::think*/ };
401+ static Value constValues[] = { " test" , 5.5 , " hello" };
402+
403+ Target target;
404+ target.setBubbleType (Target::BubbleType::Say);
405+ VirtualMachine vm (&target, &m_engineMock, nullptr );
406+ vm.setFunctions (functions);
407+ vm.setConstValues (constValues);
408+ vm.setBytecode (bytecode1);
409+
410+ ClockMock clock;
411+ LooksBlocks::clock = &clock;
412+
413+ std::chrono::steady_clock::time_point startTime (std::chrono::milliseconds (1000 ));
414+ EXPECT_CALL (clock, currentSteadyTime ()).Times (2 ).WillRepeatedly (Return (startTime));
415+ EXPECT_CALL (m_engineMock, requestRedraw ());
416+ vm.run ();
417+
418+ ASSERT_EQ (vm.registerCount (), 0 );
419+ ASSERT_TRUE (LooksBlocks::m_timeMap.find (&vm) != LooksBlocks::m_timeMap.cend ());
420+ ASSERT_FALSE (vm.atEnd ());
421+ ASSERT_EQ (target.bubbleType (), Target::BubbleType::Think);
422+ ASSERT_EQ (target.bubbleText (), " test" );
423+
424+ std::chrono::steady_clock::time_point time1 (std::chrono::milliseconds (6450 ));
425+ EXPECT_CALL (clock, currentSteadyTime ()).WillOnce (Return (time1));
426+ target.setBubbleType (Target::BubbleType::Say);
427+ target.setBubbleText (" another" );
428+ vm.run ();
429+
430+ ASSERT_EQ (vm.registerCount (), 0 );
431+ ASSERT_TRUE (LooksBlocks::m_timeMap.find (&vm) != LooksBlocks::m_timeMap.cend ());
432+ ASSERT_FALSE (vm.atEnd ());
433+ ASSERT_EQ (target.bubbleType (), Target::BubbleType::Say);
434+ ASSERT_EQ (target.bubbleText (), " another" );
435+
436+ std::chrono::steady_clock::time_point time2 (std::chrono::milliseconds (6500 ));
437+ EXPECT_CALL (clock, currentSteadyTime ()).WillOnce (Return (time2));
438+ vm.run ();
439+
440+ ASSERT_EQ (vm.registerCount (), 0 );
441+ ASSERT_TRUE (LooksBlocks::m_timeMap.find (&vm) == LooksBlocks::m_timeMap.cend ());
442+ ASSERT_FALSE (vm.atEnd ());
443+ ASSERT_EQ (target.bubbleType (), Target::BubbleType::Say);
444+ ASSERT_TRUE (target.bubbleText ().empty ());
445+
446+ vm.run ();
447+
448+ ASSERT_EQ (vm.registerCount (), 0 );
449+ ASSERT_TRUE (LooksBlocks::m_timeMap.find (&vm) == LooksBlocks::m_timeMap.cend ());
450+ ASSERT_TRUE (vm.atEnd ());
451+ ASSERT_EQ (target.bubbleType (), Target::BubbleType::Say);
452+ ASSERT_TRUE (target.bubbleText ().empty ());
453+
454+ // Run the say block while waiting
455+ /* VirtualMachine vm2(&target, &m_engineMock, nullptr);
456+ vm2.setFunctions(functions);
457+ vm2.setConstValues(constValues);
458+ vm2.setBytecode(bytecode2);
459+
460+ EXPECT_CALL(clock, currentSteadyTime()).Times(2).WillRepeatedly(Return(startTime));
461+ EXPECT_CALL(m_engineMock, requestRedraw());
462+ vm.reset();
463+ vm.run();
464+
465+ ASSERT_EQ(vm.registerCount(), 0);
466+ ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm) != LooksBlocks::m_timeMap.cend());
467+ ASSERT_FALSE(vm.atEnd());
468+ ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
469+ ASSERT_EQ(target.bubbleText(), "test");
470+
471+ vm2.run();
472+ ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
473+ ASSERT_EQ(target.bubbleText(), "hello");
474+
475+ EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(time2));
476+ vm.run();
477+
478+ ASSERT_EQ(vm.registerCount(), 0);
479+ ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm) == LooksBlocks::m_timeMap.cend());
480+ ASSERT_FALSE(vm.atEnd());
481+ ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
482+ ASSERT_EQ(target.bubbleText(), "hello");
483+
484+ vm.run();
485+
486+ ASSERT_EQ(vm.registerCount(), 0);
487+ ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm) == LooksBlocks::m_timeMap.cend());
488+ ASSERT_TRUE(vm.atEnd());
489+ ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
490+ ASSERT_EQ(target.bubbleText(), "hello");
491+
492+ // Run the say for secs block while waiting
493+ vm2.reset();
494+ vm2.setBytecode(bytecode3);
495+
496+ EXPECT_CALL(clock, currentSteadyTime()).Times(2).WillRepeatedly(Return(startTime));
497+ EXPECT_CALL(m_engineMock, requestRedraw());
498+ vm2.reset();
499+ vm2.run();
500+
501+ ASSERT_EQ(vm2.registerCount(), 0);
502+ ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm2) != LooksBlocks::m_timeMap.cend());
503+ ASSERT_FALSE(vm2.atEnd());
504+ ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
505+ ASSERT_EQ(target.bubbleText(), "hello");
506+
507+ EXPECT_CALL(clock, currentSteadyTime()).Times(2).WillRepeatedly(Return(startTime));
508+ EXPECT_CALL(m_engineMock, requestRedraw());
509+ vm.reset();
510+ vm.run();
511+ ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
512+ ASSERT_EQ(target.bubbleText(), "test");
513+
514+ EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(time2));
515+ vm2.run();
516+
517+ ASSERT_EQ(vm2.registerCount(), 0);
518+ ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm2) == LooksBlocks::m_timeMap.cend());
519+ ASSERT_FALSE(vm2.atEnd());
520+ ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
521+ ASSERT_EQ(target.bubbleText(), "test");
522+
523+ vm2.run();
524+
525+ ASSERT_EQ(vm2.registerCount(), 0);
526+ ASSERT_TRUE(LooksBlocks::m_timeMap.find(&vm2) == LooksBlocks::m_timeMap.cend());
527+ ASSERT_TRUE(vm2.atEnd());
528+ ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);
529+ ASSERT_EQ(target.bubbleText(), "test");*/
530+
531+ LooksBlocks::clock = Clock::instance ().get ();
532+ }
533+
373534TEST_F (LooksBlocksTest, Show)
374535{
375536 Compiler compiler (&m_engineMock);
0 commit comments