From abbb616d8949992824e6cbce5c18275ec22e40c0 Mon Sep 17 00:00:00 2001 From: chengxingzhen Date: Fri, 29 Jul 2022 17:30:40 +0800 Subject: [PATCH] =?UTF-8?q?XTS=E5=85=83=E8=83=BD=E5=8A=9B=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chengxingzhen --- .../src/main/ets/MainAbility/MainAbility.ts | 5 - .../src/main/ets/MainAbility/MainAbility.ts | 7 + .../src/main/ets/MainAbility/MainAbility.ts | 26 ++- .../entry/src/main/ets/test/Ability.test.ets | 181 ++++++++---------- 4 files changed, 112 insertions(+), 107 deletions(-) diff --git a/ability/ability_runtime/newwant/actsnewwantarelyhap/entry/src/main/ets/MainAbility/MainAbility.ts b/ability/ability_runtime/newwant/actsnewwantarelyhap/entry/src/main/ets/MainAbility/MainAbility.ts index 0389de11e..950088fc2 100644 --- a/ability/ability_runtime/newwant/actsnewwantarelyhap/entry/src/main/ets/MainAbility/MainAbility.ts +++ b/ability/ability_runtime/newwant/actsnewwantarelyhap/entry/src/main/ets/MainAbility/MainAbility.ts @@ -89,11 +89,6 @@ export default class MainAbility extends Ability { console.log("ACTS_NewWant Publish CallBack onNewWantMain_To_Test_CommonEvent") }); switch (want.action) { - case 'restartHapB': - commonEvent.publish("onNewWantMain1_To_Test_CommonEvent", publishData, () => { - console.log("ACTS_NewWant Publish CallBack onNewWantMain1_To_Test_CommonEvent") - }); - break; case 'restartHapC': commonEvent.publish("onNewWantMain2_To_Test_CommonEvent", publishData, () => { console.log("ACTS_NewWant Publish CallBack onNewWantMain2_To_Test_CommonEvent") diff --git a/ability/ability_runtime/newwant/actsnewwantbrelyhap/entry/src/main/ets/MainAbility/MainAbility.ts b/ability/ability_runtime/newwant/actsnewwantbrelyhap/entry/src/main/ets/MainAbility/MainAbility.ts index 737b598be..70dd29e22 100644 --- a/ability/ability_runtime/newwant/actsnewwantbrelyhap/entry/src/main/ets/MainAbility/MainAbility.ts +++ b/ability/ability_runtime/newwant/actsnewwantbrelyhap/entry/src/main/ets/MainAbility/MainAbility.ts @@ -88,5 +88,12 @@ export default class MainAbility extends Ability { commonEvent.publish("onNewWantMain_To_Test_CommonEvent", publishData, () => { console.log("ACTS_NewWant Publish CallBack onNewWantMain_To_Test_CommonEvent") }); + switch (want.action) { + case 'restartHapB': + commonEvent.publish("onNewWantMain1_To_Test_CommonEvent", publishData, () => { + console.log("ACTS_NewWant Publish CallBack onNewWantMain1_To_Test_CommonEvent") + }); + break; + } } }; diff --git a/ability/ability_runtime/newwant/actsnewwantrelyhap/entry/src/main/ets/MainAbility/MainAbility.ts b/ability/ability_runtime/newwant/actsnewwantrelyhap/entry/src/main/ets/MainAbility/MainAbility.ts index 4ec9dbc6a..8dbd22aa9 100644 --- a/ability/ability_runtime/newwant/actsnewwantrelyhap/entry/src/main/ets/MainAbility/MainAbility.ts +++ b/ability/ability_runtime/newwant/actsnewwantrelyhap/entry/src/main/ets/MainAbility/MainAbility.ts @@ -80,6 +80,16 @@ export default class MainAbility extends Ability { + JSON.stringify(error) + ", " + JSON.stringify(data)) }) } + if (globalThis.abilityWant.action == 'startMainAbility0800') { + globalThis.abilityContext.startAbility({ + bundleName: "com.example.newwanthap", + abilityName: "com.example.newwanthapa.SecondAbility", + action: "startSecondAbility0800" + }, (error, data) => { + console.log('ACTS_NewWant MainAbility onForeground - startAbility startServiceAbility: ' + + JSON.stringify(error) + ", " + JSON.stringify(data)) + }) + } commonEvent.publish("onForegroundMain_To_Test_CommonEvent", () => { console.log("ACTS_NewWant MainAbility Publish CallBack onForegroundMain_To_Test_CommonEvent") }); @@ -102,9 +112,23 @@ export default class MainAbility extends Ability { console.log("ACTS_NewWant MainAbility onNewWant type :" + want.type) console.log("ACTS_NewWant MainAbility onNewWant flags :" + want.flags) console.log("ACTS_NewWant MainAbility onNewWant action :" + want.action) - + var publishData = { + data: want.action + } commonEvent.publish("onNewWantMain_To_Test_CommonEvent", () => { console.log("ACTS_NewWant MainAbility Publish CallBack onNewWantMain_To_Test_CommonEvent") }); + switch (want.action) { + case 'restartHapB': + commonEvent.publish("onNewWantSecond1_To_Test_CommonEvent", publishData,() => { + console.log("ACTS_NewWant SecondAbility Publish CallBack onNewWantSecond1_To_Test_CommonEvent") + }); + break; + case 'restartSecondAbility0700': + commonEvent.publish("onNewWantSecond2_To_Test_CommonEvent", publishData,() => { + console.log("ACTS_NewWant SecondAbility Publish CallBack onNewWantSecond2_To_Test_CommonEvent") + }); + break; + } } }; diff --git a/ability/ability_runtime/newwant/actsnewwanttest/entry/src/main/ets/test/Ability.test.ets b/ability/ability_runtime/newwant/actsnewwanttest/entry/src/main/ets/test/Ability.test.ets index 0887c61ed..9c9d001cd 100644 --- a/ability/ability_runtime/newwant/actsnewwanttest/entry/src/main/ets/test/Ability.test.ets +++ b/ability/ability_runtime/newwant/actsnewwanttest/entry/src/main/ets/test/Ability.test.ets @@ -16,7 +16,7 @@ import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from "deccjsunit/index" import commonEvent from '@ohos.commonEvent' -var subscriberInfo = { +let subscriberInfo = { events: ['onCreateMain_To_Test_CommonEvent', 'onWindowStageCreateMain_To_Test_CommonEvent', 'onForegroundMain_To_Test_CommonEvent', @@ -33,6 +33,8 @@ var subscriberInfo = { ], }; +let flagNewWant = false; + export default function abilityTest(abilityContext) { describe('ActsNewWantTest', function () { @@ -43,13 +45,13 @@ export default function abilityTest(abilityContext) { */ it('ACTS_NewWant_Test_0100', 0, async function (done) { console.log("ACTS_NewWant_Test_0100 --- start") - var Subscriber; - var flagOnCreate = 0; - var flagOnWindowStageCreate = 0; - var flagOnForeground = 0; - var flagOnNewWant = 0; + let Subscriber; + let flagOnCreate = 0; + let flagOnWindowStageCreate = 0; + let flagOnForeground = 0; + let flagOnNewWant = 0; - commonEvent.createSubscriber(subscriberInfo).then(async (data) => { + await commonEvent.createSubscriber(subscriberInfo).then(async (data) => { console.debug("ACTS_NewWant_Test_0100====>Create Subscriber====>"); Subscriber = data; await commonEvent.subscribe(Subscriber, SubscribeCallBack); @@ -94,17 +96,14 @@ export default function abilityTest(abilityContext) { + flagOnNewWant) if (flagOnCreate == 1 && flagOnWindowStageCreate == 1 && flagOnForeground == 1 && flagOnNewWant == 0) { - expect(flagOnCreate).assertEqual(1); - expect(flagOnWindowStageCreate).assertEqual(1); - expect(flagOnForeground).assertEqual(1); - expect(flagOnNewWant).assertEqual(0); - commonEvent.unsubscribe(Subscriber, UnSubscribeCallback); + flagNewWant = true; + commonEvent.unsubscribe(Subscriber, UnSubscribeCallback); } - done() } function UnSubscribeCallback() { console.debug("ACTS_NewWant_Test_0100====>UnSubscribe CallBack====>"); + expect(flagNewWant).assertTrue(); done(); } }) @@ -116,13 +115,13 @@ export default function abilityTest(abilityContext) { */ it('ACTS_NewWant_Test_0200', 0, async function (done) { console.log("ACTS_NewWant_Test_0200 --- start") - var Subscriber; - var flagOnCreate = 0; - var flagOnWindowStageCreate = 0; - var flagOnForeground = 0; - var flagOnNewWant = 0; + let Subscriber; + let flagOnCreate = 0; + let flagOnWindowStageCreate = 0; + let flagOnForeground = 0; + let flagOnNewWant = 0; - commonEvent.createSubscriber(subscriberInfo).then(async (data) => { + await commonEvent.createSubscriber(subscriberInfo).then(async (data) => { console.debug("ACTS_NewWant_Test_0200====>Create Subscriber====>"); Subscriber = data; await commonEvent.subscribe(Subscriber, SubscribeCallBack); @@ -166,17 +165,14 @@ export default function abilityTest(abilityContext) { + flagOnNewWant) if (flagOnCreate == 1 && flagOnWindowStageCreate == 1 && flagOnForeground == 1 && flagOnNewWant == 0) { - expect(flagOnCreate).assertEqual(1); - expect(flagOnWindowStageCreate).assertEqual(1); - expect(flagOnForeground).assertEqual(1); - expect(flagOnNewWant).assertEqual(0); - commonEvent.unsubscribe(Subscriber, UnSubscribeCallback); + flagNewWant = true; + commonEvent.unsubscribe(Subscriber, UnSubscribeCallback); } - done() } function UnSubscribeCallback() { console.debug("ACTS_NewWant_Test_0200====>UnSubscribe CallBack====>"); + expect(flagNewWant).assertTrue(); done(); } }) @@ -188,13 +184,13 @@ export default function abilityTest(abilityContext) { */ it('ACTS_NewWant_Test_0300', 0, async function (done) { console.log("ACTS_NewWant_Test_0300 --- start") - var Subscriber; - var flagOnCreate = 0; - var flagOnWindowStageCreate = 0; - var flagOnForeground = 0; - var flagOnNewWant = 0; + let Subscriber; + let flagOnCreate = 0; + let flagOnWindowStageCreate = 0; + let flagOnForeground = 0; + let flagOnNewWant = 0; - commonEvent.createSubscriber(subscriberInfo).then(async (data) => { + await commonEvent.createSubscriber(subscriberInfo).then(async (data) => { console.debug("ACTS_NewWant_Test_0300====>Create Subscriber====>"); Subscriber = data; await commonEvent.subscribe(Subscriber, SubscribeCallBack); @@ -240,15 +236,13 @@ export default function abilityTest(abilityContext) { if (flagOnCreate == 2 && flagOnWindowStageCreate == 2 && flagOnForeground == 2 && flagOnNewWant == 0) { - expect(flagOnCreate).assertEqual(2); - expect(flagOnWindowStageCreate).assertEqual(2); - expect(flagOnForeground).assertEqual(2); - expect(flagOnNewWant).assertEqual(0); - commonEvent.unsubscribe(Subscriber, UnSubscribeCallback); + flagNewWant = true; + commonEvent.unsubscribe(Subscriber, UnSubscribeCallback); } } function UnSubscribeCallback() { console.debug("ACTS_NewWant_Test_0300====>UnSubscribe CallBack====>"); + expect(flagNewWant).assertTrue(); done(); } }) @@ -260,13 +254,13 @@ export default function abilityTest(abilityContext) { */ it('ACTS_NewWant_Test_0400', 0, async function (done) { console.log("ACTS_NewWant_Test_0400 --- start") - var Subscriber; - var flagOnCreate = 0; - var flagOnWindowStageCreate = 0; - var flagOnForeground = 0; - var flagOnNewWant = 0; + let Subscriber; + let flagOnCreate = 0; + let flagOnWindowStageCreate = 0; + let flagOnForeground = 0; + let flagOnNewWant = 0; - commonEvent.createSubscriber(subscriberInfo).then(async (data) => { + await commonEvent.createSubscriber(subscriberInfo).then(async (data) => { console.debug("ACTS_NewWant_Test_0400====>Create Subscriber====>"); Subscriber = data; await commonEvent.subscribe(Subscriber, SubscribeCallBack); @@ -311,19 +305,16 @@ export default function abilityTest(abilityContext) { console.debug("ACTS_NewWant_Test_0400====>flagOnNewWant:====>" + flagOnNewWant) - if (flagOnCreate == 1 && flagOnWindowStageCreate == 1 + if (flagOnCreate == 0 && flagOnWindowStageCreate == 0 && flagOnForeground == 2 && flagOnNewWant == 1) { - expect(flagOnCreate).assertEqual(1); - expect(flagOnWindowStageCreate).assertEqual(1); - expect(flagOnForeground).assertEqual(2); - expect(flagOnNewWant).assertEqual(1); - commonEvent.unsubscribe(Subscriber, UnSubscribeCallback); + flagNewWant = true; + commonEvent.unsubscribe(Subscriber, UnSubscribeCallback); } - done() } function UnSubscribeCallback() { console.debug("ACTS_NewWant_Test_0400====>UnSubscribe CallBack====>"); + expect(flagNewWant).assertTrue(); done(); } }) @@ -335,13 +326,13 @@ export default function abilityTest(abilityContext) { */ it('ACTS_NewWant_Test_0500', 0, async function (done) { console.log("ACTS_NewWant_Test_0500 --- start") - var Subscriber; - var flagOnCreate = 0; - var flagOnWindowStageCreate = 0; - var flagOnForeground = 0; - var flagOnNewWant = 0; + let Subscriber; + let flagOnCreate = 0; + let flagOnWindowStageCreate = 0; + let flagOnForeground = 0; + let flagOnNewWant = 0; - commonEvent.createSubscriber(subscriberInfo).then(async (data) => { + await commonEvent.createSubscriber(subscriberInfo).then(async (data) => { console.debug("ACTS_NewWant_Test_0500====>Create Subscriber====>"); Subscriber = data; await commonEvent.subscribe(Subscriber, SubscribeCallBack); @@ -388,17 +379,14 @@ export default function abilityTest(abilityContext) { if (flagOnCreate == 1 && flagOnWindowStageCreate == 1 && flagOnForeground == 2 && flagOnNewWant == 1) { - expect(flagOnCreate).assertEqual(1); - expect(flagOnWindowStageCreate).assertEqual(1); - expect(flagOnForeground).assertEqual(2); - expect(flagOnNewWant).assertEqual(1); - commonEvent.unsubscribe(Subscriber, UnSubscribeCallback); + flagNewWant = true; + commonEvent.unsubscribe(Subscriber, UnSubscribeCallback); } - done() } function UnSubscribeCallback() { console.debug("ACTS_NewWant_Test_0500====>UnSubscribe CallBack====>"); + expect(flagNewWant).assertTrue(); done(); } }) @@ -410,13 +398,13 @@ export default function abilityTest(abilityContext) { */ it('ACTS_NewWant_Test_0600', 0, async function (done) { console.log("ACTS_NewWant_Test_0600 --- start") - var Subscriber; - var flagOnCreate = 0; - var flagOnWindowStageCreate = 0; - var flagOnForeground = 0; - var flagOnNewWant = 0; + let Subscriber; + let flagOnCreate = 0; + let flagOnWindowStageCreate = 0; + let flagOnForeground = 0; + let flagOnNewWant = 0; - commonEvent.createSubscriber(subscriberInfo).then(async (data) => { + await commonEvent.createSubscriber(subscriberInfo).then(async (data) => { console.debug("ACTS_NewWant_Test_0600====>Create Subscriber====>"); Subscriber = data; await commonEvent.subscribe(Subscriber, SubscribeCallBack); @@ -463,17 +451,14 @@ export default function abilityTest(abilityContext) { if (flagOnCreate == 1 && flagOnWindowStageCreate == 1 && flagOnForeground == 2 && flagOnNewWant == 1) { - expect(flagOnCreate).assertEqual(1); - expect(flagOnWindowStageCreate).assertEqual(1); - expect(flagOnForeground).assertEqual(2); - expect(flagOnNewWant).assertEqual(1); - commonEvent.unsubscribe(Subscriber, UnSubscribeCallback); + flagNewWant = true; + commonEvent.unsubscribe(Subscriber, UnSubscribeCallback); } - done() } function UnSubscribeCallback() { console.debug("ACTS_NewWant_Test_0600====>UnSubscribe CallBack====>"); + expect(flagNewWant).assertTrue(); done(); } }) @@ -485,13 +470,13 @@ export default function abilityTest(abilityContext) { */ it('ACTS_NewWant_Test_0700', 0, async function (done) { console.log("ACTS_NewWant_Test_0700 --- start") - var Subscriber; - var flagOnCreate = 0; - var flagOnWindowStageCreate = 0; - var flagOnForeground = 0; - var flagOnNewWant = 0; + let Subscriber; + let flagOnCreate = 0; + let flagOnWindowStageCreate = 0; + let flagOnForeground = 0; + let flagOnNewWant = 0; - commonEvent.createSubscriber(subscriberInfo).then(async (data) => { + await commonEvent.createSubscriber(subscriberInfo).then(async (data) => { console.debug("ACTS_NewWant_Test_0700====>Create Subscriber====>"); Subscriber = data; await commonEvent.subscribe(Subscriber, SubscribeCallBack); @@ -536,18 +521,15 @@ export default function abilityTest(abilityContext) { console.debug("ACTS_NewWant_Test_0700====>flagOnNewWant:====>" + flagOnNewWant) - if (flagOnCreate == 1 && flagOnWindowStageCreate == 1 + if (flagOnCreate == 0 && flagOnWindowStageCreate == 0 && flagOnForeground == 2 && flagOnNewWant == 1) { - expect(flagOnCreate).assertEqual(1); - expect(flagOnWindowStageCreate).assertEqual(1); - expect(flagOnForeground).assertEqual(2); - expect(flagOnNewWant).assertEqual(1); - commonEvent.unsubscribe(Subscriber, UnSubscribeCallback); + flagNewWant = true; + commonEvent.unsubscribe(Subscriber, UnSubscribeCallback); } - done() } function UnSubscribeCallback() { console.debug("ACTS_NewWant_Test_0700====>UnSubscribe CallBack====>"); + expect(flagNewWant).assertTrue(); done(); } }) @@ -559,13 +541,13 @@ export default function abilityTest(abilityContext) { */ it('ACTS_NewWant_Test_0800', 0, async function (done) { console.log("ACTS_NewWant_Test_0800 --- start") - var Subscriber; - var flagOnCreate = 0; - var flagOnWindowStageCreate = 0; - var flagOnForeground = 0; - var flagOnNewWant = 0; + let Subscriber; + let flagOnCreate = 0; + let flagOnWindowStageCreate = 0; + let flagOnForeground = 0; + let flagOnNewWant = 0; - commonEvent.createSubscriber(subscriberInfo).then(async (data) => { + await commonEvent.createSubscriber(subscriberInfo).then(async (data) => { console.debug("ACTS_NewWant_Test_0800====>Create Subscriber====>"); Subscriber = data; await commonEvent.subscribe(Subscriber, SubscribeCallBack); @@ -573,8 +555,8 @@ export default function abilityTest(abilityContext) { abilityContext.startAbility({ bundleName: "com.example.newwanthap", - abilityName: "com.example.newwanthapa.SecondAbility", - action: "startSecondAbility0800" + abilityName: "com.example.newwanthapa.MainAbility", + action: "startMainAbility0800" }, (error, data) => { console.log('ACTS_NewWant_Test_0800 - startAbility start SecondAbility: ' + JSON.stringify(error) + ", " + JSON.stringify(data)) @@ -611,18 +593,15 @@ export default function abilityTest(abilityContext) { console.debug("ACTS_NewWant_Test_0800====>flagOnNewWant:====>" + flagOnNewWant) - if (flagOnCreate == 1 && flagOnWindowStageCreate == 1 + if (flagOnCreate == 0 && flagOnWindowStageCreate == 0 && flagOnForeground == 2 && flagOnNewWant == 1) { - expect(flagOnCreate).assertEqual(1); - expect(flagOnWindowStageCreate).assertEqual(1); - expect(flagOnForeground).assertEqual(2); - expect(flagOnNewWant).assertEqual(1); - commonEvent.unsubscribe(Subscriber, UnSubscribeCallback); + flagNewWant = true; + commonEvent.unsubscribe(Subscriber, UnSubscribeCallback); } - done() } function UnSubscribeCallback() { console.debug("ACTS_NewWant_Test_0800====>UnSubscribe CallBack====>"); + expect(flagNewWant).assertTrue(); done(); } }) -- GitLab