• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define private public
19 #include "ams_mgr_scheduler.h"
20 #undef private
21 #include "app_mgr_interface.h"
22 #include "hilog_wrapper.h"
23 #include "mock_bundle_manager.h"
24 #include "mock_native_token.h"
25 #include "system_ability_definition.h"
26 #include "sys_mgr_client.h"
27 
28 using namespace testing::ext;
29 using namespace OHOS;
30 using namespace OHOS::AppExecFwk;
31 
32 namespace {
33 const std::string STRING_BUNDLE_NAME = "com.example.bundle";
34 
35 constexpr int ACCOUNT_ID = 100;
36 }  // namespace
37 
38 class AmsMgrKillProcessTest : public testing::Test {
39 public:
40     static void SetUpTestCase(void);
41     static void TearDownTestCase(void);
42     void SetUp(void) override;
43     void TearDown(void) override;
44 
45     std::shared_ptr<AppMgrServiceInner> GetAppMgrServiceInner(void);
46     sptr<IAppMgr> GetAppMgrProxy(void);
47 };
48 
SetUpTestCase(void)49 void AmsMgrKillProcessTest::SetUpTestCase(void)
50 {
51     MockNativeToken::SetNativeToken();
52 }
53 
TearDownTestCase(void)54 void AmsMgrKillProcessTest::TearDownTestCase(void)
55 {}
56 
SetUp(void)57 void AmsMgrKillProcessTest::SetUp(void)
58 {}
59 
TearDown(void)60 void AmsMgrKillProcessTest::TearDown(void)
61 {}
62 
GetAppMgrServiceInner(void)63 std::shared_ptr<AppMgrServiceInner> AmsMgrKillProcessTest::GetAppMgrServiceInner(void)
64 {
65     auto appMgrServiceInner = std::make_shared<AppMgrServiceInner>();
66     EXPECT_NE(appMgrServiceInner, nullptr);
67 
68     sptr<IBundleMgr> bundleMgr = new BundleMgrService();
69     appMgrServiceInner->remoteClientManager_->SetBundleManager(bundleMgr);
70 
71     return appMgrServiceInner;
72 }
73 
GetAppMgrProxy(void)74 sptr<IAppMgr> AmsMgrKillProcessTest::GetAppMgrProxy(void)
75 {
76     auto instance = DelayedSingleton<SysMrgClient>::GetInstance();
77     EXPECT_NE(instance, nullptr);
78 
79     auto object = instance->GetSystemAbility(APP_MGR_SERVICE_ID);
80     EXPECT_NE(object, nullptr);
81 
82     auto proxy = iface_cast<IAppMgr>(object);
83     EXPECT_NE(proxy, nullptr);
84 
85     return proxy;
86 }
87 
88 /*
89  * Feature: AppMgrServiceInner
90  * Function: KillApplicationByUserId
91  * SubFunction: NA
92  * FunctionPoints:Kill process
93  * EnvConditions: NA
94  * CaseDescription: creat AppMgrServiceInner object, call function.
95  */
96 HWTEST_F(AmsMgrKillProcessTest, KillProcess_0100, TestSize.Level0)
97 {
98     HILOG_INFO("AmsMgrKillProcessTest_KillProcess_0100");
99 
100     auto appMgrServiceInner = GetAppMgrServiceInner();
101     EXPECT_NE(appMgrServiceInner, nullptr);
102 
103     ErrCode result = appMgrServiceInner->KillApplicationByUserId(STRING_BUNDLE_NAME, ACCOUNT_ID);
104     EXPECT_EQ(result, ERR_OK);
105 }
106 
107 /*
108  * Feature: AppMgrProxy
109  * Function: KillApplicationByUserId
110  * SubFunction: NA
111  * FunctionPoints:Kill process
112  * EnvConditions: NA
113  * CaseDescription: creat AppMgrProxy object, call function.
114  */
115 HWTEST_F(AmsMgrKillProcessTest, KillProcess_0200, TestSize.Level0)
116 {
117     HILOG_INFO("AmsMgrKillProcessTest_KillProcess_0200");
118 
119     auto proxy = GetAppMgrProxy();
120     EXPECT_NE(proxy, nullptr);
121 
122     ErrCode result = proxy->GetAmsMgr()->KillProcessWithAccount(STRING_BUNDLE_NAME, ACCOUNT_ID);
123     EXPECT_EQ(result, ERR_OK);
124 }
125