blob: f8f2f4e931f1920a48c853726f331e9bfae93a05 [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>
Siarhei Vishniakou257553c2019-02-21 14:37:06 -060023
24namespace android {
25
26// --- InputDeviceIdentifierTest ---
27
28TEST(InputDeviceIdentifierTest, getCanonicalName) {
29 InputDeviceIdentifier identifier;
30 identifier.name = "test device";
31 ASSERT_EQ(std::string("test_device"), identifier.getCanonicalName());
32
33 identifier.name = "deviceName-123 version_C!";
34 ASSERT_EQ(std::string("deviceName-123_version_C_"), identifier.getCanonicalName());
35}
36
Chris Yef59a2f42020-10-16 12:55:26 -070037class InputDeviceKeyMapTest : public testing::Test {
38protected:
39 void loadKeyLayout(const char* name) {
40 std::string path =
41 getInputDeviceConfigurationFilePathByName(name,
42 InputDeviceConfigurationFileType::
43 KEY_LAYOUT);
44 ASSERT_FALSE(path.empty());
45 base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(path);
Bernie Innocenti189c0f82020-12-22 19:45:18 +090046 ASSERT_TRUE(ret.ok()) << "Cannot load KeyLayout at " << path;
Chris Yef59a2f42020-10-16 12:55:26 -070047 mKeyMap.keyLayoutMap = std::move(*ret);
48 mKeyMap.keyLayoutFile = path;
49 }
50
51 void loadKeyCharacterMap(const char* name) {
52 InputDeviceIdentifier identifier;
53 identifier.name = name;
54 std::string path =
55 getInputDeviceConfigurationFilePathByName(identifier.getCanonicalName(),
56 InputDeviceConfigurationFileType::
57 KEY_CHARACTER_MAP);
58 ASSERT_FALSE(path.empty()) << "KeyCharacterMap for " << name << " not found";
59 base::Result<std::shared_ptr<KeyCharacterMap>> ret =
60 KeyCharacterMap::load(path, KeyCharacterMap::Format::BASE);
Bernie Innocenti189c0f82020-12-22 19:45:18 +090061 ASSERT_TRUE(ret.ok()) << "Cannot load KeyCharacterMap at " << path;
Chris Yef59a2f42020-10-16 12:55:26 -070062 mKeyMap.keyCharacterMap = *ret;
63 mKeyMap.keyCharacterMapFile = path;
64 }
65
66 virtual void SetUp() override {
67 loadKeyLayout("Generic");
68 loadKeyCharacterMap("Generic");
69 }
70
71 virtual void TearDown() override {}
72
73 KeyMap mKeyMap;
74};
75
76TEST_F(InputDeviceKeyMapTest, keyCharacterMapParcelingTest) {
77 Parcel parcel;
78 mKeyMap.keyCharacterMap->writeToParcel(&parcel);
79 parcel.setDataPosition(0);
80 std::shared_ptr<KeyCharacterMap> map = KeyCharacterMap::readFromParcel(&parcel);
81 // Verify the key character map is the same as original
82 ASSERT_EQ(*map, *mKeyMap.keyCharacterMap);
83}
84
Bernie Innocenti189c0f82020-12-22 19:45:18 +090085} // namespace android