• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "keep_alive_utils.h"
17 #include <gtest/gtest.h>
18 
19 #include "ability_resident_process_rdb.h"
20 #include "bundle_info.h"
21 #include "keep_alive_process_manager.h"
22 #include "main_element_utils.h"
23 
24 #include <vector>
25 #include <memory>
26 
27 using namespace testing;
28 using namespace testing::ext;
29 using namespace OHOS::AppExecFwk;
30 namespace OHOS {
31 namespace AbilityRuntime {
32 bool AmsResidentProcessRdb::residentProcessEnable = false;
33 }
34 namespace AAFwk {
35 bool MainElementUtils::retCheckMainElement = false;
36 bool MainElementUtils::isUpdateMainElementCalled = false;
37 bool KeepAliveProcessManager::isKeepAliveBundle = false;
38 
39 namespace {
40     const std::string PROCESS_NAME = "com.ohos.example.process";
41     const std::string MAIN_ABILITY = "com.ohos.example.mainability";
42     const std::string URI = "https://www.example.com:8080/path/to/resource?key1=value1&key2=value2#section1";
43     const std::string BUNDLE_NAME = "com.ohos.example.bundlename";
44     const std::string NAME = "com.ohos.example.name";
45     const std::string MAINELEMENTNAME = "com.ohos.example.mainelementname";
46 }
47 
48 class KeepAliveUtilsTest : public testing::Test {
49 public:
50     KeepAliveUtilsTest();
51     ~KeepAliveUtilsTest();
52     static void SetUpTestCase(void);
53     static void TearDownTestCase(void);
54     void SetUp();
55     void TearDown();
56 };
57 
KeepAliveUtilsTest()58 KeepAliveUtilsTest::KeepAliveUtilsTest()
59 {
60 }
61 
~KeepAliveUtilsTest()62 KeepAliveUtilsTest::~KeepAliveUtilsTest()
63 {
64 }
65 
SetUpTestCase()66 void KeepAliveUtilsTest::SetUpTestCase()
67 {
68     MainElementUtils::isUpdateMainElementCalled = false;
69 }
70 
TearDownTestCase()71 void KeepAliveUtilsTest::TearDownTestCase()
72 {
73     MainElementUtils::isUpdateMainElementCalled = false;
74 }
75 
SetUp()76 void KeepAliveUtilsTest::SetUp()
77 {
78 }
79 
TearDown()80 void KeepAliveUtilsTest::TearDown()
81 {
82 }
83 
84 /**
85  * @tc.name:  NotifyDisableKeepAliveProcesses_0100
86  * @tc.desc: basic function test.
87  * @tc.type: FUNC
88  */
89 HWTEST_F(KeepAliveUtilsTest, NotifyDisableKeepAliveProcesses_0100, TestSize.Level1)
90 {
91     GTEST_LOG_(INFO) << "NotifyDisableKeepAliveProcesses_0100 start";
92 
93     AppExecFwk::BundleInfo bundleInfo;
94     bundleInfo.applicationInfo.process = PROCESS_NAME;
95     AppExecFwk::HapModuleInfo hapModuleInfo;
96     hapModuleInfo.isModuleJson = false;
97     bundleInfo.hapModuleInfos.push_back(hapModuleInfo);
98 
99     std::vector<AppExecFwk::BundleInfo> bundleInfos;
100     bundleInfos.push_back(bundleInfo);
101     MainElementUtils::retCheckMainElement = false;
102     int32_t userId = 0;
103     std::string mainElement;
104     std::string uriStr;
105     bool isDataAbility = false;
106     KeepAliveUtils::NotifyDisableKeepAliveProcesses(bundleInfos, userId);
107     EXPECT_FALSE(MainElementUtils::CheckMainElement(hapModuleInfo, bundleInfo.applicationInfo.process,
108         mainElement, isDataAbility, uriStr, userId));
109     EXPECT_FALSE(MainElementUtils::isUpdateMainElementCalled);
110 
111     GTEST_LOG_(INFO) << "NotifyDisableKeepAliveProcesses_0100 end";
112 }
113 
114 /**
115  * @tc.name:  NotifyDisableKeepAliveProcesses_0200
116  * @tc.desc: basic function test.
117  * @tc.type: FUNC
118  */
119 HWTEST_F(KeepAliveUtilsTest, NotifyDisableKeepAliveProcesses_0200, TestSize.Level1)
120 {
121     GTEST_LOG_(INFO) << "NotifyDisableKeepAliveProcesses_0200 start";
122 
123     AppExecFwk::BundleInfo bundleInfo;
124     bundleInfo.applicationInfo.process = PROCESS_NAME;
125 
126     AppExecFwk::HapModuleInfo hapModuleInfo;
127     hapModuleInfo.mainAbility = MAIN_ABILITY;
128     hapModuleInfo.bundleName = BUNDLE_NAME;
129     hapModuleInfo.mainElementName = MAINELEMENTNAME;
130     hapModuleInfo.name = NAME;
131     hapModuleInfo.isModuleJson = true;
132     hapModuleInfo.process = PROCESS_NAME;
133 
134     AppExecFwk::AbilityInfo abilityInfo;
135     abilityInfo.process = PROCESS_NAME;
136     abilityInfo.name = MAIN_ABILITY;
137     abilityInfo.type = AppExecFwk::AbilityType::DATA;
138     abilityInfo.uri = URI;
139     hapModuleInfo.abilityInfos.push_back(abilityInfo);
140     bundleInfo.hapModuleInfos.push_back(hapModuleInfo);
141 
142     MainElementUtils::retCheckMainElement = true;
143     std::vector<AppExecFwk::BundleInfo> bundleInfos;
144     bundleInfos.push_back(bundleInfo);
145     int32_t userId = 0;
146     std::string mainElement;
147     std::string uriStr;
148     bool isDataAbility = false;
149 
150     KeepAliveUtils::NotifyDisableKeepAliveProcesses(bundleInfos, userId);
151     EXPECT_TRUE(MainElementUtils::CheckMainElement(hapModuleInfo, bundleInfo.applicationInfo.process,
152         mainElement, isDataAbility, uriStr, userId));
153     EXPECT_TRUE(MainElementUtils::isUpdateMainElementCalled);
154 
155     GTEST_LOG_(INFO) << "NotifyDisableKeepAliveProcesses_0200 end";
156 }
157 
158 /**
159  * @tc.name:  IsKeepAliveBundle_0100
160  * @tc.desc: basic function test.
161  * @tc.type: FUNC
162  */
163 HWTEST_F(KeepAliveUtilsTest, IsKeepAliveBundle_0100, TestSize.Level1)
164 {
165     GTEST_LOG_(INFO) << "IsKeepAliveBundle_0100 start";
166 
167     KeepAliveProcessManager::isKeepAliveBundle = true;
168     AppExecFwk::BundleInfo bundleInfo;
169     bundleInfo.name = BUNDLE_NAME;
170     bundleInfo.isKeepAlive = false;
171     int32_t userId = 0;
172     AAFwk::KeepAliveType type = AAFwk::KeepAliveType::UNSPECIFIED;
173 
174     auto result = KeepAliveUtils::IsKeepAliveBundle(bundleInfo, userId, type);
175     EXPECT_EQ(result, true);
176     EXPECT_EQ(type, KeepAliveType::THIRD_PARTY);
177 
178     GTEST_LOG_(INFO) << "IsKeepAliveBundle_0100 end";
179 }
180 
181 /**
182  * @tc.name:  IsKeepAliveBundle_0200
183  * @tc.desc: basic function test.
184  * @tc.type: FUNC
185  */
186 HWTEST_F(KeepAliveUtilsTest, IsKeepAliveBundle_0200, TestSize.Level1)
187 {
188     GTEST_LOG_(INFO) << "IsKeepAliveBundle_0200 start";
189 
190     KeepAliveProcessManager::isKeepAliveBundle = false;
191     AbilityRuntime::AmsResidentProcessRdb::residentProcessEnable = true;
192     AppExecFwk::BundleInfo bundleInfo;
193     bundleInfo.isKeepAlive = true;
194     int32_t userId = 0;
195     AAFwk::KeepAliveType type = AAFwk::KeepAliveType::UNSPECIFIED;
196     auto result = KeepAliveUtils::IsKeepAliveBundle(bundleInfo, userId, type);
197     EXPECT_EQ(result, true);
198     EXPECT_EQ(type, AAFwk::KeepAliveType::RESIDENT_PROCESS);
199 
200     GTEST_LOG_(INFO) << "IsKeepAliveBundle_0200 end";
201 }
202 
203 } // namespace AbilityRuntime
204 } // namespace OHOS