/*
 * Copyright (c) 2025 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 AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
import { describe, it, expect } from '@ohos/hypium';
import { Driver, ON } from '@ohos.UiTest';
import { hilog } from '@kit.PerformanceAnalysisKit';

const TAG = '[Sample_ArkTSInputConsumerAPI]';
const DOMAIN = 0xF811;
const BUNDLE = 'ArkTSInputConsumerAPI_';


export default function abilityTest() {
  describe('ActsAbilityTest', () => {
    /**
     * 打开应用
     */
    it('ArkTSInputConsumerAPI_StartAbility_001', 0, async (done: Function) => {
      hilog.info(DOMAIN, TAG, BUNDLE + 'StartAbility_001, begin');
      let driver = Driver.create();
      let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
      try {
        await abilityDelegator.startAbility({
          bundleName: 'com.example.arktsinputconsumer',
          abilityName: 'EntryAbility'
        });
      } catch (exception) {
        hilog.info(DOMAIN, TAG, BUNDLE + `StartAbility_001 exception = ${JSON.stringify(exception)}`);
        expect().assertFail();
      }
      await driver.delayMs(1000);
      await driver.assertComponentExist(ON.text('Add monitoring for Volume Up key'));
      await driver.assertComponentExist(ON.text('Remove monitoring for Volume Up key'));
      await driver.assertComponentExist(ON.text('Add monitoring for Volume Down key'));
      await driver.assertComponentExist(ON.text('Remove monitoring for Volume Down key'));
      done();
      hilog.info(DOMAIN, TAG, BUNDLE + 'StartAbility_001 end')
    })

    /**
     * 取消音量上键的监听
     */
    it('ArkTSInputConsumerAPI_off_001', 0, async (done: Function) => {
      hilog.info(DOMAIN, TAG, BUNDLE + 'CancelMonitor_001, begin');
      let driver = Driver.create();
      let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
      try {
        await abilityDelegator.startAbility({
          bundleName: 'com.example.arktsinputconsumer',
          abilityName: 'EntryAbility'
        });
      } catch (exception) {
        hilog.info(DOMAIN, TAG, BUNDLE + `CancelMonitor_001 exception = ${JSON.stringify(exception)}`);
        expect().assertFail();
      }
      await driver.delayMs(1000);
      await driver.assertComponentExist(ON.text('Remove monitoring for Volume Up key'));
      let button = await driver.findComponent(ON.text('Remove monitoring for Volume Up key'));
      await button.click();
      await driver.delayMs(1000);
      done();
      hilog.info(DOMAIN, TAG, BUNDLE + 'CancelMonitor_001 end')
    })

    /**
     * 取消音量下键的监听
     */
    it('ArkTSInputConsumerAPI_off_002', 0, async (done: Function) => {
      hilog.info(DOMAIN, TAG, BUNDLE + 'CancelMonitor_002, begin');
      let driver = Driver.create();
      let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
      try {
        await abilityDelegator.startAbility({
          bundleName: 'com.example.arktsinputconsumer',
          abilityName: 'EntryAbility'
        });
      } catch (exception) {
        hilog.info(DOMAIN, TAG, BUNDLE + `CancelMonitor_001 exception = ${JSON.stringify(exception)}`);
        expect().assertFail();
      }
      await driver.delayMs(1000);
      await driver.assertComponentExist(ON.text('Remove monitoring for Volume Down key'));
      let button = await driver.findComponent(ON.text('Remove monitoring for Volume Down key'));
      await button.click();
      await driver.delayMs(1000);
      done();
      hilog.info(DOMAIN, TAG, BUNDLE + 'CancelMonitor_002 end')
    })

    /**
     * 添加音量下键的监听
     */
    it('ArkTSInputConsumerAPI_on_001', 0, async (done: Function) => {
      hilog.info(DOMAIN, TAG, BUNDLE + 'AddMonitor_001, begin');
      let driver = Driver.create();
      let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
      try {
        await abilityDelegator.startAbility({
          bundleName: 'com.example.arktsinputconsumer',
          abilityName: 'EntryAbility'
        });
      } catch (exception) {
        hilog.info(DOMAIN, TAG, BUNDLE + `CancelMonitor_001 exception = ${JSON.stringify(exception)}`);
        expect().assertFail();
      }
      await driver.delayMs(1000);
      await driver.assertComponentExist(ON.text('Add monitoring for Volume Up key'));
      let button = await driver.findComponent(ON.text('Add monitoring for Volume Up key'));
      await button.click();
      await driver.delayMs(1000);
      done();
      hilog.info(DOMAIN, TAG, BUNDLE + 'AddMonitor_002 end')
    })

    /**
     * 添加音量下键的监听
     */
    it('ArkTSInputConsumerAPI_on_002', 0, async (done: Function) => {
      hilog.info(DOMAIN, TAG, BUNDLE + 'AddMonitor_002, begin');
      let driver = Driver.create();
      let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
      try {
        await abilityDelegator.startAbility({
          bundleName: 'com.example.arktsinputconsumer',
          abilityName: 'EntryAbility'
        });
      } catch (exception) {
        hilog.info(DOMAIN, TAG, BUNDLE + `CancelMonitor_001 exception = ${JSON.stringify(exception)}`);
        expect().assertFail();
      }
      await driver.delayMs(1000);
      await driver.assertComponentExist(ON.text('Add monitoring for Volume Down key'));
      let button = await driver.findComponent(ON.text('Add monitoring for Volume Down key'));
      await button.click();
      await driver.delayMs(1000);
      done();
      hilog.info(DOMAIN, TAG, BUNDLE + 'AddMonitor_002 end')
    })
  })
}