File: test_cursor_observer.cpp

package info (click to toggle)
mir 2.25.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,080 kB
  • sloc: cpp: 192,777; xml: 13,784; ansic: 8,207; python: 1,304; sh: 794; makefile: 258
file content (108 lines) | stat: -rw-r--r-- 3,537 bytes parent folder | download
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
/*
 * 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_test_framework/fake_input_device.h>
#include <mir_test_framework/fake_input_server_configuration.h>
#include <mir_test_framework/stub_server_platform_factory.h>
#include <mir_test_framework/temporary_environment_value.h>
#include <mir/test/auto_unblock_thread.h>
#include <mir/test/signal_actions.h>
#include <mir/test/event_factory.h>

#include <mir/main_loop.h>
#include <mir/input/cursor_observer.h>
#include <mir/input/cursor_observer_multiplexer.h>
#include <mir/input/input_dispatcher.h>
#include <mir/input/input_manager.h>
#include <mir/input/input_device_info.h>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

namespace mi = mir::input;
namespace mis = mir::input::synthesis;
namespace mt = mir::test;
namespace mtf = mir_test_framework;

namespace
{
using namespace ::testing;

struct MockCursorObserver : public mi::CursorObserver
{
    MOCK_METHOD(void, cursor_moved_to, (float, float), (override));

    ~MockCursorObserver() noexcept {}

    void pointer_usable() {}
    void pointer_unusable() {}
};

struct CursorObserverIntegrationTest : testing::Test, mtf::FakeInputServerConfiguration
{
    mtf::TemporaryEnvironmentValue input_lib{"MIR_SERVER_PLATFORM_INPUT_LIB", "mir:stub-input"};
    mtf::TemporaryEnvironmentValue real_input{"MIR_SERVER_TESTS_USE_REAL_INPUT", "1"};
    mtf::TemporaryEnvironmentValue no_key_repeat{"MIR_SERVER_ENABLE_KEY_REPEAT", "0"};

    void SetUp() override
    {
        input_manager->start();
        input_dispatcher->start();

        cursor_observer_multiplexer->register_interest(cursor_observer);
    }

    void TearDown() override
    {
        input_dispatcher->stop();
        input_manager->stop();
    }

    std::shared_ptr<MockCursorObserver> cursor_observer{std::make_shared<MockCursorObserver>()};
    std::shared_ptr<mi::InputManager> input_manager{the_input_manager()};
    std::shared_ptr<mi::InputDispatcher> const input_dispatcher{the_input_dispatcher()};
    std::shared_ptr<mir::MainLoop> const main_loop{the_main_loop()};
    std::shared_ptr<mi::CursorObserverMultiplexer> cursor_observer_multiplexer{the_cursor_observer_multiplexer()};

    std::unique_ptr<mtf::FakeInputDevice> fake_mouse{
        mtf::add_fake_input_device(mi::InputDeviceInfo{"mouse", "mouse-uid" , mi::DeviceCapability::pointer})
        };

    mir::test::AutoUnblockThread main_thread{
        [this] { main_loop->stop(); },
        [this]
        {
            main_loop->run();
        }};
};

}

TEST_F(CursorObserverIntegrationTest, cursor_observer_receives_motion)
{
    using namespace ::testing;

    auto signal = std::make_shared<mt::Signal>();

    static const float x = 100.f;
    static const float y = 100.f;

    EXPECT_CALL(*cursor_observer, cursor_moved_to(x, y)).Times(1).WillOnce(mt::WakeUp(signal));

    fake_mouse->emit_event(mis::a_pointer_event().with_movement(x, y));

    signal->wait_for(std::chrono::seconds{10});
}