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
|
/*
* 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 <gtest/gtest.h>
#include <gmock/gmock.h>
#include "src/server/input/default_event_builder.h"
#include "src/platforms/x11/input/input_platform.h"
#include "src/platforms/x11/input/input_device.h"
#include "mir/events/event_private.h"
#include "mir/dispatch/dispatchable.h"
#include "mir_toolkit/events/input/input_event.h"
#include "mir/test/doubles/mock_input_sink.h"
#include "mir/test/doubles/mock_input_seat.h"
#include "mir/test/doubles/mock_input_device_registry.h"
#include "mir/test/doubles/mock_x11.h"
#include "mir/test/doubles/mock_xkb.h"
#include "mir/test/doubles/mock_x11_resources.h"
#include "mir/test/doubles/advanceable_clock.h"
#include "mir/test/fake_shared.h"
#include "mir/test/event_matchers.h"
namespace md = mir::dispatch;
namespace mi = mir::input;
namespace mt = mir::test;
namespace mtd = mt::doubles;
using namespace ::testing;
namespace
{
struct X11PlatformTest : ::testing::Test
{
NiceMock<mtd::MockInputSink> mock_pointer_sink;
NiceMock<mtd::MockInputSink> mock_keyboard_sink;
NiceMock<mtd::MockInputSeat> mock_seat;
NiceMock<mtd::MockX11> mock_x11;
NiceMock<mtd::MockXkb> mock_xkb;
NiceMock<mtd::MockInputDeviceRegistry> mock_registry;
mtd::AdvanceableClock clock;
mir::input::DefaultEventBuilder builder{
0,
mt::fake_shared(clock)};
mir::input::X::XInputPlatform x11_platform{
mt::fake_shared(mock_registry),
std::make_shared<mtd::MockX11Resources>()};
std::vector<std::shared_ptr<mi::InputDevice>> devices;
enum class GrabState { FOCUSIN, FOCUSOUT, ENTERNOTIFY, LEAVENOTIFY };
GrabState grab_state{GrabState::FOCUSIN};
void capture_devices()
{
ON_CALL(mock_registry, add_device(_))
.WillByDefault(Invoke([this](auto const& dev) -> std::weak_ptr<mi::Device>
{
devices.push_back(dev);
auto const info = dev->get_device_info();
if (contains(info.capabilities, mi::DeviceCapability::pointer))
dev->start(&mock_pointer_sink, &builder);
else if (contains(info.capabilities, mi::DeviceCapability::keyboard))
dev->start(&mock_keyboard_sink, &builder);
return {};
})
);
}
void prepare_event_processing()
{
capture_devices();
x11_platform.start();
}
void process_input_event()
{
x11_platform.dispatchable()->dispatch(md::FdEvent::readable);
}
};
}
MATCHER(ButtonUpEventWithNoButtonsPressed, "")
{
auto pev = mt::maybe_pointer_event(mt::to_address(arg));
return mt::button_event_matches(pev, 0, 0, mir_pointer_action_button_up, 0, true, true, false);
}
TEST_F(X11PlatformTest, registers_two_devices)
{
EXPECT_CALL(mock_registry, add_device(_)).Times(2);
x11_platform.start();
}
TEST_F(X11PlatformTest, unregisters_devices_on_stop)
{
EXPECT_CALL(mock_registry, add_device(_)).Times(2);
EXPECT_CALL(mock_registry, remove_device(_)).Times(2);
x11_platform.start();
x11_platform.stop();
}
TEST_F(X11PlatformTest, registered_devices_mimic_mouse_and_keyboard)
{
capture_devices();
x11_platform.start();
ASSERT_THAT(devices.size(), Eq(2u));
auto const& first_info = devices[0]->get_device_info();
auto const& second_info = devices[1]->get_device_info();
EXPECT_FALSE(first_info.capabilities == second_info.capabilities);
EXPECT_TRUE(contains(first_info.capabilities | second_info.capabilities,
mi::DeviceCapability::pointer));
EXPECT_TRUE(contains(first_info.capabilities | second_info.capabilities,
mi::DeviceCapability::keyboard));
}
|