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 "test/unittest/interfaces/ace_kit/mock/mock_ace_kit_pattern.h" 18 #include "ui/view/view.h" 19 #include "ui/view_factory/abstract_view_factory.h" 20 #include "test/unittest/core/base/view_abstract_test_ng.h" 21 #include "interfaces/inner_api/ace_kit/src/view/frame_node_impl.h" 22 23 using namespace testing; 24 using namespace testing::ext; 25 using namespace OHOS::Ace::Kit; 26 27 namespace OHOS::Ace::Kit { 28 29 class ViewTest : public testing::Test {}; 30 31 /** 32 * @tc.name: ViewTest001 33 * @tc.desc: Test SetWidth of view 34 * @tc.type: FUNC 35 */ 36 HWTEST_F(ViewTest, ViewTest001, TestSize.Level1) 37 { 38 /** 39 * @tc.steps1: initialize View. 40 * @tc.expected: All pointer is non-null. 41 */ 42 View view; 43 auto node = 44 AbstractViewFactory::CreateFrameNode("test", 0, AceType::MakeRefPtr<MockAceKitPattern>()); 45 EXPECT_NE(node, nullptr); 46 view.node_ = node; 47 48 /** 49 * @tc.steps: make width.Unit() is DimensionUnit::CALC. 50 */ 51 const CalcDimension width = CalcDimension("20", DimensionUnit::CALC); 52 view.SetWidth(width); 53 54 /** 55 * @tc.steps: getwidth to verify width is setted. 56 */ 57 auto frameNode = reinterpret_cast<AceNode*>(node->GetHandle()); 58 auto res = NG::ViewAbstract::GetWidth(frameNode); 59 60 auto unitNum = static_cast<int>(res.Unit()); 61 EXPECT_EQ(unitNum, 6); 62 } 63 64 /** 65 * @tc.name: ViewTest002 66 * @tc.desc: Test SetWidth of view 67 * @tc.type: FUNC 68 */ 69 HWTEST_F(ViewTest, ViewTest002, TestSize.Level1) 70 { 71 /** 72 * @tc.steps1: initialize View. 73 * @tc.expected: All pointer is non-null. 74 */ 75 View view; 76 auto node = 77 AbstractViewFactory::CreateFrameNode("test", 0, AceType::MakeRefPtr<MockAceKitPattern>()); 78 EXPECT_NE(node, nullptr); 79 view.node_ = node; 80 81 /** 82 * @tc.steps: make width.Unit() is not DimensionUnit::CALC. 83 */ 84 const CalcDimension width = CalcDimension(30.0f, DimensionUnit::PX); 85 view.SetWidth(width); 86 87 /** 88 * @tc.steps: getwidth to verify width is setted. 89 */ 90 auto frameNode = reinterpret_cast<AceNode*>(node->GetHandle()); 91 auto res = NG::ViewAbstract::GetWidth(frameNode); 92 93 auto resWidth = res.Value(); 94 EXPECT_EQ(resWidth, 30); 95 } 96 97 /** 98 * @tc.name: ViewTest003 99 * @tc.desc: Test SetHeight of view 100 * @tc.type: FUNC 101 */ 102 HWTEST_F(ViewTest, ViewTest003, TestSize.Level1) 103 { 104 /** 105 * @tc.steps1: initialize View. 106 * @tc.expected: All pointer is non-null. 107 */ 108 View view; 109 auto node = 110 AbstractViewFactory::CreateFrameNode("test", 0, AceType::MakeRefPtr<MockAceKitPattern>()); 111 EXPECT_NE(node, nullptr); 112 view.node_ = node; 113 114 /** 115 * @tc.steps: make width.Unit() is DimensionUnit::CALC. 116 */ 117 const CalcDimension height = CalcDimension("20", DimensionUnit::CALC); 118 view.SetHeight(height); 119 120 /** 121 * @tc.steps: getwidth to verify width is setted. 122 */ 123 auto frameNode = reinterpret_cast<AceNode*>(node->GetHandle()); 124 auto res = NG::ViewAbstract::GetHeight(frameNode); 125 126 auto unitNum = static_cast<int>(res.Unit()); 127 EXPECT_EQ(unitNum, 6); 128 } 129 130 /** 131 * @tc.name: ViewTest004 132 * @tc.desc: Test SetWidth of view 133 * @tc.type: FUNC 134 */ 135 HWTEST_F(ViewTest, ViewTest004, TestSize.Level1) 136 { 137 /** 138 * @tc.steps1: initialize View. 139 * @tc.expected: All pointer is non-null. 140 */ 141 View view; 142 auto node = 143 AbstractViewFactory::CreateFrameNode("test", 0, AceType::MakeRefPtr<MockAceKitPattern>()); 144 EXPECT_NE(node, nullptr); 145 view.node_ = node; 146 147 /** 148 * @tc.steps: make width.Unit() is not DimensionUnit::CALC. 149 */ 150 const CalcDimension height = CalcDimension(30.0f, DimensionUnit::PX); 151 view.SetHeight(height); 152 153 /** 154 * @tc.steps: getwidth to verify width is setted. 155 */ 156 auto frameNode = reinterpret_cast<AceNode*>(node->GetHandle()); 157 auto res = NG::ViewAbstract::GetHeight(frameNode); 158 159 auto resHeight = res.Value(); 160 EXPECT_EQ(resHeight, 30); 161 } 162 163 /** 164 * @tc.name: LinearGradientBlurViewTest001 165 * @tc.desc: Test SetWidth of view 166 * @tc.type: FUNC 167 */ 168 HWTEST_F(ViewTest, LinearGradientBlurViewTest001, TestSize.Level1) 169 { 170 /** 171 * @tc.steps: initialize View. 172 * @tc.expected: All pointer is non-null. 173 */ 174 View view; 175 auto node = 176 AbstractViewFactory::CreateFrameNode("testNode", 0, AceType::MakeRefPtr<MockAceKitPattern>()); 177 EXPECT_NE(node, nullptr); 178 view.node_ = node; 179 180 /** 181 * @tc.steps: Initialize linearGradientBlurPara parameters. 182 */ 183 std::vector<std::pair<float, float>> fractionStops; 184 fractionStops.push_back(std::pair<float, float>(0.0f, 1.0f)); 185 CalcDimension dimensionRadius(0.0); 186 NG::LinearGradientBlurPara blurPara(dimensionRadius, fractionStops, NG::GradientDirection::LEFT); 187 188 /** 189 * @tc.steps: Test SetLinearGradientBlur. 190 */ 191 view.SetLinearGradientBlur(blurPara); 192 auto frameNode = reinterpret_cast<AceNode*>(node->GetHandle()); 193 ASSERT_NE(frameNode, nullptr); 194 const auto& target = frameNode->GetRenderContext(); 195 ASSERT_NE(target, nullptr); 196 auto graphicsProperty = target->CloneGraphics(); 197 ASSERT_NE(graphicsProperty, nullptr); 198 EXPECT_EQ(graphicsProperty->CheckLinearGradientBlur(blurPara), true); 199 EXPECT_EQ(graphicsProperty->GetLinearGradientBlurValue(), blurPara); 200 } 201 202 /** 203 * @tc.name: LinearGradientBlurViewTest002 204 * @tc.desc: Test SetWidth of view 205 * @tc.type: FUNC 206 */ 207 HWTEST_F(ViewTest, LinearGradientBlurViewTest002, TestSize.Level1) 208 { 209 /** 210 * @tc.steps: Build a object viewAbstract and set visual state. 211 */ 212 NG::ViewStackProcessor viewStackProcessor; 213 int32_t index = 1; 214 auto state = static_cast<VisualState>(index); 215 viewStackProcessor.GetInstance()->SetVisualState(state); 216 bool result = NG::ViewStackProcessor::GetInstance()->IsCurrentVisualStateProcess(); 217 EXPECT_FALSE(result); 218 219 /** 220 * @tc.steps: initialize View. 221 */ 222 View view; 223 auto node = 224 AbstractViewFactory::CreateFrameNode("testNode", 0, AceType::MakeRefPtr<MockAceKitPattern>()); 225 EXPECT_NE(node, nullptr); 226 view.node_ = node; 227 228 /** 229 * @tc.steps: Initialize linearGradientBlurPara parameters. 230 */ 231 std::vector<std::pair<float, float>> fractionStops; 232 fractionStops.push_back(std::pair<float, float>(0.0f, 1.0f)); 233 CalcDimension dimensionRadius(60.0); 234 NG::LinearGradientBlurPara blurPara(dimensionRadius, fractionStops, NG::GradientDirection::BOTTOM); 235 236 /** 237 * @tc.steps: Test "SetLinearGradientBlur", when "IsCurrentVisualStateProcess" return false. 238 */ 239 view.SetLinearGradientBlur(blurPara); 240 auto frameNode = reinterpret_cast<AceNode*>(node->GetHandle()); 241 ASSERT_NE(frameNode, nullptr); 242 const auto& renderContext = frameNode->GetRenderContext(); 243 ASSERT_NE(renderContext, nullptr); 244 auto graphicsProperty = renderContext->CloneGraphics(); 245 ASSERT_NE(graphicsProperty, nullptr); 246 EXPECT_EQ(graphicsProperty->CheckLinearGradientBlur(blurPara), true); 247 EXPECT_EQ(graphicsProperty->GetLinearGradientBlurValue(), blurPara); 248 } 249 } 250 251