1 /*
2  * Copyright (c) 2021 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_manager.h"
17 #include <gtest/gtest.h>
18 #include "ability_info.h"
19 #include "application_info.h"
20 #include "mock_overlay_manager.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 const long int UNEXPIRED_TIME = 1860000000;
GetApplicationInfo(const std::string & appName,const ApplicationFlag flag,const int userId,ApplicationInfo & appInfo)25 bool BundleMgrProxy::GetApplicationInfo(
26     const std::string& appName, const ApplicationFlag flag, const int userId, ApplicationInfo& appInfo)
27 {
28     if (appName.empty()) {
29         return false;
30     }
31     appInfo.name = "Helloworld";
32     appInfo.bundleName = "com.ohos.hiworld";
33     return true;
34 }
35 
GetAppType(const std::string & bundleName)36 std::string BundleMgrProxy::GetAppType(const std::string& bundleName)
37 {
38     GTEST_LOG_(INFO) << " BundleMgrProxy::GetAppTyp";
39     return "system";
40 }
41 
QueryAbilityInfo(const Want & want,AbilityInfo & abilityInfo)42 bool BundleMgrProxy::QueryAbilityInfo(const Want& want, AbilityInfo& abilityInfo)
43 {
44     ElementName eleName = want.GetElement();
45     if (eleName.GetBundleName().empty()) {
46         return false;
47     }
48     abilityInfo.visible = true;
49     abilityInfo.name = eleName.GetAbilityName();
50     abilityInfo.bundleName = eleName.GetBundleName();
51     abilityInfo.applicationName = "Helloworld";
52     return true;
53 }
54 
GetHapModuleInfo(const AbilityInfo & abilityInfo,HapModuleInfo & hapModuleInfo)55 bool BundleMgrProxy::GetHapModuleInfo(const AbilityInfo& abilityInfo, HapModuleInfo& hapModuleInfo)
56 {
57     GTEST_LOG_(INFO) << " BundleMgrProxy::GetHapModuleInfo";
58     hapModuleInfo.name = abilityInfo.package;
59     return true;
60 }
61 
GetHapModuleInfo(const AbilityInfo & abilityInfo,int32_t userId,HapModuleInfo & hapModuleInfo)62 bool BundleMgrProxy::GetHapModuleInfo(const AbilityInfo& abilityInfo, int32_t userId, HapModuleInfo& hapModuleInfo)
63 {
64     GTEST_LOG_(INFO) << " BundleMgrProxy::GetHapModuleInfo";
65     hapModuleInfo.name = abilityInfo.package;
66     return true;
67 }
68 
GetOverlayManagerProxy()69 sptr<IOverlayManager> BundleMgrProxy::GetOverlayManagerProxy()
70 {
71     sptr<IOverlayManager> overlayModuleProxy = new (std::nothrow) OverlayManagerProxy(nullptr);
72     return overlayModuleProxy;
73 }
74 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)75 int BundleMgrStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
76 {
77     GTEST_LOG_(INFO) << " BundleMgrStub::OnRemoteRequest";
78     return 0;
79 }
80 
GetApplicationInfo(const std::string & appName,const ApplicationFlag flag,const int userId,ApplicationInfo & appInfo)81 bool BundleMgrService::GetApplicationInfo(
82     const std::string& appName, const ApplicationFlag flag, const int userId, ApplicationInfo& appInfo)
83 {
84     if (appName.empty()) {
85         return false;
86     }
87     appInfo.name = "Helloworld";
88     appInfo.bundleName = "com.foobar.hiworld";
89     if (appName == "com.crowdtest.expired") {
90         appInfo.appDistributionType = "crowdtesting";
91         appInfo.crowdtestDeadline = 0;
92     }
93     if (appName == "com.crowdtest.unexpired") {
94         appInfo.appDistributionType = "crowdtesting";
95         appInfo.crowdtestDeadline = UNEXPIRED_TIME;
96     }
97     return true;
98 }
99 
GetAppType(const std::string & bundleName)100 std::string BundleMgrService::GetAppType(const std::string& bundleName)
101 {
102     GTEST_LOG_(INFO) << " BundleMgrService::GetAppType";
103     return "system";
104 }
105 
QueryAbilityInfo(const Want & want,AbilityInfo & abilityInfo)106 bool BundleMgrService::QueryAbilityInfo(const Want& want, AbilityInfo& abilityInfo)
107 {
108     ElementName elementName = want.GetElement();
109     if (elementName.GetBundleName().empty()) {
110         return false;
111     }
112     if (std::string::npos != elementName.GetBundleName().find("service")) {
113         abilityInfo.type = AppExecFwk::AbilityType::SERVICE;
114     }
115     abilityInfo.visible = true;
116     abilityInfo.name = elementName.GetAbilityName();
117     abilityInfo.bundleName = elementName.GetBundleName();
118     abilityInfo.applicationName = elementName.GetBundleName();
119     if (want.HasEntity(Want::ENTITY_HOME) && want.GetAction() == Want::ACTION_HOME) {
120         abilityInfo.applicationInfo.isLauncherApp = true;
121     } else {
122         abilityInfo.applicationInfo.isLauncherApp = false;
123     }
124     return true;
125 }
126 
GetHapModuleInfo(const AbilityInfo & abilityInfo,HapModuleInfo & hapModuleInfo)127 bool BundleMgrService::GetHapModuleInfo(const AbilityInfo& abilityInfo, HapModuleInfo& hapModuleInfo)
128 {
129     GTEST_LOG_(INFO) << " BundleMgrService::GetHapModuleInfo";
130     hapModuleInfo.name = abilityInfo.package;
131     return true;
132 }
GetHapModuleInfo(const AbilityInfo & abilityInfo,int32_t userId,HapModuleInfo & hapModuleInfo)133 bool BundleMgrService::GetHapModuleInfo(const AbilityInfo& abilityInfo, int32_t userId, HapModuleInfo& hapModuleInfo)
134 {
135     GTEST_LOG_(INFO) << " BundleMgrService::GetHapModuleInfo";
136     hapModuleInfo.name = abilityInfo.package;
137     return true;
138 }
139 
GetOverlayManagerProxy()140 sptr<IOverlayManager> BundleMgrService::GetOverlayManagerProxy()
141 {
142     sptr<IOverlayManager> overlayManagerProxy = new (std::nothrow) OverlayManagerProxy(nullptr);
143     return overlayManagerProxy;
144 }
145 }  // namespace AppExecFwk
146 }  // namespace OHOS
147