-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathSwitchActionTest.cpp
More file actions
80 lines (69 loc) · 3.44 KB
/
SwitchActionTest.cpp
File metadata and controls
80 lines (69 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2021 Charles Tytler
#include <gtest/gtest.h>
#include "StreamdeckContext/SendActions/SwitchAction.h"
#include "ElgatoSD/test/MockESDConnectionManager.h"
#include "SimulatorInterface/SimConnectionManager.h"
namespace test
{
class SwitchActionKeyPressTestFixture : public ::testing::Test
{
public:
SwitchActionKeyPressTestFixture()
: // Mock DCS socket uses the reverse rx and tx ports of simulator_interface so it can communicate with it.
mock_dcs(connection_settings.ip_address, connection_settings.tx_port, connection_settings.rx_port),
// Create default json payload.
payload({{"state", 0},
{"settings",
{{"send_address", send_address},
{"send_when_first_state_value", send_when_first_state_value},
{"send_when_second_state_value", send_when_second_state_value}}}})
{
sim_connection_manager.connect_to_protocol(Protocol::DCS_ExportScript, connection_settings);
simulator_interface = sim_connection_manager.get_interface(Protocol::DCS_ExportScript);
// Consume intial reset command sent to to mock_dcs.
(void)mock_dcs.receive_stream();
}
std::string send_address = "23,2";
std::string send_when_first_state_value = "6";
std::string send_when_second_state_value = "7";
json payload;
SimulatorConnectionSettings connection_settings = {"1948", "1949", "127.0.0.1"};
UdpSocket mock_dcs; // A socket that will mock Send/Receive messages from DCS.
MockESDConnectionManager esd_connection_manager; // Streamdeck connection manager, using mock class definition.
SwitchAction fixture_context;
SimulatorInterface *simulator_interface; // Simulator Interface to test.
private:
SimConnectionManager sim_connection_manager;
};
TEST_F(SwitchActionKeyPressTestFixture, handle_keyup_switch_in_first_state)
{
fixture_context.handleButtonReleasedEvent(simulator_interface, &esd_connection_manager, payload);
const std::stringstream ss_received = mock_dcs.receive_stream();
std::string expected_command = "C" + send_address + "," + send_when_first_state_value;
EXPECT_EQ(expected_command, ss_received.str());
}
TEST_F(SwitchActionKeyPressTestFixture, handle_keyup_switch_in_second_state)
{
payload["state"] = 1;
fixture_context.handleButtonReleasedEvent(simulator_interface, &esd_connection_manager, payload);
const std::stringstream ss_received = mock_dcs.receive_stream();
std::string expected_command = "C" + send_address + "," + send_when_second_state_value;
EXPECT_EQ(expected_command, ss_received.str());
}
TEST_F(SwitchActionKeyPressTestFixture, handle_keydown_switch)
{
fixture_context.handleButtonPressedEvent(simulator_interface, &esd_connection_manager, payload);
const std::stringstream ss_received = mock_dcs.receive_stream();
// Expect no command sent (empty string is due to mock socket functionality).
std::string expected_command = "";
EXPECT_EQ(expected_command, ss_received.str());
}
TEST_F(SwitchActionKeyPressTestFixture, handle_keyup_switch_empty_value)
{
payload["settings"]["send_when_first_state_value"] = "";
fixture_context.handleButtonReleasedEvent(simulator_interface, &esd_connection_manager, payload);
const std::stringstream ss_received = mock_dcs.receive_stream();
std::string expected_command = "";
EXPECT_EQ(expected_command, ss_received.str());
}
} // namespace test