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