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 16import { paramMock } from "../utils" 17 18export function mockInputDevice() { 19 const DeviceListener = { 20 type: '[PC preview] unknow type', 21 deviceId: '[PC preview] unknow deviceId', 22 } 23 const AxisRange = { 24 source: '[PC preview] unknow source', 25 axis: '[PC preview] unknow axis', 26 max: '[PC preview] unknow max', 27 min: '[PC preview] unknow min', 28 fuzz: '[PC preview] unknow fuzz', 29 flat: '[PC preview] unknow flat', 30 resolution: '[PC preview] unknow resolution', 31 } 32 const InputDeviceData = { 33 id: '[PC preview] unknow id', 34 name: '[PC preview] unknow name', 35 sources: ['[PC preview] unknow sources'], 36 axisRanges: [AxisRange], 37 bus: '[PC preview] unknow bus', 38 product: '[PC preview] unknow product', 39 vendor: '[PC preview] unknow vendor', 40 version: '[PC preview] unknow version', 41 phys: '[PC preview] unknow phys', 42 uniq: '[PC preview] unknow uniq', 43 } 44 const KeyboardType = { 45 NONE: 0, 46 UNKNOWN: 1, 47 ALPHABETIC_KEYBOARD: 2, 48 DIGITAL_KEYBOARD: 3, 49 HANDWRITING_PEN: 4, 50 REMOTE_CONTROL: 5, 51 } 52 53 const EventType = ['change'] 54 const DeviceIds = [0, 1, 2, 3, 4] 55 const GetKeyboardType = 3 56 const inputDevice = { 57 KeyboardType, 58 on: function (...args) { 59 console.warn("inputDevice.on interface mocked in the Previewer. How this interface works on the" + 60 " Previewer may be different from that on a real device.") 61 const len = args.length; 62 if (len === 2) { 63 if (EventType.indexOf(args[0]) === -1) { 64 console.warn("the first parameter must be 'change'") 65 } 66 if (typeof args[1] != 'function') { 67 console.warn("the second parameter type must be 'function'") 68 } 69 } else { 70 console.warn("the number of parameter must be two") 71 } 72 }, 73 off: function (...args) { 74 console.warn("inputDevice.off interface mocked in the Previewer. How this interface works on the" + 75 " Previewer may be different from that on a real device.") 76 const len = args.length; 77 if (len < 1 || len > 2) { 78 console.warn("a maximum of two parameters") 79 } else if (len === 1) { 80 if (EventType.indexOf(args[0]) === -1) { 81 console.warn("first parameter must be 'change'") 82 } 83 } else { 84 if (EventType.indexOf(args[0]) === -1) { 85 console.warn("first parameter must be 'change'") 86 } 87 if (typeof args[1] != 'function') { 88 console.warn("second parameter type must be 'function'") 89 } 90 } 91 }, 92 getDeviceSync: function (...args) { 93 console.warn("inputDevice.getDeviceSync interface mocked in the Previewer. How this interface works on the" + 94 " Previewer may be different from that on a real device.") 95 const len = args.length; 96 if (len != 1) { 97 console.warn("the number of parameter must be one") 98 } else { 99 if (DeviceIds.indexOf(args[len - 1]) != -1) { 100 return InputDeviceData; 101 } else { 102 console.warn("parameter error") 103 return paramMock.businessErrorMock; 104 } 105 } 106 }, 107 getDeviceIdsSync: function () { 108 console.warn("inputDevice.getDeviceIdsSync interface mocked in the Previewer. How this interface works on the" + 109 " Previewer may be different from that on a real device.") 110 return DeviceIds; 111 }, 112 getDeviceIds: function (...args) { 113 console.warn("inputDevice.getDeviceIds interface mocked in the Previewer. How this interface works on the" + 114 " Previewer may be different from that on a real device.") 115 const len = args.length; 116 if (len > 1) { 117 console.warn("the number of parameter must be one") 118 return; 119 } 120 if (len === 1) { 121 if (typeof args[0] === 'function') { 122 args[0].call(this, DeviceIds); 123 } else { 124 console.warn("parameter type must be 'function'") 125 } 126 } else { 127 return new Promise((resolve, reject) => { 128 resolve(DeviceIds); 129 }) 130 } 131 }, 132 getDevice: function (...args) { 133 console.warn("inputDevice.getDevice interface mocked in the Previewer. How this interface works on the" + 134 " Previewer may be different from that on a real device."); 135 const len = args.length; 136 if (len < 1 || len > 2) { 137 console.warn("the number of parameter must be two"); 138 return; 139 } 140 if (typeof args[0] !== 'number') { 141 console.warn("the first parameter error"); 142 return; 143 } 144 if (len === 1) { 145 return new Promise((resolve, reject) => { 146 resolve(InputDeviceData); 147 }) 148 } else { 149 if (typeof args[1] !== 'function') { 150 console.warn("the second parameter type must be 'function'"); 151 return; 152 } 153 args[1].call(this, InputDeviceData); 154 } 155 }, 156 supportKeys: function(...args) { 157 console.warn("inputDevice.supportKeys interface mocked in the Previewer." + 158 "How this interface works on the" + " Previewer may be different from that on a real device."); 159 const len = args.length; 160 if (len < 2 || len > 3) { 161 console.warn("parameter number error"); 162 return; 163 } 164 if (typeof args[0] !== 'number') { 165 console.warn("the first parameter error"); 166 return; 167 } 168 if (typeof args[1] !== 'object') { 169 console.warn("the second parameter type must be array"); 170 return; 171 } 172 if (args[1].length < 1 || args[1].length > 5) { 173 console.warn("the number of keys is incorrect, the range is 1 to 5"); 174 return; 175 } 176 var supportKeysRet = []; 177 for (var i = 0; i < args[1].length; ++i) { 178 supportKeysRet.unshift('[PC preview] unknow boolean'); 179 } 180 if (len === 2) { 181 return new Promise((resolve, reject) => { 182 resolve(supportKeysRet); 183 }) 184 } else { 185 if (typeof args[2] !== 'function') { 186 console.warn("the third parameter type is wrong"); 187 return; 188 } 189 args[2].call(this, supportKeysRet); 190 } 191 }, 192 getKeyboardType: function(...args) { 193 console.warn("inputDevice.getKeyboardType interface mocked in the Previewer." + 194 "How this interface works on the" + " Previewer may be different from that on a real device."); 195 const len = args.length; 196 if (len < 1 || len > 2) { 197 console.warn("parameter number error"); 198 return; 199 } 200 if (typeof args[0] !== 'number') { 201 console.warn("the first parameter error"); 202 return; 203 } 204 if (len == 1) { 205 return new Promise((resolve, reject) => { 206 resolve(GetKeyboardType); 207 }) 208 } else { 209 if (typeof args[1] !== 'function') { 210 console.warn("the second parameter type must be function"); 211 return; 212 } 213 args[1].call(this, GetKeyboardType); 214 } 215 }, 216 }; 217 return inputDevice 218}