1/* 2 * Copyright (c) 2025 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 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 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. 14 */ 15 16import * as path from 'path'; 17import { PluginTestContext, PluginTester } from '../../../utils/plugin-tester'; 18import { BuildConfig, mockBuildConfig } from '../../../utils/artkts-config'; 19import { getRootPath, MOCK_ENTRY_DIR_PATH } from '../../../utils/path-config'; 20import { parseDumpSrc } from '../../../utils/parse-string'; 21import { uiNoRecheck } from '../../../utils/plugins'; 22import { uiTransform } from '../../../../ui-plugins'; 23import { Plugins } from '../../../../common/plugin-context'; 24 25const XCOMPONENT_DIR_PATH: string = 'xcomponent'; 26 27const buildConfig: BuildConfig = mockBuildConfig(); 28buildConfig.compileFiles = [ 29 path.resolve(getRootPath(), MOCK_ENTRY_DIR_PATH, XCOMPONENT_DIR_PATH, 'xcomponent-basic.ets'), 30]; 31 32const xcomponentTransform: Plugins = { 33 name: 'xcomponent', 34 parsed: uiTransform().parsed, 35} 36 37const pluginTester = new PluginTester('test basic XComponent transform', buildConfig); 38 39const expectedScript: string = ` 40import { __memo_id_type as __memo_id_type } from "arkui.stateManagement.runtime"; 41 42import { __memo_context_type as __memo_context_type } from "arkui.stateManagement.runtime"; 43 44import { memo as memo } from "arkui.stateManagement.runtime"; 45 46import { UIXComponentAttribute as UIXComponentAttribute } from "@ohos.arkui.component"; 47 48import { UIFlexAttribute as UIFlexAttribute } from "@ohos.arkui.component"; 49 50import { EntryPoint as EntryPoint } from "arkui.UserView"; 51 52import { CustomComponent as CustomComponent } from "arkui.component.customComponent"; 53 54import { Component as Component, Flex as Flex, XComponent as XComponent, FlexDirection as FlexDirection, XComponentType as XComponentType, Entry as Entry, XComponentController as XComponentController, ItemAlign as ItemAlign, FlexAlign as FlexAlign, XComponentParameter as XComponentParameter } from "@ohos.arkui.component"; 55 56function main() {} 57 58 59 60@Entry({useSharedStorage:false,storage:"",routeName:""}) @Component({freezeWhenInactive:false}) final class Index extends CustomComponent<Index, __Options_Index> { 61 public __initializeStruct(initializers: __Options_Index | undefined, @memo() content: (()=> void) | undefined): void { 62 this.__backing_myXComponentController = ((({let gensym___221905990 = initializers; 63 (((gensym___221905990) == (null)) ? undefined : gensym___221905990.myXComponentController)})) ?? (new XComponentController())); 64 } 65 66 public __updateStruct(initializers: __Options_Index | undefined): void {} 67 68 private __backing_myXComponentController?: XComponentController; 69 70 public get myXComponentController(): XComponentController { 71 return (this.__backing_myXComponentController as XComponentController); 72 } 73 74 public set myXComponentController(value: XComponentController) { 75 this.__backing_myXComponentController = value; 76 } 77 78 @memo() public _build(@memo() style: ((instance: Index)=> Index) | undefined, @memo() content: (()=> void) | undefined, initializers: __Options_Index | undefined): void { 79 Flex(@memo() ((instance: UIFlexAttribute): void => { 80 instance.width("100%").height("100%"); 81 return; 82 }), { 83 direction: FlexDirection.Column, 84 alignItems: ItemAlign.Center, 85 justifyContent: FlexAlign.Start, 86 }, (() => { 87 XComponent(undefined, ({ 88 id: "xComponentId", 89 type: XComponentType.TEXTURE, 90 libraryname: "nativerender", 91 controller: this.myXComponentController, 92 } as XComponentParameter), "", undefined); 93 })); 94 } 95 96 public constructor() {} 97 98} 99 100interface __Options_Index { 101 set myXComponentController(myXComponentController: XComponentController | undefined) 102 103 get myXComponentController(): XComponentController | undefined 104 105} 106 107class __EntryWrapper extends EntryPoint { 108 @memo() public entry(): void { 109 Index._instantiateImpl(undefined, (() => { 110 return new Index(); 111 }), undefined, undefined, undefined); 112 } 113 114 public constructor() {} 115 116} 117`; 118 119function testXComponentTransformer(this: PluginTestContext): void { 120 expect(parseDumpSrc(this.scriptSnapshot ?? '')).toBe(parseDumpSrc(expectedScript)); 121} 122 123pluginTester.run( 124 'test basic XComponent transform', 125 [xcomponentTransform, uiNoRecheck], 126 { 127 checked: [testXComponentTransformer], 128 }, 129 { 130 stopAfter: 'checked', 131 } 132); 133