From 5e50b45ab1712f5a2b28b70bed92e26aaf47d485 Mon Sep 17 00:00:00 2001 From: jiyong_sd Date: Mon, 1 Aug 2022 19:02:17 +0800 Subject: [PATCH] Adapt to the new framework Signed-off-by: jiyong_sd Change-Id: Ie6977c74e4eff60e32b3abb6510e20a3cf2fccc4 --- graphic/windowStage/Test.json | 8 +- .../src/main/ets/MainAbility/MainAbility.ts | 2 +- .../{ => MainAbility}/pages/index/index.ets | 30 +++---- .../{ => MainAbility}/pages/second/second.ets | 0 .../src/main/ets/TestAbility/TestAbility.ts | 50 ++++++++++++ .../src/main/ets/TestAbility/pages/index.ets | 48 ++++++++++++ .../ets/TestRunner/OpenHarmonyTestRunner.ts | 78 +++++++++++++++++++ .../entry/src/main/ets/test/List.test.ets | 6 +- .../src/main/ets/test/windowCallback.test.ets | 2 +- .../src/main/ets/test/windowPromise.test.ets | 4 +- .../resources/base/profile/main_pages.json | 4 +- 11 files changed, 200 insertions(+), 32 deletions(-) rename graphic/windowStage/entry/src/main/ets/{ => MainAbility}/pages/index/index.ets (58%) rename graphic/windowStage/entry/src/main/ets/{ => MainAbility}/pages/second/second.ets (100%) create mode 100644 graphic/windowStage/entry/src/main/ets/TestAbility/TestAbility.ts create mode 100644 graphic/windowStage/entry/src/main/ets/TestAbility/pages/index.ets create mode 100644 graphic/windowStage/entry/src/main/ets/TestRunner/OpenHarmonyTestRunner.ts diff --git a/graphic/windowStage/Test.json b/graphic/windowStage/Test.json index a253b89d3..af8f71702 100644 --- a/graphic/windowStage/Test.json +++ b/graphic/windowStage/Test.json @@ -1,10 +1,12 @@ { "description": "Configuration for hjunit demo Tests", "driver": { - "type": "JSUnitTest", + "type": "OHJSUnitTest", "test-timeout": "180000", - "package": "com.example.myapplication", - "shell-timeout": "600000" + "bundle-name": "com.example.myapplication", + "module-name": "phone", + "shell-timeout": "600000", + "testcase-timeout": 70000 }, "kits": [{ "test-file-name": [ diff --git a/graphic/windowStage/entry/src/main/ets/MainAbility/MainAbility.ts b/graphic/windowStage/entry/src/main/ets/MainAbility/MainAbility.ts index ed9de5ac3..a0946c604 100644 --- a/graphic/windowStage/entry/src/main/ets/MainAbility/MainAbility.ts +++ b/graphic/windowStage/entry/src/main/ets/MainAbility/MainAbility.ts @@ -18,7 +18,7 @@ export default class MainAbility extends Ability { globalThis.windowStage = windowStage globalThis.abilityStorage = this.storage globalThis.abilityContext = this.context - windowStage.setUIContent(this.context, "pages/index/index", null) + windowStage.setUIContent(this.context, "MainAbility/pages/index/index", null) } onWindowStageDestroy() { diff --git a/graphic/windowStage/entry/src/main/ets/pages/index/index.ets b/graphic/windowStage/entry/src/main/ets/MainAbility/pages/index/index.ets similarity index 58% rename from graphic/windowStage/entry/src/main/ets/pages/index/index.ets rename to graphic/windowStage/entry/src/main/ets/MainAbility/pages/index/index.ets index a35d71438..97504e244 100644 --- a/graphic/windowStage/entry/src/main/ets/pages/index/index.ets +++ b/graphic/windowStage/entry/src/main/ets/MainAbility/pages/index/index.ets @@ -12,10 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import file from '@system.file'; - -import {Core, ExpectExtend, InstrumentLog, ReportExtend} from "deccjsunit/index.ets" -import testsuite from "../../test/List.test.ets" +import router from '@ohos.router'; +import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry' +import { Hypium } from '@ohos/hypium' +import testsuite from '../../../test/List.test' @Entry @@ -24,22 +24,12 @@ struct Index { aboutToAppear(){ console.info("start run testcase!!!!") - const core = Core.getInstance() - const expectExtend = new ExpectExtend({ - 'id': 'extend' - }) - core.addService('expect', expectExtend) - const reportExtend = new ReportExtend(file) - - core.addService('report', reportExtend) - core.init() - core.subscribeEvent('task', reportExtend) - const configService = core.getDefaultService('config') - console.info('parameters---->' + JSON.stringify(globalThis.abilityWant.parameters)) - globalThis.abilityWant.parameters.timeout = 70000; - configService.setConfig(globalThis.abilityWant.parameters) - testsuite(globalThis.abilityContext,globalThis.windowStage,globalThis.abilityStorage) - core.execute() + var abilityDelegator: any + abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() + var abilityDelegatorArguments: any + abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() + console.info('start run testcase!!!') + Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite) } build() { diff --git a/graphic/windowStage/entry/src/main/ets/pages/second/second.ets b/graphic/windowStage/entry/src/main/ets/MainAbility/pages/second/second.ets similarity index 100% rename from graphic/windowStage/entry/src/main/ets/pages/second/second.ets rename to graphic/windowStage/entry/src/main/ets/MainAbility/pages/second/second.ets diff --git a/graphic/windowStage/entry/src/main/ets/TestAbility/TestAbility.ts b/graphic/windowStage/entry/src/main/ets/TestAbility/TestAbility.ts new file mode 100644 index 000000000..89a847305 --- /dev/null +++ b/graphic/windowStage/entry/src/main/ets/TestAbility/TestAbility.ts @@ -0,0 +1,50 @@ +/* + * 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 Ability from '@ohos.application.Ability' + +export default class TestAbility extends Ability { + onCreate(want, launchParam) { + console.log('TestAbility onCreate') + } + + onDestroy() { + console.log('TestAbility onDestroy') + } + + onWindowStageCreate(windowStage) { + console.log('TestAbility onWindowStageCreate') + windowStage.loadContent("TestAbility/pages/index", (err, data) => { + if (err.code) { + console.error('Failed to load the content. Cause:' + JSON.stringify(err)); + return; + } + console.info('Succeeded in loading the content. Data: ' + JSON.stringify(data)) + }); + + globalThis.abilityContext = this.context; + } + + onWindowStageDestroy() { + console.log('TestAbility onWindowStageDestroy') + } + + onForeground() { + console.log('TestAbility onForeground') + } + + onBackground() { + console.log('TestAbility onBackground') + } +}; \ No newline at end of file diff --git a/graphic/windowStage/entry/src/main/ets/TestAbility/pages/index.ets b/graphic/windowStage/entry/src/main/ets/TestAbility/pages/index.ets new file mode 100644 index 000000000..b93567f96 --- /dev/null +++ b/graphic/windowStage/entry/src/main/ets/TestAbility/pages/index.ets @@ -0,0 +1,48 @@ +/* + * 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 router from '@ohos.router'; + +@Entry +@Component +struct Index { + aboutToAppear() { + console.info('TestAbility index aboutToAppear') + } + @State message: string = 'Hello World' + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + Button() { + Text('next page') + .fontSize(20) + .fontWeight(FontWeight.Bold) + }.type(ButtonType.Capsule) + .margin({ + top: 20 + }) + .backgroundColor('#0D9FFB') + .width('35%') + .height('5%') + .onClick(()=>{ + }) + } + .width('100%') + } + .height('100%') + } + } \ No newline at end of file diff --git a/graphic/windowStage/entry/src/main/ets/TestRunner/OpenHarmonyTestRunner.ts b/graphic/windowStage/entry/src/main/ets/TestRunner/OpenHarmonyTestRunner.ts new file mode 100644 index 000000000..a4ee2f165 --- /dev/null +++ b/graphic/windowStage/entry/src/main/ets/TestRunner/OpenHarmonyTestRunner.ts @@ -0,0 +1,78 @@ +/* + * 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 TestRunner from '@ohos.application.testRunner' +import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry' + +var abilityDelegator = undefined +var abilityDelegatorArguments = undefined + +function translateParamsToString(parameters) { + const keySet = new Set([ + '-s class', '-s notClass', '-s suite', '-s it', + '-s level', '-s testType', '-s size', '-s timeout', + '-s dryRun' + ]) + let targetParams = ''; + for (const key in parameters) { + if (keySet.has(key)) { + targetParams = `${targetParams} ${key} ${parameters[key]}` + } + } + return targetParams.trim() +} + +async function onAbilityCreateCallback() { + console.log("onAbilityCreateCallback"); +} + +async function addAbilityMonitorCallback(err: any) { + console.info("addAbilityMonitorCallback : " + JSON.stringify(err)) +} + +export default class OpenHarmonyTestRunner implements TestRunner { + constructor() { + } + + onPrepare() { + console.info("OpenHarmonyTestRunner OnPrepare ") + } + + async onRun() { + console.log('OpenHarmonyTestRunner onRun run') + abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() + abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() + var testAbilityName = abilityDelegatorArguments.bundleName + '.MainAbility' + let lMonitor = { + abilityName: testAbilityName, + onAbilityCreate: onAbilityCreateCallback, + }; + abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback) + var cmd = 'aa start -d 0 -a com.example.myapplication.MainAbility' + ' -b ' + abilityDelegatorArguments.bundleName + cmd += ' '+translateParamsToString(abilityDelegatorArguments.parameters) + var debug = abilityDelegatorArguments.parameters["-D"] + if (debug == 'true') + { + cmd += ' -D' + } + console.info('cmd : '+cmd) + abilityDelegator.executeShellCommand(cmd, + (err: any, d: any) => { + console.info('executeShellCommand : err : ' + JSON.stringify(err)); + console.info('executeShellCommand : data : ' + d.stdResult); + console.info('executeShellCommand : data : ' + d.exitCode); + }) + console.info('OpenHarmonyTestRunner onRun end') + } +}; \ No newline at end of file diff --git a/graphic/windowStage/entry/src/main/ets/test/List.test.ets b/graphic/windowStage/entry/src/main/ets/test/List.test.ets index 90de0c158..1277958a1 100644 --- a/graphic/windowStage/entry/src/main/ets/test/List.test.ets +++ b/graphic/windowStage/entry/src/main/ets/test/List.test.ets @@ -15,7 +15,7 @@ import windowPromiseTest from './windowPromise.test' import windowCallbackTest from './windowCallback.test' -export default function testsuite(context, windowStage, abilityStorage) { - windowCallbackTest(context, windowStage, abilityStorage) - windowPromiseTest(context, windowStage, abilityStorage) +export default function testsuite() { + windowCallbackTest(globalThis.abilityContext,globalThis.windowStage,globalThis.abilityStorage) + windowPromiseTest(globalThis.abilityContext,globalThis.windowStage,globalThis.abilityStorage) } \ No newline at end of file diff --git a/graphic/windowStage/entry/src/main/ets/test/windowCallback.test.ets b/graphic/windowStage/entry/src/main/ets/test/windowCallback.test.ets index 9a58fccdc..3e6c161ac 100644 --- a/graphic/windowStage/entry/src/main/ets/test/windowCallback.test.ets +++ b/graphic/windowStage/entry/src/main/ets/test/windowCallback.test.ets @@ -13,7 +13,7 @@ * limitations under the License. */ -import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from "hypium/index" +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from "@ohos/hypium" import ohosWindow from '@ohos.window'; import display from '@ohos.display'; import screenManager from '@ohos.screen'; diff --git a/graphic/windowStage/entry/src/main/ets/test/windowPromise.test.ets b/graphic/windowStage/entry/src/main/ets/test/windowPromise.test.ets index 538cc6bbb..179dbfafc 100644 --- a/graphic/windowStage/entry/src/main/ets/test/windowPromise.test.ets +++ b/graphic/windowStage/entry/src/main/ets/test/windowPromise.test.ets @@ -13,7 +13,7 @@ * limitations under the License. */ // @ts-nocheck -import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from "hypium/index" +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from "@ohos/hypium" import ohosWindow from '@ohos.window'; import screenManager from '@ohos.screen'; import display from '@ohos.display'; @@ -1349,6 +1349,7 @@ export default function windowPromiseTest(context, windowStage, abilityStorage) } console.log(msgStr + 'done '); done(); + }) /** * @tc.number SUB_WINDOW_SETPREFERREDORIENTATION_JSAPI_001 * @tc.name Test setPreferredOrientation @@ -1403,5 +1404,4 @@ export default function windowPromiseTest(context, windowStage, abilityStorage) console.log(msgStr + 'done '); done(); }) - }) } diff --git a/graphic/windowStage/entry/src/main/resources/base/profile/main_pages.json b/graphic/windowStage/entry/src/main/resources/base/profile/main_pages.json index 6898b31d2..02221db61 100644 --- a/graphic/windowStage/entry/src/main/resources/base/profile/main_pages.json +++ b/graphic/windowStage/entry/src/main/resources/base/profile/main_pages.json @@ -1,6 +1,6 @@ { "src": [ - "pages/index/index", - "pages/second/second" + "MainAbility/pages/index/index", + "MainAbility/pages/second/second" ] } \ No newline at end of file -- GitLab