19#include <gmock/gmock.h>
20#include <gtest/gtest.h>
24using ::testing::Return;
25using ::testing::Throw;
54 EXPECT_CALL(mockSPI, openDevice(
"/dev/spidev0.0")).WillOnce(Return(
true));
55 EXPECT_CALL(mockSPI, closeDevice()).Times(1);
56 EXPECT_CALL(mockSPI, spiTransfer(_,
nullptr, 1)).WillOnce(Return());
57 EXPECT_CALL(mockSPI, readByte(_)).WillOnce(Return(0x80)).WillRepeatedly(Return(0x00));
58 EXPECT_CALL(mockSPI, writeByte(_, _)).Times(::testing::AtLeast(1));
61 ASSERT_NO_THROW(controller.
init());
72 EXPECT_CALL(mockSPI, openDevice(
"/dev/nonexistent")).WillOnce(Return(
false));
85 EXPECT_CALL(mockSPI, openDevice(
"/dev/spidev0.0")).WillOnce(Return(
true));
86 EXPECT_CALL(mockSPI, closeDevice()).Times(1);
91 processor.registerHandler(0x100, [](
const std::vector<uint8_t> &) {}));
93 processor.registerHandler(0x200, [](
const std::vector<uint8_t> &) {}));
104 EXPECT_CALL(mockSPI, openDevice(
"/dev/spidev0.0")).WillOnce(Return(
true));
105 EXPECT_CALL(mockSPI, closeDevice()).Times(1);
111 std::vector<uint8_t> data = {0x00, 0x00, 0x20, 0x41};
114 ASSERT_EQ(speedSpy.count(), 1);
115 QList<QVariant> arguments = speedSpy.takeFirst();
116 ASSERT_EQ(arguments.at(0).toFloat(), 1.0F);
127 EXPECT_CALL(mockSPI, openDevice(
"/dev/spidev0.0")).WillOnce(Return(
true));
128 EXPECT_CALL(mockSPI, closeDevice()).Times(1);
134 std::vector<uint8_t> data = {0x03, 0xE8};
137 ASSERT_EQ(rpmSpy.count(), 1);
138 QList<QVariant> arguments = rpmSpy.takeFirst();
139 ASSERT_EQ(arguments.at(0).toInt(), 1000);
150 EXPECT_CALL(mockSPI, openDevice(
"/dev/spidev0.0")).WillOnce(Return(
true));
151 EXPECT_CALL(mockSPI, closeDevice()).Times(1);
152 EXPECT_CALL(mockSPI, readByte(_))
153 .WillOnce(Return(0x01))
154 .WillRepeatedly(Return(0x00));
155 EXPECT_CALL(mockSPI, spiTransfer(_, _, _))
156 .WillRepeatedly([](
const uint8_t *tx, uint8_t *rx,
size_t length) {
157 if (length == 3 && tx[0] == 0x03) {
162 EXPECT_CALL(mockSPI, writeByte(_, _)).Times(::testing::AtLeast(1));
166 std::thread readerThread([&controller]() { controller.
processReading(); });
168 std::this_thread::sleep_for(std::chrono::milliseconds(50));
183 EXPECT_CALL(mockSPI, openDevice(
"/dev/spidev0.0")).WillOnce(Return(
true));
184 EXPECT_CALL(mockSPI, closeDevice()).Times(1);
Definition of the MCP2515Controller class.
File containing Mock classes to test the SPI controller.
Class that processes CAN messages.
void processMessage(uint16_t frameID, const std::vector< uint8_t > &data)
Process a CAN message.
void rpmUpdated(int newRpm)
Signal emitted when the RPM is updated.
void speedUpdated(float newSpeed)
Signal emitted when the speed is updated.
Class that configures the MCP2515 CAN controller.
Test fixture for testing the MCP2515Controller class.
MCP2515Configurator configurator
MCP2515Configurator object.
MockSPIController mockSPI
Mocked SPI controller.
CANMessageProcessor messageProcessor
CANMessageProcessor object.
MCP2515ControllerTest()=default
MCP2515Controller object set as default.
Class that controls the MCP2515 CAN controller. IMCP2515Controller.
bool isStopReadingFlagSet() const override
Check if the stop reading flag is set.
void processReading() override
Start reading CAN messages.
bool init() override
Initialize the MCP2515 controller.
void stopReading() override
Stop reading CAN messages.
CANMessageProcessor & getMessageProcessor()
Class to emulate the behavior of the SPI controller. (Overrided the Can0)
TEST_F(MCP2515ControllerTest, InitializationSuccess)
Ensures that init() does not throw an exception.