1/* 2 * Copyright (c) 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' 17import { ExtensionContextClass } from './ExtensionContext' 18 19const ElementAttributeNameMock = '[PC Preview] unknown element attribute name'; 20 21const ElementAttributeValuesMock = '[PC Preview] unknown element attribute value'; 22 23const Rect = { 24 left: '[PC Preview] unknown element left', 25 top: '[PC Preview] unknown element top', 26 width: '[PC Preview] unknown element width', 27 height: '[PC Preview] unknown element height' 28}; 29 30const AccessibilityElement = { 31 attributeNames: function (...args) { 32 console.warn('AccessibilityElement.attributeNames interface mocked in the Previewer.' + 33 ' How this interface works on the Previewer may be different from that on a real device.'); 34 const len = args.length; 35 if (typeof args[len - 1] === 'function') { 36 args[len - 1].call(this, paramMock.businessErrorMock, [ElementAttributeNameMock]); 37 } else { 38 return new Promise((resolve, reject) => { 39 resolve([ElementAttributeNameMock]); 40 }); 41 } 42 }, 43 attributeValue: function (...args) { 44 console.warn('AccessibilityElement.attributeValue interface mocked in the Previewer.' + 45 ' How this interface works on the Previewer may be different from that on a real device.'); 46 let value = null; 47 switch (args[0]) { 48 case 'contents': 49 value = [ElementAttributeValuesMock]; 50 break; 51 case 'rect': 52 case 'screenRect': 53 value = Rect; 54 break; 55 case 'parent': 56 case 'rootElement': 57 value = AccessibilityElement; 58 break; 59 case 'children': 60 value = [AccessibilityElement]; 61 break; 62 default: 63 value = ElementAttributeValuesMock; 64 break; 65 } 66 67 const len = args.length; 68 if (typeof args[len - 1] === 'function') { 69 args[len - 1].call(this, paramMock.businessErrorMock, value); 70 } else { 71 return new Promise((resolve, reject) => { 72 resolve(value); 73 }); 74 } 75 }, 76 actionNames: function (...args) { 77 console.warn('AccessibilityElement.actionNames interface mocked in the Previewer.' + 78 ' How this interface works on the Previewer may be different from that on a real device.'); 79 const len = args.length; 80 if (typeof args[len - 1] === 'function') { 81 args[len - 1].call(this, paramMock.businessErrorMock, [param.paramStringMock]); 82 } else { 83 return new Promise((resolve, reject) => { 84 resolve([param.paramStringMock]); 85 }); 86 } 87 }, 88 performAction: function (...args) { 89 console.warn('AccessibilityElement.performAction interface mocked in the Previewer.' + 90 ' How this interface works on the Previewer may be different from that on a real device.'); 91 const len = args.length; 92 if (typeof args[len - 1] === 'function') { 93 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramBooleanMock); 94 } else { 95 return new Promise((resolve, reject) => { 96 resolve(paramMock.paramBooleanMock); 97 }); 98 } 99 }, 100 findElement: function (...args) { 101 console.warn('AccessibilityElement.findElement interface mocked in the Previewer.' + 102 ' How this interface works on the Previewer may be different from that on a real device.'); 103 const result = (args[0] === 'content') ? [AccessibilityElement] : AccessibilityElement; 104 const len = args.length; 105 if (typeof args[len - 1] === 'function') { 106 args[len - 1].call(this, paramMock.businessErrorMock, result); 107 } else { 108 return new Promise((resolve, reject) => { 109 resolve(result); 110 }); 111 } 112 }, 113}; 114 115export class AccessibilityExtensionContext extends ExtensionContextClass { 116 constructor(...args) { 117 super(); 118 console.warn('AccessibilityExtensionContext.constructor interface mocked in the Previewer.' + 119 ' How this interface works on the Previewer may be different from that on a real device.'); 120 121 this.setEventTypeFilter = function (...args) { 122 console.warn('AccessibilityExtensionContext.setEventTypeFilter interface mocked in the Previewer.' + 123 ' How this interface works on the Previewer may be different from that on a real device.'); 124 const len = args.length; 125 if (typeof args[len - 1] === 'function') { 126 args[len - 1].call(this, paramMock.businessErrorMock); 127 } else { 128 return new Promise((resolve, reject) => { 129 resolve(); 130 }); 131 } 132 }; 133 134 this.setTargetBundleName = function (...args) { 135 console.warn('AccessibilityExtensionContext.setTargetBundleName interface mocked in the Previewer.' + 136 ' How this interface works on the Previewer may be different from that on a real device.'); 137 const len = args.length; 138 if (typeof args[len - 1] === 'function') { 139 args[len - 1].call(this, paramMock.businessErrorMock); 140 } else { 141 return new Promise((resolve, reject) => { 142 resolve(); 143 }); 144 } 145 }; 146 147 this.getFocusElement = function (...args) { 148 console.warn('AccessibilityExtensionContext.getFocusElement interface mocked in the Previewer.' + 149 ' How this interface works on the Previewer may be different from that on a real device.'); 150 const len = args.length; 151 if (typeof args[len - 1] === 'function') { 152 args[len - 1].call(this, paramMock.businessErrorMock, AccessibilityElement); 153 } else { 154 return new Promise((resolve, reject) => { 155 resolve(AccessibilityElement); 156 }); 157 } 158 }; 159 160 this.getWindowRootElement = function (...args) { 161 console.warn('AccessibilityExtensionContext.getWindowRootElement interface mocked in the Previewer.' + 162 ' How this interface works on the Previewer may be different from that on a real device.'); 163 const len = args.length; 164 if (typeof args[len - 1] === 'function') { 165 args[len - 1].call(this, paramMock.businessErrorMock, AccessibilityElement); 166 } else { 167 return new Promise((resolve, reject) => { 168 resolve(AccessibilityElement); 169 }); 170 } 171 }; 172 173 this.getWindows = function (...args) { 174 console.warn('AccessibilityExtensionContext.getWindows interface mocked in the Previewer.' + 175 ' How this interface works on the Previewer may be different from that on a real device.'); 176 const len = args.length; 177 if (typeof args[len - 1] === 'function') { 178 args[len - 1].call(this, paramMock.businessErrorMock, [AccessibilityElement]); 179 } else { 180 return new Promise((resolve, reject) => { 181 resolve([AccessibilityElement]); 182 }); 183 } 184 }; 185 186 this.injectGesture = function (...args) { 187 console.warn('AccessibilityExtensionContext.injectGesture interface mocked in the Previewer.' + 188 ' How this interface works on the Previewer may be different from that on a real device.'); 189 const len = args.length; 190 if (typeof args[len - 1] === 'function') { 191 args[len - 1].call(this, paramMock.businessErrorMock); 192 } else { 193 return new Promise((resolve, reject) => { 194 resolve(); 195 }); 196 } 197 }; 198 } 199};