From 4a3d6030a35e2d9094e0d9073feee4058cfada7c Mon Sep 17 00:00:00 2001 From: zhoudede Date: Tue, 9 Sep 2025 17:24:08 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=865=E6=9D=A1?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhoudede --- .../RoutingSupportstageInterface10.ets | 221 ++++++++++++++++++ .../RoutingSupportstageInterface100.ets | 181 ++++++++++++++ .../RoutingSupportstageInterface20.ets | 107 +++++++++ .../RoutingSupportstageInterface40.ets | 58 +++++ .../RoutingSupportstageInterface40_copy.ets | 38 +++ 5 files changed, 605 insertions(+) create mode 100644 sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface10.ets create mode 100644 sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface100.ets create mode 100644 sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface20.ets create mode 100644 sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface40.ets create mode 100644 sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface40_copy.ets diff --git a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface10.ets b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface10.ets new file mode 100644 index 00000000..ec08f946 --- /dev/null +++ b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface10.ets @@ -0,0 +1,221 @@ +/** + * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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' +import { BusinessError } from '@ohos.base'; + +class innerParams { + data3:number[] + + constructor(tuple:number[]) { + this.data3 = tuple + } +} + +class routerParams { + data1:string + data2:innerParams + + constructor(str:string, tuple:number[]) { + this.data1 = str + this.data2 = new innerParams(tuple) + } +} + +@Entry +@Component +struct routerPage10 { + @State routerStackInfo: string = ''; + @State operationLog: string = ''; + + aboutToAppear() { + this.updateRouterStackInfo(); + this.addLog('Router.back 测试主页面初始化完成'); + } + + // 获取当前路由栈信息 + updateRouterStackInfo() { + try { + const length = router.getLength(); + const state = router.getState(); + this.routerStackInfo = `路由栈深度: ${length}\n当前页面: ${state.name || '未知'}\n当前路径: ${state.path || '未知'}`; + } catch (err) { + this.routerStackInfo = '获取路由信息失败'; + } + } + + // 添加操作日志 + addLog(message: string) { + const timestamp = new Date().toLocaleTimeString(); + this.operationLog = `[${timestamp}] ${message}\n${this.operationLog}`; + } + + // 跳转到测试页面(为返回操作创建上一页) + navigateToTargetPage() { + this.addLog('准备跳转到目标页面...'); + + try { + router.pushUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface10_target', + params: { + fromPage: 'RoutingSupportstageInterface10', + testId: 10, + startTime: Date.now(), + message: '来自router.back测试主页面' + } + }).then(() => { + console.info('跳转到目标页面成功'); + this.addLog('成功跳转到目标页面'); + }).catch((err: BusinessError) => { + console.error('跳转失败:', JSON.stringify(err)); + this.addLog(`跳转失败: ${err.message}`); + }); + } catch (err) { + console.error('跳转异常:', JSON.stringify(err)); + this.addLog('跳转发生异常'); + } + } + + // 测试直接返回(用于测试当前页面的返回行为) + testDirectBack() { + this.addLog('测试直接返回操作...'); + + try { + router.back(); + console.info('直接返回操作已执行'); + this.addLog('直接返回操作已执行'); + } catch (err) { + console.error('直接返回失败:', JSON.stringify(err)); + this.addLog(`直接返回失败: ${JSON.stringify(err)}`); + } + } + + build() { + Column() { + Text('Router back() 接口测试') + .fontSize(24) + .fontWeight(FontWeight.Bold) + .margin({ bottom: 20 }) + + Text('主测试页面') + .fontSize(18) + .fontColor('#007dff') + .fontWeight(FontWeight.Medium) + .margin({ bottom: 15 }) + + // 路由栈信息显示 + Text('当前路由栈信息:') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ bottom: 10 }) + + Text(this.routerStackInfo) + .fontSize(14) + .textAlign(TextAlign.Center) + .backgroundColor('#f0f8ff') + .padding(15) + .borderRadius(10) + .margin({ bottom: 20 }) + .width('90%') + + // 主要测试按钮 + Button('跳转到目标页面') + .onClick(() => { + this.navigateToTargetPage(); + }) + .id('btn') + .width('85%') + .backgroundColor('#007dff') + .fontColor('#ffffff') + + Button('测试直接返回 (back)') + .onClick(() => { + this.testDirectBack(); + }) + .width('85%') + .backgroundColor('#ff9800') + .fontColor('#ffffff') + .margin({ top: 10 }) + + Button('刷新路由栈信息') + .onClick(() => { + this.updateRouterStackInfo(); + this.addLog('已刷新路由栈信息'); + }) + .width('85%') + .backgroundColor('#4caf50') + .fontColor('#ffffff') + .margin({ top: 10 }) + + Divider() + .margin({ top: 20, bottom: 15 }) + + // 操作日志区域 + Text('操作日志:') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ bottom: 10 }) + + Scroll() { + Text(this.operationLog || '暂无操作记录') + .fontSize(12) + .textAlign(TextAlign.Start) + .backgroundColor('#f9f9f9') + .padding(10) + .borderRadius(8) + .width('100%') + .constraintSize({ minHeight: 120, maxHeight: 180 }) + } + .width('90%') + .height(180) + + Button('清空操作日志') + .onClick(() => { + this.operationLog = ''; + this.addLog('操作日志已清空'); + }) + .width('60%') + .backgroundColor('#607d8b') + .fontColor('#ffffff') + .margin({ top: 10 }) + + Divider() + .margin({ top: 20, bottom: 15 }) + + // 测试说明 + Text('测试说明:') + .fontSize(14) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + + Text('1. 点击"跳转到目标页面"创建页面栈\n' + + '2. 在目标页面测试各种返回方式\n' + + '3. 验证返回到上一页或指定页面\n' + + '4. 观察路由栈深度变化') + .fontSize(12) + .fontColor('#666666') + .textAlign(TextAlign.Start) + .alignSelf(ItemAlign.Start) + .lineHeight(18) + + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Start) + .padding(20) + .scrollable(ScrollDirection.Vertical) + } +} diff --git a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface100.ets b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface100.ets new file mode 100644 index 00000000..e3837ca6 --- /dev/null +++ b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface100.ets @@ -0,0 +1,181 @@ +/** + * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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' +import { BusinessError } from '@ohos.base'; + +class innerParams { + data3:number[] + + constructor(tuple:number[]) { + this.data3 = tuple + } +} + +class routerParams { + data1:string + data2:innerParams + + constructor(str:string, tuple:number[]) { + this.data1 = str + this.data2 = new innerParams(tuple) + } +} + +@Entry +@Component +struct routerPage100 { + @State routeStatus: string = '准备进行命名路由跳转'; + @State routerStackInfo: string = ''; + + aboutToAppear() { + this.updateRouterStackInfo(); + } + + // 获取当前路由栈信息 + updateRouterStackInfo() { + try { + const length = router.getLength(); + const state = router.getState(); + this.routerStackInfo = `路由栈深度: ${length}\n当前页面: ${state.name || '未知'}\n当前路径: ${state.path || '未知'}`; + } catch (err) { + this.routerStackInfo = '获取路由信息失败'; + } + } + + build() { + Column() { + Text('命名路由接口测试') + .fontSize(24) + .fontWeight(FontWeight.Bold) + .margin({ bottom: 20 }) + + Text(this.routerStackInfo) + .fontSize(14) + .textAlign(TextAlign.Center) + .backgroundColor('#f0f0f0') + .padding(10) + .borderRadius(8) + .margin({ bottom: 20 }) + .width('90%') + + Text(this.routeStatus) + .fontSize(16) + .fontColor('#666666') + .margin({ bottom: 20 }) + + Button('命名路由跳转 (pushUrl)') + .onClick(() => { + this.routeStatus = '正在执行命名路由跳转...'; + + try { + // 使用命名路由进行跳转,带参数 + router.pushUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface100_target', + params: { + fromPage: 'RoutingSupportstageInterface100', + testId: 100, + timestamp: Date.now(), + message: '来自命名路由的跳转参数' + } + }, router.RouterMode.Standard, (err) => { + if (err) { + console.error(`命名路由跳转失败,错误码:${err.code}, 原因:${err.message}`); + this.routeStatus = `跳转失败: ${err.message}`; + } else { + console.info('命名路由跳转成功'); + this.routeStatus = '跳转成功'; + } + }); + } catch (err) { + console.error('命名路由跳转异常:', JSON.stringify(err)); + this.routeStatus = '跳转异常'; + } + }) + .id('btn') + .width('80%') + .backgroundColor('#007dff') + .fontColor('#ffffff') + + Button('使用Promise方式跳转') + .onClick(() => { + this.routeStatus = '正在使用Promise方式跳转...'; + + // 使用Promise方式进行命名路由跳转 + router.pushUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface100_target', + params: { + fromPage: 'RoutingSupportstageInterface100', + testId: 100, + jumpType: 'Promise', + timestamp: Date.now() + } + }).then(() => { + console.info('Promise方式命名路由跳转成功'); + this.routeStatus = 'Promise跳转成功'; + }).catch((err: BusinessError) => { + console.error(`Promise方式跳转失败,错误码:${err.code}, 原因:${err.message}`); + this.routeStatus = `Promise跳转失败: ${err.message}`; + }); + }) + .width('80%') + .backgroundColor('#4ecdc4') + .fontColor('#ffffff') + .margin({ top: 10 }) + + Button('替换当前页面 (replaceUrl)') + .onClick(() => { + this.routeStatus = '正在替换当前页面...'; + + try { + router.replaceUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface100_target', + params: { + fromPage: 'RoutingSupportstageInterface100', + testId: 100, + jumpType: 'replace', + message: '这是替换跳转' + } + }).then(() => { + console.info('replaceUrl命名路由跳转成功'); + }).catch((err: BusinessError) => { + console.error(`replaceUrl跳转失败,错误码:${err.code}, 原因:${err.message}`); + this.routeStatus = `替换跳转失败: ${err.message}`; + }); + } catch (err) { + console.error('replaceUrl跳转异常:', JSON.stringify(err)); + this.routeStatus = '替换跳转异常'; + } + }) + .width('80%') + .backgroundColor('#ff9800') + .fontColor('#ffffff') + .margin({ top: 10 }) + + Button('刷新路由信息') + .onClick(() => { + this.updateRouterStackInfo(); + }) + .width('80%') + .backgroundColor('#9e9e9e') + .fontColor('#ffffff') + .margin({ top: 20 }) + + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Center) + .padding(20) + } +} diff --git a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface20.ets b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface20.ets new file mode 100644 index 00000000..f39ab664 --- /dev/null +++ b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface20.ets @@ -0,0 +1,107 @@ +/** + * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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' +import { BusinessError } from '@ohos.base'; +class innerParams { + data3:number[] + + constructor(tuple:number[]) { + this.data3 = tuple + } +} + +class routerParams { + data1:string + data2:innerParams + + constructor(str:string, tuple:number[]) { + this.data1 = str + this.data2 = new innerParams(tuple) + } +} +@Entry +@Component +struct routerPage20 { + @State routerStackInfo: string = ''; + + aboutToAppear() { + this.updateRouterStackInfo(); + } + + // 获取当前路由栈信息 + updateRouterStackInfo() { + try { + const length = router.getLength(); + const state = router.getState(); + this.routerStackInfo = `路由栈深度: ${length}\n当前页面: ${state.name || '未知'}\n当前路径: ${state.path || '未知'}`; + } catch (err) { + this.routerStackInfo = '获取路由信息失败'; + } + } + + build() { + Column() { + Text('Router Clear 测试页面') + .fontSize(20) + .fontWeight(FontWeight.Bold) + .margin({ bottom: 20 }) + + Text(this.routerStackInfo) + .fontSize(14) + .textAlign(TextAlign.Center) + .backgroundColor('#f5f5f5') + .padding(10) + .borderRadius(8) + .margin({ bottom: 20 }) + + Button('清空路由栈 (router.clear)') + .onClick(() => { + try { + // 获取清空前的路由栈信息 + const beforeLength = router.getLength(); + console.info(`准备清空路由栈,当前深度: ${beforeLength}`); + + // 执行清空操作 + router.clear(); + console.info('router.clear() 执行成功,路由栈已清空'); + + // 更新显示信息 + setTimeout(() => { + this.updateRouterStackInfo(); + }, 100); + } catch (err) { + console.error(`router.clear() 执行失败,错误码:${err.code}, 原因:${err.message}`); + } + }) + .id('btn') + .width('80%') + .backgroundColor('#ff6b6b') + .fontColor('#ffffff') + + Button('刷新路由栈信息') + .onClick(() => { + this.updateRouterStackInfo(); + }) + .width('80%') + .backgroundColor('#4ecdc4') + .fontColor('#ffffff') + .margin({ top: 10 }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Center) + .padding(20) + } +} \ No newline at end of file diff --git a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface40.ets b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface40.ets new file mode 100644 index 00000000..a2a8180a --- /dev/null +++ b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface40.ets @@ -0,0 +1,58 @@ +/** + * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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' +import { BusinessError } from '@ohos.base'; +class innerParams { + data3:number[] + + constructor(tuple:number[]) { + this.data3 = tuple + } +} + +class routerParams { + data1:string + data2:innerParams + + constructor(str:string, tuple:number[]) { + this.data1 = str + this.data2 = new innerParams(tuple) + } +} +@Entry +@Component +struct routerPage40 { + + build() { + Column() { + Button('跳转到详情页(带参数)') + .onClick(() => { + // pushUrl保留当前页,参数通过params传递 + router.pushUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface40_copy', + params: { id: 123, title: 'oh_course' } // 支持对象、数组等复杂参数 + }, router.RouterMode.Standard, (err) => { + if (err) { + console.error(`跳转失败,错误码:${err.code}, 原因:${err.message}`); + } + }); + }) + .id('btn') + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Center) + } +} \ No newline at end of file diff --git a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface40_copy.ets b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface40_copy.ets new file mode 100644 index 00000000..110ce475 --- /dev/null +++ b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface40_copy.ets @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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 router from '@ohos.router' + +@Entry +@Component +struct routerPage40_copy { + aboutToAppear() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility index aboutToAppear'); + } + @State message: string = 'Hello World' + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file -- Gitee From 49b69e023427bf02c9adc8bf38d48d200c0eb100 Mon Sep 17 00:00:00 2001 From: zhoudede Date: Tue, 9 Sep 2025 17:24:25 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=863=E6=9D=A1?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhoudede --- ...RoutingSupportstageInterface100_target.ets | 150 ++++++++++++ .../RoutingSupportstageInterface50.ets | 229 ++++++++++++++++++ .../RoutingSupportstageInterface70_copy.ets | 37 +++ 3 files changed, 416 insertions(+) create mode 100644 sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface100_target.ets create mode 100644 sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface50.ets create mode 100644 sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface70_copy.ets diff --git a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface100_target.ets b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface100_target.ets new file mode 100644 index 00000000..ee4b0262 --- /dev/null +++ b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface100_target.ets @@ -0,0 +1,150 @@ +/** + * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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 routerTargetPage100 { + @State receivedParams: string = ''; + @State routerStackInfo: string = ''; + + aboutToAppear() { + this.loadReceivedParams(); + this.updateRouterStackInfo(); + } + + // 加载接收到的参数 + loadReceivedParams() { + try { + const params = router.getParams(); + if (params) { + this.receivedParams = `接收到的参数:\n${JSON.stringify(params, null, 2)}`; + console.info('目标页面接收到参数:', JSON.stringify(params)); + } else { + this.receivedParams = '未接收到任何参数'; + } + } catch (err) { + this.receivedParams = '获取参数失败'; + console.error('获取参数失败:', JSON.stringify(err)); + } + } + + // 获取当前路由栈信息 + updateRouterStackInfo() { + try { + const length = router.getLength(); + const state = router.getState(); + this.routerStackInfo = `路由栈深度: ${length}\n当前页面: ${state.name || '未知'}\n当前路径: ${state.path || '未知'}`; + } catch (err) { + this.routerStackInfo = '获取路由信息失败'; + } + } + + build() { + Column() { + Text('命名路由目标页面') + .fontSize(24) + .fontWeight(FontWeight.Bold) + .fontColor('#007dff') + .margin({ bottom: 30 }) + + Text('✅ 命名路由跳转成功!') + .fontSize(18) + .fontColor('#4caf50') + .fontWeight(FontWeight.Medium) + .margin({ bottom: 20 }) + + Text(this.routerStackInfo) + .fontSize(14) + .textAlign(TextAlign.Center) + .backgroundColor('#e8f5e8') + .padding(15) + .borderRadius(10) + .margin({ bottom: 20 }) + .width('90%') + + Text(this.receivedParams) + .fontSize(12) + .textAlign(TextAlign.Start) + .backgroundColor('#f5f5f5') + .padding(15) + .borderRadius(10) + .margin({ bottom: 30 }) + .width('90%') + + Button('返回上一页 (back)') + .onClick(() => { + try { + router.back(); + console.info('返回上一页成功'); + } catch (err) { + console.error('返回失败:', JSON.stringify(err)); + } + }) + .width('80%') + .backgroundColor('#ff5722') + .fontColor('#ffffff') + + Button('返回到指定页面') + .onClick(() => { + try { + router.back({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface100' + }); + console.info('返回到指定页面成功'); + } catch (err) { + console.error('返回到指定页面失败:', JSON.stringify(err)); + } + }) + .width('80%') + .backgroundColor('#9c27b0') + .fontColor('#ffffff') + .margin({ top: 10 }) + + Button('刷新页面信息') + .onClick(() => { + this.loadReceivedParams(); + this.updateRouterStackInfo(); + }) + .width('80%') + .backgroundColor('#607d8b') + .fontColor('#ffffff') + .margin({ top: 10 }) + + Divider() + .margin({ top: 30, bottom: 20 }) + + Text('测试验证信息:') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + + Text('1. 成功接收到命名路由传递的参数\n' + + '2. 路由栈深度正确增加\n' + + '3. 页面路径显示正确\n' + + '4. 支持返回操作') + .fontSize(14) + .fontColor('#666666') + .textAlign(TextAlign.Start) + .alignSelf(ItemAlign.Start) + .lineHeight(20) + + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Center) + .padding(20) + } +} diff --git a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface50.ets b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface50.ets new file mode 100644 index 00000000..846e4e40 --- /dev/null +++ b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface50.ets @@ -0,0 +1,229 @@ +/** + * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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' +import { BusinessError } from '@ohos.base'; + +class innerParams { + data3:number[] + + constructor(tuple:number[]) { + this.data3 = tuple + } +} + +class routerParams { + data1:string + data2:innerParams + + constructor(str:string, tuple:number[]) { + this.data1 = str + this.data2 = new innerParams(tuple) + } +} + +@Entry +@Component +struct routerPage50 { + @State currentState: string = ''; + @State stateDetails: string = ''; + @State operationLog: string = ''; + + aboutToAppear() { + this.getPageState(); + this.addLog('页面初始化完成'); + } + + // 获取当前页面状态 + getPageState() { + try { + const state = router.getState(); + this.currentState = `页面状态获取成功`; + + // 格式化状态信息显示 + this.stateDetails = `页面名称: ${state.name || '未设置'}\n` + + `页面路径: ${state.path || '未知路径'}\n` + + `页面索引: ${state.index !== undefined ? state.index : '未知'}\n` + + `完整状态: ${JSON.stringify(state, null, 2)}`; + + console.info('getState()获取到的页面状态:', JSON.stringify(state)); + this.addLog(`成功获取页面状态: ${state.name || '未命名'}`); + + } catch (err) { + this.currentState = '获取页面状态失败'; + this.stateDetails = `错误信息: ${JSON.stringify(err)}`; + console.error('getState()调用失败:', JSON.stringify(err)); + this.addLog('获取页面状态失败'); + } + } + + // 添加操作日志 + addLog(message: string) { + const timestamp = new Date().toLocaleTimeString(); + this.operationLog = `[${timestamp}] ${message}\n${this.operationLog}`; + } + + build() { + Column() { + Text('Router getState() 接口测试') + .fontSize(24) + .fontWeight(FontWeight.Bold) + .margin({ bottom: 20 }) + + // 状态显示区域 + Text(this.currentState) + .fontSize(18) + .fontColor(this.currentState.includes('成功') ? '#4caf50' : '#f44336') + .fontWeight(FontWeight.Medium) + .margin({ bottom: 15 }) + + // 状态详细信息 + Text('页面状态详细信息:') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ bottom: 10 }) + + Text(this.stateDetails) + .fontSize(12) + .textAlign(TextAlign.Start) + .backgroundColor('#f5f5f5') + .padding(15) + .borderRadius(10) + .margin({ bottom: 20 }) + .width('90%') + + // 操作按钮 + Button('获取页面状态 (getState)') + .onClick(() => { + this.getPageState(); + }) + .id('btn') + .width('80%') + .backgroundColor('#007dff') + .fontColor('#ffffff') + + Button('带参数跳转到自己') + .onClick(() => { + this.addLog('准备带参数跳转到当前页面'); + + router.replaceUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface50', + params: { + testParam: 'getState测试参数', + timestamp: Date.now(), + counter: Math.floor(Math.random() * 100), + testObject: { + name: 'OpenHarmony', + version: '5.0', + feature: 'router.getState()' + } + } + }).then(() => { + console.info('带参数跳转成功'); + this.addLog('带参数跳转成功,状态已更新'); + // 跳转后重新获取状态 + setTimeout(() => { + this.getPageState(); + }, 100); + }).catch((err: BusinessError) => { + console.error('带参数跳转失败:', JSON.stringify(err)); + this.addLog(`带参数跳转失败: ${err.message}`); + }); + }) + .width('80%') + .backgroundColor('#4caf50') + .fontColor('#ffffff') + .margin({ top: 10 }) + + Button('获取传入参数') + .onClick(() => { + try { + const params = router.getParams(); + if (params && Object.keys(params).length > 0) { + this.addLog(`接收到参数: ${JSON.stringify(params)}`); + console.info('getParams()获取到参数:', JSON.stringify(params)); + } else { + this.addLog('未接收到任何参数'); + } + } catch (err) { + this.addLog('获取参数失败'); + console.error('getParams()调用失败:', JSON.stringify(err)); + } + }) + .width('80%') + .backgroundColor('#ff9800') + .fontColor('#ffffff') + .margin({ top: 10 }) + + Button('清空操作日志') + .onClick(() => { + this.operationLog = ''; + this.addLog('日志已清空'); + }) + .width('80%') + .backgroundColor('#9e9e9e') + .fontColor('#ffffff') + .margin({ top: 10 }) + + Divider() + .margin({ top: 20, bottom: 15 }) + + // 操作日志区域 + Text('操作日志:') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ bottom: 10 }) + + Scroll() { + Text(this.operationLog || '暂无操作记录') + .fontSize(12) + .textAlign(TextAlign.Start) + .backgroundColor('#f9f9f9') + .padding(10) + .borderRadius(8) + .width('100%') + .constraintSize({ minHeight: 100, maxHeight: 150 }) + } + .width('90%') + .height(150) + + Divider() + .margin({ top: 15, bottom: 10 }) + + // 使用说明 + Text('功能说明:') + .fontSize(14) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + + Text('1. getState() - 获取当前页面状态信息\n' + + '2. 支持获取页面名称、路径、索引等\n' + + '3. 可以检测页面参数变化\n' + + '4. 实时显示状态更新') + .fontSize(12) + .fontColor('#666666') + .textAlign(TextAlign.Start) + .alignSelf(ItemAlign.Start) + .lineHeight(18) + + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Start) + .padding(20) + .scrollable(ScrollDirection.Vertical) + } +} diff --git a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface70_copy.ets b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface70_copy.ets new file mode 100644 index 00000000..6fe37b15 --- /dev/null +++ b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface70_copy.ets @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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'; + +@Entry +@Component +struct routerPage70_copy { + aboutToAppear() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility index aboutToAppear'); + } + @State message: string = 'Hello World' + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file -- Gitee From 13727d70463f6fad491cf0fe3b24bb80404170e8 Mon Sep 17 00:00:00 2001 From: zhoudede Date: Tue, 9 Sep 2025 17:25:52 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=864=E6=9D=A1?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhoudede --- .../RoutingSupportstageInterface30.ets | 360 ++++++++++++++++++ .../RoutingSupportstageInterface60.ets | 350 +++++++++++++++++ .../RoutingSupportstageInterface70.ets | 34 +- .../RoutingSupportstageInterface80.ets | 256 +++++++++++++ 4 files changed, 988 insertions(+), 12 deletions(-) create mode 100644 sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface30.ets create mode 100644 sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface60.ets create mode 100644 sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface80.ets diff --git a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface30.ets b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface30.ets new file mode 100644 index 00000000..d751bbfa --- /dev/null +++ b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface30.ets @@ -0,0 +1,360 @@ +/** + * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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' +import { BusinessError } from '@ohos.base'; + +class innerParams { + data3:number[] + + constructor(tuple:number[]) { + this.data3 = tuple + } +} + +class routerParams { + data1:string + data2:innerParams + + constructor(str:string, tuple:number[]) { + this.data1 = str + this.data2 = new innerParams(tuple) + } +} + +@Entry +@Component +struct routerPage30 { + @State currentLength: number = 0; + @State lengthStatus: string = ''; + @State operationLog: string = ''; + @State lengthHistory: string = ''; + + aboutToAppear() { + this.getCurrentLength(); + this.addLog('Router.getLength 测试页面初始化完成'); + } + + // 获取当前页面栈长度 + getCurrentLength() { + try { + const length = router.getLength(); + this.currentLength = length; + this.lengthStatus = `✅ 成功获取页面栈长度: ${length}`; + + // 更新历史记录 + const timestamp = new Date().toLocaleTimeString(); + this.lengthHistory = `[${timestamp}] 栈深度: ${length}\n${this.lengthHistory}`; + + console.info(`router.getLength() 返回值: ${length}`); + this.addLog(`成功获取页面栈长度: ${length}`); + + } catch (err) { + this.lengthStatus = '❌ 获取页面栈长度失败'; + this.currentLength = -1; + console.error('getLength() 调用失败:', JSON.stringify(err)); + this.addLog(`获取页面栈长度失败: ${JSON.stringify(err)}`); + } + } + + // 添加操作日志 + addLog(message: string) { + const timestamp = new Date().toLocaleTimeString(); + this.operationLog = `[${timestamp}] ${message}\n${this.operationLog}`; + } + + // 跳转到测试页面并观察长度变化 + pushToTargetPage() { + this.addLog('准备跳转到目标页面,观察栈长度变化...'); + + const beforeLength = this.currentLength; + + router.pushUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface30_target', + params: { + fromPage: 'RoutingSupportstageInterface30', + beforeLength: beforeLength, + testId: 30, + timestamp: Date.now(), + operation: 'pushUrl' + } + }).then(() => { + console.info('跳转到目标页面成功'); + this.addLog(`跳转成功,跳转前栈长度: ${beforeLength}`); + }).catch((err: BusinessError) => { + console.error('跳转失败:', JSON.stringify(err)); + this.addLog(`跳转失败: ${err.message}`); + }); + } + + // 使用 replaceUrl 替换当前页面 + replaceToTargetPage() { + this.addLog('准备替换到目标页面,观察栈长度变化...'); + + const beforeLength = this.currentLength; + + router.replaceUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface30_target', + params: { + fromPage: 'RoutingSupportstageInterface30', + beforeLength: beforeLength, + testId: 30, + timestamp: Date.now(), + operation: 'replaceUrl' + } + }).then(() => { + console.info('替换到目标页面成功'); + this.addLog(`替换成功,替换前栈长度: ${beforeLength}`); + }).catch((err: BusinessError) => { + console.error('替换失败:', JSON.stringify(err)); + this.addLog(`替换失败: ${err.message}`); + }); + } + + // 多次跳转测试栈深度累积 + multiplePush() { + this.addLog('开始多次跳转测试...'); + + const currentLength = this.currentLength; + + // 第一次跳转 + router.pushUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface30_target', + params: { + fromPage: 'RoutingSupportstageInterface30', + beforeLength: currentLength, + testId: 30, + operation: 'multiplePush_1', + step: 1 + } + }).then(() => { + this.addLog(`第1次跳转完成,原始栈长度: ${currentLength}`); + + // 延时后进行第二次跳转 + setTimeout(() => { + router.pushUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface30_target', + params: { + fromPage: 'RoutingSupportstageInterface30_target', + beforeLength: currentLength + 1, + testId: 30, + operation: 'multiplePush_2', + step: 2 + } + }).then(() => { + this.addLog('第2次跳转完成,栈深度应该增加2层'); + }).catch((err: BusinessError) => { + this.addLog(`第2次跳转失败: ${err.message}`); + }); + }, 500); + + }).catch((err: BusinessError) => { + this.addLog(`第1次跳转失败: ${err.message}`); + }); + } + + // 测试返回操作对栈长度的影响 + testBackOperation() { + const currentLength = this.currentLength; + + if (currentLength <= 1) { + this.addLog('当前栈深度过小,无法执行返回操作'); + return; + } + + this.addLog(`准备执行返回操作,当前栈长度: ${currentLength}`); + + try { + router.back(); + this.addLog('返回操作已执行'); + } catch (err) { + this.addLog(`返回操作失败: ${JSON.stringify(err)}`); + } + } + + build() { + Column() { + Text('Router getLength() 接口测试') + .fontSize(24) + .fontWeight(FontWeight.Bold) + .margin({ bottom: 20 }) + + Text('获取当前页面栈内的页面数量') + .fontSize(16) + .fontColor('#666666') + .margin({ bottom: 15 }) + + // 当前栈长度显示 + Text('当前页面栈长度:') + .fontSize(18) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ bottom: 10 }) + + Row() { + Text(this.currentLength >= 0 ? `${this.currentLength}` : '获取失败') + .fontSize(36) + .fontWeight(FontWeight.Bold) + .fontColor(this.currentLength >= 0 ? '#007dff' : '#f44336') + + Text('页面') + .fontSize(18) + .fontColor('#666666') + .margin({ left: 8 }) + } + .margin({ bottom: 15 }) + + // 状态显示 + Text(this.lengthStatus) + .fontSize(16) + .fontColor( + this.lengthStatus.includes('✅') ? '#4caf50' : '#f44336' + ) + .backgroundColor('#f8f9fa') + .padding(12) + .borderRadius(8) + .margin({ bottom: 20 }) + .width('90%') + + // 测试按钮组 + Button('获取页面栈长度 (getLength)') + .onClick(() => { + this.getCurrentLength(); + }) + .id('btn') + .width('85%') + .backgroundColor('#007dff') + .fontColor('#ffffff') + + Button('跳转到目标页面 (pushUrl)') + .onClick(() => { + this.pushToTargetPage(); + }) + .width('85%') + .backgroundColor('#4caf50') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Button('替换到目标页面 (replaceUrl)') + .onClick(() => { + this.replaceToTargetPage(); + }) + .width('85%') + .backgroundColor('#ff9800') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Button('多次跳转测试') + .onClick(() => { + this.multiplePush(); + }) + .width('85%') + .backgroundColor('#9c27b0') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Button('测试返回操作') + .onClick(() => { + this.testBackOperation(); + }) + .width('85%') + .backgroundColor('#f44336') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Divider() + .margin({ top: 20, bottom: 15 }) + + // 长度变化历史 + Text('栈深度变化历史:') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ bottom: 10 }) + + Scroll() { + Text(this.lengthHistory || '暂无历史记录') + .fontSize(12) + .textAlign(TextAlign.Start) + .backgroundColor('#e3f2fd') + .padding(10) + .borderRadius(8) + .width('100%') + .constraintSize({ minHeight: 80, maxHeight: 120 }) + } + .width('90%') + .height(120) + + Divider() + .margin({ top: 15, bottom: 10 }) + + // 操作日志区域 + Text('操作日志:') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ bottom: 10 }) + + Scroll() { + Text(this.operationLog || '暂无操作记录') + .fontSize(12) + .textAlign(TextAlign.Start) + .backgroundColor('#f9f9f9') + .padding(10) + .borderRadius(8) + .width('100%') + .constraintSize({ minHeight: 80, maxHeight: 100 }) + } + .width('90%') + .height(100) + + Button('清空日志') + .onClick(() => { + this.operationLog = ''; + this.lengthHistory = ''; + this.addLog('日志已清空'); + }) + .width('60%') + .backgroundColor('#607d8b') + .fontColor('#ffffff') + .margin({ top: 10 }) + + Divider() + .margin({ top: 15, bottom: 10 }) + + // 功能说明 + Text('功能说明:') + .fontSize(14) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + + Text('1. getLength() - 获取当前路由栈中页面数量\n' + + '2. pushUrl 会使栈深度+1\n' + + '3. replaceUrl 不改变栈深度\n' + + '4. back 会使栈深度-1\n' + + '5. 实时监控栈深度变化') + .fontSize(12) + .fontColor('#666666') + .textAlign(TextAlign.Start) + .alignSelf(ItemAlign.Start) + .lineHeight(18) + + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Start) + .padding(20) + .scrollable(ScrollDirection.Vertical) + } +} diff --git a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface60.ets b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface60.ets new file mode 100644 index 00000000..0e54d514 --- /dev/null +++ b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface60.ets @@ -0,0 +1,350 @@ +/** + * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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' +import { BusinessError } from '@ohos.base'; + +class innerParams { + data3:number[] + + constructor(tuple:number[]) { + this.data3 = tuple + } +} + +class routerParams { + data1:string + data2:innerParams + + constructor(str:string, tuple:number[]) { + this.data1 = str + this.data2 = new innerParams(tuple) + } +} + +@Entry +@Component +struct routerPage60 { + @State routerStackInfo: string = ''; + @State operationLog: string = ''; + + aboutToAppear() { + this.updateRouterStackInfo(); + this.addLog('Router.pushUrl 测试页面初始化完成'); + } + + // 获取当前路由栈信息 + updateRouterStackInfo() { + try { + const length = router.getLength(); + const state = router.getState(); + this.routerStackInfo = `路由栈深度: ${length}\n当前页面: ${state.name || '未知'}\n当前路径: ${state.path || '未知'}`; + } catch (err) { + this.routerStackInfo = '获取路由信息失败'; + } + } + + // 添加操作日志 + addLog(message: string) { + const timestamp = new Date().toLocaleTimeString(); + this.operationLog = `[${timestamp}] ${message}\n${this.operationLog}`; + } + + // 基础 pushUrl 跳转(带参数) + basicPushUrl() { + this.addLog('执行基础 pushUrl 跳转...'); + + try { + router.pushUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface60_target', + params: { + fromPage: 'RoutingSupportstageInterface60', + testType: '基础pushUrl', + testId: 60, + timestamp: Date.now(), + data: { + title: '基础跳转测试', + description: '这是一个基础的pushUrl跳转测试', + items: ['项目1', '项目2', '项目3'] + } + } + }); + + console.info('基础 pushUrl 跳转已执行'); + this.addLog('基础 pushUrl 跳转已执行'); + + } catch (err) { + console.error('基础 pushUrl 跳转失败:', JSON.stringify(err)); + this.addLog(`基础 pushUrl 跳转失败: ${JSON.stringify(err)}`); + } + } + + // Promise 方式 pushUrl 跳转 + promisePushUrl() { + this.addLog('执行 Promise 方式 pushUrl 跳转...'); + + router.pushUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface60_target', + params: { + fromPage: 'RoutingSupportstageInterface60', + testType: 'Promise pushUrl', + testId: 60, + timestamp: Date.now(), + data: { + title: 'Promise跳转测试', + description: '这是一个Promise方式的pushUrl跳转测试', + features: ['支持异步处理', '错误捕获', '状态回调'] + } + } + }).then(() => { + console.info('Promise 方式 pushUrl 跳转成功'); + this.addLog('Promise 方式 pushUrl 跳转成功'); + }).catch((err: BusinessError) => { + console.error('Promise 方式 pushUrl 跳转失败:', JSON.stringify(err)); + this.addLog(`Promise 方式 pushUrl 跳转失败: ${err.message}`); + }); + } + + // 回调方式 pushUrl 跳转 + callbackPushUrl() { + this.addLog('执行回调方式 pushUrl 跳转...'); + + try { + router.pushUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface60_target', + params: { + fromPage: 'RoutingSupportstageInterface60', + testType: '回调pushUrl', + testId: 60, + timestamp: Date.now(), + data: { + title: '回调跳转测试', + description: '这是一个回调方式的pushUrl跳转测试', + callback: true + } + } + }, router.RouterMode.Standard, (err) => { + if (err) { + console.error('回调方式 pushUrl 跳转失败:', JSON.stringify(err)); + this.addLog(`回调方式 pushUrl 跳转失败: ${err.message}`); + } else { + console.info('回调方式 pushUrl 跳转成功'); + this.addLog('回调方式 pushUrl 跳转成功'); + } + }); + } catch (err) { + console.error('回调方式 pushUrl 跳转异常:', JSON.stringify(err)); + this.addLog(`回调方式 pushUrl 跳转异常: ${JSON.stringify(err)}`); + } + } + + // Standard 路由模式跳转 + standardModePushUrl() { + this.addLog('执行 Standard 模式 pushUrl 跳转...'); + + router.pushUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface60_target', + params: { + fromPage: 'RoutingSupportstageInterface60', + testType: 'Standard模式pushUrl', + testId: 60, + routerMode: 'Standard', + timestamp: Date.now(), + data: { + title: 'Standard模式跳转', + description: '这是Standard路由模式的pushUrl跳转测试', + mode: 'router.RouterMode.Standard' + } + } + }, router.RouterMode.Standard).then(() => { + console.info('Standard 模式 pushUrl 跳转成功'); + this.addLog('Standard 模式 pushUrl 跳转成功'); + }).catch((err: BusinessError) => { + console.error('Standard 模式 pushUrl 跳转失败:', JSON.stringify(err)); + this.addLog(`Standard 模式 pushUrl 跳转失败: ${err.message}`); + }); + } + + // Single 路由模式跳转 + singleModePushUrl() { + this.addLog('执行 Single 模式 pushUrl 跳转...'); + + router.pushUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface60_target', + params: { + fromPage: 'RoutingSupportstageInterface60', + testType: 'Single模式pushUrl', + testId: 60, + routerMode: 'Single', + timestamp: Date.now(), + data: { + title: 'Single模式跳转', + description: '这是Single路由模式的pushUrl跳转测试', + mode: 'router.RouterMode.Single' + } + } + }, router.RouterMode.Single).then(() => { + console.info('Single 模式 pushUrl 跳转成功'); + this.addLog('Single 模式 pushUrl 跳转成功'); + }).catch((err: BusinessError) => { + console.error('Single 模式 pushUrl 跳转失败:', JSON.stringify(err)); + this.addLog(`Single 模式 pushUrl 跳转失败: ${err.message}`); + }); + } + + build() { + Column() { + Text('Router pushUrl() 接口测试') + .fontSize(24) + .fontWeight(FontWeight.Bold) + .margin({ bottom: 20 }) + + Text('跳转到应用内指定页面') + .fontSize(16) + .fontColor('#666666') + .margin({ bottom: 15 }) + + // 路由栈信息显示 + Text('当前路由栈信息:') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ bottom: 10 }) + + Text(this.routerStackInfo) + .fontSize(14) + .textAlign(TextAlign.Center) + .backgroundColor('#e3f2fd') + .padding(15) + .borderRadius(10) + .margin({ bottom: 20 }) + .width('90%') + + // pushUrl 测试按钮组 + Button('基础 pushUrl 跳转') + .onClick(() => { + this.basicPushUrl(); + }) + .id('btn') + .width('85%') + .backgroundColor('#007dff') + .fontColor('#ffffff') + + Button('Promise 方式跳转') + .onClick(() => { + this.promisePushUrl(); + }) + .width('85%') + .backgroundColor('#4caf50') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Button('回调方式跳转') + .onClick(() => { + this.callbackPushUrl(); + }) + .width('85%') + .backgroundColor('#ff9800') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Button('Standard 模式跳转') + .onClick(() => { + this.standardModePushUrl(); + }) + .width('85%') + .backgroundColor('#9c27b0') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Button('Single 模式跳转') + .onClick(() => { + this.singleModePushUrl(); + }) + .width('85%') + .backgroundColor('#f44336') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Button('刷新路由栈信息') + .onClick(() => { + this.updateRouterStackInfo(); + this.addLog('已刷新路由栈信息'); + }) + .width('85%') + .backgroundColor('#607d8b') + .fontColor('#ffffff') + .margin({ top: 15 }) + + Divider() + .margin({ top: 20, bottom: 15 }) + + // 操作日志区域 + Text('操作日志:') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ bottom: 10 }) + + Scroll() { + Text(this.operationLog || '暂无操作记录') + .fontSize(12) + .textAlign(TextAlign.Start) + .backgroundColor('#f9f9f9') + .padding(10) + .borderRadius(8) + .width('100%') + .constraintSize({ minHeight: 100, maxHeight: 120 }) + } + .width('90%') + .height(120) + + Button('清空操作日志') + .onClick(() => { + this.operationLog = ''; + this.addLog('操作日志已清空'); + }) + .width('60%') + .backgroundColor('#9e9e9e') + .fontColor('#ffffff') + .margin({ top: 10 }) + + Divider() + .margin({ top: 15, bottom: 10 }) + + // 功能说明 + Text('功能说明:') + .fontSize(14) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + + Text('1. pushUrl() - 保留当前页面,跳转到新页面\n' + + '2. 支持参数传递和多种异步处理方式\n' + + '3. Standard模式: 标准跳转,会增加栈深度\n' + + '4. Single模式: 单实例跳转,清除同页面实例') + .fontSize(12) + .fontColor('#666666') + .textAlign(TextAlign.Start) + .alignSelf(ItemAlign.Start) + .lineHeight(18) + + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Start) + .padding(20) + .scrollable(ScrollDirection.Vertical) + } +} + diff --git a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface70.ets b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface70.ets index 583f981c..ae12a67a 100644 --- a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface70.ets +++ b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface70.ets @@ -12,24 +12,34 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import router from '@ohos.router' +import { BusinessError } from '@ohos.base'; +class routerParams { + data1:string + + constructor(str:string) { + this.data1 = str + } +} @Entry @Component -struct ProgressLinear01 { +struct routerPage70 { build() { Column() { - Text('进度条1:') - Progress({ value: 10, type: ProgressType.Linear }) - Text('进度条2:') - Progress({ value: 10, type: ProgressType.Linear }) - .style({strokeWidth: 0}) - Text('进度条3:') - Progress({ value: 10, type: ProgressType.Linear }) - .style({strokeWidth: -2}) - Text('进度条4:') - Progress({ value: 10, type: ProgressType.Linear }) - .style({strokeWidth: 20}) + Text('点击替换RepalceUrl:') + .onClick(()=>{ + try { + router.replaceUrl({ + url: 'pages/RoutingSupportstageInterface/RoutingSupportstageInterface70_copy', + params: new routerParams('message') + }, router.RouterMode.Standard) + } catch (err) { + console.error(`replaceUrl failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); + } + }) + .id('btn') } .width('100%') .height('100%') diff --git a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface80.ets b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface80.ets new file mode 100644 index 00000000..883f39d7 --- /dev/null +++ b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface80.ets @@ -0,0 +1,256 @@ +/** + * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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' +import { BusinessError } from '@ohos.base'; + +class innerParams { + data3:number[] + + constructor(tuple:number[]) { + this.data3 = tuple + } +} + +class routerParams { + data1:string + data2:innerParams + + constructor(str:string, tuple:number[]) { + this.data1 = str + this.data2 = new innerParams(tuple) + } +} + +@Entry +@Component +struct routerPage80 { + @State alertStatus: string = '返回询问对话框未启用'; + @State operationLog: string = ''; + @State isAlertEnabled: boolean = false; + + aboutToAppear() { + this.addLog('页面初始化完成'); + } + + // 添加操作日志 + addLog(message: string) { + const timestamp = new Date().toLocaleTimeString(); + this.operationLog = `[${timestamp}] ${message}\n${this.operationLog}`; + } + + // 启用返回询问对话框 + enableBackAlert() { + try { + router.enableAlertBeforeBackPage({ + message: '确定要离开此页面吗?此操作将丢失未保存的数据。' + }); + + this.alertStatus = '✅ 返回询问对话框已启用'; + this.isAlertEnabled = true; + this.addLog('成功启用返回询问对话框'); + console.info('enableAlertBeforeBackPage 调用成功'); + + } catch (err) { + this.alertStatus = '❌ 启用返回询问对话框失败'; + this.isAlertEnabled = false; + this.addLog(`启用返回询问对话框失败: ${JSON.stringify(err)}`); + console.error('enableAlertBeforeBackPage 调用失败:', JSON.stringify(err)); + } + } + + // 启用自定义消息的返回询问对话框 + enableCustomAlert() { + try { + router.enableAlertBeforeBackPage({ + message: '您正在进行重要操作,确认离开吗?\n\n• 未保存的更改将丢失\n• 当前进度不会保存\n\n请谨慎选择!' + }); + + this.alertStatus = '✅ 自定义返回询问对话框已启用'; + this.isAlertEnabled = true; + this.addLog('成功启用自定义返回询问对话框'); + console.info('enableAlertBeforeBackPage (自定义消息) 调用成功'); + + } catch (err) { + this.alertStatus = '❌ 启用自定义返回询问对话框失败'; + this.isAlertEnabled = false; + this.addLog(`启用自定义返回询问对话框失败: ${JSON.stringify(err)}`); + console.error('enableAlertBeforeBackPage (自定义消息) 调用失败:', JSON.stringify(err)); + } + } + + // 禁用返回询问对话框 + disableBackAlert() { + try { + router.disableAlertBeforeBackPage(); + + this.alertStatus = '⚪ 返回询问对话框已禁用'; + this.isAlertEnabled = false; + this.addLog('成功禁用返回询问对话框'); + console.info('disableAlertBeforeBackPage 调用成功'); + + } catch (err) { + this.alertStatus = '❌ 禁用返回询问对话框失败'; + this.addLog(`禁用返回询问对话框失败: ${JSON.stringify(err)}`); + console.error('disableAlertBeforeBackPage 调用失败:', JSON.stringify(err)); + } + } + + // 测试返回功能 + testBackNavigation() { + this.addLog('准备测试返回功能...'); + + try { + router.back(); + this.addLog('返回操作已执行'); + console.info('返回操作已执行'); + } catch (err) { + this.addLog(`返回操作失败: ${JSON.stringify(err)}`); + console.error('返回操作失败:', JSON.stringify(err)); + } + } + + build() { + Column() { + Text('Router enableAlertBeforeBackPage 接口测试') + .fontSize(22) + .fontWeight(FontWeight.Bold) + .margin({ bottom: 20 }) + + // 状态显示区域 + Text(this.alertStatus) + .fontSize(16) + .fontColor( + this.alertStatus.includes('✅') ? '#4caf50' : + this.alertStatus.includes('❌') ? '#f44336' : '#ff9800' + ) + .fontWeight(FontWeight.Medium) + .textAlign(TextAlign.Center) + .backgroundColor('#f8f9fa') + .padding(15) + .borderRadius(10) + .margin({ bottom: 20 }) + .width('90%') + + // 主要功能按钮 + Button('启用返回询问对话框') + .onClick(() => { + this.enableBackAlert(); + }) + .id('btn') + .width('85%') + .backgroundColor('#007dff') + .fontColor('#ffffff') + + Button('启用自定义消息对话框') + .onClick(() => { + this.enableCustomAlert(); + }) + .width('85%') + .backgroundColor('#4caf50') + .fontColor('#ffffff') + .margin({ top: 10 }) + + Button('禁用返回询问对话框') + .onClick(() => { + this.disableBackAlert(); + }) + .width('85%') + .backgroundColor('#ff9800') + .fontColor('#ffffff') + .margin({ top: 10 }) + + Button('测试返回功能') + .onClick(() => { + this.testBackNavigation(); + }) + .width('85%') + .backgroundColor(this.isAlertEnabled ? '#e91e63' : '#9e9e9e') + .fontColor('#ffffff') + .margin({ top: 15 }) + + Divider() + .margin({ top: 20, bottom: 15 }) + + // 状态指示器 + Row() { + Text('当前状态: ') + .fontSize(14) + .fontWeight(FontWeight.Medium) + + Text(this.isAlertEnabled ? '已启用询问对话框' : '未启用询问对话框') + .fontSize(14) + .fontColor(this.isAlertEnabled ? '#4caf50' : '#757575') + .fontWeight(FontWeight.Medium) + } + .margin({ bottom: 15 }) + + // 操作日志区域 + Text('操作日志:') + .fontSize(16) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ bottom: 10 }) + + Scroll() { + Text(this.operationLog || '暂无操作记录') + .fontSize(12) + .textAlign(TextAlign.Start) + .backgroundColor('#f9f9f9') + .padding(10) + .borderRadius(8) + .width('100%') + .constraintSize({ minHeight: 120, maxHeight: 180 }) + } + .width('90%') + .height(180) + + Button('清空操作日志') + .onClick(() => { + this.operationLog = ''; + this.addLog('操作日志已清空'); + }) + .width('60%') + .backgroundColor('#607d8b') + .fontColor('#ffffff') + .margin({ top: 10 }) + + Divider() + .margin({ top: 20, bottom: 15 }) + + // 功能说明 + Text('功能说明:') + .fontSize(14) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + + Text('1. enableAlertBeforeBackPage() - 启用返回询问对话框\n' + + '2. disableAlertBeforeBackPage() - 禁用返回询问对话框\n' + + '3. 支持自定义提示消息内容\n' + + '4. 启用后返回时会弹出确认对话框\n' + + '5. 用户可选择确认返回或取消操作') + .fontSize(12) + .fontColor('#666666') + .textAlign(TextAlign.Start) + .alignSelf(ItemAlign.Start) + .lineHeight(18) + + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Start) + .padding(20) + .scrollable(ScrollDirection.Vertical) + } +} -- Gitee From c1f8025eee4266eb9709a57f7c3ef92ee4d9208e Mon Sep 17 00:00:00 2001 From: zhoudede Date: Tue, 9 Sep 2025 17:26:04 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=864=E6=9D=A1?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhoudede --- .../RoutingSupportstageInterface.test.ets | 319 ++++++++++++---- .../RoutingSupportstageInterface10_target.ets | 282 ++++++++++++++ .../RoutingSupportstageInterface30_target.ets | 346 ++++++++++++++++++ .../RoutingSupportstageInterface60_target.ets | 273 ++++++++++++++ 4 files changed, 1143 insertions(+), 77 deletions(-) create mode 100644 sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface10_target.ets create mode 100644 sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface30_target.ets create mode 100644 sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface60_target.ets diff --git a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/test/RoutingSupportstageInterfaceTest/RoutingSupportstageInterface.test.ets b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/test/RoutingSupportstageInterfaceTest/RoutingSupportstageInterface.test.ets index a46d95ab..7081e61f 100644 --- a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/test/RoutingSupportstageInterfaceTest/RoutingSupportstageInterface.test.ets +++ b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/test/RoutingSupportstageInterfaceTest/RoutingSupportstageInterface.test.ets @@ -1,77 +1,242 @@ -/** - * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect, Level } from '@ohos/hypium' -import Settings from '../model/Settings' -import windowSnap from '../model/snapShot' -import Logger from '../model/Logger' -import Utils from '../model/Utils' -import router from '@ohos.router' -import { BusinessError } from '@ohos.base'; - -class routerParams { - data1:string - - constructor(str:string) { - this.data1 = str - } -} - - -export default function RoutingSupportstageInterface() { - - describe('RoutingSupportstageInterface', () => { - afterEach(async (done: Function) => { - if (Settings.windowClass == undefined) { - return - } - - Settings.windowClass.destroyWindow((err) => { - if (err.code) { - Logger.error('TEST', `Failed to destroy the window. Cause : ${JSON.stringify(err)}`) - return; - } - Logger.info('TEST', `Succeeded in destroy the window.`); - }) - await Utils.sleep(1000); - done() - }) - - /* - * @tc.number SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0070 - * @tc.name SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0070 - * @tc.desc ProgressLinear sets the strokeRadius property - * @tc.size MediumTest - * @tc.type Function - * @tc.level 0 - */ - it('SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0070', Level.LEVEL1, async (done: Function) => { - Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0070 start.`); - try { - router.replaceUrl({ - url: 'pages/RoutingSupportstageInterface/RoutingSupportstageInterface70', - params: new routerParams('message') - }, router.RouterMode.Standard) - } catch (err) { - console.error(`replaceUrl failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); - } - windowSnap.snapShot(); - await Utils.sleep(1000); - Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0070 finish.`); - done(); - }) - - }) -} +/** + * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect, Level } from '@ohos/hypium' +import Settings from '../model/Settings' +import windowSnap from '../model/snapShot' +import Logger from '../model/Logger' +import Utils from '../model/Utils' +import { Driver, ON, Component,} from '@kit.TestKit'; + + +export default function RoutingSupportstageInterface() { + + describe('RoutingSupportstageInterface', () => { + afterEach(async (done: Function) => { + if (Settings.windowClass == undefined) { + return + } + + Settings.windowClass.destroyWindow((err) => { + if (err.code) { + Logger.error('TEST', `Failed to destroy the window. Cause : ${JSON.stringify(err)}`) + return; + } + Logger.info('TEST', `Succeeded in destroy the window.`); + }) + await Utils.sleep(1000); + done() + }) + + /* + * @tc.number SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0010 + * @tc.name SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0010 + * @tc.desc Test router.back() interface to navigate back to previous page or specified page + * @tc.size MediumTest + * @tc.type Function + * @tc.level 0 + */ + it('SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0010', Level.LEVEL1, async (done: Function) => { + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0010 start.`); + Settings.createWindow("testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface10") + let driver:Driver = Driver.create(); + // 通过ID定位(推荐) + let component = await driver.waitForComponent(ON.id('btn'), 1000); // 显式等待组件出现[6](@ref) + await component.click(); + await driver.waitForIdle(500,2000); + windowSnap.snapShot(); + await Utils.sleep(1000); + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0010 finish.`); + done(); + }) + + /* + * @tc.number SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0030 + * @tc.name SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0030 + * @tc.desc Test router.getLength() interface to get current page stack count + * @tc.size MediumTest + * @tc.type Function + * @tc.level 0 + */ + it('SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0030', Level.LEVEL1, async (done: Function) => { + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0030 start.`); + Settings.createWindow("testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface30") + let driver:Driver = Driver.create(); + // 通过ID定位(推荐) + let component = await driver.waitForComponent(ON.id('btn'), 1000); // 显式等待组件出现[6](@ref) + await component.click(); + await driver.waitForIdle(500,2000); + windowSnap.snapShot(); + await Utils.sleep(1000); + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0030 finish.`); + done(); + }) + + /* + * @tc.number SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0050 + * @tc.name SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0050 + * @tc.desc Test router.getState() interface to get current page state information + * @tc.size MediumTest + * @tc.type Function + * @tc.level 0 + */ + it('SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0050', Level.LEVEL1, async (done: Function) => { + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0050 start.`); + Settings.createWindow("testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface50") + let driver:Driver = Driver.create(); + // 通过ID定位(推荐) + let component = await driver.waitForComponent(ON.id('btn'), 1000); // 显式等待组件出现[6](@ref) + await component.click(); + await driver.waitForIdle(500,2000); + windowSnap.snapShot(); + await Utils.sleep(1000); + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0050 finish.`); + done(); + }) + + /* + * @tc.number SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0060 + * @tc.name SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0060 + * @tc.desc Test router.pushUrl() interface to navigate to specified pages within the app + * @tc.size MediumTest + * @tc.type Function + * @tc.level 0 + */ + it('SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0060', Level.LEVEL1, async (done: Function) => { + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0060 start.`); + Settings.createWindow("testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface60") + let driver:Driver = Driver.create(); + // 通过ID定位(推荐) + let component = await driver.waitForComponent(ON.id('btn'), 1000); // 显式等待组件出现[6](@ref) + await component.click(); + await driver.waitForIdle(500,2000); + windowSnap.snapShot(); + await Utils.sleep(1000); + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0060 finish.`); + done(); + }) + + /* + * @tc.number SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0070 + * @tc.name SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0070 + * @tc.desc ProgressLinear sets the strokeRadius property + * @tc.size MediumTest + * @tc.type Function + * @tc.level 0 + */ + it('SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0070', Level.LEVEL1, async (done: Function) => { + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0070 start.`); + Settings.createWindow("testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface70") + let driver:Driver = Driver.create(); + // 通过ID定位(推荐) + let component = await driver.waitForComponent(ON.id('btn'), 1000); // 显式等待组件出现[6](@ref) + await component.click(); + await driver.waitForIdle(500,2000); + windowSnap.snapShot(); + await Utils.sleep(1000); + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0070 finish.`); + done(); + }) + + + /* + * @tc.number SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0080 + * @tc.name SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0080 + * @tc.desc Test router.enableAlertBeforeBackPage interface to enable back page confirmation dialog + * @tc.size MediumTest + * @tc.type Function + * @tc.level 0 + */ + it('SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0080', Level.LEVEL1, async (done: Function) => { + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0080 start.`); + Settings.createWindow("testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface80") + let driver:Driver = Driver.create(); + // 通过ID定位(推荐) + let component = await driver.waitForComponent(ON.id('btn'), 1000); // 显式等待组件出现[6](@ref) + await component.click(); + await driver.waitForIdle(500,2000); + windowSnap.snapShot(); + await Utils.sleep(1000); + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0080 finish.`); + done(); + }) + + /* + * @tc.number SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0040 + * @tc.name SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0040 + * @tc.desc ProgressLinear sets the strokeRadius property + * @tc.size MediumTest + * @tc.type Function + * @tc.level 0 + */ + it('SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0040', Level.LEVEL1, async (done: Function) => { + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0040 start.`); + Settings.createWindow("testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface40") + let driver:Driver = Driver.create(); + // 通过ID定位(推荐) + let component = await driver.waitForComponent(ON.id('btn'), 1000); // 显式等待组件出现[6](@ref) + await component.click(); + await driver.waitForIdle(500,2000); + windowSnap.snapShot(); + await Utils.sleep(1000); + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0040 finish.`); + done(); + }) + + /* + * @tc.number SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0100 + * @tc.name SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0100 + * @tc.desc Test named routing interface functionality with successful navigation + * @tc.size MediumTest + * @tc.type Function + * @tc.level 0 + */ + it('SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0100', Level.LEVEL1, async (done: Function) => { + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0100 start.`); + Settings.createWindow("testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface100") + let driver:Driver = Driver.create(); + // 通过ID定位(推荐) + let component = await driver.waitForComponent(ON.id('btn'), 1000); // 显式等待组件出现[6](@ref) + await component.click(); + await driver.waitForIdle(500,2000); + windowSnap.snapShot(); + await Utils.sleep(1000); + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0100 finish.`); + done(); + }) + + /* + * @tc.number SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0020 + * @tc.name SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0020 + * @tc.desc Test router.clear() functionality to clear all pages in the router stack + * @tc.size MediumTest + * @tc.type Function + * @tc.level 0 + */ + it('SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0020', Level.LEVEL1, async (done: Function) => { + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0020 start.`); + Settings.createWindow("testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface20") + let driver:Driver = Driver.create(); + // 通过ID定位(推荐) + let component = await driver.waitForComponent(ON.id('btn'), 1000); // 显式等待组件出现[6](@ref) + await component.click(); + await driver.waitForIdle(500,2000); + windowSnap.snapShot(); + await Utils.sleep(1000); + Logger.info('TEST', `SUB_ACE_Routing_SUPPORTSTAGE_INTERFACE_0020 finish.`); + done(); + }) + }) +} diff --git a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface10_target.ets b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface10_target.ets new file mode 100644 index 00000000..3ced7410 --- /dev/null +++ b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface10_target.ets @@ -0,0 +1,282 @@ +/** + * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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' +import { BusinessError } from '@ohos.base'; + +@Entry +@Component +struct routerTargetPage10 { + @State receivedParams: string = ''; + @State routerStackInfo: string = ''; + @State operationLog: string = ''; + + aboutToAppear() { + this.loadReceivedParams(); + this.updateRouterStackInfo(); + this.addLog('Router.back 测试目标页面初始化完成'); + } + + // 加载接收到的参数 + loadReceivedParams() { + try { + const params = router.getParams(); + if (params) { + this.receivedParams = `接收到的参数:\n${JSON.stringify(params, null, 2)}`; + console.info('目标页面接收到参数:', JSON.stringify(params)); + } else { + this.receivedParams = '未接收到任何参数'; + } + } catch (err) { + this.receivedParams = '获取参数失败'; + console.error('获取参数失败:', JSON.stringify(err)); + } + } + + // 获取当前路由栈信息 + updateRouterStackInfo() { + try { + const length = router.getLength(); + const state = router.getState(); + this.routerStackInfo = `路由栈深度: ${length}\n当前页面: ${state.name || '未知'}\n当前路径: ${state.path || '未知'}`; + } catch (err) { + this.routerStackInfo = '获取路由信息失败'; + } + } + + // 添加操作日志 + addLog(message: string) { + const timestamp = new Date().toLocaleTimeString(); + this.operationLog = `[${timestamp}] ${message}\n${this.operationLog}`; + } + + // 基础返回到上一页 + basicBack() { + this.addLog('执行基础返回操作 router.back()...'); + + try { + router.back(); + console.info('基础返回操作已执行'); + this.addLog('基础返回操作已执行'); + } catch (err) { + console.error('基础返回失败:', JSON.stringify(err)); + this.addLog(`基础返回失败: ${JSON.stringify(err)}`); + } + } + + // Promise方式返回 + promiseBack() { + this.addLog('执行Promise方式返回...'); + + router.back() + .then(() => { + console.info('Promise方式返回成功'); + this.addLog('Promise方式返回成功'); + }) + .catch((err: BusinessError) => { + console.error('Promise方式返回失败:', JSON.stringify(err)); + this.addLog(`Promise方式返回失败: ${err.message}`); + }); + } + + // 返回到指定页面 + backToSpecificPage() { + this.addLog('执行返回到指定页面操作...'); + + try { + router.back({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface10' + }); + console.info('返回到指定页面操作已执行'); + this.addLog('返回到指定页面操作已执行'); + } catch (err) { + console.error('返回到指定页面失败:', JSON.stringify(err)); + this.addLog(`返回到指定页面失败: ${JSON.stringify(err)}`); + } + } + + // Promise方式返回到指定页面 + promiseBackToSpecificPage() { + this.addLog('执行Promise方式返回到指定页面...'); + + router.back({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface10' + }).then(() => { + console.info('Promise方式返回到指定页面成功'); + this.addLog('Promise方式返回到指定页面成功'); + }).catch((err: BusinessError) => { + console.error('Promise方式返回到指定页面失败:', JSON.stringify(err)); + this.addLog(`Promise方式返回到指定页面失败: ${err.message}`); + }); + } + + // 回调方式返回 + callbackBack() { + this.addLog('执行回调方式返回...'); + + try { + router.back(undefined, (err) => { + if (err) { + console.error('回调方式返回失败:', JSON.stringify(err)); + this.addLog(`回调方式返回失败: ${err.message}`); + } else { + console.info('回调方式返回成功'); + this.addLog('回调方式返回成功'); + } + }); + } catch (err) { + console.error('回调方式返回异常:', JSON.stringify(err)); + this.addLog(`回调方式返回异常: ${JSON.stringify(err)}`); + } + } + + build() { + Column() { + Text('Router back() 目标测试页面') + .fontSize(22) + .fontWeight(FontWeight.Bold) + .fontColor('#e91e63') + .margin({ bottom: 20 }) + + Text('✅ 已成功跳转到目标页面!') + .fontSize(16) + .fontColor('#4caf50') + .fontWeight(FontWeight.Medium) + .margin({ bottom: 15 }) + + // 路由栈信息显示 + Text(this.routerStackInfo) + .fontSize(14) + .textAlign(TextAlign.Center) + .backgroundColor('#e8f5e8') + .padding(15) + .borderRadius(10) + .margin({ bottom: 15 }) + .width('90%') + + // 接收到的参数显示 + Text(this.receivedParams) + .fontSize(10) + .textAlign(TextAlign.Start) + .backgroundColor('#f5f5f5') + .padding(10) + .borderRadius(8) + .margin({ bottom: 20 }) + .width('90%') + .constraintSize({ maxHeight: 80 }) + + // 各种返回方式测试按钮 + Button('基础返回 (router.back)') + .onClick(() => { + this.basicBack(); + }) + .width('85%') + .backgroundColor('#f44336') + .fontColor('#ffffff') + + Button('Promise方式返回') + .onClick(() => { + this.promiseBack(); + }) + .width('85%') + .backgroundColor('#9c27b0') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Button('回调方式返回') + .onClick(() => { + this.callbackBack(); + }) + .width('85%') + .backgroundColor('#ff5722') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Button('返回到指定页面') + .onClick(() => { + this.backToSpecificPage(); + }) + .width('85%') + .backgroundColor('#3f51b5') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Button('Promise返回到指定页面') + .onClick(() => { + this.promiseBackToSpecificPage(); + }) + .width('85%') + .backgroundColor('#009688') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Button('刷新页面信息') + .onClick(() => { + this.loadReceivedParams(); + this.updateRouterStackInfo(); + this.addLog('已刷新页面信息'); + }) + .width('85%') + .backgroundColor('#607d8b') + .fontColor('#ffffff') + .margin({ top: 15 }) + + Divider() + .margin({ top: 15, bottom: 10 }) + + // 操作日志区域 + Text('操作日志:') + .fontSize(14) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ bottom: 8 }) + + Scroll() { + Text(this.operationLog || '暂无操作记录') + .fontSize(11) + .textAlign(TextAlign.Start) + .backgroundColor('#f9f9f9') + .padding(8) + .borderRadius(6) + .width('100%') + .constraintSize({ minHeight: 80, maxHeight: 120 }) + } + .width('90%') + .height(120) + + // 测试说明 + Text('测试方法说明:') + .fontSize(12) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ top: 10, bottom: 5 }) + + Text('• 基础返回: router.back()\n' + + '• 指定页面: router.back({url: "xxx"})\n' + + '• Promise: 使用.then().catch()\n' + + '• 回调: router.back(options, callback)') + .fontSize(10) + .fontColor('#666666') + .textAlign(TextAlign.Start) + .alignSelf(ItemAlign.Start) + .lineHeight(15) + + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Start) + .padding(15) + .scrollable(ScrollDirection.Vertical) + } +} diff --git a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface30_target.ets b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface30_target.ets new file mode 100644 index 00000000..b4f678bd --- /dev/null +++ b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface30_target.ets @@ -0,0 +1,346 @@ +/** + * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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' +import { BusinessError } from '@ohos.base'; + +@Entry +@Component +struct routerTargetPage30 { + @State currentLength: number = 0; + @State beforeLength: number = 0; + @State operation: string = ''; + @State receivedParams: string = ''; + @State operationLog: string = ''; + @State lengthComparison: string = ''; + + aboutToAppear() { + this.loadReceivedParams(); + this.getCurrentLength(); + this.addLog('getLength 测试目标页面初始化完成'); + } + + // 加载接收到的参数 + loadReceivedParams() { + try { + const params = router.getParams(); + if (params) { + this.beforeLength = params['beforeLength'] || 0; + this.operation = params['operation'] || '未知操作'; + this.receivedParams = `接收到的参数:\n${JSON.stringify(params, null, 2)}`; + console.info('目标页面接收到参数:', JSON.stringify(params)); + this.addLog(`成功接收参数,来源操作: ${this.operation}`); + } else { + this.receivedParams = '未接收到任何参数'; + this.addLog('未接收到任何参数'); + } + } catch (err) { + this.receivedParams = '获取参数失败'; + this.addLog('获取参数失败'); + console.error('获取参数失败:', JSON.stringify(err)); + } + } + + // 获取当前页面栈长度 + getCurrentLength() { + try { + const length = router.getLength(); + this.currentLength = length; + + // 比较跳转前后的长度变化 + this.updateLengthComparison(); + + console.info(`目标页面 router.getLength() 返回值: ${length}`); + this.addLog(`当前页面栈长度: ${length}`); + + } catch (err) { + this.currentLength = -1; + console.error('getLength() 调用失败:', JSON.stringify(err)); + this.addLog(`获取页面栈长度失败: ${JSON.stringify(err)}`); + } + } + + // 更新长度对比信息 + updateLengthComparison() { + if (this.beforeLength > 0) { + const difference = this.currentLength - this.beforeLength; + + if (difference > 0) { + this.lengthComparison = `✅ 栈深度增加了 ${difference} 层 (${this.beforeLength} → ${this.currentLength})`; + } else if (difference === 0) { + this.lengthComparison = `⚪ 栈深度保持不变 (${this.beforeLength} → ${this.currentLength})`; + } else { + this.lengthComparison = `📉 栈深度减少了 ${Math.abs(difference)} 层 (${this.beforeLength} → ${this.currentLength})`; + } + } else { + this.lengthComparison = `当前栈深度: ${this.currentLength}`; + } + } + + // 添加操作日志 + addLog(message: string) { + const timestamp = new Date().toLocaleTimeString(); + this.operationLog = `[${timestamp}] ${message}\n${this.operationLog}`; + } + + // 继续跳转到自己,测试栈深度累积 + continuePush() { + this.addLog('继续跳转到自己,测试栈深度累积...'); + + const currentLength = this.currentLength; + + router.pushUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface30_target', + params: { + fromPage: 'RoutingSupportstageInterface30_target', + beforeLength: currentLength, + testId: 30, + operation: 'continuePush', + level: currentLength + 1, + timestamp: Date.now() + } + }).then(() => { + console.info('继续跳转成功'); + this.addLog(`继续跳转成功,跳转前栈长度: ${currentLength}`); + }).catch((err: BusinessError) => { + console.error('继续跳转失败:', JSON.stringify(err)); + this.addLog(`继续跳转失败: ${err.message}`); + }); + } + + // 返回上一页并观察栈深度变化 + goBackAndCheck() { + this.addLog(`准备返回上一页,当前栈长度: ${this.currentLength}`); + + try { + router.back(); + console.info('返回操作已执行'); + this.addLog('返回操作已执行'); + } catch (err) { + console.error('返回失败:', JSON.stringify(err)); + this.addLog(`返回失败: ${JSON.stringify(err)}`); + } + } + + // 返回到主页面 + backToMainPage() { + this.addLog('准备返回到主页面...'); + + router.back({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface30' + }).then(() => { + console.info('返回到主页面成功'); + this.addLog('返回到主页面成功'); + }).catch((err: BusinessError) => { + console.error('返回到主页面失败:', JSON.stringify(err)); + this.addLog(`返回到主页面失败: ${err.message}`); + }); + } + + // 替换到自己,测试栈深度不变 + replaceSelf() { + this.addLog('替换到自己,测试栈深度是否保持不变...'); + + const currentLength = this.currentLength; + + router.replaceUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface30_target', + params: { + fromPage: 'RoutingSupportstageInterface30_target', + beforeLength: currentLength, + testId: 30, + operation: 'replaceSelf', + timestamp: Date.now() + } + }).then(() => { + console.info('替换操作成功'); + this.addLog(`替换操作成功,替换前栈长度: ${currentLength}`); + }).catch((err: BusinessError) => { + console.error('替换操作失败:', JSON.stringify(err)); + this.addLog(`替换操作失败: ${err.message}`); + }); + } + + build() { + Column() { + Text('getLength 测试目标页面') + .fontSize(22) + .fontWeight(FontWeight.Bold) + .fontColor('#007dff') + .margin({ bottom: 15 }) + + Text('✅ 跳转成功!正在验证栈深度变化') + .fontSize(16) + .fontColor('#4caf50') + .fontWeight(FontWeight.Medium) + .margin({ bottom: 15 }) + + // 操作类型显示 + if (this.operation !== '') { + Text(`来源操作: ${this.operation}`) + .fontSize(14) + .fontColor('#e91e63') + .fontWeight(FontWeight.Medium) + .backgroundColor('#fce4ec') + .padding(8) + .borderRadius(6) + .margin({ bottom: 15 }) + } + + // 当前栈长度显示 + Row() { + Text('当前栈深度:') + .fontSize(18) + .fontWeight(FontWeight.Bold) + + Text(`${this.currentLength >= 0 ? this.currentLength : '获取失败'}`) + .fontSize(28) + .fontWeight(FontWeight.Bold) + .fontColor('#007dff') + .margin({ left: 10 }) + + Text('页面') + .fontSize(16) + .fontColor('#666666') + .margin({ left: 5 }) + } + .margin({ bottom: 15 }) + + // 长度变化对比 + Text(this.lengthComparison) + .fontSize(14) + .fontColor( + this.lengthComparison.includes('✅') ? '#4caf50' : + this.lengthComparison.includes('📉') ? '#f44336' : '#ff9800' + ) + .backgroundColor('#f8f9fa') + .padding(10) + .borderRadius(8) + .margin({ bottom: 15 }) + .width('90%') + + // 操作按钮 + Button('刷新栈长度信息') + .onClick(() => { + this.getCurrentLength(); + this.addLog('已刷新栈长度信息'); + }) + .width('80%') + .backgroundColor('#007dff') + .fontColor('#ffffff') + + Button('继续跳转 (pushUrl)') + .onClick(() => { + this.continuePush(); + }) + .width('80%') + .backgroundColor('#4caf50') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Button('替换到自己 (replaceUrl)') + .onClick(() => { + this.replaceSelf(); + }) + .width('80%') + .backgroundColor('#ff9800') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Button('返回上一页 (back)') + .onClick(() => { + this.goBackAndCheck(); + }) + .width('80%') + .backgroundColor('#f44336') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Button('返回到主页面') + .onClick(() => { + this.backToMainPage(); + }) + .width('80%') + .backgroundColor('#9c27b0') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Divider() + .margin({ top: 15, bottom: 10 }) + + // 接收到的参数显示 + Text('接收到的参数:') + .fontSize(14) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ bottom: 8 }) + + Scroll() { + Text(this.receivedParams) + .fontSize(10) + .textAlign(TextAlign.Start) + .backgroundColor('#f5f5f5') + .padding(8) + .borderRadius(6) + .width('100%') + } + .width('90%') + .height(100) + .margin({ bottom: 15 }) + + // 操作日志区域 + Text('操作日志:') + .fontSize(14) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ bottom: 8 }) + + Scroll() { + Text(this.operationLog || '暂无操作记录') + .fontSize(11) + .textAlign(TextAlign.Start) + .backgroundColor('#f9f9f9') + .padding(8) + .borderRadius(6) + .width('100%') + .constraintSize({ minHeight: 80, maxHeight: 120 }) + } + .width('90%') + .height(120) + + // 验证说明 + Text('验证项目:') + .fontSize(12) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ top: 10, bottom: 5 }) + + Text('• pushUrl: 栈深度应该+1\n' + + '• replaceUrl: 栈深度保持不变\n' + + '• back: 栈深度应该-1\n' + + '• getLength(): 返回正确的栈深度值') + .fontSize(10) + .fontColor('#666666') + .textAlign(TextAlign.Start) + .alignSelf(ItemAlign.Start) + .lineHeight(15) + + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Start) + .padding(15) + .scrollable(ScrollDirection.Vertical) + } +} diff --git a/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface60_target.ets b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface60_target.ets new file mode 100644 index 00000000..7ae197e6 --- /dev/null +++ b/sample/ui_compare/uiCompareTest_09/entry/src/ohosTest/ets/testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface60_target.ets @@ -0,0 +1,273 @@ +/** + * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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' +import { BusinessError } from '@ohos.base'; + +@Entry +@Component +struct routerTargetPage60 { + @State receivedParams: string = ''; + @State routerStackInfo: string = ''; + @State testType: string = ''; + @State operationLog: string = ''; + + aboutToAppear() { + this.loadReceivedParams(); + this.updateRouterStackInfo(); + this.addLog('pushUrl 跳转目标页面初始化完成'); + } + + // 加载接收到的参数 + loadReceivedParams() { + try { + const params = router.getParams(); + if (params) { + this.testType = params['testType'] || '未知测试类型'; + this.receivedParams = `接收到的参数:\n${JSON.stringify(params, null, 2)}`; + console.info('pushUrl 目标页面接收到参数:', JSON.stringify(params)); + this.addLog(`成功接收参数,测试类型: ${this.testType}`); + } else { + this.receivedParams = '未接收到任何参数'; + this.addLog('未接收到任何参数'); + } + } catch (err) { + this.receivedParams = '获取参数失败'; + this.addLog('获取参数失败'); + console.error('获取参数失败:', JSON.stringify(err)); + } + } + + // 获取当前路由栈信息 + updateRouterStackInfo() { + try { + const length = router.getLength(); + const state = router.getState(); + this.routerStackInfo = `路由栈深度: ${length}\n当前页面: ${state.name || '未知'}\n当前路径: ${state.path || '未知'}`; + } catch (err) { + this.routerStackInfo = '获取路由信息失败'; + } + } + + // 添加操作日志 + addLog(message: string) { + const timestamp = new Date().toLocaleTimeString(); + this.operationLog = `[${timestamp}] ${message}\n${this.operationLog}`; + } + + // 继续pushUrl到自己(测试多层跳转) + continuePushUrl() { + this.addLog('继续 pushUrl 到自己...'); + + router.pushUrl({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface60_target', + params: { + fromPage: 'RoutingSupportstageInterface60_target', + testType: '多层pushUrl', + testId: 60, + level: (router.getLength() + 1), + timestamp: Date.now(), + data: { + title: '多层跳转测试', + description: '这是多层pushUrl跳转测试', + currentLevel: router.getLength() + } + } + }).then(() => { + console.info('继续 pushUrl 跳转成功'); + this.addLog('继续 pushUrl 跳转成功'); + }).catch((err: BusinessError) => { + console.error('继续 pushUrl 跳转失败:', JSON.stringify(err)); + this.addLog(`继续 pushUrl 跳转失败: ${err.message}`); + }); + } + + // 返回到上一页 + goBack() { + this.addLog('准备返回上一页...'); + + try { + router.back(); + console.info('返回操作已执行'); + this.addLog('返回操作已执行'); + } catch (err) { + console.error('返回失败:', JSON.stringify(err)); + this.addLog(`返回失败: ${JSON.stringify(err)}`); + } + } + + // 返回到主页面 + backToMainPage() { + this.addLog('准备返回到主页面...'); + + router.back({ + url: 'testability/pages/RoutingSupportstageInterface/RoutingSupportstageInterface60' + }).then(() => { + console.info('返回到主页面成功'); + this.addLog('返回到主页面成功'); + }).catch((err: BusinessError) => { + console.error('返回到主页面失败:', JSON.stringify(err)); + this.addLog(`返回到主页面失败: ${err.message}`); + }); + } + + build() { + Column() { + Text('pushUrl 跳转目标页面') + .fontSize(22) + .fontWeight(FontWeight.Bold) + .fontColor('#007dff') + .margin({ bottom: 15 }) + + Text('✅ pushUrl 跳转成功!') + .fontSize(18) + .fontColor('#4caf50') + .fontWeight(FontWeight.Medium) + .margin({ bottom: 15 }) + + // 测试类型显示 + if (this.testType !== '') { + Text(`测试类型: ${this.testType}`) + .fontSize(16) + .fontColor('#e91e63') + .fontWeight(FontWeight.Medium) + .backgroundColor('#fce4ec') + .padding(10) + .borderRadius(8) + .margin({ bottom: 15 }) + } + + // 路由栈信息显示 + Text('当前路由栈信息:') + .fontSize(14) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ bottom: 8 }) + + Text(this.routerStackInfo) + .fontSize(12) + .textAlign(TextAlign.Center) + .backgroundColor('#e8f5e8') + .padding(12) + .borderRadius(8) + .margin({ bottom: 15 }) + .width('90%') + + // 接收到的参数显示 + Text('接收到的参数:') + .fontSize(14) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ bottom: 8 }) + + Scroll() { + Text(this.receivedParams) + .fontSize(10) + .textAlign(TextAlign.Start) + .backgroundColor('#f5f5f5') + .padding(10) + .borderRadius(8) + .width('100%') + } + .width('90%') + .height(120) + .margin({ bottom: 15 }) + + // 操作按钮 + Button('继续 pushUrl 到自己') + .onClick(() => { + this.continuePushUrl(); + }) + .width('80%') + .backgroundColor('#ff9800') + .fontColor('#ffffff') + + Button('返回上一页 (back)') + .onClick(() => { + this.goBack(); + }) + .width('80%') + .backgroundColor('#f44336') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Button('返回到主页面') + .onClick(() => { + this.backToMainPage(); + }) + .width('80%') + .backgroundColor('#9c27b0') + .fontColor('#ffffff') + .margin({ top: 8 }) + + Button('刷新页面信息') + .onClick(() => { + this.loadReceivedParams(); + this.updateRouterStackInfo(); + this.addLog('已刷新页面信息'); + }) + .width('80%') + .backgroundColor('#607d8b') + .fontColor('#ffffff') + .margin({ top: 15 }) + + Divider() + .margin({ top: 15, bottom: 10 }) + + // 操作日志区域 + Text('操作日志:') + .fontSize(14) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ bottom: 8 }) + + Scroll() { + Text(this.operationLog || '暂无操作记录') + .fontSize(11) + .textAlign(TextAlign.Start) + .backgroundColor('#f9f9f9') + .padding(8) + .borderRadius(6) + .width('100%') + .constraintSize({ minHeight: 80, maxHeight: 100 }) + } + .width('90%') + .height(100) + + // 验证说明 + Text('验证项目:') + .fontSize(12) + .fontWeight(FontWeight.Bold) + .alignSelf(ItemAlign.Start) + .margin({ top: 10, bottom: 5 }) + + Text('• 成功跳转到目标页面\n' + + '• 正确接收传递的参数\n' + + '• 路由栈深度正确增加\n' + + '• 保留了上一页面状态') + .fontSize(10) + .fontColor('#666666') + .textAlign(TextAlign.Start) + .alignSelf(ItemAlign.Start) + .lineHeight(15) + + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Start) + .padding(15) + .scrollable(ScrollDirection.Vertical) + } +} + -- Gitee