1 /*
2  * Copyright (c) 2023 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 #include "active_key_event_test.h"
16 
17 #include <ctime>
18 #include <memory>
19 #include <vector>
20 #include <string>
21 
22 #define private public
23 #define protected public
24 #include "active_key_event.h"
25 #include "log_store_ex.h"
26 #include "hiview_platform.h"
27 #include "sysevent_source.h"
28 using namespace testing::ext;
29 using namespace OHOS::HiviewDFX;
30 
31 namespace OHOS {
32 namespace HiviewDFX {
33 SysEventSource source;
SetUp()34 void ActiveKeyEventTest::SetUp()
35 {
36     printf("SetUp.\n");
37 }
38 
TearDown()39 void ActiveKeyEventTest::TearDown()
40 {
41     printf("TearDown.\n");
42 }
43 
SetUpTestCase()44 void ActiveKeyEventTest::SetUpTestCase()
45 {
46     HiviewPlatform platform;
47     source.SetHiviewContext(&platform);
48     source.OnLoad();
49 }
50 
TearDownTestCase()51 void ActiveKeyEventTest::TearDownTestCase()
52 {
53     source.OnUnload();
54 }
55 
56 /**
57  * @tc.name: ActiveKeyEventTest_001
58  * @tc.desc: ActiveKeyEventTest CombinationKeyHandle
59  * @tc.type: FUNC
60  */
61 HWTEST_F(ActiveKeyEventTest, ActiveKeyEventTest_001, TestSize.Level3)
62 {
63     std::string logStorePath = "/data/test/";
64     std::shared_ptr<LogStoreEx> logStoreEx = std::make_shared<LogStoreEx>(logStorePath, true);
65     auto ret = logStoreEx->Init();
66     EXPECT_EQ(ret, true);
67     std::shared_ptr<ActiveKeyEvent> activeKeyEvent = std::make_shared<ActiveKeyEvent>();
68     activeKeyEvent->Init(logStoreEx);
69     activeKeyEvent->triggeringTime_ = 0;
70     auto keyEvent = OHOS::MMI::KeyEvent::Create();
71     activeKeyEvent->CombinationKeyHandle(keyEvent);
72 }
73 
74 /**
75  * @tc.name: ActiveKeyEventTest_002
76  * @tc.desc: ActiveKeyEventTest CombinationKeyCallback
77  * @tc.type: FUNC
78  */
79 HWTEST_F(ActiveKeyEventTest, ActiveKeyEventTest_002, TestSize.Level3)
80 {
81     std::string logStorePath = "/data/test/";
82     std::shared_ptr<LogStoreEx> logStoreEx = std::make_shared<LogStoreEx>(logStorePath, true);
83     auto ret = logStoreEx->Init();
84     EXPECT_EQ(ret, true);
85     std::shared_ptr<ActiveKeyEvent> activeKeyEvent = std::make_shared<ActiveKeyEvent>();
86     activeKeyEvent->Init(logStoreEx);
87     activeKeyEvent->triggeringTime_ = 0;
88     auto keyEvent = OHOS::MMI::KeyEvent::Create();
89     activeKeyEvent->CombinationKeyCallback(keyEvent);
90 }
91 
92 /**
93  * @tc.name: ActiveKeyEventTest_003
94  * @tc.desc: ActiveKeyEventTest Init
95  * @tc.type: FUNC
96  */
97 HWTEST_F(ActiveKeyEventTest, ActiveKeyEventTest_003, TestSize.Level3)
98 {
99     std::string logStorePath = "/data/test/";
100     std::shared_ptr<LogStoreEx> logStoreEx = std::make_shared<LogStoreEx>(logStorePath, true);
101     auto ret = logStoreEx->Init();
102     EXPECT_EQ(ret, true);
103     std::shared_ptr<ActiveKeyEvent> activeKeyEvent = std::make_shared<ActiveKeyEvent>();
104     std::set<int32_t> preKeys;
105     preKeys.insert(OHOS::MMI::KeyEvent::KEYCODE_VOLUME_UP);
106     activeKeyEvent->InitSubscribe(preKeys, OHOS::MMI::KeyEvent::KEYCODE_VOLUME_DOWN, 5, 500);
107 }
108 
109 /**
110  * @tc.name: ActiveKeyEventTest_004
111  * @tc.desc: ActiveKeyEventTest InitSubscribe
112  * @tc.type: FUNC
113  */
114 HWTEST_F(ActiveKeyEventTest, ActiveKeyEventTest_004, TestSize.Level3)
115 {
116     std::shared_ptr<ActiveKeyEvent> activeKeyEvent = std::make_shared<ActiveKeyEvent>();
117     activeKeyEvent->triggeringTime_ = (uint64_t)ActiveKeyEvent::SystemTimeMillisecond();
118     std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent = OHOS::MMI::KeyEvent::Create();
119     activeKeyEvent->CombinationKeyCallback(keyEvent);
120     std::set<int32_t> preKeys;
121     preKeys.insert(OHOS::MMI::KeyEvent::KEYCODE_VOLUME_UP);
122     activeKeyEvent->InitSubscribe(preKeys, OHOS::MMI::KeyEvent::KEYCODE_VOLUME_DOWN, 5, 500);
123 }
124 
125 /**
126  * @tc.name: ActiveKeyEventTest_005
127  * @tc.desc: ActiveKeyEventTest CombinationKeyCallback
128  * @tc.type: FUNC
129  */
130 HWTEST_F(ActiveKeyEventTest, ActiveKeyEventTest_005, TestSize.Level3)
131 {
132     std::shared_ptr<ActiveKeyEvent> activeKeyEvent = std::make_shared<ActiveKeyEvent>();
133     activeKeyEvent->triggeringTime_ = (uint64_t)ActiveKeyEvent::SystemTimeMillisecond();
134     std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent = OHOS::MMI::KeyEvent::Create();
135     activeKeyEvent->CombinationKeyCallback(keyEvent);
136 }
137 
138 /**
139  * @tc.name: ActiveKeyEventTest_006
140  * @tc.desc: ActiveKeyEventTest CombinationKeyCallback
141  * @tc.type: FUNC
142  */
143 HWTEST_F(ActiveKeyEventTest, ActiveKeyEventTest_006, TestSize.Level3)
144 {
145     std::shared_ptr<ActiveKeyEvent> activeKeyEvent = std::make_shared<ActiveKeyEvent>();
146     activeKeyEvent->triggeringTime_ = 0;
147     auto keyEvent = OHOS::MMI::KeyEvent::Create();
148     activeKeyEvent->CombinationKeyCallback(keyEvent);
149 }
150 } // namespace HiviewDFX
151 } // namespace OHOS
152