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
18 #include "mock_app_mgr_service.h"
19 #include "app_mgr_proxy.h"
20 #include "hilog_wrapper.h"
21 #include "quick_fix_callback_stub.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 const int32_t USER_ID = 100;
30 } // namespace
31
32 class QuickFixCallbackImpl : public AppExecFwk::QuickFixCallbackStub {
33 public:
34 QuickFixCallbackImpl() = default;
35 virtual ~QuickFixCallbackImpl() = default;
36
OnLoadPatchDone(int32_t resultCode,int32_t recordId)37 void OnLoadPatchDone(int32_t resultCode, int32_t recordId) override
38 {
39 HILOG_DEBUG("function called.");
40 }
41
OnUnloadPatchDone(int32_t resultCode,int32_t recordId)42 void OnUnloadPatchDone(int32_t resultCode, int32_t recordId) override
43 {
44 HILOG_DEBUG("function called.");
45 }
46
OnReloadPageDone(int32_t resultCode,int32_t recordId)47 void OnReloadPageDone(int32_t resultCode, int32_t recordId) override
48 {
49 HILOG_DEBUG("function called.");
50 }
51 };
52
53 class AppMgrProxyTest : public testing::Test {
54 public:
55 static void SetUpTestCase();
56 static void TearDownTestCase();
57 void SetUp() override;
58 void TearDown() override;
59
60 sptr<MockAppMgrService> mockAppMgrService_;
61 sptr<AppMgrProxy> appMgrProxy_;
62 };
63
SetUpTestCase(void)64 void AppMgrProxyTest::SetUpTestCase(void)
65 {}
66
TearDownTestCase(void)67 void AppMgrProxyTest::TearDownTestCase(void)
68 {}
69
SetUp()70 void AppMgrProxyTest::SetUp()
71 {
72 GTEST_LOG_(INFO) << "AppMgrProxyTest::SetUp()";
73
74 mockAppMgrService_ = new MockAppMgrService();
75 appMgrProxy_ = new AppMgrProxy(mockAppMgrService_);
76 }
77
TearDown()78 void AppMgrProxyTest::TearDown()
79 {}
80
81 /**
82 * @tc.name: AppMgrProxy_GetProcessRunningInfosByUserId_0100
83 * @tc.desc: GetProcessRunningInfosByUserId
84 * @tc.type: FUNC
85 * @tc.require: SR000GH1GO
86 */
87 HWTEST_F(AppMgrProxyTest, AppMgrProxy_GetProcessRunningInfosByUserId_0100, TestSize.Level0)
88 {
89 GTEST_LOG_(INFO) << "AppMgrProxy_GetProcessRunningInfosByUserId_0100 start";
90
91 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
92 .Times(1)
93 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
94
95 std::vector<RunningProcessInfo> info;
96 appMgrProxy_->GetProcessRunningInfosByUserId(info, USER_ID);
97
98 EXPECT_EQ(
99 mockAppMgrService_->code_, static_cast<uint32_t>(IAppMgr::Message::APP_GET_RUNNING_PROCESSES_BY_USER_ID));
100
101 GTEST_LOG_(INFO) << "AppMgrProxy_GetProcessRunningInfosByUserId_0100 end";
102 }
103
104 /**
105 * @tc.name: GetAppRunningStateByBundleName_0100
106 * @tc.desc: Get app running state by bundle name.
107 * @tc.type: FUNC
108 * @tc.require: issueI581VW
109 */
110 HWTEST_F(AppMgrProxyTest, GetAppRunningStateByBundleName_0100, TestSize.Level0)
111 {
112 HILOG_INFO("%{public}s start.", __func__);
113
114 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
115 .Times(1)
116 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
117
118 std::string bundleName = "testBundleName";
119 appMgrProxy_->GetAppRunningStateByBundleName(bundleName);
120
121 EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(IAppMgr::Message::GET_APP_RUNNING_STATE));
122
123 HILOG_INFO("%{public}s end.", __func__);
124 }
125
126 /**
127 * @tc.name: NotifyLoadRepairPatch_0100
128 * @tc.desc: Notify load repair patch.
129 * @tc.type: FUNC
130 * @tc.require: issueI581VW
131 */
132 HWTEST_F(AppMgrProxyTest, NotifyLoadRepairPatch_0100, TestSize.Level0)
133 {
134 HILOG_INFO("%{public}s start.", __func__);
135
136 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
137 .Times(1)
138 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
139
140 std::string bundleName = "testBundleName";
141 sptr<IQuickFixCallback> callback = new QuickFixCallbackImpl();
142 appMgrProxy_->NotifyLoadRepairPatch(bundleName, callback);
143
144 EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(IAppMgr::Message::NOTIFY_LOAD_REPAIR_PATCH));
145
146 HILOG_INFO("%{public}s end.", __func__);
147 }
148
149 /**
150 * @tc.name: NotifyHotReloadPage_0100
151 * @tc.desc: Notify ace execute hot reload page.
152 * @tc.type: FUNC
153 * @tc.require: issueI581VW
154 */
155 HWTEST_F(AppMgrProxyTest, NotifyHotReloadPage_0100, TestSize.Level0)
156 {
157 HILOG_INFO("%{public}s start", __func__);
158
159 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
160 .Times(1)
161 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
162
163 std::string bundleName = "testBundleName";
164 sptr<IQuickFixCallback> callback = new QuickFixCallbackImpl();
165 appMgrProxy_->NotifyHotReloadPage(bundleName, callback);
166
167 EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(IAppMgr::Message::NOTIFY_HOT_RELOAD_PAGE));
168
169 HILOG_INFO("%{public}s end", __func__);
170 }
171
172 /**
173 * @tc.name: NotifyUnLoadRepairPatch_0100
174 * @tc.desc: Notify unload repair patch.
175 * @tc.type: FUNC
176 * @tc.require: issueI581VW
177 */
178 HWTEST_F(AppMgrProxyTest, NotifyUnLoadRepairPatch_0100, TestSize.Level0)
179 {
180 HILOG_INFO("%{public}s start.", __func__);
181
182 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
183 .Times(1)
184 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
185
186 std::string bundleName = "testBundleName";
187 sptr<IQuickFixCallback> callback = new QuickFixCallbackImpl();
188 appMgrProxy_->NotifyUnLoadRepairPatch(bundleName, callback);
189
190 EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(IAppMgr::Message::NOTIFY_UNLOAD_REPAIR_PATCH));
191
192 HILOG_INFO("%{public}s end.", __func__);
193 }
194
195 /**
196 * @tc.name: PreStartNWebSpawnProcess_001
197 * @tc.desc: prestart nwebspawn process.
198 * @tc.type: FUNC
199 * @tc.require: issueI5W4S7
200 */
201 HWTEST_F(AppMgrProxyTest, PreStartNWebSpawnProcess_001, TestSize.Level0)
202 {
203 HILOG_INFO("%{public}s start.", __func__);
204
205 EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _))
206 .Times(1)
207 .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest));
208
209 appMgrProxy_->PreStartNWebSpawnProcess();
210 EXPECT_EQ(mockAppMgrService_->code_, static_cast<uint32_t>(IAppMgr::Message::PRE_START_NWEBSPAWN_PROCESS));
211
212 HILOG_INFO("%{public}s end.", __func__);
213 }
214 } // namespace AppExecFwk
215 } // namespace OHOS
216