Skip to content

Add stopped signal #542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/scratchcpp/iengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ class LIBSCRATCHCPP_EXPORT IEngine
/*! Emits when a script is about to stop. */
virtual sigslot::signal<VirtualMachine *> &threadAboutToStop() = 0;

/*! Emits when the project is stopped by calling stop(). */
virtual sigslot::signal<> &stopped() = 0;

/*! Returns true if the project is currently running. */
virtual bool isRunning() const = 0;

Expand Down
7 changes: 7 additions & 0 deletions src/engine/internal/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ void Engine::stop()
m_threads.clear();
m_running = false;
}

m_stopped();
}

VirtualMachine *Engine::startScript(std::shared_ptr<Block> topLevelBlock, Target *target)
Expand Down Expand Up @@ -565,6 +567,11 @@ sigslot::signal<VirtualMachine *> &Engine::threadAboutToStop()
return m_threadAboutToStop;
}

sigslot::signal<> &Engine::stopped()
{
return m_stopped;
}

std::vector<std::shared_ptr<VirtualMachine>> Engine::stepThreads()
{
// https://github.com/scratchfoundation/scratch-vm/blob/develop/src/engine/sequencer.js#L70-L173
Expand Down
2 changes: 2 additions & 0 deletions src/engine/internal/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Engine : public IEngine

sigslot::signal<> &aboutToRender() override;
sigslot::signal<VirtualMachine *> &threadAboutToStop() override;
sigslot::signal<> &stopped() override;

bool isRunning() const override;

Expand Down Expand Up @@ -268,6 +269,7 @@ class Engine : public IEngine
bool m_redrawRequested = false;
sigslot::signal<> m_aboutToRedraw;
sigslot::signal<VirtualMachine *> m_threadAboutToStop;
sigslot::signal<> m_stopped;
bool m_stopEventLoop = false;
std::mutex m_stopEventLoopMutex;

Expand Down
67 changes: 51 additions & 16 deletions test/engine/engine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ class RedrawMock
MOCK_METHOD(void, redraw, ());
};

class ThreadAboutToStopMock
class StopMock
{
public:
MOCK_METHOD(void, threadRemoved, (VirtualMachine *));
MOCK_METHOD(void, stopped, ());
};

class AddRemoveMonitorMock
Expand Down Expand Up @@ -124,13 +125,13 @@ TEST(EngineTest, ClearThreadAboutToStopSignal)
engine->start();
engine->step();

ThreadAboutToStopMock threadRemovedMock;
EXPECT_CALL(threadRemovedMock, threadRemoved(_)).Times(3).WillRepeatedly(WithArgs<0>(Invoke([](VirtualMachine *vm) {
StopMock stopMock;
EXPECT_CALL(stopMock, threadRemoved(_)).Times(3).WillRepeatedly(WithArgs<0>(Invoke([](VirtualMachine *vm) {
ASSERT_TRUE(vm);
ASSERT_FALSE(vm->atEnd());
})));

engine->threadAboutToStop().connect(&ThreadAboutToStopMock::threadRemoved, &threadRemovedMock);
engine->threadAboutToStop().connect(&StopMock::threadRemoved, &stopMock);
engine->clear();
}

Expand All @@ -143,16 +144,50 @@ TEST(EngineTest, StopThreadAboutToStopSignal)
engine->start();
engine->step();

ThreadAboutToStopMock threadRemovedMock;
EXPECT_CALL(threadRemovedMock, threadRemoved(_)).Times(3).WillRepeatedly(WithArgs<0>(Invoke([](VirtualMachine *vm) {
StopMock stopMock;
EXPECT_CALL(stopMock, threadRemoved(_)).Times(3).WillRepeatedly(WithArgs<0>(Invoke([](VirtualMachine *vm) {
ASSERT_TRUE(vm);
ASSERT_FALSE(vm->atEnd());
})));

engine->threadAboutToStop().connect(&ThreadAboutToStopMock::threadRemoved, &threadRemovedMock);
engine->threadAboutToStop().connect(&StopMock::threadRemoved, &stopMock);
engine->stop();
}

TEST(EngineTest, StopSignal)
{
StopMock stopMock;

{
Project p("3_threads.sb3");
ASSERT_TRUE(p.load());
auto engine = p.engine();

engine->stopped().connect(&StopMock::stopped, &stopMock);
EXPECT_CALL(stopMock, stopped());
engine->start();
engine->step();

EXPECT_CALL(stopMock, stopped());
engine->stop();
}

{
Project p("stop_all_bypass.sb3");
ASSERT_TRUE(p.load());
auto engine = p.engine();

engine->stopped().connect(&StopMock::stopped, &stopMock);
EXPECT_CALL(stopMock, stopped());
engine->start();
EXPECT_CALL(stopMock, stopped());
engine->step();

EXPECT_CALL(stopMock, stopped());
engine->stop();
}
}

TEST(EngineTest, CompileAndExecuteMonitors)
{
Engine engine;
Expand Down Expand Up @@ -1894,13 +1929,13 @@ TEST(EngineTest, BroadcastsProject)

auto engine = p.engine();

ThreadAboutToStopMock threadRemovedMock;
EXPECT_CALL(threadRemovedMock, threadRemoved(_)).Times(21).WillRepeatedly(WithArgs<0>(Invoke([](VirtualMachine *vm) {
StopMock stopMock;
EXPECT_CALL(stopMock, threadRemoved(_)).Times(21).WillRepeatedly(WithArgs<0>(Invoke([](VirtualMachine *vm) {
ASSERT_TRUE(vm);
ASSERT_FALSE(vm->atEnd());
})));

engine->threadAboutToStop().connect(&ThreadAboutToStopMock::threadRemoved, &threadRemovedMock);
engine->threadAboutToStop().connect(&StopMock::threadRemoved, &stopMock);
engine->setFps(1000);
p.run();

Expand Down Expand Up @@ -1942,13 +1977,13 @@ TEST(EngineTest, StopAllBypass)

auto engine = p.engine();

ThreadAboutToStopMock threadRemovedMock;
EXPECT_CALL(threadRemovedMock, threadRemoved(_)).Times(2).WillRepeatedly(WithArgs<0>(Invoke([](VirtualMachine *vm) {
StopMock stopMock;
EXPECT_CALL(stopMock, threadRemoved(_)).Times(2).WillRepeatedly(WithArgs<0>(Invoke([](VirtualMachine *vm) {
ASSERT_TRUE(vm);
ASSERT_FALSE(vm->atEnd());
})));

engine->threadAboutToStop().connect(&ThreadAboutToStopMock::threadRemoved, &threadRemovedMock);
engine->threadAboutToStop().connect(&StopMock::threadRemoved, &stopMock);
p.run();

Stage *stage = engine->stage();
Expand All @@ -1970,13 +2005,13 @@ TEST(EngineTest, StopOtherScriptsInSprite)

auto engine = p.engine();

ThreadAboutToStopMock threadRemovedMock;
EXPECT_CALL(threadRemovedMock, threadRemoved(_)).Times(4).WillRepeatedly(WithArgs<0>(Invoke([](VirtualMachine *vm) {
StopMock stopMock;
EXPECT_CALL(stopMock, threadRemoved(_)).Times(4).WillRepeatedly(WithArgs<0>(Invoke([](VirtualMachine *vm) {
ASSERT_TRUE(vm);
ASSERT_FALSE(vm->atEnd());
})));

engine->threadAboutToStop().connect(&ThreadAboutToStopMock::threadRemoved, &threadRemovedMock);
engine->threadAboutToStop().connect(&StopMock::threadRemoved, &stopMock);
p.run();

Stage *stage = engine->stage();
Expand Down
1 change: 1 addition & 0 deletions test/mocks/enginemock.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class EngineMock : public IEngine

MOCK_METHOD(sigslot::signal<> &, aboutToRender, (), (override));
MOCK_METHOD(sigslot::signal<VirtualMachine *> &, threadAboutToStop, (), (override));
MOCK_METHOD(sigslot::signal<> &, stopped, (), (override));

MOCK_METHOD(bool, isRunning, (), (const, override));

Expand Down