1 /*
2  * Copyright (c) 2022 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 <gtest/gtest.h>
17 #define private public
18 #define protected public
19 #include "ability_post_event_timeout.h"
20 #undef protected
21 #undef private
22 #include "ability_handler.h"
23 #include "event_handler.h"
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::AppExecFwk;
27 namespace OHOS {
28 namespace AppExecFwk {
29 class AbilityPostEventTimeoutTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
32     static void TearDownTestCase(void);
33     void SetUp();
34     void TearDown();
35     std::shared_ptr<AbilityPostEventTimeout> abilityPostEventTimeout_;
36 };
SetUpTestCase(void)37 void AbilityPostEventTimeoutTest::SetUpTestCase(void) {}
TearDownTestCase(void)38 void AbilityPostEventTimeoutTest::TearDownTestCase(void) {}
SetUp(void)39 void AbilityPostEventTimeoutTest::SetUp(void) {}
TearDown(void)40 void AbilityPostEventTimeoutTest::TearDown(void) {}
41 
42 /**
43  * @tc.number: AaFwk_Ability_Context_TimingBegin_001
44  * @tc.name: TimingBegin
45  * @tc.desc: Ability Post Event Timeout and handler_ is nullptr.
46  */
47 HWTEST_F(AbilityPostEventTimeoutTest, AaFwk_Ability_Context_TimingBegin_001, Function | MediumTest | Level1)
48 {
49     std::string str = "";
50     std::shared_ptr<AbilityHandler> eventHandler = nullptr;
51     abilityPostEventTimeout_ = std::make_shared<AbilityPostEventTimeout>(str, eventHandler);
52     int64_t delaytime = 1;
53     abilityPostEventTimeout_->TimingBegin(delaytime);
54     EXPECT_TRUE(abilityPostEventTimeout_->handler_ == nullptr);
55 }
56 
57 /**
58  * @tc.number: AaFwk_Ability_Context_TimingBegin_002
59  * @tc.name: TimingBegin
60  * @tc.desc: Ability Post Event Timeout and handler_ Not empty.
61  */
62 HWTEST_F(AbilityPostEventTimeoutTest, AaFwk_Ability_Context_TimingBegin_002, Function | MediumTest | Level1)
63 {
64     std::string str = "";
65     std::shared_ptr<EventRunner> runner = nullptr;
66     std::shared_ptr<AbilityHandler> eventHandler = std::make_shared<AbilityHandler>(runner);
67     abilityPostEventTimeout_ = std::make_shared<AbilityPostEventTimeout>(str, eventHandler);
68     int64_t delaytime = 1;
69     abilityPostEventTimeout_->TimingBegin(delaytime);
70     EXPECT_TRUE(abilityPostEventTimeout_->handler_ != nullptr);
71 }
72 
73 /**
74  * @tc.number: AaFwk_Ability_Context_TimeEnd_001
75  * @tc.name: TimeEnd
76  * @tc.desc: Verification function TimeEnd and the result is handler_ is nullptr.
77  */
78 HWTEST_F(AbilityPostEventTimeoutTest, AaFwk_Ability_Context_TimeEnd_001, Function | MediumTest | Level1)
79 {
80     std::string str = "";
81     std::shared_ptr<AbilityHandler> eventHandler = nullptr;
82     abilityPostEventTimeout_ = std::make_shared<AbilityPostEventTimeout>(str, eventHandler);
83     abilityPostEventTimeout_->TimeEnd();
84     EXPECT_TRUE(abilityPostEventTimeout_->handler_ == nullptr);
85 }
86 
87 /**
88  * @tc.number: AaFwk_Ability_Context_TimeEnd_002
89  * @tc.name: TimeEnd
90  * @tc.desc: Verification function TimeEnd and the result is handler_ isn't nullptr.
91  */
92 HWTEST_F(AbilityPostEventTimeoutTest, AaFwk_Ability_Context_TimeEnd_002, Function | MediumTest | Level1)
93 {
94     std::string str = "";
95     std::shared_ptr<EventRunner> runner = nullptr;
96     std::shared_ptr<AbilityHandler> eventHandler = std::make_shared<AbilityHandler>(runner);
97     abilityPostEventTimeout_ = std::make_shared<AbilityPostEventTimeout>(str, eventHandler);
98     abilityPostEventTimeout_->TimeEnd();
99     EXPECT_TRUE(abilityPostEventTimeout_->handler_ != nullptr);
100     EXPECT_TRUE(abilityPostEventTimeout_->taskExec_);
101 }
102 
103 /**
104  * @tc.number: AaFwk_Ability_Context_TimeEnd_003
105  * @tc.name: TimeEnd
106  * @tc.desc: Verification function TimeEnd and the result is handler_ isn't nullptr.
107  */
108 HWTEST_F(AbilityPostEventTimeoutTest, AaFwk_Ability_Context_TimeEnd_003, Function | MediumTest | Level1)
109 {
110     std::string str = "";
111     std::shared_ptr<EventRunner> runner = nullptr;
112     std::shared_ptr<AbilityHandler> eventHandler = std::make_shared<AbilityHandler>(runner);
113     abilityPostEventTimeout_ = std::make_shared<AbilityPostEventTimeout>(str, eventHandler);
114     abilityPostEventTimeout_->taskExec_ = true;
115     abilityPostEventTimeout_->TimeEnd();
116     EXPECT_TRUE(abilityPostEventTimeout_->handler_ != nullptr);
117     EXPECT_TRUE(abilityPostEventTimeout_->taskExec_);
118 }
119 
120 /**
121  * @tc.number: AaFwk_Ability_Context_TimeOutProc_001
122  * @tc.name: TimeOutProc
123  * @tc.desc: Verification function TimeOutProc and the result is handler_ is nullptr.
124  */
125 HWTEST_F(AbilityPostEventTimeoutTest, AaFwk_Ability_Context_TimeOutProc_001, Function | MediumTest | Level1)
126 {
127     std::string str = "";
128     std::shared_ptr<AbilityHandler> eventHandler = nullptr;
129     abilityPostEventTimeout_ = std::make_shared<AbilityPostEventTimeout>(str, eventHandler);
130     abilityPostEventTimeout_->TimeOutProc();
131     EXPECT_TRUE(abilityPostEventTimeout_->handler_ == nullptr);
132 }
133 
134 /**
135  * @tc.number: AaFwk_Ability_Context_TimeOutProc_002
136  * @tc.name: TimeOutProc
137  * @tc.desc: Verification function TimeOutProc and the result is handler_ isn't nullptr.
138  */
139 HWTEST_F(AbilityPostEventTimeoutTest, AaFwk_Ability_Context_TimeOutProc_002, Function | MediumTest | Level1)
140 {
141     std::string str = "";
142     std::shared_ptr<EventRunner> runner = nullptr;
143     std::shared_ptr<AbilityHandler> eventHandler = std::make_shared<AbilityHandler>(runner);
144     abilityPostEventTimeout_ = std::make_shared<AbilityPostEventTimeout>(str, eventHandler);
145     abilityPostEventTimeout_->TimeOutProc();
146     EXPECT_TRUE(abilityPostEventTimeout_->handler_ != nullptr);
147     EXPECT_TRUE(abilityPostEventTimeout_->taskExec_);
148 }
149 
150 /**
151  * @tc.number: AaFwk_Ability_Context_TimeOutProc_003
152  * @tc.name: TimeOutProc
153  * @tc.desc: Verification function TimeOutProc and the result is handler_ isn't nullptr.
154  */
155 HWTEST_F(AbilityPostEventTimeoutTest, AaFwk_Ability_Context_TimeOutProc_003, Function | MediumTest | Level3)
156 {
157     std::string str = "";
158     std::shared_ptr<EventRunner> runner = nullptr;
159     std::shared_ptr<AbilityHandler> eventHandler = std::make_shared<AbilityHandler>(runner);
160     abilityPostEventTimeout_ = std::make_shared<AbilityPostEventTimeout>(str, eventHandler);
161     abilityPostEventTimeout_->taskExec_ = true;
162     abilityPostEventTimeout_->TimeOutProc();
163     EXPECT_TRUE(abilityPostEventTimeout_->handler_ != nullptr);
164     EXPECT_TRUE(abilityPostEventTimeout_->taskExec_);
165 }
166 } // namespace AppExecFwk
167 } // namespace OHOS