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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/exo/wayland/serial_tracker.h"
#include <linux/input-event-codes.h>
#include <wayland-client-core.h>
#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "components/exo/wayland/test/client_util.h"
#include "components/exo/wayland/test/server_util.h"
#include "components/exo/wayland/test/shell_client_data.h"
#include "components/exo/wayland/test/test_client.h"
#include "components/exo/wayland/test/wayland_server_test.h"
namespace exo::wayland {
using SerialTrackerTest = test::WaylandServerTest;
namespace {
class InputListenerImpl2 : public test::InputListener {
public:
// test::InputListener:
void OnButtonPressed(uint32_t serial, uint32_t button) override {
serial_map[button] = serial;
}
void OnButtonReleased(uint32_t serial, uint32_t button) override {
serial_map[button] = serial;
}
base::flat_map<uint32_t, uint32_t> serial_map;
};
} // namespace
TEST_F(SerialTrackerTest, CheckButtonsSeparately) {
test::ResourceKey surface_key;
InputListenerImpl2* input_listener = nullptr;
PostToClientAndWait([&](test::TestClient* client) {
ASSERT_TRUE(client->InitShmBufferFactory(256 * 256 * 4));
auto* data_ptr =
client->set_data(std::make_unique<test::ShellClientData>(client));
data_ptr->CreateXdgToplevel();
data_ptr->CreateAndAttachBuffer({256, 256});
data_ptr->Commit();
auto input_listener_impl = std::make_unique<InputListenerImpl2>();
input_listener = input_listener_impl.get();
data_ptr->set_input_listener(std::move(input_listener_impl));
surface_key = data_ptr->GetSurfaceResourceKey();
});
Surface* surface = test::server_util::GetUserDataForResource<Surface>(
server_.get(), surface_key);
auto* generator = GetEventGenerator();
generator->MoveMouseToCenterOf(surface->window());
generator->PressLeftButton();
// process events on client side.
PostToClientAndWait([&](test::TestClient* client) {});
EXPECT_TRUE(base::Contains(input_listener->serial_map, BTN_LEFT));
EXPECT_FALSE(base::Contains(input_listener->serial_map, BTN_MIDDLE));
EXPECT_FALSE(base::Contains(input_listener->serial_map, BTN_RIGHT));
generator->PressButton(ui::EF_MIDDLE_MOUSE_BUTTON);
generator->PressRightButton();
// process events on client side.
PostToClientAndWait([&](test::TestClient* client) {});
EXPECT_TRUE(base::Contains(input_listener->serial_map, BTN_LEFT));
EXPECT_TRUE(base::Contains(input_listener->serial_map, BTN_MIDDLE));
EXPECT_TRUE(base::Contains(input_listener->serial_map, BTN_RIGHT));
auto* tracker = server_->serial_tracker_for_test();
auto get_event_type_for_serial = [=](uint32_t button) -> uint32_t {
return tracker->GetEventType(input_listener->serial_map[button])
.value_or(SerialTracker::EventType::OTHER_EVENT);
};
EXPECT_EQ(SerialTracker::EventType::POINTER_LEFT_BUTTON_DOWN,
get_event_type_for_serial(BTN_LEFT));
EXPECT_EQ(SerialTracker::EventType::POINTER_MIDDLE_BUTTON_DOWN,
get_event_type_for_serial(BTN_MIDDLE));
EXPECT_EQ(SerialTracker::EventType::POINTER_RIGHT_BUTTON_DOWN,
get_event_type_for_serial(BTN_RIGHT));
generator->ReleaseLeftButton();
// process events on client side.
PostToClientAndWait([&](test::TestClient* client) {});
EXPECT_EQ(SerialTracker::EventType::POINTER_LEFT_BUTTON_UP,
get_event_type_for_serial(BTN_LEFT));
EXPECT_EQ(SerialTracker::EventType::POINTER_MIDDLE_BUTTON_DOWN,
get_event_type_for_serial(BTN_MIDDLE));
EXPECT_EQ(SerialTracker::EventType::POINTER_RIGHT_BUTTON_DOWN,
get_event_type_for_serial(BTN_RIGHT));
generator->ReleaseButton(ui::EF_MIDDLE_MOUSE_BUTTON);
// process events on client side.
PostToClientAndWait([&](test::TestClient* client) {});
EXPECT_EQ(SerialTracker::EventType::POINTER_LEFT_BUTTON_UP,
get_event_type_for_serial(BTN_LEFT));
EXPECT_EQ(SerialTracker::EventType::POINTER_MIDDLE_BUTTON_UP,
get_event_type_for_serial(BTN_MIDDLE));
EXPECT_EQ(SerialTracker::EventType::POINTER_RIGHT_BUTTON_DOWN,
get_event_type_for_serial(BTN_RIGHT));
// Forward/Back events
EXPECT_EQ(SerialTracker::EventType::OTHER_EVENT,
get_event_type_for_serial(BTN_FORWARD));
EXPECT_EQ(SerialTracker::EventType::OTHER_EVENT,
get_event_type_for_serial(BTN_BACK));
generator->PressButton(ui::EF_FORWARD_MOUSE_BUTTON);
generator->PressButton(ui::EF_BACK_MOUSE_BUTTON);
PostToClientAndWait([&](test::TestClient* client) {});
EXPECT_EQ(SerialTracker::EventType::POINTER_FORWARD_BUTTON_DOWN,
get_event_type_for_serial(BTN_EXTRA));
EXPECT_EQ(SerialTracker::EventType::POINTER_BACK_BUTTON_DOWN,
get_event_type_for_serial(BTN_SIDE));
}
} // namespace exo::wayland
|