1 /*
2  * Copyright (c) 2021 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 #include "app_process_data.h"
18 #include "lifecycle_deal.h"
19 #include "ability_scheduler_mock.h"
20 #include "session_info.h"
21 
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 namespace AAFwk {
26 class LifecycleDealTest : public testing::Test {
27 public:
28     static void SetUpTestCase(void);
29     static void TearDownTestCase(void);
30     void SetUp();
31     void TearDown();
32 
33     std::shared_ptr<LifecycleDeal> lifecycleDeal_{ nullptr };
34     sptr<AbilitySchedulerMock> abilityScheduler_{ nullptr };
35 };
36 
SetUpTestCase(void)37 void LifecycleDealTest::SetUpTestCase(void)
38 {}
TearDownTestCase(void)39 void LifecycleDealTest::TearDownTestCase(void)
40 {}
TearDown()41 void LifecycleDealTest::TearDown()
42 {}
43 
SetUp()44 void LifecycleDealTest::SetUp()
45 {
46     lifecycleDeal_ = std::make_shared<LifecycleDeal>();
47     abilityScheduler_ = new AbilitySchedulerMock();
48 }
49 
50 /*
51  * Feature: LifecycleDeal
52  * Function: Activate
53  * SubFunction: NA
54  * FunctionPoints: LifecycleDeal Activate
55  * EnvConditions:NA
56  * CaseDescription: Verify activate operation and call mock once
57  */
58 HWTEST_F(LifecycleDealTest, LifecycleDeal_oprator_001, TestSize.Level1)
59 {
60     LifeCycleStateInfo val;
61     EXPECT_CALL(*abilityScheduler_, ScheduleAbilityTransaction(::testing::_, ::testing::_, ::testing::_))
62         .Times(1)
63         .WillOnce(testing::DoAll(testing::SaveArg<1>(&val), testing::Return(true)));
64 
65     const Want want;
66     CallerInfo caller;
67     caller.deviceId = "device";
68     caller.bundleName = "bundle";
69     caller.abilityName = "LifecycleDealTest";
70 
71     LifeCycleStateInfo info;
72     info.caller = caller;
73     lifecycleDeal_->Activate(want, info);
74     lifecycleDeal_->SetScheduler(abilityScheduler_);
75     lifecycleDeal_->Activate(want, info);
76 
77     EXPECT_EQ(val.caller.deviceId, caller.deviceId);
78     EXPECT_EQ(val.caller.bundleName, caller.bundleName);
79     EXPECT_EQ(val.caller.abilityName, caller.abilityName);
80 }
81 
82 /*
83  * Feature: LifecycleDeal
84  * Function: Inactivate
85  * SubFunction: NA
86  * FunctionPoints: LifecycleDeal Inactivate
87  * EnvConditions:NA
88  * CaseDescription: Verify Inactivate operation and call mock once
89  */
90 HWTEST_F(LifecycleDealTest, LifecycleDeal_oprator_002, TestSize.Level1)
91 {
92     LifeCycleStateInfo val;
93     EXPECT_CALL(*abilityScheduler_, ScheduleAbilityTransaction(::testing::_, ::testing::_, ::testing::_))
94         .Times(1)
95         .WillOnce(testing::DoAll(testing::SaveArg<1>(&val), testing::Return(true)));
96 
97     const Want want;
98     CallerInfo caller;
99     caller.deviceId = "device";
100     caller.bundleName = "bundle";
101     caller.abilityName = "LifecycleDealTest";
102 
103     LifeCycleStateInfo info;
104     info.caller = caller;
105     lifecycleDeal_->Inactivate(want, info);
106     lifecycleDeal_->SetScheduler(abilityScheduler_);
107     lifecycleDeal_->Inactivate(want, info);
108 
109     EXPECT_EQ(val.caller.deviceId, caller.deviceId);
110     EXPECT_EQ(val.caller.bundleName, caller.bundleName);
111     EXPECT_EQ(val.caller.abilityName, caller.abilityName);
112 }
113 
114 /*
115  * Feature: LifecycleDeal
116  * Function: MoveToBackground
117  * SubFunction: NA
118  * FunctionPoints: LifecycleDeal MoveToBackground
119  * EnvConditions:NA
120  * CaseDescription: Verify MoveToBackground operation and call mock once
121  */
122 HWTEST_F(LifecycleDealTest, LifecycleDeal_oprator_003, TestSize.Level1)
123 {
124     LifeCycleStateInfo val;
125     EXPECT_CALL(*abilityScheduler_, ScheduleAbilityTransaction(::testing::_, ::testing::_, ::testing::_))
126         .Times(1)
127         .WillOnce(testing::DoAll(testing::SaveArg<1>(&val), testing::Return(true)));
128 
129     const Want want;
130     CallerInfo caller;
131     caller.deviceId = "device";
132     caller.bundleName = "bundle";
133     caller.abilityName = "LifecycleDealTest";
134 
135     LifeCycleStateInfo info;
136     info.caller = caller;
137     lifecycleDeal_->MoveToBackground(want, info);
138     lifecycleDeal_->SetScheduler(abilityScheduler_);
139     lifecycleDeal_->MoveToBackground(want, info);
140 
141     EXPECT_EQ(val.caller.deviceId, caller.deviceId);
142     EXPECT_EQ(val.caller.bundleName, caller.bundleName);
143     EXPECT_EQ(val.caller.abilityName, caller.abilityName);
144 }
145 
146 /*
147  * Feature: LifecycleDeal
148  * Function: ConnectAbility
149  * SubFunction: NA
150  * FunctionPoints: LifecycleDeal ConnectAbility
151  * EnvConditions:NA
152  * CaseDescription: Verify ConnectAbility operation and call mock once
153  */
154 HWTEST_F(LifecycleDealTest, LifecycleDeal_oprator_004, TestSize.Level1)
155 {
156     EXPECT_CALL(*abilityScheduler_, ScheduleConnectAbility(::testing::_)).Times(1);
157     const Want want;
158     lifecycleDeal_->ConnectAbility(want);
159     lifecycleDeal_->SetScheduler(abilityScheduler_);
160     lifecycleDeal_->ConnectAbility(want);
161 }
162 
163 /*
164  * Feature: LifecycleDeal
165  * Function: DisconnectAbility
166  * SubFunction: NA
167  * FunctionPoints: LifecycleDeal DisconnectAbility
168  * EnvConditions:NA
169  * CaseDescription: Verify DisconnectAbility operation and call mock once
170  */
171 HWTEST_F(LifecycleDealTest, LifecycleDeal_oprator_005, TestSize.Level1)
172 {
173     EXPECT_CALL(*abilityScheduler_, ScheduleDisconnectAbility(::testing::_)).Times(1);
174 
175     const Want want;
176     lifecycleDeal_->DisconnectAbility(want);
177     lifecycleDeal_->SetScheduler(abilityScheduler_);
178     lifecycleDeal_->DisconnectAbility(want);
179 }
180 
181 /*
182  * Feature: LifecycleDeal
183  * Function: Terminate
184  * SubFunction: NA
185  * FunctionPoints: LifecycleDeal Terminate
186  * EnvConditions:NA
187  * CaseDescription: Verify Terminate operation and call mock once
188  */
189 HWTEST_F(LifecycleDealTest, LifecycleDeal_oprator_006, TestSize.Level1)
190 {
191     EXPECT_CALL(*abilityScheduler_, ScheduleAbilityTransaction(::testing::_, ::testing::_, ::testing::_))
192         .Times(1);
193 
194     const Want want;
195     CallerInfo caller;
196     caller.deviceId = "device";
197     caller.bundleName = "bundle";
198     caller.abilityName = "LifecycleDealTest";
199 
200     LifeCycleStateInfo info;
201     info.caller = caller;
202     lifecycleDeal_->Activate(want, info);
203     lifecycleDeal_->SetScheduler(abilityScheduler_);
204     lifecycleDeal_->Activate(want, info);
205 }
206 
207 /*
208  * Feature: LifecycleDeal
209  * Function: CommandAbility
210  * SubFunction: NA
211  * FunctionPoints: LifecycleDeal CommandAbility
212  * EnvConditions:NA
213  * CaseDescription: Verify CommandAbility operation and call mock once
214  */
215 HWTEST_F(LifecycleDealTest, LifecycleDeal_oprator_007, TestSize.Level1)
216 {
217     EXPECT_CALL(*abilityScheduler_, ScheduleCommandAbility(::testing::_, ::testing::_, ::testing::_)).Times(1);
218     const Want want;
219     LifeCycleStateInfo info;
220     lifecycleDeal_->CommandAbility(want, false, 1);
221     lifecycleDeal_->SetScheduler(abilityScheduler_);
222     lifecycleDeal_->CommandAbility(want, false, 1);
223 }
224 
225 /*
226  * Feature: LifecycleDeal
227  * Function: CommandAbilityWindow
228  * SubFunction: NA
229  * FunctionPoints: LifecycleDeal CommandAbilityWindow
230  * EnvConditions:NA
231  * CaseDescription: Verify CommandAbilityWindow operation and call mock once
232  * @tc.require: AR000I8B26
233  */
234 HWTEST_F(LifecycleDealTest, LifecycleDeal_oprator_008, TestSize.Level1)
235 {
236     EXPECT_CALL(*abilityScheduler_, ScheduleCommandAbilityWindow(::testing::_, ::testing::_, ::testing::_)).Times(1);
237     sptr<SessionInfo> sessionInfo = new (std::nothrow) SessionInfo();
238     EXPECT_NE(sessionInfo, nullptr);
239     const Want want;
240     lifecycleDeal_->CommandAbilityWindow(want, sessionInfo, AAFwk::WIN_CMD_FOREGROUND);
241     lifecycleDeal_->SetScheduler(abilityScheduler_);
242     lifecycleDeal_->CommandAbilityWindow(want, sessionInfo, AAFwk::WIN_CMD_FOREGROUND);
243 }
244 }  // namespace AAFwk
245 }  // namespace OHOS
246