Hotwheels-Cluster 1.2
Creation of Cluster APP for SEA:ME project.
 
Loading...
Searching...
No Matches
test_MCP2515Configurator.cpp
Go to the documentation of this file.
1
14
16#include "MockSPIController.hpp"
17#include <gmock/gmock.h>
18#include <gtest/gtest.h>
19
20using ::testing::_;
21using ::testing::Return;
22
37
45TEST_F(MCP2515ConfiguratorTest, ResetChipSuccess) {
46 EXPECT_CALL(mockSPI, spiTransfer(_, nullptr, 1)).Times(1);
47 EXPECT_CALL(mockSPI, readByte(MCP2515Configurator::CANSTAT))
48 .WillOnce(Return(0x80));
49 ASSERT_TRUE(configurator.resetChip());
50}
51
58TEST_F(MCP2515ConfiguratorTest, ResetChipFailure) {
59 EXPECT_CALL(mockSPI, spiTransfer(_, nullptr, 1)).Times(1);
60 EXPECT_CALL(mockSPI, readByte(MCP2515Configurator::CANSTAT))
61 .WillOnce(Return(0x00));
62 ASSERT_FALSE(configurator.resetChip());
63}
64
73TEST_F(MCP2515ConfiguratorTest, ConfigureBaudRate) {
74 EXPECT_CALL(mockSPI, writeByte(MCP2515Configurator::CNF1, 0x00)).Times(1);
75 EXPECT_CALL(mockSPI, writeByte(MCP2515Configurator::CNF2, 0x90)).Times(1);
76 EXPECT_CALL(mockSPI, writeByte(MCP2515Configurator::CNF3, 0x02)).Times(1);
77 configurator.configureBaudRate();
78}
79
88TEST_F(MCP2515ConfiguratorTest, ConfigureTXBuffer) {
89 EXPECT_CALL(mockSPI, writeByte(MCP2515Configurator::TXB0CTRL, 0x00)).Times(1);
90 configurator.configureTXBuffer();
91}
92
101TEST_F(MCP2515ConfiguratorTest, ConfigureRXBuffer) {
102 EXPECT_CALL(mockSPI, writeByte(MCP2515Configurator::RXB0CTRL, 0x60)).Times(1);
103 configurator.configureRXBuffer();
104}
105
114TEST_F(MCP2515ConfiguratorTest, ConfigureFiltersAndMasks) {
115 EXPECT_CALL(mockSPI, writeByte(0x00, 0xFF)).Times(1);
116 EXPECT_CALL(mockSPI, writeByte(0x01, 0xFF)).Times(1);
117 configurator.configureFiltersAndMasks();
118}
119
128TEST_F(MCP2515ConfiguratorTest, ConfigureInterrupts) {
129 EXPECT_CALL(mockSPI, writeByte(MCP2515Configurator::CANINTE, 0x01)).Times(1);
130 configurator.configureInterrupts();
131}
132
142 EXPECT_CALL(mockSPI, writeByte(MCP2515Configurator::CANCTRL, 0x02)).Times(1);
143 configurator.setMode(0x02);
144}
145
152TEST_F(MCP2515ConfiguratorTest, VerifyModeSuccess) {
153 EXPECT_CALL(mockSPI, readByte(MCP2515Configurator::CANSTAT))
154 .WillOnce(Return(0x80));
155 ASSERT_TRUE(configurator.verifyMode(0x80));
156}
157
164TEST_F(MCP2515ConfiguratorTest, VerifyModeFailure) {
165 EXPECT_CALL(mockSPI, readByte(MCP2515Configurator::CANSTAT))
166 .WillOnce(Return(0x00));
167 ASSERT_FALSE(configurator.verifyMode(0x80));
168}
169
177TEST_F(MCP2515ConfiguratorTest, ReadCANMessageWithData) {
178 EXPECT_CALL(mockSPI, readByte(MCP2515Configurator::CANINTF))
179 .WillOnce(Return(0x01));
180 EXPECT_CALL(mockSPI, readByte(MCP2515Configurator::RXB0SIDH))
181 .WillOnce(Return(0x10));
182 EXPECT_CALL(mockSPI, readByte(MCP2515Configurator::RXB0SIDL))
183 .WillOnce(Return(0x20));
184 EXPECT_CALL(mockSPI, readByte(0x65)).WillOnce(Return(3));
185 EXPECT_CALL(mockSPI, readByte(0x66)).WillOnce(Return(0xA0));
186 EXPECT_CALL(mockSPI, readByte(0x67)).WillOnce(Return(0xB1));
187 EXPECT_CALL(mockSPI, readByte(0x68)).WillOnce(Return(0xC2));
188 EXPECT_CALL(mockSPI, writeByte(MCP2515Configurator::CANINTF, 0x00)).Times(1);
189
190 uint16_t frameID;
191 auto data = configurator.readCANMessage(frameID);
192 ASSERT_EQ(frameID, 0x81);
193 ASSERT_EQ(data.size(), 3);
194 ASSERT_EQ(data[0], 0xA0);
195 ASSERT_EQ(data[1], 0xB1);
196 ASSERT_EQ(data[2], 0xC2);
197}
198
207TEST_F(MCP2515ConfiguratorTest, ReadCANMessageNoData) {
208 EXPECT_CALL(mockSPI, readByte(MCP2515Configurator::CANINTF))
209 .WillOnce(Return(0x00));
210 uint16_t frameID;
211 auto data = configurator.readCANMessage(frameID);
212 ASSERT_TRUE(data.empty());
213}
Definition of the MCP2515Configurator class.
File containing Mock classes to test the SPI controller.
Test fixture for testing the MCP2515Configurator class.
MCP2515Configurator configurator
MCP2515Configurator object.
MockSPIController mockSPI
Mocked SPI controller.
Class that configures the MCP2515 CAN controller.
static constexpr uint8_t RXB0SIDH
static constexpr uint8_t CNF2
static constexpr uint8_t CNF1
static constexpr uint8_t TXB0CTRL
static constexpr uint8_t CNF3
static constexpr uint8_t CANCTRL
static constexpr uint8_t CANINTF
static constexpr uint8_t CANINTE
static constexpr uint8_t RXB0SIDL
static constexpr uint8_t CANSTAT
static constexpr uint8_t RXB0CTRL
Class to emulate the behavior of the SPI controller. (Overrided the Can0)
TEST_F(MCP2515ConfiguratorTest, ResetChipSuccess)
Ensures that resetChip() returns true when the reset is successful.