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