• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "app_scheduler_proxy.h"
17 #include <unistd.h>
18 #include <gtest/gtest.h>
19 #include "errors.h"
20 #include "ipc_types.h"
21 #include "app_mgr_proxy.h"
22 #include "app_record_id.h"
23 #include "hilog_wrapper.h"
24 #include "mock_application.h"
25 #include "mock_app_mgr_service.h"
26 #include "application_state_observer_stub.h"
27 
28 using namespace testing::ext;
29 
30 using OHOS::iface_cast;
31 using OHOS::sptr;
32 using testing::_;
33 using testing::Invoke;
34 using testing::InvokeWithoutArgs;
35 using testing::Return;
36 
37 namespace OHOS {
38 namespace AppExecFwk {
39 class AmsIpcAppMgrInterfaceTest : public testing::Test {
40 public:
41     static void SetUpTestCase();
42     static void TearDownTestCase();
43     void SetUp();
44     void TearDown();
45 };
46 
SetUpTestCase()47 void AmsIpcAppMgrInterfaceTest::SetUpTestCase()
48 {}
49 
TearDownTestCase()50 void AmsIpcAppMgrInterfaceTest::TearDownTestCase()
51 {}
52 
SetUp()53 void AmsIpcAppMgrInterfaceTest::SetUp()
54 {}
55 
TearDown()56 void AmsIpcAppMgrInterfaceTest::TearDown()
57 {}
58 
59 /*
60  * Feature: AMS
61  * Function: IPC
62  * SubFunction: appmgr interface
63  * FunctionPoints: interface
64  * CaseDescription: test interface of AttachApplication
65  */
66 HWTEST_F(AmsIpcAppMgrInterfaceTest, Interface_001, TestSize.Level1)
67 {
68     HILOG_DEBUG("AppMgrIpcInterfaceTest_AppMgr_001 start");
69     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
70     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
71     sptr<MockApplication> app(new MockApplication());
72 
73     EXPECT_CALL(*mockAppMgr, AttachApplication(_))
74         .WillOnce(InvokeWithoutArgs(mockAppMgr.GetRefPtr(), &MockAppMgrService::Post));
75     appMgrClient->AttachApplication(app);
76     mockAppMgr->Wait();
77     HILOG_DEBUG("AppMgrIpcInterfaceTest_AppMgr_001 end");
78 }
79 
80 /*
81  * Feature: AMS
82  * Function: IPC
83  * SubFunction: appmgr interface
84  * FunctionPoints: interface
85  * CaseDescription: test interface of ApplicationForegrounded
86  */
87 HWTEST_F(AmsIpcAppMgrInterfaceTest, Interface_002, TestSize.Level1)
88 {
89     HILOG_DEBUG("AppMgrIpcInterfaceTest_AppMgr_002 start");
90     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
91     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
92 
93     EXPECT_CALL(*mockAppMgr, ApplicationForegrounded(_))
94         .WillOnce(InvokeWithoutArgs(mockAppMgr.GetRefPtr(), &MockAppMgrService::Post));
95     auto recordId = AppRecordId::Create();
96     appMgrClient->ApplicationForegrounded(recordId);
97     mockAppMgr->Wait();
98     HILOG_DEBUG("AppMgrIpcInterfaceTest_AppMgr_002 end");
99 }
100 
101 /*
102  * Feature: AMS
103  * Function: IPC
104  * SubFunction: appmgr interface
105  * FunctionPoints: interface
106  * CaseDescription: test interface of ApplicationBackgrounded
107  */
108 HWTEST_F(AmsIpcAppMgrInterfaceTest, Interface_003, TestSize.Level1)
109 {
110     HILOG_DEBUG("AppMgrIpcInterfaceTest_AppMgr_003 start");
111     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
112     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
113 
114     EXPECT_CALL(*mockAppMgr, ApplicationBackgrounded(_))
115         .WillOnce(InvokeWithoutArgs(mockAppMgr.GetRefPtr(), &MockAppMgrService::Post));
116     auto recordId = AppRecordId::Create();
117     appMgrClient->ApplicationBackgrounded(recordId);
118     mockAppMgr->Wait();
119     HILOG_DEBUG("AppMgrIpcInterfaceTest_AppMgr_003 end");
120 }
121 
122 /*
123  * Feature: AMS
124  * Function: IPC
125  * SubFunction: appmgr interface
126  * FunctionPoints: interface
127  * CaseDescription: test interface of ApplicationTerminated
128  */
129 HWTEST_F(AmsIpcAppMgrInterfaceTest, Interface_004, TestSize.Level1)
130 {
131     HILOG_DEBUG("AppMgrIpcInterfaceTest_AppMgr_004 start");
132     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
133     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
134 
135     EXPECT_CALL(*mockAppMgr, ApplicationTerminated(_))
136         .WillOnce(InvokeWithoutArgs(mockAppMgr.GetRefPtr(), &MockAppMgrService::Post));
137     auto recordId = AppRecordId::Create();
138     appMgrClient->ApplicationTerminated(recordId);
139     mockAppMgr->Wait();
140     HILOG_DEBUG("AppMgrIpcInterfaceTest_AppMgr_004 end");
141 }
142 
143 /*
144  * Feature: AMS
145  * Function: IPC
146  * SubFunction: appmgr interface
147  * FunctionPoints: interface
148  * CaseDescription: test interface of CheckPermission
149  */
150 HWTEST_F(AmsIpcAppMgrInterfaceTest, Interface_005, TestSize.Level1)
151 {
152     HILOG_DEBUG("AppMgrIpcInterfaceTest_AppMgr_005 start");
153     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
154     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
155 
156     EXPECT_CALL(*mockAppMgr, CheckPermission(_, _)).Times(1).WillOnce(Return(OHOS::NO_ERROR));
157     auto recordId = AppRecordId::Create();
158     int ret = appMgrClient->CheckPermission(recordId, "write");
159     EXPECT_EQ(OHOS::NO_ERROR, ret);
160 
161     EXPECT_CALL(*mockAppMgr, CheckPermission(_, _)).Times(1).WillOnce(Return(OHOS::NO_ERROR));
162     ret = appMgrClient->CheckPermission(recordId, "read");
163     EXPECT_EQ(OHOS::NO_ERROR, ret);
164 
165     EXPECT_CALL(*mockAppMgr, CheckPermission(_, _)).Times(1).WillOnce(Return(OHOS::ERR_INVALID_STATE));
166     ret = appMgrClient->CheckPermission(recordId, "location");
167     EXPECT_EQ(OHOS::ERR_INVALID_STATE, ret);
168 
169     HILOG_DEBUG("AppMgrIpcInterfaceTest_AppMgr_005 end");
170 }
171 
172 /*
173  * Feature: AMS
174  * Function: IPC
175  * SubFunction: appmgr interface
176  * FunctionPoints: interface
177  * CaseDescription: test IPC can transact data
178  */
179 HWTEST_F(AmsIpcAppMgrInterfaceTest, Interface_006, TestSize.Level1)
180 {
181     HILOG_DEBUG("AppMgrIpcInterfaceTest_AppMgr_006 start");
182     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
183     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
184 
185     EXPECT_CALL(*mockAppMgr, CheckPermission(_, _))
186         .Times(1)
187         .WillOnce(Invoke(mockAppMgr.GetRefPtr(), &MockAppMgrService::CheckPermissionImpl));
188     auto recordId = AppRecordId::Create();
189     int ret = appMgrClient->CheckPermission(recordId, "write");
190     EXPECT_EQ(0, ret);
191     EXPECT_EQ("write", mockAppMgr->GetData());
192     HILOG_DEBUG("AppMgrIpcInterfaceTest_AppMgr_006 end");
193 }
194 
195 /*
196  * Feature: AMS
197  * Function: IPC
198  * SubFunction: appmgr interface
199  * FunctionPoints: KillApplication interface
200  * CaseDescription: test IPC can transact data
201  */
202 HWTEST_F(AmsIpcAppMgrInterfaceTest, ClearUpApplicationData_008, TestSize.Level1)
203 {
204     HILOG_DEBUG("ClearUpApplicationData_008 start");
205 
206     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
207     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
208 
209     EXPECT_CALL(*mockAppMgr, ClearUpApplicationData(_)).Times(1);
210 
211     appMgrClient->ClearUpApplicationData("PROCESS");
212 
213     HILOG_DEBUG("ClearUpApplicationData_008 end");
214 }
215 
216 /*
217  * Feature: AMS
218  * Function: IPC
219  * SubFunction: appmgr interface
220  * FunctionPoints: KillApplication interface
221  * CaseDescription: test IPC can transact data
222  */
223 HWTEST_F(AmsIpcAppMgrInterfaceTest, GetAllRunningProcesses_010, TestSize.Level1)
224 {
225     HILOG_DEBUG("GetAllRunningProcesses_009 start");
226 
227     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
228     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
229 
230     EXPECT_CALL(*mockAppMgr, GetAllRunningProcesses(_)).Times(1).WillOnce(Return(OHOS::ERR_NULL_OBJECT));
231 
232     std::vector<RunningProcessInfo> runningProcessInfo;
233     int32_t ret = appMgrClient->GetAllRunningProcesses(runningProcessInfo);
234     EXPECT_EQ(ret, OHOS::ERR_NULL_OBJECT);
235 
236     EXPECT_CALL(*mockAppMgr, GetAllRunningProcesses(_)).Times(1).WillOnce(Return(OHOS::ERR_NONE));
237     ret = appMgrClient->GetAllRunningProcesses(runningProcessInfo);
238     EXPECT_EQ(ret, OHOS::ERR_NONE);
239 
240     HILOG_DEBUG("GetAllRunningProcesses_009 end");
241 }
242 
243 /*
244  * @tc.name: RegisterApplicationStateObserver_001
245  * @tc.desc: Register application state observer test.
246  * @tc.type: FUNC
247  * @tc.require: issueI5822Q
248  */
249 HWTEST_F(AmsIpcAppMgrInterfaceTest, RegisterApplicationStateObserver_001, TestSize.Level0)
250 {
251     HILOG_DEBUG("RegisterApplicationStateObserver_001 start");
252 
253     sptr<IApplicationStateObserver> observer = new ApplicationStateObserverStub();
254     std::vector<std::string> bundleNameList;
255     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
256     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
257 
258     EXPECT_CALL(*mockAppMgr, RegisterApplicationStateObserver(_, _)).Times(1).WillOnce(Return(OHOS::NO_ERROR));
259 
260     int32_t err = appMgrClient->RegisterApplicationStateObserver(observer, bundleNameList);
261 
262     EXPECT_EQ(OHOS::NO_ERROR, err);
263 
264     HILOG_DEBUG("RegisterApplicationStateObserver_001 end");
265 }
266 
267 /*
268  * Feature: AMS
269  * Function: IPC
270  * SubFunction: appmgr interface
271  * FunctionPoints: KillApplication interface
272  * CaseDescription: test IPC can transact data
273  */
274 HWTEST_F(AmsIpcAppMgrInterfaceTest, UnregisterApplicationStateObserver_001, TestSize.Level0)
275 {
276     HILOG_DEBUG("UnregisterApplicationStateObserver_001 start");
277 
278     sptr<IApplicationStateObserver> observer = new ApplicationStateObserverStub();
279     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
280     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
281 
282     EXPECT_CALL(*mockAppMgr, UnregisterApplicationStateObserver(_)).Times(1).WillOnce(Return(OHOS::NO_ERROR));
283 
284     int32_t err = appMgrClient->UnregisterApplicationStateObserver(observer);
285 
286     EXPECT_EQ(OHOS::NO_ERROR, err);
287 
288     HILOG_DEBUG("UnregisterApplicationStateObserver_001 end");
289 }
290 }  // namespace AppExecFwk
291 }  // namespace OHOS
292