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_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 HILOG_DEBUG("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 HILOG_DEBUG("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: ScheduleLoadJs_0100
103 * @tc.desc: Test ScheduleLoadJs_0100 works
104 * @tc.type: FUNC
105 */
106 HWTEST_F(ChildMainThreadTest, ScheduleLoadJs_0100, TestSize.Level0)
107 {
108 HILOG_DEBUG("ScheduleLoadJs_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
116 auto ret = thread->ScheduleLoadJs();
117 EXPECT_TRUE(ret);
118 }
119
120 /**
121 * @tc.number: HandleLoadJs_0100
122 * @tc.desc: Test HandleLoadJs works
123 * @tc.type: FUNC
124 */
125 HWTEST_F(ChildMainThreadTest, HandleLoadJs_0100, TestSize.Level0)
126 {
127 HILOG_DEBUG("HandleLoadJs_0100 called.");
128 sptr<ChildMainThread> thread = sptr<ChildMainThread>(new (std::nothrow) ChildMainThread());
129 ASSERT_NE(thread, nullptr);
130
131 BundleInfo bundleInfo;
132 std::vector<HapModuleInfo> hapModuleInfos;
133 HapModuleInfo moduleInfo;
134 moduleInfo.name = "entry";
135 moduleInfo.moduleName = "entry";
136 moduleInfo.moduleType = ModuleType::ENTRY;
137 moduleInfo.hapPath = "/data/app/el1/bundle/public/com.ohos.demoprocess/entry";
138 moduleInfo.compileMode = CompileMode::ES_MODULE;
139 moduleInfo.isStageBasedModel = true;
140 hapModuleInfos.push_back(moduleInfo);
141 bundleInfo.hapModuleInfos = hapModuleInfos;
142
143 ApplicationInfo applicationInfo;
144 applicationInfo.uid = 2001;
145 bundleInfo.applicationInfo = applicationInfo;
146
147 thread->bundleInfo_ = std::make_shared<BundleInfo>(bundleInfo);
148 thread->processInfo_ = std::make_shared<ChildProcessInfo>();
149 thread->appMgr_ = sptr<MockAppMgrService>(new (std::nothrow) MockAppMgrService());
150 thread->HandleLoadJs();
151 ASSERT_NE(thread->runtime_, nullptr);
152 }
153
154 /**
155 * @tc.number: ScheduleExitProcessSafely_0100
156 * @tc.desc: Test ScheduleExitProcessSafely works
157 * @tc.type: FUNC
158 */
159 HWTEST_F(ChildMainThreadTest, ScheduleExitProcessSafely_0100, TestSize.Level0)
160 {
161 HILOG_DEBUG("ScheduleExitProcessSafely_0100 called.");
162 sptr<ChildMainThread> thread = sptr<ChildMainThread>(new (std::nothrow) ChildMainThread());
163 ASSERT_NE(thread, nullptr);
164
165 std::shared_ptr<EventRunner> runner = EventRunner::GetMainEventRunner();
166 std::shared_ptr<EventHandler> handler = std::make_shared<EventHandler>(runner);
167 thread->mainHandler_ = handler;
168
169 auto ret = thread->ScheduleExitProcessSafely();
170 EXPECT_TRUE(ret);
171 }
172 } // namespace AppExecFwk
173 } // namespace OHOS