1 /*
2 * Copyright (c) 2022-2024 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 #define protected public
20 #include "ability_record.h"
21 #include "app_mgr_client.h"
22 #include "app_mgr_constants.h"
23 #include "app_scheduler.h"
24 #include "hilog_tag_wrapper.h"
25 #include "mock_ability_debug_response_stub.h"
26 #include "mock_app_debug_listener_stub.h"
27 #include "mock_native_token.h"
28 #include "mock_sa_call.h"
29 #undef protected
30 #undef private
31
32 using namespace testing;
33 using namespace testing::ext;
34
35 namespace OHOS {
36 namespace AppExecFwk {
37 namespace {
38 const int32_t APP_NUMBER_ZERO = 0;
39 const int32_t ERROR_PID = 999999;
40 const int32_t ERROR_USER_ID = -1;
41 const int32_t ERROR_STATE = -1;
42 const std::string EMPTY_STRING = "";
43 const int32_t INIT_VALUE = 0;
44 const int32_t ERROR_RET = 3;
45 } // namespace
46
47 class AppMgrClientFirstTest : public testing::Test {
48 public:
49 static void SetUpTestCase();
50 static void TearDownTestCase();
51 void SetUp() override;
52 void TearDown() override;
53 AbilityRequest GenerateAbilityRequest(const std::string& deviceName,
54 const std::string& abilityName,
55 const std::string& appName,
56 const std::string& bundleName);
57 };
58
SetUpTestCase(void)59 void AppMgrClientFirstTest::SetUpTestCase(void)
60 {
61 MockNativeToken::SetNativeToken();
62 }
63
TearDownTestCase(void)64 void AppMgrClientFirstTest::TearDownTestCase(void) {}
65
SetUp()66 void AppMgrClientFirstTest::SetUp() {}
67
TearDown()68 void AppMgrClientFirstTest::TearDown() {}
69
GenerateAbilityRequest(const std::string & deviceName,const std::string & abilityName,const std::string & appName,const std::string & bundleName)70 AbilityRequest AppMgrClientFirstTest::GenerateAbilityRequest(
71 const std::string& deviceName, const std::string& abilityName,
72 const std::string& appName, const std::string& bundleName)
73 {
74 ElementName element(deviceName, abilityName, bundleName);
75 AAFwk::Want want;
76 want.SetElement(element);
77 AbilityInfo abilityInfo;
78 abilityInfo.applicationName = appName;
79 ApplicationInfo appinfo;
80 appinfo.name = appName;
81 AbilityRequest abilityRequest;
82 abilityRequest.want = want;
83 abilityRequest.abilityInfo = abilityInfo;
84 abilityRequest.appInfo = appinfo;
85 return abilityRequest;
86 }
87
GetTestAbilityToken()88 sptr<Token> GetTestAbilityToken()
89 {
90 sptr<Token> token = nullptr;
91 AbilityRequest abilityRequest;
92 abilityRequest.appInfo.bundleName = "com.example.utTest";
93 abilityRequest.abilityInfo.name = "MainAbility";
94 abilityRequest.abilityInfo.type = AbilityType::DATA;
95 std::shared_ptr<AbilityRecord> abilityRecord =
96 AbilityRecord::CreateAbilityRecord(abilityRequest);
97 if (abilityRecord) {
98 token = abilityRecord->GetToken();
99 }
100 return token;
101 }
102
103 /**
104 * @tc.name: ForceKillApplication_001
105 * @tc.desc: AppMgrClient test for ForceKillApplication.
106 * @tc.type: FUNC
107 */
108 HWTEST_F(AppMgrClientFirstTest, ForceKillApplication_001, TestSize.Level0)
109 {
110 TAG_LOGI(AAFwkTag::TEST, "ForceKillApplication_001 start");
111 auto appMgrClient = std::make_unique<AppMgrClient>();
112 EXPECT_NE(appMgrClient, nullptr);
113 std::string bundleName = "bundleName";
114 int userId = ERROR_USER_ID;
115 int appIndex = 0;
116 auto result =
117 appMgrClient->ForceKillApplication(bundleName, userId, appIndex);
118 EXPECT_EQ(result, AppMgrResultCode::ERROR_SERVICE_NOT_READY);
119 TAG_LOGI(AAFwkTag::TEST, "ForceKillApplication_001 end");
120 }
121
122 /**
123 * @tc.name: NotifyProcMemoryLevel_001
124 * @tc.desc: AppMgrClient test for NotifyProcMemoryLevel.
125 * @tc.type: FUNC
126 */
127 HWTEST_F(AppMgrClientFirstTest, NotifyProcMemoryLevel_001, TestSize.Level1)
128 {
129 TAG_LOGI(AAFwkTag::TEST, "NotifyProcMemoryLevel_001 start");
130 auto appMgrClient = std::make_unique<AppMgrClient>();
131 EXPECT_NE(appMgrClient, nullptr);
132 std::map<pid_t, MemoryLevel> procLevelMap = {};
133 auto result = appMgrClient->NotifyProcMemoryLevel(procLevelMap);
134 EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
135 TAG_LOGI(AAFwkTag::TEST, "NotifyProcMemoryLevel_001 end");
136 }
137
138 /**
139 * @tc.name: IsKilledForUpgradeWeb_001
140 * @tc.desc: AppMgrClient test for IsKilledForUpgradeWeb.
141 * @tc.type: FUNC
142 */
143 HWTEST_F(AppMgrClientFirstTest, IsKilledForUpgradeWeb_001, TestSize.Level1)
144 {
145 TAG_LOGI(AAFwkTag::TEST, "IsKilledForUpgradeWeb_001 start");
146 auto appMgrClient = std::make_unique<AppMgrClient>();
147 std::string bundleName = "bundleName";
148 auto result = appMgrClient->IsKilledForUpgradeWeb(bundleName);
149 EXPECT_EQ(result, false);
150 TAG_LOGI(AAFwkTag::TEST, "IsKilledForUpgradeWeb_001 end");
151 }
152
153 /**
154 * @tc.name: IsProcessContainsOnlyUIAbility_001
155 * @tc.desc: AppMgrClient test for IsProcessContainsOnlyUIAbility.
156 * @tc.type: FUNC
157 */
158 HWTEST_F(AppMgrClientFirstTest, IsProcessContainsOnlyUIAbility_001, TestSize.Level1)
159 {
160 TAG_LOGI(AAFwkTag::TEST, "IsProcessContainsOnlyUIAbility_001 start");
161 auto appMgrClient = std::make_unique<AppMgrClient>();
162 pid_t pid = 1;
163 auto result = appMgrClient->IsProcessContainsOnlyUIAbility(pid);
164 EXPECT_EQ(result, false);
165 TAG_LOGI(AAFwkTag::TEST, "IsProcessContainsOnlyUIAbility_001 end");
166 }
167
168 /**
169 * @tc.name: KillProcessesByAccessTokenId_001
170 * @tc.desc: AppMgrClient test for KillProcessesByAccessTokenId.
171 * @tc.type: FUNC
172 */
173 HWTEST_F(AppMgrClientFirstTest, KillProcessesByAccessTokenId_001, TestSize.Level1)
174 {
175 TAG_LOGI(AAFwkTag::TEST, "KillProcessesByAccessTokenId_001 start");
176 auto appMgrClient = std::make_unique<AppMgrClient>();
177 uint32_t accessTokenId = 1;
178 auto result = appMgrClient->KillProcessesByAccessTokenId(accessTokenId);
179 EXPECT_EQ(result, AppMgrResultCode::ERROR_SERVICE_NOT_READY);
180 TAG_LOGI(AAFwkTag::TEST, "KillProcessesByAccessTokenId_001 end");
181 }
182
183 /**
184 * @tc.name: CleanAbilityByUserRequest_001
185 * @tc.desc: AppMgrClient test for CleanAbilityByUserRequest.
186 * @tc.type: FUNC
187 */
188 HWTEST_F(AppMgrClientFirstTest, CleanAbilityByUserRequest_001, TestSize.Level1)
189 {
190 TAG_LOGI(AAFwkTag::TEST, "CleanAbilityByUserRequest_001 start");
191 auto appMgrClient = std::make_unique<AppMgrClient>();
192 sptr<IRemoteObject> token = GetTestAbilityToken();
193 auto result = appMgrClient->CleanAbilityByUserRequest(token);
194 EXPECT_TRUE(result != true);
195 TAG_LOGI(AAFwkTag::TEST, "CleanAbilityByUserRequest_001 end");
196 }
197
198 /**
199 * @tc.name: IsCallerKilling_001
200 * @tc.desc: AppMgrClient test for IsCallerKilling.
201 * @tc.type: FUNC
202 */
203 HWTEST_F(AppMgrClientFirstTest, IsCallerKilling_001, TestSize.Level1)
204 {
205 TAG_LOGI(AAFwkTag::TEST, "IsCallerKilling_001 start");
206 auto appMgrClient = std::make_unique<AppMgrClient>();
207 auto result = appMgrClient->IsCallerKilling("");
208 EXPECT_TRUE(result != true);
209 TAG_LOGI(AAFwkTag::TEST, "IsCallerKilling_001 end");
210 }
211 } // namespace AppExecFwk
212 } // namespace OHOS
213