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 #include <gtest/hwext/gtest-multithread.h>
18
19 #include <map>
20 #include <string>
21 #include <thread>
22 #include <iostream>
23
24 #include "ability_lifecycle_executor.h"
25
26 #define private public
27 #include "iability_monitor.h"
28 #include "iability_stage_monitor.h"
29 #include "ability_manager_client.h"
30 #undef private
31
32 #include "hilog_tag_wrapper.h"
33 #include "mock_ability_delegator_stub.h"
34
35 using namespace testing;
36 using namespace testing::ext;
37 using namespace testing::mt;
38 using namespace OHOS;
39 using namespace OHOS::AppExecFwk;
40 using namespace OHOS::AAFwk;
41
42 namespace OHOS {
43 namespace AppExecFwk {
44 namespace {
45 const std::string ABILITY_NAME = "com.example.myapplication.MainAbilitymodule";
46 const std::string PROPERTY_ABILITY_NAME = "com.example.myapplication.MainAbilitymodule";
47 const std::string PROPERTY_ABILITY_NAME1 = "com.example.myapplication.MainAbility1module";
48 const std::string ABILITY_STAGE_MODULE_NAME = "com.example.entry_test";
49 const std::string ABILITY_STAGE_SOURCE_ENTRANCE = "./ets/Application/TestAbilityStage.ts";
50 const std::string PROPERTY_ABILITY_STAGE_MODULE_NAME = "com.example.entry_test";
51 const std::string PROPERTY_ABILITY_STAGE_SOURCE_ENTRANCE = "./ets/Application/TestAbilityStage.ts";
52 const std::string PROPERTY_ABILITY_STAGE_MODULE_NAME2 = "com.example.entry_test2";
53 const std::string PROPERTY_ABILITY_STAGE_SOURCE_ENTRANCE2 = "./ets/Application/TestAbilityStage2.ts";
54 }
55
56 class IabilityMonitorModuleTest : public ::testing::Test {
57 public:
58 static void SetUpTestCase();
59 static void TearDownTestCase();
60 void SetUp() override;
61 void TearDown() override;
62
63 void MakeMockObjects() const;
64 };
65
SetUpTestCase()66 void IabilityMonitorModuleTest::SetUpTestCase()
67 {}
68
TearDownTestCase()69 void IabilityMonitorModuleTest::TearDownTestCase()
70 {}
71
SetUp()72 void IabilityMonitorModuleTest::SetUp()
73 {
74 // reset optind to 0
75 optind = 0;
76
77 // make mock objects
78 MakeMockObjects();
79 }
80
TearDown()81 void IabilityMonitorModuleTest::TearDown()
82 {}
83
MakeMockObjects() const84 void IabilityMonitorModuleTest::MakeMockObjects() const
85 {
86 // mock a stub
87 auto managerStubPtr = sptr<OHOS::AAFwk::IAbilityManager>(new MockAbilityDelegatorStub());
88
89 // set the mock stub
90 auto managerClientPtr = AbilityManagerClient::GetInstance();
91 managerClientPtr->proxy_ = managerStubPtr;
92 }
93
94 /**
95 * @tc.number: Iability_Monitor_Test_0100
96 * @tc.name: Match
97 * @tc.desc: Verify the Match.
98 */
99 HWTEST_F(IabilityMonitorModuleTest, Iability_Monitor_Test_0100, Function | MediumTest | Level1)
100 {
101 TAG_LOGI(AAFwkTag::TEST, "Iability_Monitor_Test_0100 is called");
102
103 IAbilityMonitor iabilityMonitor(ABILITY_NAME);
104 std::shared_ptr<ADelegatorAbilityProperty> property = std::make_shared<ADelegatorAbilityProperty>();
105 property->token_ = new MockAbilityDelegatorStub;
106 property->name_ = PROPERTY_ABILITY_NAME;
107 EXPECT_TRUE(iabilityMonitor.Match(property));
108 }
109
110 /**
111 * @tc.number: Iability_Monitor_Test_0200
112 * @tc.name: Match AbilityStage
113 * @tc.desc: Verify the AbilityStage Match.
114 * @tc.require: issueI5801E
115 */
116 HWTEST_F(IabilityMonitorModuleTest, Iability_Monitor_Test_0200, Function | MediumTest | Level1)
117 {
118 TAG_LOGI(AAFwkTag::TEST, "Iability_Monitor_Test_0200 is called");
119
120 IAbilityStageMonitor stageMonitor(ABILITY_STAGE_MODULE_NAME, ABILITY_STAGE_SOURCE_ENTRANCE);
121 std::shared_ptr<DelegatorAbilityStageProperty> property = std::make_shared<DelegatorAbilityStageProperty>();
122 property->moduleName_ = PROPERTY_ABILITY_STAGE_MODULE_NAME;
123 property->srcEntrance_ = PROPERTY_ABILITY_STAGE_SOURCE_ENTRANCE;
124 EXPECT_TRUE(stageMonitor.Match(property));
125
126 std::shared_ptr<DelegatorAbilityStageProperty> property2 = std::make_shared<DelegatorAbilityStageProperty>();
127 property->moduleName_ = PROPERTY_ABILITY_STAGE_MODULE_NAME2;
128 property->srcEntrance_ = PROPERTY_ABILITY_STAGE_SOURCE_ENTRANCE;
129 EXPECT_FALSE(stageMonitor.Match(property2));
130
131 std::shared_ptr<DelegatorAbilityStageProperty> property3 = std::make_shared<DelegatorAbilityStageProperty>();
132 property->moduleName_ = PROPERTY_ABILITY_STAGE_MODULE_NAME;
133 property->srcEntrance_ = PROPERTY_ABILITY_STAGE_SOURCE_ENTRANCE2;
134 EXPECT_FALSE(stageMonitor.Match(property3));
135 }
136
137 /**
138 * @tc.name: MatchTest_0100
139 * @tc.desc: Match test when ability stage is invalid.
140 * @tc.type: FUNC
141 * @tc.require: issueI76SHL
142 */
143 HWTEST_F(IabilityMonitorModuleTest, MatchTest_0100, TestSize.Level1)
144 {
145 TAG_LOGI(AAFwkTag::TEST, "test start.");
146 IAbilityStageMonitor stageMonitor(ABILITY_STAGE_MODULE_NAME, ABILITY_STAGE_SOURCE_ENTRANCE);
147 EXPECT_FALSE(stageMonitor.Match(nullptr));
148 }
149
150 /**
151 * @tc.name: MatchTest_0200
152 * @tc.desc: Test notify when matched.
153 * @tc.type: FUNC
154 * @tc.require: issueI76SHL
155 */
156 HWTEST_F(IabilityMonitorModuleTest, MatchTest_0200, TestSize.Level1)
157 {
158 TAG_LOGI(AAFwkTag::TEST, "test start.");
159 IAbilityStageMonitor stageMonitor(ABILITY_STAGE_MODULE_NAME, ABILITY_STAGE_SOURCE_ENTRANCE);
160 std::shared_ptr<DelegatorAbilityStageProperty> property = std::make_shared<DelegatorAbilityStageProperty>();
161 property->moduleName_ = PROPERTY_ABILITY_STAGE_MODULE_NAME;
162 property->srcEntrance_ = PROPERTY_ABILITY_STAGE_SOURCE_ENTRANCE;
163 EXPECT_TRUE(stageMonitor.Match(property, true));
164 }
165
166 /**
167 * @tc.name: WaitForAbility_0100
168 * @tc.desc: Wait for ability timeout.
169 * @tc.type: FUNC
170 * @tc.require: issueI76SHL
171 */
172 HWTEST_F(IabilityMonitorModuleTest, WaitForAbilityTest_0100, TestSize.Level1)
173 {
174 TAG_LOGI(AAFwkTag::TEST, "test start.");
175 IAbilityStageMonitor stageMonitor(ABILITY_STAGE_MODULE_NAME, ABILITY_STAGE_SOURCE_ENTRANCE);
176
177 // wait for 100ms until timeout
178 EXPECT_EQ(stageMonitor.WaitForAbilityStage(100), nullptr);
179 }
180
181 /**
182 * @tc.name: WaitForAbilityTest_0200
183 * @tc.desc: Wait for ability in multi-thread test.
184 * @tc.type: FUNC
185 * @tc.require: issueI76SHL
186 */
187 std::shared_ptr<IAbilityStageMonitor> gt_iAbilityStageMonitor = nullptr;
188
IAbilityStageMonitorWaitTask()189 void IAbilityStageMonitorWaitTask()
190 {
191 ASSERT_NE(gt_iAbilityStageMonitor, nullptr);
192 TAG_LOGI(AAFwkTag::TEST, "Running in thread %{public}d", gettid());
193 auto property = gt_iAbilityStageMonitor->WaitForAbilityStage();
194 if (property == nullptr) {
195 TAG_LOGW(AAFwkTag::TEST, "Wait for ability stage failed.");
196 }
197 }
198
IAbilityStageMonitorMatchTask()199 void IAbilityStageMonitorMatchTask()
200 {
201 ASSERT_NE(gt_iAbilityStageMonitor, nullptr);
202 TAG_LOGI(AAFwkTag::TEST, "Running in thread %{public}d", gettid());
203 std::shared_ptr<DelegatorAbilityStageProperty> property = std::make_shared<DelegatorAbilityStageProperty>();
204 property->moduleName_ = PROPERTY_ABILITY_STAGE_MODULE_NAME;
205 property->srcEntrance_ = PROPERTY_ABILITY_STAGE_SOURCE_ENTRANCE;
206 EXPECT_TRUE(gt_iAbilityStageMonitor->Match(property, true));
207 }
208
209 HWTEST_F(IabilityMonitorModuleTest, WaitForAbilityTest_0200, TestSize.Level1)
210 {
211 TAG_LOGI(AAFwkTag::TEST, "test start.");
212 gt_iAbilityStageMonitor = std::make_shared<IAbilityStageMonitor>(ABILITY_STAGE_MODULE_NAME,
213 ABILITY_STAGE_SOURCE_ENTRANCE);
214 SET_THREAD_NUM(1);
215 GTEST_RUN_TASK(IAbilityStageMonitorWaitTask);
216 GTEST_RUN_TASK(IAbilityStageMonitorMatchTask);
217 gt_iAbilityStageMonitor.reset();
218 }
219 } // namespace AppExecFwk
220 } // namespace OHOS
221