diff --git a/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/README_zh.md b/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/README_zh.md index 48336868a5ea49145a689d17a8e7a04b2d2c2150..3f2934cd20f2296d6d62d7b39faf080b7b63fe3a 100644 --- a/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/README_zh.md +++ b/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/README_zh.md @@ -2,7 +2,7 @@ ### 介绍 -本样例展示了sensor模块js接口的使用样例,包含订阅,去订阅传感器,订阅、去订阅外设热插拔事件、获取设备及扩展设备的上传感器信息等接口功能 +本样例展示了sensor模块js接口的使用样例,包含订阅和去订阅传感器、订阅和去订阅外设热插拔事件、获取设备及扩展设备的上传感器信息等接口功能 @@ -21,12 +21,12 @@ 1. 在主界面,可以点击Please select sensor type文本框选择此设备上的传感器; 2. 在主界面,可以点击deviceId选择想要使用的设备,目前手机上暂时不支持设置; -4. 在主界面,点击subscribe按钮可以订阅Please select sensor type文本框中选择的传感器,后界面下方会显示回调数据; -5. 在主界面,点击unsubscribe按钮可以去订阅选中的传感器,去订阅后不会再收到回调数据; -6. 在主界面,点击getSensorListByDeviceSync按钮获取本设备及扩展设备上的传感器列表,并在下发显示(手机上只能获取本机上的传感器信息); -7. 在主界面,点击getSingleSensorByDeviceSync按钮可以获取对应设备上对应的传感器信息,(手机只能获取本机上的传感器信息); -8. 在主界面,点击on SensorStatusChange按钮订阅本设备上扩展设备的热插拔事件,(目前手机上暂时不支持此功能) -8. 在主界面,点击off SensorStatusChange按钮去订阅本设备上扩展设备的热插拔事件,(目前手机上暂时不支持此功能) +3. 在主界面,点击subscribe按钮可以订阅Please select sensor type文本框中选择的传感器后,界面下方会显示回调数据; +4. 在主界面,点击unsubscribe按钮可以去订阅选中的传感器,去订阅后不会再收到回调数据; +5. 在主界面,点击getSensorListByDeviceSync按钮获取本设备及扩展设备上的传感器列表,并在下方显示(手机上只能获取本机上的传感器信息); +6. 在主界面,点击getSingleSensorByDeviceSync按钮可以获取对应设备上对应的传感器信息,(手机只能获取本机上的传感器信息); +7. 在主界面,点击on SensorStatusChange按钮订阅本设备上扩展设备的热插拔事件,(目前手机上暂时不支持此功能); +8. 在主界面,点击off SensorStatusChange按钮去订阅本设备上扩展设备的热插拔事件,(目前手机上暂时不支持此功能)。 @@ -44,18 +44,18 @@ entry ### 具体实现 -本样例展示了sensor模块js接口的使用样例,包含订阅,去订阅传感器,订阅、去订阅外设热插拔事件、获取设备及扩展设备的上传感器信息等接口的功能接口封装在index,源码参考:[index.ets](./entry/src/main/ets/pages/index.ets) +本样例展示了sensor模块js接口的使用样例,包含订阅和去订阅传感器、订阅和去订阅外设热插拔事件、获取设备及扩展设备的上传感器信息等接口的功能,该功能全部接口已封装在index,源码参考:[index.ets](./entry/src/main/ets/pages/index.ets) * Please select sensor type文本框:使用sensor.getSensorList接口获取本设备上的所有传感器信息列表; * subscribe订阅传感器:先使用窗口框选择想要订阅的传感器,接着使用sensor.on接口对已经选择的传感器进行订阅; -* unsubscribe去订阅传感器:先使用窗口框选择想要去订阅的传感器,接着使用sensor.off接口对已经选择的传感器进行去订阅,未订阅的传感器会去订阅失败 -* getSensorListByDeviceSync获取本设备及扩展设备上传感器列表,使用sensor.getSensorListByDeviceSync接口即可获取本设备及扩展设备上传感器列表。手机暂时只能获取本机传感器信息列表 -* getSingleSensorByDeviceSync获取指定设备上传感器信息,使用sensor.getSingleSensorByDeviceSync接口获取指定设备上指定传感器信息,目前手机上只能获取本机上传感器信息。 -* 订阅去订阅外设热插拔事件,通过sensor.on和sensor.off来实现对扩设备热插拔事件订阅。目前手机不支持此功能。 +* unsubscribe去订阅传感器:先使用窗口框选择想要去订阅的传感器,接着使用sensor.off接口对已经选择的传感器进行去订阅,未订阅的传感器会去订阅失败; +* getSensorListByDeviceSync获取本设备及扩展设备上传感器列表,使用sensor.getSensorListByDeviceSync接口即可获取本设备及扩展设备上传感器列表。手机暂时只能获取本机传感器信息列表; +* getSingleSensorByDeviceSync获取指定设备上传感器信息,使用sensor.getSingleSensorByDeviceSync接口获取指定设备上指定传感器信息,目前手机上只能获取本机上传感器信息; +* 订阅去订阅外设热插拔事件,通过sensor.on和sensor.off来实现对扩设备热插拔事件订阅,目前手机不支持此功能。 ### 相关权限 -附上使用到权限和链接,示例如下: +附上使用到的权限和链接,示例如下: [ohos.permission.ACCELEROMETER](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissionaccelerometer) @@ -67,7 +67,7 @@ entry 1.本示例仅支持标准系统上运行,支持设备:RK3568; -2.本示例为Stage模型,仅支持API20版本SDK,SDK版本号(API Version 20 Beta),镜像版本号(4.1Beta) +2.本示例为Stage模型,仅支持API20版本SDK,SDK版本号(API Version 20 Beta),镜像版本号(6.0Beta); 3.本示例需要使用DevEco Studio 5.0.4 Release (Build Version: 5.0.11.100, built on March 28, 2025)及以上版本才可编译运行。 diff --git a/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/entry/src/main/ets/pages/Index.ets b/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/entry/src/main/ets/pages/Index.ets index 46a47e9afb3bc999f68f554a65fc11e00193398a..07134a8288689fd04a02cb1df1d30c00b1ab093b 100644 --- a/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/entry/src/main/ets/pages/Index.ets +++ b/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/entry/src/main/ets/pages/Index.ets @@ -50,41 +50,42 @@ class Logger { private domain: number = 0xC02701; private prefix: string = '[Sample_Vibrator]' private format: string = '%{public}, %{public}' + debug(...args: string[]) { hilog.debug(this.domain, this.prefix, this.format, args) } + info(...args: string[]) { hilog.info(this.domain, this.prefix, this.format, args) } + error(...args: string[]) { hilog.error(this.domain, this.prefix, this.format, args) } } + const logger = new Logger(); @Entry @Component struct SensorList { - @State - private sensorItems: string[] = []; + @State private sensorItems: string[] = []; private sampleRate: number = 200000000; private uiContext: UIContext = this.getUIContext(); private promptAction: PromptAction = this.uiContext.getPromptAction(); dialogControllerList: CustomDialogController | null = null; selectedSensorId: number = 0; - @State - private realSensorName: string = ''; - @State - private deviceId: number = 0; + scroller: Scroller = new Scroller; + @State private realSensorName: string = ''; + @State private deviceId: number = 0; private strDeviceId: string = ''; private sensors = new Map(); - @State - private callbackContent:string = ''; - @State - private SensorListByDevice:string = ''; - SensorListByDeviceList: sensor.Sensor[] = []; - private supportSensorIdList: (number|undefined)[] = [1, 2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 256, + @State private callbackContent: string = ''; + @State private sensorListByDevice: string = ''; + sensorListByDeviceList: sensor.Sensor[] = []; + private supportSensorIdList: (number | undefined)[] = [1, 2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 277, 278, 279, 280, 281, 282]; + private checkDeviceId(): CheckRet { if (!this.strDeviceId.length) { return { ok: false, ret: -1 }; @@ -116,7 +117,7 @@ struct SensorList { return result; } - onPageShow(): void { + aboutToAppear(): void { let traceCount = 0; logger.info(TAG + 'getSensorList in'); try { @@ -138,12 +139,37 @@ struct SensorList { } logger.info(TAG + 'getSensorList left'); } + build() { Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) { + Scroll(this.scroller) { + Column() { + Text(`${this.callbackContent}`) + .fontSize(14) + .margin({ top: 15 }) + .id('callbackText') + + Text(`${this.sensorListByDevice}`) + .fontSize(14) + .id('deviceText') + } + } + .width('96%') + .height('20%') + .backgroundColor('#F1F1F1') + .borderRadius(10) + .borderColor($r('sys.color.popup_border_color')) + TextInput({ placeholder: 'Please select sensor type', text: this.realSensorName }) .placeholderColor('rgb(0,0,225)') - .placeholderFont({ size: 20, weight: 100, family: 'cursive', style: FontStyle.Italic }) + .placeholderFont({ + size: 18, + weight: 100, + family: 'cursive', + style: FontStyle.Italic + }) .caretColor(Color.Blue) + .id('sensor_type_selector') .height(50) .type(InputType.Normal) .fontSize(20) @@ -153,7 +179,10 @@ struct SensorList { .fontColor(Color.Black) .enableKeyboardOnFocus(false) .textAlign(TextAlign.Center) - .borderStyle(BorderStyle.Solid).borderWidth(1).borderColor('rgb(0,0,225)').borderRadius(10) + .borderStyle(BorderStyle.Solid) + .borderWidth(1) + .borderColor('rgb(0,0,225)') + .borderRadius(10) .focusable(false) .onClick(() => { TextPickerDialog.show({ @@ -165,14 +194,19 @@ struct SensorList { onCancel: () => { logger.info(TAG, 'TextPickerDialog onCancel'); } - }); + }) }) .width('80%') - .margin({top: 10}) + .margin({ top: 10 }) TextInput({ placeholder: 'deviceId' }) .placeholderColor('rgb(0,0,225)') - .placeholderFont({ size: 20, weight: 100, family: 'cursive', style: FontStyle.Italic }) + .placeholderFont({ + size: 18, + weight: 100, + family: 'cursive', + style: FontStyle.Italic + }) .caretColor(Color.Blue) .height(50) .type(InputType.Number) @@ -184,10 +218,11 @@ struct SensorList { .textAlign(TextAlign.Center) .onChange((value: string) => { this.deviceId = parseInt(value) - }).width('100%') - .margin({top: 10}) + }) + .width('100%') + .margin({ top: 10 }) - Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.SpaceAround, alignItems: ItemAlign.Center}) { + Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.SpaceAround, alignItems: ItemAlign.Center }) { Button($r('app.string.subscribe')).onClick(() => { try { logger.info(TAG + this.realSensorName + 'subscribe in') @@ -195,26 +230,26 @@ struct SensorList { logger.info('This is not support, please change other sensorId!'); return } - if(this.sensors.get(this.realSensorName) == 278) { - logger.info(TAG + this.sensors.get(this.realSensorName) + 'on in'); + if (this.sensors.get(this.realSensorName) == 278) { + logger.info(TAG + this.sensors.get(this.realSensorName) + 'on in'); let atManager = AbilityAccessCtrl.createAtManager(); let context = getContext(this) as common.UIAbilityContext; - atManager.requestPermissionsFromUser(context, ['ohos.permission.READ_HEALTH_DATA'], (err, data)=>{ + atManager.requestPermissionsFromUser(context, ['ohos.permission.READ_HEALTH_DATA'], (err, data) => { if (err) { - logger.info(TAG + 'grant READ_HEALTH_DATA failed:' + JSON.stringify(data)); + logger.error(TAG + 'grant READ_HEALTH_DATA failed:' + JSON.stringify(data)); return; } logger.info(TAG + 'data:' + JSON.stringify(data)); logger.info(TAG + 'data permissions:' + data.permissions); logger.info(TAG + 'data authResults:' + data.authResults); sensor.getSingleSensor(this.sensors.get(this.realSensorName), (err, data) => { - if(err) { + if (err) { logger.error(TAG + JSON.stringify(err)); return; } - if(data) { + if (data) { const userOption = this.getUserOption(); - sensor.on(this.sensors.get(this.realSensorName), (data:sensor.Response)=>{ + sensor.on(this.sensors.get(this.realSensorName), (data: sensor.Response) => { logger.info(TAG + 'PEDOMETER: ' + JSON.stringify(data)); this.callbackContent = JSON.stringify(data); }, userOption) @@ -225,11 +260,9 @@ struct SensorList { }); } else { //普通sensor logger.info('before') - let countSame:number = 0; - let timestamp:number = 0; - let countSame2:number = 0; - let timestamp2:number = 0; - sensor.on(this.sensors.get(this.realSensorName), ((data:sensor.Response) => { + let countSame: number = 0; + let timestamp: number = 0; + sensor.on(this.sensors.get(this.realSensorName), ((data: sensor.Response) => { logger.info(TAG + 'callback: ' + JSON.stringify(data)); this.callbackContent = JSON.stringify(data) if (data.timestamp - timestamp == 0) { @@ -237,42 +270,45 @@ struct SensorList { logger.info(TAG + 'callbackcountSame: ' + JSON.stringify(data)); } timestamp = data.timestamp; - }), {'interval': this.sampleRate}); + }), { 'interval': this.sampleRate }); logger.info('left') } - }catch(e) { - logger.info(TAG + 'subscribe exception in,msg:' + JSON.stringify(e)) - logger.info(TAG + 'unSubscribe exception in,code:' + e.code + 'msg:' + e.message) + } catch (e) { + logger.error(TAG + 'subscribe exception in,msg:' + JSON.stringify(e)) + logger.error(TAG + 'unSubscribe exception in,code:' + e.code + 'msg:' + e.message) } }) + .id('subscribeId') Button($r('app.string.unSubscribe')).onClick(() => { logger.info(TAG + 'unSubscribe in'); try { sensor.off(this.sensors.get(this.realSensorName)); - }catch(e) { - logger.info(TAG + 'unSubscribe exception in,msg:' + JSON.stringify(e)); + } catch (e) { + logger.error(TAG + 'unSubscribe exception in,msg:' + JSON.stringify(e)); } this.callbackContent = ''; }) + .id('unSubscribeId') Button($r('app.string.getSensorListByDeviceSync')) .onClick(() => { logger.info(TAG + 'getSensorListByDeviceSync in'); try { let sensorInfoList = sensor.getSensorListByDeviceSync(); - this.SensorListByDevice = 'sensorInfoList:' + JSON.stringify(sensorInfoList); - this.SensorListByDeviceList = sensorInfoList; + this.sensorListByDevice = 'sensorInfoList:' + JSON.stringify(sensorInfoList); + this.sensorListByDeviceList = sensorInfoList; logger.info(TAG + 'unSubscribe exception in,msg:' + JSON.stringify(sensorInfoList)) } catch (err) { logger.error('Error occurred: ' + JSON.stringify(err)); } }) + .id('getSensorListByDeviceId') Button($r('app.string.getSingleSensorByDeviceSync')) .onClick(() => { - if (!this.SensorListByDevice.length) { + if (!this.sensorListByDevice.length) { this.promptAction.showToast({ message: 'Please click getSensorListByDeviceSync.', duration: 2000 @@ -284,7 +320,7 @@ struct SensorList { builder: SelectDialog({ title: 'select SensorId', selectedIndex: this.selectedSensorId, - radioContent: this.SensorListByDeviceList.map((sensorInfo: sensor.Sensor) => { + radioContent: this.sensorListByDeviceList.map((sensorInfo: sensor.Sensor) => { return { title: `${sensorInfo.sensorName}_${sensorInfo.sensorId}`, action: async () => { @@ -293,7 +329,7 @@ struct SensorList { const checkRet = this.checkDeviceId(); const data = sensor.getSingleSensorByDeviceSync(this.selectedSensorId, checkRet.ok ? checkRet.ret : undefined); - this.SensorListByDevice = 'sensorInfoList:' + JSON.stringify(data); + this.sensorListByDevice = 'sensorInfoList:' + JSON.stringify(data); } catch (error) { logger.error('Error occurred: ' + JSON.stringify(error)); } @@ -304,8 +340,12 @@ struct SensorList { }) this.dialogControllerList.open(); }) + .id('getSingleDeviceId') - Button($r('app.string.on')).fontSize(16).fontWeight(FontWeight.Bold).width('100%') + Button($r('app.string.on')) + .fontSize(16) + .fontWeight(FontWeight.Bold) + .width('80%') .onClick(() => { logger.info("sensor.on('SensorStatusChange')"); try { @@ -316,8 +356,12 @@ struct SensorList { logger.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); } }) + .id('onSensorStatusId') - Button($r('app.string.off')).fontSize(16).fontWeight(FontWeight.Bold).width('100%') + Button($r('app.string.off')) + .fontSize(16) + .fontWeight(FontWeight.Bold) + .width('80%') .onClick(() => { try { sensor.off('sensorStatusChange', this.sensorStatusChangeCallback); @@ -327,21 +371,18 @@ struct SensorList { logger.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); } }) + .id('offSensorStatusId') Button($r('app.string.clean')) .onClick(() => { - this.SensorListByDevice = ''; + this.sensorListByDevice = ''; }) + .id('cleanId') - }.margin({top: 20}) - - Text(`${this.callbackContent}`) - .fontSize(20) - .margin({ top: 20 }) - - Text(`${this.SensorListByDevice}`) - .fontSize(20) - .margin({ top: 20 }) + } + .margin({ top: 20 }) + .width('100%') + .height('70%') } .height('100%') .width('100%') diff --git a/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/entry/src/ohosTest/ets/test/List.test.ets b/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/entry/src/ohosTest/ets/test/List.test.ets index c64e0b06938d246ce044186d4b2d02b500a89e14..3141f499ae5c1731f6deee0c4e46db218796aab6 100644 --- a/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/entry/src/ohosTest/ets/test/List.test.ets +++ b/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/entry/src/ohosTest/ets/test/List.test.ets @@ -13,7 +13,9 @@ * limitations under the License. */ import abilityTest from './Ability.test'; +import sensorSampleTest from './SensorSample.test'; export default function testsuite() { abilityTest(); + sensorSampleTest(); } \ No newline at end of file diff --git a/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/entry/src/ohosTest/ets/test/SensorSample.test.ets b/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/entry/src/ohosTest/ets/test/SensorSample.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7d7ff39be641dc88c09bc5550373b8cd8f4689e5 --- /dev/null +++ b/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/entry/src/ohosTest/ets/test/SensorSample.test.ets @@ -0,0 +1,258 @@ +/* + * 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 hilog from '@ohos.hilog'; +import { Driver, ON, Component } from '@ohos.UiTest'; +import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +const TAG = 'Sample_Sensor'; +const DOMAIN = 0xF811; +const BUNDLE = 'SensorSample' + +export default function SensorSampleTest() { + describe('SensorSampleTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + + /** + * 测试设备正常运行 + */ + it(BUNDLE + 'DeviceNormalOperation_001', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'DeviceNormalOperation_001 begin'); + // 启动应用 + try { + let want: Want = { + bundleName: "com.samples.sensor", + abilityName: "EntryAbility" + }; + + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + abilityDelegator.startAbility(want, (err) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'DeviceNormalOperation_001,err.code:' + err.code); + expect(0).assertEqual(err.code); + }); + } catch (error) { + hilog.error(DOMAIN, TAG, BUNDLE + 'DeviceNormalOperation_001,err.message:' + (error as Error).message); + expect(true).assertFail(); + } + hilog.info(DOMAIN, TAG, BUNDLE + 'DeviceNormalOperation_001 end'); + done(); + }); + + + /** + * 测试订阅传感器 + */ + it(BUNDLE + 'SubscribeSensor_002', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'SubscribeSensor_002 begin'); + try { + let driver: Driver = Driver.create() + await driver.delayMs(1500); + await driver.assertComponentExist(ON.id('sensor_type_selector')) + let button: Component = await driver.findComponent(ON.id('sensor_type_selector')); + await button.click(); + await driver.delayMs(1500); + await driver.assertComponentExist(ON.text('确定')) + let selectorButton: Component = await driver.findComponent(ON.text('确定')); + await selectorButton.click(); + await driver.delayMs(1000); + // 点击订阅按钮 + await driver.assertComponentExist(ON.id('subscribeId')) + let accelerometerButton: Component = await driver.findComponent(ON.id('subscribeId')); + await accelerometerButton.click(); + await driver.delayMs(1000); + // 验证数据上报正常 + let dataDisplay = await driver.findComponent(ON.id('callbackText')); + let dataText = await dataDisplay.getText(); + await driver.delayMs(1000); + expect(dataText.length > 0).assertTrue(); + } catch (error) { + hilog.error(DOMAIN, TAG, BUNDLE + 'SubscribeSensor_002,err.message:' + (error as Error).message); + expect(true).assertFail(); + } + hilog.info(DOMAIN, TAG, BUNDLE + 'SubscribeSensor_002 end'); + done(); + }); + + /** + * 测试取消订阅传感器 + */ + it(BUNDLE + 'UnsubscribeSensor_003', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'UnsubscribeSensor_003 begin'); + try { + let driver: Driver = Driver.create(); + //点击取消订阅 + await driver.assertComponentExist(ON.id('unSubscribeId')); + let unSubscribeButton: Component = await driver.findComponent(ON.id('unSubscribeId')); + await unSubscribeButton.click(); + await driver.delayMs(1500); + let callbackTextDisplay = await driver.findComponent(ON.id('callbackText')); + await driver.delayMs(1000); + await callbackTextDisplay.getText(); + } catch (error) { + hilog.error(DOMAIN, TAG, BUNDLE + 'UnsubscribeSensor_003,err.message:' + (error as Error).message); + expect(error.message.includes('null')).assertTrue(); + } + hilog.info(DOMAIN, TAG, BUNDLE + 'UnsubscribeSensor_003 end'); + done(); + }); + + /** + * 获取设备传感器信息列表 + */ + it(BUNDLE + 'GetSensorList_004', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'GetSensorList_004 begin') + try { + let driver: Driver = Driver.create() + // 点击获取传感器列表按钮 + await driver.assertComponentExist(ON.id('getSensorListByDeviceId')) + let deviceListButton = await driver.findComponent(ON.id('getSensorListByDeviceId')); + await deviceListButton.click(); + await driver.delayMs(1000); + + // 验证列表信息获取正常 + await driver.assertComponentExist(ON.id('deviceText')); + let deviceText = await driver.findComponent(ON.id('deviceText')); + await driver.delayMs(1000); + let childCount = await deviceText.getText(); + expect(childCount.length > 0).assertTrue(); + } catch (error) { + hilog.error(DOMAIN, TAG, BUNDLE + 'GetSensorList_004,err.message:' + (error as Error).message); + expect(true).assertFail(); + } + hilog.info(DOMAIN, TAG, BUNDLE + 'GetSensorList_004 end'); + done(); + }); + + /** + * 获取指定设备指定类型传感器信息 + */ + it(BUNDLE + 'GetSingleSensor_005', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'GetSingleSensor_005 begin'); + try { + // 点击获取指定传感器按钮 + let driver: Driver = Driver.create(); + await driver.assertComponentExist(ON.id('getSingleDeviceId')); + let deviceListButton = await driver.findComponent(ON.id('getSingleDeviceId')); + await deviceListButton.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('sensor_test_0')); + let singleDeviceText = await driver.findComponent(ON.text('sensor_test_0')); + await singleDeviceText.click(); + await driver.delayMs(1000); + // 验证信息获取正常 + let sensorInfo = await driver.findComponent(ON.id('deviceText')); + let infoText = await sensorInfo.getText(); + expect(infoText.includes('sensor_test')).assertTrue(); + } catch (error) { + hilog.error(DOMAIN, TAG, BUNDLE + 'GetSingleSensor_005,err.message:' + (error as Error).message); + expect(true).assertFail(); + } + hilog.info(DOMAIN, TAG, BUNDLE + 'GetSingleSensor_005 end'); + done(); + }); + + /** + * 清除sensor信息列表 + */ + it(BUNDLE + 'CleanSensorList_006', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'CleanSensorList_006 begin'); + try { + // 点击清除按钮 + let driver: Driver = Driver.create() + let cleanButton = await driver.findComponent(ON.id('cleanId')) + await cleanButton.click(); + await driver.delayMs(1000); + + // 验证列表恢复默认 + let sensorInfo = await driver.findComponent(ON.id('deviceText')); + await sensorInfo.getText(); + expect(true).assertFail(); + } catch (error) { + hilog.error(DOMAIN, TAG, BUNDLE + 'CleanSensorList_006,err.message:' + (error as Error).message); + expect(error.message.includes('null')).assertTrue(); + } + hilog.info(DOMAIN, TAG, BUNDLE + 'CleanSensorList_006 end'); + done(); + }); + + /** + * 订阅扩展设备热插拔事件 + */ + it(BUNDLE + 'SubscribeHotPlug_007', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'SubscribeHotPlug_007 begin'); + try { + // 点击订阅热插拔事件按钮 + let driver: Driver = Driver.create() + await driver.assertComponentExist(ON.id('onSensorStatusId')); + let statusChangeButton = await driver.findComponent(ON.id('onSensorStatusId')); + await statusChangeButton.click(); + await driver.delayMs(1500); + // 验证订阅状态 + const statusDisplay = await driver.findComponent(ON.id('callbackText')); + await driver.delayMs(1000); + const statusText = await statusDisplay.getText(); + expect(statusText).assertEqual('on:sensorStatusChange'); + } catch (error) { + hilog.error(DOMAIN, TAG, BUNDLE + 'SubscribeHotPlug_007,err.message:' + (error as Error).message); + expect(true).assertFail(); + } + hilog.info(DOMAIN, TAG, BUNDLE + 'SubscribeHotPlug_007 end'); + done(); + }); + + /** + * 取消订阅扩展设备热插拔事件 + */ + it(BUNDLE + 'UnsubscribeHotPlug_008', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'UnsubscribeHotPlug_008 begin'); + try { + // 点击取消订阅热插拔事件按钮 + let driver: Driver = Driver.create() + await driver.assertComponentExist(ON.id('offSensorStatusId')); + let statusChangeButton = await driver.findComponent(ON.id('offSensorStatusId')); + await statusChangeButton.click(); + await driver.delayMs(1000); + // 验证取消订阅状态 + let statusDisplay = await driver.findComponent(ON.id('callbackText')); + let statusText = await statusDisplay.getText(); + expect(statusText).assertEqual('off:sensorStatusChange'); + } catch (error) { + hilog.error(DOMAIN, TAG, BUNDLE + 'UnsubscribeHotPlug_008,err.message:' + (error as Error).message); + expect(error.message.includes('null')).assertTrue(); + } + hilog.info(DOMAIN, TAG, BUNDLE + 'UnsubscribeHotPlug_008 end'); + done(); + }); + }); + +} \ No newline at end of file diff --git a/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/sensorSample.png b/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/sensorSample.png index ceab4102f4b9fee64028e8f4c0762a61af7b6b13..d705f7d3f4aaba7baa77e61e3198750f262d9593 100644 Binary files a/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/sensorSample.png and b/code/BasicFeature/DeviceManagement/Sensor/SensorJsSamples/sensorSample.png differ diff --git a/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/README_zh.md b/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/README_zh.md index 7a5ce43b216d9e78729149be7d8279887f1cd0b3..ecae78019ae8513275d3f083579e07ea89e0f9fa 100644 --- a/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/README_zh.md +++ b/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/README_zh.md @@ -2,7 +2,7 @@ ### 介绍 -本样例展示了vibrator模块js接口的使用样例,包含time,preset,file,pattern四种形式的振动,订阅去订阅外设热插拔事件、获取设备上马达信息等接口功能 +本样例展示了vibrator模块js接口的使用样例,包含time、preset、file、pattern四种形式的振动,订阅去订阅外设热插拔事件、获取设备上马达信息等接口功能 @@ -45,19 +45,19 @@ entry ### 具体实现 -time,preset,file,pattern四种形式的振动,订阅去订阅外设热插拔事件、获取设备上马达信息等的功能接口封装在index,源码参考:[index.ets](./entry/src/main/ets/pages/index.ets) +time、preset、file、pattern四种形式的振动,订阅去订阅外设热插拔事件、获取设备上马达信息等的功能接口封装在index,源码参考:[index.ets](./entry/src/main/ets/pages/index.ets) -* file文件形式振动:使用Vibrator.startVibration接口的file形式来实现文件形式振动的下发。 -* preset文件形式振动:先使用窗口框选择Vibrator模块提供的effectId,然后使用Vibrator.startVibration接口使用preset形式来实现预置效果串形式振动的下发。 -* time文件形式振动:使用Vibrator.startVibration接口的time形式来下发指定时间形式的振动。 -* pattern文件形式振动:使用Vibrator.startVibration接口的pattern形式来实现ContinuousEvent和TransientEvent事件形式振动的下发。 -* 停止振动:使用Vibrator.stopVibration来停止振动。 -* 订阅去订阅外设热插拔事件,通过Vibrator.on和Vibrator.off来实现对扩设备热插拔事件订阅。 -* 获取设备上马达信息列表:通过Vibrator.getVibratorInfoSync接口来实现对本设备及扩展设备上马达列表的获取 +* file文件形式振动:使用Vibrator.startVibration接口的file形式来实现文件形式振动的下发; +* preset文件形式振动:先使用窗口框选择Vibrator模块提供的effectId,然后使用Vibrator.startVibration接口使用preset形式来实现预置效果串形式振动的下发; +* time文件形式振动:使用Vibrator.startVibration接口的time形式来下发指定时间形式的振动; +* pattern文件形式振动:使用Vibrator.startVibration接口的pattern形式来实现ContinuousEvent和TransientEvent事件形式振动的下发; +* 停止振动:使用Vibrator.stopVibration来停止振动; +* 订阅去订阅外设热插拔事件,通过Vibrator.on和Vibrator.off来实现对扩设备热插拔事件订阅; +* 获取设备上马达信息列表:通过Vibrator.getVibratorInfoSync接口来实现对本设备及扩展设备上马达列表的获取; ### 相关权限 -附上使用到权限和链接,示例如下: +附上使用到的权限和链接,示例如下: [ohos.permission.VIBRATE](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissionvibrate) @@ -69,7 +69,7 @@ time,preset,file,pattern四种形式的振动,订阅去订阅外设热 1.本示例仅支持标准系统上运行,支持设备:RK3568; -2.本示例为Stage模型,仅支持API20版本SDK,SDK版本号(API Version 20 Beta),镜像版本号(4.1Beta) +2.本示例为Stage模型,仅支持API20版本SDK,SDK版本号(API Version 20 Beta),镜像版本号(6.0Beta); 3.本示例需要使用DevEco Studio 5.0.4 Release (Build Version: 5.0.11.100, built on March 28, 2025)及以上版本才可编译运行。 diff --git a/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/entry/src/main/ets/pages/Index.ets b/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/entry/src/main/ets/pages/Index.ets index 1fa6a900e04f4387820343a4312c31315d920fa4..d40906bd246a3465d712feaa5c00e61a034d0209 100644 --- a/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/entry/src/main/ets/pages/Index.ets +++ b/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/entry/src/main/ets/pages/Index.ets @@ -25,16 +25,20 @@ class Logger { private domain: number = 0xC02701; private prefix: string = '[Sample_Vibrator]' private format: string = '%{public}, %{public}' + debug(...args: string[]) { hilog.debug(this.domain, this.prefix, this.format, args) } + info(...args: string[]) { hilog.info(this.domain, this.prefix, this.format, args) } + error(...args: string[]) { hilog.error(this.domain, this.prefix, this.format, args) } } + const logger = new Logger(); @Entry @@ -84,9 +88,25 @@ struct Index { logger.info('Vibrator plunged info:', JSON.stringify(data)); logger.info('on is end'); }; + scroller: Scroller = new Scroller; + @State private callbackContent: string = ''; build() { Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) { + Scroll(this.scroller) { + Column() { + Text(`${this.callbackContent}`) + .fontSize(14) + .margin({ top: 15 }) + .id('callbackText') + } + } + .width('96%') + .height('20%') + .backgroundColor('#F1F1F1') + .borderRadius(10) + .borderColor($r('sys.color.popup_border_color')) + TextInput({ placeholder: 'Please select effectId', text: this.realEffectId }) .placeholderColor('rgb(0,0,225)') .placeholderFont({ @@ -110,6 +130,7 @@ struct Index { .borderColor('rgb(0,0,225)') .borderRadius(10) .focusable(false) + .id('Please select effectId') .onClick(() => { TextPickerDialog.show({ range: this.effectId, @@ -189,13 +210,15 @@ struct Index { }); } this.uiContext.getHostContext()?.resourceManager.closeRawFdSync(fileName); + this.callbackContent = 'Click the file button successfully'; + hilog.info(0xF811, 'Sample_Vibrator', 'Click the file button successfully'); } catch (e) { logger.error(TAG + 'Exception:', e); } }) - Button($r('app.string.preset') + this.realEffectId) + Button('preset:' + this.realEffectId) .onClick(() => { try { // 查询是否支持'haptic.clock.timer' @@ -234,7 +257,10 @@ struct Index { let e: BusinessError = error as BusinessError; logger.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } + this.callbackContent = 'Click the preset button successfully' + hilog.info(0xF811, 'Sample_Vibrator', 'Click the preset button successfully'); }) + .id('preset') Button($r('app.string.time')) .onClick(() => { @@ -257,11 +283,13 @@ struct Index { } catch (e) { logger.error(TAG + 'Exception:', e); } + this.callbackContent = 'Click the time button successfully'; + hilog.info(0xF811, 'Sample_Vibrator', 'Click the time button successfully'); }) Button($r('app.string.addTransientEvent')) .onClick(() => { - let builder:vibrator.VibratorPatternBuilder = new vibrator.VibratorPatternBuilder(); + let builder: vibrator.VibratorPatternBuilder = new vibrator.VibratorPatternBuilder(); try { let param: vibrator.TransientParam = { intensity: 80, @@ -295,11 +323,13 @@ struct Index { let e: BusinessError = error as BusinessError; logger.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } + this.callbackContent = 'Click the addTransientEvent button successfully'; + hilog.info(0xF811, 'Sample_Vibrator', 'Click the addTransientEvent button successfully'); }) Button($r('app.string.addContinuousEvent')) .onClick(() => { - let builder:vibrator.VibratorPatternBuilder = new vibrator.VibratorPatternBuilder(); + let builder: vibrator.VibratorPatternBuilder = new vibrator.VibratorPatternBuilder(); try { // VibratorCurvePoint参数最少设置4个,最大设置16个 let pointsMe: vibrator.VibratorCurvePoint[] = [ @@ -348,6 +378,8 @@ struct Index { let e: BusinessError = error as BusinessError; logger.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } + this.callbackContent = 'Click the addContinuousEvent button successfully'; + hilog.info(0xF811, 'Sample_Vibrator', 'Click the addContinuousEvent button successfully'); }) Button($r('app.string.stopVibration')) @@ -364,6 +396,8 @@ struct Index { } catch (error) { logger.error('stopVibration Error:', error.code, error.message); } + this.callbackContent = 'Click the stopVibration button successfully' + hilog.info(0xF811, 'Sample_Vibrator', 'Click the stopVibration button successfully'); }) Button($r('app.string.on')) @@ -373,6 +407,8 @@ struct Index { } catch (err) { logger.error('Error in GetVibratorList:', err); } + this.callbackContent = 'Click the on button successfully' + hilog.info(0xF811, 'Sample_Vibrator', 'Click the on button successfully'); }) Button($r('app.string.off')) @@ -382,6 +418,8 @@ struct Index { } catch (err) { logger.error("Error in GetVibratorList:", err); } + this.callbackContent = 'Click the off button successfully'; + hilog.info(0xF811, 'Sample_Vibrator', 'Click the off button successfully'); }) Button($r('app.string.getVibratorInfoSync')) @@ -396,6 +434,8 @@ struct Index { logger.error('Error in GetVibratorList:', err); } logger.info('getVibratorInfoSync end'); + this.callbackContent = 'Click the getVibratorInfoSync button successfully'; + hilog.info(0xF811, 'Sample_Vibrator', 'Click the getVibratorInfoSync button successfully'); }) Text(`${this.VibratorStateChangeCallback}`) diff --git a/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/entry/src/ohosTest/ets/test/List.test.ets b/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/entry/src/ohosTest/ets/test/List.test.ets index c64e0b06938d246ce044186d4b2d02b500a89e14..85769a41244eadf936cf4795305c2deb47bfd840 100644 --- a/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/entry/src/ohosTest/ets/test/List.test.ets +++ b/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/entry/src/ohosTest/ets/test/List.test.ets @@ -13,7 +13,9 @@ * limitations under the License. */ import abilityTest from './Ability.test'; +import vibratorSample from './VibratorSample.test'; export default function testsuite() { abilityTest(); + vibratorSample(); } \ No newline at end of file diff --git a/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/entry/src/ohosTest/ets/test/VibratorSample.test.ets b/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/entry/src/ohosTest/ets/test/VibratorSample.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..a36dbac3d377325782faa78dcbdb4d0219a67488 --- /dev/null +++ b/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/entry/src/ohosTest/ets/test/VibratorSample.test.ets @@ -0,0 +1,307 @@ +/* + * 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 hilog from '@ohos.hilog'; +import { Driver, ON } from '@ohos.UiTest'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +const TAG = 'Sample_Vibrator'; +const DOMAIN = 0xF811; +const BUNDLE = 'VibratorSample' + +export default function vibratorSample() { + describe('vibratorSample', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + + /** + * 启动应用 + */ + it(BUNDLE + 'DeviceNormalOperation_001', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'DeviceNormalOperation_001 begin'); + try { + let want: Want = { + bundleName: "com.samples.vibrator", + abilityName: "EntryAbility" + }; + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + await new Promise((resolve, reject) => { + abilityDelegator.startAbility(want, (err: Error) => { + if (err) { + const errorMsg = `Failed to start application: ${err.message}`; + hilog.error(DOMAIN, TAG, errorMsg); + reject(new Error(errorMsg)); + } else { + hilog.info(DOMAIN, TAG, 'Application started successfully'); + resolve(); + } + }); + }); + let driver: Driver = Driver.create(); + await driver.delayMs(5000); + } catch (error) { + hilog.error(DOMAIN, TAG, `Test failed: ${(error as Error).message}`); + expect(false).assertFail(); + } finally { + hilog.info(DOMAIN, TAG, BUNDLE + 'DeviceNormalOperation_001 test complete'); + done(); + } + }); + + /** + *file类型震动 + */ + it(BUNDLE + 'FileTypeVibration_002', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'FileTypeVibration_002 begin'); + try { + let driver: Driver = Driver.create(); + await driver.delayMs(5000); + await driver.assertComponentExist(ON.text('file')); + hilog.info(DOMAIN, TAG, 'Button exists'); + //点击file按钮 + let button = await driver.findComponent(ON.text('file')); + await button.click(); + await driver.delayMs(1000); + } catch (error) { + hilog.error(DOMAIN, TAG, `Test failed: ${(error as Error).message}`); + expect(false).assertFail(); + } finally { + hilog.info(DOMAIN, TAG, BUNDLE + 'FileTypeVibration_002 test complete'); + done(); + } + }); + + + /** + *preset类型震动 + */ + it(BUNDLE + 'PresetTypeVibration_003', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'PresetTypeVibration_003 begin'); + try { + let driver: Driver = Driver.create(); + await driver.delayMs(3000); + await driver.assertComponentExist(ON.id('Please select effectId')); + await driver.assertComponentExist(ON.id('preset')); + hilog.info(DOMAIN, TAG, 'Button exists'); + let button = await driver.findComponent(ON.id('preset')); + let textInput = await driver.findComponent(ON.id('Please select effectId')); + //点击获取effectId列表,选择一个类型 + await textInput.click(); + hilog.info(DOMAIN, TAG, 'Click TextInput and wait for the option list to load'); + let option = await driver.findComponent(ON.text('haptic.notice.success')) + await option.click(); + let confirmButton = await driver.findComponent(ON.text('确定')); + await confirmButton.click(); + await driver.delayMs(1000); + let text = await textInput.getText(); + //点击preset按钮 + await button.click(); + await driver.delayMs(1000); + hilog.info(DOMAIN, TAG, 'text:' + text); + } catch (error) { + hilog.error(DOMAIN, TAG, `Test failed: ${(error as Error).message}`); + expect(false).assertFail(); + } finally { + hilog.info(DOMAIN, TAG, BUNDLE + 'PresetTypeVibration_003 test complete'); + done(); + } + }); + + + /** + * time类型震动 + */ + it(BUNDLE + 'TimeTypeVibration_004', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'TimeTypeVibration_004 begin'); + try { + let driver: Driver = Driver.create(); + await driver.delayMs(5000); + await driver.assertComponentExist(ON.text('time')); + hilog.info(DOMAIN, TAG, 'Button exists'); + //点击time按钮 + let button = await driver.findComponent(ON.text('time')); + await button.click(); + await driver.delayMs(1000); + } catch (error) { + hilog.error(DOMAIN, TAG, `Test failed: ${(error as Error).message}`); + expect(false).assertFail(); + } finally { + hilog.info(DOMAIN, TAG, BUNDLE + 'TimeTypeVibration_004 test complete'); + done(); + } + }); + + + /** + * 短事件类型震动 + */ + it(BUNDLE + 'ShortEventTypeVibration_005', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'ShortEventTypeVibration_005 begin'); + try { + let driver: Driver = Driver.create(); + await driver.delayMs(5000); + await driver.assertComponentExist(ON.text('pattern:addTransientEvent')); + hilog.info(DOMAIN, TAG, 'Button exists'); + //点击短事件按钮 + let button = await driver.findComponent(ON.text('pattern:addTransientEvent')); + await button.click(); + await driver.delayMs(1000); + } catch (error) { + hilog.error(DOMAIN, TAG, `Test failed: ${(error as Error).message}`); + expect(false).assertFail(); + } finally { + hilog.info(DOMAIN, TAG, BUNDLE + 'ShortEventTypeVibration_005 test complete'); + done(); + } + }); + + + /** + * 长事件类型震动 + */ + it(BUNDLE + 'LongEventTypeVibration_006', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'LongEventTypeVibration_006 begin'); + try { + let driver: Driver = Driver.create(); + await driver.delayMs(5000); + await driver.assertComponentExist(ON.text('pattern:addContinuousEvent')); + hilog.info(DOMAIN, TAG, 'Button exists'); + //点击长事件按钮 + let button = await driver.findComponent(ON.text('pattern:addContinuousEvent')); + await button.click(); + await driver.delayMs(1000); + } catch (error) { + hilog.error(DOMAIN, TAG, `Test failed: ${(error as Error).message}`); + expect(false).assertFail(); + } finally { + hilog.info(DOMAIN, TAG, BUNDLE + 'LongEventTypeVibration_006 test complete'); + done(); + } + }); + + + /** + * 停止震动 + */ + it(BUNDLE + 'StopVibration_007', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'StopVibration_007 begin'); + try { + let driver: Driver = Driver.create(); + await driver.delayMs(5000); + await driver.assertComponentExist(ON.text('stopVibration')); + hilog.info(DOMAIN, TAG, 'Button exists'); + //点击按钮停止震动 + let button = await driver.findComponent(ON.text('stopVibration')); + await button.click(); + await driver.delayMs(1000); + } catch (error) { + hilog.error(DOMAIN, TAG, `Test failed: ${(error as Error).message}`); + expect(false).assertFail(); + } finally { + hilog.info(DOMAIN, TAG, BUNDLE + 'StopVibration_007 test complete'); + done(); + } + }); + + + /** + * 订阅扩展设备热插拔事件 + */ + it(BUNDLE + 'SubscribeToExtendedEvents_008', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'SubscribeToExtendedEvents_008 begin'); + try { + let driver: Driver = Driver.create(); + await driver.delayMs(5000); + await driver.assertComponentExist(ON.text('on')); + hilog.info(DOMAIN, TAG, 'Button exists'); + //点击on订阅 + let button = await driver.findComponent(ON.text('on')); + await button.click(); + await driver.delayMs(1000); + } catch (error) { + hilog.error(DOMAIN, TAG, `Test failed: ${(error as Error).message}`); + expect(false).assertFail(); + } finally { + hilog.info(DOMAIN, TAG, BUNDLE + 'SubscribeToExtendedEvents_008 test complete'); + done(); + } + }); + + + /** + * 去订阅扩展设备热插拔事件 + */ + it(BUNDLE + 'UnSubscribeToExtendedEvents_009', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'UnSubscribeToExtendedEvents_009 begin'); + try { + let driver: Driver = Driver.create(); + await driver.delayMs(5000); + await driver.assertComponentExist(ON.text('off')); + hilog.info(DOMAIN, TAG, 'Button exists'); + //点击off去订阅 + let button = await driver.findComponent(ON.text('off')); + await button.click(); + await driver.delayMs(1000); + } catch (error) { + hilog.error(DOMAIN, TAG, `Test failed: ${(error as Error).message}`); + expect(false).assertFail(); + } finally { + hilog.info(DOMAIN, TAG, BUNDLE + 'UnSubscribeToExtendedEvents_009 test complete'); + done(); + } + }); + + + /** + * 获取本设备及扩展设备马达信息列表 + */ + it(BUNDLE + 'GetMotorInformationList_010', 0, async (done: Function) => { + hilog.info(DOMAIN, TAG, BUNDLE + 'GetMotorInformationList_010 begin'); + try { + let driver: Driver = Driver.create(); + await driver.delayMs(5000); + await driver.assertComponentExist(ON.text('getVibratorInfoSync')); + hilog.info(DOMAIN, TAG, 'Button exists'); + //点击按钮获取马达信息列表 + let button = await driver.findComponent(ON.text('getVibratorInfoSync')); + await button.click(); + await driver.delayMs(1000); + } catch (error) { + hilog.error(DOMAIN, TAG, `Test failed: ${(error as Error).message}`); + expect(false).assertFail(); + } finally { + hilog.info(DOMAIN, TAG, BUNDLE + 'GetMotorInformationList_010 test complete'); + done() + } + }); + }) +} \ No newline at end of file diff --git a/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/vibratorSample.png b/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/vibratorSample.png index 0143778af680fe3e942b09dc206d1142278661c3..697e0ee840b8f5bc30ae48007d064f9171e3d462 100644 Binary files a/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/vibratorSample.png and b/code/BasicFeature/DeviceManagement/Vibrator/VibratorJsSamples/vibratorSample.png differ