summaryrefslogtreecommitdiff
path: root/src/tests/input_common/calibration_configuration_job.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/input_common/calibration_configuration_job.cpp')
-rw-r--r--src/tests/input_common/calibration_configuration_job.cpp136
1 files changed, 136 insertions, 0 deletions
diff --git a/src/tests/input_common/calibration_configuration_job.cpp b/src/tests/input_common/calibration_configuration_job.cpp
new file mode 100644
index 000000000..8c77d81e9
--- /dev/null
+++ b/src/tests/input_common/calibration_configuration_job.cpp
@@ -0,0 +1,136 @@
1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <array>
6#include <string>
7#include <thread>
8#include <boost/asio.hpp>
9#include <boost/crc.hpp>
10#include <catch2/catch.hpp>
11
12#include "input_common/drivers/udp_client.h"
13#include "input_common/helpers/udp_protocol.h"
14
15class FakeCemuhookServer {
16public:
17 FakeCemuhookServer()
18 : socket(io_service, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 0)) {}
19
20 ~FakeCemuhookServer() {
21 is_running = false;
22 boost::system::error_code error_code;
23 socket.shutdown(boost::asio::socket_base::shutdown_both, error_code);
24 socket.close();
25 if (handler.joinable()) {
26 handler.join();
27 }
28 }
29
30 u16 GetPort() {
31 return socket.local_endpoint().port();
32 }
33
34 std::string GetHost() {
35 return socket.local_endpoint().address().to_string();
36 }
37
38 void Run(const std::vector<InputCommon::CemuhookUDP::Response::TouchPad> touch_movement_path) {
39 constexpr size_t HeaderSize = sizeof(InputCommon::CemuhookUDP::Header);
40 constexpr size_t PadDataSize =
41 sizeof(InputCommon::CemuhookUDP::Message<InputCommon::CemuhookUDP::Response::PadData>);
42
43 REQUIRE(touch_movement_path.size() > 0);
44 is_running = true;
45 handler = std::thread([touch_movement_path, this]() {
46 auto current_touch_position = touch_movement_path.begin();
47 while (is_running) {
48 boost::asio::ip::udp::endpoint sender_endpoint;
49 boost::system::error_code error_code;
50 auto received_size = socket.receive_from(boost::asio::buffer(receive_buffer),
51 sender_endpoint, 0, error_code);
52
53 if (received_size < HeaderSize) {
54 continue;
55 }
56
57 InputCommon::CemuhookUDP::Header header{};
58 std::memcpy(&header, receive_buffer.data(), HeaderSize);
59 switch (header.type) {
60 case InputCommon::CemuhookUDP::Type::PadData: {
61 InputCommon::CemuhookUDP::Response::PadData pad_data{};
62 pad_data.touch[0] = *current_touch_position;
63 const auto pad_message = InputCommon::CemuhookUDP::CreateMessage(
64 InputCommon::CemuhookUDP::SERVER_MAGIC, pad_data, 0);
65 std::memcpy(send_buffer.data(), &pad_message, PadDataSize);
66 socket.send_to(boost::asio::buffer(send_buffer, PadDataSize), sender_endpoint,
67 0, error_code);
68
69 bool can_advance =
70 std::next(current_touch_position) != touch_movement_path.end();
71 if (can_advance) {
72 std::advance(current_touch_position, 1);
73 }
74 break;
75 }
76 case InputCommon::CemuhookUDP::Type::PortInfo:
77 case InputCommon::CemuhookUDP::Type::Version:
78 default:
79 break;
80 }
81 }
82 });
83 }
84
85private:
86 boost::asio::io_service io_service;
87 boost::asio::ip::udp::socket socket;
88 std::array<u8, InputCommon::CemuhookUDP::MAX_PACKET_SIZE> send_buffer;
89 std::array<u8, InputCommon::CemuhookUDP::MAX_PACKET_SIZE> receive_buffer;
90 bool is_running = false;
91 std::thread handler;
92};
93
94TEST_CASE("CalibrationConfigurationJob completed", "[input_common]") {
95 Common::Event complete_event;
96 FakeCemuhookServer server;
97 server.Run({{
98 .is_active = 1,
99 .x = 0,
100 .y = 0,
101 },
102 {
103 .is_active = 1,
104 .x = 200,
105 .y = 200,
106 }});
107
108 InputCommon::CemuhookUDP::CalibrationConfigurationJob::Status status{};
109 u16 min_x{};
110 u16 min_y{};
111 u16 max_x{};
112 u16 max_y{};
113 InputCommon::CemuhookUDP::CalibrationConfigurationJob job(
114 server.GetHost(), server.GetPort(),
115 [&status,
116 &complete_event](InputCommon::CemuhookUDP::CalibrationConfigurationJob::Status status_) {
117 status = status_;
118 if (status ==
119 InputCommon::CemuhookUDP::CalibrationConfigurationJob::Status::Completed) {
120 complete_event.Set();
121 }
122 },
123 [&](u16 min_x_, u16 min_y_, u16 max_x_, u16 max_y_) {
124 min_x = min_x_;
125 min_y = min_y_;
126 max_x = max_x_;
127 max_y = max_y_;
128 });
129
130 complete_event.WaitUntil(std::chrono::system_clock::now() + std::chrono::seconds(10));
131 REQUIRE(status == InputCommon::CemuhookUDP::CalibrationConfigurationJob::Status::Completed);
132 REQUIRE(min_x == 0);
133 REQUIRE(min_y == 0);
134 REQUIRE(max_x == 200);
135 REQUIRE(max_y == 200);
136}