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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #define private public
20 #include "child_main_thread.h"
21 #undef private
22 #include "child_process_info.h"
23 #include "event_handler.h"
24 #include "hilog_tag_wrapper.h"
25 #include "mock_app_mgr_service.h"
26 #include "mock_bundle_manager.h"
27 #include "sys_mgr_client.h"
28 #include "system_ability_definition.h"
29
30 using namespace testing;
31 using namespace testing::ext;
32
33 namespace OHOS {
34 namespace AppExecFwk {
35 class ChildMainThreadTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp() override;
40 void TearDown() override;
41 };
42
SetUpTestCase()43 void ChildMainThreadTest::SetUpTestCase()
44 {
45 sptr<IRemoteObject> bundleMgrService = sptr<IRemoteObject>(new (std::nothrow) BundleMgrService());
46 sptr<IRemoteObject> mockAppMgrService = sptr<IRemoteObject>(new (std::nothrow) MockAppMgrService());
47 auto sysMgr = DelayedSingleton<SysMrgClient>::GetInstance();
48 if (sysMgr == nullptr) {
49 GTEST_LOG_(ERROR) << "Failed to get ISystemAbilityManager.";
50 return;
51 }
52
53 sysMgr->RegisterSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, bundleMgrService);
54 sysMgr->RegisterSystemAbility(APP_MGR_SERVICE_ID, mockAppMgrService);
55 }
56
TearDownTestCase()57 void ChildMainThreadTest::TearDownTestCase()
58 {}
59
SetUp()60 void ChildMainThreadTest::SetUp()
61 {}
62
TearDown()63 void ChildMainThreadTest::TearDown()
64 {}
65
66 /**
67 * @tc.number: Init_0100
68 * @tc.desc: Test Init works
69 * @tc.type: FUNC
70 */
71 HWTEST_F(ChildMainThreadTest, Init_0100, TestSize.Level0)
72 {
73 TAG_LOGD(AAFwkTag::TEST, "Init_0100 called.");
74 sptr<ChildMainThread> thread = sptr<ChildMainThread>(new (std::nothrow) ChildMainThread());
75 ASSERT_NE(thread, nullptr);
76
77 std::shared_ptr<EventRunner> runner = EventRunner::GetMainEventRunner();
78 ChildProcessInfo info;
79 auto ret = thread->Init(runner, info);
80 EXPECT_TRUE(ret);
81 }
82
83 /**
84 * @tc.number: Attach_0100
85 * @tc.desc: Test Attach works
86 * @tc.type: FUNC
87 */
88 HWTEST_F(ChildMainThreadTest, Attach_0100, TestSize.Level0)
89 {
90 TAG_LOGD(AAFwkTag::TEST, "Attach_0100 called.");
91 sptr<ChildMainThread> thread = sptr<ChildMainThread>(new (std::nothrow) ChildMainThread());
92 ASSERT_NE(thread, nullptr);
93
94 std::shared_ptr<EventRunner> runner = EventRunner::GetMainEventRunner();
95 thread->mainHandler_ = std::make_shared<EventHandler>(runner);
96
97 auto ret = thread->Attach();
98 EXPECT_TRUE(ret);
99 }
100
101 /**
102 * @tc.number: ScheduleLoadChild_0100
103 * @tc.desc: Test ScheduleLoadChild_0100 works
104 * @tc.type: FUNC
105 */
106 HWTEST_F(ChildMainThreadTest, ScheduleLoadChild_0100, TestSize.Level0)
107 {
108 TAG_LOGD(AAFwkTag::TEST, "ScheduleLoadChild_0100 called.");
109 sptr<ChildMainThread> thread = sptr<ChildMainThread>(new (std::nothrow) ChildMainThread());
110 ASSERT_NE(thread, nullptr);
111
112 std::shared_ptr<EventRunner> runner = EventRunner::GetMainEventRunner();
113 std::shared_ptr<EventHandler> handler = std::make_shared<EventHandler>(runner);
114 thread->mainHandler_ = handler;
115 thread->processInfo_ = std::make_shared<ChildProcessInfo>();
116
117 auto ret = thread->ScheduleLoadChild();
118 EXPECT_TRUE(ret);
119 }
120
121 /**
122 * @tc.number: HandleLoadJs_0100
123 * @tc.desc: Test HandleLoadJs works
124 * @tc.type: FUNC
125 */
126 HWTEST_F(ChildMainThreadTest, HandleLoadJs_0100, TestSize.Level0)
127 {
128 TAG_LOGD(AAFwkTag::TEST, "HandleLoadJs_0100 called.");
129 sptr<ChildMainThread> thread = sptr<ChildMainThread>(new (std::nothrow) ChildMainThread());
130 ASSERT_NE(thread, nullptr);
131
132 BundleInfo bundleInfo;
133 std::vector<HapModuleInfo> hapModuleInfos;
134 HapModuleInfo moduleInfo;
135 moduleInfo.name = "entry";
136 moduleInfo.moduleName = "entry";
137 moduleInfo.moduleType = ModuleType::ENTRY;
138 moduleInfo.hapPath = "/data/app/el1/bundle/public/com.ohos.demoprocess/entry";
139 moduleInfo.compileMode = CompileMode::ES_MODULE;
140 moduleInfo.isStageBasedModel = true;
141 hapModuleInfos.push_back(moduleInfo);
142 bundleInfo.hapModuleInfos = hapModuleInfos;
143
144 ApplicationInfo applicationInfo;
145 applicationInfo.uid = 2001;
146 bundleInfo.applicationInfo = applicationInfo;
147
148 thread->bundleInfo_ = std::make_shared<BundleInfo>(bundleInfo);
149 thread->processInfo_ = std::make_shared<ChildProcessInfo>();
150 thread->appMgr_ = sptr<MockAppMgrService>(new (std::nothrow) MockAppMgrService());
151 thread->HandleLoadJs();
152 ASSERT_NE(thread->runtime_, nullptr);
153 }
154
155 /**
156 * @tc.number: HandleLoadArkTs_0100
157 * @tc.desc: Test HandleLoadArkTs works
158 * @tc.type: FUNC
159 */
160 HWTEST_F(ChildMainThreadTest, HandleLoadArkTs_0100, TestSize.Level0)
161 {
162 TAG_LOGD(AAFwkTag::TEST, "HandleLoadArkTs_0100 called.");
163 sptr<ChildMainThread> thread = sptr<ChildMainThread>(new (std::nothrow) ChildMainThread());
164 ASSERT_NE(thread, nullptr);
165
166 BundleInfo bundleInfo;
167 std::vector<HapModuleInfo> hapModuleInfos;
168 HapModuleInfo moduleInfo;
169 moduleInfo.name = "entry";
170 moduleInfo.moduleName = "entry";
171 moduleInfo.moduleType = ModuleType::ENTRY;
172 moduleInfo.hapPath = "/data/app/el1/bundle/public/com.ohos.demoprocess/entry";
173 moduleInfo.compileMode = CompileMode::ES_MODULE;
174 moduleInfo.isStageBasedModel = true;
175 hapModuleInfos.push_back(moduleInfo);
176 bundleInfo.hapModuleInfos = hapModuleInfos;
177
178 ApplicationInfo applicationInfo;
179 applicationInfo.uid = 2001;
180 bundleInfo.applicationInfo = applicationInfo;
181
182 thread->bundleInfo_ = std::make_shared<BundleInfo>(bundleInfo);
183 thread->processInfo_ = std::make_shared<ChildProcessInfo>();
184 thread->processInfo_->srcEntry = "entry/./ets/process/AProcess.ets";
185 thread->appMgr_ = sptr<MockAppMgrService>(new (std::nothrow) MockAppMgrService());
186 thread->HandleLoadArkTs();
187 ASSERT_NE(thread->runtime_, nullptr);
188 }
189
190 /**
191 * @tc.number: ScheduleExitProcessSafely_0100
192 * @tc.desc: Test ScheduleExitProcessSafely works
193 * @tc.type: FUNC
194 */
195 HWTEST_F(ChildMainThreadTest, ScheduleExitProcessSafely_0100, TestSize.Level0)
196 {
197 TAG_LOGD(AAFwkTag::TEST, "ScheduleExitProcessSafely_0100 called.");
198 sptr<ChildMainThread> thread = sptr<ChildMainThread>(new (std::nothrow) ChildMainThread());
199 ASSERT_NE(thread, nullptr);
200
201 std::shared_ptr<EventRunner> runner = EventRunner::GetMainEventRunner();
202 std::shared_ptr<EventHandler> handler = std::make_shared<EventHandler>(runner);
203 thread->mainHandler_ = handler;
204
205 auto ret = thread->ScheduleExitProcessSafely();
206 EXPECT_TRUE(ret);
207 }
208
209 /**
210 * @tc.number: ScheduleRunNativeProc_0100
211 * @tc.desc: Test ScheduleRunNativeProc works
212 * @tc.type: FUNC
213 */
214 HWTEST_F(ChildMainThreadTest, ScheduleRunNativeProc_0100, TestSize.Level0)
215 {
216 TAG_LOGD(AAFwkTag::TEST, "ScheduleRunNativeProc_0100 called.");
217 sptr<ChildMainThread> thread = sptr<ChildMainThread>(new (std::nothrow) ChildMainThread());
218 ASSERT_NE(thread, nullptr);
219
220 std::shared_ptr<EventRunner> runner = EventRunner::GetMainEventRunner();
221 std::shared_ptr<EventHandler> handler = std::make_shared<EventHandler>(runner);
222 thread->mainHandler_ = handler;
223
224 sptr<IRemoteObject> mainPorcessCb = nullptr;
225 auto ret = thread->ScheduleRunNativeProc(mainPorcessCb);
226 EXPECT_FALSE(ret);
227 }
228
229 } // namespace AppExecFwk
230 } // namespace OHOS
231