Skip to content
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

♻️ (RC): Refactor UT / Remove UT based on StateMachine #1350

Merged
merged 2 commits into from
Feb 1, 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
18 changes: 8 additions & 10 deletions libs/RobotKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +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_stateSetup.cpp
tests/RobotController_test_stateIdle.cpp
tests/RobotController_test_stateSleeping.cpp
tests/RobotController_test_stateCharging.cpp
tests/RobotController_test_stateConnected.cpp
tests/RobotController_test_stateDisconnected.cpp
tests/RobotController_test_stateWorking.cpp
tests/RobotController_test_stateEmergencyStopped.cpp
tests/RobotController_test_stateAutonomousActivities.cpp
tests/RobotController_test_stateFileExchange.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()
157 changes: 33 additions & 124 deletions libs/RobotKit/tests/RobotController_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,6 @@ class RobotControllerTest : public testing::Test

bool spy_isCharging_return_value = false;

void expectedCallsStopMotors()
{
EXPECT_CALL(mock_motor_left, stop());
EXPECT_CALL(mock_motor_right, stop());
}

void expectedCallsStopActuators()
{
EXPECT_CALL(mock_ledkit, stop).Times(AtLeast(1));
Expand All @@ -162,135 +156,50 @@ class RobotControllerTest : public testing::Test

void expectedCallsInitializeComponents()
{
{
InSequence seq;

EXPECT_CALL(mbed_mock_gatt, addService).Times(8);
EXPECT_CALL(mbed_mock_gap, setEventHandler).Times(1);
EXPECT_CALL(mbed_mock_gatt, setEventHandler).Times(1);

EXPECT_CALL(mock_mcu, getID).Times(1);
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(1);

EXPECT_CALL(firmware_update, getCurrentVersion).Times(1);
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(1);

Sequence set_serial_number_as_ble_device_name;
EXPECT_CALL(mock_mcu, getID).InSequence(set_serial_number_as_ble_device_name);
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(1).InSequence(set_serial_number_as_ble_device_name);
EXPECT_CALL(mbed_mock_gap, setAdvertisingPayload).InSequence(set_serial_number_as_ble_device_name);

expectedCallsStopMotors();

EXPECT_CALL(mock_ledkit, init);

EXPECT_CALL(mock_videokit, initializeScreen).Times(1);
EXPECT_CALL(mock_lcd, turnOff).Times(1);
EXPECT_CALL(mock_videokit, stopVideo).Times(1);

EXPECT_CALL(mock_ledkit, stop).Times(AtLeast(1));
}
EXPECT_CALL(mock_mcu, getID).Times(AnyNumber());
EXPECT_CALL(firmware_update, getCurrentVersion).Times(AnyNumber());

EXPECT_CALL(mbed_mock_gap, setEventHandler).Times(AnyNumber());
EXPECT_CALL(mbed_mock_gap, setAdvertisingPayload).Times(AnyNumber());
EXPECT_CALL(mbed_mock_gatt, addService).Times(AnyNumber());
EXPECT_CALL(mbed_mock_gatt, setEventHandler).Times(AnyNumber());
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(AnyNumber());

EXPECT_CALL(mock_ledkit, init).Times(AnyNumber());
EXPECT_CALL(mock_videokit, initializeScreen).Times(AnyNumber());
EXPECT_CALL(mock_lcd, turnOff).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.initializeComponents();
}

void expectedCallsRegisterEvents()
{
{
InSequence seq;

Sequence on_low_battery_sequence;
EXPECT_CALL(battery, level).InSequence(on_low_battery_sequence);
EXPECT_CALL(battery, isCharging)
.InSequence(on_low_battery_sequence)
.WillOnce(Return(spy_isCharging_return_value));
if (spy_isCharging_return_value == false) {
EXPECT_CALL(
mock_videokit,
displayImage(std::filesystem::path {"/fs/home/img/system/robot-battery-empty-must_be_charged.jpg"}))
.Times(1);
expectedCallsStopMotors();
}
EXPECT_CALL(battery, level).InSequence(on_low_battery_sequence);

Sequence on_data_updated_sequence;
EXPECT_CALL(battery, level).InSequence(on_data_updated_sequence);
EXPECT_CALL(battery, isCharging).InSequence(on_data_updated_sequence);
EXPECT_CALL(mbed_mock_gap, setAdvertisingPayload).InSequence(on_data_updated_sequence);
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(2).InSequence(on_data_updated_sequence);

EXPECT_CALL(battery, onChargeDidStart).WillOnce(SaveArg<0>(&on_charge_did_start));

EXPECT_CALL(battery, onChargeDidStop).WillOnce(SaveArg<0>(&on_charge_did_stop));

{
InSequence event_setup_complete;

if (spy_isCharging_return_value == true) {
expectedCallsTransitionSetupToCharging();
} else {
expectedCallsTransitionSetupToIdle();
}
}
}

rc.registerEvents();
}

void expectedCallsTransitionSetupToIdle()
{
Sequence is_charging_sequence;

EXPECT_CALL(battery, isCharging).InSequence(is_charging_sequence).WillOnce(Return(spy_isCharging_return_value));
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).InSequence(is_charging_sequence);

expectedCallsRunLaunchingBehavior();

Sequence on_idle_entry_sequence;
EXPECT_CALL(timeout_state_transition, onTimeout)
.InSequence(on_idle_entry_sequence)
.WillOnce(GetCallback<interface::Timeout::callback_t>(&on_sleep_timeout));
EXPECT_CALL(timeout_state_transition, start).InSequence(on_idle_entry_sequence);

EXPECT_CALL(mock_videokit, playVideoOnRepeat).InSequence(on_idle_entry_sequence);
EXPECT_CALL(mock_lcd, turnOn).Times(AtLeast(1)).InSequence(on_idle_entry_sequence);
}

void expectedCallsTransitionSetupToCharging()
{
Sequence is_charging_sequence;

EXPECT_CALL(battery, isCharging).InSequence(is_charging_sequence).WillOnce(Return(spy_isCharging_return_value));
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).InSequence(is_charging_sequence);

EXPECT_CALL(battery, isCharging).InSequence(is_charging_sequence).WillOnce(Return(spy_isCharging_return_value));
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).InSequence(is_charging_sequence);
EXPECT_CALL(battery, isCharging).Times(AnyNumber());
EXPECT_CALL(battery, level).Times(AnyNumber());

expectedCallsRunLaunchingBehavior();
EXPECT_CALL(mock_ledkit, stop).Times(AnyNumber());
EXPECT_CALL(mock_motor_left, stop).Times(AnyNumber());
EXPECT_CALL(mock_motor_right, stop).Times(AnyNumber());
EXPECT_CALL(mock_videokit, displayImage).Times(AnyNumber());

Sequence start_deep_sleep_timeout_sequence;
EXPECT_CALL(timeout_state_transition, onTimeout).InSequence(start_deep_sleep_timeout_sequence);
EXPECT_CALL(timeout_state_transition, start).InSequence(start_deep_sleep_timeout_sequence);
EXPECT_CALL(mbed_mock_gap, setAdvertisingPayload).Times(AnyNumber());
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(AnyNumber());

Sequence start_charging_behavior_sequence;
EXPECT_CALL(battery, level).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(mock_videokit, displayImage).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(mock_ledkit, start).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(mock_lcd, turnOn).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(timeout_state_internal, onTimeout)
.InSequence(start_charging_behavior_sequence)
.WillOnce(GetCallback<interface::Timeout::callback_t>(&on_charging_start_timeout));
EXPECT_CALL(timeout_state_internal, start).InSequence(start_charging_behavior_sequence);
}
{
EXPECT_CALL(timeout_state_transition, onTimeout).Times(AnyNumber());
EXPECT_CALL(timeout_state_transition, start).Times(AnyNumber());
EXPECT_CALL(mock_videokit, playVideoOnRepeat).Times(AnyNumber());
EXPECT_CALL(mock_lcd, turnOn).Times(AnyNumber());
} // ? On Idle entry

void expectedCallsRunLaunchingBehavior()
{
InSequence run_launching_behavior_sequence;
// Saved callback
EXPECT_CALL(battery, onChargeDidStart).WillOnce(SaveArg<0>(&on_charge_did_start));
EXPECT_CALL(battery, onChargeDidStop).WillOnce(SaveArg<0>(&on_charge_did_stop));

EXPECT_CALL(mock_videokit,
displayImage(std::filesystem::path {"/fs/home/img/system/robot-misc-splash_screen-large-400.jpg"}))
.Times(1);
EXPECT_CALL(mock_lcd, turnOn);
rc.registerEvents();
}

void expectedCallsResetAutonomousActivitiesTimeout()
Expand Down
Loading
Loading