1 /* 2 * Copyright (c) 2021-2022 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 #ifndef OHOS_FORM_FWK_MOCK_BUNDLE_MANAGER_H 17 #define OHOS_FORM_FWK_MOCK_BUNDLE_MANAGER_H 18 19 #include <vector> 20 21 #include "ability_info.h" 22 #include "application_info.h" 23 #include "bundle_mgr_interface.h" 24 #include "gmock/gmock.h" 25 #include "iremote_proxy.h" 26 #include "iremote_stub.h" 27 #include "want.h" 28 29 namespace OHOS { 30 namespace AppExecFwk { 31 const int32_t APP_600 = 600; 32 33 class BundleMgrProxy : public IRemoteProxy<IBundleMgr> { 34 public: BundleMgrProxy(const sptr<IRemoteObject> & impl)35 explicit BundleMgrProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IBundleMgr>(impl) 36 {} ~BundleMgrProxy()37 virtual ~BundleMgrProxy() 38 {} 39 40 MOCK_METHOD5(QueryAbilityInfo, bool(const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo, 41 const sptr<IRemoteObject> &callBack)); QueryAbilityInfo(const AAFwk::Want & want,AbilityInfo & abilityInfo)42 bool QueryAbilityInfo(const AAFwk::Want &want, AbilityInfo &abilityInfo) override 43 { 44 return true; 45 } 46 GetAppType(const std::string & bundleName)47 std::string GetAppType(const std::string &bundleName) override 48 { 49 return "system"; 50 } 51 GetUidByBundleName(const std::string & bundleName,const int userId)52 virtual int GetUidByBundleName(const std::string &bundleName, const int userId) override 53 { 54 if (bundleName.compare("com.form.host.app600") == 0) { 55 return APP_600; 56 } 57 return 0; 58 } 59 GetBundleNameForUid(const int uid,std::string & bundleName)60 virtual bool GetBundleNameForUid(const int uid, std::string &bundleName) override 61 { 62 bundleName = "com.form.provider.service"; 63 return true; 64 } 65 66 virtual bool GetFormsInfoByApp(const std::string &bundleName, std::vector<FormInfo> &formInfo) override; 67 virtual bool GetFormsInfoByModule(const std::string &bundleName, const std::string &moduleName, 68 std::vector<FormInfo> &formInfo) override; 69 }; 70 71 class BundleMgrStub : public IRemoteStub<IBundleMgr> { 72 public: 73 virtual int OnRemoteRequest( 74 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; 75 }; 76 77 class BundleMgrService : public BundleMgrStub { 78 public: 79 static bool IsSystemApp; 80 81 bool QueryAbilityInfo(const AAFwk::Want &want, AbilityInfo &abilityInfo) override; 82 MOCK_METHOD5(QueryAbilityInfo, bool(const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo, 83 const sptr<IRemoteObject> &callBack)); 84 85 virtual int GetUidByBundleName(const std::string &bundleName, const int userId) override; 86 87 virtual bool GetBundleInfo( 88 const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, int32_t userId) override; 89 GetBundleNameForUid(const int uid,std::string & bundleName)90 virtual bool GetBundleNameForUid(const int uid, std::string &bundleName) override 91 { 92 bundleName = "com.form.provider.service"; 93 return true; 94 }; 95 96 virtual std::string GetAppType(const std::string &bundleName); 97 CheckIsSystemAppByUid(const int uid)98 virtual bool CheckIsSystemAppByUid(const int uid) override 99 { 100 return IsSystemApp; 101 } 102 103 virtual bool GetFormsInfoByApp(const std::string &bundleName, std::vector<FormInfo> &formInfo) override; 104 virtual bool GetFormsInfoByModule(const std::string &bundleName, const std::string &moduleName, 105 std::vector<FormInfo> &formInfo) override; 106 ImplicitQueryInfoByPriority(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,ExtensionAbilityInfo & extensionInfo)107 virtual bool ImplicitQueryInfoByPriority(const Want &want, int32_t flags, int32_t userId, 108 AbilityInfo &abilityInfo, ExtensionAbilityInfo &extensionInfo) override 109 { 110 abilityInfo.name = "MainAbility"; 111 abilityInfo.bundleName = "com.ohos.launcher"; 112 extensionInfo.name = "MainAbility"; 113 extensionInfo.bundleName = "com.ohos.launcher"; 114 return true; 115 } 116 117 MOCK_METHOD3(SetModuleRemovable, bool(const std::string &bundleName, const std::string &moduleName, bool isEnable)); 118 119 MOCK_METHOD2(GetNameForUid, int32_t(const int, std::string &)); 120 }; 121 122 class MockBundleMgrService : public BundleMgrService { 123 public: 124 MOCK_METHOD(bool, GetBundleInfo, 125 (const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, int32_t userId), (override)); 126 127 MOCK_METHOD(ErrCode, GetApplicationInfoV9, 128 (const std::string &, int32_t, int32_t, ApplicationInfo &), (override)); 129 130 MOCK_METHOD2(GetBundleNameForUid, bool(const int, std::string &)); 131 132 MOCK_METHOD1(CheckIsSystemAppByUid, bool(const int)); 133 134 MOCK_METHOD2(GetNameForUid, int32_t(const int, std::string &)); 135 }; 136 } // namespace AppExecFwk 137 } // namespace OHOS 138 139 #endif // OHOS_FORM_FWK_MOCK_BUNDLE_MANAGER_H 140