• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 
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>();
__anonfb6e11720202(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     for (auto& item : context_->localCallContainer_->connections_) {
98         if (item->localCallRecord_->GetElementName().GetURI() == element.GetURI()) {
99             item->OnAbilityConnectDone(
100                 element, callRemoteObject, static_cast<int32_t>(AppExecFwk::LaunchMode::SINGLETON));
101         }
102     }
103     std::vector<std::string> info;
104     EXPECT_TRUE(info.size() == 0);
105     context_->localCallContainer_->DumpCalls(info);
106     EXPECT_TRUE(info.size() != 0);
107     EXPECT_TRUE(callback->IsCallBack());
108 }
109 
110 /**
111  * @tc.number: AaFwk_Ability_StartAbility_0200
112  * @tc.name: AbilityFwk
113  * @tc.desc: Ability caller to process StartAbility, and the result is fail because call back is nullptr.
114  */
115 HWTEST_F(AbilityCallerTest, AaFwk_Ability_StartAbility_0200, Function | MediumTest | Level1)
116 {
117     Want want;
118     want.SetElementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName");
119 
120     ErrCode ret = context_->StartAbilityByCall(want, nullptr);
121     EXPECT_FALSE(ret == 0);
122 }
123 
124 /**
125  * @tc.number: AaFwk_Ability_StartAbility_0300
126  * @tc.name: AbilityFwk
127  * @tc.desc: Ability caller to process StartAbility, and the result is fail because the element of want is empty.
128  */
129 HWTEST_F(AbilityCallerTest, AaFwk_Ability_StartAbility_0300, Function | MediumTest | Level1)
130 {
131     Want want;
132     std::shared_ptr<CallerCallBack> callback = std::make_shared<CallerCallBack>();
__anonfb6e11720302(const sptr<IRemoteObject>&) 133     callback->SetCallBack([](const sptr<IRemoteObject>&) {});
134     EXPECT_FALSE(callback->IsCallBack());
135 
136     ErrCode ret = context_->StartAbilityByCall(want, callback);
137     EXPECT_FALSE(ret == 0);
138 }
139 
140 /**
141  * @tc.number: AaFwk_Ability_ReleaseCall_0100
142  * @tc.name: AbilityFwk
143  * @tc.desc: Ability Caller to process ReleaseCall, and the result is success.
144  */
145 HWTEST_F(AbilityCallerTest, AaFwk_Ability_ReleaseCall_0100, Function | MediumTest | Level1)
146 {
147     Want want;
148     want.SetElementName("DemoDeviceIdB", "DemoBundleNameB", "DemoAbilityNameB");
149 
150     std::shared_ptr<CallerCallBack> callback = std::make_shared<CallerCallBack>();
__anonfb6e11720402(const sptr<IRemoteObject>&) 151     callback->SetCallBack([](const sptr<IRemoteObject>&) {});
152 
153     ErrCode ret = context_->StartAbilityByCall(want, callback);
154     EXPECT_TRUE(ret == 0);
155 
156     sptr<IRemoteObject> callRemoteObject =
157         OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
158     for (auto& item : context_->localCallContainer_->connections_) {
159         if (item->localCallRecord_->GetElementName().GetURI() == want.GetElement().GetURI()) {
160             item->OnAbilityConnectDone(
161                 want.GetElement(), callRemoteObject, static_cast<int32_t>(AppExecFwk::LaunchMode::SINGLETON));
162         }
163     }
164 
165     ret = context_->ReleaseCall(callback);
166     EXPECT_TRUE(ret == 0);
167 }
168 
169 /**
170  * @tc.number: AaFwk_Ability_ReleaseCall_0200
171  * @tc.name: AbilityFwk
172  * @tc.desc: Ability Caller to process ReleaseCall, and the result is fail because has no caller record.
173  */
174 HWTEST_F(AbilityCallerTest, AaFwk_Ability_ReleaseCall_0200, Function | MediumTest | Level1)
175 {
176     std::shared_ptr<CallerCallBack> callback = std::make_shared<CallerCallBack>();
__anonfb6e11720502(const sptr<IRemoteObject>&) 177     callback->SetCallBack([](const sptr<IRemoteObject>&) {});
178 
179     ErrCode ret = context_->ReleaseCall(callback);
180     EXPECT_FALSE(ret == 0);
181 }
182 
183 /**
184  * @tc.number: AaFwk_Ability_OnCallStubDied_0100
185  * @tc.name: AbilityFwk
186  * @tc.desc: Ability Caller to process OnCallStubDied, and the result is success.
187  */
188 HWTEST_F(AbilityCallerTest, AaFwk_Ability_OnCallStubDied_0100, Function | MediumTest | Level1)
189 {
190     Want want;
191     want.SetElementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName");
192 
193     std::shared_ptr<CallerCallBack> callback = std::make_shared<CallerCallBack>();
194     bool isSetOnReleaseCalled = false;
__anonfb6e11720602(const sptr<IRemoteObject>&) 195     callback->SetCallBack([](const sptr<IRemoteObject>&) {});
__anonfb6e11720702(const std::string& result) 196     callback->SetOnRelease([&isSetOnReleaseCalled](const std::string& result) mutable {
197         GTEST_LOG_(ERROR) << "OnRelease-----------" << result;
198         EXPECT_TRUE(result == "died");
199         isSetOnReleaseCalled = true;
200     });
201     std::shared_ptr<LocalCallRecord> localCallRecord = std::make_shared<LocalCallRecord>(want.GetElement());
202     localCallRecord->AddCaller(callback);
203     localCallRecord->OnCallStubDied(nullptr);
204     EXPECT_TRUE(isSetOnReleaseCalled);
205 }
206 
207 /**
208  * @tc.number: AaFwk_Ability_OnCallStubDied_0200
209  * @tc.name: AbilityFwk
210  * @tc.desc: Ability Caller to process OnCallStubDied, and the result is fail because no caller.
211  */
212 HWTEST_F(AbilityCallerTest, AaFwk_Ability_OnCallStubDied_0200, Function | MediumTest | Level1)
213 {
214     std::shared_ptr<CallerCallBack> callback = std::make_shared<CallerCallBack>();
215     bool isSetOnReleaseCalled = false;
__anonfb6e11720802(const std::string& result) 216     callback->SetOnRelease([&isSetOnReleaseCalled](const std::string& result) mutable {
217         GTEST_LOG_(ERROR) << "OnRelease-----------" << result;
218         isSetOnReleaseCalled = true;
219     });
220 
221     AppExecFwk::ElementName elementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName");
222     LocalCallRecord localCallRecord(elementName);
223 
224     sptr<IRemoteObject> callRemoteObject =
225         OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
226     localCallRecord.OnCallStubDied(callRemoteObject);
227     EXPECT_FALSE(isSetOnReleaseCalled);
228 }
229 } // namespace AppExecFwk
230 } // namespace OHOS