blob: a0ec6adc658448e8fd9451f41df9998032b57a13 [file] [log] [blame]
Siarhei Vishniakou257553c2019-02-21 14:37:06 -06001/*
2 * Copyright (C) 2019 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
Chris Yef59a2f42020-10-16 12:55:26 -070017#include <binder/Binder.h>
18#include <binder/Parcel.h>
Siarhei Vishniakou257553c2019-02-21 14:37:06 -060019#include <gtest/gtest.h>
20#include <input/InputDevice.h>
Chris Yef59a2f42020-10-16 12:55:26 -070021#include <input/KeyLayoutMap.h>
22#include <input/Keyboard.h>
Vaibhav Devmurari0b2b5672023-09-15 11:23:12 +000023#include <linux/uinput.h>
Philip Junker90bc9492021-12-10 18:39:42 +010024#include "android-base/file.h"
Siarhei Vishniakou257553c2019-02-21 14:37:06 -060025
26namespace android {
27
28// --- InputDeviceIdentifierTest ---
29
30TEST(InputDeviceIdentifierTest, getCanonicalName) {
31 InputDeviceIdentifier identifier;
32 identifier.name = "test device";
33 ASSERT_EQ(std::string("test_device"), identifier.getCanonicalName());
34
35 identifier.name = "deviceName-123 version_C!";
36 ASSERT_EQ(std::string("deviceName-123_version_C_"), identifier.getCanonicalName());
37}
38
Chris Yef59a2f42020-10-16 12:55:26 -070039class InputDeviceKeyMapTest : public testing::Test {
40protected:
41 void loadKeyLayout(const char* name) {
42 std::string path =
43 getInputDeviceConfigurationFilePathByName(name,
44 InputDeviceConfigurationFileType::
45 KEY_LAYOUT);
46 ASSERT_FALSE(path.empty());
47 base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(path);
Bernie Innocenti189c0f82020-12-22 19:45:18 +090048 ASSERT_TRUE(ret.ok()) << "Cannot load KeyLayout at " << path;
Chris Yef59a2f42020-10-16 12:55:26 -070049 mKeyMap.keyLayoutMap = std::move(*ret);
50 mKeyMap.keyLayoutFile = path;
51 }
52
53 void loadKeyCharacterMap(const char* name) {
54 InputDeviceIdentifier identifier;
55 identifier.name = name;
56 std::string path =
57 getInputDeviceConfigurationFilePathByName(identifier.getCanonicalName(),
58 InputDeviceConfigurationFileType::
59 KEY_CHARACTER_MAP);
60 ASSERT_FALSE(path.empty()) << "KeyCharacterMap for " << name << " not found";
61 base::Result<std::shared_ptr<KeyCharacterMap>> ret =
62 KeyCharacterMap::load(path, KeyCharacterMap::Format::BASE);
Bernie Innocenti189c0f82020-12-22 19:45:18 +090063 ASSERT_TRUE(ret.ok()) << "Cannot load KeyCharacterMap at " << path;
Chris Yef59a2f42020-10-16 12:55:26 -070064 mKeyMap.keyCharacterMap = *ret;
65 mKeyMap.keyCharacterMapFile = path;
66 }
67
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -070068 void SetUp() override {
Siarhei Vishniakou5e83dfe2022-09-28 17:04:42 -070069#if !defined(__ANDROID__)
70 GTEST_SKIP() << "b/253299089 Generic files are currently read directly from device.";
71#endif
Chris Yef59a2f42020-10-16 12:55:26 -070072 loadKeyLayout("Generic");
73 loadKeyCharacterMap("Generic");
74 }
75
Chris Yef59a2f42020-10-16 12:55:26 -070076 KeyMap mKeyMap;
77};
78
79TEST_F(InputDeviceKeyMapTest, keyCharacterMapParcelingTest) {
80 Parcel parcel;
81 mKeyMap.keyCharacterMap->writeToParcel(&parcel);
82 parcel.setDataPosition(0);
83 std::shared_ptr<KeyCharacterMap> map = KeyCharacterMap::readFromParcel(&parcel);
84 // Verify the key character map is the same as original
85 ASSERT_EQ(*map, *mKeyMap.keyCharacterMap);
86}
87
Philip Junker90bc9492021-12-10 18:39:42 +010088TEST_F(InputDeviceKeyMapTest, keyCharacterMapWithOverlayParcelingTest) {
89 Parcel parcel;
90 std::string overlayPath = base::GetExecutableDirectory() + "/data/german.kcm";
91 base::Result<std::shared_ptr<KeyCharacterMap>> overlay =
92 KeyCharacterMap::load(overlayPath, KeyCharacterMap::Format::OVERLAY);
93 ASSERT_TRUE(overlay.ok()) << "Cannot load KeyCharacterMap at " << overlayPath;
94 mKeyMap.keyCharacterMap->combine(*overlay->get());
95 mKeyMap.keyCharacterMap->writeToParcel(&parcel);
96 parcel.setDataPosition(0);
97 std::shared_ptr<KeyCharacterMap> map = KeyCharacterMap::readFromParcel(&parcel);
98 ASSERT_EQ(*map, *mKeyMap.keyCharacterMap);
99}
100
Vaibhav Devmurari0b2b5672023-09-15 11:23:12 +0000101TEST_F(InputDeviceKeyMapTest, keyCharacterMapApplyMultipleOverlaysTest) {
Philip Junker90bc9492021-12-10 18:39:42 +0100102 std::string frenchOverlayPath = base::GetExecutableDirectory() + "/data/french.kcm";
103 std::string englishOverlayPath = base::GetExecutableDirectory() + "/data/english_us.kcm";
104 std::string germanOverlayPath = base::GetExecutableDirectory() + "/data/german.kcm";
105 base::Result<std::shared_ptr<KeyCharacterMap>> frenchOverlay =
106 KeyCharacterMap::load(frenchOverlayPath, KeyCharacterMap::Format::OVERLAY);
107 ASSERT_TRUE(frenchOverlay.ok()) << "Cannot load KeyCharacterMap at " << frenchOverlayPath;
108 base::Result<std::shared_ptr<KeyCharacterMap>> englishOverlay =
109 KeyCharacterMap::load(englishOverlayPath, KeyCharacterMap::Format::OVERLAY);
110 ASSERT_TRUE(englishOverlay.ok()) << "Cannot load KeyCharacterMap at " << englishOverlayPath;
111 base::Result<std::shared_ptr<KeyCharacterMap>> germanOverlay =
112 KeyCharacterMap::load(germanOverlayPath, KeyCharacterMap::Format::OVERLAY);
113 ASSERT_TRUE(germanOverlay.ok()) << "Cannot load KeyCharacterMap at " << germanOverlayPath;
114
115 // Apply the French overlay
116 mKeyMap.keyCharacterMap->combine(*frenchOverlay->get());
117 // Copy the result for later
118 std::shared_ptr<KeyCharacterMap> frenchOverlaidKeyCharacterMap =
119 std::make_shared<KeyCharacterMap>(*mKeyMap.keyCharacterMap);
120
121 // Apply the English overlay
122 mKeyMap.keyCharacterMap->combine(*englishOverlay->get());
123 // Verify that the result is different from the French overlay result
124 ASSERT_NE(*mKeyMap.keyCharacterMap, *frenchOverlaidKeyCharacterMap);
125
126 // Apply the German overlay
127 mKeyMap.keyCharacterMap->combine(*germanOverlay->get());
128 // Verify that the result is different from the French overlay result
129 ASSERT_NE(*mKeyMap.keyCharacterMap, *frenchOverlaidKeyCharacterMap);
130
131 // Apply the French overlay
132 mKeyMap.keyCharacterMap->combine(*frenchOverlay->get());
133 // Verify that the result is the same like after applying it initially
134 ASSERT_EQ(*mKeyMap.keyCharacterMap, *frenchOverlaidKeyCharacterMap);
135}
136
Vaibhav Devmurari0b2b5672023-09-15 11:23:12 +0000137TEST_F(InputDeviceKeyMapTest, keyCharacterMapApplyOverlayTest) {
138 std::string frenchOverlayPath = base::GetExecutableDirectory() + "/data/french.kcm";
139 base::Result<std::shared_ptr<KeyCharacterMap>> frenchOverlay =
140 KeyCharacterMap::load(frenchOverlayPath, KeyCharacterMap::Format::OVERLAY);
141 ASSERT_TRUE(frenchOverlay.ok()) << "Cannot load KeyCharacterMap at " << frenchOverlayPath;
142
143 // Apply the French overlay
144 mKeyMap.keyCharacterMap->combine(*frenchOverlay->get());
145
146 // Check if mapping for key_Q is correct
147 int32_t outKeyCode;
148 status_t mapKeyResult = mKeyMap.keyCharacterMap->mapKey(KEY_Q, /*usageCode=*/0, &outKeyCode);
149 ASSERT_EQ(mapKeyResult, OK) << "No mapping for KEY_Q for " << frenchOverlayPath;
150 ASSERT_EQ(outKeyCode, AKEYCODE_A);
151
152 mapKeyResult = mKeyMap.keyCharacterMap->mapKey(KEY_E, /*usageCode=*/0, &outKeyCode);
153 ASSERT_NE(mapKeyResult, OK) << "Mapping exists for KEY_E for " << frenchOverlayPath;
154}
155
156TEST_F(InputDeviceKeyMapTest, keyCharacterMapBadAxisLabel) {
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800157 std::string klPath = base::GetExecutableDirectory() + "/data/bad_axis_label.kl";
158
159 base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(klPath);
160 ASSERT_FALSE(ret.ok()) << "Should not be able to load KeyLayout at " << klPath;
161}
162
Vaibhav Devmurari0b2b5672023-09-15 11:23:12 +0000163TEST_F(InputDeviceKeyMapTest, keyCharacterMapBadLedLabel) {
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800164 std::string klPath = base::GetExecutableDirectory() + "/data/bad_led_label.kl";
165
166 base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(klPath);
167 ASSERT_FALSE(ret.ok()) << "Should not be able to load KeyLayout at " << klPath;
168}
169
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700170TEST(InputDeviceKeyLayoutTest, DoesNotLoadWhenRequiredKernelConfigIsMissing) {
Siarhei Vishniakou5e83dfe2022-09-28 17:04:42 -0700171#if !defined(__ANDROID__)
172 GTEST_SKIP() << "Can't check kernel configs on host";
173#endif
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700174 std::string klPath = base::GetExecutableDirectory() + "/data/kl_with_required_fake_config.kl";
175 base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(klPath);
176 ASSERT_FALSE(ret.ok()) << "Should not be able to load KeyLayout at " << klPath;
177 // We assert error message here because it's used by 'validatekeymaps' tool
178 ASSERT_EQ("Missing kernel config", ret.error().message());
179}
180
181TEST(InputDeviceKeyLayoutTest, LoadsWhenRequiredKernelConfigIsPresent) {
Siarhei Vishniakou5e83dfe2022-09-28 17:04:42 -0700182#if !defined(__ANDROID__)
183 GTEST_SKIP() << "Can't check kernel configs on host";
184#endif
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700185 std::string klPath = base::GetExecutableDirectory() + "/data/kl_with_required_real_config.kl";
186 base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(klPath);
187 ASSERT_TRUE(ret.ok()) << "Cannot load KeyLayout at " << klPath;
188 const std::shared_ptr<KeyLayoutMap>& map = *ret;
189 ASSERT_NE(nullptr, map) << "Map should be valid because CONFIG_UHID should always be present";
190}
191
Bernie Innocenti189c0f82020-12-22 19:45:18 +0900192} // namespace android