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 16 #include "gtest/gtest.h" 17 #include "native_interface.h" 18 #include "native_node.h" 19 #include "native_type.h" 20 #include "node_component_snapshot.h" 21 #include "test/mock/core/common/mock_container.h" 22 #include "test/mock/core/pipeline/mock_pipeline_context.h" 23 24 #include "core/interfaces/arkoala/arkoala_api.h" 25 26 using namespace testing; 27 using namespace testing::ext; 28 29 namespace OHOS::Ace { 30 class SnapshotTest : public testing::Test { 31 public: SetUpTestSuite()32 static void SetUpTestSuite() 33 { 34 NG::MockPipelineContext::SetUp(); 35 MockContainer::SetUp(); 36 } TearDownTestSuite()37 static void TearDownTestSuite() 38 { 39 NG::MockPipelineContext::TearDown(); 40 MockContainer::TearDown(); 41 } 42 }; 43 44 /** 45 * @tc.name: SnapshotTest001 46 * @tc.desc: Test OH_ArkUI_CreateSnapshotOptions function. 47 * @tc.type: FUNC 48 */ 49 HWTEST_F(SnapshotTest, SnapshotTest001, TestSize.Level1) 50 { 51 auto nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1*>( 52 OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1")); 53 auto node = nodeAPI->createNode(ARKUI_NODE_COLUMN); 54 auto button = nodeAPI->createNode(ARKUI_NODE_BUTTON); 55 ArkUI_NumberValue value[] = { { .f32 = 150 } }; 56 ArkUI_AttributeItem item = { value, 1 }; 57 nodeAPI->setAttribute(button, NODE_WIDTH, &item); 58 nodeAPI->setAttribute(button, NODE_HEIGHT, &item); 59 nodeAPI->addChild(node, button); 60 auto options = OH_ArkUI_CreateSnapshotOptions(); 61 EXPECT_NE(options, nullptr); 62 } 63 64 /** 65 * @tc.name: SnapshotTest002 66 * @tc.desc: Test OH_ArkUI_SnapshotOptions_SetScale function. 67 * @tc.type: FUNC 68 */ 69 HWTEST_F(SnapshotTest, SnapshotTest002, TestSize.Level1) 70 { 71 auto nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1*>( 72 OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1")); 73 auto node = nodeAPI->createNode(ARKUI_NODE_COLUMN); 74 auto button = nodeAPI->createNode(ARKUI_NODE_BUTTON); 75 ArkUI_NumberValue value[] = { { .f32 = 150 } }; 76 ArkUI_AttributeItem item = { value, 1 }; 77 nodeAPI->setAttribute(button, NODE_WIDTH, &item); 78 nodeAPI->setAttribute(button, NODE_HEIGHT, &item); 79 nodeAPI->addChild(node, button); 80 auto options = OH_ArkUI_CreateSnapshotOptions(); 81 ASSERT_NE(options, nullptr); 82 EXPECT_EQ(OH_ArkUI_SnapshotOptions_SetScale(options, 0.0f), ArkUI_ErrorCode::ARKUI_ERROR_CODE_PARAM_INVALID); 83 } 84 85 /** 86 * @tc.name: SnapshotTest003 87 * @tc.desc: Test OH_ArkUI_SnapshotOptions_SetScale function. 88 * @tc.type: FUNC 89 */ 90 HWTEST_F(SnapshotTest, SnapshotTest003, TestSize.Level1) 91 { 92 auto nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1*>( 93 OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1")); 94 auto node = nodeAPI->createNode(ARKUI_NODE_COLUMN); 95 auto button = nodeAPI->createNode(ARKUI_NODE_BUTTON); 96 ArkUI_NumberValue value[] = { { .f32 = 150 } }; 97 ArkUI_AttributeItem item = { value, 1 }; 98 nodeAPI->setAttribute(button, NODE_WIDTH, &item); 99 nodeAPI->setAttribute(button, NODE_HEIGHT, &item); 100 nodeAPI->addChild(node, button); 101 auto options = OH_ArkUI_CreateSnapshotOptions(); 102 ASSERT_NE(options, nullptr); 103 auto opt = reinterpret_cast<ArkUISnapshotOptions*>(options); 104 OH_ArkUI_SnapshotOptions_SetScale(options, 0.5f); 105 EXPECT_EQ(opt->scale, 0.5f); 106 } 107 108 /** 109 * @tc.name: SnapshotTest004 110 * @tc.desc: Test OH_ArkUI_SnapshotOptions_SetScale function. 111 * @tc.type: FUNC 112 */ 113 HWTEST_F(SnapshotTest, SnapshotTest004, TestSize.Level1) 114 { 115 auto nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1*>( 116 OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1")); 117 auto node = nodeAPI->createNode(ARKUI_NODE_COLUMN); 118 auto button = nodeAPI->createNode(ARKUI_NODE_BUTTON); 119 ArkUI_NumberValue value[] = { { .f32 = 150 } }; 120 ArkUI_AttributeItem item = { value, 1 }; 121 nodeAPI->setAttribute(button, NODE_WIDTH, &item); 122 nodeAPI->setAttribute(button, NODE_HEIGHT, &item); 123 nodeAPI->addChild(node, button); 124 auto options = OH_ArkUI_CreateSnapshotOptions(); 125 ASSERT_NE(options, nullptr); 126 auto opt = reinterpret_cast<ArkUISnapshotOptions*>(options); 127 OH_ArkUI_SnapshotOptions_SetScale(options, 1.5f); 128 EXPECT_EQ(opt->scale, 1.5f); 129 } 130 131 /** 132 * @tc.name: SnapshotTest005 133 * @tc.desc: Test OH_ArkUI_GetNodeSnapshot function. 134 * @tc.type: FUNC 135 */ 136 HWTEST_F(SnapshotTest, SnapshotTest005, TestSize.Level1) 137 { 138 OH_PixelmapNative* pixelmap = nullptr; 139 auto result = OH_ArkUI_GetNodeSnapshot(nullptr, nullptr, &pixelmap); 140 EXPECT_EQ(result, ArkUI_ErrorCode::ARKUI_ERROR_CODE_PARAM_INVALID); 141 } 142 143 } // namespace OHOS::Ace