Stage模型手工测试套示例

Signed-off-by: wangyang2022 <wangyang412@huawei.com>
Change-Id: Id48239501911c931952c7fb28b60b835bda77a18
This commit is contained in:
wangyang2022
2022-07-26 17:04:47 +08:00
parent 93062133d2
commit 06ffe591c1
27 changed files with 154 additions and 92 deletions
@@ -1,14 +0,0 @@
{
"license":"ISC",
"devDependencies":{},
"name":"entry",
"ohos":{
"org":"ohos",
"directoryLevel":"module",
"buildTool":"hvigor"
},
"description":"example description",
"repository":{},
"version":"1.0.0",
"dependencies":{}
}
@@ -1,18 +0,0 @@
{
"license": "ISC",
"devDependencies": {},
"name": "serviceextensiondemo",
"ohos": {
"org": "ohos",
"directoryLevel": "project",
"buildTool": "hvigor"
},
"description": "example description",
"repository": {},
"version": "1.0.0",
"dependencies": {
"@ohos/hypium": "1.0.1",
"@ohos/hvigor-ohos-plugin": "1.1.6",
"@ohos/hvigor": "1.1.6"
}
}
@@ -15,7 +15,7 @@
{
"app": {
"bundleName": "com.example.serviceextensiondemo",
"bundleName": "com.dms.stage_manual_test_suite",
"vendor": "example",
"versionCode": 1000000,
"versionName": "1.0.0",
@@ -2,7 +2,7 @@
"string": [
{
"name": "app_name",
"value": "ServiceExtensionDemo"
"value": "StageManualTestSuite"
}
]
}
}
@@ -1,4 +1,14 @@
# 跨设备绑定ServiceExtensionAbility示例
# Stage模型手工测试套示例
本示例展示了分布式调度子系统基于stage模型手工测试套,包含:
1、跨设备绑定ServiceExtensionAbility
效果图
![](screenshots/MainPage.png)
## 跨设备绑定ServiceExtensionAbility示例
### 简介
本示例展示了在eTS中如何构建stage模型跨设备绑定ServiceExtensionAbility,包含:
@@ -11,6 +21,9 @@
4、绑定成功获取远端 ServiceExtensionAbility 句柄,发起端使用句柄的 sendRequest() 接口调用远端定义的方法
效果图
![](screenshots/ConnectServiceExtensionAbility.png)
### 测试步骤
1、两台设备都安装此应用,并组网成功(计算器可以跨端拉起)
@@ -0,0 +1,14 @@
{
"name": "entry",
"version": "1.0.0",
"ohos": {
"org": "ohos",
"buildTool": "hvigor",
"directoryLevel": "module"
},
"description": "example description",
"repository": {},
"license": "ISC",
"dependencies": {}
}
@@ -18,69 +18,69 @@ import Ability from '@ohos.application.Ability'
import RemoteDeviceModel from '../Model/RemoteDeviceModel'
import rpc from '@ohos.rpc'
var printLog = '[ServiceExtensionDemo]'
var printLog = '[MainAbility]'
var remoteDeviceModel = new RemoteDeviceModel
var messageProxy = null
var connectedAbility
var options = {
onConnect: async function (elementName, proxy) {
showLog("service extension ability connect success")
showLog(printLog + "service extension ability connect success")
if (proxy === null) {
showLog("can not get proxy")
return
}
messageProxy = proxy
showLog("get proxy, elementName: " + elementName)
showLog(printLog + "get proxy, elementName: " + elementName)
},
onDisconnect: function () {
showLog("service extension ability disconnect success")
showLog(printLog + "service extension ability disconnect success")
},
onFailed: function () {
showLog("service extension ability connect fail")
showLog(printLog + "service extension ability connect fail")
}
}
function ConnectRemoteAbility() {
showLog("connect service extension ability begin")
showLog("connect remote device id is " + globalThis.selectDevice)
showLog(printLog + "connect service extension ability begin")
showLog(printLog + "connect remote device id is " + globalThis.selectDevice)
let want = {
bundleName: "com.example.serviceextensiondemo",
bundleName: "com.dms.stage_manual_test_suite",
abilityName: "MyServiceExtensionAbility",
deviceId: globalThis.selectDevice
}
connectedAbility = globalThis.context.connectAbility(want, options)
showLog("connect service extension ability, connectId = " + connectedAbility)
showLog(printLog + "connect service extension ability, connectId = " + connectedAbility)
}
async function DisconnectRemoteAbility() {
showLog("disconnect service extension ability begin")
showLog("disconnect remote device id is " + globalThis.selectDevice)
showLog(printLog + "disconnect service extension ability begin")
showLog(printLog + "disconnect remote device id is " + globalThis.selectDevice)
await globalThis.context.disconnectAbility(connectedAbility)
showLog("disconnect service extension ability, connectId = " + connectedAbility)
showLog(printLog + "disconnect service extension ability, connectId = " + connectedAbility)
connectedAbility = null
}
async function SendMessage() {
if (messageProxy === null) {
showLog("can not get proxy")
showLog(printLog + "can not get proxy")
return
}
showLog("send message : " + AppStorage.Get<string>("message"))
showLog(printLog + "send message : " + AppStorage.Get<string>("message"))
let option = new rpc.MessageOption()
let data = new rpc.MessageParcel()
let reply = new rpc.MessageParcel()
data.writeString(AppStorage.Get<string>("message"))
await messageProxy.sendRequest(1, data, reply, option)
showLog("send message success")
showLog(printLog + "send message success")
globalThis.receiveMessage = reply.readString()
showLog("receive reply message : " + globalThis.receiveMessage)
showLog(printLog + "receive reply message : " + globalThis.receiveMessage)
}
function showLog(logMessage) {
console.log(printLog + logMessage)
console.log(logMessage)
AppStorage.Set<string>("receiveMessage", AppStorage.Get<string>("receiveMessage") + logMessage + "\n")
}
@@ -111,11 +111,11 @@ export default class MainAbility extends Ability {
onWindowStageCreate(windowStage) {
// Main window is created, set main page for this ability
console.log("[ServiceExtensionDemo] MainAbility onWindowStageCreate")
console.log("[ServiceExtension] MainAbility onWindowStageCreate")
globalThis.ConnectRemoteAbility = ConnectRemoteAbility
globalThis.DisconnectRemoteAbility = DisconnectRemoteAbility
globalThis.SendMessage = SendMessage
windowStage.loadContent("pages/index", (err, data) => {
windowStage.loadContent("pages/MainPage", (err, data) => {
if (err.code) {
console.error('Failed to load the content. Cause:' + JSON.stringify(err));
return;
@@ -126,16 +126,16 @@ export default class MainAbility extends Ability {
onWindowStageDestroy() {
// Main window is destroyed, release UI related resources
console.log("[ServiceExtensionDemo] MainAbility onWindowStageDestroy")
console.log("[ServiceExtension] MainAbility onWindowStageDestroy")
}
onForeground() {
// Ability has brought to foreground
console.log("[ServiceExtensionDemo] MainAbility onForeground")
console.log("[ServiceExtension] MainAbility onForeground")
}
onBackground() {
// Ability has back to background
console.log("[ServiceExtensionDemo] MainAbility onBackground")
console.log("[ServiceExtension] MainAbility onBackground")
}
};
@@ -22,13 +22,13 @@ export default class RemoteDeviceModel {
discoverList = [];
callback;
authCallback = null;
#deviceManager;
MyDeviceManager;
constructor() {
}
registerDeviceListCallback(callback) {
if (typeof (this.#deviceManager) === 'undefined') {
if (typeof (this.MyDeviceManager) === 'undefined') {
console.log('[ServiceExtensionDemo] deviceManager.createDeviceManager begin');
let self = this;
deviceManager.createDeviceManager('com.example.serviceextensiondemo', (error, value) => {
@@ -36,7 +36,7 @@ export default class RemoteDeviceModel {
console.error('createDeviceManager failed.');
return;
}
self.#deviceManager = value;
self.MyDeviceManager = value;
self.registerDeviceListCallback_(callback);
console.log('[ServiceExtensionDemo] createDeviceManager callback returned, error=' + error + ' value=' + value);
});
@@ -49,14 +49,14 @@ export default class RemoteDeviceModel {
registerDeviceListCallback_(callback) {
console.info('[ServiceExtensionDemo] registerDeviceListCallback');
this.callback = callback;
if (this.#deviceManager == undefined) {
if (this.MyDeviceManager == undefined) {
console.error('[ServiceExtensionDemo] deviceManager has not initialized');
this.callback();
return;
}
console.info('[ServiceExtensionDemo] getTrustedDeviceListSync begin');
var list = this.#deviceManager.getTrustedDeviceListSync();
var list = this.MyDeviceManager.getTrustedDeviceListSync();
console.info('[ServiceExtensionDemo] getTrustedDeviceListSync end, deviceList=' + JSON.stringify(list));
if (typeof (list) != 'undefined' && typeof (list.length) != 'undefined') {
this.deviceList = list;
@@ -67,7 +67,7 @@ export default class RemoteDeviceModel {
getTrustDeviceList(callback) {
console.info('[ServiceExtensionDemo] getTrustDeviceListSync begin');
var list = this.#deviceManager.getTrustedDeviceListSync();
var list = this.MyDeviceManager.getTrustedDeviceListSync();
console.info('[ServiceExtensionDemo] getTrustedDeviceListSync end, deviceList=' + JSON.stringify(list));
if (typeof (list) != 'undefined' && typeof (list.length) != 'undefined') {
this.deviceList = list;
@@ -79,11 +79,11 @@ export default class RemoteDeviceModel {
unregisterDeviceListCallback() {
console.info('[dmsDemo] stopDeviceDiscovery ' + SUBSCRIBE_ID);
this.#deviceManager.stopDeviceDiscovery(SUBSCRIBE_ID);
this.#deviceManager.off('deviceStateChange');
this.#deviceManager.off('deviceFound');
this.#deviceManager.off('discoverFail');
this.#deviceManager.off('serviceDie');
this.MyDeviceManager.stopDeviceDiscovery(SUBSCRIBE_ID);
this.MyDeviceManager.off('deviceStateChange');
this.MyDeviceManager.off('deviceFound');
this.MyDeviceManager.off('discoverFail');
this.MyDeviceManager.off('serviceDie');
this.deviceList = [];
}
}
@@ -16,7 +16,7 @@
import rpc from '@ohos.rpc';
import ServiceExtensionAbility from '@ohos.application.ServiceExtensionAbility';
var printLog = '[ServiceExtensionDemo]'
var printLog = '[ServiceExtension]'
class MyServiceExtensionAbilityStub extends rpc.RemoteObject {
constructor(des) {
@@ -24,12 +24,12 @@ class MyServiceExtensionAbilityStub extends rpc.RemoteObject {
}
onRemoteRequest(code, data, reply, option) {
console.log(printLog + "MyServiceExtensionAbility onRemoteRequest call")
showLog(printLog + "MyServiceExtensionAbility onRemoteRequest call")
if (code === 1) {
globalThis.receiveMessage = data.readString()
console.log(printLog + "receive message : " + globalThis.receiveMessage)
showLog(printLog + "receive message : " + globalThis.receiveMessage)
} else {
console.log(printLog + "unknown request code")
showLog(printLog + "unknown request code")
}
reply.writeString("MyServiceExtensionAbility receive your message")
return true
@@ -58,6 +58,11 @@ class MyServiceExtensionAbilityStub extends rpc.RemoteObject {
attachLocalInterface(localInterface, descriptor) {}
}
function showLog(logMessage) {
console.log(logMessage)
AppStorage.Set<string>("receiveMessage", AppStorage.Get<string>("receiveMessage") + logMessage + "\n")
}
export default class MyServiceExtensionAbility extends ServiceExtensionAbility {
onCreate(want) {
console.log(printLog + 'MyServiceExtensionAbility OnCreate, want : ' + want.abilityName)
@@ -66,11 +71,11 @@ export default class MyServiceExtensionAbility extends ServiceExtensionAbility {
console.log(printLog + 'MyServiceExtensionAbility onRequest, want : ' + want.abilityName)
}
onConnect(want) {
console.log(printLog + 'MyServiceExtensionAbility onConnect, want : ' + want.abilityName)
showLog(printLog + 'MyServiceExtensionAbility onConnect, want : ' + want.abilityName)
return new MyServiceExtensionAbilityStub('test')
}
onDisconnect(want) {
console.log(printLog + 'MyServiceExtensionAbility onDisconnect, want : ' + want.abilityName)
showLog(printLog + 'MyServiceExtensionAbility onDisconnect, want : ' + want.abilityName)
}
onDestroy() {
console.log(printLog + 'MyServiceExtensionAbility onDestroy')
@@ -59,8 +59,9 @@ struct Index {
.width('100%').height(50).fontSize(10)
.onClick(() => {
console.log(printLog + "select device : " + item)
AppStorage.Set<string>('receiveMessage', AppStorage.Get<string>("receiveMessage") +
"select device:" + item + '\n')
this.selectDeviceName = item
globalThis.selectDevice = item
AppStorage.Set<string>('receiveMessage', AppStorage.Get<string>("receiveMessage") + "select device:" + item + '\n')
})
}.editable(this.editFlag)
}, (item : string) => item.toString())
@@ -79,12 +80,12 @@ struct Index {
Scroll() {
Text(this.receiveMessage)
.width('100%')
.fontSize(20)
.fontSize(15)
.textAlign(TextAlign.Start)
}.scrollable(BarState.Auto)
.height('55%')
.align(ItemAlign.Start)
.borderWidth(1)
}.scrollBar(BarState.Auto)
.height('55%')
.align(ItemAlign.Start)
.borderWidth(1)
Text("待发送数据:")
.height('5%')
@@ -97,13 +98,13 @@ struct Index {
Text(this.message)
.height('10%')
.width('100%')
.fontSize(20)
.fontSize(15)
.borderWidth(1)
.textAlign(TextAlign.Start)
TextInput("输入消息")
.width('10%')
.height('100%')
.width('100%')
.height('10%')
.borderStyle(BorderStyle.Solid)
.type(InputType.Normal)
.onChange((value : string) => {
@@ -118,7 +119,7 @@ struct Index {
globalThis.SendMessage()
})
}.height('15%')
}.width('70%')
}.width('70%').height('100%')
}.width('100%').height('90%')
}.width('100%').height('100%')
}
@@ -0,0 +1,43 @@
/*
* 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.
*/
// @ts-nocheck
import router from '@system.router'
@Entry
@Component
struct Index {
build() {
Column() {
Text("StageManualTestSuite")
.width('100%')
.height('10%')
.fontSize(20)
.textAlign(TextAlign.Center)
Button('ConnectServiceExtensionAbility')
.width("90%")
.height(50)
.onClick(() => {
router.push({uri : 'pages/ConnectServiceExtensionAbility'})
})
Button('other')
.width("90%")
.height(50)
.onClick(() => {
})
}.width('100%').height('100%')
}
}
@@ -10,7 +10,7 @@
},
{
"name": "MainAbility_label",
"value": "ServiceExtensionDemo"
"value": "StageManualTestSuite"
}
]
}
@@ -0,0 +1,6 @@
{
"src": [
"pages/ConnectServiceExtensionAbility",
"pages/MainPage"
]
}
@@ -0,0 +1,17 @@
{
"name": "stagemanualtestsuite",
"version": "1.0.0",
"ohos": {
"org": "ohos",
"buildTool": "hvigor",
"directoryLevel": "project"
},
"description": "example description",
"repository": {},
"license": "ISC",
"dependencies": {
"@ohos/hypium": "1.0.1",
"@ohos/hvigor": "1.1.6",
"@ohos/hvigor-ohos-plugin": "1.1.6"
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB