2
* Copyright (c) 2024 Huawei Device Co., Ltd.
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
* you may not use this file except in compliance with the License.
5
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
* See the License for the specific language governing permissions and
13
* limitations under the License.
15
import { NativeModule } from "./NativeModule"
16
import { ArkButtonPeer } from "@arkoala/arkui/ArkButtonPeer"
17
import { ArkUINodeType } from "@arkoala/arkui/ArkUINodeType"
18
import { ButtonType } from '@arkoala/arkui/ArkButtonInterfaces'
23
TextHeightAdaptivePolicy,
49
BlurOptions } from "./dts-exports"
51
const testString1000 = "One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand";
55
backdropBlur: 1456ms for 5000000 iteration, 291ms per 1M iterations
56
widthAttributeString: 1006ms for 5000000 iteration, 201ms per 1M iterations
60
"backdropBlur: 3518ms for 5000000 iteration, 704ms per 5000000 iterations"
61
"widthAttributeString: 1380ms for 5000000 iteration, 276ms per 5000000 iterations"
64
backdropBlur: 284ms for 5000000 iteration, 57ms per 1M iterations
65
widthAttributeString: 502ms for 5000000 iteration, 100ms per 1M iterations
68
function checkPerf(count: number) {
69
let start = Date.now()
70
for (let i = 0; i < count; i++) {
71
NativeModule._TestPerfNumber(i)
73
let passed = Date.now() - start
74
console.log(`NUMBER: ${passed}ms for ${count} iteration, ${Math.round(passed / count * 1000000)}ms per 1M iterations`)
77
for (let i = 0; i < count; i++) {
78
let data = new byte[5]
80
data[1] = (i >> 24) as byte
81
data[2] = (i >> 16) as byte
82
data[3] = (i >> 8) as byte
83
data[4] = (i >> 0) as byte
84
NativeModule._TestPerfNumberWithArray(data, data.length)
86
passed = Date.now() - start
87
console.log(`ARRAY: ${passed}ms for ${count} iteration, ${Math.round(passed / count * 1000000)}ms per 1M iterations`)
90
export function getNativeLog(): string {
91
let ptr = NativeModule._GetGroupedLog(1)
92
let length = NativeModule._StringLength(ptr)
93
let data = new byte[length]
94
NativeModule._StringData(ptr, data, length)
95
NativeModule._InvokeFinalizer(ptr, NativeModule._GetStringFinalizer())
96
// TODO: better string decoding.
97
let result = new StringBuilder()
98
for (let i = 0; i < length; i++) {
99
result.append(String.fromCharCode(data[i] as number))
101
return result.toString()
104
export function checkResult(name: string, test: () => void, expected: string) {
105
NativeModule._StartGroupedLog(1)
107
NativeModule._StopGroupedLog(1)
108
const actual = getNativeLog()
109
if (actual != expected) {
110
console.log(`TEST ${name} FAIL:\n EXPECTED "${expected}"\n ACTUAL "${actual}"`)
111
console.log(`output: ${actual}`)
113
console.log(`TEST ${name} PASS`)
117
class LabelStyleImpl implements LabelStyle {
118
_textOverflow: TextOverflow | undefined
119
_maxLines: number | undefined
120
_minFontSize: number | ResourceStr | undefined
121
_maxFontSize: number | ResourceStr | undefined
122
_heightAdaptivePolicy: TextHeightAdaptivePolicy | undefined
123
_font: Font | undefined
125
constructor(maxLines?: number) {
126
this._maxLines = maxLines
129
get overflow(): TextOverflow | undefined {
130
return this._textOverflow
132
set overflow(arg: TextOverflow | undefined) {
133
this._textOverflow = arg
136
get maxLines(): number | undefined {
137
return this._maxLines
139
set maxLines(arg: number | undefined) {
143
get minFontSize(): number | ResourceStr | undefined {
144
return this._minFontSize
146
set minFontSize(arg: number | ResourceStr | undefined) {
147
this._minFontSize = arg
150
get maxFontSize(): number | ResourceStr | undefined {
151
return this._maxFontSize
153
set maxFontSize(arg: number | ResourceStr | undefined) {
154
this._maxFontSize = arg
157
get heightAdaptivePolicy(): TextHeightAdaptivePolicy | undefined {
158
return this._heightAdaptivePolicy
160
set heightAdaptivePolicy(arg: TextHeightAdaptivePolicy | undefined) {
161
this._heightAdaptivePolicy = arg
164
get font(): Font | undefined {
167
set font(arg: Font | undefined) {
172
class SheetTitleOptionsImpl implements SheetTitleOptions {
174
_subtitle: ResourceStr | undefined
176
constructor(title: ResourceStr) {
180
get title(): ResourceStr {
183
set title(arg: ResourceStr) {
187
get subtitle(): ResourceStr | undefined {
188
return this._subtitle
190
set subtitle(arg: ResourceStr | undefined) {
195
class SheetOptionsImpl implements SheetOptions {
196
_title: SheetTitleOptions | CustomBuilder| undefined
197
_detents: [(SheetSize | Length | undefined), (SheetSize | Length | undefined), (SheetSize | Length | undefined)] | undefined
198
_height: SheetSize | Length| undefined
199
_dragBar: boolean| undefined
200
_maskColor: ResourceColor| undefined
201
_blurStyle: BlurStyle| undefined
202
_showClose: boolean | Resource| undefined
203
_preferType: SheetType| undefined
204
_shouldDismiss: ((sheetDismiss: SheetDismiss) => void) | undefined
205
_onWillDismiss: Callback<DismissSheetAction>| undefined
206
_onWillSpringBackWhenDismiss: Callback<SpringBackAction>| undefined
207
_enableOutsideInteractive: boolean| undefined
208
_width: Dimension| undefined
209
_borderWidth: Dimension | EdgeWidths | LocalizedEdgeWidths| undefined
210
_borderColor: ResourceColor | EdgeColors | LocalizedEdgeColors| undefined
211
_borderStyle: BorderStyle | EdgeStyles| undefined
212
_shadow: ShadowOptions | ShadowStyle| undefined
213
_onHeightDidChange: Callback<number>| undefined
214
_mode: SheetMode| undefined
215
_onDetentsDidChange: Callback<number>| undefined
216
_onWidthDidChange: Callback<number>| undefined
217
_onTypeDidChange: Callback<int>| undefined
218
_uiContext: UIContext| undefined
219
_backgroundColor: ResourceColor | undefined
220
_onAppear: (() => void) | undefined
221
_onDisappear: (() => void) | undefined
222
_onWillAppear: (() => void) | undefined
223
_onWillDisappear: (() => void) | undefined
225
constructor(title?: SheetTitleOptions) {
229
get title(): SheetTitleOptions | CustomBuilder| undefined {
232
set title(arg: SheetTitleOptions | CustomBuilder| undefined) {
236
get onWillDismiss(): Callback<DismissSheetAction>| undefined {
237
return this._onWillDismiss
239
set onWillDismiss(arg: Callback<DismissSheetAction>| undefined) {
240
this._onWillDismiss = arg
243
get detents(): [(SheetSize | Length | undefined), (SheetSize | Length | undefined), (SheetSize | Length | undefined)] | undefined {
246
set detents(arg: [(SheetSize | Length | undefined), (SheetSize | Length | undefined), (SheetSize | Length | undefined)] | undefined) {
250
get height(): SheetSize | Length| undefined {
253
set height(arg: SheetSize | Length| undefined) {
257
get dragBar(): boolean| undefined {
260
set dragBar(arg: boolean| undefined) {
264
get maskColor(): ResourceColor| undefined {
265
return this._maskColor
267
set maskColor(arg: ResourceColor| undefined) {
268
this._maskColor = arg
271
get blurStyle(): BlurStyle| undefined {
272
return this._blurStyle
274
set blurStyle(arg: BlurStyle| undefined) {
275
this._blurStyle = arg
278
get showClose(): boolean | Resource| undefined {
279
return this._showClose
281
set showClose(arg: boolean | Resource| undefined) {
282
this._showClose = arg
285
get preferType(): SheetType| undefined {
286
return this._preferType
288
set preferType(arg: SheetType| undefined) {
289
this._preferType = arg
292
get shouldDismiss(): ((sheetDismiss: SheetDismiss) => void) | undefined {
293
return this._shouldDismiss
295
set shouldDismiss(arg: ((sheetDismiss: SheetDismiss) => void) | undefined) {
296
this._shouldDismiss = arg
299
get onWillSpringBackWhenDismiss(): Callback<SpringBackAction>| undefined {
300
return this._onWillSpringBackWhenDismiss
302
set onWillSpringBackWhenDismiss(arg: Callback<SpringBackAction>| undefined) {
303
this._onWillSpringBackWhenDismiss = arg
306
get enableOutsideInteractive(): boolean| undefined {
307
return this._enableOutsideInteractive
309
set enableOutsideInteractive(arg: boolean| undefined) {
310
this._enableOutsideInteractive = arg
313
get width(): Dimension| undefined {
316
set width(arg: Dimension| undefined) {
320
get borderWidth(): Dimension | EdgeWidths | LocalizedEdgeWidths| undefined {
321
return this._borderWidth
323
set borderWidth(arg: Dimension | EdgeWidths | LocalizedEdgeWidths| undefined) {
324
this._borderWidth = arg
327
get borderColor(): ResourceColor | EdgeColors | LocalizedEdgeColors| undefined {
328
return this._borderColor
330
set borderColor(arg: ResourceColor | EdgeColors | LocalizedEdgeColors| undefined) {
331
this._borderColor = arg
334
get borderStyle(): BorderStyle | EdgeStyles| undefined {
335
return this._borderStyle
337
set borderStyle(arg: BorderStyle | EdgeStyles| undefined) {
338
this._borderStyle = arg
341
get shadow(): ShadowOptions | ShadowStyle| undefined {
344
set shadow(arg: ShadowOptions | ShadowStyle| undefined) {
348
get onHeightDidChange(): Callback<number>| undefined {
349
return this._onHeightDidChange
351
set onHeightDidChange(arg: Callback<number>| undefined) {
352
this._onHeightDidChange = arg
355
get mode(): SheetMode| undefined {
358
set mode(arg: SheetMode| undefined) {
362
get onDetentsDidChange(): Callback<number>| undefined {
363
return this._onDetentsDidChange
365
set onDetentsDidChange(arg: Callback<number>| undefined) {
366
this._onDetentsDidChange = arg
369
get onWidthDidChange(): Callback<number>| undefined {
370
return this._onWidthDidChange
372
set onWidthDidChange(arg: Callback<number>| undefined) {
373
this._onWidthDidChange = arg
376
get onTypeDidChange(): Callback<int>| undefined {
377
return this._onTypeDidChange
379
set onTypeDidChange(arg: Callback<int>| undefined) {
380
this._onTypeDidChange = arg
383
get uiContext(): UIContext| undefined {
384
return this._uiContext
386
set uiContext(arg: UIContext| undefined) {
387
this._uiContext = arg
390
get backgroundColor(): ResourceColor | undefined {
391
return this._backgroundColor
393
set backgroundColor(arg: ResourceColor | undefined) {
394
this._backgroundColor = arg
397
get onAppear(): (() => void) | undefined {
398
return this._onAppear
400
set onAppear(arg: (() => void) | undefined) {
404
get onDisappear(): (() => void) | undefined {
405
return this._onDisappear
407
set onDisappear(arg: (() => void) | undefined) {
408
this._onDisappear = arg
411
get onWillAppear(): (() => void) | undefined {
412
return this._onWillAppear
414
set onWillAppear(arg: (() => void) | undefined) {
415
this._onWillAppear = arg
418
get onWillDisappear(): (() => void) | undefined {
419
return this._onWillDisappear
421
set onWillDisappear(arg: (() => void) | undefined) {
422
this._onWillDisappear = arg
426
class BlurOptionsImpl implements BlurOptions {
427
_grayscale: [number, number]
429
constructor(grayscale: [number, number]) {
430
this._grayscale = grayscale
433
get grayscale(): [number, number] {
434
return this._grayscale
436
set grayscale(arg: [number, number]) {
437
this._grayscale = arg
441
function checkPerf2(count: number) {
442
let peer = new ArkButtonPeer(ArkUINodeType.Button)
443
let start = Date.now()
444
for (let i = 0; i < count; i++) {
453
peer.backdropBlurAttribute(i, i % 2 == 0 ? undefined : new BlurOptionsImpl([1, 2] as [number, number]))
455
let passed = Date.now() - start
456
console.log(`backdropBlur: ${Math.round(passed)}ms for ${count} iteration, ${Math.round(passed / count * 1_000_000)}ms per 1M iterations`)
459
function checkPerf3(count: number) {
460
let peer = new ArkButtonPeer(ArkUINodeType.Button)
461
let start = Date.now()
462
for (let i = 0; i < count; i++) {
463
peer.widthAttribute(testString1000)
465
let passed = Date.now() - start
466
console.log(`widthAttributeString: ${Math.round(passed)}ms for ${count} iteration, ${Math.round(passed / count * 1_000_000)}ms per 1M iterations`)
469
function checkButton() {
470
let data = new byte[5]
472
checkResult("TestPerfNumberWithArray",
473
() => NativeModule._TestPerfNumberWithArray(data, data.length),
474
"TestPerfNumberWithArray(42, 5)")
475
let peer = new ArkButtonPeer(ArkUINodeType.Button)
476
checkResult("width", () => peer.widthAttribute("42%"),
477
"width({1, 42.000000, 3, 0})")
478
checkResult("type", () => peer.typeAttribute(1 as ButtonType), "type(1)")
479
checkResult("labelStyle", () => peer.labelStyleAttribute(new LabelStyleImpl(3)),
480
"labelStyle({{ARK_TAG_UNDEFINED, {}}, {ARK_TAG_OBJECT, {102, .i32=3}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}})")
481
checkResult("labelStyle2", () => peer.labelStyleAttribute(new LabelStyleImpl()),
482
"labelStyle({{ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}})")
484
checkResult("height", () => peer.heightAttribute({ id: 43, bundleName: "MyApp", moduleName: "MyApp" }),
485
"height(Length {value=0.000000, unit=vp, resource=43})")
487
checkResult("bindSheet", () =>
488
peer.bindSheetAttribute(false, () => {}, new SheetOptionsImpl(new SheetTitleOptionsImpl("My App"))),
489
"bindSheet(false, {0, .value0={42}}, {ARK_TAG_OBJECT, {{ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_OBJECT, {0, .value0={{0, .value0={\"My App\", 6}}, {ARK_TAG_UNDEFINED, {}}}}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}}})"
493
export function main(): void {
494
checkPerf(5 * 1000 * 1000)
495
checkPerf2(5 * 1000 * 1000)
496
checkPerf3(5 * 1000 * 1000)