blob: a96d664a4256a54d37f2486509d037cb7b2436d0 [file] [log] [blame]
Tim Kilbourn73475a42015-02-13 10:35:20 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "InputHub_test"
18//#define LOG_NDEBUG 0
19
20#include <linux/input.h>
21
22#include <gtest/gtest.h>
23
24#include <utils/Timers.h>
25
26#include "InputDevice.h"
27#include "InputHub.h"
28
29// # of milliseconds to allow for timing measurements
30#define TIMING_TOLERANCE_MS 25
31
32#define MSC_ANDROID_TIME_SEC 0x6
33#define MSC_ANDROID_TIME_USEC 0x7
34
35namespace android {
36namespace tests {
37
38class MockInputDeviceNode : public InputDeviceNode {
39 virtual const std::string& getPath() const override { return mPath; }
40
41 virtual const std::string& getName() const override { return mName; }
42 virtual const std::string& getLocation() const override { return mLocation; }
43 virtual const std::string& getUniqueId() const override { return mUniqueId; }
44
45 virtual uint16_t getBusType() const override { return 0; }
46 virtual uint16_t getVendorId() const override { return 0; }
47 virtual uint16_t getProductId() const override { return 0; }
48 virtual uint16_t getVersion() const override { return 0; }
49
50 virtual bool hasKey(int32_t key) const { return false; }
51 virtual bool hasRelativeAxis(int axis) const { return false; }
52 virtual bool hasInputProperty(int property) const { return false; }
53
54 virtual int32_t getKeyState(int32_t key) const { return 0; }
55 virtual int32_t getSwitchState(int32_t sw) const { return 0; }
56 virtual const AbsoluteAxisInfo* getAbsoluteAxisInfo(int32_t axis) const { return nullptr; }
57 virtual status_t getAbsoluteAxisValue(int32_t axis, int32_t* outValue) const { return 0; }
58
59 virtual void vibrate(nsecs_t duration) {}
60 virtual void cancelVibrate(int32_t deviceId) {}
61
62 virtual void disableDriverKeyRepeat() {}
63
64private:
65 std::string mPath = "/test";
66 std::string mName = "Test Device";
67 std::string mLocation = "test/0";
68 std::string mUniqueId = "test-id";
69};
70
71TEST(EvdevDeviceTest, testOverrideTime) {
72 auto node = std::make_shared<MockInputDeviceNode>();
73 auto device = std::make_unique<EvdevDevice>(node);
74 ASSERT_TRUE(device != nullptr);
75
76 // Send two timestamp override events before an input event.
77 nsecs_t when = 2ULL;
78 InputEvent msc1 = { when, EV_MSC, MSC_ANDROID_TIME_SEC, 1 };
79 InputEvent msc2 = { when, EV_MSC, MSC_ANDROID_TIME_USEC, 900000 };
80
81 // Send a key down and syn. Should get the overridden timestamp.
82 InputEvent keyDown = { when, EV_KEY, KEY_HOME, 1 };
83 InputEvent syn = { when, EV_SYN, SYN_REPORT, 0 };
84
85 // Send a key up, which should be at the reported timestamp.
86 InputEvent keyUp = { when, EV_KEY, KEY_HOME, 0 };
87
88 device->processInput(msc1, when);
89 device->processInput(msc2, when);
90 device->processInput(keyDown, when);
91 device->processInput(syn, when);
92 device->processInput(keyUp, when);
93
94 nsecs_t expectedWhen = s2ns(1) + us2ns(900000);
95 EXPECT_EQ(expectedWhen, keyDown.when);
96 EXPECT_EQ(expectedWhen, syn.when);
97 EXPECT_EQ(when, keyUp.when);
98}
99
100TEST(EvdevDeviceTest, testWrongClockCorrection) {
101 auto node = std::make_shared<MockInputDeviceNode>();
102 auto device = std::make_unique<EvdevDevice>(node);
103 ASSERT_TRUE(device != nullptr);
104
105 auto now = systemTime(SYSTEM_TIME_MONOTONIC);
106
107 // Input event that supposedly comes from 1 minute in the future. In
108 // reality, the timestamps would be much further off.
109 InputEvent event = { now + s2ns(60), EV_KEY, KEY_HOME, 1 };
110
111 device->processInput(event, now);
112
113 EXPECT_NEAR(now, event.when, ms2ns(TIMING_TOLERANCE_MS));
114}
115
116TEST(EvdevDeviceTest, testClockCorrectionOk) {
117 auto node = std::make_shared<MockInputDeviceNode>();
118 auto device = std::make_unique<EvdevDevice>(node);
119 ASSERT_TRUE(device != nullptr);
120
121 auto now = systemTime(SYSTEM_TIME_MONOTONIC);
122
123 // Input event from now, but will be reported as if it came early.
124 InputEvent event = { now, EV_KEY, KEY_HOME, 1 };
125
126 // event_time parameter is 11 seconds in the past, so it looks like we used
127 // the wrong clock.
128 device->processInput(event, now - s2ns(11));
129
130 EXPECT_NEAR(now, event.when, ms2ns(TIMING_TOLERANCE_MS));
131}
132
133} // namespace tests
134} // namespace android