From 6db67f3a6690d60de1e25d9cffab14ee016d4b4d Mon Sep 17 00:00:00 2001 From: Yann Locatelli Date: Thu, 21 Dec 2023 15:23:33 +0100 Subject: [PATCH] :recycle: (rc): Simple UT behaviors, timeouts, hardware, raise, fileExchange, update, magicCard --- libs/RobotKit/CMakeLists.txt | 10 +- .../tests/RobotController_test_behaviors.cpp | 234 +++++++++++++ .../RobotController_test_fileExchange.cpp | 78 +++++ .../tests/RobotController_test_hardware.cpp | 61 ++++ .../tests/RobotController_test_magicCard.cpp | 313 ++++++++++++++++++ .../tests/RobotController_test_raise.cpp | 12 + .../RobotController_test_registerEvents.cpp | 64 ---- .../RobotController_test_stateConnected.cpp | 38 --- ...RobotController_test_stateDisconnected.cpp | 25 -- .../tests/RobotController_test_timeouts.cpp | 65 ++++ .../tests/RobotController_test_update.cpp | 99 ++++++ 11 files changed, 870 insertions(+), 129 deletions(-) create mode 100644 libs/RobotKit/tests/RobotController_test_behaviors.cpp create mode 100644 libs/RobotKit/tests/RobotController_test_fileExchange.cpp create mode 100644 libs/RobotKit/tests/RobotController_test_hardware.cpp create mode 100644 libs/RobotKit/tests/RobotController_test_magicCard.cpp create mode 100644 libs/RobotKit/tests/RobotController_test_raise.cpp delete mode 100644 libs/RobotKit/tests/RobotController_test_stateConnected.cpp delete mode 100644 libs/RobotKit/tests/RobotController_test_stateDisconnected.cpp create mode 100644 libs/RobotKit/tests/RobotController_test_timeouts.cpp create mode 100644 libs/RobotKit/tests/RobotController_test_update.cpp diff --git a/libs/RobotKit/CMakeLists.txt b/libs/RobotKit/CMakeLists.txt index bfc5f892df..2d03e705f8 100644 --- a/libs/RobotKit/CMakeLists.txt +++ b/libs/RobotKit/CMakeLists.txt @@ -42,8 +42,14 @@ if(${CMAKE_PROJECT_NAME} STREQUAL "LekaOSUnitTests") leka_unit_tests_sources( tests/RobotController_test_initializeComponents.cpp tests/RobotController_test_registerEvents.cpp - tests/RobotController_test_stateConnected.cpp - tests/RobotController_test_stateDisconnected.cpp + + tests/RobotController_test_raise.cpp + tests/RobotController_test_hardware.cpp + tests/RobotController_test_timeouts.cpp + tests/RobotController_test_behaviors.cpp + tests/RobotController_test_fileExchange.cpp + tests/RobotController_test_update.cpp + tests/RobotController_test_magicCard.cpp ) endif() endif() diff --git a/libs/RobotKit/tests/RobotController_test_behaviors.cpp b/libs/RobotKit/tests/RobotController_test_behaviors.cpp new file mode 100644 index 0000000000..de81973bd0 --- /dev/null +++ b/libs/RobotKit/tests/RobotController_test_behaviors.cpp @@ -0,0 +1,234 @@ +// Leka - LekaOS +// Copyright 2023 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#include "./RobotController_test.h" + +TEST_F(RobotControllerTest, runLaunchingBehavior) +{ + EXPECT_CALL(mock_videokit, + displayImage(std::filesystem::path { + "/fs/home/img/system/robot-misc-splash_screen-large-400.jpg"})); // TODO: Replace with + // EXPECT_CALL(mock_behaviorkit, + // launching); + EXPECT_CALL(mock_lcd, turnOn); + + rc.runLaunchingBehavior(); +} + +TEST_F(RobotControllerTest, startWaitingBehavior) +{ + EXPECT_CALL( + mock_videokit, + playVideoOnRepeat( + std::filesystem::path {"/fs/home/vid/system/robot-system-idle-looking_top_right_left-no_eyebrows.avi"}, + _)); // TODO: Replace with EXPECT_CALL(mock_behaviorkit, waiting); + EXPECT_CALL(mock_lcd, turnOn); + + rc.startWaitingBehavior(); +} + +TEST_F(RobotControllerTest, stopWaitingBehavior) +{ + { + EXPECT_CALL(mock_ledkit, stop); + EXPECT_CALL(mock_videokit, stopVideo); + EXPECT_CALL(mock_motor_left, stop); + EXPECT_CALL(mock_motor_right, stop); + } // TODO: Replace with EXPECT_CALL(mock_behaviorkit, stop); + + rc.stopWaitingBehavior(); +} + +TEST_F(RobotControllerTest, startWorkingBehavior) +{ + EXPECT_CALL( + mock_videokit, + displayImage(std::filesystem::path { + "/fs/home/img/system/robot-face-smiling-slightly.jpg"})); // TODO: Replace with + // EXPECT_CALL(mock_behaviorkit, working); + EXPECT_CALL(mock_lcd, turnOn); + + rc.startWorkingBehavior(); +} + +TEST_F(RobotControllerTest, startSleepingBehavior) +{ + { + EXPECT_CALL( + mock_videokit, + playVideoOnce( + std::filesystem::path {"/fs/home/vid/system/robot-system-sleep-yawn_then_sleep-no_eyebrows.avi"}, _)); + EXPECT_CALL(mock_ledkit, start(isSameAnimation(&led::animation::sleeping))); + + } // TODO: Replace with EXPECT_CALL(mock_behaviorkit, sleeping); + EXPECT_CALL(mock_lcd, turnOn); + + Sequence seq; + EXPECT_CALL(timeout_state_internal, onTimeout) + .InSequence(seq) + .WillOnce(GetCallback(&on_sleeping_start_timeout)); + EXPECT_CALL(timeout_state_internal, start).InSequence(seq); + + rc.startSleepingBehavior(); + + EXPECT_CALL(mock_lcd, turnOff); + EXPECT_CALL(mock_ledkit, stop); + + on_sleeping_start_timeout(); +} + +TEST_F(RobotControllerTest, stopSleepingBehavior) +{ + { + EXPECT_CALL(mock_ledkit, stop); + EXPECT_CALL(mock_videokit, stopVideo); + EXPECT_CALL(mock_motor_left, stop); + EXPECT_CALL(mock_motor_right, stop); + } // TODO: Replace with EXPECT_CALL(mock_behaviorkit, stop); + + EXPECT_CALL(timeout_state_internal, stop); + + rc.stopSleepingBehavior(); +} + +TEST_F(RobotControllerTest, onChargingBehavior) +{ + auto level = uint8_t {}; + + level = 10; + EXPECT_CALL(mock_videokit, + displayImage(std::filesystem::path { + "/fs/home/img/system/robot-battery-charging-empty_red.jpg"})); // TODO: Replace with + // EXPECT_CALL(mock_behaviorkit, + // chargingEmpty); + rc.onChargingBehavior(level); + + level = 33; + EXPECT_CALL( + mock_videokit, + displayImage(std::filesystem::path { + "/fs/home/img/system/robot-battery-charging-quarter_1-red.jpg"})); // TODO: Replace with + // EXPECT_CALL(mock_behaviorkit, + // chargingLow); + rc.onChargingBehavior(level); + + level = 66; + EXPECT_CALL( + mock_videokit, + displayImage(std::filesystem::path { + "/fs/home/img/system/robot-battery-charging-quarter_2-orange.jpg"})); // TODO: Replace with + // EXPECT_CALL(mock_behaviorkit, + // chargingMedium); + rc.onChargingBehavior(level); + + level = 80; + EXPECT_CALL( + mock_videokit, + displayImage(std::filesystem::path { + "/fs/home/img/system/robot-battery-charging-quarter_3-green.jpg"})); // TODO: Replace with + // EXPECT_CALL(mock_behaviorkit, + // chargingHigh); + rc.onChargingBehavior(level); + + level = 100; + EXPECT_CALL( + mock_videokit, + displayImage(std::filesystem::path { + "/fs/home/img/system/robot-battery-charging-quarter_4-green.jpg"})); // TODO: Replace with + // EXPECT_CALL(mock_behaviorkit, + // chargingFull); + rc.onChargingBehavior(level); +} + +TEST_F(RobotControllerTest, startChargingBehavior) +{ + EXPECT_CALL(battery, level).WillRepeatedly(Return(0)); + EXPECT_CALL(mock_videokit, displayImage); // TODO: Replace with EXPECT_CALL(mock_behaviorkit, chargingEmpty); + + EXPECT_CALL( + mock_ledkit, + start(isSameAnimation( + &led::animation::blink_on_charge))); // TODO: Replace with EXPECT_CALL(mock_behaviorkit, blinkOnCharge); + EXPECT_CALL(mock_lcd, turnOn); + + Sequence seq; + EXPECT_CALL(timeout_state_internal, onTimeout) + .InSequence(seq) + .WillOnce(GetCallback(&on_charging_start_timeout)); + EXPECT_CALL(timeout_state_internal, start).InSequence(seq); + + rc.startChargingBehavior(); + + EXPECT_CALL(mock_lcd, turnOff); + + on_charging_start_timeout(); +} + +TEST_F(RobotControllerTest, stopChargingBehavior) +{ + { + EXPECT_CALL(mock_ledkit, stop); + EXPECT_CALL(mock_videokit, stopVideo); + EXPECT_CALL(mock_motor_left, stop); + EXPECT_CALL(mock_motor_right, stop); + } // TODO: Replace with EXPECT_CALL(mock_behaviorkit, stop); + + EXPECT_CALL(timeout_state_internal, stop); + + rc.stopChargingBehavior(); +} + +TEST_F(RobotControllerTest, startConnectionBehaviorIsCharging) +{ + expectedCallsStopActuators(); + + Sequence seq; + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(true)); + EXPECT_CALL(mock_ledkit, start(isSameAnimation(&led::animation::ble_connection))) + .InSequence(seq); // TODO: Replace with EXPECT_CALL(mock_behaviorkit, bleConnectionWithoutVideo); + EXPECT_CALL(mock_ledkit, start(isSameAnimation(&led::animation::blink_on_charge))) + .InSequence(seq); // TODO: Replace with EXPECT_CALL(mock_behaviorkit, blinkOnCharge); + + EXPECT_CALL(mock_videokit, playVideoOnce).Times(0); + EXPECT_CALL(mock_lcd, turnOn).Times(0); + + rc.startConnectionBehavior(); +} + +TEST_F(RobotControllerTest, startConnectionBehaviorIsNotCharging) +{ + expectedCallsStopActuators(); + + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(false)); + { + EXPECT_CALL(mock_ledkit, start(isSameAnimation(&led::animation::ble_connection))); + EXPECT_CALL(mock_videokit, playVideoOnce); + } // TODO: Replace with EXPECT_CALL(mock_behaviorkit, bleConnectionWithVideo); + + EXPECT_CALL(mock_lcd, turnOn); + + rc.startConnectionBehavior(); +} + +TEST_F(RobotControllerTest, startDisconnectionBehaviorCharging) +{ + expectedCallsStopActuators(); + + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(true)); + EXPECT_CALL(mock_ledkit, start(isSameAnimation(&led::animation::blink_on_charge))) + .Times(1); // TODO: Replace with EXPECT_CALL(mock_behaviorkit, blinkOnCharge); + + rc.startDisconnectionBehavior(); +} + +TEST_F(RobotControllerTest, startDisconnectionBehaviorNotCharging) +{ + expectedCallsStopActuators(); + + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(false)); + + EXPECT_CALL(mock_ledkit, start).Times(0); + + rc.startDisconnectionBehavior(); +} diff --git a/libs/RobotKit/tests/RobotController_test_fileExchange.cpp b/libs/RobotKit/tests/RobotController_test_fileExchange.cpp new file mode 100644 index 0000000000..71f06c8a90 --- /dev/null +++ b/libs/RobotKit/tests/RobotController_test_fileExchange.cpp @@ -0,0 +1,78 @@ +// Leka - LekaOS +// Copyright 2023 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#include "./RobotController_test.h" + +TEST_F(RobotControllerTest, startFileExchangeIsCharging) +{ + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(true)); + + EXPECT_CALL( + mock_ledkit, + start(isSameAnimation( + &led::animation::blink_on_charge))); // TODO: Replace with EXPECT_CALL(mock_behaviorkit, blinkOnCharge); + EXPECT_CALL(mock_videokit, + displayImage(std::filesystem::path { + "/fs/home/img/system/robot-file_exchange.jpg"})); // TODO: Replace with + // EXPECT_CALL(mock_behaviorkit, fileExchange); + EXPECT_CALL(mock_lcd, turnOn); + + // TODO: Specify which BLE service and what is expected if necessary + EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(AnyNumber()); + + rc.startFileExchange(); +} + +TEST_F(RobotControllerTest, startFileExchangeIsNotCharging) +{ + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(false)); + + EXPECT_CALL(mock_videokit, + displayImage(std::filesystem::path { + "/fs/home/img/system/robot-file_exchange.jpg"})); // TODO: Replace with + // EXPECT_CALL(mock_behaviorkit, fileExchange); + EXPECT_CALL(mock_lcd, turnOn); + + // TODO: Specify which BLE service and what is expected if necessary + EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(AnyNumber()); + + rc.startFileExchange(); +} + +TEST_F(RobotControllerTest, stopFileExchange) +{ + // TODO: Specify which BLE service and what is expected if necessary + EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(AnyNumber()); + + rc.stopFileExchange(); +} + +TEST_F(RobotControllerTest, isReadyToFileExchange) +{ + auto actual_is_ready = bool {}; + auto minimal_battery = uint8_t {25}; + + // TODO: Specify which BLE service and what is expected if necessary + EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(AnyNumber()); + + EXPECT_CALL(battery, level).WillRepeatedly(Return(minimal_battery)); + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(false)); + actual_is_ready = rc.isReadyToFileExchange(); + EXPECT_FALSE(actual_is_ready); + + EXPECT_CALL(battery, level).WillRepeatedly(Return(minimal_battery)); + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(true)); + actual_is_ready = rc.isReadyToFileExchange(); + EXPECT_FALSE(actual_is_ready); + + EXPECT_CALL(battery, level).WillRepeatedly(Return(minimal_battery + 1)); + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(false)); + actual_is_ready = rc.isReadyToFileExchange(); + EXPECT_FALSE(actual_is_ready); + + EXPECT_CALL(battery, level).WillRepeatedly(Return(minimal_battery + 1)); + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(true)); + actual_is_ready = rc.isReadyToFileExchange(); + EXPECT_TRUE(actual_is_ready); +} diff --git a/libs/RobotKit/tests/RobotController_test_hardware.cpp b/libs/RobotKit/tests/RobotController_test_hardware.cpp new file mode 100644 index 0000000000..7d7a5b773a --- /dev/null +++ b/libs/RobotKit/tests/RobotController_test_hardware.cpp @@ -0,0 +1,61 @@ +// Leka - LekaOS +// Copyright 2023 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#include "./RobotController_test.h" + +TEST_F(RobotControllerTest, stopActuators) +{ + EXPECT_CALL(mock_ears, hide).Times(AnyNumber()); + EXPECT_CALL(mock_belt, hide).Times(AnyNumber()); + EXPECT_CALL(mock_ledkit, stop).Times(AnyNumber()); + EXPECT_CALL(mock_videokit, stopVideo).Times(AnyNumber()); + EXPECT_CALL(mock_motor_left, stop).Times(AnyNumber()); + EXPECT_CALL(mock_motor_right, stop).Times(AnyNumber()); + + rc.stopActuators(); +} + +TEST_F(RobotControllerTest, stopActuatorsAndLcd) +{ + EXPECT_CALL(mock_lcd, turnOff); + + EXPECT_CALL(mock_ears, hide).Times(AnyNumber()); + EXPECT_CALL(mock_belt, hide).Times(AnyNumber()); + EXPECT_CALL(mock_ledkit, stop).Times(AnyNumber()); + EXPECT_CALL(mock_videokit, stopVideo).Times(AnyNumber()); + EXPECT_CALL(mock_motor_left, stop).Times(AnyNumber()); + EXPECT_CALL(mock_motor_right, stop).Times(AnyNumber()); + + rc.stopActuatorsAndLcd(); +} + +TEST_F(RobotControllerTest, suspendHardwareForDeepSleep) +{ + rc.suspendHardwareForDeepSleep(); + + // nothing expected +} + +TEST_F(RobotControllerTest, isCharging) +{ + auto actual_is_charging = bool {}; + + // TODO: Specify which BLE service and what is expected if necessary + EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(AnyNumber()); + + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(true)); + actual_is_charging = rc.isCharging(); + EXPECT_TRUE(actual_is_charging); + + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(false)); + actual_is_charging = rc.isCharging(); + EXPECT_FALSE(actual_is_charging); +} + +TEST_F(RobotControllerTest, isBleConnected) +{ + rc.isBleConnected(); + + // nothing expected +} diff --git a/libs/RobotKit/tests/RobotController_test_magicCard.cpp b/libs/RobotKit/tests/RobotController_test_magicCard.cpp new file mode 100644 index 0000000000..dd6fb49435 --- /dev/null +++ b/libs/RobotKit/tests/RobotController_test_magicCard.cpp @@ -0,0 +1,313 @@ +// Leka - LekaOS +// Copyright 2023 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#include "./RobotController_test.h" + +TEST_F(RobotControllerTest, onMagicCardAvailableEmergencyStopDelayNotOver) +{ + expectedCallsResetAutonomousActivitiesTimeout(); + + rc.onMagicCardAvailable(MagicCard::emergency_stop); +} + +TEST_F(RobotControllerTest, onMagicCardAvailableEmergencyStopDelayOver) +{ + auto delay_over = 11s; + + expectedCallsResetAutonomousActivitiesTimeout(); + + { + EXPECT_CALL(mock_lcd, turnOff); + + EXPECT_CALL(mock_ears, hide).Times(AnyNumber()); + EXPECT_CALL(mock_belt, hide).Times(AnyNumber()); + EXPECT_CALL(mock_ledkit, stop).Times(AnyNumber()); + EXPECT_CALL(mock_videokit, stopVideo).Times(AnyNumber()); + EXPECT_CALL(mock_motor_left, stop).Times(AnyNumber()); + EXPECT_CALL(mock_motor_right, stop).Times(AnyNumber()); + + EXPECT_CALL(timeout_state_internal, stop).Times(AnyNumber()); + EXPECT_CALL(timeout_state_transition, stop).Times(AnyNumber()); + } // TODO: is a consequence of SM + + spy_kernel_addElapsedTimeToTickCount(delay_over); + + rc.onMagicCardAvailable(MagicCard::emergency_stop); +} + +TEST_F(RobotControllerTest, onMagicCardAvailableDiceRollNotInAutonomousActivityDelayNotOver) +{ + expectedCallsResetAutonomousActivitiesTimeout(); + + auto delay_not_over = 1000ms; + spy_kernel_addElapsedTimeToTickCount(delay_not_over); + + rc.onMagicCardAvailable(MagicCard::dice_roll); +} + +TEST_F(RobotControllerTest, onMagicCardAvailableDiceRollNotInAutonomousActivityDelayOver) +{ + expectedCallsResetAutonomousActivitiesTimeout(); + + auto delay_over = 1001ms; + spy_kernel_addElapsedTimeToTickCount(delay_over); + + { + EXPECT_CALL(mock_ears, hide).Times(AnyNumber()); + EXPECT_CALL(mock_belt, hide).Times(AnyNumber()); + EXPECT_CALL(mock_ledkit, stop).Times(AnyNumber()); + EXPECT_CALL(mock_videokit, stopVideo).Times(AnyNumber()); + EXPECT_CALL(mock_motor_left, stop).Times(AnyNumber()); + EXPECT_CALL(mock_motor_right, stop).Times(AnyNumber()); + + EXPECT_CALL(timeout_state_internal, stop).Times(AnyNumber()); + EXPECT_CALL(timeout_state_transition, stop).Times(AnyNumber()); + + EXPECT_CALL(mock_videokit, displayImage).Times(AnyNumber()); + } // TODO: is a consequence of SM + + rc.onMagicCardAvailable(MagicCard::dice_roll); +} + +TEST_F(RobotControllerTest, onMagicCardAvailableDiceRollInAutonomousActivityIsNotPlayingDelayNotOver) +{ + rc.state_machine.set_current_states(lksm::state::autonomous_activities); + + expectedCallsResetAutonomousActivitiesTimeout(); + + auto delay_not_over = 2000ms; + spy_kernel_addElapsedTimeToTickCount(delay_not_over); + + rc.onMagicCardAvailable(MagicCard::dice_roll); +} + +TEST_F(RobotControllerTest, onMagicCardAvailableDiceRollInAutonomousActivityIsNotPlayingDelayOver) +{ + rc.state_machine.set_current_states(lksm::state::autonomous_activities); + + // expectedCallsResetAutonomousActivitiesTimeout(); + + auto delay_over = 2001ms; + spy_kernel_addElapsedTimeToTickCount(delay_over); + + { + EXPECT_CALL(timeout_autonomous_activities, stop).Times(AnyNumber()); + EXPECT_CALL(timeout_autonomous_activities, onTimeout).Times(AnyNumber()); + EXPECT_CALL(timeout_autonomous_activities, start).Times(AnyNumber()); + + EXPECT_CALL(mock_ears, hide).Times(AnyNumber()); + EXPECT_CALL(mock_belt, hide).Times(AnyNumber()); + EXPECT_CALL(mock_ledkit, stop).Times(AnyNumber()); + EXPECT_CALL(mock_videokit, stopVideo).Times(AnyNumber()); + EXPECT_CALL(mock_motor_left, stop).Times(AnyNumber()); + EXPECT_CALL(mock_motor_right, stop).Times(AnyNumber()); + + EXPECT_CALL(timeout_state_internal, stop).Times(AnyNumber()); + EXPECT_CALL(timeout_state_transition, stop).Times(AnyNumber()); + + EXPECT_CALL(mock_videokit, displayImage).Times(AnyNumber()); + + EXPECT_CALL(timeout_state_transition, onTimeout).Times(AnyNumber()); + EXPECT_CALL(timeout_state_transition, start).Times(AnyNumber()); + EXPECT_CALL(mock_videokit, playVideoOnRepeat); // TODO: Replace with EXPECT_CALL(mock_behaviorkit, waiting); + EXPECT_CALL(mock_lcd, turnOn); + } // TODO: is a consequence of SM + + rc.onMagicCardAvailable(MagicCard::dice_roll); +} + +TEST_F(RobotControllerTest, onMagicCardAvailableDiceRollInAutonomousActivityIsPlayingDelayNotOver) +{ + rc.state_machine.set_current_states(lksm::state::autonomous_activities); + + expectedCallsResetAutonomousActivitiesTimeout(); + + auto set_activitykit_is_playing = [this] { + const std::unordered_map activities {{MagicCard::number_10, &display_tag}}; + activitykit.registerActivities(activities); + EXPECT_CALL(mock_videokit, displayImage); + activitykit.start(MagicCard::number_10); + }; + set_activitykit_is_playing(); + + auto delay_not_over = 1000ms; + spy_kernel_addElapsedTimeToTickCount(delay_not_over); + + rc.onMagicCardAvailable(MagicCard::dice_roll); +} + +TEST_F(RobotControllerTest, onMagicCardAvailableDiceRollInAutonomousActivityIsPlayingDelayOver) +{ + rc.state_machine.set_current_states(lksm::state::autonomous_activities); + + // expectedCallsResetAutonomousActivitiesTimeout(); + + auto set_activitykit_is_playing = [this] { + const std::unordered_map activities {{MagicCard::number_10, &display_tag}}; + activitykit.registerActivities(activities); + EXPECT_CALL(mock_videokit, displayImage); + activitykit.start(MagicCard::number_10); + }; + set_activitykit_is_playing(); + + auto delay_over = 1001ms; + spy_kernel_addElapsedTimeToTickCount(delay_over); + + { + EXPECT_CALL(timeout_autonomous_activities, stop).Times(AnyNumber()); + EXPECT_CALL(timeout_autonomous_activities, onTimeout).Times(AnyNumber()); + EXPECT_CALL(timeout_autonomous_activities, start).Times(AnyNumber()); + + EXPECT_CALL(mock_ears, hide).Times(AnyNumber()); + EXPECT_CALL(mock_belt, hide).Times(AnyNumber()); + EXPECT_CALL(mock_ledkit, stop).Times(AnyNumber()); + EXPECT_CALL(mock_videokit, stopVideo).Times(AnyNumber()); + EXPECT_CALL(mock_motor_left, stop).Times(AnyNumber()); + EXPECT_CALL(mock_motor_right, stop).Times(AnyNumber()); + + EXPECT_CALL(timeout_state_internal, stop).Times(AnyNumber()); + EXPECT_CALL(timeout_state_transition, stop).Times(AnyNumber()); + + EXPECT_CALL(mock_videokit, displayImage).Times(AnyNumber()); + } // TODO: is a consequence of SM + + rc.onMagicCardAvailable(MagicCard::dice_roll); +} + +TEST_F(RobotControllerTest, onMagicCardAvailableOtherNotInAutonomousActivityIsNotPlaying) +{ + expectedCallsResetAutonomousActivitiesTimeout(); + + rc.onMagicCardAvailable(MagicCard::number_10); +} + +TEST_F(RobotControllerTest, onMagicCardAvailableOtherNotInAutonomousActivityIsPlaying) +{ + expectedCallsResetAutonomousActivitiesTimeout(); + + auto set_activitykit_is_playing = [this] { + const std::unordered_map activities {{MagicCard::number_10, &display_tag}}; + activitykit.registerActivities(activities); + EXPECT_CALL(mock_videokit, displayImage); + activitykit.start(MagicCard::number_10); + }; + set_activitykit_is_playing(); + + rc.onMagicCardAvailable(MagicCard::number_10); +} + +TEST_F(RobotControllerTest, onMagicCardAvailableOtherInAutonomousActivityIsPlaying) +{ + rc.state_machine.set_current_states(lksm::state::autonomous_activities); + + expectedCallsResetAutonomousActivitiesTimeout(); + + auto set_activitykit_is_playing = [this] { + const std::unordered_map activities {{MagicCard::number_10, &display_tag}}; + activitykit.registerActivities(activities); + EXPECT_CALL(mock_videokit, displayImage); + activitykit.start(MagicCard::number_10); + }; + set_activitykit_is_playing(); + + rc.onMagicCardAvailable(MagicCard::number_10); +} + +TEST_F(RobotControllerTest, onMagicCardAvailableOtherInAutonomousActivityIsNotPlaying) +{ + rc.state_machine.set_current_states(lksm::state::autonomous_activities); + + expectedCallsResetAutonomousActivitiesTimeout(); + + rc.onMagicCardAvailable(MagicCard::number_10); +} + +TEST_F(RobotControllerTest, raiseEmergencyStop) +{ + { + EXPECT_CALL(mock_lcd, turnOff); + + EXPECT_CALL(mock_ears, hide).Times(AnyNumber()); + EXPECT_CALL(mock_belt, hide).Times(AnyNumber()); + EXPECT_CALL(mock_ledkit, stop).Times(AnyNumber()); + EXPECT_CALL(mock_videokit, stopVideo).Times(AnyNumber()); + EXPECT_CALL(mock_motor_left, stop).Times(AnyNumber()); + EXPECT_CALL(mock_motor_right, stop).Times(AnyNumber()); + + EXPECT_CALL(timeout_state_internal, stop).Times(AnyNumber()); + EXPECT_CALL(timeout_state_transition, stop).Times(AnyNumber()); + EXPECT_CALL(timeout_autonomous_activities, stop).Times(AnyNumber()); + } // TODO: is a consequence of SM + + rc.raiseEmergencyStop(); +} + +TEST_F(RobotControllerTest, resetEmergencyStopCounter) +{ + rc.resetEmergencyStopCounter(); + + // nothing expected (?) +} + +TEST_F(RobotControllerTest, startAutonomousActivityMode) +{ + EXPECT_CALL(mock_videokit, displayImage).Times(1); + EXPECT_CALL(mock_lcd, turnOn); + + rc.startAutonomousActivityMode(); +} + +TEST_F(RobotControllerTest, stopAutonomousActivityMode) +{ + EXPECT_CALL(timeout_autonomous_activities, stop); + { + EXPECT_CALL(mock_ledkit, stop); + EXPECT_CALL(mock_videokit, stopVideo); + EXPECT_CALL(mock_motor_left, stop); + EXPECT_CALL(mock_motor_right, stop); + } // TODO: Replace with EXPECT_CALL(mock_behaviorkit, stop); + + rc.stopAutonomousActivityMode(); +} + +TEST_F(RobotControllerTest, raiseAutonomousActivityModeRequested) +{ + { + EXPECT_CALL(mock_ears, hide).Times(AnyNumber()); + EXPECT_CALL(mock_belt, hide).Times(AnyNumber()); + EXPECT_CALL(mock_ledkit, stop).Times(AnyNumber()); + EXPECT_CALL(mock_videokit, stopVideo).Times(AnyNumber()); + EXPECT_CALL(mock_motor_left, stop).Times(AnyNumber()); + EXPECT_CALL(mock_motor_right, stop).Times(AnyNumber()); + + EXPECT_CALL(timeout_state_internal, stop).Times(AnyNumber()); + EXPECT_CALL(timeout_state_transition, stop).Times(AnyNumber()); + EXPECT_CALL(timeout_autonomous_activities, stop).Times(AnyNumber()); + + EXPECT_CALL(mock_videokit, displayImage).Times(AnyNumber()); + } // TODO: is a consequence of SM + + rc.raiseAutonomousActivityModeRequested(); +} + +TEST_F(RobotControllerTest, raiseAutonomousActivityModeExited) +{ + rc.raiseAutonomousActivityModeExited(); + + // nothing expected +} + +TEST_F(RobotControllerTest, resetAutonomousActivitiesTimeout) +{ + auto expected_duration = 600s; + + Sequence seq; + EXPECT_CALL(timeout_autonomous_activities, stop).InSequence(seq); + EXPECT_CALL(timeout_autonomous_activities, onTimeout) + .InSequence(seq) + .WillOnce(GetCallback(&on_autonomous_activities_timeout)); + EXPECT_CALL(timeout_autonomous_activities, start(std::chrono::microseconds {expected_duration})).InSequence(seq); + + rc.resetAutonomousActivitiesTimeout(); +} diff --git a/libs/RobotKit/tests/RobotController_test_raise.cpp b/libs/RobotKit/tests/RobotController_test_raise.cpp new file mode 100644 index 0000000000..98fe83e7c5 --- /dev/null +++ b/libs/RobotKit/tests/RobotController_test_raise.cpp @@ -0,0 +1,12 @@ +// Leka - LekaOS +// Copyright 2023 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#include "./RobotController_test.h" + +TEST_F(RobotControllerTest, raise) +{ + rc.raise(system::robot::sm::event::setup_complete {}); + + // nothing expected +} diff --git a/libs/RobotKit/tests/RobotController_test_registerEvents.cpp b/libs/RobotKit/tests/RobotController_test_registerEvents.cpp index ecf7320b8c..f5f4016555 100644 --- a/libs/RobotKit/tests/RobotController_test_registerEvents.cpp +++ b/libs/RobotKit/tests/RobotController_test_registerEvents.cpp @@ -118,70 +118,6 @@ TEST_F(RobotControllerTest, registerEventsBatteryIsCharging) rc.registerEvents(); } -TEST_F(RobotControllerTest, registerOnFactoryResetNotificationCallback) -{ - MockFunction callback {}; - - rc.registerOnFactoryResetNotificationCallback(callback.AsStdFunction()); - - // nothing can be expected -} - -TEST_F(RobotControllerTest, onChargingBehaviorLevelBelow25) -{ - auto battery_level = 0; - - EXPECT_CALL(mock_videokit, - displayImage(std::filesystem::path {"/fs/home/img/system/robot-battery-charging-empty_red.jpg"})) - .Times(1); - - rc.onChargingBehavior(battery_level); -} - -TEST_F(RobotControllerTest, onChargingBehaviorLevelAbove25Below50) -{ - auto battery_level = 32; - - EXPECT_CALL(mock_videokit, - displayImage(std::filesystem::path {"/fs/home/img/system/robot-battery-charging-quarter_1-red.jpg"})) - .Times(1); - - rc.onChargingBehavior(battery_level); -} - -TEST_F(RobotControllerTest, onChargingBehaviorLevelAbove50Below75) -{ - auto battery_level = 62; - - EXPECT_CALL(mock_videokit, - displayImage(std::filesystem::path {"/fs/home/img/system/robot-battery-charging-quarter_2-orange.jpg"})) - .Times(1); - - rc.onChargingBehavior(battery_level); -} - -TEST_F(RobotControllerTest, onChargingBehaviorLevelAbove75Below90) -{ - auto battery_level = 88; - - EXPECT_CALL(mock_videokit, - displayImage(std::filesystem::path {"/fs/home/img/system/robot-battery-charging-quarter_3-green.jpg"})) - .Times(1); - - rc.onChargingBehavior(battery_level); -} - -TEST_F(RobotControllerTest, onChargingBehaviorLevelAbove90) -{ - auto battery_level = 94; - - EXPECT_CALL(mock_videokit, - displayImage(std::filesystem::path {"/fs/home/img/system/robot-battery-charging-quarter_4-green.jpg"})) - .Times(1); - - rc.onChargingBehavior(battery_level); -} - TEST_F(RobotControllerTest, onTagActivated) { auto onTagActivated = rfidkit.getCallback(); diff --git a/libs/RobotKit/tests/RobotController_test_stateConnected.cpp b/libs/RobotKit/tests/RobotController_test_stateConnected.cpp deleted file mode 100644 index d1ef7e2e50..0000000000 --- a/libs/RobotKit/tests/RobotController_test_stateConnected.cpp +++ /dev/null @@ -1,38 +0,0 @@ -// Leka - LekaOS -// Copyright 2022 APF France handicap -// SPDX-License-Identifier: Apache-2.0 - -#include "./RobotController_test.h" - -TEST_F(RobotControllerTest, startConnectionBehaviorIsCharging) -{ - EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(true)); - - expectedCallsStopActuators(); - Sequence on_ble_connection_sequence; - EXPECT_CALL(mock_ledkit, start(isSameAnimation(&led::animation::ble_connection))) - .Times(1) - .InSequence(on_ble_connection_sequence); - EXPECT_CALL(mock_videokit, playVideoOnce).Times(0).InSequence(on_ble_connection_sequence); - EXPECT_CALL(mock_lcd, turnOn).Times(0).InSequence(on_ble_connection_sequence); - EXPECT_CALL(mock_ledkit, start(isSameAnimation(&led::animation::blink_on_charge))) - .Times(1) - .InSequence(on_ble_connection_sequence); - - rc.startConnectionBehavior(); -} - -TEST_F(RobotControllerTest, startConnectionBehaviorIsNotCharging) -{ - EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(false)); - - expectedCallsStopActuators(); - Sequence on_ble_connection_sequence; - EXPECT_CALL(mock_ledkit, start(isSameAnimation(&led::animation::ble_connection))) - .Times(1) - .InSequence(on_ble_connection_sequence); - EXPECT_CALL(mock_videokit, playVideoOnce).Times(1).InSequence(on_ble_connection_sequence); - EXPECT_CALL(mock_lcd, turnOn).Times(1).InSequence(on_ble_connection_sequence); - - rc.startConnectionBehavior(); -} diff --git a/libs/RobotKit/tests/RobotController_test_stateDisconnected.cpp b/libs/RobotKit/tests/RobotController_test_stateDisconnected.cpp deleted file mode 100644 index fa7728576d..0000000000 --- a/libs/RobotKit/tests/RobotController_test_stateDisconnected.cpp +++ /dev/null @@ -1,25 +0,0 @@ -// Leka - LekaOS -// Copyright 2022 APF France handicap -// SPDX-License-Identifier: Apache-2.0 - -#include "./RobotController_test.h" - -TEST_F(RobotControllerTest, startDisconnectionBehaviorCharging) -{ - expectedCallsStopActuators(); - - EXPECT_CALL(battery, isCharging).WillOnce(Return(true)); - EXPECT_CALL(mock_ledkit, start(isSameAnimation(&led::animation::blink_on_charge))).Times(1); - - rc.startDisconnectionBehavior(); -} - -TEST_F(RobotControllerTest, startDisconnectionBehaviorNotCharging) -{ - expectedCallsStopActuators(); - - EXPECT_CALL(battery, isCharging).WillOnce(Return(false)); - EXPECT_CALL(mock_ledkit, start(isSameAnimation(&led::animation::blink_on_charge))).Times(0); - - rc.startDisconnectionBehavior(); -} diff --git a/libs/RobotKit/tests/RobotController_test_timeouts.cpp b/libs/RobotKit/tests/RobotController_test_timeouts.cpp new file mode 100644 index 0000000000..62a0c314a1 --- /dev/null +++ b/libs/RobotKit/tests/RobotController_test_timeouts.cpp @@ -0,0 +1,65 @@ +// Leka - LekaOS +// Copyright 2023 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#include "./RobotController_test.h" + +TEST_F(RobotControllerTest, startIdleTimeout) +{ + auto expected_duration = 600s; + + Sequence seq; + EXPECT_CALL(timeout_state_transition, onTimeout) + .InSequence(seq) + .WillOnce(GetCallback(&on_idle_timeout)); + EXPECT_CALL(timeout_state_transition, start(std::chrono::microseconds {expected_duration})).InSequence(seq); + + rc.startIdleTimeout(); +} + +TEST_F(RobotControllerTest, stopIdleTimeout) +{ + EXPECT_CALL(timeout_state_transition, stop); + + rc.stopIdleTimeout(); +} + +TEST_F(RobotControllerTest, startSleepTimeout) +{ + auto expected_duration = 60s; + + Sequence seq; + EXPECT_CALL(timeout_state_transition, onTimeout) + .InSequence(seq) + .WillOnce(GetCallback(&on_sleep_timeout)); + EXPECT_CALL(timeout_state_transition, start(std::chrono::microseconds {expected_duration})).InSequence(seq); + + rc.startSleepTimeout(); +} + +TEST_F(RobotControllerTest, stopSleepTimeout) +{ + EXPECT_CALL(timeout_state_transition, stop); + + rc.stopSleepTimeout(); +} + +TEST_F(RobotControllerTest, startDeepSleepTimeout) +{ + auto expected_duration = 600s; + + Sequence seq; + EXPECT_CALL(timeout_state_transition, onTimeout) + .InSequence(seq) + .WillOnce(GetCallback(&on_deep_sleep_timeout)); + EXPECT_CALL(timeout_state_transition, start(std::chrono::microseconds {expected_duration})).InSequence(seq); + + rc.startDeepSleepTimeout(); +} + +TEST_F(RobotControllerTest, stopDeepSleepTimeout) +{ + EXPECT_CALL(timeout_state_transition, stop); + + rc.stopDeepSleepTimeout(); +} diff --git a/libs/RobotKit/tests/RobotController_test_update.cpp b/libs/RobotKit/tests/RobotController_test_update.cpp new file mode 100644 index 0000000000..ad97c98b1e --- /dev/null +++ b/libs/RobotKit/tests/RobotController_test_update.cpp @@ -0,0 +1,99 @@ +// Leka - LekaOS +// Copyright 2023 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#include "./RobotController_test.h" + +TEST_F(RobotControllerTest, isReadyToUpdateVersionIsNotAvailable) +{ + auto actual_is_ready = bool {}; + auto minimal_battery = uint8_t {25}; + + EXPECT_CALL(firmware_update, isVersionAvailable).WillRepeatedly(Return(false)); + + // TODO: Specify which BLE service and what is expected if necessary + EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(AnyNumber()); + + EXPECT_CALL(battery, level).WillRepeatedly(Return(minimal_battery)); + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(false)); + actual_is_ready = rc.isReadyToUpdate(); + EXPECT_FALSE(actual_is_ready); + + EXPECT_CALL(battery, level).WillRepeatedly(Return(minimal_battery)); + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(true)); + actual_is_ready = rc.isReadyToUpdate(); + EXPECT_FALSE(actual_is_ready); + + EXPECT_CALL(battery, level).WillRepeatedly(Return(minimal_battery + 1)); + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(false)); + actual_is_ready = rc.isReadyToUpdate(); + EXPECT_FALSE(actual_is_ready); + + EXPECT_CALL(battery, level).WillRepeatedly(Return(minimal_battery + 1)); + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(true)); + actual_is_ready = rc.isReadyToUpdate(); + EXPECT_FALSE(actual_is_ready); +} + +TEST_F(RobotControllerTest, isReadyToUpdateVersionIsAvailable) +{ + auto actual_is_ready = bool {}; + auto minimal_battery = uint8_t {25}; + + EXPECT_CALL(firmware_update, isVersionAvailable).WillRepeatedly(Return(true)); + + // TODO: Specify which BLE service and what is expected if necessary + EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(AnyNumber()); + + EXPECT_CALL(battery, level).WillRepeatedly(Return(minimal_battery)); + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(false)); + actual_is_ready = rc.isReadyToUpdate(); + EXPECT_FALSE(actual_is_ready); + + EXPECT_CALL(battery, level).WillRepeatedly(Return(minimal_battery)); + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(true)); + actual_is_ready = rc.isReadyToUpdate(); + EXPECT_FALSE(actual_is_ready); + + EXPECT_CALL(battery, level).WillRepeatedly(Return(minimal_battery + 1)); + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(false)); + actual_is_ready = rc.isReadyToUpdate(); + EXPECT_FALSE(actual_is_ready); + + EXPECT_CALL(battery, level).WillRepeatedly(Return(minimal_battery + 1)); + EXPECT_CALL(battery, isCharging).WillRepeatedly(Return(true)); + actual_is_ready = rc.isReadyToUpdate(); + EXPECT_TRUE(actual_is_ready); +} + +TEST_F(RobotControllerTest, applyUpdateCallbackNotRegistered) +{ + EXPECT_CALL(firmware_update, loadFirmware).WillOnce(Return(false)); + rc.applyUpdate(); + + EXPECT_CALL(firmware_update, loadFirmware).WillOnce(Return(true)); + rc.applyUpdate(); +} + +TEST_F(RobotControllerTest, applyUpdateCallbackRegistered) +{ + MockFunction callback; + rc.registerOnUpdateLoadedCallback(callback.AsStdFunction()); + + EXPECT_CALL(firmware_update, loadFirmware).WillOnce(Return(false)); + EXPECT_CALL(callback, Call).Times(0); + rc.applyUpdate(); + + EXPECT_CALL(firmware_update, loadFirmware).WillOnce(Return(true)); + EXPECT_CALL(callback, Call).Times(1); + rc.applyUpdate(); +} + +TEST_F(RobotControllerTest, registerOnFactoryResetNotificationCallback) +{ + MockFunction callback {}; + + rc.registerOnFactoryResetNotificationCallback(callback.AsStdFunction()); + + // nothing expected +}