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 18const DisplayState = { 19 STATE_UNKNOWN: 0, 20 STATE_OFF: 1, 21 STATE_ON: 2, 22 STATE_DOZE: 3, 23 STATE_DOZE_SUSPEND: 4, 24 STATE_VR: 5, 25 STATE_ON_SUSPEND: 6 26} 27const DisplayType = { 28 add: 'add', 29 remove: 'remove', 30 change: 'change' 31} 32 33export function mockDisplay() { 34 const Rect = { 35 left: '[PC preview] unknow left', 36 top: '[PC preview] unknow top', 37 width: '[PC preview] unknow width', 38 height: '[PC preview] unknow height', 39 } 40 41 const WaterfallDisplayAreaRects = { 42 left: Rect, 43 top: Rect, 44 width: Rect, 45 height: Rect 46 } 47 48 const CutoutInfo = { 49 boundingRects: Array(Rect), 50 waterfallDisplayAreaRects: WaterfallDisplayAreaRects 51 } 52 53 const Display = { 54 id: '[PC preview] unknow id', 55 name: '[PC preview] unknow name', 56 alive: '[PC preview] unknow alive', 57 state: DisplayState, 58 refreshRate: '[PC preview] unknow refreshRate', 59 rotation: '[PC preview] unknow rotation', 60 width: '[PC preview] unknow width', 61 height: '[PC preview] unknow height', 62 densityDPI: '[PC preview] unknow densityDPI', 63 densityPixels: '[PC preview] unknow densityPixels', 64 scaledDensity: '[PC preview] unknow scaledDensity', 65 xDPI: '[PC preview] unknow xDPI', 66 yDPI: '[PC preview] unknow yDPI', 67 getCutoutInfo: function(...args) { 68 console.warn('Display.getCutoutInfo interface mocked in the Previewer. How this interface works on the' + 69 ' Previewer may be different from that on a real device.'); 70 const len = args.length; 71 if (typeof args[len - 1] === 'function') { 72 args[len - 1].call(this, paramMock.businessErrorMock, CutoutInfo); 73 } else { 74 return new Promise((resolve) => { 75 resolve(CutoutInfo); 76 }); 77 } 78 } 79 } 80 81 const display = { 82 getDefaultDisplay: function (...args) { 83 console.warn("Display.getDefaultDisplay interface mocked in the Previewer. How this interface works on the" + 84 " Previewer may be different from that on a real device.") 85 const len = args.length 86 if (typeof args[len - 1] === 'function') { 87 args[len - 1].call(this, paramMock.businessErrorMock, Display) 88 } else { 89 return new Promise((resolve, reject) => { 90 resolve(Display) 91 }) 92 } 93 }, 94 getDefaultDisplaySync: function (...args) { 95 console.warn("Display.getDefaultDisplay interface mocked in the Previewer. How this interface works on the Previewer" + 96 " may be different from that on a real device.") 97 return Display 98 }, 99 getAllDisplay: function (...args) { 100 console.warn("Display.getAllDisplay interface mocked in the Previewer. How this interface works on the Previewer" + 101 " may be different from that on a real device.") 102 const len = args.length 103 if (typeof args[len - 1] === 'function') { 104 args[len - 1].call(this, paramMock.businessErrorMock, [Display]) 105 } else { 106 return new Promise((resolve, reject) => { 107 resolve([Display]) 108 }) 109 } 110 }, 111 hasPrivateWindow: function (...args) { 112 console.warn("Display.hasPrivateWindow interface mocked in the Previewer. How this interface works on the Previewer" + 113 " may be different from that on a real device.") 114 return paramMock.paramBooleanMock 115 }, 116 on: function (...args) { 117 console.warn("Display.on interface mocked in the Previewer. How this interface works on the Previewer may be" + 118 " different from that on a real device.") 119 const len = args.length 120 if (len!==2){ 121 console.warn("Display.on:please check params !") 122 return 123 } 124 if (typeof args[len - 1] === 'function') { 125 if (args[0] === DisplayType.add|| args[0] === DisplayType.remove || args[0] === DisplayType.change){ 126 console.warn(`Display.on: you has registered ${args[0]} event.`) 127 } else { 128 console.warn("Display.on:please check first param!") 129 } 130 } else { 131 console.warn("Display.on:please check params, the second parma must be a function!") 132 } 133 }, 134 off: function (...args) { 135 console.warn("Display.off interface mocked in the Previewer. How this interface works on the Previewer may be" + 136 " different from that on a real device.") 137 const len = args.length 138 if (len!==2){ 139 console.warn("Display.off:please check params!") 140 return 141 } 142 if (typeof args[len - 1] === 'function') { 143 if (args[0] === DisplayType.add|| args[0] === DisplayType.remove || args[0] === DisplayType.change){ 144 console.warn(`Display.off: you has registered ${args[0]} event`) 145 } else { 146 console.warn("Display.off:please check first param!") 147 } 148 } else { 149 console.warn("Display.off:please check params, the second parma must be a function!") 150 } 151 }, 152 DisplayState 153 } 154 return display 155}