Siarhei Vishniakou | 257553c | 2019-02-21 14:37:06 -0600 | [diff] [blame] | 1 | /* |
| 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 Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 17 | #include <binder/Binder.h> |
| 18 | #include <binder/Parcel.h> |
Siarhei Vishniakou | 257553c | 2019-02-21 14:37:06 -0600 | [diff] [blame] | 19 | #include <gtest/gtest.h> |
| 20 | #include <input/InputDevice.h> |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 21 | #include <input/KeyLayoutMap.h> |
| 22 | #include <input/Keyboard.h> |
Philip Junker | b60596f | 2021-12-10 18:39:42 +0100 | [diff] [blame] | 23 | #include "android-base/file.h" |
Siarhei Vishniakou | 257553c | 2019-02-21 14:37:06 -0600 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | // --- InputDeviceIdentifierTest --- |
| 28 | |
| 29 | TEST(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 Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 38 | class InputDeviceKeyMapTest : public testing::Test { |
| 39 | protected: |
| 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 Innocenti | 189c0f8 | 2020-12-22 19:45:18 +0900 | [diff] [blame] | 47 | ASSERT_TRUE(ret.ok()) << "Cannot load KeyLayout at " << path; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 48 | 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 Innocenti | 189c0f8 | 2020-12-22 19:45:18 +0900 | [diff] [blame] | 62 | ASSERT_TRUE(ret.ok()) << "Cannot load KeyCharacterMap at " << path; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 63 | mKeyMap.keyCharacterMap = *ret; |
| 64 | mKeyMap.keyCharacterMapFile = path; |
| 65 | } |
| 66 | |
Siarhei Vishniakou | 0a448ac | 2022-05-18 12:30:16 -0700 | [diff] [blame^] | 67 | void SetUp() override { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 68 | loadKeyLayout("Generic"); |
| 69 | loadKeyCharacterMap("Generic"); |
| 70 | } |
| 71 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 72 | KeyMap mKeyMap; |
| 73 | }; |
| 74 | |
| 75 | TEST_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 Junker | b60596f | 2021-12-10 18:39:42 +0100 | [diff] [blame] | 84 | TEST_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 | |
| 97 | TEST_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 Innocenti | 189c0f8 | 2020-12-22 19:45:18 +0900 | [diff] [blame] | 133 | } // namespace android |