ExtensionContextTest.ts 26.8 KB
Newer Older
M
Mupceet 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import commonEvent from '@ohos.commonEvent';
import display from '@ohos.display'
M
Mupceet 已提交
17 18
import { GesturePath } from '@ohos.accessibility.GesturePath';
import { GesturePoint } from '@ohos.accessibility.GesturePoint';
M
Mupceet 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

export class ExtensionContextTest {
    private context = undefined;
    private subScriber = undefined;

    constructor(context) {
        this.context = context;
    }

    async init() {
        this.subScriber = await commonEvent.createSubscriber({events: ['caseExecute']});
        commonEvent.subscribe(this.subScriber, (err, data) => {
            console.info('ExtensionContextTest subscribe callback data:' + JSON.stringify(data));
            this.processCase(data.data);
        });
    }

    clear() {
        commonEvent.unsubscribe(this.subScriber);
        this.subScriber = undefined;
        this.context = undefined;
    }

    private publishCaseResult(caseName: string, result: boolean) {
        let commonEventPublishData = {
            data: (result ? 'pass' : 'fail'),
            parameters: {
                case: caseName
            }
        }
        commonEvent.publish('caseResult', commonEventPublishData, (err) => {
            console.info("ExtensionContextTest publishCaseResult finish: " + JSON.stringify(commonEventPublishData));
        });
    }

    private async processCase(caseName) {
        console.info('ExtensionContextTest processCase: ' + caseName);
        let bundleName: Array<string> = [];
        let windowId = -1;
        let displayId = -1;
M
Mupceet 已提交
59
        let gesturePath;
M
Mupceet 已提交
60 61 62

        switch (caseName) {
            case 'AccessibilityExtensionContextTest_setTargetBundleName_asyncCallback_1500':
M
Mupceet 已提交
63
                bundleName = ['com.example.accessibilityxts'];
M
Mupceet 已提交
64 65 66 67 68 69 70 71 72 73
                this.setTargetBundleNameCallback(caseName, bundleName);
                break;
            case 'AccessibilityExtensionContextTest_setTargetBundleName_asyncCallback_1600':
                bundleName = ['aaa'];
                this.setTargetBundleNameCallback(caseName, bundleName);
                break;
            case 'AccessibilityExtensionContextTest_setTargetBundleName_asyncCallback_1700':
                this.setTargetBundleNameCallback(caseName, bundleName);
                break;
            case 'AccessibilityExtensionContextTest_setTargetBundleName_asyncPromise_1800':
M
Mupceet 已提交
74
                bundleName = ['com.example.accessibilityxts'];
M
Mupceet 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
                this.setTargetBundleNamePromise(caseName, bundleName);
                break;
            case 'AccessibilityExtensionContextTest_setTargetBundleName_asyncPromise_1900':
                bundleName = ['aaa'];
                this.setTargetBundleNamePromise(caseName, bundleName);
                break;
            case 'AccessibilityExtensionContextTest_setTargetBundleName_asyncPromise_2000':
                this.setTargetBundleNamePromise(caseName, bundleName);
                break;
            case 'AccessibilityExtensionContextTest_getWindowRootElement_asyncCallback_2100':
                this.getWindowRootElementCallback(caseName);
                break;
            case 'AccessibilityExtensionContextTest_getWindowRootElement_asyncCallback_2200':
                windowId = await this.getWindowIdByText('button1');
                this.getWindowRootElementByIdCallback(caseName, windowId);
                break;
            case 'AccessibilityExtensionContextTest_getWindowRootElement_asyncCallback_2300':
                windowId = 1000;
                this.getWindowRootElementByIdCallback(caseName, windowId);
                break;
            case 'AccessibilityExtensionContextTest_getWindowRootElement_asyncCallback_2400':
                windowId = -1;
                this.getWindowRootElementByIdCallback(caseName, windowId);
                break;
            case 'AccessibilityExtensionContextTest_getWindowRootElement_Promise_2500':
                windowId = await this.getWindowId();
                this.getWindowRootElementByIdPromise(caseName, windowId);
                break;
            case 'AccessibilityExtensionContextTest_getWindowRootElement_Promise_2600':
                windowId = 1000;
                this.getWindowRootElementByIdPromise(caseName, windowId);
                break;
            case 'AccessibilityExtensionContextTest_getWindowRootElement_Promise_2700':
                windowId = -1;
                this.getWindowRootElementByIdPromise(caseName, windowId);
                break;
            case 'AccessibilityExtensionContextTest_getWindows_asyncCallback_2800':
                this.getWindowsCallback(caseName);
                break;
            case 'AccessibilityExtensionContextTest_getWindows_asyncCallback_2900':
                displayId = await this.getDisPlayId();
                this.getWindowsByIdCallback(caseName, displayId);
                break;
            case 'AccessibilityExtensionContextTest_getWindows_asyncCallback_3000':
                displayId = 100;
                this.getWindowsByIdCallback(caseName, displayId);
                break;
            case 'AccessibilityExtensionContextTest_getWindows_asyncCallback_3100':
                displayId = -1;
                this.getWindowsByIdCallback(caseName, displayId);
                break;
            case 'AccessibilityExtensionContextTest_getWindows_Promise_3200':
                displayId = await this.getDisPlayId();
                this.getWindowsByIdPromise(caseName, displayId);
                break;
            case 'AccessibilityExtensionContextTest_getWindows_Promise_3300':
                displayId = 100;
                this.getWindowsByIdPromise(caseName, displayId);
                break;
            case 'AccessibilityExtensionContextTest_getWindows_Promise_3400':
                displayId = -1;
                this.getWindowsByIdPromise(caseName, displayId);
                break;
            case 'AccessibilityExtensionContextTest_getFocusElement_asyncCallback_3700':
                await this.setAccessibilityFocus();
                this.getFocusElementByTypeCallback(caseName, true, true);
                break;
            case 'AccessibilityExtensionContextTest_getFocusElement_asyncCallback_3900':
                await this.clearAccessibilityFocus();
                this.getFocusElementByTypeCallback(caseName, false, true);
                break;
            case 'AccessibilityExtensionContextTest_getFocusElement_asyncPromise_4100':
                await this.setAccessibilityFocus();
                this.getFocusElementByTypePromise(caseName, true, true);
                break;
            case 'AccessibilityExtensionContextTest_getFocusElement_asyncPromise_4300':
                await this.clearAccessibilityFocus();
                this.getFocusElementByTypePromise(caseName, false, true);
                break;
            case 'AccessibilityExtensionContextTest_gestureInject_asyncCallback_4500':
M
Mupceet 已提交
155 156
                gesturePath = new GesturePath(100);
                gesturePath.points.push(new GesturePoint(10, 10));
M
Mupceet 已提交
157 158 159
                this.gestureInjectCallback(caseName, gesturePath);
                break;
            case 'AccessibilityExtensionContextTest_gestureInject_asyncCallback_4600':
M
Mupceet 已提交
160 161
                gesturePath = new GesturePath(60000);
                gesturePath.points.push(new GesturePoint(50, 50));
M
Mupceet 已提交
162 163 164
                this.gestureInjectCallback(caseName, gesturePath);
                break;
            case 'AccessibilityExtensionContextTest_gestureInject_asyncCallback_4700':
M
Mupceet 已提交
165 166 167
                gesturePath = new GesturePath(1000);
                gesturePath.points.push(new GesturePoint(200, 200),
                                        new GesturePoint(100, 100));
M
Mupceet 已提交
168 169 170
                this.gestureInjectCallback(caseName, gesturePath);
                break;
            case 'AccessibilityExtensionContextTest_gestureInject_asyncCallback_4800':
M
Mupceet 已提交
171 172 173 174
                gesturePath = new GesturePath(60000);
                gesturePath.points.push(new GesturePoint(50, 50),
                                        new GesturePoint(100, 100),
                                        new GesturePoint(1000, 1000));
M
Mupceet 已提交
175 176 177 178 179 180 181
                this.gestureInjectCallback(caseName, gesturePath);
                break;
            case 'AccessibilityExtensionContextTest_gestureInject_asyncCallback_4900':
                gesturePath = undefined;
                this.gestureInjectCallback(caseName, gesturePath);
                break;
            case 'AccessibilityExtensionContextTest_gestureInject_asyncPromise_5000':
M
Mupceet 已提交
182 183
                gesturePath = new GesturePath(100);
                gesturePath.points.push(new GesturePoint(10, 10));
M
Mupceet 已提交
184 185 186
                this.gestureInjectPromise(caseName, gesturePath);
                break;
            case 'AccessibilityExtensionContextTest_gestureInject_asyncPromise_5100':
M
Mupceet 已提交
187 188
                gesturePath = new GesturePath(60000);
                gesturePath.points.push(new GesturePoint(50, 50));
M
Mupceet 已提交
189 190 191
                this.gestureInjectPromise(caseName, gesturePath);
                break;
            case 'AccessibilityExtensionContextTest_gestureInject_asyncPromise_5200':
M
Mupceet 已提交
192 193 194
                gesturePath = new GesturePath(1000);
                gesturePath.points.push(new GesturePoint(200, 200),
                                        new GesturePoint(100, 100));
M
Mupceet 已提交
195 196 197
                this.gestureInjectPromise(caseName, gesturePath);
                break;
            case 'AccessibilityExtensionContextTest_gestureInject_asyncPromise_5300':
M
Mupceet 已提交
198 199 200 201
                gesturePath = new GesturePath(60000);
                gesturePath.points.push(new GesturePoint(50, 50),
                                        new GesturePoint(100, 100),
                                        new GesturePoint(1000, 1000));
M
Mupceet 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
                this.gestureInjectPromise(caseName, gesturePath);
                break;
            case 'AccessibilityExtensionContextTest_gestureInject_asyncPromise_5400':
                gesturePath = undefined;
                this.gestureInjectPromise(caseName, gesturePath);
                break;
            default:
                console.warn('ExtensionContextTest processCase unknown!!!!!!!!!!');
                break;
        }
    }

    private async getWindowId() {
        let windowId = -1;
        let rootNode = await this.context.getWindowRootElement();
        console.info("getWindowId rootNode: " + JSON.stringify(rootNode));
        if (rootNode) {
            windowId = await rootNode.attributeValue('windowId');
            console.info("getWindowId windowId: " + JSON.stringify(windowId));
        }
        return windowId;
    }

    private async getWindowIdByText(text: string) {
        let windowId = -1;
        let rootNode = await this.context.getWindowRootElement();
        console.info("getWindowId rootNode: " + JSON.stringify(rootNode));
        if (rootNode) {
            let tartgetNode = await rootNode.findElement('content', text);
            console.info("getWindowId tartgetNode: " + JSON.stringify(tartgetNode));
            if (tartgetNode) {
                windowId = await tartgetNode[0].attributeValue('windowId');
                console.info("getWindowId windowId: " + JSON.stringify(windowId));
            }
        }
        return windowId;
    }

    private async getDisPlayId() {
        let displayId = -1;
        display.getDefaultDisplay((err, data) => {
M
Mupceet 已提交
243
            if (err && err.code != 0) {
M
Mupceet 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
                console.error("getDisPlayId getDefaultDisplay error");
            } else {
                console.info("getDisPlayId getDefaultDisplay data.id: " + JSON.stringify(data.id));
                displayId = data.id;
            }
        });
        return displayId;
    }

    private async getRoot() {
        let rootNode = undefined;
        if (this.context) {
            let rootNode = await this.context.getWindowRootElement();
            console.info("getRoot rootNode: " + JSON.stringify(rootNode));
        }
        return rootNode;
    }

    private async setAccessibilityFocus() {
        let isFocus = false;
        if (this.context) {
            let rootNode = await this.context.getWindowRootElement();
            console.info("setAccessibilityFocus rootNode: " + JSON.stringify(rootNode));
            if (rootNode) {
                let tartgetNode = await rootNode.findElement('content', 'button1');
                console.info("setAccessibilityFocus tartgetNode: " + JSON.stringify(tartgetNode));
                if (tartgetNode) {
M
Mupceet 已提交
271
                    await tartgetNode[0].performAction("accessibilityFocus");
M
Mupceet 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285
                }
            }
        }
    }

    private async clearAccessibilityFocus() {
        let isFocus = false;
        if (this.context) {
            let rootNode = await this.context.getWindowRootElement();
            console.info("setAccessibilityFocus rootNode: " + JSON.stringify(rootNode));
            if (rootNode) {
                let tartgetNode = await rootNode.findElement('content', 'button1');
                console.info("setAccessibilityFocus tartgetNode: " + JSON.stringify(tartgetNode));
                if (tartgetNode) {
M
Mupceet 已提交
286
                    await tartgetNode[0].performAction("clearAccessibilityFocus");
M
Mupceet 已提交
287 288 289 290 291 292
                }
            }
        }
    }

    private async setTargetBundleNameCallback(caseName: string, bundleName: Array<string>) {
M
Mupceet 已提交
293
        let result = true;
M
Mupceet 已提交
294 295
        this.context.setTargetBundleName(bundleName, (err, res) => {
            console.info(caseName + " res: " + JSON.stringify(res));
M
Mupceet 已提交
296
            if (err && err.code != 0) {
M
Mupceet 已提交
297
                console.error(caseName + " err: " + JSON.stringify(err.code));
M
Mupceet 已提交
298 299 300 301 302
                if ((bundleName.length == 0 || bundleName[0] == 'aaa') && err && err.code != 0) {
                    result = true;
                } else {
                    result = false;
                }
M
Mupceet 已提交
303 304 305 306 307 308
            }
            this.publishCaseResult(caseName, result);
        });
    }

    private async setTargetBundleNamePromise(caseName: string, bundleName: Array<string>) {
M
Mupceet 已提交
309
        let result = true;
M
Mupceet 已提交
310 311
        this.context.setTargetBundleName(bundleName).then((res) => {
            console.info(caseName + " res: " + JSON.stringify(res));
M
Mupceet 已提交
312 313 314
            this.publishCaseResult(caseName, result);
        }).catch((error) => {
            console.error(caseName + " error: " + JSON.stringify(error));
M
Mupceet 已提交
315 316 317 318 319
            if ((bundleName.length == 0 || bundleName[0] == 'aaa') && error && error.code != 0) {
                result = true;
            } else {
                result = false;
            }
M
Mupceet 已提交
320 321 322 323 324 325 326 327
            this.publishCaseResult(caseName, result);
        });
    }

    private async getWindowRootElementCallback(caseName: string) {
        let result = false;
        this.context.getWindowRootElement(async (err, res) => {
            console.info(caseName + " res: " + JSON.stringify(res));
M
Mupceet 已提交
328
            if (err && err.code != 0) {
M
Mupceet 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
                console.error(caseName + " err: " + JSON.stringify(err.code));
            }
            if (res == undefined) {
                console.error(caseName + " res is undefined");
            }
            if (res) {
                let value = await res.attributeValue('componentId');
                if (typeof(value) == 'number') {
                    result = true;
                }
            } 
            this.publishCaseResult(caseName, result);
        });
    }

    private async getWindowRootElementByIdCallback(caseName: string, windowId: number) {
        let result = false;
        if ((windowId == -1) || (windowId == 1000)){
            this.context.getWindowRootElement(-1, async (err, res) => {
M
Mupceet 已提交
348
                if (err && err.code != 0) {
M
Mupceet 已提交
349
                    console.error(caseName + " err: " + JSON.stringify(err.code));
M
Mupceet 已提交
350
                    result = true;
M
Mupceet 已提交
351 352 353 354 355 356
                }
                this.publishCaseResult(caseName, result); 
            });
        } else {
            this.context.getWindowRootElement(windowId, async (err, res) => {
                console.info(caseName + " res: " + JSON.stringify(res));
M
Mupceet 已提交
357
                if (err && err.code != 0) {
M
Mupceet 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
                    console.error(caseName + " err: " + JSON.stringify(err.code));
                }
                if (res == undefined) {
                    console.error(caseName + " res is undefined");
                }
                if (res) {
                    let value = await res.attributeValue('componentId');
                    if (typeof(value) == 'number') {
                        result = true;
                    }
                }
                this.publishCaseResult(caseName, result); 
            });
        }
    }

    private async getWindowRootElementByIdPromise(caseName: string, windowId: number) {
        let result = false;
        if ((windowId == -1) || (windowId == 1000)){
            this.context.getWindowRootElement(-1).then(async (res) => {
                this.publishCaseResult(caseName, result);
                }).catch((error) => {
                    console.error(caseName + " error: " + JSON.stringify(error));
                    result = true;
                    this.publishCaseResult(caseName, result);
                });
        } else {
            this.context.getWindowRootElement(windowId).then(async (res) => {
                console.info(caseName + " res: " + JSON.stringify(res));
                if (res == undefined) {
                    console.error(caseName + " res is undefined");
                }
                if (res) {
                    let value = await res.attributeValue('componentId');
                    if (typeof(value) == 'number') {
                        result = true;
                    }
                }
                this.publishCaseResult(caseName, result);
                }).catch((error) => {
                    console.error(caseName + " error: " + JSON.stringify(error));
                    this.publishCaseResult(caseName, result);
            });
        }
    }


    private async getWindowsCallback(caseName: string) {
        let result = false;
        this.context.getWindows((err, res) => {
            console.info(caseName + " res: " + JSON.stringify(res));
M
Mupceet 已提交
409
            if (err && err.code != 0) {
M
Mupceet 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
                console.error(caseName + " err: " + JSON.stringify(err.code));
            }
            if (res == undefined) {
                console.error(caseName + " res is undefined");
            }
            if (res) {
                if (Array.isArray(res)) {
                    result = true;
                }
            }
            this.publishCaseResult(caseName, result);
        });
    }

    private async getWindowsByIdCallback(caseName: string, displayId: number) {
        let result = false;
        console.info(caseName + " displayId: " + JSON.stringify(displayId));
M
Mupceet 已提交
427 428
        if (displayId == -1 || displayId == 100) {
            this.context.getWindows(-1, (err, res) => {
M
Mupceet 已提交
429 430
                console.info(caseName + " res: " + JSON.stringify(res));
                if (err.code != 0) {
M
Mupceet 已提交
431
                console.error(caseName + " err: " + JSON.stringify(err.code));
M
Mupceet 已提交
432 433 434 435 436 437 438
                    result = true;
                }
                this.publishCaseResult(caseName, result);
            });
        } else {
            this.context.getWindows(displayId, (err, res) => {
                console.info(caseName + " res: " + JSON.stringify(res));
M
Mupceet 已提交
439
                if (err && err.code != 0) {
M
Mupceet 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
                    console.error(caseName + " err: " + JSON.stringify(err.code));
                }
                if (res) {
                    if (Array.isArray(res)) {
                        result = true;
                    }
                }
                this.publishCaseResult(caseName, result);
            });
        }
    }

    private async getWindowsByIdPromise(caseName: string, displayId: number) {
        let result = false;
        console.info(caseName + " displayId: " + JSON.stringify(displayId));
        if (displayId == -1) {
M
Mupceet 已提交
456 457 458 459 460 461
            this.context.getWindows(displayId).then(async (res) => {
                result = false;
                this.publishCaseResult(caseName, result);
            }).catch((err) => {
                console.error(caseName + 'err:' + JSON.stringify(err));
                if (err && err.code != 0) {
M
Mupceet 已提交
462 463 464 465
                    result = true;
                }
                this.publishCaseResult(caseName, result);
            });
M
Mupceet 已提交
466

M
Mupceet 已提交
467 468 469
        } else {
            this.context.getWindows(displayId, (err, res) => {
                console.info(caseName + " res: " + JSON.stringify(res));
M
Mupceet 已提交
470
                if (err && err.code != 0) {
M
Mupceet 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
                    console.error(caseName + " err: " + JSON.stringify(err.code));
                }
                if (res) {
                    if (Array.isArray(res)) {
                        result = true;
                    }
                }
                this.publishCaseResult(caseName, result);
            });
        }
    }

    private async getFocusElementCallback(caseName: string, hasFocus: boolean) {
        let result = false;
        this.context.getFocusElement((err, res) => {
            console.info(caseName + "res: " + JSON.stringify(res));
M
Mupceet 已提交
487
            if (err && err.code != 0) {
M
Mupceet 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
                console.error(caseName + "err: " + JSON.stringify(err.code));
                result = true;
            }
            if (res == undefined) {
                result = true;
            }
            this.publishCaseResult(caseName, result);
        });
    }

    private async getFocusElementByTypeCallback(caseName: string, hasFocus: boolean, isAccessibilityFocus: boolean) {
        let result = false;
        if (isAccessibilityFocus) {
            this.context.getFocusElement(true, async (err, res) => {
                console.info(caseName + "res: " + JSON.stringify(res));
                if (hasFocus) {
                    let value = await res.attributeValue('componentId');
                    console.info(caseName + "value: " + JSON.stringify(value));
                    if (typeof(value) == 'number') {
                        result = true;
                    }
                    this.publishCaseResult(caseName, result);
                } else {
M
Mupceet 已提交
511
                    if (err && err.code != 0) {
M
Mupceet 已提交
512 513 514 515 516 517 518 519 520 521 522 523
                        console.error(caseName + "err: " + JSON.stringify(err.code));
                        result = true;
                    }
                    if (res == undefined) {
                        result = true;
                    }
                    this.publishCaseResult(caseName, result);
                }
            });
        } else {
            this.context.getFocusElement(false, (err, res) => {
                console.info(caseName + "res: " + JSON.stringify(res));
M
Mupceet 已提交
524
                if (err && err.code != 0) {
M
Mupceet 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
                    console.error(caseName + "err: " + JSON.stringify(err.code));
                    result = true;
                }
                if (res == undefined) {
                    result = true;
                }
                this.publishCaseResult(caseName, result);
            });
        }
    }

    private async getFocusElementByTypePromise(caseName: string, hasFocus: boolean, isAccessibilityFocus: boolean) {
        let result = false;
        if (isAccessibilityFocus) {
            this.context.getFocusElement(true).then(async (res) => {
                console.info(caseName + " res: " + JSON.stringify(res));
                if (hasFocus) {
                    let value = await res.attributeValue('componentId');
                    console.info(caseName + "value: " + JSON.stringify(value));
                    if (typeof(value) == 'number') {
                        result = true;
                    }
                    this.publishCaseResult(caseName, result);
                } else {
                    if (res == undefined) {
                        result = true;
                    }
                    this.publishCaseResult(caseName, result);
                }
            }).catch((err) => {
                console.error(caseName + 'error:' + JSON.stringify(err));
                if (!hasFocus) {
                    result = true;
                }
                this.publishCaseResult(caseName, result);
            });
        } else {
            this.context.getFocusElement(false).then(async (res) => {
                console.info(caseName + " res: " + JSON.stringify(res));
                if (res == undefined) {
                    result = true;
                }
                this.publishCaseResult(caseName, result);
            }).catch((err) => {
                result = true;
                console.error(caseName + 'error:' + JSON.stringify(err));
                this.publishCaseResult(caseName, result);
            });
        }
    }

    private async gestureInjectCallback(caseName: string, gesturePath: object) {
        let result = false;
        if (gesturePath == undefined) {
M
Mupceet 已提交
579
            this.context.injectGesture(gesturePath, (err, res) => {
M
Mupceet 已提交
580
                if (err && err.code != 0) {
M
Mupceet 已提交
581 582 583 584 585 586
                    console.error(caseName + " err: " + JSON.stringify(err.code));
                    result = true;
                }
                this.publishCaseResult(caseName, result);
            });
        } else {
M
Mupceet 已提交
587
            this.context.injectGesture(gesturePath, (err, res) => {
M
Mupceet 已提交
588
                console.info(caseName + " res: " + JSON.stringify(res));
M
Mupceet 已提交
589
                result = true;
M
Mupceet 已提交
590
                if (err && err.code != 0) {
M
Mupceet 已提交
591 592 593 594 595 596 597 598 599 600 601 602
                    console.error(caseName + " err: " + JSON.stringify(err.code));
                    result = false;
                }
                this.publishCaseResult(caseName, result);
            });
        }
    }

 
    private async gestureInjectPromise(caseName: string, gesturePath: object) {
        let result = false;
        if (gesturePath == undefined) {
M
Mupceet 已提交
603
            this.context.injectGesture(gesturePath).then((res) => {
M
Mupceet 已提交
604 605
                this.publishCaseResult(caseName, result);
            }).catch ((err) => {
M
Mupceet 已提交
606 607 608 609
                if (err && err.code != 0) {
                    console.error(caseName + 'error:' + JSON.stringify(err));
                    result = true;
                }
M
Mupceet 已提交
610 611 612
                this.publishCaseResult(caseName, result);
            });
        } else {
M
Mupceet 已提交
613
            this.context.injectGesture(gesturePath).then((res) => {
M
Mupceet 已提交
614
                console.info(caseName + " res: " + JSON.stringify(res));
M
Mupceet 已提交
615
                result = true;
M
Mupceet 已提交
616 617
                this.publishCaseResult(caseName, result);
            }).catch ((err) => {
M
Mupceet 已提交
618 619 620 621
                if (err && err.code != 0) {
                    console.error(caseName + 'error:' + JSON.stringify(err));
                    result = true;
                }
M
Mupceet 已提交
622 623 624 625 626
                this.publishCaseResult(caseName, result);
            });
        }
    }
}