forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xbox_hid_controller_unittest.cc
169 lines (137 loc) · 6.05 KB
/
xbox_hid_controller_unittest.cc
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/gamepad/xbox_hid_controller.h"
#include <memory>
#include "base/bind.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "device/gamepad/hid_writer.h"
#include "device/gamepad/public/mojom/gamepad.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace device {
namespace {
constexpr size_t kReportLength = 9;
constexpr uint8_t kStopVibration[] = {0x03, // report ID
0x03, 0x00, 0x00,
0x00, // strong magnitude
0x00, // weak magnitude
0xff, 0x00, 0x01};
static_assert(sizeof(kStopVibration) == kReportLength,
"kStopVibration has incorrect size");
constexpr uint8_t kStartVibration[] = {0x03, // report ID
0x03, 0x00, 0x00,
0xff, // strong magnitude
0x7f, // weak magnitude
0xff, 0x00, 0x01};
static_assert(sizeof(kStartVibration) == kReportLength,
"kStartVibration has incorrect size");
// Use 1 ms for all non-zero effect durations. There is no reason to test longer
// delays as they will be skipped anyway.
constexpr double kDurationMillis = 1.0;
// Setting |start_delay| to zero can cause additional reports to be sent.
constexpr double kZeroStartDelayMillis = 0.0;
// Vibration magnitudes for the strong and weak channels of a typical
// dual-rumble vibration effect. kStartVibrationData describes a report with
// these magnitudes.
constexpr double kStrongMagnitude = 1.0; // 100% intensity
constexpr double kWeakMagnitude = 0.5; // 50% intensity
constexpr base::TimeDelta kPendingTaskDuration =
base::TimeDelta::FromMillisecondsD(kDurationMillis);
class FakeHidWriter : public HidWriter {
public:
FakeHidWriter() = default;
~FakeHidWriter() override = default;
// HidWriter implementation.
size_t WriteOutputReport(base::span<const uint8_t> report) override {
output_reports.emplace_back(report.begin(), report.end());
return report.size_bytes();
}
std::vector<std::vector<uint8_t>> output_reports;
};
// Main test fixture
class XboxHidControllerTest : public testing::Test {
public:
XboxHidControllerTest()
: start_vibration_report_(kStartVibration,
kStartVibration + kReportLength),
stop_vibration_report_(kStopVibration, kStopVibration + kReportLength),
callback_count_(0),
callback_result_(
mojom::GamepadHapticsResult::GamepadHapticsResultError) {
auto fake_hid_writer = std::make_unique<FakeHidWriter>();
fake_hid_writer_ = fake_hid_writer.get();
gamepad_ = std::make_unique<XboxHidController>(std::move(fake_hid_writer));
}
void TearDown() override { gamepad_->Shutdown(); }
void PostPlayEffect(
double start_delay,
double strong_magnitude,
double weak_magnitude,
mojom::GamepadHapticsManager::PlayVibrationEffectOnceCallback callback) {
gamepad_->PlayEffect(
mojom::GamepadHapticEffectType::GamepadHapticEffectTypeDualRumble,
mojom::GamepadEffectParameters::New(kDurationMillis, start_delay,
strong_magnitude, weak_magnitude),
std::move(callback), base::ThreadTaskRunnerHandle::Get());
}
void PostResetVibration(
mojom::GamepadHapticsManager::ResetVibrationActuatorCallback callback) {
gamepad_->ResetVibration(std::move(callback),
base::ThreadTaskRunnerHandle::Get());
}
// Callback for PlayEffect or ResetVibration.
void Callback(mojom::GamepadHapticsResult result) {
callback_count_++;
callback_result_ = result;
}
const std::vector<uint8_t> start_vibration_report_;
const std::vector<uint8_t> stop_vibration_report_;
int callback_count_;
mojom::GamepadHapticsResult callback_result_;
FakeHidWriter* fake_hid_writer_;
std::unique_ptr<XboxHidController> gamepad_;
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
DISALLOW_COPY_AND_ASSIGN(XboxHidControllerTest);
};
TEST_F(XboxHidControllerTest, PlayEffect) {
EXPECT_TRUE(fake_hid_writer_->output_reports.empty());
EXPECT_EQ(0, callback_count_);
PostPlayEffect(
kZeroStartDelayMillis, kStrongMagnitude, kWeakMagnitude,
base::BindOnce(&XboxHidControllerTest::Callback, base::Unretained(this)));
// Run the queued task and start vibration.
task_environment_.RunUntilIdle();
EXPECT_THAT(fake_hid_writer_->output_reports,
testing::ElementsAre(start_vibration_report_));
EXPECT_EQ(0, callback_count_);
EXPECT_LT(0U, task_environment_.GetPendingMainThreadTaskCount());
// Finish the effect.
task_environment_.FastForwardBy(kPendingTaskDuration);
EXPECT_THAT(fake_hid_writer_->output_reports,
testing::ElementsAre(start_vibration_report_));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(mojom::GamepadHapticsResult::GamepadHapticsResultComplete,
callback_result_);
EXPECT_EQ(0U, task_environment_.GetPendingMainThreadTaskCount());
}
TEST_F(XboxHidControllerTest, ResetVibration) {
EXPECT_TRUE(fake_hid_writer_->output_reports.empty());
EXPECT_EQ(0, callback_count_);
PostResetVibration(
base::BindOnce(&XboxHidControllerTest::Callback, base::Unretained(this)));
// Run the queued task and reset vibration.
task_environment_.RunUntilIdle();
EXPECT_THAT(fake_hid_writer_->output_reports,
testing::ElementsAre(stop_vibration_report_));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(mojom::GamepadHapticsResult::GamepadHapticsResultComplete,
callback_result_);
EXPECT_EQ(0U, task_environment_.GetPendingMainThreadTaskCount());
}
} // namespace
} // namespace device