1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #include "key_auto_repeat.h"
20 #include "libinput_mock.h"
21 #include "mmi_log.h"
22 #include "mock.h"
23
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "KeyAutoRepeatExTest"
26 namespace OHOS {
27 namespace MMI {
28 namespace {
29 using namespace testing::ext;
30 using namespace testing;
31 } // namespace
32
33 class KeyAutoRepeatExTest : public testing::Test {
34 public:
35 static void SetUpTestCase(void);
36 static void TearDownTestCase(void);
37
38 static inline std::shared_ptr<MessageParcelMock> messageParcelMock_ = nullptr;
39 };
40
SetUpTestCase(void)41 void KeyAutoRepeatExTest::SetUpTestCase(void)
42 {
43 messageParcelMock_ = std::make_shared<MessageParcelMock>();
44 MessageParcelMock::messageParcel = messageParcelMock_;
45 }
TearDownTestCase()46 void KeyAutoRepeatExTest::TearDownTestCase()
47 {
48 MessageParcelMock::messageParcel = nullptr;
49 messageParcelMock_ = nullptr;
50 }
51
52 /**
53 * @tc.name: KeyAutoRepeatExTest_RemoveDeviceConfig
54 * @tc.desc: Cover if (iter == deviceConfig_.end()) branch
55 * @tc.type: FUNC
56 * @tc.require:
57 */
58 HWTEST_F(KeyAutoRepeatExTest, KeyAutoRepeatExTest_RemoveDeviceConfig, TestSize.Level1)
59 {
60 CALL_TEST_DEBUG;
61 int32_t deviceId = 10;
62 EXPECT_CALL(*messageParcelMock_, FindInputDeviceId(_)).WillRepeatedly(Return(deviceId));
63 KeyAutoRepeat keyAutoRepeat;
64 libinput_device device {};
65 EXPECT_NO_FATAL_FAILURE(keyAutoRepeat.RemoveDeviceConfig(&device));
66 }
67
68 /**
69 * @tc.name: KeyAutoRepeatExTest_RemoveDeviceConfig_001
70 * @tc.desc: Cover the else branch of if (iter == deviceConfig_.end())
71 * @tc.type: FUNC
72 * @tc.require:
73 */
74 HWTEST_F(KeyAutoRepeatExTest, KeyAutoRepeatExTest_RemoveDeviceConfig_001, TestSize.Level1)
75 {
76 CALL_TEST_DEBUG;
77 int32_t deviceId = 15;
78 EXPECT_CALL(*messageParcelMock_, FindInputDeviceId(_)).WillRepeatedly(Return(deviceId));
79 KeyAutoRepeat keyAutoRepeat;
80 libinput_device device {};
81 DeviceConfig deviceConfig;
82 keyAutoRepeat.deviceConfig_.insert(std::make_pair(deviceId, deviceConfig));
83 EXPECT_NO_FATAL_FAILURE(keyAutoRepeat.RemoveDeviceConfig(&device));
84 }
85 } // namespace MMI
86 } // namespace OHOS