| #include "activity_watcher.h" | |
| #include <memory> | |
| #include "mock_listener.h" | |
| #include "system_state.pb.h" | |
| #include "state_updater.h" | |
| #include "gmock/gmock.h" | |
| #include "gtest/gtest.h" | |
| namespace safepower_agent { | |
| namespace { | |
| using ::safepower_agent_proto::SystemState; | |
| using ::testing::Mock; | |
| using ::testing::MockFunction; | |
| using ::testing::NiceMock; | |
| TEST(ActivityWatcherTest, NotifiesOnActiveAndIdleTransitions) { | |
| auto state_updater = std::make_shared<StateUpdater<SystemState>>(); | |
| MockFunction<void(bool)> on_change; | |
| EXPECT_CALL(on_change, Call).Times(0); | |
| ActivityWatcher<SystemState> watcher( | |
| state_updater, [&](bool is_active) { on_change.Call(is_active); }); | |
| Mock::VerifyAndClearExpectations(&on_change); | |
| NiceMock<MockStateUpdaterListener<SystemState>> listener1; | |
| EXPECT_CALL(on_change, Call(true)).Times(1); | |
| listener1.Listen(state_updater); | |
| Mock::VerifyAndClearExpectations(&on_change); | |
| NiceMock<MockStateUpdaterListener<SystemState>> listener2; | |
| EXPECT_CALL(on_change, Call).Times(0); | |
| listener2.Listen(state_updater); | |
| Mock::VerifyAndClearExpectations(&on_change); | |
| EXPECT_CALL(on_change, Call).Times(0); | |
| listener1.StopListening(); | |
| Mock::VerifyAndClearExpectations(&on_change); | |
| EXPECT_CALL(on_change, Call(false)).Times(1); | |
| listener2.StopListening(); | |
| Mock::VerifyAndClearExpectations(&on_change); | |
| EXPECT_CALL(on_change, Call(true)).Times(1); | |
| listener1.Listen(state_updater); | |
| Mock::VerifyAndClearExpectations(&on_change); | |
| watcher.Stop(); | |
| EXPECT_CALL(on_change, Call).Times(0); | |
| listener1.StopListening(); | |
| } | |
| TEST(ActivityWatcherTest, NotifiesImmediatelyIfAlreadyActive) { | |
| auto state_updater = std::make_shared<StateUpdater<SystemState>>(); | |
| NiceMock<MockStateUpdaterListener<SystemState>> listener; | |
| listener.Listen(state_updater); | |
| MockFunction<void(bool)> on_change; | |
| EXPECT_CALL(on_change, Call(true)).Times(1); | |
| ActivityWatcher<SystemState> watcher( | |
| state_updater, [&](bool is_active) { on_change.Call(is_active); }); | |
| Mock::VerifyAndClearExpectations(&on_change); | |
| EXPECT_CALL(on_change, Call(false)).Times(1); | |
| listener.StopListening(); | |
| } | |
| TEST(ActivityWatcherTest, IgnoresPassiveListeners) { | |
| auto state_updater = std::make_shared<StateUpdater<SystemState>>(); | |
| MockFunction<void(bool)> on_change; | |
| EXPECT_CALL(on_change, Call).Times(0); | |
| ActivityWatcher<SystemState> watcher( | |
| state_updater, [&](bool is_active) { on_change.Call(is_active); }); | |
| NiceMock<MockStateUpdaterListener<SystemState>> passive_listener(true); | |
| passive_listener.Listen(state_updater); | |
| Mock::VerifyAndClearExpectations(&on_change); | |
| NiceMock<MockStateUpdaterListener<SystemState>> active_listener; | |
| EXPECT_CALL(on_change, Call(true)).Times(1); | |
| active_listener.Listen(state_updater); | |
| Mock::VerifyAndClearExpectations(&on_change); | |
| // Detaching the only active listener transitions to idle even while a passive | |
| // listener remains attached. | |
| EXPECT_CALL(on_change, Call(false)).Times(1); | |
| active_listener.StopListening(); | |
| Mock::VerifyAndClearExpectations(&on_change); | |
| EXPECT_CALL(on_change, Call).Times(0); | |
| passive_listener.StopListening(); | |
| } | |
| TEST(ActivityWatcherTest, StopWhileIdlePreventsActivation) { | |
| auto state_updater = std::make_shared<StateUpdater<SystemState>>(); | |
| MockFunction<void(bool)> on_change; | |
| EXPECT_CALL(on_change, Call).Times(0); | |
| ActivityWatcher<SystemState> watcher( | |
| state_updater, [&](bool is_active) { on_change.Call(is_active); }); | |
| watcher.Stop(); | |
| watcher.Stop(); // Idempotent. | |
| NiceMock<MockStateUpdaterListener<SystemState>> listener; | |
| listener.Listen(state_updater); | |
| listener.StopListening(); | |
| } | |
| TEST(ActivityWatcherTest, DestructorStopsWatchingWithoutExplicitStop) { | |
| auto state_updater = std::make_shared<StateUpdater<SystemState>>(); | |
| MockFunction<void(bool)> on_change; | |
| EXPECT_CALL(on_change, Call).Times(0); | |
| { | |
| ActivityWatcher<SystemState> watcher( | |
| state_updater, [&](bool is_active) { on_change.Call(is_active); }); | |
| } | |
| NiceMock<MockStateUpdaterListener<SystemState>> listener; | |
| listener.Listen(state_updater); | |
| listener.StopListening(); | |
| } | |
| } // namespace | |
| } // namespace safepower_agent |