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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
/*
* Copyright © Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 or 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mir/input/input_device_info.h>
#include <mir/input/keymap.h>
#include <mir/input/mir_touchpad_config.h>
#include <mir/input/mir_input_config.h>
#include <mir/input/input_device.h>
#include <mir/input/device.h>
#include <mir/input/touchscreen_settings.h>
#include <mir_test_framework/stub_server_platform_factory.h>
#include <mir_test_framework/headless_in_process_server.h>
#include <mir_test_framework/fake_input_device.h>
#include <mir/test/signal.h>
#include <mir/test/event_matchers.h>
#include <mir/test/event_factory.h>
#include <mir/input/input_device_observer.h>
#include <mir/input/input_device_hub.h>
#include <mir/input/seat_observer.h>
#include <mir/observer_registrar.h>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <linux/input.h>
#include <condition_variable>
#include <unordered_map>
#include <chrono>
#include <atomic>
#include <mutex>
namespace mi = mir::input;
namespace mt = mir::test;
namespace mis = mir::input::synthesis;
namespace mtf = mir_test_framework;
namespace geom = mir::geometry;
using namespace std::chrono_literals;
using namespace testing;
using TestSeatReport = mtf::HeadlessInProcessServer;
namespace
{
class NullSeatListener : public mi::SeatObserver
{
public:
void seat_add_device(uint64_t /*id*/) override {}
void seat_remove_device(uint64_t /*id*/) override {}
void seat_dispatch_event(std::shared_ptr<MirEvent const> const& /*event*/) override {}
void seat_set_key_state(
uint64_t /*id*/,
std::vector<uint32_t> const& /*scan_codes*/) override
{
}
void seat_set_pointer_state(uint64_t /*id*/, unsigned /*buttons*/) override {}
void seat_set_cursor_position(float /*cursor_x*/, float /*cursor_y*/) override {}
void seat_set_confinement_region_called(geom::Rectangles const& /*regions*/) override {}
void seat_reset_confinement_regions() override {}
};
}
TEST_F(TestSeatReport, add_device_received)
{
class DeviceAddListener : public NullSeatListener
{
public:
DeviceAddListener(size_t expected_devices)
: expected_devices{expected_devices}
{
}
void seat_add_device(uint64_t id) override
{
seen_ids.push_back(id);
if (seen_ids.size() == expected_devices)
{
seen_expected_devices.raise();
}
}
std::vector<uint64_t> wait_for_devices()
{
seen_expected_devices.wait_for(30s);
return std::move(seen_ids);
}
private:
size_t const expected_devices;
std::vector<uint64_t> seen_ids;
mt::Signal seen_expected_devices;
};
auto listener = std::make_shared<DeviceAddListener>(3);
server.the_seat_observer_registrar()->register_interest(listener);
auto constexpr mouse_name = "mouse";
auto constexpr mouse_uid = "some-sort-of-uid";
auto fake_pointer = mtf::add_fake_input_device(
mi::InputDeviceInfo{mouse_name, mouse_uid, mi::DeviceCapability::pointer});
auto constexpr touch_name = "touchscreen";
auto constexpr touch_uid = "Bottle Rocket";
auto fake_touch = mtf::add_fake_input_device(
mi::InputDeviceInfo{touch_name, touch_uid, mi::DeviceCapability::touchscreen});
auto constexpr keyboard_name = "keybored";
auto constexpr keyboard_uid = "Panther Dash";
auto fake_keyboard = mtf::add_fake_input_device(
mi::InputDeviceInfo{
keyboard_name,
keyboard_uid,
mi::DeviceCapability::alpha_numeric | mi::DeviceCapability::keyboard});
auto device_ids_seen = listener->wait_for_devices();
std::vector<uint64_t> devices;
server.the_input_device_hub()->for_each_input_device(
[&devices](mi::Device const& device)
{
devices.push_back(device.id());
});
EXPECT_THAT(device_ids_seen, IsSubsetOf(devices));
}
TEST_F(TestSeatReport, remove_device_received)
{
class DeviceRemoveListener : public NullSeatListener
{
public:
DeviceRemoveListener(size_t expected_devices)
: expected_devices{expected_devices}
{
}
void seat_add_device(uint64_t id) override
{
seen_ids.push_back(id);
}
void seat_remove_device(uint64_t id) override
{
removed_ids.push_back(id);
if (removed_ids.size() == expected_devices)
seen_expected_devices.raise();
}
bool wait_for_all_device_removals()
{
bool const waited = seen_expected_devices.wait_for(30s);
EXPECT_THAT(removed_ids, UnorderedElementsAreArray(seen_ids));
return waited;
}
private:
size_t const expected_devices;
std::vector<uint64_t> seen_ids;
std::vector<uint64_t> removed_ids;
mt::Signal seen_expected_devices;
};
auto listener = std::make_shared<DeviceRemoveListener>(3);
server.the_seat_observer_registrar()->register_interest(listener);
auto fake_pointer = mtf::add_fake_input_device(
mi::InputDeviceInfo{"name", "uid", mi::DeviceCapability::pointer});
auto fake_touch = mtf::add_fake_input_device(
mi::InputDeviceInfo{"name", "uid", mi::DeviceCapability::touchscreen});
auto fake_keyboard = mtf::add_fake_input_device(
mi::InputDeviceInfo{
"name",
"uid",
mi::DeviceCapability::alpha_numeric | mi::DeviceCapability::keyboard});
fake_keyboard->emit_device_removal();
fake_pointer->emit_device_removal();
fake_touch->emit_device_removal();
EXPECT_TRUE(listener->wait_for_all_device_removals());
}
namespace
{
class DeviceAwaitingObserver : public NullSeatListener
{
public:
DeviceAwaitingObserver(size_t expected_devices)
: expected_devices{expected_devices}
{
}
void seat_add_device(uint64_t /*id*/) override
{
devices_seen++;
if (devices_seen == expected_devices)
{
seen_expected_devices.raise();
}
}
template<typename Period, typename Ratio>
bool wait_for_expected_devices(std::chrono::duration<Period, Ratio> timeout)
{
return seen_expected_devices.wait_for(timeout);
}
private:
size_t const expected_devices;
size_t devices_seen{0};
mt::Signal seen_expected_devices;
};
}
TEST_F(TestSeatReport, dispatch_event_received)
{
int const event_x = 234;
int const event_y = 2097;
class DispatchEventListener : public DeviceAwaitingObserver
{
public:
DispatchEventListener(size_t expected_devices)
: DeviceAwaitingObserver(expected_devices)
{
}
void seat_dispatch_event(std::shared_ptr<MirEvent const> const& event) override
{
if (mir_event_get_type(event.get()) == mir_event_type_input)
{
auto iev = mir_event_get_input_event(event.get());
if (mir_input_event_get_type(iev) == mir_input_event_type_pointer)
{
auto pointer = mir_input_event_get_pointer_event(iev);
EXPECT_THAT(
mir_pointer_event_axis_value(pointer, mir_pointer_axis_relative_x),
FloatNear(event_x, 0.5f));
EXPECT_THAT(
mir_pointer_event_axis_value(pointer, mir_pointer_axis_relative_y),
FloatNear(event_y, 0.5f));
pointer_event_seen.raise();
}
}
}
bool wait_for_matching_input(std::chrono::seconds timeout)
{
return pointer_event_seen.wait_for(timeout);
};
private:
mt::Signal pointer_event_seen;
};
auto listener = std::make_shared<DispatchEventListener>(1);
server.the_seat_observer_registrar()->register_interest(listener);
auto fake_pointer = mtf::add_fake_input_device(
mi::InputDeviceInfo{"name", "uid", mi::DeviceCapability::pointer});
listener->wait_for_expected_devices(30s);
fake_pointer->emit_event(
mis::a_pointer_event()
.with_movement(event_x, event_y));
EXPECT_TRUE(listener->wait_for_matching_input(30s));
}
TEST_F(TestSeatReport, key_state_received)
{
class KeyStateListener : public DeviceAwaitingObserver
{
public:
KeyStateListener(size_t expected_devices)
: DeviceAwaitingObserver(expected_devices)
{
}
void seat_set_key_state(
uint64_t id,
std::vector<uint32_t> const& scan_codes) override
{
key_state = scan_codes;
this->id = id;
key_state_received.raise();
}
void wait_for_key_state(
std::chrono::seconds timeout,
uint64_t expected_id,
std::vector<uint32_t> const& expected_state)
{
if (!key_state_received.wait_for(timeout))
{
BOOST_THROW_EXCEPTION(std::runtime_error("Timeout waiting for key state event"));
}
EXPECT_THAT(key_state, ContainerEq(expected_state));
EXPECT_THAT(id, Eq(expected_id));
}
private:
mt::Signal key_state_received;
uint64_t id;
std::vector<uint32_t> key_state;
};
auto listener = std::make_shared<KeyStateListener>(1);
server.the_seat_observer_registrar()->register_interest(listener);
auto const fake_keyboard_name = "fake-keyboard";
auto fake_keyboard = mtf::add_fake_input_device(
mi::InputDeviceInfo{
fake_keyboard_name,
"uid",
mi::DeviceCapability::keyboard | mi::DeviceCapability::alpha_numeric});
ASSERT_TRUE(listener->wait_for_expected_devices(30s));
std::vector<uint32_t> const sent_state{23, 32, 55, 9, 233};
{
/* Take a temporary copy of sent_state to ensure the lifetime of
* the value we send to emit_key_state expires before we expect
* the Observer to see the event...
*/
auto temporary_copy = sent_state;
fake_keyboard->emit_key_state(temporary_copy);
}
std::optional<size_t> fake_keyboard_id = std::nullopt;
server.the_input_device_hub()->for_each_input_device(
[&fake_keyboard_id, fake_keyboard_name](auto const& device)
{
if(device.name() == fake_keyboard_name)
fake_keyboard_id = device.id();
});
if (!fake_keyboard_id.has_value())
FAIL() << "Fake keyboard not found";
listener->wait_for_key_state(
30s,
*fake_keyboard_id,
sent_state);
}
|