1 /*
2 * Copyright (c) 2023-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 "mock_bundle_mgr.h"
17
18 #include <functional>
19 #include <gtest/gtest.h>
20 #include "ans_ut_constant.h"
21
22 namespace OHOS {
23 namespace Notification {
24 namespace {
25 bool g_isNonBundleName = false;
26 bool g_isEnable = true;
27 bool g_setBundleInfoEnabled = false;
28 bool g_getBundleInfoFailed = false;
29 }
30
MockSetBundleInfoFailed(bool getFail)31 void MockSetBundleInfoFailed(bool getFail)
32 {
33 g_getBundleInfoFailed = getFail;
34 }
35
MockSetBundleInfoEnabled(bool enabled)36 void MockSetBundleInfoEnabled(bool enabled)
37 {
38 g_setBundleInfoEnabled = enabled;
39 }
40
MockIsNonBundleName(bool isNonBundleName)41 void MockIsNonBundleName(bool isNonBundleName)
42 {
43 g_isNonBundleName = isNonBundleName;
44 }
45
MockDistributedNotificationEnabled(bool isEnable)46 void MockDistributedNotificationEnabled(bool isEnable)
47 {
48 g_isEnable = isEnable;
49 }
50 }
51 }
52
53 namespace OHOS {
54 namespace AppExecFwk {
GetNameForUid(const int uid,std::string & name)55 ErrCode BundleMgrProxy::GetNameForUid(const int uid, std::string &name)
56 {
57 name = Notification::g_isNonBundleName ? "": "bundleName";
58 return ERR_OK;
59 }
60
GetBundleInfo(const std::string & bundleName,const BundleFlag flag,BundleInfo & bundleInfo,int32_t userId)61 bool BundleMgrProxy::GetBundleInfo(const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo,
62 int32_t userId)
63 {
64 if (Notification::g_getBundleInfoFailed) {
65 return false;
66 }
67 if (Notification::g_setBundleInfoEnabled) {
68 bundleInfo.applicationInfo.allowEnableNotification = true;
69 }
70 return true;
71 }
72
GetUidByBundleName(const std::string & bundleName,const int userId)73 int BundleMgrProxy::GetUidByBundleName(const std::string &bundleName, const int userId)
74 {
75 if (userId == 0) {
76 return -1;
77 } else {
78 return Notification::NON_SYSTEM_APP_UID;
79 }
80 }
81
GetApplicationInfo(const std::string & appName,const ApplicationFlag flag,const int userId,ApplicationInfo & appInfo)82 bool BundleMgrProxy::GetApplicationInfo(
83 const std::string &appName, const ApplicationFlag flag, const int userId, ApplicationInfo &appInfo)
84 {
85 appInfo.distributedNotificationEnabled = Notification::g_isEnable;
86 return true;
87 }
88
GetBundleInfos(const BundleFlag flags,std::vector<BundleInfo> & bundleInfos,int32_t userId)89 bool BundleMgrProxy::GetBundleInfos(const BundleFlag flags, std::vector<BundleInfo> &bundleInfos, int32_t userId)
90 {
91 if (Notification::g_setBundleInfoEnabled) {
92 int i = 1;
93 BundleInfo info;
94 info.applicationInfo.allowEnableNotification = true;
95 info.applicationInfo.bundleName = "test";
96 info.uid = i;
97 bundleInfos.push_back(info);
98 BundleInfo info1;
99 info1.applicationInfo.allowEnableNotification = false;
100 info1.applicationInfo.bundleName = "test1";
101 info1.uid = i+1;
102 bundleInfos.push_back(info);
103 return true;
104 }
105 return false;
106 }
107 } // namespace AppExecFwk
108 } // namespace OHOS
109