Skip to content

Commit

Permalink
update UT
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Feb 19, 2024
1 parent 4796bd3 commit 1efdbb9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
4 changes: 3 additions & 1 deletion drivers/CoreIMU/source/CoreIMU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void CoreIMU::registerOnWakeUpCallback(std::function<void()> const &callback)
lsm6dsox_all_sources_t all_source;
lsm6dsox_all_sources_get(&_register_io_function, &all_source);

if (all_source.sleep_change && all_source.sleep_state == 0) {
if (all_source.sleep_change && all_source.sleep_state == 0 && _on_wake_up_callback != nullptr) {
_on_wake_up_callback();
}
};
Expand Down Expand Up @@ -218,6 +218,8 @@ void CoreIMU::setInterruptCallback(std::function<void()> const &callback)
{
if (callback != nullptr) {
_irq.onRise(callback);
} else {
_irq.onRise({});
}
}

Expand Down
52 changes: 51 additions & 1 deletion drivers/CoreIMU/tests/CoreIMU_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using namespace leka;

using testing::_;
using testing::AnyNumber;
using testing::AtLeast;
using testing::MockFunction;

Expand Down Expand Up @@ -109,7 +110,7 @@ TEST_F(CoreIMUTest, onWakeUpCallback)

EXPECT_CALL(mocki2c, write).Times(AtLeast(1));
EXPECT_CALL(mocki2c, read).Times(AtLeast(1));
// EXPECT_CALL(mock_callback, Call).Times(1); // TODO: Should setup struct
EXPECT_CALL(mock_callback, Call).Times(AnyNumber());

coreimu.registerOnWakeUpCallback(mock_callback.AsStdFunction());

Expand Down Expand Up @@ -140,3 +141,52 @@ TEST_F(CoreIMUTest, disableOnWakeUpInterrupt)

coreimu.disableOnWakeUpInterrupt();
}

TEST_F(CoreIMUTest, switchCallbacks)
{
auto mock_data_available_callback = MockFunction<void(const leka::interface::IMU::SensorData &data)> {};
auto mock_wake_up_callback = MockFunction<void()> {};
auto on_rise_callback = mbed::Callback<void()> {};

EXPECT_CALL(mocki2c, write).Times(AnyNumber());
EXPECT_CALL(mocki2c, read).Times(AnyNumber());

coreimu.registerOnDataAvailableCallback(mock_data_available_callback.AsStdFunction());
coreimu.registerOnWakeUpCallback(mock_wake_up_callback.AsStdFunction());

{
// Enable Data Available
EXPECT_CALL(mock_data_available_callback, Call).Times(1);
EXPECT_CALL(mock_wake_up_callback, Call).Times(0);
coreimu.enableOnDataAvailable();
on_rise_callback = spy_InterruptIn_getRiseCallback();
on_rise_callback();
}

{
// Enable Wake Up
EXPECT_CALL(mock_data_available_callback, Call).Times(0);
EXPECT_CALL(mock_wake_up_callback, Call).Times(AnyNumber());
coreimu.enableOnWakeUpInterrupt();
on_rise_callback = spy_InterruptIn_getRiseCallback();
on_rise_callback();
}

{
// Enable Data Available
EXPECT_CALL(mock_data_available_callback, Call).Times(1);
EXPECT_CALL(mock_wake_up_callback, Call).Times(0);
coreimu.enableOnDataAvailable();
on_rise_callback = spy_InterruptIn_getRiseCallback();
on_rise_callback();
}

{
// Disable Data Available
EXPECT_CALL(mock_data_available_callback, Call).Times(0);
EXPECT_CALL(mock_wake_up_callback, Call).Times(0);
coreimu.disableOnDataAvailable();
on_rise_callback = spy_InterruptIn_getRiseCallback();
on_rise_callback();
}
}

0 comments on commit 1efdbb9

Please sign in to comment.