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 */
15import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'
16import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'
17import prompt from '@ohos.prompt'
18import { BusinessError } from '@ohos.base'
19
20let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator | undefined = undefined
21let abilityDelegator1: AbilityDelegatorRegistry.AbilityDelegator | undefined = undefined
22let cmd:string;
23
24function sleep(time:number) {
25  return new Promise <void>((resolve, reject) => {
26    setTimeout(() => {
27      resolve();
28    }, time)
29  })
30}
31
32export default function abilityTest() {
33  describe('ActsWukongTest',  () =>{
34    beforeAll(async () => {
35      abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
36      abilityDelegator1 = AbilityDelegatorRegistry.getAbilityDelegator();
37      await sleep(2000);
38    })
39
40    afterAll(async  () =>{
41      prompt.showToast({
42        message: 'CASE All End'
43      })
44    })
45
46    /**
47     * @tc.number: wukongtest_0010
48     * @tc.name: getAbilityLists callback API.
49     * @tc.desc: getAbilityLists callback API abilityType is spoken, stateType is install.
50     */
51    it('wukongtest_0010', 1, async  ()=>{
52      cmd = "wukong exec -T 0.5 &";
53      if(abilityDelegator != undefined){
54        await abilityDelegator.executeShellCommand("pkill accessibility");
55        abilityDelegator.executeShellCommand(cmd,
56          async (err: BusinessError, d) => {
57            console.log('ACTS_wukongTest executeShellCommand : err : ' + JSON.stringify(err));
58            console.log('ACTS_wukongTest executeShellCommand : stdResult : ' + d.stdResult);
59            console.log('ACTS_wukongTest executeShellCommand : exitCode : ' + d.exitCode);
60          });
61      }
62      await sleep(5000);
63      if(abilityDelegator1 != undefined){
64        abilityDelegator1.executeShellCommand("ps -ef|grep 'wukong exec -T 0.5'|grep -c -v grep ", async (error: BusinessError, date) => {
65          console.log('ACTS_wukongTest cmdPs executeShellCommand : err : ' + JSON.stringify(error));
66          console.log('ACTS_wukongTest cmdPs executeShellCommand : stdResult : ' + date.stdResult);
67          console.log('ACTS_wukongTest cmdPs executeShellCommand : exitCode : ' + date.exitCode);
68          let i:number = date.stdResult.indexOf('1');
69          if (i == -1) {
70            expect().assertFail();
71          }
72        });
73      }
74    })
75  })
76}
77