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 16import { paramMock } from "../utils" 17 18export function mockPackageManager() { 19 const PackageFlagMock = { 20 DEFAULT: 0x00000000, 21 GET_ACTIVITIES: 0x00000001, 22 GET_META_DATA: 0x00000080, 23 MATCH_UNINSTALLED_PACKAGES: 0x00002000, 24 MATCH_ALL: 0x00020000 25 } 26 const PackageErrorMock = { 27 NAME_NOT_FOUND_EXCEPTION: 0 28 } 29 const ApplicationInfosMock = [ 30 { 31 packageName: "[PC preview] unknown package name", 32 className: "[PC preview] unknown class name", 33 name: "[PC preview] unknown name", 34 labelId: "[PC preview] unknown label id", 35 iconId: "[PC preview] unknown icon id", 36 sourceDir: "[PC preview] unknown source dir", 37 flags: "[PC preview] unknown flags", 38 customizeData: { 39 key: "[PC preview] unknown key", 40 value: { 41 CustomizeData: { 42 name: "[PC preview] unknown name", 43 value: "[PC preview] unknown value", 44 } 45 } 46 } 47 }, 48 ] 49 const ApplicationInfoMock = { 50 packageName: "[PC preview] unknown package name", 51 className: "[PC preview] unknown class name", 52 name: "[PC preview] unknown name", 53 labelId: "[PC preview] unknown label id", 54 iconId: "[PC preview] unknown icon id", 55 sourceDir: "[PC preview] unknown source dir", 56 flags: "[PC preview] unknown flags", 57 customizeData: { 58 key: "[PC preview] unknown key", 59 value: { 60 CustomizeData: { 61 name: "[PC preview] unknown name", 62 value: "[PC preview] unknown value", 63 } 64 } 65 } 66 } 67 const ApplicationFlagMock = { 68 FLAG_SYSTEM: 0x0000000001, 69 FLAG_IS_DATA_ONLY: 0x1000000000, 70 } 71 const PackageInfoMock = { 72 packageName: "[PC preview] unknown package name", 73 name: "[PC preview] unknown name ", 74 lastUpdateTime: "[PC preview] unknown last update time", 75 versionCode: "[PC preview] unknown version code", 76 appInfo: { 77 packageName: "[PC preview] unknown package name", 78 className: "[PC preview] unknown class name", 79 name: "[PC preview] unknown name", 80 labelId: "[PC preview] unknown label id", 81 iconId: "[PC preview] unknown icon id", 82 sourceDir: "[PC preview] unknown source dir", 83 flags: "[PC preview] unknown flags", 84 customizeData: { 85 key: "[PC preview] unknown key", 86 value: { 87 CustomizeData: { 88 name: "[PC preview] unknown name", 89 value: "[PC preview] unknown value", 90 } 91 } 92 } 93 } 94 } 95 const PackageInfosMock = [ 96 { 97 packageName: "[PC preview] unknown package name", 98 name: "[PC preview] unknown name ", 99 lastUpdateTime: "[PC preview] unknown last update time", 100 versionCode: "[PC preview] unknown version code", 101 appInfo: { 102 packageName: "[PC preview] unknown package name", 103 className: "[PC preview] unknown class name", 104 name: "[PC preview] unknown name", 105 labelId: "[PC preview] unknown label id", 106 iconId: "[PC preview] unknown icon id", 107 sourceDir: "[PC preview] unknown source dir", 108 flags: "[PC preview] unknown flags", 109 customizeData: { 110 key: "[PC preview] unknown key", 111 value: { 112 CustomizeData: { 113 name: "[PC preview] unknown name", 114 value: "[PC preview] unknown value", 115 } 116 } 117 } 118 } 119 }, 120 ] 121 122 const CustomizeDataMock = { 123 name: "[PC preview] unknown name", 124 value: "[PC preview] unknown value", 125 } 126 const BundleErrorMock = { 127 REMOTE_EXCEPTION: 0, 128 SECURITY_EXCEPTION: 1 129 } 130 const ActivityInfoMock = { 131 packageName: "[PC preview] unknown package name", 132 name: "[PC preview] unknown name", 133 iconId: "[PC preview] unknown icon id", 134 labelId: "[PC preview] unknown label id", 135 customizeData: { 136 key: "[PC preview] unknown key", 137 value: { 138 CustomizeData: { 139 name: "[PC preview] unknown name", 140 value: "[PC preview] unknown value", 141 } 142 } 143 } 144 } 145 const ActivityInfosMock = [ 146 { 147 packageName: "[PC preview] unknown package name", 148 name: "[PC preview] unknown name", 149 iconId: "[PC preview] unknown icon id", 150 labelId: "[PC preview] unknown label id", 151 customizeData: { 152 key: "[PC preview] unknown key", 153 value: { 154 CustomizeData: { 155 name: "[PC preview] unknown name", 156 value: "[PC preview] unknown value", 157 } 158 } 159 } 160 }, 161 ] 162 const ComponentNameMock = { 163 package: "[PC preview] unknown package", 164 name: "[PC preview] unknown name", 165 } 166 global.ohosplugin.package = { 167 getApplicationInfo: function (...args) { 168 console.warn("PackageManger.getApplicationInfo interface mocked in the Previewer. How this interface works on the" + 169 " Previewer may be different from that on a real device.") 170 const len = args.length 171 if (typeof args[len - 1] === 'function') { 172 args[len - 1].call(this, paramMock.businessErrorMock, ApplicationInfoMock) 173 } else { 174 return new Promise((resolve) => { 175 resolve(ApplicationInfoMock) 176 }); 177 } 178 }, 179 getInstalledApplications: function (...args) { 180 console.warn("PackageManger.getInstalledApplications interface mocked in the Previewer. How this interface works on the" + 181 " Previewer may be different from that on a real device.") 182 const len = args.length 183 if (typeof args[len - 1] === 'function') { 184 args[len - 1].call(this, paramMock.businessErrorMock, ApplicationInfosMock) 185 } else { 186 return new Promise((resolve) => { 187 resolve(ApplicationInfosMock) 188 }); 189 } 190 }, 191 getPackageInfo: function (...args) { 192 console.warn("PackageManger.getPackageInfo interface mocked in the Previewer. How this interface works on the" + 193 " Previewer may be different from that on a real device.") 194 const len = args.length 195 if (typeof args[len - 1] === 'function') { 196 args[len - 1].call(this, paramMock.businessErrorMock, PackageInfoMock) 197 } else { 198 return new Promise((resolve) => { 199 resolve(PackageInfoMock) 200 }); 201 } 202 }, 203 getInstalledPackages: function (...args) { 204 console.warn("PackageManger.getInstalledPackages interface mocked in the Previewer. How this interface works on the" + 205 " Previewer may be different from that on a real device.") 206 const len = args.length 207 if (typeof args[len - 1] === 'function') { 208 args[len - 1].call(this, paramMock.businessErrorMock, PackageInfosMock) 209 } else { 210 return new Promise((resolve) => { 211 resolve(PackageInfosMock) 212 }); 213 } 214 }, 215 getActivityInfo: function (...args) { 216 console.warn("PackageManger.getActivityInfo interface mocked in the Previewer. How this interface works on the" + 217 " Previewer may be different from that on a real device.") 218 const len = args.length 219 if (typeof args[len - 1] === 'function') { 220 args[len - 1].call(this, paramMock.businessErrorMock, ActivityInfoMock) 221 } else { 222 return new Promise((resolve) => { 223 resolve(ActivityInfoMock) 224 }); 225 } 226 }, 227 getActivityList: function (...args) { 228 console.warn("PackageManger.getActivityList interface mocked in the Previewer. How this interface works on the" + 229 " Previewer may be different from that on a real device.") 230 const len = args.length 231 if (typeof args[len - 1] === 'function') { 232 args[len - 1].call(this, paramMock.businessErrorMock, ActivityInfosMock) 233 } else { 234 return new Promise((resolve) => { 235 resolve(ActivityInfosMock) 236 }); 237 } 238 }, 239 } 240} 241