1 /*
2 * Copyright (c) 2022-2023 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 #include <numeric>
18 #define private public
19 #include "bundle_manager_helper.h"
20 #undef private
21 #include "mock_common_event_stub.h"
22
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::EventFwk;
26 using namespace OHOS::AppExecFwk;
27
28 class BundleManagerHelperTest : public testing::Test {
29 public:
BundleManagerHelperTest()30 BundleManagerHelperTest()
31 {}
~BundleManagerHelperTest()32 ~BundleManagerHelperTest()
33 {}
34
35 static void SetUpTestCase(void);
36 static void TearDownTestCase(void);
37 void SetUp();
38 void TearDown();
39 };
40
SetUpTestCase(void)41 void BundleManagerHelperTest::SetUpTestCase(void)
42 {}
43
TearDownTestCase(void)44 void BundleManagerHelperTest::TearDownTestCase(void)
45 {}
46
SetUp(void)47 void BundleManagerHelperTest::SetUp(void)
48 {}
49
TearDown(void)50 void BundleManagerHelperTest::TearDown(void)
51 {}
52
53 using IBundleMgr = OHOS::AppExecFwk::IBundleMgr;
54
55 class TestIBundleMgr : public IBundleMgr {
56 public:
57 TestIBundleMgr() = default;
~TestIBundleMgr()58 virtual ~TestIBundleMgr()
59 {};
AsObject()60 sptr<IRemoteObject> AsObject() override
61 {
62 return nullptr;
63 }
64 };
65
66 /**
67 * @tc.name: BundleManagerHelper_0200
68 * @tc.desc: test ClearBundleManagerHelper function and sptrBundleMgr_ is nullptr.
69 * @tc.type: FUNC
70 */
HWTEST_F(BundleManagerHelperTest,BundleManagerHelper_0200,Level1)71 HWTEST_F(BundleManagerHelperTest, BundleManagerHelper_0200, Level1)
72 {
73 GTEST_LOG_(INFO) << "BundleManagerHelper_0200 start";
74 std::shared_ptr<BundleManagerHelper> bundleManagerHelper = std::make_shared<BundleManagerHelper>();
75 ASSERT_NE(nullptr, bundleManagerHelper);
76 bundleManagerHelper->ClearBundleManagerHelper();
77 GTEST_LOG_(INFO) << "BundleManagerHelper_0200 end";
78 }
79
80 /**
81 * @tc.name: BundleManagerHelper_0300
82 * @tc.desc: test GetBundleMgrProxy function and sptrBundleMgr_ is not nullptr.
83 * @tc.type: FUNC
84 */
HWTEST_F(BundleManagerHelperTest,BundleManagerHelper_0300,Level1)85 HWTEST_F(BundleManagerHelperTest, BundleManagerHelper_0300, Level1)
86 {
87 GTEST_LOG_(INFO) << "BundleManagerHelper_0300 start";
88 BundleManagerHelper bundleManagerHelper;
89 bundleManagerHelper.sptrBundleMgr_ = new (std::nothrow) TestIBundleMgr();
90 EXPECT_EQ(true, bundleManagerHelper.GetBundleMgrProxy());
91 GTEST_LOG_(INFO) << "BundleManagerHelper_0300 end";
92 }
93
94 /**
95 * @tc.name: BundleManagerHelper_0400
96 * @tc.desc: test GetBundleMgrProxy function and sptrBundleMgr_ is nullptr.
97 * @tc.type: FUNC
98 */
HWTEST_F(BundleManagerHelperTest,BundleManagerHelper_0400,Level1)99 HWTEST_F(BundleManagerHelperTest, BundleManagerHelper_0400, Level1)
100 {
101 GTEST_LOG_(INFO) << "BundleManagerHelper_0400 start";
102 BundleManagerHelper bundleManagerHelper;
103 bundleManagerHelper.sptrBundleMgr_ = nullptr;
104 EXPECT_EQ(false, bundleManagerHelper.GetBundleMgrProxy());
105 GTEST_LOG_(INFO) << "BundleManagerHelper_0400 end";
106 }
107
108 /**
109 * @tc.name: BundleManagerHelper_0500
110 * @tc.desc: test GetBundleName function and GetBundleMgrProxy is false.
111 * @tc.type: FUNC
112 */
HWTEST_F(BundleManagerHelperTest,BundleManagerHelper_0500,Level1)113 HWTEST_F(BundleManagerHelperTest, BundleManagerHelper_0500, Level1)
114 {
115 GTEST_LOG_(INFO) << "BundleManagerHelper_0500 start";
116 BundleManagerHelper bundleManagerHelper;
117 bundleManagerHelper.sptrBundleMgr_ = nullptr;
118 const uid_t uid = 1;
119 EXPECT_EQ("", bundleManagerHelper.GetBundleName(uid));
120 GTEST_LOG_(INFO) << "BundleManagerHelper_0500 end";
121 }
122
123 /**
124 * @tc.name: BundleManagerHelper_0600
125 * @tc.desc: test QueryExtensionInfos function and GetBundleMgrProxy is false.
126 * @tc.type: FUNC
127 */
HWTEST_F(BundleManagerHelperTest,BundleManagerHelper_0600,Level1)128 HWTEST_F(BundleManagerHelperTest, BundleManagerHelper_0600, Level1)
129 {
130 GTEST_LOG_(INFO) << "BundleManagerHelper_0600 start";
131 BundleManagerHelper bundleManagerHelper;
132 bundleManagerHelper.sptrBundleMgr_ = nullptr;
133 std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos;
134 const int32_t userId = 1;
135 EXPECT_EQ(false, bundleManagerHelper.QueryExtensionInfos(extensionInfos, userId));
136 GTEST_LOG_(INFO) << "BundleManagerHelper_0600 end";
137 }
138
139 /**
140 * @tc.name: BundleManagerHelper_0700
141 * @tc.desc: test QueryExtensionInfos function and GetBundleMgrProxy is false.
142 * @tc.type: FUNC
143 */
HWTEST_F(BundleManagerHelperTest,BundleManagerHelper_0700,Level1)144 HWTEST_F(BundleManagerHelperTest, BundleManagerHelper_0700, Level1)
145 {
146 GTEST_LOG_(INFO) << "BundleManagerHelper_0700 start";
147 BundleManagerHelper bundleManagerHelper;
148 bundleManagerHelper.sptrBundleMgr_ = nullptr;
149 std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos;
150 EXPECT_EQ(false, bundleManagerHelper.QueryExtensionInfos(extensionInfos));
151 GTEST_LOG_(INFO) << "BundleManagerHelper_0700 end";
152 }
153
154 /**
155 * @tc.name: BundleManagerHelper_0800
156 * @tc.desc: test QueryExtensionInfos function and GetBundleMgrProxy is true and osAccountIds.size() is 0.
157 * @tc.type: FUNC
158 */
HWTEST_F(BundleManagerHelperTest,BundleManagerHelper_0800,Level1)159 HWTEST_F(BundleManagerHelperTest, BundleManagerHelper_0800, Level1)
160 {
161 GTEST_LOG_(INFO) << "BundleManagerHelper_0800 start";
162 BundleManagerHelper bundleManagerHelper;
163 sptr<IRemoteObject> remoteObject = sptr<IRemoteObject>(new MockCommonEventStub());
164 bundleManagerHelper.sptrBundleMgr_ = iface_cast<IBundleMgr>(remoteObject);
165 std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos;
166 EXPECT_EQ(false, bundleManagerHelper.QueryExtensionInfos(extensionInfos));
167 GTEST_LOG_(INFO) << "BundleManagerHelper_0800 end";
168 }
169
170 /**
171 * @tc.name: BundleManagerHelper_0900
172 * @tc.desc: test GetResConfigFile function and GetBundleMgrProxy is false.
173 * @tc.type: FUNC
174 */
HWTEST_F(BundleManagerHelperTest,BundleManagerHelper_0900,Level1)175 HWTEST_F(BundleManagerHelperTest, BundleManagerHelper_0900, Level1)
176 {
177 GTEST_LOG_(INFO) << "BundleManagerHelper_0900 start";
178 BundleManagerHelper bundleManagerHelper;
179 bundleManagerHelper.sptrBundleMgr_ = nullptr;
180 AppExecFwk::ExtensionAbilityInfo extension;
181 std::vector<std::string> profileInfos;
182 EXPECT_EQ(false, bundleManagerHelper.GetResConfigFile(extension, profileInfos));
183 GTEST_LOG_(INFO) << "BundleManagerHelper_0900 end";
184 }
185
186 /**
187 * @tc.name: BundleManagerHelper_1000
188 * @tc.desc: test CheckIsSystemAppByUid function and GetBundleMgrProxy is false.
189 * @tc.type: FUNC
190 */
HWTEST_F(BundleManagerHelperTest,BundleManagerHelper_1000,Level1)191 HWTEST_F(BundleManagerHelperTest, BundleManagerHelper_1000, Level1)
192 {
193 GTEST_LOG_(INFO) << "BundleManagerHelper_1000 start";
194 BundleManagerHelper bundleManagerHelper;
195 bundleManagerHelper.sptrBundleMgr_ = nullptr;
196 const uid_t uid = 1;
197 EXPECT_EQ(false, bundleManagerHelper.CheckIsSystemAppByUid(uid));
198 GTEST_LOG_(INFO) << "BundleManagerHelper_1000 end";
199 }
200
201 /**
202 * @tc.name: BundleManagerHelper_1100
203 * @tc.desc: test CheckIsSystemAppByBundleName function and GetBundleMgrProxy is false.
204 * @tc.type: FUNC
205 */
HWTEST_F(BundleManagerHelperTest,BundleManagerHelper_1100,Level1)206 HWTEST_F(BundleManagerHelperTest, BundleManagerHelper_1100, Level1)
207 {
208 GTEST_LOG_(INFO) << "BundleManagerHelper_1100 start";
209 BundleManagerHelper bundleManagerHelper;
210 bundleManagerHelper.sptrBundleMgr_ = nullptr;
211 std::string bundleName = "aa";
212 const int32_t userId = 1;
213 EXPECT_EQ(false, bundleManagerHelper.CheckIsSystemAppByBundleName(bundleName, userId));
214 GTEST_LOG_(INFO) << "BundleManagerHelper_1100 end";
215 }
216
217 /**
218 * @tc.name: BundleManagerHelper_1200
219 * @tc.desc: test ClearBundleManagerHelper function and sptrBundleMgr_ is not nullptr.
220 * @tc.type: FUNC
221 */
HWTEST_F(BundleManagerHelperTest,BundleManagerHelper_1200,Level1)222 HWTEST_F(BundleManagerHelperTest, BundleManagerHelper_1200, Level1)
223 {
224 GTEST_LOG_(INFO) << "BundleManagerHelper_1200 start";
225 std::shared_ptr<BundleManagerHelper> bundleManagerHelper = std::make_shared<BundleManagerHelper>();
226 ASSERT_NE(nullptr, bundleManagerHelper);
227 sptr<IRemoteObject> remoteObject = sptr<IRemoteObject>(new MockCommonEventStub());
228 bundleManagerHelper->sptrBundleMgr_ = iface_cast<IBundleMgr>(remoteObject);
229 bundleManagerHelper->ClearBundleManagerHelper();
230 GTEST_LOG_(INFO) << "BundleManagerHelper_1200 end";
231 }