diff --git a/libs/RobotKit/include/StateMachine.h b/libs/RobotKit/include/StateMachine.h index 51b102010a..ca8cf776c7 100644 --- a/libs/RobotKit/include/StateMachine.h +++ b/libs/RobotKit/include/StateMachine.h @@ -105,7 +105,8 @@ struct StateMachine { return make_transition_table( // clang-format off - * sm::state::setup + event = sm::state::idle + * sm::state::setup + event [sm::guard::is_not_charging {}] = sm::state::idle + , sm::state::setup + event [sm::guard::is_charging {}] = sm::state::charging , sm::state::setup + boost::sml::on_exit<_> / sm::action::run_launching_behavior {} , sm::state::idle + boost::sml::on_entry<_> / (sm::action::start_sleep_timeout {}, sm::action::start_waiting_behavior {}) diff --git a/libs/RobotKit/tests/RobotController_test.cpp b/libs/RobotKit/tests/RobotController_test.cpp index 4a6732a871..bc07dac2aa 100644 --- a/libs/RobotKit/tests/RobotController_test.cpp +++ b/libs/RobotKit/tests/RobotController_test.cpp @@ -56,6 +56,7 @@ class RobotControllerTest : public testing::Test EXPECT_CALL(sleep_timeout, onTimeout).WillOnce(GetCallback(&on_sleep_timeout)); EXPECT_CALL(battery, onChargeDidStart).WillOnce(GetCallback>(&on_charge_did_start)); EXPECT_CALL(battery, onChargeDidStop).WillOnce(GetCallback>(&on_charge_did_stop)); + EXPECT_CALL(battery, isCharging).WillOnce(Return(false)); EXPECT_CALL(sleep_timeout, start).Times(1); // Hide Uninteresting mock function call rc.registerEvents(); @@ -139,7 +140,7 @@ TEST_F(RobotControllerTest, onStartChargingBehaviorLevelAt100) // nohting expected } -TEST_F(RobotControllerTest, stateSetupEventSetupComplete) +TEST_F(RobotControllerTest, stateSetupEventSetupCompleteGuardIsChargingFalse) { ble::GapMock &mbed_mock_gap = ble::gap_mock(); ble::GattServerMock &mbed_mock_gatt = ble::gatt_server_mock(); @@ -153,6 +154,7 @@ TEST_F(RobotControllerTest, stateSetupEventSetupComplete) EXPECT_CALL(sleep_timeout, onTimeout).Times(1); EXPECT_CALL(battery, onChargeDidStart).Times(1); EXPECT_CALL(battery, onChargeDidStop).Times(1); + EXPECT_CALL(battery, isCharging).WillOnce(Return(false)); EXPECT_CALL(sleep_timeout, start).Times(1); rc.registerEvents(); @@ -160,6 +162,27 @@ TEST_F(RobotControllerTest, stateSetupEventSetupComplete) EXPECT_TRUE(rc.state_machine.is(lksm::state::idle)); } +TEST_F(RobotControllerTest, stateSetupEventSetupCompleteGuardIsChargingTrue) +{ + ble::GapMock &mbed_mock_gap = ble::gap_mock(); + ble::GattServerMock &mbed_mock_gatt = ble::gatt_server_mock(); + + rc.state_machine.set_current_states(lksm::state::setup); + + auto expected_level = 0x2A; + EXPECT_CALL(battery, level).WillOnce(Return(expected_level)); + EXPECT_CALL(mbed_mock_gatt, write(_, sameValue(expected_level), _, _)).Times(1); + + EXPECT_CALL(sleep_timeout, onTimeout).Times(1); + EXPECT_CALL(battery, onChargeDidStart).Times(1); + EXPECT_CALL(battery, onChargeDidStop).Times(1); + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(true)); + + rc.registerEvents(); + + EXPECT_TRUE(rc.state_machine.is(lksm::state::charging)); +} + TEST_F(RobotControllerTest, stateIdleEventTimeout) { rc.state_machine.set_current_states(lksm::state::idle); diff --git a/libs/RobotKit/tests/StateMachine_test.cpp b/libs/RobotKit/tests/StateMachine_test.cpp index 061eeebe51..d5ad9913c1 100644 --- a/libs/RobotKit/tests/StateMachine_test.cpp +++ b/libs/RobotKit/tests/StateMachine_test.cpp @@ -51,11 +51,12 @@ TEST_F(StateMachineTest, initialState) EXPECT_FALSE(sm.is(lksm::state::charging)); } -TEST_F(StateMachineTest, stateSetupEventSetupComplete) +TEST_F(StateMachineTest, stateSetupEventSetupCompleteGuardIsChargingFalse) { sm.set_current_states(lksm::state::setup); EXPECT_CALL(mock_rc, runLaunchingBehavior).Times(1); + EXPECT_CALL(mock_rc, isCharging).WillRepeatedly(Return(false)); EXPECT_CALL(mock_rc, startSleepTimeout).Times(1); EXPECT_CALL(mock_rc, startWaitingBehavior).Times(1); @@ -64,6 +65,19 @@ TEST_F(StateMachineTest, stateSetupEventSetupComplete) EXPECT_TRUE(sm.is(lksm::state::idle)); } +TEST_F(StateMachineTest, stateSetupEventSetupCompleteGuardIsChargingTrue) +{ + sm.set_current_states(lksm::state::setup); + + EXPECT_CALL(mock_rc, runLaunchingBehavior).Times(1); + EXPECT_CALL(mock_rc, isCharging).WillRepeatedly(Return(true)); + EXPECT_CALL(mock_rc, startChargingBehavior).Times(1); + + sm.process_event(lksm::event::setup_complete {}); + + EXPECT_TRUE(sm.is(lksm::state::charging)); +} + TEST_F(StateMachineTest, stateIdleEventTimeout) { sm.set_current_states(lksm::state::idle);