Add:Led control test code.

This commit is contained in:
Fancy code 2024-02-21 03:30:38 -08:00
parent fdbe733bc7
commit 818aea1417
6 changed files with 94 additions and 28 deletions

View File

@ -59,7 +59,8 @@ public:
void SetAllLedsResult(std::map<std::string, std::shared_ptr<VLedHal>> &allLeds);
// void SetKeyEvent(const std::string keyName, const KeyHalEvent &event); // TODO: unused function?
void SetKeyClick(const std::string &keyName, const unsigned int &pressingTimeMs = 200);
void SetLedStateExpectations(const std::string &ledName, const LedState &state);
void SetLedStateExpectations(const std::string &ledName, const LedState &state, const unsigned int &aliveTimeMs,
const unsigned int &blinkTimeMs);
protected:
virtual void DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs) {}
@ -76,7 +77,7 @@ private:
private:
void SetAllLedsResult(std::shared_ptr<IHalCpp> &vMock, std::map<std::string, std::shared_ptr<VLedHal>> &allLeds);
std::shared_ptr<VLedHal> SearchLed(const std::string &ledName);
std::shared_ptr<VLedHal> &SearchLed(const std::string &ledName);
void InitAllLedsMock(std::map<std::string, std::shared_ptr<VLedHal>> &allLeds);
void InitLedsMock(std::shared_ptr<VLedHal> &vMock);

View File

@ -78,20 +78,22 @@ void HalTestTool::SetKeyClick(const std::string &keyName, const unsigned int &pr
LogWarning("Key mock error.\n");
}
}
void HalTestTool::SetLedStateExpectations(const std::string &ledName, const LedState &state)
void HalTestTool::SetLedStateExpectations(const std::string &ledName, const LedState &state,
const unsigned int &aliveTimeMs, const unsigned int &blinkTimeMs)
{
std::shared_ptr<VLedHal> led = SearchLed(ledName);
if (!led) {
LogError("Can't set led state, led not found.\n");
return;
}
std::shared_ptr<LedControlMock> ledMock = std::dynamic_pointer_cast<LedControlMock>(led);
if (ledMock) {
LedControlMock::SetLedStateMock(ledMock, state);
}
else {
LogWarning("led mock error.\n");
}
std::shared_ptr<VLedHal> &led = SearchLed(ledName);
// if (!led) {
// LogError("Can't set led state, led not found.\n");
// return;
// }
// std::shared_ptr<LedControlMock> ledMock = std::dynamic_pointer_cast<LedControlMock>(led);
// if (ledMock) {
// LedControlMock::SetLedStateMock(ledMock, state, aliveTimeMs, blinkTimeMs);
// }
// else {
// LogWarning("led mock error.\n");
// }
LedControlMock::SetLedStateMock(led, state, aliveTimeMs, blinkTimeMs);
}
void HalTestTool::KeyEventHappendOnce(std::shared_ptr<VKeyHal> &vMock, const unsigned int &pressingTimeMs)
{
@ -192,18 +194,19 @@ void HalTestTool::SetAllLedsResult(std::shared_ptr<IHalCpp> &vMock,
.WillRepeatedly(DoAll(WithArgs<0>(Invoke(getAllLeds)), Return(CreateStatusCode(STATUS_CODE_OK))));
InitAllLedsMock(allLeds);
}
std::shared_ptr<VLedHal> HalTestTool::SearchLed(const std::string &ledName)
std::shared_ptr<VLedHal> &HalTestTool::SearchLed(const std::string &ledName)
{
static std::shared_ptr<VLedHal> noLed = std::make_shared<VLedHal>();
std::shared_ptr<LedControlMock> mock;
std::map<std::string, std::shared_ptr<VLedHal>>::iterator iter;
iter = mAllLeds.find(ledName);
if (iter != mAllLeds.end()) {
mock = std::dynamic_pointer_cast<LedControlMock>(mAllLeds[ledName]);
// mock = std::dynamic_pointer_cast<LedControlMock>(mAllLeds[ledName]);
std::shared_ptr<VLedHal> &existLed = mAllLeds[ledName];
return existLed;
}
else {
LogWarning("Can't found the led control.\n");
}
return mock;
LogWarning("Can't found the led control.\n");
return noLed;
}
void HalTestTool::InitAllLedsMock(std::map<std::string, std::shared_ptr<VLedHal>> &allLeds)
{

View File

@ -35,14 +35,35 @@ StatusCode LedControlTest::SetLedStateTrace(const LedState &state)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
void LedControlMock::SetLedStateMock(std::shared_ptr<LedControlMock> &mock, const LedState &state)
void LedControlMock::SetLedStateMock(std::shared_ptr<VLedHal> &vMock, const LedState &state,
const unsigned int &aliveTimeMs, const unsigned int &blinkTimeMs)
{
LogInfo("LedControlMock::SetLedState\n");
std::shared_ptr<LedControlMock> mock = std::dynamic_pointer_cast<LedControlMock>(vMock);
if (!mock) {
LogError("Can't set led state, led not found.\n");
return;
}
constexpr int SOMEBODY_CONTROL_LED = 1;
EXPECT_CALL(*mock.get(), SetLedStateTrace(_))
// .Times(SOMEBODY_CONTROL_LED)
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
EXPECT_CALL(*mock.get(), SetLedStateTrace(state))
.Times(SOMEBODY_CONTROL_LED)
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
auto defaultExpectations = [=, &vMock]() {
// std::shared_ptr<LedControlMock> mock = std::dynamic_pointer_cast<LedControlMock>(vMock);
// if (!mock) {
// LogError("Can't set led state, led not found.\n");
// return;
// }
// EXPECT_CALL(*mock.get(), SetLedStateTrace(state))
// .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
};
if (blinkTimeMs == LED_NOT_BLINK) {
EXPECT_CALL(*mock.get(), SetLedStateTrace(state))
.Times(SOMEBODY_CONTROL_LED)
.WillRepeatedly(DoAll(Invoke(defaultExpectations), Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
}
else {
EXPECT_CALL(*mock.get(), SetLedStateTrace(state))
.Times(AtLeast(SOMEBODY_CONTROL_LED))
.WillRepeatedly(DoAll(Invoke(defaultExpectations), Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
}
}

View File

@ -38,6 +38,7 @@ public:
MOCK_METHOD1(SetLedStateTrace, StatusCode(const LedState &));
public:
static void SetLedStateMock(std::shared_ptr<LedControlMock> &mock, const LedState &state);
static void SetLedStateMock(std::shared_ptr<VLedHal> &vMock, const LedState &state, const unsigned int &aliveTimeMs,
const unsigned int &blinkTimeMs);
};
#endif

View File

@ -149,4 +149,44 @@ TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed)
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed2
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed2)
{
SetAllLedsResult(mAllLedsMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
constexpr int BLINK_TIME = 500;
ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, BLINK_TIME);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed3
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed3)
{
SetAllLedsResult(mAllLedsMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
constexpr int BLINK_TIME = 500;
constexpr int KEEP_ALIVE = 1000;
ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE, BLINK_TIME);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed4
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed4)
{
SetAllLedsResult(mAllLedsMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
constexpr int KEEP_ALIVE = 1000;
ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE, LED_NOT_BLINK);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IDeviceManager::GetInstance()->UnInit();
}
} // namespace DeviceManagerTest

View File

@ -42,7 +42,7 @@ std::shared_ptr<VirtualLedControl> DeviceManagerTestTool::ControlLed(const std::
const unsigned int &aliveTimeMs,
const unsigned int &blinkTimeMs)
{
HalTestTool::SetLedStateExpectations(ledName, state);
HalTestTool::SetLedStateExpectations(ledName, state, aliveTimeMs, blinkTimeMs);
std::shared_ptr<VirtualLedControl> ledControl =
std::make_shared<SingleControlMock>(state, aliveTimeMs, blinkTimeMs);
IDeviceManager::GetInstance()->ControlLed(ledName, ledControl);