blob: 6b695c4581c7627c19f9b329b481d2a526d237fc [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>
Philip Junkerb60596f2021-12-10 18:39:42 +010023#include "android-base/file.h"
Siarhei Vishniakou257553c2019-02-21 14:37:06 -060024
25namespace android {
26
27// --- InputDeviceIdentifierTest ---
28
29TEST(InputDeviceIdentifierTest, getCanonicalName) {
30 InputDeviceIdentifier identifier;
31 identifier.name = "test device";
32 ASSERT_EQ(std::string("test_device"), identifier.getCanonicalName());
33
34 identifier.name = "deviceName-123 version_C!";
35 ASSERT_EQ(std::string("deviceName-123_version_C_"), identifier.getCanonicalName());
36}
37
Chris Yef59a2f42020-10-16 12:55:26 -070038class InputDeviceKeyMapTest : public testing::Test {
39protected:
40 void loadKeyLayout(const char* name) {
41 std::string path =
42 getInputDeviceConfigurationFilePathByName(name,
43 InputDeviceConfigurationFileType::
44 KEY_LAYOUT);
45 ASSERT_FALSE(path.empty());
46 base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(path);
Bernie Innocenti189c0f82020-12-22 19:45:18 +090047 ASSERT_TRUE(ret.ok()) << "Cannot load KeyLayout at " << path;
Chris Yef59a2f42020-10-16 12:55:26 -070048 mKeyMap.keyLayoutMap = std::move(*ret);
49 mKeyMap.keyLayoutFile = path;
50 }
51
52 void loadKeyCharacterMap(const char* name) {
53 InputDeviceIdentifier identifier;
54 identifier.name = name;
55 std::string path =
56 getInputDeviceConfigurationFilePathByName(identifier.getCanonicalName(),
57 InputDeviceConfigurationFileType::
58 KEY_CHARACTER_MAP);
59 ASSERT_FALSE(path.empty()) << "KeyCharacterMap for " << name << " not found";
60 base::Result<std::shared_ptr<KeyCharacterMap>> ret =
61 KeyCharacterMap::load(path, KeyCharacterMap::Format::BASE);
Bernie Innocenti189c0f82020-12-22 19:45:18 +090062 ASSERT_TRUE(ret.ok()) << "Cannot load KeyCharacterMap at " << path;
Chris Yef59a2f42020-10-16 12:55:26 -070063 mKeyMap.keyCharacterMap = *ret;
64 mKeyMap.keyCharacterMapFile = path;
65 }
66
Siarhei Vishniakou0a448ac2022-05-18 12:30:16 -070067 void SetUp() override {
Chris Yef59a2f42020-10-16 12:55:26 -070068 loadKeyLayout("Generic");
69 loadKeyCharacterMap("Generic");
70 }
71
Chris Yef59a2f42020-10-16 12:55:26 -070072 KeyMap mKeyMap;
73};
74
75TEST_F(InputDeviceKeyMapTest, keyCharacterMapParcelingTest) {
76 Parcel parcel;
77 mKeyMap.keyCharacterMap->writeToParcel(&parcel);
78 parcel.setDataPosition(0);
79 std::shared_ptr<KeyCharacterMap> map = KeyCharacterMap::readFromParcel(&parcel);
80 // Verify the key character map is the same as original
81 ASSERT_EQ(*map, *mKeyMap.keyCharacterMap);
82}
83
Philip Junkerb60596f2021-12-10 18:39:42 +010084TEST_F(InputDeviceKeyMapTest, keyCharacterMapWithOverlayParcelingTest) {
85 Parcel parcel;
86 std::string overlayPath = base::GetExecutableDirectory() + "/data/german.kcm";
87 base::Result<std::shared_ptr<KeyCharacterMap>> overlay =
88 KeyCharacterMap::load(overlayPath, KeyCharacterMap::Format::OVERLAY);
89 ASSERT_TRUE(overlay.ok()) << "Cannot load KeyCharacterMap at " << overlayPath;
90 mKeyMap.keyCharacterMap->combine(*overlay->get());
91 mKeyMap.keyCharacterMap->writeToParcel(&parcel);
92 parcel.setDataPosition(0);
93 std::shared_ptr<KeyCharacterMap> map = KeyCharacterMap::readFromParcel(&parcel);
94 ASSERT_EQ(*map, *mKeyMap.keyCharacterMap);
95}
96
97TEST_F(InputDeviceKeyMapTest, keyCharacteMapApplyMultipleOverlaysTest) {
98 std::string frenchOverlayPath = base::GetExecutableDirectory() + "/data/french.kcm";
99 std::string englishOverlayPath = base::GetExecutableDirectory() + "/data/english_us.kcm";
100 std::string germanOverlayPath = base::GetExecutableDirectory() + "/data/german.kcm";
101 base::Result<std::shared_ptr<KeyCharacterMap>> frenchOverlay =
102 KeyCharacterMap::load(frenchOverlayPath, KeyCharacterMap::Format::OVERLAY);
103 ASSERT_TRUE(frenchOverlay.ok()) << "Cannot load KeyCharacterMap at " << frenchOverlayPath;
104 base::Result<std::shared_ptr<KeyCharacterMap>> englishOverlay =
105 KeyCharacterMap::load(englishOverlayPath, KeyCharacterMap::Format::OVERLAY);
106 ASSERT_TRUE(englishOverlay.ok()) << "Cannot load KeyCharacterMap at " << englishOverlayPath;
107 base::Result<std::shared_ptr<KeyCharacterMap>> germanOverlay =
108 KeyCharacterMap::load(germanOverlayPath, KeyCharacterMap::Format::OVERLAY);
109 ASSERT_TRUE(germanOverlay.ok()) << "Cannot load KeyCharacterMap at " << germanOverlayPath;
110
111 // Apply the French overlay
112 mKeyMap.keyCharacterMap->combine(*frenchOverlay->get());
113 // Copy the result for later
114 std::shared_ptr<KeyCharacterMap> frenchOverlaidKeyCharacterMap =
115 std::make_shared<KeyCharacterMap>(*mKeyMap.keyCharacterMap);
116
117 // Apply the English overlay
118 mKeyMap.keyCharacterMap->combine(*englishOverlay->get());
119 // Verify that the result is different from the French overlay result
120 ASSERT_NE(*mKeyMap.keyCharacterMap, *frenchOverlaidKeyCharacterMap);
121
122 // Apply the German overlay
123 mKeyMap.keyCharacterMap->combine(*germanOverlay->get());
124 // Verify that the result is different from the French overlay result
125 ASSERT_NE(*mKeyMap.keyCharacterMap, *frenchOverlaidKeyCharacterMap);
126
127 // Apply the French overlay
128 mKeyMap.keyCharacterMap->combine(*frenchOverlay->get());
129 // Verify that the result is the same like after applying it initially
130 ASSERT_EQ(*mKeyMap.keyCharacterMap, *frenchOverlaidKeyCharacterMap);
131}
132
Bernie Innocenti189c0f82020-12-22 19:45:18 +0900133} // namespace android