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 "ability_thread.h"
18 #define private public
19 #define protected public
20 #include "ability_context_impl.h"
21 #include "caller_callback.h"
22 #undef private
23 #undef protected
24 #include "ability_context.h"
25 #include "ability_loader.h"
26 #include "ability_manager_client.h"
27 #include "mock_serviceability_manager_service.h"
28 #include "system_ability_definition.h"
29 #include "sys_mgr_client.h"
30
31 namespace OHOS {
32 namespace AppExecFwk {
33 using namespace testing::ext;
34 using namespace OHOS::AppExecFwk;
35 using namespace OHOS::AbilityRuntime;
36 using namespace OHOS;
37 using namespace AAFwk;
38
39 namespace {
40 const std::string ACE_SERVICE_ABILITY_NAME = "AceServiceAbility";
41 } // namespace
42 class AbilityCallerTest : public testing::Test {
43 public:
44 static void SetUpTestCase(void);
45 static void TearDownTestCase(void);
46 void SetUp();
47 void TearDown();
48 static constexpr int TEST_WAIT_TIME = 500 * 1000; // 500 ms
49 public:
50 std::unique_ptr<AbilityContextImpl> context_ = nullptr;
51 };
52
SetUpTestCase(void)53 void AbilityCallerTest::SetUpTestCase(void)
54 {
55 OHOS::sptr<OHOS::IRemoteObject> abilityObject = new (std::nothrow) MockServiceAbilityManagerService();
56
57 auto sysMgr = OHOS::DelayedSingleton<SysMrgClient>::GetInstance();
58 if (sysMgr == nullptr) {
59 GTEST_LOG_(ERROR) << "fail to get ISystemAbilityManager";
60 return;
61 }
62
63 sysMgr->RegisterSystemAbility(OHOS::ABILITY_MGR_SERVICE_ID, abilityObject);
64 }
65
TearDownTestCase(void)66 void AbilityCallerTest::TearDownTestCase(void)
67 {}
68
SetUp(void)69 void AbilityCallerTest::SetUp(void)
70 {
71 context_ = std::make_unique<AbilityContextImpl>();
72 }
73
TearDown(void)74 void AbilityCallerTest::TearDown(void)
75 {}
76
77 /**
78 * @tc.number: AaFwk_Ability_StartAbility_0100
79 * @tc.name: AbilityFwk
80 * @tc.desc: Ability caller to process StartAbility, and the result is success.
81 */
82 HWTEST_F(AbilityCallerTest, AaFwk_Ability_StartAbility_0100, Function | MediumTest | Level1)
83 {
84 Want want;
85 want.SetElementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName");
86
87 std::shared_ptr<CallerCallBack> callback = std::make_shared<CallerCallBack>();
__anoncc149f500202(const sptr<IRemoteObject> &) 88 callback->SetCallBack([](const sptr<IRemoteObject> &) {});
89 EXPECT_FALSE(callback->IsCallBack());
90
91 ErrCode ret = context_->StartAbilityByCall(want, callback);
92 EXPECT_TRUE(ret == 0);
93
94 AppExecFwk::ElementName element("DemoDeviceId", "DemoBundleName", "DemoAbilityName");
95 sptr<IRemoteObject> callRemoteObject =
96 OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
97 context_->localCallContainer_->OnAbilityConnectDone(element, callRemoteObject, ERR_OK);
98 std::vector<std::string> info;
99 EXPECT_TRUE(info.size() == 0);
100 context_->localCallContainer_->DumpCalls(info);
101 EXPECT_TRUE(info.size() != 0);
102 EXPECT_TRUE(callback->IsCallBack());
103 }
104
105 /**
106 * @tc.number: AaFwk_Ability_StartAbility_0200
107 * @tc.name: AbilityFwk
108 * @tc.desc: Ability caller to process StartAbility, and the result is fail because call back is nullptr.
109 */
110 HWTEST_F(AbilityCallerTest, AaFwk_Ability_StartAbility_0200, Function | MediumTest | Level1)
111 {
112 Want want;
113 want.SetElementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName");
114
115 ErrCode ret = context_->StartAbilityByCall(want, nullptr);
116 EXPECT_FALSE(ret == 0);
117 }
118
119 /**
120 * @tc.number: AaFwk_Ability_StartAbility_0300
121 * @tc.name: AbilityFwk
122 * @tc.desc: Ability caller to process StartAbility, and the result is fail because the element of want is empty.
123 */
124 HWTEST_F(AbilityCallerTest, AaFwk_Ability_StartAbility_0300, Function | MediumTest | Level1)
125 {
126 Want want;
127 std::shared_ptr<CallerCallBack> callback = std::make_shared<CallerCallBack>();
__anoncc149f500302(const sptr<IRemoteObject> &) 128 callback->SetCallBack([](const sptr<IRemoteObject> &) {});
129 EXPECT_FALSE(callback->IsCallBack());
130
131 ErrCode ret = context_->StartAbilityByCall(want, callback);
132 EXPECT_FALSE(ret == 0);
133 }
134
135 /**
136 * @tc.number: AaFwk_Ability_ReleaseCall_0100
137 * @tc.name: AbilityFwk
138 * @tc.desc: Ability Caller to process ReleaseCall, and the result is success.
139 */
140 HWTEST_F(AbilityCallerTest, AaFwk_Ability_ReleaseCall_0100, Function | MediumTest | Level1)
141 {
142 Want want;
143 want.SetElementName("DemoDeviceIdB", "DemoBundleNameB", "DemoAbilityNameB");
144
145 std::shared_ptr<CallerCallBack> callback = std::make_shared<CallerCallBack>();
__anoncc149f500402(const sptr<IRemoteObject> &) 146 callback->SetCallBack([](const sptr<IRemoteObject> &) {});
147
148 ErrCode ret = context_->StartAbilityByCall(want, callback);
149 EXPECT_TRUE(ret == 0);
150
151 ret = context_->ReleaseCall(callback);
152 EXPECT_TRUE(ret == 0);
153 }
154
155 /**
156 * @tc.number: AaFwk_Ability_ReleaseCall_0200
157 * @tc.name: AbilityFwk
158 * @tc.desc: Ability Caller to process ReleaseCall, and the result is fail because has no caller record.
159 */
160 HWTEST_F(AbilityCallerTest, AaFwk_Ability_ReleaseCall_0200, Function | MediumTest | Level1)
161 {
162 std::shared_ptr<CallerCallBack> callback = std::make_shared<CallerCallBack>();
__anoncc149f500502(const sptr<IRemoteObject> &) 163 callback->SetCallBack([](const sptr<IRemoteObject> &) {});
164
165 ErrCode ret = context_->ReleaseCall(callback);
166 EXPECT_FALSE(ret == 0);
167 }
168
169 /**
170 * @tc.number: AaFwk_Ability_OnCallStubDied_0100
171 * @tc.name: AbilityFwk
172 * @tc.desc: Ability Caller to process OnCallStubDied, and the result is success.
173 */
174 HWTEST_F(AbilityCallerTest, AaFwk_Ability_OnCallStubDied_0100, Function | MediumTest | Level1)
175 {
176 Want want;
177 want.SetElementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName");
178
179 std::shared_ptr<CallerCallBack> callback = std::make_shared<CallerCallBack>();
180 bool isSetOnReleaseCalled = false;
__anoncc149f500602(const sptr<IRemoteObject> &) 181 callback->SetCallBack([](const sptr<IRemoteObject> &) {});
__anoncc149f500702(const std::string &result) 182 callback->SetOnRelease([&isSetOnReleaseCalled](const std::string &result) mutable {
183 GTEST_LOG_(ERROR) << "OnRelease-----------" << result;
184 EXPECT_TRUE(result == "died");
185 isSetOnReleaseCalled = true;
186 });
187
188 ErrCode ret = context_->StartAbilityByCall(want, callback);
189 EXPECT_TRUE(ret == 0);
190
191 std::shared_ptr<LocalCallRecord> localCallRecord;
192 AppExecFwk::ElementName elementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName");
193 context_->localCallContainer_->GetCallLocalRecord(elementName, localCallRecord);
194
195 sptr<IRemoteObject> callRemoteObject =
196 OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
197 localCallRecord->OnCallStubDied(callRemoteObject);
198 EXPECT_TRUE(isSetOnReleaseCalled);
199 }
200
201 /**
202 * @tc.number: AaFwk_Ability_OnCallStubDied_0200
203 * @tc.name: AbilityFwk
204 * @tc.desc: Ability Caller to process OnCallStubDied, and the result is fail because no caller.
205 */
206 HWTEST_F(AbilityCallerTest, AaFwk_Ability_OnCallStubDied_0200, Function | MediumTest | Level1)
207 {
208 std::shared_ptr<CallerCallBack> callback = std::make_shared<CallerCallBack>();
209 bool isSetOnReleaseCalled = false;
__anoncc149f500802(const std::string &result) 210 callback->SetOnRelease([&isSetOnReleaseCalled](const std::string &result) mutable {
211 GTEST_LOG_(ERROR) << "OnRelease-----------" << result;
212 isSetOnReleaseCalled = true;
213 });
214
215 AppExecFwk::ElementName elementName ("DemoDeviceId", "DemoBundleName", "DemoAbilityName");
216 LocalCallRecord localCallRecord(elementName);
217
218 sptr<IRemoteObject> callRemoteObject =
219 OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
220 localCallRecord.OnCallStubDied(callRemoteObject);
221 EXPECT_FALSE(isSetOnReleaseCalled);
222 }
223 } // namespace AppExecFwk
224 } // namespace OHOS