~ubuntu-branches/ubuntu/wily/mir/wily-proposed

« back to all changes in this revision

Viewing changes to tests/unit-tests/input/android/test_input_dispatcher.cpp

  • Committer: Package Import Robot
  • Author(s): CI Train Bot
  • Date: 2015-05-12 13:12:55 UTC
  • mto: This revision was merged to the branch mainline in revision 96.
  • Revision ID: package-import@ubuntu.com-20150512131255-y7z12i8n4pbvo70x
Tags: upstream-0.13.0+15.10.20150512
Import upstream version 0.13.0+15.10.20150512

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2015 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Andreas Pokorny <andreas.pokorny@canonical.com>
 
17
 */
 
18
 
 
19
#include "InputDispatcher.h"
 
20
#include "InputApplication.h"
 
21
#include "InputWindow.h"
 
22
#include "InputWindow.h"
 
23
 
 
24
#include "mir/input/android/android_input_lexicon.h"
 
25
#include "mir/events/event_private.h"
 
26
#include "mir/report/legacy_input_report.h"
 
27
#include "src/server/report/null_report_factory.h"
 
28
#include "src/server/input/android/event_filter_dispatcher_policy.h"
 
29
 
 
30
#include "mir_test_doubles/stub_input_enumerator.h"
 
31
#include "mir_test_doubles/mock_event_filter.h"
 
32
#include "mir_test_doubles/null_logger.h"
 
33
#include "mir_test/fake_shared.h"
 
34
#include "mir_test/event_matchers.h"
 
35
 
 
36
#include <gtest/gtest.h>
 
37
#include <gmock/gmock.h>
 
38
 
 
39
#include <linux/input.h>
 
40
 
 
41
namespace mt = mir::test;
 
42
namespace mtd = mt::doubles;
 
43
namespace mia = mir::input::android;
 
44
using namespace std::literals;
 
45
 
 
46
namespace
 
47
{
 
48
struct MockInputDispatcherPolicy : mia::EventFilterDispatcherPolicy
 
49
{
 
50
    using mia::EventFilterDispatcherPolicy::EventFilterDispatcherPolicy;
 
51
    MOCK_METHOD1(handle,void(MirEvent const& event));
 
52
    std::chrono::nanoseconds interceptKeyBeforeDispatching(
 
53
        const android::sp<android::InputWindowHandle>&,
 
54
            const android::KeyEvent* keyEvent, uint32_t)
 
55
    {
 
56
        MirEvent mir_ev;
 
57
        mia::Lexicon::translate(keyEvent, mir_ev);
 
58
        handle(mir_ev);
 
59
        return 0ns;
 
60
    }
 
61
};
 
62
 
 
63
 
 
64
struct InputDispatcher : testing::Test
 
65
{
 
66
    mtd::StubInputEnumerator enumerator;
 
67
    testing::NiceMock<mtd::MockEventFilter> filter;
 
68
    testing::NiceMock<MockInputDispatcherPolicy> filter_policy{mt::fake_shared(filter), true};
 
69
    android::InputDispatcher dispatcher{mt::fake_shared(filter_policy), mir::report::null_input_report(), mt::fake_shared(enumerator)};
 
70
    mtd::NullLogger logger;
 
71
    InputDispatcher()
 
72
    {
 
73
        mir::report::legacy_input::initialize(mt::fake_shared(logger));
 
74
    }
 
75
 
 
76
    android::NotifyKeyArgs create_key_event(int32_t key_code, int32_t scan_code, int32_t action = AKEY_EVENT_ACTION_DOWN)
 
77
    {
 
78
        static auto time = 0ns;
 
79
        time += 1ns;
 
80
        int32_t deviceid = 0;
 
81
        uint32_t source = AINPUT_SOURCE_KEYBOARD;
 
82
        uint32_t policy_flag = 0;
 
83
        int32_t flag = 0;
 
84
        int32_t meta_state = 0;
 
85
        auto down_time = 0ns;
 
86
 
 
87
        return android::NotifyKeyArgs{time, deviceid, source, policy_flag, action, flag, key_code, scan_code, meta_state, down_time};
 
88
    }
 
89
};
 
90
 
 
91
}
 
92
 
 
93
TEST_F(InputDispatcher, forwards_multiple_downs)
 
94
{
 
95
    using namespace ::testing;
 
96
    InSequence seq;
 
97
    EXPECT_CALL(filter_policy, handle(AllOf(mt::KeyDownEvent(), mt::KeyOfScanCode(KEY_RIGHTSHIFT))));
 
98
    EXPECT_CALL(filter_policy, handle(AllOf(mt::KeyDownEvent(), mt::KeyOfScanCode(KEY_M))));
 
99
    auto first_key_event = create_key_event(0, KEY_RIGHTSHIFT);
 
100
    auto second_key_event = create_key_event(0, KEY_M);
 
101
 
 
102
    dispatcher.notifyKey(&first_key_event);
 
103
    dispatcher.notifyKey(&second_key_event);
 
104
 
 
105
    dispatcher.dispatchOnce();
 
106
    dispatcher.dispatchOnce();
 
107
    dispatcher.dispatchOnce();
 
108
}
 
109
 
 
110
TEST_F(InputDispatcher, detects_multiple_down_of_same_key)
 
111
{
 
112
    using namespace ::testing;
 
113
    InSequence seq;
 
114
    EXPECT_CALL(filter_policy, handle(AllOf(mt::KeyDownEvent(), mt::KeyOfScanCode(KEY_M))));
 
115
    EXPECT_CALL(filter_policy, handle(AllOf(mt::KeyRepeatEvent(), mt::KeyOfScanCode(KEY_M))));
 
116
    auto first_key_event = create_key_event(0, KEY_M);
 
117
    auto second_key_event = create_key_event(0, KEY_M);
 
118
 
 
119
    dispatcher.notifyKey(&first_key_event);
 
120
    dispatcher.notifyKey(&second_key_event);
 
121
 
 
122
    dispatcher.dispatchOnce();
 
123
    dispatcher.dispatchOnce();
 
124
    dispatcher.dispatchOnce();
 
125
}