From 8abd7ce945256417baf73f41aaed3f2f23396214 Mon Sep 17 00:00:00 2001 From: chengxingzhen Date: Thu, 23 Mar 2023 12:04:38 +0800 Subject: [PATCH] =?UTF-8?q?xts-getCurrentTopAbility=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chengxingzhen --- .../actsgetabilitystatestagetest/Test.json | 2 +- .../src/main/ets/MainAbility2/MainAbility2.ts | 16 +- .../src/main/ets/MainAbility3/MainAbility3.ts | 19 +- .../src/main/ets/MainAbility4/MainAbility4.ts | 19 +- .../entry/src/main/ets/test/Ability.test.ets | 185 ++++++++++-------- 5 files changed, 146 insertions(+), 95 deletions(-) diff --git a/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/Test.json b/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/Test.json index 14772064d..fd7b80121 100644 --- a/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/Test.json +++ b/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/Test.json @@ -5,7 +5,7 @@ "test-timeout": "600000", "bundle-name": "com.example.actsgetabilitystatestagetest", "module-name": "entry", - "shell-timeout": "600000" + "shell-timeout": "10000" }, "kits": [ { diff --git a/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/entry/src/main/ets/MainAbility2/MainAbility2.ts b/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/entry/src/main/ets/MainAbility2/MainAbility2.ts index f30966b58..0a9d78124 100644 --- a/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/entry/src/main/ets/MainAbility2/MainAbility2.ts +++ b/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/entry/src/main/ets/MainAbility2/MainAbility2.ts @@ -14,6 +14,10 @@ */ import Ability from '@ohos.app.ability.UIAbility' +import commonEvent from '@ohos.commonEvent' +function publishCallBackOne() { + console.log("====MainAbility2 Publish CallBack GetCurrentTopAbility"); +} export default class MainAbility extends Ability { onCreate(want, launchParam) { @@ -27,9 +31,15 @@ export default class MainAbility extends Ability { onWindowStageCreate(windowStage) { // Main window is created, set main page for this ability - console.log("[Demo] MainAbility onWindowStageCreate") - - windowStage.setUIContent(this.context, "pages/index", null) + console.log("[Demo] MainAbility onWindowStageCreate"); + windowStage.setUIContent(this.context, "pages/index", null); + windowStage.on('windowStageEvent', (data) => { + console.log(`ActsGetAbilityStatestAgeTest onWindwoStageMainAbilty2 is : ${JSON.stringify(data)}`); + if (data == 2 ) { + console.log(`ActsGetAbilityStatestAgeTest getWindowStageActiveMainAbility2 is sucess`); + commonEvent.publish("GetCurrentTopAbility", publishCallBackOne); + } + }) } onWindowStageDestroy() { diff --git a/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/entry/src/main/ets/MainAbility3/MainAbility3.ts b/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/entry/src/main/ets/MainAbility3/MainAbility3.ts index 7eb526f21..db7df22fe 100644 --- a/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/entry/src/main/ets/MainAbility3/MainAbility3.ts +++ b/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/entry/src/main/ets/MainAbility3/MainAbility3.ts @@ -1,4 +1,3 @@ -// @ts-nocheck /* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,22 +14,34 @@ */ import Ability from '@ohos.app.ability.UIAbility' +import commonEvent from '@ohos.commonEvent' + +function publishCallBackOne() { + console.log("====MainAbility3 Publish CallBack GetCurrentTopAbility"); +} export default class MainAbility3 extends Ability { onCreate(want, launchParam) { console.log("MainAbility3 onCreate") } - onDestroy(want) { + onDestroy() { // Ability is destroying, release resources for this ability console.log("MainAbility3 onDestroy") } onWindowStageCreate(windowStage) { // Main window is created, set main page for this ability - console.log("MainAbility3 onWindowStageCreate") - windowStage.setUIContent(this.context, "pages/index", null) + console.log("MainAbility3 onWindowStageCreate"); + windowStage.setUIContent(this.context, "pages/index", null); globalThis.ability3Context = this.context; + windowStage.on('windowStageEvent', (data) => { + console.log(`ActsGetAbilityStatestAgeTest onWindwoStageMainAbilty3 is : ${JSON.stringify(data)}`); + if (data == 2 ) { + console.log(`ActsGetAbilityStatestAgeTest getWindowStageActiveMainAbility3 is sucess`); + commonEvent.publish("GetCurrentTopAbility", publishCallBackOne); + } + }) } onWindowStageDestroy() { diff --git a/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/entry/src/main/ets/MainAbility4/MainAbility4.ts b/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/entry/src/main/ets/MainAbility4/MainAbility4.ts index 2ea13cbba..542732edf 100644 --- a/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/entry/src/main/ets/MainAbility4/MainAbility4.ts +++ b/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/entry/src/main/ets/MainAbility4/MainAbility4.ts @@ -1,4 +1,3 @@ -// @ts-nocheck /* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,24 +13,34 @@ * limitations under the License. */ import Ability from '@ohos.app.ability.UIAbility' +import commonEvent from '@ohos.commonEvent' +function publishCallBackOne() { + console.log("====MainAbility4 Publish CallBack GetCurrentTopAbility"); +} export default class MainAbility4 extends Ability { onCreate(want, launchParam) { console.log("MainAbility4 onCreate") } - onDestroy(want) { + onDestroy() { // Ability is destroying, release resources for this ability console.log("MainAbility4 onDestroy") } onWindowStageCreate(windowStage) { // Main window is created, set main page for this ability - console.log("MainAbility4 onWindowStageCreate") - windowStage.setUIContent(this.context, "pages/index", null) + console.log("MainAbility4 onWindowStageCreate"); + windowStage.setUIContent(this.context, "pages/index", null); globalThis.ability4Context = this.context; - //commonEvent.publish("ACTS_GetAppContext", PublishCallBackOne4); + windowStage.on('windowStageEvent', (data) => { + console.log(`ActsGetAbilityStatestAgeTest onWindwoStageMainAbilty4 is : ${JSON.stringify(data)}`); + if (data == 2 ) { + console.log(`ActsGetAbilityStatestAgeTest getWindowStageActiveMainAbility4 is sucess`); + commonEvent.publish("GetCurrentTopAbility", publishCallBackOne); + } + }) } onWindowStageDestroy() { diff --git a/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/entry/src/main/ets/test/Ability.test.ets b/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/entry/src/main/ets/test/Ability.test.ets index 4ea65299c..e744cca43 100644 --- a/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/entry/src/main/ets/test/Ability.test.ets +++ b/ability/ability_runtime/actsqueryfunctiontest/actsgetabilitystatestagetest/entry/src/main/ets/test/Ability.test.ets @@ -13,11 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -// @ts-nocheck import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium' import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry' -const START_ABILITY_TIMEOUT = 4000; +import commonEvent from "@ohos.commonEvent" +let subscriberInfo_MainAbility = { + events: ["GetCurrentTopAbility"], +}; export default function abilityTest() { describe('ActsGetAbilityStateStageTest', function () { @@ -27,31 +29,44 @@ export default function abilityTest() { * @tc.desc : Get the status of the Ability in the BACKGROUND state */ it('ACTS_GetAbilityState_0200', 0, async function (done) { - console.debug('====>start ACTS_GetAbilityState_0200===='); - var flag = true; - var ability; - var abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + console.log('====>start ACTS_GetAbilityState_0200===='); + let ability; + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + let count = 0; + let subscriber; function onAbilityForegroundCallback2(){ - console.debug("====>onAbilityForegroundCallback2===="); - abilityDelegator.getCurrentTopAbility((err, data)=>{ - console.debug("====>getCurrentTopAbility_0200 err:" + JSON.stringify(err) + " data:" + JSON.stringify(data)); + console.log("====>onAbilityForegroundCallback2===="); + count ++; + if (count == 2) { + abilityDelegator.getCurrentTopAbility((err, data)=>{ + console.log("====>getCurrentTopAbility_0200 err:" + JSON.stringify(err) + " data:" + JSON.stringify(data)); ability = data; abilityDelegator.doAbilityBackground(ability, (err)=>{ - console.debug("====>doAbilityBackground_0200 data:" + JSON.stringify(err)); + console.log("====>doAbilityBackground_0200 data:" + JSON.stringify(err)); expect(err.code).assertEqual(0); }) }) + } } function onAbilityBackgroundCallback2(){ - console.debug("====>onAbilityBackgroundCallback2===="); - var state = abilityDelegator.getAbilityState(ability); - console.debug("====>ACTS_GetAbilityState_0200 state:" + state); + console.log("====>onAbilityBackgroundCallback2===="); + let state = abilityDelegator.getAbilityState(ability); + console.log("====>ACTS_GetAbilityState_0200 state:" + state); expect(state).assertEqual(AbilityDelegatorRegistry.AbilityLifecycleState.BACKGROUND); - console.debug("====>ACTS_GetAbilityState_0200 end===="); - flag=false; - done(); + console.log("====>ACTS_GetAbilityState_0200 end===="); + commonEvent.unsubscribe(subscriber, UnSubscribeCallback); } + commonEvent.createSubscriber(subscriberInfo_MainAbility).then(async (data) => { + console.log("ACTS_GetAbilityState_0200====>Create Subscriber====>"); + subscriber = data; + await commonEvent.subscribe(subscriber, onAbilityForegroundCallback2); + }) + + function UnSubscribeCallback() { + console.log("ACTS_GetAbilityState_0200====>UnSubscribe CallBack====>"); + done(); + } abilityDelegator.addAbilityMonitor( {abilityName: 'MainAbility2', @@ -59,24 +74,15 @@ export default function abilityTest() { onAbilityBackground:onAbilityBackgroundCallback2 } ).then(()=>{ - console.debug("====>addAbilityMonitor_0200 finish===="); + console.log("====>addAbilityMonitor_0200 finish===="); globalThis.abilityContext.startAbility( { bundleName: 'com.example.actsgetabilitystatestagetest', abilityName: 'MainAbility2', }, (err)=>{ - console.debug("====>startAbility_0200 err:" + JSON.stringify(err)); + console.log("====>startAbility_0200 err:" + JSON.stringify(err)); }) }) - - setTimeout(()=>{ - if(flag==true){ - console.debug("====>in timeout 0200===="); - expect().assertFail(); - done(); - } - },START_ABILITY_TIMEOUT) - }) /* @@ -85,46 +91,54 @@ export default function abilityTest() { * @tc.desc : Get the status of the Ability in the FOREGROUND state */ it('ACTS_GetAbilityState_0300', 0, async function (done) { - console.debug('====>start ACTS_GetAbilityState_0300===='); - var flag = true; - var ability; - var abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + console.log('====>start ACTS_GetAbilityState_0300===='); + let ability; + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + let count = 0; + let subscriber; function onAbilityForegroundCallback3(){ - console.debug("====>onAbilityForegroundCallback3===="); - abilityDelegator.getCurrentTopAbility((err, data)=>{ - console.debug("====>getCurrentTopAbility_0300 err:" + JSON.stringify(err) + "data:" + JSON.stringify(data)); + console.log("====>onAbilityForegroundCallback3===="); + count++; + if (count == 2) { + abilityDelegator.getCurrentTopAbility((err, data)=>{ + console.log("====>getCurrentTopAbility_0300 err:" + JSON.stringify(err) + "data:" + JSON.stringify(data)); ability = data; - var state = abilityDelegator.getAbilityState(ability); - console.debug("====>ACTS_GetAbilityState_0300 state:" + state); + let state = abilityDelegator.getAbilityState(ability); + console.log("====>ACTS_GetAbilityState_0300 state:" + state); expect(state).assertEqual(AbilityDelegatorRegistry.AbilityLifecycleState.FOREGROUND); - expect(state != AbilityDelegatorRegistry.AbilityLifecycleState.CREATE).assertTrue() - expect(state != AbilityDelegatorRegistry.AbilityLifecycleState.DESTROY).assertTrue() + expect(state != AbilityDelegatorRegistry.AbilityLifecycleState.CREATE).assertTrue(); + expect(state != AbilityDelegatorRegistry.AbilityLifecycleState.DESTROY).assertTrue(); abilityDelegator.doAbilityBackground(ability, (err)=>{ - console.debug("====>doAbilityBackground_0300 data:" + JSON.stringify(err)); + console.log("====>doAbilityBackground_0300 data:" + JSON.stringify(err)); expect(err.code).assertEqual(0); - console.debug("====>ACTS_GetAbilityState_0300 end===="); - flag=false; - done(); + console.log("====>ACTS_GetAbilityState_0300 end===="); + commonEvent.unsubscribe(subscriber, UnSubscribeCallback); }) }) - } - setTimeout(()=>{ - if(flag==true){ - console.debug("====>in timeout 0300===="); - expect().assertFail(); - done(); } - },START_ABILITY_TIMEOUT) + } + + commonEvent.createSubscriber(subscriberInfo_MainAbility).then(async (data) => { + console.log("ACTS_GetAbilityState_0300====>Create Subscriber====>"); + subscriber = data; + await commonEvent.subscribe(subscriber, onAbilityForegroundCallback3); + }) + + function UnSubscribeCallback() { + console.log("ACTS_GetAbilityState_0300====>UnSubscribe CallBack====>"); + done(); + } + abilityDelegator.addAbilityMonitor( {abilityName: 'MainAbility3', onAbilityForeground:onAbilityForegroundCallback3}).then(()=>{ - console.debug("====>addAbilityMonitor_0300 finish===="); + console.log("====>addAbilityMonitor_0300 finish===="); globalThis.abilityContext.startAbility( { bundleName: 'com.example.actsgetabilitystatestagetest', abilityName: 'MainAbility3', }, (err)=>{ - console.debug("====>startAbility_0300 err:" + JSON.stringify(err)); + console.log("====>startAbility_0300 err:" + JSON.stringify(err)); }) }) }) @@ -135,13 +149,13 @@ export default function abilityTest() { * @tc.desc : Get the status of an inactive or nonexistent Ability */ it('ACTS_GetAbilityState_0500', 0, async function (done) { - console.debug('====>start ACTS_GetAbilityState_0500===='); - var ability = Object.create(null); - var abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); - var state = abilityDelegator.getAbilityState(ability); - console.debug("====>ACTS_GetAppState_0500 state:" + state); + console.log('====>start ACTS_GetAbilityState_0500===='); + let ability = Object.create(null); + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + let state = abilityDelegator.getAbilityState(ability); + console.log("====>ACTS_GetAppState_0500 state:" + state); expect(state).assertEqual(undefined); - console.debug("====>ACTS_GetAbilityState_0500 end===="); + console.log("====>ACTS_GetAbilityState_0500 end===="); done(); }) @@ -151,49 +165,56 @@ export default function abilityTest() { * @tc.desc : Get the status of the Ability in the uninitialized state */ it('ACTS_GetAbilityState_0600', 0, async function (done) { - console.debug('====>start ACTS_GetAbilityState_0600====') - var flag = true; - var ability; - var abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + console.log('====>start ACTS_GetAbilityState_0600===='); + let ability; + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + let count = 0; + let subscriber; function onAbilityForegroundCallback6(){ - console.debug("====>onAbilityForegroundCallback6===="); - abilityDelegator.getCurrentTopAbility((err, data)=>{ - console.debug("====>getCurrentTopAbility_0600 err:" + JSON.stringify(err) + "data:" + JSON.stringify(data)); + console.log("====>onAbilityForegroundCallback6===="); + count++; + if (count == 2) { + abilityDelegator.getCurrentTopAbility((err, data)=>{ + console.log("====>getCurrentTopAbility_0600 err:" + JSON.stringify(err) + "data:" + JSON.stringify(data)); ability = data; globalThis.ability4Context.terminateSelf(); - }) + }) + } } function onAbilityDestroyCallback6(){ - console.debug("====>onAbilityDestroyCallback6===="); - var state = abilityDelegator.getAbilityState(ability); - console.debug("====>ACTS_GetAbilityState_0600 data:" + state); - console.debug("====>UNINITIALIZED:"+ AbilityDelegatorRegistry.AbilityLifecycleState.UNINITIALIZED); + console.log("====>onAbilityDestroyCallback6===="); + let state = abilityDelegator.getAbilityState(ability); + console.log("====>ACTS_GetAbilityState_0600 data:" + state); + console.log("====>UNINITIALIZED:"+ AbilityDelegatorRegistry.AbilityLifecycleState.UNINITIALIZED); expect(state).assertEqual(AbilityDelegatorRegistry.AbilityLifecycleState.UNINITIALIZED); - console.debug("====>ACTS_GetAbilityState_0600 end===="); - flag=false; - done(); + console.log("====>ACTS_GetAbilityState_0600 end===="); + commonEvent.unsubscribe(subscriber, UnSubscribeCallback); } - setTimeout(()=>{ - if(flag==true){ - console.debug("====>in timeout 0600==="); - expect().assertFail(); - done(); - } - },START_ABILITY_TIMEOUT) + + commonEvent.createSubscriber(subscriberInfo_MainAbility).then(async (data) => { + console.log("ACTS_GetAbilityState_0600====>Create Subscriber====>"); + subscriber = data; + await commonEvent.subscribe(subscriber, onAbilityForegroundCallback6); + }) + + function UnSubscribeCallback() { + console.log("ACTS_GetAbilityState_0600====>UnSubscribe CallBack====>"); + done(); + } + abilityDelegator.addAbilityMonitor( {abilityName: 'MainAbility4', onAbilityForeground:onAbilityForegroundCallback6, onAbilityDestroy:onAbilityDestroyCallback6}).then(()=>{ - console.debug("====>addAbilityMonitor_0600 finish===="); + console.log("====>addAbilityMonitor_0600 finish===="); globalThis.abilityContext.startAbility( { bundleName: 'com.example.actsgetabilitystatestagetest', abilityName: 'MainAbility4', }, (err)=>{ - console.debug("====>startAbility_0600 err:" + JSON.stringify(err)); + console.log("====>startAbility_0600 err:" + JSON.stringify(err)); }) }) - }) }) } \ No newline at end of file -- GitLab