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> |
Siarhei Vishniakou | 257553c | 2019-02-21 14:37:06 -0600 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | // --- InputDeviceIdentifierTest --- |
| 27 | |
| 28 | TEST(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 Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 37 | class InputDeviceKeyMapTest : public testing::Test { |
| 38 | protected: |
| 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 Innocenti | 189c0f8 | 2020-12-22 19:45:18 +0900 | [diff] [blame] | 46 | ASSERT_TRUE(ret.ok()) << "Cannot load KeyLayout at " << path; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 47 | 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 Innocenti | 189c0f8 | 2020-12-22 19:45:18 +0900 | [diff] [blame] | 61 | ASSERT_TRUE(ret.ok()) << "Cannot load KeyCharacterMap at " << path; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 62 | 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 | |
| 76 | TEST_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 Innocenti | 189c0f8 | 2020-12-22 19:45:18 +0900 | [diff] [blame] | 85 | } // namespace android |