1 /*
2 * Copyright (c) 2022-2023 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
18 #define private public
19 #define protected public
20
21 #include "core/components/common/layout/constants.h"
22 #include "core/components/common/properties/shadow_config.h"
23 #include "core/components/container_modal/container_modal_constants.h"
24 #include "core/components/select/select_theme.h"
25 #include "core/components_ng/base/view_stack_processor.h"
26 #include "core/components_ng/pattern/image/image_layout_property.h"
27 #include "core/components_ng/pattern/image/image_pattern.h"
28 #include "core/components_ng/pattern/menu/menu_item/menu_item_model_ng.h"
29 #include "core/components_ng/pattern/menu/menu_item/menu_item_pattern.h"
30 #include "core/components_ng/pattern/menu/menu_item_group/menu_item_group_pattern.h"
31 #include "core/components_ng/pattern/menu/menu_item_group/menu_item_group_view.h"
32 #include "core/components_ng/pattern/menu/menu_model_ng.h"
33 #include "core/components_ng/pattern/menu/menu_pattern.h"
34 #include "core/components_ng/pattern/menu/menu_view.h"
35 #include "core/components_ng/pattern/menu/multi_menu_layout_algorithm.h"
36 #include "core/components_ng/pattern/menu/sub_menu_layout_algorithm.h"
37 #include "core/components_ng/pattern/menu/wrapper/menu_wrapper_pattern.h"
38 #include "core/components_ng/pattern/pattern.h"
39 #include "core/components_ng/pattern/scroll/scroll_pattern.h"
40 #include "core/components_ng/pattern/text/text_layout_property.h"
41 #include "core/components_ng/pattern/text/text_pattern.h"
42 #include "core/components_ng/property/measure_property.h"
43 #include "core/components_ng/syntax/lazy_for_each_model.h"
44 #include "core/components_ng/syntax/lazy_layout_wrapper_builder.h"
45 #include "core/components_ng/test/mock/render/mock_render_context.h"
46 #include "core/components_ng/test/mock/rosen/mock_canvas.h"
47 #include "core/components_ng/test/mock/rosen/testing_canvas.h"
48 #include "core/components_ng/test/mock/theme/mock_theme_manager.h"
49 #include "core/event/touch_event.h"
50 #include "core/pipeline_ng/test/mock/mock_pipeline_base.h"
51
52 using namespace testing;
53 using namespace testing::ext;
54 using namespace OHOS::Ace::Framework;
55
56 namespace OHOS::Ace::NG {
57 namespace {
58 constexpr int32_t TARGET_ID = 3;
59 constexpr MenuType TYPE = MenuType::MENU;
60 constexpr int32_t SELECTED_INDEX = 10;
61 constexpr float CURRENT_OFFSET = -0.5f;
62 const std::string EMPTY_TEXT = "";
63 const std::string TEXT_TAG = "text";
64 const std::string MENU_TAG = "menu";
65 const std::string MENU_ITEM_TEXT = "menuItem";
66 const std::string MENU_ITEM_GROUP_TEXT = "menuItemGroup";
67 const std::string MENU_TOUCH_EVENT_TYPE = "1";
68 const DirtySwapConfig configDirtySwap = {false, false, false, false, true, false};
69 constexpr Color ITEM_DISABLED_COLOR = Color(0x0c182431);
70
71 constexpr float FULL_SCREEN_WIDTH = 720.0f;
72 constexpr float FULL_SCREEN_HEIGHT = 1136.0f;
73 constexpr float POSITION_OFFSET = 100.0f;
74 constexpr float TARGET_SIZE_WIDTH = 100.0f;
75 constexpr float TARGET_SIZE_HEIGHT = 100.0f;
76 constexpr float MENU_SIZE_WIDTH = 150.0f;
77 constexpr float MENU_SIZE_HEIGHT = 150.0f;
78 constexpr double MENU_OFFSET_X = 10.0;
79 constexpr double MENU_OFFSET_Y = 10.0;
80 constexpr float MENU_ITEM_SIZE_WIDTH = 100.0f;
81 constexpr float MENU_ITEM_SIZE_HEIGHT = 50.0f;
82
83 constexpr int CHILD_SIZE_X = 1;
84 constexpr int CHILD_SIZE_Y = 2;
85 constexpr int TOP_POSITION_X = 10;
86 constexpr int TOP_POSITION_Y = 20;
87 constexpr int BOTTOM_POSITION_X = 30;
88 constexpr int BOTTOM_POSITION_Y = 40;
89 constexpr float OFFSET_FIRST = 20.0f;
90 constexpr float OFFSET_SECOND = 5.0f;
91 constexpr float OFFSET_THIRD = 200.0f;
92 constexpr float OFFSET_FORTH = 300.0f;
93 constexpr float OFFSET_FIFTH = 50.0f;
94 constexpr float OFFSET_SIXTH = 60.0f;
95 constexpr int SIZE_X_SECOND = 20;
96 constexpr int SIZE_Y_SECOND = 20;
97 constexpr int OFFSET_X_THIRD = 100;
98 constexpr int OFFSET_Y_THIRD = 20;
99 constexpr int NODEID = 1;
100 constexpr int TOP_LEFT_X = 100;
101 constexpr int TOP_LEFT_Y = 18;
102 constexpr int TOP_RIGHT_X = 119;
103 constexpr int TOP_RIGHT_Y = 18;
104 constexpr int BOTTOM_LEFT_X = 100;
105 constexpr int BOTTOM_LEFT_Y = 40;
106 constexpr int BOTTOM_RIGHT_X = 119;
107 constexpr int BOTTOM_RIGHT_Y = 40;
108 constexpr int PLACEMENT_LEFT_X = 99;
109 constexpr int PLACEMENT_LEFT_Y = 29;
110 constexpr int PLACEMENT_LEFT_TOP_X = 99;
111 constexpr int PLACEMENT_LEFT_TOP_Y = 20;
112 constexpr int PLACEMENT_LEFT_BOTTOM_X = 99;
113 constexpr int PLACEMENT_LEFT_BOTTOM_Y = 38;
114 constexpr int PLACEMENT_RIGHT_X = 120;
115 constexpr int PLACEMENT_RIGHT_Y = 29;
116 constexpr int PLACEMENT_RIGHT_TOP_X = 120;
117 constexpr int PLACEMENT_RIGHT_TOP_Y = 20;
118 constexpr int PLACEMENT_RIGHT_BOTTOM_X = 120;
119 constexpr int PLACEMENT_RIGHT_BOTTOM_Y = 38;
120 const SizeF FULL_SCREEN_SIZE(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT);
121 } // namespace
122 class MenuTestNg : public testing::Test {
123 public:
124 static void SetUpTestCase();
125 static void TearDownTestCase();
126 void SetUp() override;
127 void TearDown() override;
128 void InitMenuTestNg();
129 void InitMenuItemTestNg();
130 PaintWrapper* GetPaintWrapper(RefPtr<MenuPaintProperty> paintProperty);
131 RefPtr<FrameNode> menuFrameNode_;
132 RefPtr<MenuAccessibilityProperty> menuAccessibilityProperty_;
133 RefPtr<FrameNode> menuItemFrameNode_;
134 RefPtr<MenuItemPattern> menuItemPattern_;
135 RefPtr<MenuItemAccessibilityProperty> menuItemAccessibilityProperty_;
136 };
137
SetUpTestCase()138 void MenuTestNg::SetUpTestCase() {}
139
TearDownTestCase()140 void MenuTestNg::TearDownTestCase() {}
141
SetUp()142 void MenuTestNg::SetUp()
143 {
144 MockPipelineBase::SetUp();
145 auto themeManager = AceType::MakeRefPtr<MockThemeManager>();
146 MockPipelineBase::GetCurrent()->SetThemeManager(themeManager);
147 EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<SelectTheme>()));
148 }
149
TearDown()150 void MenuTestNg::TearDown()
151 {
152 MockPipelineBase::TearDown();
153 menuFrameNode_ = nullptr;
154 menuAccessibilityProperty_ = nullptr;
155 menuItemFrameNode_ = nullptr;
156 menuItemPattern_ = nullptr;
157 menuItemAccessibilityProperty_ = nullptr;
158 }
159
InitMenuTestNg()160 void MenuTestNg::InitMenuTestNg()
161 {
162 menuFrameNode_ = FrameNode::GetOrCreateFrameNode(V2::MENU_TAG, ViewStackProcessor::GetInstance()->ClaimNodeId(),
163 []() { return AceType::MakeRefPtr<MenuPattern>(TARGET_ID, "", TYPE); });
164 ASSERT_NE(menuFrameNode_, nullptr);
165
166 menuAccessibilityProperty_ = menuFrameNode_->GetAccessibilityProperty<MenuAccessibilityProperty>();
167 ASSERT_NE(menuAccessibilityProperty_, nullptr);
168 }
169
InitMenuItemTestNg()170 void MenuTestNg::InitMenuItemTestNg()
171 {
172 menuItemFrameNode_ = FrameNode::GetOrCreateFrameNode(V2::MENU_ITEM_ETS_TAG,
173 ViewStackProcessor::GetInstance()->ClaimNodeId(), []() { return AceType::MakeRefPtr<MenuItemPattern>(); });
174 ASSERT_NE(menuItemFrameNode_, nullptr);
175
176 menuItemPattern_ = menuItemFrameNode_->GetPattern<MenuItemPattern>();
177 ASSERT_NE(menuItemPattern_, nullptr);
178
179 menuItemAccessibilityProperty_ = menuItemFrameNode_->GetAccessibilityProperty<MenuItemAccessibilityProperty>();
180 ASSERT_NE(menuItemAccessibilityProperty_, nullptr);
181 }
182
GetPaintWrapper(RefPtr<MenuPaintProperty> paintProperty)183 PaintWrapper* MenuTestNg::GetPaintWrapper(RefPtr<MenuPaintProperty> paintProperty)
184 {
185 WeakPtr<RenderContext> renderContext;
186 RefPtr<GeometryNode> geometryNode = AceType::MakeRefPtr<GeometryNode>();
187 PaintWrapper* paintWrapper = new PaintWrapper(renderContext, geometryNode, paintProperty);
188 return paintWrapper;
189 }
190
191 /**
192 * @tc.name: MenuWrapperPatternTestNg001
193 * @tc.desc: Verify HideMenu(Menu).
194 * @tc.type: FUNC
195 */
196 HWTEST_F(MenuTestNg, MenuWrapperPatternTestNg001, TestSize.Level1)
197 {
198 /**
199 * @tc.steps: step1. Create wrapper and child menu
200 * @tc.expected: wrapper pattern not null
201 */
202 auto wrapperNode =
203 FrameNode::CreateFrameNode(V2::MENU_WRAPPER_ETS_TAG, 1, AceType::MakeRefPtr<MenuWrapperPattern>(1));
204 auto mainMenu =
205 FrameNode::CreateFrameNode(V2::MENU_ETS_TAG, 2, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::MENU));
206 auto subMenuFirst = FrameNode::CreateFrameNode(
207 V2::MENU_ETS_TAG, 3, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::SUB_MENU));
208 auto subMenuSecond = FrameNode::CreateFrameNode(
209 V2::MENU_ETS_TAG, 4, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::SUB_MENU));
210 mainMenu->MountToParent(wrapperNode);
211 subMenuFirst->MountToParent(wrapperNode);
212 subMenuSecond->MountToParent(wrapperNode);
213 auto wrapperPattern = wrapperNode->GetPattern<MenuWrapperPattern>();
214 ASSERT_NE(wrapperPattern, nullptr);
215 /**
216 * @tc.steps: step2. excute HideMenu
217 * @tc.expected: wrapper child size is 3
218 */
219 wrapperPattern->HideMenu(mainMenu);
220 wrapperPattern->OnModifyDone();
221 EXPECT_EQ(wrapperNode->GetChildren().size(), 3);
222 }
223
224 /**
225 * @tc.name: MenuWrapperPatternTestNg002
226 * @tc.desc: Verify HideMenu().
227 * @tc.type: FUNC
228 */
229 HWTEST_F(MenuTestNg, MenuWrapperPatternTestNg002, TestSize.Level1)
230 {
231 auto wrapperNode =
232 FrameNode::CreateFrameNode(V2::MENU_WRAPPER_ETS_TAG, 1, AceType::MakeRefPtr<MenuWrapperPattern>(1));
233 auto mainMenu =
234 FrameNode::CreateFrameNode(V2::MENU_ETS_TAG, 2, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::MENU));
235 mainMenu->MountToParent(wrapperNode);
236 auto wrapperPattern = wrapperNode->GetPattern<MenuWrapperPattern>();
237 ASSERT_NE(wrapperPattern, nullptr);
238 wrapperPattern->HideMenu();
239 EXPECT_EQ(wrapperNode->GetChildren().size(), 1);
240 }
241
242 /**
243 * @tc.name: MenuWrapperPatternTestNg003
244 * @tc.desc: Verify HideSubMenu.
245 * @tc.type: FUNC
246 */
247 HWTEST_F(MenuTestNg, MenuWrapperPatternTestNg003, TestSize.Level1)
248 {
249 /**
250 * @tc.steps: step1. Create wrapper
251 * @tc.expected: wrapper pattern not null
252 */
253 auto wrapperNode =
254 FrameNode::CreateFrameNode(V2::MENU_WRAPPER_ETS_TAG, 1, AceType::MakeRefPtr<MenuWrapperPattern>(1));
255 auto wrapperPattern = wrapperNode->GetPattern<MenuWrapperPattern>();
256 wrapperPattern->HideSubMenu();
257 ASSERT_NE(wrapperPattern, nullptr);
258 /**
259 * @tc.steps: step2. add submenu to wrapper
260 * @tc.expected: wrapper child size is 1
261 */
262 auto mainMenu =
263 FrameNode::CreateFrameNode(V2::MENU_ETS_TAG, 2, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::MENU));
264 auto subMenu = FrameNode::CreateFrameNode(
265 V2::MENU_ETS_TAG, 2, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::SUB_MENU));
266 mainMenu->MountToParent(wrapperNode);
267 subMenu->MountToParent(wrapperNode);
268 wrapperPattern->HideSubMenu();
269 EXPECT_EQ(wrapperNode->GetChildren().size(), 1);
270 }
271
272 /**
273 * @tc.name: MenuWrapperPatternTestNg004
274 * @tc.desc: Verify HandleMouseEvent.
275 * @tc.type: FUNC
276 */
277 HWTEST_F(MenuTestNg, MenuWrapperPatternTestNg004, TestSize.Level1)
278 {
279 /**
280 * @tc.steps: step1. Create menuItem and mouseInfo
281 * @tc.expected: menuItem and mouseInfo function result as expected
282 */
283 MouseInfo mouseInfo;
284 mouseInfo.SetAction(MouseAction::PRESS);
285 mouseInfo.SetGlobalLocation(Offset(200, 200));
286 auto menuItemNode = FrameNode::CreateFrameNode(V2::MENU_ITEM_ETS_TAG, 100, AceType::MakeRefPtr<MenuItemPattern>());
287 auto menuItemPattern = menuItemNode->GetPattern<MenuItemPattern>();
288 ASSERT_NE(menuItemPattern, nullptr);
289 menuItemPattern->SetIsSubMenuShowed(true);
290 menuItemPattern->AddHoverRegions(OffsetF(0, 0), OffsetF(100, 100));
291 const auto& mousePosition = mouseInfo.GetGlobalLocation();
292 EXPECT_TRUE(!menuItemPattern->IsInHoverRegions(mousePosition.GetX(), mousePosition.GetY()));
293 EXPECT_TRUE(menuItemPattern->IsSubMenuShowed());
294 /**
295 * @tc.steps: step2. Create menuWrapper
296 * @tc.expected: wrapperPattern is not null
297 */
298 auto wrapperNode =
299 FrameNode::CreateFrameNode(V2::MENU_WRAPPER_ETS_TAG, 1, AceType::MakeRefPtr<MenuWrapperPattern>(1));
300 auto wrapperPattern = wrapperNode->GetPattern<MenuWrapperPattern>();
301 wrapperPattern->HandleMouseEvent(mouseInfo, menuItemPattern);
302 ASSERT_NE(wrapperPattern, nullptr);
303 /**
304 * @tc.steps: step3. add submenu to wrapper
305 * @tc.expected: wrapper child size is 2
306 */
307 auto mainMenu =
308 FrameNode::CreateFrameNode(V2::MENU_ETS_TAG, 2, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::MENU));
309 auto subMenu = FrameNode::CreateFrameNode(
310 V2::MENU_ETS_TAG, 2, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::SUB_MENU));
311 auto currentMenuItemNode =
312 FrameNode::CreateFrameNode(V2::MENU_ITEM_ETS_TAG, 101, AceType::MakeRefPtr<MenuItemPattern>());
313 auto currentMenuItemPattern = currentMenuItemNode->GetPattern<MenuItemPattern>();
314 auto subMenuPattern = subMenu->GetPattern<MenuPattern>();
315 currentMenuItemPattern->SetIsSubMenuShowed(true);
316 mainMenu->MountToParent(wrapperNode);
317 subMenu->MountToParent(wrapperNode);
318 EXPECT_EQ(wrapperNode->GetChildren().size(), 2);
319 /**
320 * @tc.steps: step4. excute HandleMouseEvent
321 * @tc.expected: menuItemPattern IsSubMenuShowed as expected
322 */
323 subMenuPattern->SetParentMenuItem(currentMenuItemNode);
324 wrapperPattern->HandleMouseEvent(mouseInfo, menuItemPattern);
325 EXPECT_TRUE(currentMenuItemPattern->IsSubMenuShowed());
326 subMenuPattern->SetParentMenuItem(menuItemNode);
327 wrapperPattern->HandleMouseEvent(mouseInfo, menuItemPattern);
328 EXPECT_FALSE(menuItemPattern->IsSubMenuShowed());
329 }
330
331 /**
332 * @tc.name: MenuWrapperPatternTestNg005
333 * @tc.desc: Verify OnTouchEvent.
334 * @tc.type: FUNC
335 */
336 HWTEST_F(MenuTestNg, MenuWrapperPatternTestNg005, TestSize.Level1)
337 {
338 /**
339 * @tc.steps: step1. Create touchEventInfo
340 * @tc.expected: touchEventInfo size is 1
341 */
342 TouchEventInfo contextMenuTouchUpEventInfo(MENU_TOUCH_EVENT_TYPE);
343 TouchLocationInfo upLocationInfo(TARGET_ID);
344 Offset touchUpGlobalLocation(80, 80);
345 upLocationInfo.SetTouchType(TouchType::MOVE);
346 auto touchUpLocationInfo = upLocationInfo.SetGlobalLocation(touchUpGlobalLocation);
347 contextMenuTouchUpEventInfo.touches_.emplace_back(touchUpLocationInfo);
348 EXPECT_EQ(contextMenuTouchUpEventInfo.touches_.size(), 1);
349 /**
350 * @tc.steps: step2. update touchEventInfo, excute OnTouchEvent
351 * @tc.expected: touchEventInfo size is 1
352 */
353 auto wrapperNode =
354 FrameNode::CreateFrameNode(V2::MENU_WRAPPER_ETS_TAG, 1, AceType::MakeRefPtr<MenuWrapperPattern>(1));
355 auto wrapperPattern = wrapperNode->GetPattern<MenuWrapperPattern>();
356 ASSERT_NE(wrapperPattern, nullptr);
357 wrapperPattern->OnTouchEvent(contextMenuTouchUpEventInfo);
358 upLocationInfo.SetTouchType(TouchType::UP);
359 touchUpLocationInfo = upLocationInfo.SetGlobalLocation(touchUpGlobalLocation);
360 contextMenuTouchUpEventInfo.touches_.clear();
361 contextMenuTouchUpEventInfo.touches_.emplace_back(touchUpLocationInfo);
362 wrapperPattern->OnTouchEvent(contextMenuTouchUpEventInfo);
363 wrapperPattern->isHided_ = true;
364 wrapperPattern->OnTouchEvent(contextMenuTouchUpEventInfo);
365 wrapperPattern->isHided_ = false;
366 wrapperPattern->OnTouchEvent(contextMenuTouchUpEventInfo);
367 EXPECT_EQ(contextMenuTouchUpEventInfo.touches_.size(), 1);
368 /**
369 * @tc.steps: step3. add submenu to wrapper,excute OnTouchEvent
370 * @tc.expected: wrapper child size is 2
371 */
372 auto topMenu =
373 FrameNode::CreateFrameNode(V2::MENU_ETS_TAG, 2, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::MENU));
374 auto bottomMenu =
375 FrameNode::CreateFrameNode(V2::MENU_ETS_TAG, 3, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::MENU));
376 auto subMenu = FrameNode::CreateFrameNode(
377 V2::MENU_ETS_TAG, 4, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::SUB_MENU));
378 topMenu->GetGeometryNode()->SetFrameSize(SizeF(100, 100));
379 topMenu->MountToParent(wrapperNode);
380 bottomMenu->GetGeometryNode()->SetFrameSize(SizeF(70, 70));
381 bottomMenu->MountToParent(wrapperNode);
382 subMenu->GetGeometryNode()->SetFrameSize(SizeF(70, 70));
383 subMenu->MountToParent(wrapperNode);
384 wrapperPattern->OnTouchEvent(contextMenuTouchUpEventInfo);
385 EXPECT_EQ(wrapperNode->GetChildren().size(), 3);
386 }
387
388 /**
389 * @tc.name: MenuPatternTestNg001
390 * @tc.desc: Verify RegisterOnTouch.
391 * @tc.type: FUNC
392 */
393 HWTEST_F(MenuTestNg, MenuPatternTestNg001, TestSize.Level1)
394 {
395 RefPtr<MenuPattern> menuPattern = AceType::MakeRefPtr<MenuPattern>(TARGET_ID, "", TYPE);
396 std::string type = "1";
397 TouchEventInfo info(type);
398 menuPattern->RegisterOnTouch();
399 EXPECT_TRUE(info.GetTouches().empty());
400 }
401
402 /**
403 * @tc.name: MenuPatternTestNg002
404 * @tc.desc: Verify RegisterOnTouch.
405 * @tc.type: FUNC
406 */
407 HWTEST_F(MenuTestNg, MenuPatternTestNg002, TestSize.Level1)
408 {
409 RefPtr<MenuPattern> menuPattern = AceType::MakeRefPtr<MenuPattern>(TARGET_ID, "", TYPE);
410 std::string type = "1";
411 TouchType touchType = TouchType::UP;
412 TouchEventInfo info(type);
413 TouchLocationInfo locationInfo(TARGET_ID);
414 Offset globalLocation(1, 1);
415 locationInfo.SetTouchType(touchType);
416 auto touchLocationInfo = locationInfo.SetGlobalLocation(globalLocation);
417 info.touches_.emplace_back(touchLocationInfo);
418 menuPattern->RegisterOnTouch();
419 EXPECT_FALSE(info.GetTouches().empty());
420 EXPECT_TRUE(info.GetTouches().front().GetTouchType() == TouchType::UP);
421 }
422
423 /**
424 * @tc.name: MenuPatternTestNg003
425 * @tc.desc: Verify RegisterOnTouch.
426 * @tc.type: FUNC
427 */
428 HWTEST_F(MenuTestNg, MenuPatternTestNg003, TestSize.Level1)
429 {
430 RefPtr<MenuPattern> menuPattern = AceType::MakeRefPtr<MenuPattern>(TARGET_ID, "", TYPE);
431 std::string type = "1";
432 TouchType touchType = TouchType::UP;
433 TouchEventInfo info(type);
434 TouchLocationInfo locationInfo(TARGET_ID);
435 Offset globalLocation(1, 1);
436 locationInfo.SetTouchType(touchType);
437 auto touchLocationInfo = locationInfo.SetGlobalLocation(globalLocation);
438 info.touches_.emplace_back(touchLocationInfo);
439 menuPattern->type_ = MenuType::CONTEXT_MENU;
440 menuPattern->RegisterOnTouch();
441 EXPECT_FALSE(info.GetTouches().empty());
442 EXPECT_TRUE(info.GetTouches().front().GetTouchType() == TouchType::UP);
443 }
444
445 /**
446 * @tc.name: MenuPatternTestNg004
447 * @tc.desc: Verify RegisterOnTouch.
448 * @tc.type: FUNC
449 */
450 HWTEST_F(MenuTestNg, MenuPatternTestNg004, TestSize.Level1)
451 {
452 RefPtr<MenuPattern> menuPattern = AceType::MakeRefPtr<MenuPattern>(TARGET_ID, "", TYPE);
453 std::string type = "1";
454 TouchEventInfo info(type);
455 TouchLocationInfo locationInfo(TARGET_ID);
456 Offset globalLocation(1, 1);
457 auto touchLocationInfo = locationInfo.SetGlobalLocation(globalLocation);
458 info.touches_.emplace_back(touchLocationInfo);
459 menuPattern->RegisterOnTouch();
460 EXPECT_FALSE(info.GetTouches().empty());
461 EXPECT_TRUE(info.GetTouches().front().GetTouchType() == TouchType::UNKNOWN);
462 }
463
464 /**
465 * @tc.name: MenuPatternTestNg005
466 * @tc.desc: Verify RegisterOnTouch.
467 * @tc.type: FUNC
468 */
469 HWTEST_F(MenuTestNg, MenuPatternTestNg005, TestSize.Level1)
470 {
471 RefPtr<MenuPattern> menuPattern = AceType::MakeRefPtr<MenuPattern>(TARGET_ID, "", TYPE);
472 std::string type = "1";
473 TouchEventInfo info(type);
474 TouchType touchType = TouchType::UP;
475 TouchLocationInfo locationInfo(TARGET_ID);
476 locationInfo.SetTouchType(touchType);
477 menuPattern->RegisterOnTouch();
478 EXPECT_TRUE(info.GetTouches().empty());
479 EXPECT_FALSE(info.GetTouches().front().GetTouchType() == TouchType::UP);
480 }
481
482 /**
483 * @tc.name: MenuPatternTestNg006
484 * @tc.desc: Verify UpdateMenuItemChildren.
485 * @tc.type: FUNC
486 */
487 HWTEST_F(MenuTestNg, MenuPatternTestNg006, TestSize.Level1)
488 {
489 MenuModelNG MneuModelInstance;
490 MneuModelInstance.Create();
491 MneuModelInstance.SetFontSize(Dimension(25.0));
492 MneuModelInstance.SetFontColor(Color::RED);
493 MneuModelInstance.SetFontWeight(FontWeight::BOLD);
494 MneuModelInstance.SetFontStyle(Ace::FontStyle::ITALIC);
495
496 auto menuNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
497 ASSERT_NE(menuNode, nullptr);
498 auto menuPattern = menuNode->GetPattern<MenuPattern>();
499 ASSERT_NE(menuPattern, nullptr);
500 auto layoutProperty = menuPattern->GetLayoutProperty<MenuLayoutProperty>();
501 ASSERT_NE(layoutProperty, nullptr);
502
503 // call UpdateMenuItemChildren
504 menuPattern->OnModifyDone();
505
506 ASSERT_TRUE(layoutProperty->GetFontSize().has_value());
507 EXPECT_EQ(layoutProperty->GetFontSize().value(), Dimension(25.0));
508 ASSERT_TRUE(layoutProperty->GetFontWeight().has_value());
509 EXPECT_EQ(layoutProperty->GetFontWeight().value(), FontWeight::BOLD);
510 ASSERT_TRUE(layoutProperty->GetFontColor().has_value());
511 EXPECT_EQ(layoutProperty->GetFontColor().value(), Color::RED);
512 ASSERT_TRUE(layoutProperty->GetItalicFontStyle().has_value());
513 EXPECT_EQ(layoutProperty->GetItalicFontStyle().value(), Ace::FontStyle::ITALIC);
514 }
515
516 /**
517 * @tc.name: MenuPatternTestNg007
518 * @tc.desc: Verify UpdateMenuItemChildren.
519 * @tc.type: FUNC
520 */
521 HWTEST_F(MenuTestNg, MenuPatternTestNg007, TestSize.Level1)
522 {
523 MenuModelNG MneuModelInstance;
524 MenuItemModelNG MneuItemModelInstance;
525 MneuModelInstance.Create();
526 MneuModelInstance.SetFontSize(Dimension(25.0));
527 MneuModelInstance.SetFontColor(Color::RED);
528 MneuModelInstance.SetFontWeight(FontWeight::BOLD);
529
530 auto menuNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
531 ASSERT_NE(menuNode, nullptr);
532 auto menuPattern = menuNode->GetPattern<MenuPattern>();
533 ASSERT_NE(menuPattern, nullptr);
534 auto layoutProperty = menuPattern->GetLayoutProperty<MenuLayoutProperty>();
535 ASSERT_NE(layoutProperty, nullptr);
536
537 MenuItemProperties itemOption;
538 itemOption.content = "content";
539 itemOption.labelInfo = "label";
540 MneuItemModelInstance.Create(itemOption);
541 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
542 ASSERT_NE(itemNode, nullptr);
543 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
544 ASSERT_NE(itemPattern, nullptr);
545 itemPattern->OnModifyDone();
546 itemNode->MountToParent(menuNode);
547 itemNode->OnMountToParentDone();
548
549 // call UpdateMenuItemChildren
550 menuPattern->OnModifyDone();
551
552 auto contentNode = itemPattern->GetContentNode();
553 ASSERT_NE(contentNode, nullptr);
554 auto textProperty = contentNode->GetLayoutProperty<TextLayoutProperty>();
555 ASSERT_NE(textProperty, nullptr);
556 ASSERT_TRUE(textProperty->GetContent().has_value());
557 EXPECT_EQ(textProperty->GetContent().value(), "content");
558 ASSERT_TRUE(textProperty->GetFontSize().has_value());
559 EXPECT_EQ(textProperty->GetFontSize().value(), Dimension(25.0));
560 ASSERT_TRUE(textProperty->GetFontWeight().has_value());
561 EXPECT_EQ(textProperty->GetFontWeight().value(), FontWeight::BOLD);
562 ASSERT_TRUE(textProperty->GetTextColor().has_value());
563 EXPECT_EQ(textProperty->GetTextColor().value(), Color::RED);
564
565 auto labelNode = itemPattern->GetLabelNode();
566 ASSERT_NE(labelNode, nullptr);
567 auto labelProperty = labelNode->GetLayoutProperty<TextLayoutProperty>();
568 ASSERT_NE(labelProperty, nullptr);
569 ASSERT_TRUE(labelProperty->GetContent().has_value());
570 EXPECT_EQ(labelProperty->GetContent().value(), "label");
571 ASSERT_TRUE(labelProperty->GetFontSize().has_value());
572 EXPECT_EQ(labelProperty->GetFontSize().value(), Dimension(25.0));
573 ASSERT_TRUE(labelProperty->GetFontWeight().has_value());
574 EXPECT_EQ(labelProperty->GetFontWeight().value(), FontWeight::BOLD);
575 ASSERT_TRUE(labelProperty->GetTextColor().has_value());
576 EXPECT_EQ(labelProperty->GetTextColor().value(), Color::RED);
577 }
578
579 /**
580 * @tc.name: MenuPatternTestNg008
581 * @tc.desc: Verify UpdateMenuItemChildren.
582 * @tc.type: FUNC
583 */
584 HWTEST_F(MenuTestNg, MenuPatternTestNg008, TestSize.Level1)
585 {
586 MenuModelNG MneuModelInstance;
587 MenuItemModelNG MneuItemModelInstance;
588 MneuModelInstance.Create();
589 MneuModelInstance.SetFontSize(Dimension(25.0));
590 MneuModelInstance.SetFontColor(Color::RED);
591 MneuModelInstance.SetFontWeight(FontWeight::BOLD);
592
593 auto menuNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
594 ASSERT_NE(menuNode, nullptr);
595 auto menuPattern = menuNode->GetPattern<MenuPattern>();
596 ASSERT_NE(menuPattern, nullptr);
597 auto layoutProperty = menuPattern->GetLayoutProperty<MenuLayoutProperty>();
598 ASSERT_NE(layoutProperty, nullptr);
599
600 MenuItemProperties itemOption;
601 itemOption.content = "content";
602 itemOption.labelInfo = "label";
603 MneuItemModelInstance.Create(itemOption);
604 MneuItemModelInstance.SetFontSize(Dimension(35.0));
605 MneuItemModelInstance.SetFontColor(Color::BLUE);
606 MneuItemModelInstance.SetFontWeight(FontWeight::LIGHTER);
607 MneuItemModelInstance.SetLabelFontSize(Dimension(40.0));
608 MneuItemModelInstance.SetLabelFontColor(Color::GRAY);
609 MneuItemModelInstance.SetLabelFontWeight(FontWeight::LIGHTER);
610 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
611 ASSERT_NE(itemNode, nullptr);
612 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
613 ASSERT_NE(itemPattern, nullptr);
614 itemPattern->OnModifyDone();
615 itemNode->MountToParent(menuNode);
616 itemNode->OnMountToParentDone();
617
618 // call UpdateMenuItemChildren
619 menuPattern->OnModifyDone();
620
621 auto contentNode = itemPattern->GetContentNode();
622 ASSERT_NE(contentNode, nullptr);
623 auto textProperty = contentNode->GetLayoutProperty<TextLayoutProperty>();
624 ASSERT_NE(textProperty, nullptr);
625 ASSERT_TRUE(textProperty->GetContent().has_value());
626 EXPECT_EQ(textProperty->GetContent().value(), "content");
627 ASSERT_TRUE(textProperty->GetFontSize().has_value());
628 EXPECT_EQ(textProperty->GetFontSize().value(), Dimension(35.0));
629 ASSERT_TRUE(textProperty->GetFontWeight().has_value());
630 EXPECT_EQ(textProperty->GetFontWeight().value(), FontWeight::LIGHTER);
631 ASSERT_TRUE(textProperty->GetTextColor().has_value());
632 EXPECT_EQ(textProperty->GetTextColor().value(), Color::BLUE);
633
634 auto labelNode = itemPattern->GetLabelNode();
635 ASSERT_NE(labelNode, nullptr);
636 auto labelProperty = labelNode->GetLayoutProperty<TextLayoutProperty>();
637 ASSERT_NE(labelProperty, nullptr);
638 ASSERT_TRUE(labelProperty->GetContent().has_value());
639 EXPECT_EQ(labelProperty->GetContent().value(), "label");
640 ASSERT_TRUE(labelProperty->GetFontSize().has_value());
641 EXPECT_EQ(labelProperty->GetFontSize().value(), Dimension(40.0));
642 ASSERT_TRUE(labelProperty->GetFontWeight().has_value());
643 EXPECT_EQ(labelProperty->GetFontWeight().value(), FontWeight::LIGHTER);
644 ASSERT_TRUE(labelProperty->GetTextColor().has_value());
645 EXPECT_EQ(labelProperty->GetTextColor().value(), Color::GRAY);
646 }
647
648 /**
649 * @tc.name: MenuPatternTestNg009
650 * @tc.desc: Verify UpdateMenuItemChildren.
651 * @tc.type: FUNC
652 */
653 HWTEST_F(MenuTestNg, MenuPatternTestNg009, TestSize.Level1)
654 {
655 MenuModelNG MneuModelInstance;
656 MenuItemModelNG MneuItemModelInstance;
657 MneuModelInstance.Create();
658 MneuModelInstance.SetFontSize(Dimension(25.0));
659 MneuModelInstance.SetFontColor(Color::RED);
660 MneuModelInstance.SetFontWeight(FontWeight::BOLD);
661
662 auto menuNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
663 ASSERT_NE(menuNode, nullptr);
664 auto menuPattern = menuNode->GetPattern<MenuPattern>();
665 ASSERT_NE(menuPattern, nullptr);
666 auto layoutProperty = menuPattern->GetLayoutProperty<MenuLayoutProperty>();
667 ASSERT_NE(layoutProperty, nullptr);
668
669 MenuItemGroupView::Create();
670 auto groupNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
671 MenuItemGroupView::Create();
672 auto groupNode2 = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
673 ASSERT_NE(groupNode, nullptr);
674 auto groupPattern = groupNode->GetPattern<MenuItemGroupPattern>();
675
676 MenuItemProperties itemOption;
677 itemOption.content = "content";
678 itemOption.labelInfo = "label";
679 MneuItemModelInstance.Create(itemOption);
680 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
681 ASSERT_NE(itemNode, nullptr);
682 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
683 ASSERT_NE(itemPattern, nullptr);
684 itemPattern->OnModifyDone();
685 itemNode->MountToParent(groupNode);
686 itemNode->OnMountToParentDone();
687 groupPattern->OnModifyDone();
688 groupNode->MountToParent(menuNode);
689 groupNode->OnMountToParentDone();
690 groupNode2->MountToParent(menuNode);
691 groupNode2->OnMountToParentDone();
692
693 // call UpdateMenuItemChildren
694 menuPattern->OnModifyDone();
695
696 auto contentNode = itemPattern->GetContentNode();
697 ASSERT_NE(contentNode, nullptr);
698 auto textProperty = contentNode->GetLayoutProperty<TextLayoutProperty>();
699 ASSERT_NE(textProperty, nullptr);
700 ASSERT_TRUE(textProperty->GetContent().has_value());
701 EXPECT_EQ(textProperty->GetContent().value(), "content");
702 ASSERT_TRUE(textProperty->GetFontSize().has_value());
703 EXPECT_EQ(textProperty->GetFontSize().value(), Dimension(25.0));
704 ASSERT_TRUE(textProperty->GetFontWeight().has_value());
705 EXPECT_EQ(textProperty->GetFontWeight().value(), FontWeight::BOLD);
706 ASSERT_TRUE(textProperty->GetTextColor().has_value());
707 EXPECT_EQ(textProperty->GetTextColor().value(), Color::RED);
708
709 auto labelNode = itemPattern->GetLabelNode();
710 ASSERT_NE(labelNode, nullptr);
711 auto labelProperty = labelNode->GetLayoutProperty<TextLayoutProperty>();
712 ASSERT_NE(labelProperty, nullptr);
713 ASSERT_TRUE(labelProperty->GetContent().has_value());
714 EXPECT_EQ(labelProperty->GetContent().value(), "label");
715 ASSERT_TRUE(labelProperty->GetFontSize().has_value());
716 EXPECT_EQ(labelProperty->GetFontSize().value(), Dimension(25.0));
717 ASSERT_TRUE(labelProperty->GetFontWeight().has_value());
718 EXPECT_EQ(labelProperty->GetFontWeight().value(), FontWeight::BOLD);
719 ASSERT_TRUE(labelProperty->GetTextColor().has_value());
720 EXPECT_EQ(labelProperty->GetTextColor().value(), Color::RED);
721 }
722
723 /**
724 * @tc.name: MenuPatternTestNg010
725 * @tc.desc: Verify UpdateMenuItemChildren.
726 * @tc.type: FUNC
727 */
728 HWTEST_F(MenuTestNg, MenuPatternTestNg010, TestSize.Level1)
729 {
730 MenuModelNG MneuModelInstance;
731 MenuItemModelNG MneuItemModelInstance;
732 MneuModelInstance.Create();
733 auto menuNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
734 ASSERT_NE(menuNode, nullptr);
735 auto menuPattern = menuNode->GetPattern<MenuPattern>();
736 ASSERT_NE(menuPattern, nullptr);
737 auto layoutProperty = menuPattern->GetLayoutProperty<MenuLayoutProperty>();
738 ASSERT_NE(layoutProperty, nullptr);
739
740 MenuItemGroupView::Create();
741 auto groupNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
742 ASSERT_NE(groupNode, nullptr);
743 auto groupPattern = groupNode->GetPattern<MenuItemGroupPattern>();
744
745 MenuItemProperties itemOption;
746 itemOption.content = "content";
747 MneuItemModelInstance.Create(itemOption);
748 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
749 ASSERT_NE(itemNode, nullptr);
750 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
751 ASSERT_NE(itemPattern, nullptr);
752 itemPattern->OnModifyDone();
753 itemNode->MountToParent(groupNode);
754 itemNode->OnMountToParentDone();
755 auto textNode = FrameNode::CreateFrameNode(
756 V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<TextPattern>());
757 auto textPattern = textNode->GetPattern<TextPattern>();
758 textPattern->OnModifyDone();
759 textNode->MountToParent(groupNode);
760 textNode->OnMountToParentDone();
761 groupPattern->OnModifyDone();
762 groupNode->MountToParent(menuNode);
763 groupNode->OnMountToParentDone();
764
765 // call UpdateMenuItemChildren
766 menuPattern->OnModifyDone();
767
768 auto contentNode = itemPattern->GetContentNode();
769 ASSERT_NE(contentNode, nullptr);
770 auto textProperty = contentNode->GetLayoutProperty<TextLayoutProperty>();
771 ASSERT_NE(textProperty, nullptr);
772 ASSERT_TRUE(textProperty->GetContent().has_value());
773 EXPECT_EQ(textProperty->GetContent().value(), "content");
774 ASSERT_TRUE(textProperty->GetFontSize().has_value());
775 ASSERT_TRUE(textProperty->GetFontWeight().has_value());
776 EXPECT_EQ(textProperty->GetFontWeight().value(), FontWeight::REGULAR);
777 ASSERT_TRUE(textProperty->GetTextColor().has_value());
778
779 auto labelNode = itemPattern->GetLabelNode();
780 ASSERT_EQ(labelNode, nullptr);
781 }
782
783 /**
784 * @tc.name: MenuPatternTestNg011
785 * @tc.desc: Verify UpdateMenuItemChildren.
786 * @tc.type: FUNC
787 */
788 HWTEST_F(MenuTestNg, MenuPatternTestNg011, TestSize.Level1)
789 {
790 MenuModelNG MneuModelInstance;
791 MenuItemModelNG MneuItemModelInstance;
792 MneuModelInstance.Create();
793 MneuModelInstance.SetFontSize(Dimension(25.0));
794 MneuModelInstance.SetFontColor(Color::RED);
795 MneuModelInstance.SetFontWeight(FontWeight::BOLD);
796 auto menuNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
797 ASSERT_NE(menuNode, nullptr);
798 auto menuPattern = menuNode->GetPattern<MenuPattern>();
799 ASSERT_NE(menuPattern, nullptr);
800 auto layoutProperty = menuPattern->GetLayoutProperty<MenuLayoutProperty>();
801 ASSERT_NE(layoutProperty, nullptr);
802
803 MenuItemProperties itemOption;
804 itemOption.content = "content";
805 MneuItemModelInstance.Create(itemOption);
806 MneuItemModelInstance.SetFontStyle(Ace::FontStyle::ITALIC);
807 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
808 ASSERT_NE(itemNode, nullptr);
809 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
810 ASSERT_NE(itemPattern, nullptr);
811 itemPattern->OnModifyDone();
812 itemNode->MountToParent(menuNode);
813 itemNode->OnMountToParentDone();
814
815 // call UpdateMenuItemChildren
816 menuPattern->OnModifyDone();
817 itemPattern->OnModifyDone();
818 auto labelNode = itemPattern->GetLabelNode();
819 ASSERT_EQ(labelNode, nullptr);
820 }
821
822 /**
823 * @tc.name: MenuPatternTestNg012
824 * @tc.desc: Verify UpdateSelectParam.
825 * @tc.type: FUNC
826 */
827 HWTEST_F(MenuTestNg, MenuPatternTestNg012, TestSize.Level1)
828 {
829 std::vector<SelectParam> params;
830 params.emplace_back("content1", "icon1");
831 params.emplace_back("content2", "");
832 params.emplace_back("", "icon3");
833 params.emplace_back("", "");
834 auto wrapperNode = MenuView::Create(params, TARGET_ID);
835 ASSERT_NE(wrapperNode, nullptr);
836 auto menuNode = AceType::DynamicCast<FrameNode>(wrapperNode->GetChildAtIndex(0));
837 ASSERT_NE(menuNode, nullptr);
838
839 auto menuPattern = menuNode->GetPattern<MenuPattern>();
840 ASSERT_NE(menuPattern, nullptr);
841 ASSERT_EQ(menuPattern->GetOptions().size(), 4);
842 const auto& children = menuPattern->GetOptions();
843 auto childIt = children.begin();
844 for (size_t i = 0; i < children.size(); i++, childIt++) {
845 const auto& childNode = AceType::DynamicCast<FrameNode>(*childIt);
846 ASSERT_NE(childNode, nullptr);
847 auto optionPattern = childNode->GetPattern<OptionPattern>();
848 ASSERT_NE(optionPattern, nullptr);
849 ASSERT_NE(optionPattern->text_, nullptr);
850 auto textProps = optionPattern->text_->GetLayoutProperty<TextLayoutProperty>();
851 ASSERT_NE(textProps, nullptr);
852 auto param = params.at(i);
853 EXPECT_EQ(textProps->GetContent().value_or(""), param.first);
854 if (param.second.empty()) {
855 ASSERT_EQ(optionPattern->icon_, nullptr);
856 } else {
857 ASSERT_NE(optionPattern->icon_, nullptr);
858 auto imageProps = optionPattern->icon_->GetLayoutProperty<ImageLayoutProperty>();
859 ASSERT_NE(imageProps, nullptr);
860 auto imageSrcInfo = imageProps->GetImageSourceInfo();
861 ASSERT_TRUE(imageSrcInfo.has_value());
862 ASSERT_EQ(imageSrcInfo->GetSrc(), param.second);
863 }
864 }
865
866 params.clear();
867 menuPattern->UpdateSelectParam(params);
868 ASSERT_EQ(menuPattern->GetOptions().size(), 0);
869 }
870
871 /**
872 * @tc.name: MenuPatternTestNg013
873 * @tc.desc: Verify UpdateSelectParam.
874 * @tc.type: FUNC
875 */
876 HWTEST_F(MenuTestNg, MenuPatternTestNg013, TestSize.Level1)
877 {
878 std::vector<SelectParam> params;
879 auto wrapperNode = MenuView::Create(params, TARGET_ID);
880 ASSERT_NE(wrapperNode, nullptr);
881 auto menuNode = AceType::DynamicCast<FrameNode>(wrapperNode->GetChildAtIndex(0));
882 ASSERT_NE(menuNode, nullptr);
883 ASSERT_EQ(menuNode->GetChildren().size(), 1);
884
885 params.emplace_back("content1", "icon1");
886 params.emplace_back("content2", "");
887 params.emplace_back("", "icon3");
888 params.emplace_back("", "");
889 auto menuPattern = menuNode->GetPattern<MenuPattern>();
890 ASSERT_NE(menuPattern, nullptr);
891 menuPattern->UpdateSelectParam(params);
892
893 ASSERT_EQ(menuPattern->GetOptions().size(), 4);
894 const auto& children = menuPattern->GetOptions();
895 auto childIt = children.begin();
896 for (size_t i = 0; i < children.size(); i++, childIt++) {
897 const auto& childNode = AceType::DynamicCast<FrameNode>(*childIt);
898 ASSERT_NE(childNode, nullptr);
899 auto optionPattern = childNode->GetPattern<OptionPattern>();
900 ASSERT_NE(optionPattern, nullptr);
901 ASSERT_NE(optionPattern->text_, nullptr);
902 auto textProps = optionPattern->text_->GetLayoutProperty<TextLayoutProperty>();
903 ASSERT_NE(textProps, nullptr);
904 auto param = params.at(i);
905 EXPECT_EQ(textProps->GetContent().value_or(""), param.first);
906 if (param.second.empty()) {
907 ASSERT_EQ(optionPattern->icon_, nullptr);
908 } else {
909 ASSERT_NE(optionPattern->icon_, nullptr);
910 auto imageProps = optionPattern->icon_->GetLayoutProperty<ImageLayoutProperty>();
911 ASSERT_NE(imageProps, nullptr);
912 auto imageSrcInfo = imageProps->GetImageSourceInfo();
913 ASSERT_TRUE(imageSrcInfo.has_value());
914 ASSERT_EQ(imageSrcInfo->GetSrc(), param.second);
915 }
916 }
917 }
918
919 /**
920 * @tc.name: MenuPatternTestNg014
921 * @tc.desc: Verify UpdateSelectParam.
922 * @tc.type: FUNC
923 */
924 HWTEST_F(MenuTestNg, MenuPatternTestNg014, TestSize.Level1)
925 {
926 std::vector<SelectParam> params;
927 auto wrapperNode = MenuView::Create(params, TARGET_ID);
928 ASSERT_NE(wrapperNode, nullptr);
929 auto menuNode = AceType::DynamicCast<FrameNode>(wrapperNode->GetChildAtIndex(0));
930 ASSERT_NE(menuNode, nullptr);
931 auto menuPattern = menuNode->GetPattern<MenuPattern>();
932 ASSERT_NE(menuPattern, nullptr);
933 ASSERT_EQ(menuPattern->GetOptions().size(), 0);
934
935 menuPattern->UpdateSelectParam(params);
936 ASSERT_EQ(menuPattern->GetOptions().size(), 0);
937 }
938
939 /**
940 * @tc.name: MenuPatternTestNg015
941 * @tc.desc: Verify UpdateSelectParam.
942 * @tc.type: FUNC
943 */
944 HWTEST_F(MenuTestNg, MenuPatternTestNg015, TestSize.Level1)
945 {
946 std::vector<SelectParam> params;
947 params.emplace_back("content1", "icon1");
948 params.emplace_back("content2", "");
949 params.emplace_back("", "icon3");
950 params.emplace_back("", "");
951 auto wrapperNode = MenuView::Create(params, TARGET_ID);
952 ASSERT_NE(wrapperNode, nullptr);
953 auto menuNode = AceType::DynamicCast<FrameNode>(wrapperNode->GetChildAtIndex(0));
954 ASSERT_NE(menuNode, nullptr);
955
956 auto menuPattern = menuNode->GetPattern<MenuPattern>();
957 ASSERT_NE(menuPattern, nullptr);
958 ASSERT_EQ(menuPattern->GetOptions().size(), 4);
959
960 params.clear();
961 params.emplace_back("content1_new", "");
962 params.emplace_back("content2_new", "icon2_new");
963 params.emplace_back("", "");
964 params.emplace_back("", "icon4_new");
965 menuPattern->UpdateSelectParam(params);
966 ASSERT_EQ(menuPattern->GetOptions().size(), 4);
967
968 const auto& children = menuPattern->GetOptions();
969 auto childIt = children.begin();
970 for (size_t i = 0; i < children.size(); i++, childIt++) {
971 const auto& childNode = AceType::DynamicCast<FrameNode>(*childIt);
972 ASSERT_NE(childNode, nullptr);
973 auto optionPattern = childNode->GetPattern<OptionPattern>();
974 ASSERT_NE(optionPattern, nullptr);
975 ASSERT_NE(optionPattern->text_, nullptr);
976 auto textProps = optionPattern->text_->GetLayoutProperty<TextLayoutProperty>();
977 ASSERT_NE(textProps, nullptr);
978 auto param = params.at(i);
979 EXPECT_EQ(textProps->GetContent().value_or(""), param.first);
980 if (param.second.empty()) {
981 ASSERT_EQ(optionPattern->icon_, nullptr);
982 } else {
983 ASSERT_NE(optionPattern->icon_, nullptr);
984 auto imageProps = optionPattern->icon_->GetLayoutProperty<ImageLayoutProperty>();
985 ASSERT_NE(imageProps, nullptr);
986 auto imageSrcInfo = imageProps->GetImageSourceInfo();
987 ASSERT_TRUE(imageSrcInfo.has_value());
988 ASSERT_EQ(imageSrcInfo->GetSrc(), param.second);
989 }
990 }
991 }
992
993 /**
994 * @tc.name: MenuPatternTestNg016
995 * @tc.desc: Verify UpdateSelectParam.
996 * @tc.type: FUNC
997 */
998 HWTEST_F(MenuTestNg, MenuPatternTestNg016, TestSize.Level1)
999 {
1000 std::vector<SelectParam> params;
1001 params.emplace_back("content1", "icon1");
1002 params.emplace_back("content2", "");
1003 params.emplace_back("", "icon3");
1004 params.emplace_back("", "");
1005 auto wrapperNode = MenuView::Create(params, TARGET_ID);
1006 ASSERT_NE(wrapperNode, nullptr);
1007 auto menuNode = AceType::DynamicCast<FrameNode>(wrapperNode->GetChildAtIndex(0));
1008 ASSERT_NE(menuNode, nullptr);
1009
1010 auto menuPattern = menuNode->GetPattern<MenuPattern>();
1011 ASSERT_NE(menuPattern, nullptr);
1012 ASSERT_EQ(menuPattern->GetOptions().size(), 4);
1013
1014 params.clear();
1015 params.emplace_back("content1_new", "");
1016 params.emplace_back("content2_new", "icon2_new");
1017 params.emplace_back("", "");
1018 menuPattern->UpdateSelectParam(params);
1019 ASSERT_EQ(menuPattern->GetOptions().size(), 3);
1020
1021 const auto& children = menuPattern->GetOptions();
1022 auto childIt = children.begin();
1023 for (size_t i = 0; i < children.size(); i++, childIt++) {
1024 const auto& childNode = AceType::DynamicCast<FrameNode>(*childIt);
1025 ASSERT_NE(childNode, nullptr);
1026 auto optionPattern = childNode->GetPattern<OptionPattern>();
1027 ASSERT_NE(optionPattern, nullptr);
1028 ASSERT_NE(optionPattern->text_, nullptr);
1029 auto textProps = optionPattern->text_->GetLayoutProperty<TextLayoutProperty>();
1030 ASSERT_NE(textProps, nullptr);
1031 auto param = params.at(i);
1032 EXPECT_EQ(textProps->GetContent().value_or(""), param.first);
1033 if (param.second.empty()) {
1034 ASSERT_EQ(optionPattern->icon_, nullptr);
1035 } else {
1036 ASSERT_NE(optionPattern->icon_, nullptr);
1037 auto imageProps = optionPattern->icon_->GetLayoutProperty<ImageLayoutProperty>();
1038 ASSERT_NE(imageProps, nullptr);
1039 auto imageSrcInfo = imageProps->GetImageSourceInfo();
1040 ASSERT_TRUE(imageSrcInfo.has_value());
1041 ASSERT_EQ(imageSrcInfo->GetSrc(), param.second);
1042 }
1043 }
1044 }
1045
1046 /**
1047 * @tc.name: MenuPatternTestNg017
1048 * @tc.desc: Verify UpdateSelectParam.
1049 * @tc.type: FUNC
1050 */
1051 HWTEST_F(MenuTestNg, MenuPatternTestNg017, TestSize.Level1)
1052 {
1053 std::vector<SelectParam> params;
1054 params.emplace_back("content1", "icon1");
1055 params.emplace_back("content2", "");
1056 params.emplace_back("", "icon3");
1057 auto wrapperNode = MenuView::Create(params, TARGET_ID);
1058 ASSERT_NE(wrapperNode, nullptr);
1059 auto menuNode = AceType::DynamicCast<FrameNode>(wrapperNode->GetChildAtIndex(0));
1060 ASSERT_NE(menuNode, nullptr);
1061
1062 auto menuPattern = menuNode->GetPattern<MenuPattern>();
1063 ASSERT_NE(menuPattern, nullptr);
1064 ASSERT_EQ(menuPattern->GetOptions().size(), 3);
1065
1066 params.clear();
1067 params.emplace_back("content1_new", "");
1068 params.emplace_back("content2_new", "icon2_new");
1069 params.emplace_back("", "");
1070 params.emplace_back("", "icon4_new");
1071 menuPattern->UpdateSelectParam(params);
1072 ASSERT_EQ(menuPattern->GetOptions().size(), 4);
1073
1074 const auto& children = menuPattern->GetOptions();
1075 auto childIt = children.begin();
1076 for (size_t i = 0; i < children.size(); i++, childIt++) {
1077 const auto& childNode = AceType::DynamicCast<FrameNode>(*childIt);
1078 ASSERT_NE(childNode, nullptr);
1079 auto optionPattern = childNode->GetPattern<OptionPattern>();
1080 ASSERT_NE(optionPattern, nullptr);
1081 ASSERT_NE(optionPattern->text_, nullptr);
1082 auto textProps = optionPattern->text_->GetLayoutProperty<TextLayoutProperty>();
1083 ASSERT_NE(textProps, nullptr);
1084 auto param = params.at(i);
1085 EXPECT_EQ(textProps->GetContent().value_or(""), param.first);
1086 if (param.second.empty()) {
1087 ASSERT_EQ(optionPattern->icon_, nullptr);
1088 } else {
1089 ASSERT_NE(optionPattern->icon_, nullptr);
1090 auto imageProps = optionPattern->icon_->GetLayoutProperty<ImageLayoutProperty>();
1091 ASSERT_NE(imageProps, nullptr);
1092 auto imageSrcInfo = imageProps->GetImageSourceInfo();
1093 ASSERT_TRUE(imageSrcInfo.has_value());
1094 ASSERT_EQ(imageSrcInfo->GetSrc(), param.second);
1095 }
1096 }
1097 }
1098
1099 /**
1100 * @tc.name: MenuPatternTestNg018
1101 * @tc.desc: Verify UpdateMenuItemChildren.
1102 * @tc.type: FUNC
1103 */
1104 HWTEST_F(MenuTestNg, MenuPatternTestNg018, TestSize.Level1)
1105 {
1106 MenuModelNG MneuModelInstance;
1107 MneuModelInstance.Create();
1108 auto menuNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
1109 ASSERT_NE(menuNode, nullptr);
1110 auto menuPattern = menuNode->GetPattern<MenuPattern>();
1111 ASSERT_NE(menuPattern, nullptr);
1112
1113 ASSERT_EQ(menuPattern->GetOptions().size(), 0);
1114 std::vector<SelectParam> params;
1115 params.emplace_back("content1", "icon1");
1116 menuPattern->UpdateSelectParam(params);
1117 ASSERT_EQ(menuPattern->GetOptions().size(), 0);
1118 }
1119
1120 /**
1121 * @tc.ame: MenuPatternTestNg019
1122 * @tc.desc: Test MultiMenu and outer Menu container.
1123 */
1124 HWTEST_F(MenuTestNg, MenuPatternTestNg019, TestSize.Level1)
1125 {
1126 MenuModelNG model;
1127 model.Create();
1128 auto multiMenu = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
1129 auto menuWrapper = MenuView::Create(multiMenu, -1);
1130 ASSERT_NE(menuWrapper, nullptr);
1131 auto outerMenu = AceType::DynamicCast<FrameNode>(menuWrapper->GetFirstChild());
1132 ASSERT_NE(outerMenu, nullptr);
1133
1134 // backgroundColor should be reset
1135 ASSERT_EQ(outerMenu->GetRenderContext()->GetBackgroundColorValue(), Color::TRANSPARENT);
1136
1137 // padding should be moved to inner multi menu
1138 auto scroll = AceType::DynamicCast<FrameNode>(outerMenu->GetFirstChild());
1139 ASSERT_NE(scroll, nullptr);
1140 auto&& padding = scroll->GetLayoutProperty()->GetPaddingProperty();
1141 // should have empty padding
1142 ASSERT_EQ(padding->ToString(), PaddingProperty().ToString());
1143
1144 multiMenu->GetPattern()->BeforeCreateLayoutWrapper();
1145 // inner multi menu should have backgroundColor and padding set up
1146 ASSERT_NE(multiMenu->GetLayoutProperty()->GetPaddingProperty()->ToString(), PaddingProperty().ToString());
1147 ASSERT_NE(multiMenu->GetRenderContext()->GetBackgroundColor(), std::nullopt);
1148 // inner menu should have no shadow
1149 ASSERT_EQ(multiMenu->GetRenderContext()->GetBackShadow(), ShadowConfig::NoneShadow);
1150
1151 // MultiMenu should have its own layout algorithm
1152 auto layoutAlgorithm = multiMenu->GetPattern<MenuPattern>()->CreateLayoutAlgorithm();
1153 ASSERT_NE(AceType::DynamicCast<MultiMenuLayoutAlgorithm>(layoutAlgorithm), nullptr);
1154 }
1155
1156 /**
1157 * @tc.name: MenuPatternTestNg020
1158 * @tc.desc: Verify UpdateMenuItemChildren.
1159 * @tc.type: FUNC
1160 */
1161 HWTEST_F(MenuTestNg, MenuPatternTestNg020, TestSize.Level1)
1162 {
1163 /**
1164 * @tc.steps: step1. create MenuModelNG and MenuItemModelNG object and set FontStyle properties of MenuModelNG.
1165 */
1166 MenuModelNG MneuModelInstance;
1167 MenuItemModelNG MneuItemModelInstance;
1168 MneuModelInstance.Create();
1169 MneuModelInstance.SetFontStyle(Ace::FontStyle::ITALIC);
1170
1171 /**
1172 * @tc.steps: step2. get the frameNode, MenuPattern and MenuLayoutProperty.
1173 * @tc.expected: step2. check whether the objects is available.
1174 */
1175 auto menuNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
1176 ASSERT_NE(menuNode, nullptr);
1177 auto menuPattern = menuNode->GetPattern<MenuPattern>();
1178 ASSERT_NE(menuPattern, nullptr);
1179 auto layoutProperty = menuPattern->GetLayoutProperty<MenuLayoutProperty>();
1180 ASSERT_NE(layoutProperty, nullptr);
1181
1182 /**
1183 * @tc.steps: step3. not set FontStyle properties of MenuModelNG.
1184 */
1185 MenuItemProperties itemOption;
1186 itemOption.content = "content";
1187 itemOption.labelInfo = "label";
1188 MneuItemModelInstance.Create(itemOption);
1189 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
1190 ASSERT_NE(itemNode, nullptr);
1191 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
1192 ASSERT_NE(itemPattern, nullptr);
1193 itemPattern->OnModifyDone();
1194 itemNode->MountToParent(menuNode);
1195 itemNode->OnMountToParentDone();
1196
1197 /**
1198 * @tc.steps: step4. call OnModifyDone of MenuPattern to call UpdateMenuItemChildren
1199 */
1200 menuPattern->OnModifyDone();
1201
1202 /**
1203 * @tc.steps: step5. get the FontStyle properties of menuItemLayoutProperty.
1204 * @tc.expected: step5. check whether the FontStyle properties is is correct.
1205 */
1206 auto contentNode = itemPattern->GetContentNode();
1207 ASSERT_NE(contentNode, nullptr);
1208 auto textProperty = contentNode->GetLayoutProperty<TextLayoutProperty>();
1209 ASSERT_NE(textProperty, nullptr);
1210 ASSERT_TRUE(textProperty->GetItalicFontStyle().has_value());
1211 EXPECT_EQ(textProperty->GetItalicFontStyle().value(), Ace::FontStyle::ITALIC);
1212 }
1213
1214 /**
1215 * @tc.name: MenuPatternTest021
1216 * @tc.desc: Verify RegisterOnTouch.
1217 * @tc.type: FUNC
1218 */
1219 HWTEST_F(MenuTestNg, MenuPatternTestNg021, TestSize.Level1)
1220 {
1221 MenuPattern *menuPattern = new MenuPattern(TARGET_ID, "", TYPE);
1222 const std::string tag = "tag";
1223 int32_t nodeId = 1;
1224 RefPtr<Pattern> pattern = AceType::MakeRefPtr<Pattern>();
1225 bool isRoot = false;
1226 RefPtr<GeometryNode> geometryNode = AceType::MakeRefPtr<GeometryNode>();
1227 RefPtr<FrameNode> frameNode = AceType::MakeRefPtr<FrameNode>(tag, nodeId, pattern, isRoot);
1228 const RefPtr<LayoutWrapperNode> layoutWrapper;
1229 std::string type = "1";
1230 TouchEventInfo info(type);
1231 TouchType touchType = TouchType::UP;
1232 TouchLocationInfo locationInfo(TARGET_ID);
1233 Offset globalLocation(1, 1);
1234 locationInfo.SetTouchType(touchType);
1235 auto touchLocationInfo = locationInfo.SetGlobalLocation(globalLocation);
1236 info.touches_.emplace_back(touchLocationInfo);
1237 menuPattern->OnTouchEvent(info);
1238 menuPattern->RegisterOnTouch();
1239 KeyEvent keyEvent(KeyCode::KEY_ESCAPE, KeyAction::UP);
1240 EXPECT_FALSE(menuPattern->IsMultiMenu());
1241 EXPECT_FALSE(menuPattern->OnKeyEvent(keyEvent));
1242 menuPattern->HideMenu();
1243 menuPattern->HideSubMenu();
1244 EXPECT_FALSE(menuPattern->OnDirtyLayoutWrapperSwap(layoutWrapper, configDirtySwap));
1245 menuPattern->type_ = MenuType::SUB_MENU;
1246 EXPECT_FALSE(menuPattern->OnKeyEvent(keyEvent));
1247 menuPattern->HideMenu();
1248 menuPattern->HideSubMenu();
1249 menuPattern->RemoveParentHoverStyle();
1250 delete menuPattern;
1251 menuPattern = nullptr;
1252 }
1253
1254 /**
1255 * @tc.name: MenuPatternTestNg022
1256 * @tc.desc: Verify OnTouchEvent.
1257 * @tc.type: FUNC
1258 */
1259 HWTEST_F(MenuTestNg, MenuPatternTestNg022, TestSize.Level1)
1260 {
1261 /**
1262 * @tc.steps: step1. multi menu touch event
1263 * @tc.expected: menu type is multi
1264 */
1265 auto multiMenuPattern = AceType::MakeRefPtr<MenuPattern>(-1, "", MenuType::MULTI_MENU);
1266 TouchEventInfo multiMenuTouchEventInfo(MENU_TOUCH_EVENT_TYPE);
1267 multiMenuPattern->OnTouchEvent(multiMenuTouchEventInfo);
1268 EXPECT_TRUE(multiMenuPattern->IsMultiMenu());
1269 /**
1270 * @tc.steps: step2. select menu touch event
1271 * @tc.expected: menu options nums as expected
1272 */
1273 std::vector<SelectParam> selectParams;
1274 selectParams.emplace_back("content", "icon");
1275 auto selectWrapperNode = MenuView::Create(selectParams, TARGET_ID);
1276 ASSERT_NE(selectWrapperNode, nullptr);
1277 auto selectMenuNode = AceType::DynamicCast<FrameNode>(selectWrapperNode->GetChildAtIndex(0));
1278 ASSERT_NE(selectMenuNode, nullptr);
1279 TouchEventInfo selectMenuEventInfo(MENU_TOUCH_EVENT_TYPE);
1280 auto selectMenuPattern = selectMenuNode->GetPattern<MenuPattern>();
1281 ASSERT_NE(selectMenuPattern, nullptr);
1282 selectMenuPattern->OnTouchEvent(selectMenuEventInfo);
1283 ASSERT_EQ(selectMenuPattern->GetOptions().size(), 1);
1284 /**
1285 * @tc.steps: step3. contextMenu touch event
1286 * @tc.expected: menu type is context
1287 */
1288 auto contextMenuPattern = AceType::MakeRefPtr<MenuPattern>(TARGET_ID, "", MenuType::CONTEXT_MENU);
1289 // excute touch down event
1290 TouchEventInfo contextMenuTouchDownEventInfo(MENU_TOUCH_EVENT_TYPE);
1291 TouchLocationInfo downLocationInfo(TARGET_ID);
1292 Offset touchDownGlobalLocation(1, 1);
1293 downLocationInfo.SetTouchType(TouchType::DOWN);
1294 auto touchDownLocationInfo = downLocationInfo.SetGlobalLocation(touchDownGlobalLocation);
1295 contextMenuTouchDownEventInfo.touches_.emplace_back(touchDownLocationInfo);
1296 contextMenuPattern->OnTouchEvent(contextMenuTouchDownEventInfo);
1297 // excute touch up event
1298 TouchEventInfo contextMenuTouchUpEventInfo(MENU_TOUCH_EVENT_TYPE);
1299 TouchLocationInfo upLocationInfo(TARGET_ID);
1300 Offset touchUpGlobalLocation(3, 3);
1301 upLocationInfo.SetTouchType(TouchType::UP);
1302 auto touchUpLocationInfo = upLocationInfo.SetGlobalLocation(touchUpGlobalLocation);
1303 contextMenuTouchUpEventInfo.touches_.emplace_back(touchUpLocationInfo);
1304 contextMenuPattern->OnTouchEvent(contextMenuTouchUpEventInfo);
1305 EXPECT_TRUE(contextMenuPattern->IsContextMenu());
1306 }
1307
1308 /**
1309 * @tc.name: MenuPatternTestNg023
1310 * @tc.desc: Verify OnKeyEvent.
1311 * @tc.type: FUNC
1312 */
1313 HWTEST_F(MenuTestNg, MenuPatternTestNg023, TestSize.Level1)
1314 {
1315 /**
1316 * @tc.steps: step1. create menu wrapper
1317 * @tc.expected: wrapper node is not null and has 1 child
1318 */
1319 std::vector<SelectParam> selectParams;
1320 selectParams.emplace_back(std::make_pair("MenuItem1", "Icon1"));
1321 auto menuWrapperNode = MenuView::Create(std::move(selectParams), 1);
1322 ASSERT_NE(menuWrapperNode, nullptr);
1323 EXPECT_EQ(menuWrapperNode->GetChildren().size(), 1);
1324 /**
1325 * @tc.steps: step2. excute OnKeyEvent
1326 * @tc.expected: result as expected
1327 */
1328 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
1329 ASSERT_NE(menuNode, nullptr);
1330 auto menuPattern = menuNode->GetPattern<MenuPattern>();
1331 menuPattern->type_ = MenuType::SUB_MENU;
1332 KeyEvent escapeKeyEvent(KeyCode::KEY_ESCAPE, KeyAction::DOWN);
1333 EXPECT_TRUE(menuPattern->OnKeyEvent(escapeKeyEvent));
1334 KeyEvent upKeyEvent(KeyCode::KEY_PLUS, KeyAction::DOWN);
1335 EXPECT_FALSE(menuPattern->OnKeyEvent(upKeyEvent));
1336 }
1337
1338 /**
1339 * @tc.name: MenuPatternTestNg024
1340 * @tc.desc: Verify RemoveParentHoverStyle.
1341 * @tc.type: FUNC
1342 */
1343 HWTEST_F(MenuTestNg, MenuPatternTestNg024, TestSize.Level1)
1344 {
1345 /**
1346 * @tc.steps: step1. create menuItem and menu
1347 * @tc.expected: menuItem not null
1348 */
1349 auto menuPattern = AceType::MakeRefPtr<MenuPattern>(-1, "", MenuType::MENU);
1350 auto menuItemPattern = AceType::MakeRefPtr<MenuItemPattern>();
1351 auto menuItem = AceType::MakeRefPtr<FrameNode>("", -1, menuItemPattern);
1352 ASSERT_NE(menuItem, nullptr);
1353 /**
1354 * @tc.steps: step2. excute RemoveParentHoverStyle
1355 * @tc.expected: item IsSubMenuShowed as expected
1356 */
1357 menuPattern->SetParentMenuItem(menuItem);
1358 menuPattern->RemoveParentHoverStyle();
1359 auto itemPattern = menuItem->GetPattern<MenuItemPattern>();
1360 EXPECT_FALSE(itemPattern->IsSubMenuShowed());
1361 menuPattern->type_ = MenuType::SUB_MENU;
1362 menuPattern->RemoveParentHoverStyle();
1363 EXPECT_FALSE(itemPattern->IsSubMenuShowed());
1364 }
1365
1366 /**
1367 * @tc.name: MenuPatternTestNg025
1368 * @tc.desc: Verify HideMenu.
1369 * @tc.type: FUNC
1370 */
1371 HWTEST_F(MenuTestNg, MenuPatternTestNg025, TestSize.Level1)
1372 {
1373 auto menuPattern = AceType::MakeRefPtr<MenuPattern>(-1, "", MenuType::CONTEXT_MENU);
1374 menuPattern->HideMenu(true);
1375 EXPECT_TRUE(menuPattern->IsContextMenu());
1376 }
1377
1378 /**
1379 * @tc.name: MenuPatternTestNg026
1380 * @tc.desc: Verify HideSubMenu.
1381 * @tc.type: FUNC
1382 */
1383 HWTEST_F(MenuTestNg, MenuPatternTestNg026, TestSize.Level1)
1384 {
1385 /**
1386 * @tc.steps: step1. create menu wrapper
1387 * @tc.expected: wrapper node is not null and has 1 child
1388 */
1389 std::vector<SelectParam> selectParams;
1390 selectParams.emplace_back(std::make_pair("MenuItem1", "Icon1"));
1391 auto menuWrapperNode = MenuView::Create(std::move(selectParams), 1);
1392 ASSERT_NE(menuWrapperNode, nullptr);
1393 EXPECT_EQ(menuWrapperNode->GetChildren().size(), 1);
1394 /**
1395 * @tc.steps: step2. excute HideSubMenu
1396 * @tc.expected: result as expected
1397 */
1398 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
1399 ASSERT_NE(menuNode, nullptr);
1400 auto menuPattern = menuNode->GetPattern<MenuPattern>();
1401 auto menuItemPattern = AceType::MakeRefPtr<MenuItemPattern>();
1402 auto menuItem = AceType::MakeRefPtr<FrameNode>("", -1, menuItemPattern);
1403 menuPattern->SetParentMenuItem(menuItem);
1404 menuPattern->type_ = MenuType::SUB_MENU;
1405 menuPattern->SetShowedSubMenu(menuNode);
1406 menuPattern->HideSubMenu();
1407 auto itemPattern = menuItem->GetPattern<MenuItemPattern>();
1408 EXPECT_FALSE(itemPattern->IsSubMenuShowed());
1409 ASSERT_EQ(menuPattern->GetShowedSubMenu(), nullptr);
1410 }
1411
1412 /**
1413 * @tc.name: MenuPatternTestNg027
1414 * @tc.desc: Verify GetMenuWrapper,GetMainMenuPattern.
1415 * @tc.type: FUNC
1416 */
1417 HWTEST_F(MenuTestNg, MenuPatternTestNg027, TestSize.Level1)
1418 {
1419 /**
1420 * @tc.steps: step1. create menu wrapper and menu
1421 * @tc.expected: wrapper node is not null and has 1 child
1422 */
1423 std::vector<SelectParam> selectParams;
1424 selectParams.emplace_back(std::make_pair("MenuItem1", "Icon1"));
1425 auto menuWrapperNode = MenuView::Create(std::move(selectParams), 1);
1426 ASSERT_NE(menuWrapperNode, nullptr);
1427 EXPECT_EQ(menuWrapperNode->GetChildren().size(), 1);
1428 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
1429 ASSERT_NE(menuNode, nullptr);
1430 /**
1431 * @tc.steps: step2. create child to menu, excute GetMenuWrapper,GetMainMenuPattern,GetInnerMenuCount
1432 * @tc.expected: result as expected
1433 */
1434 auto multiMenu =
1435 FrameNode::CreateFrameNode(V2::MENU_ETS_TAG, 1, AceType::MakeRefPtr<MenuPattern>(1, "", MenuType::MULTI_MENU));
1436 multiMenu->MountToParent(menuNode);
1437 auto multiPattern = multiMenu->GetPattern<MenuPattern>();
1438 ASSERT_NE(multiPattern, nullptr);
1439 ASSERT_NE(multiPattern->GetMenuWrapper(), nullptr);
1440 ASSERT_NE(multiPattern->GetMainMenuPattern(), nullptr);
1441 EXPECT_EQ(multiPattern->GetInnerMenuCount(), 0);
1442 /**
1443 * @tc.steps: step3. create cascade node
1444 * @tc.expected: wrapper node relate to child node pattern is null
1445 */
1446 auto singleMenu =
1447 FrameNode::CreateFrameNode(V2::MENU_ETS_TAG, 1, AceType::MakeRefPtr<MenuPattern>(1, "", MenuType::MULTI_MENU));
1448 auto singleParentMenu =
1449 FrameNode::CreateFrameNode(V2::MENU_ETS_TAG, 2, AceType::MakeRefPtr<MenuPattern>(2, "", MenuType::MENU));
1450 singleMenu->MountToParent(singleParentMenu);
1451 auto singleMenuPattern = singleMenu->GetPattern<MenuPattern>();
1452 ASSERT_EQ(singleMenuPattern->GetMenuWrapper(), nullptr);
1453 }
1454
1455 /**
1456 * @tc.name: MenuLayoutAlgorithmTestNg0
1457 * @tc.desc: Verify positionOffset of Layout.
1458 * @tc.type: FUNC
1459 */
1460 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg0, TestSize.Level1)
1461 {
__anon51d21a700402null1462 std::function<void()> action = [] {};
1463 std::vector<OptionParam> optionParams;
1464 optionParams.emplace_back("MenuItem1", "", action);
1465 optionParams.emplace_back("MenuItem2", "", action);
1466 MenuParam menuParam;
1467 auto menuWrapperNode = MenuView::Create(std::move(optionParams), 1, "", MenuType::MENU, menuParam);
1468 ASSERT_NE(menuWrapperNode, nullptr);
1469 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
1470 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
1471 ASSERT_NE(menuNode, nullptr);
1472 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
1473 ASSERT_NE(property, nullptr);
1474 ASSERT_TRUE(property->GetPositionOffset().has_value());
1475 EXPECT_EQ(property->GetPositionOffset().value(), OffsetF());
1476 RefPtr<MenuLayoutAlgorithm> layoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
1477 RefPtr<GeometryNode> geometryNode = AceType::MakeRefPtr<GeometryNode>();
1478 LayoutWrapperNode layoutWrapper(menuNode, geometryNode, menuNode->GetLayoutProperty());
1479 layoutWrapper.GetLayoutProperty()->UpdateUserDefinedIdealSize(
1480 CalcSize(CalcLength(FULL_SCREEN_WIDTH), CalcLength(FULL_SCREEN_HEIGHT)));
1481 LayoutConstraintF parentLayoutConstraint;
1482 parentLayoutConstraint.maxSize = FULL_SCREEN_SIZE;
1483 parentLayoutConstraint.percentReference = FULL_SCREEN_SIZE;
1484 parentLayoutConstraint.selfIdealSize.SetSize(SizeF(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT));
1485 layoutWrapper.GetLayoutProperty()->UpdateLayoutConstraint(parentLayoutConstraint);
1486 layoutWrapper.GetLayoutProperty()->UpdateContentConstraint();
1487 layoutAlgorithm->Measure(&layoutWrapper);
1488 EXPECT_EQ(layoutAlgorithm->position_, OffsetF());
1489 EXPECT_EQ(layoutAlgorithm->positionOffset_, OffsetF());
1490 EXPECT_EQ(layoutAlgorithm->wrapperSize_, SizeF(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT));
1491 auto menuPattern = menuNode->GetPattern<MenuPattern>();
1492 CHECK_NULL_VOID(menuPattern);
1493 menuPattern->isSelectMenu_ = true;
1494 layoutAlgorithm->Layout(&layoutWrapper);
1495 EXPECT_EQ(geometryNode->GetMarginFrameOffset(), OffsetF(0.0f, 0.0f));
1496 }
1497
1498 /**
1499 * @tc.name: MenuLayoutAlgorithmTestNg1
1500 * @tc.desc: Verify positionOffset of measure.
1501 * @tc.type: FUNC
1502 */
1503 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg1, TestSize.Level1)
1504 {
__anon51d21a700502null1505 std::function<void()> action = [] {};
1506 std::vector<OptionParam> optionParams;
1507 optionParams.emplace_back("MenuItem1", "", action);
1508 optionParams.emplace_back("MenuItem2", "", action);
1509 MenuParam menuParam;
1510 auto menuWrapperNode = MenuView::Create(std::move(optionParams), 1, "", MenuType::CONTEXT_MENU, menuParam);
1511 ASSERT_NE(menuWrapperNode, nullptr);
1512 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
1513 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
1514 ASSERT_NE(menuNode, nullptr);
1515 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
1516 ASSERT_NE(property, nullptr);
1517 ASSERT_TRUE(property->GetPositionOffset().has_value());
1518 EXPECT_EQ(property->GetPositionOffset().value(), OffsetF());
1519 RefPtr<MenuLayoutAlgorithm> layoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
1520 RefPtr<GeometryNode> geometryNode = AceType::MakeRefPtr<GeometryNode>();
1521 LayoutWrapperNode layoutWrapper(menuNode, geometryNode, menuNode->GetLayoutProperty());
1522 layoutWrapper.GetLayoutProperty()->UpdateUserDefinedIdealSize(
1523 CalcSize(CalcLength(FULL_SCREEN_WIDTH), CalcLength(FULL_SCREEN_HEIGHT)));
1524 LayoutConstraintF parentLayoutConstraint;
1525 parentLayoutConstraint.maxSize = FULL_SCREEN_SIZE;
1526 parentLayoutConstraint.percentReference = FULL_SCREEN_SIZE;
1527 parentLayoutConstraint.selfIdealSize.SetSize(SizeF(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT));
1528 layoutWrapper.GetLayoutProperty()->UpdateLayoutConstraint(parentLayoutConstraint);
1529 layoutWrapper.GetLayoutProperty()->UpdateContentConstraint();
1530 layoutAlgorithm->GetPaintProperty(&layoutWrapper)->UpdateEnableArrow(true);
1531 RefPtr<LayoutWrapperNode> wrapperChild =
1532 AceType::MakeRefPtr<LayoutWrapperNode>(menuNode, geometryNode, menuNode->GetLayoutProperty());
1533 layoutWrapper.cachedList_.push_back(wrapperChild);
1534 layoutAlgorithm->Measure(&layoutWrapper);
1535 EXPECT_EQ(layoutAlgorithm->position_, OffsetF());
1536 EXPECT_EQ(layoutAlgorithm->positionOffset_, OffsetF());
1537 EXPECT_EQ(layoutAlgorithm->wrapperSize_, SizeF(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT));
1538 Placement placements[] = { Placement::TOP, Placement::BOTTOM, Placement::RIGHT, Placement::LEFT,
1539 Placement::TOP_LEFT, Placement::BOTTOM_LEFT, Placement::LEFT_BOTTOM, Placement::LEFT_TOP,
1540 Placement::RIGHT_BOTTOM, Placement::RIGHT_TOP, Placement::TOP_RIGHT, Placement::BOTTOM_RIGHT
1541 };
1542 for (Placement placementValue : placements) {
1543 layoutAlgorithm->arrowPlacement_ = placementValue;
1544 layoutAlgorithm->UpdatePropArrowOffset();
1545 }
1546 layoutAlgorithm->propArrowOffset_.value_ = 1.0;
1547 layoutAlgorithm->UpdatePropArrowOffset();
1548 auto menuPattern = menuNode->GetPattern<MenuPattern>();
1549 CHECK_NULL_VOID(menuPattern);
1550 menuPattern->isSelectMenu_ = true;
1551 layoutAlgorithm->targetTag_ = EMPTY_TEXT;
1552 layoutAlgorithm->Layout(&layoutWrapper);
1553 layoutAlgorithm->LayoutArrow(&layoutWrapper);
1554 EXPECT_EQ(geometryNode->GetMarginFrameOffset(), OffsetF(0.0f, 0.0f));
1555 }
1556
1557 /**
1558 * @tc.name: PerformActionTest001
1559 * @tc.desc: MenuItem Accessibility PerformAction test Select and ClearSelection.
1560 * @tc.type: FUNC
1561 */
1562 HWTEST_F(MenuTestNg, PerformActionTest001, TestSize.Level1)
1563 {
1564 /**
1565 * @tc.steps: step1. Create menu, get menu frameNode and pattern, set callback function.
1566 * @tc.expected: FrameNode and pattern is not null, related function is called.
1567 */
1568 MenuItemProperties itemOption;
1569 itemOption.content = "content";
1570 MenuItemModelNG MneuItemModelInstance;
1571 MneuItemModelInstance.Create(itemOption);
1572 auto frameNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
1573 ASSERT_NE(frameNode, nullptr);
1574 auto menuItemPattern = frameNode->GetPattern<MenuItemPattern>();
1575 ASSERT_NE(menuItemPattern, nullptr);
1576 auto menuItemEventHub = frameNode->GetEventHub<MenuItemEventHub>();
1577 ASSERT_NE(menuItemEventHub, nullptr);
1578 auto menuItemAccessibilityProperty = frameNode->GetAccessibilityProperty<MenuItemAccessibilityProperty>();
1579 ASSERT_NE(menuItemAccessibilityProperty, nullptr);
1580 menuItemPattern->SetAccessibilityAction();
1581
1582 /**
1583 * @tc.steps: step2. When selectedChangeEvent onChange and subBuilder is null, call the callback function in
1584 * menuItemAccessibilityProperty.
1585 * @tc.expected: Related function is called.
1586 */
1587 EXPECT_TRUE(menuItemAccessibilityProperty->ActActionSelect());
1588
1589 /**
1590 * @tc.steps: step3. When selectedChangeEvent onChange and subBuilder is not null, call the callback function in
1591 * menuItemAccessibilityProperty.
1592 * @tc.expected: Related function is called.
1593 */
1594 bool isSelected = false;
__anon51d21a700602(bool select) 1595 auto changeEvent = [&isSelected](bool select) { isSelected = select; };
1596 menuItemEventHub->SetSelectedChangeEvent(changeEvent);
1597 menuItemEventHub->SetOnChange(changeEvent);
__anon51d21a700702() 1598 auto subBuilder = []() {};
1599 menuItemPattern->SetSubBuilder(subBuilder);
1600 EXPECT_TRUE(menuItemAccessibilityProperty->ActActionSelect());
1601 }
1602
1603 /**
1604 * @tc.name: PerformActionTest002
1605 * @tc.desc: Menu Accessibility PerformAction test ScrollForward and ScrollBackward.
1606 * @tc.type: FUNC
1607 */
1608 HWTEST_F(MenuTestNg, PerformActionTest002, TestSize.Level1)
1609 {
1610 /**
1611 * @tc.steps: step1. Create menu, get menu frameNode and pattern, set callback function.
1612 * @tc.expected: FrameNode and pattern is not null, related function is called.
1613 */
1614 MenuModelNG model;
1615 model.Create();
1616 auto frameNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
1617 ASSERT_NE(frameNode, nullptr);
1618 auto menuPattern = frameNode->GetPattern<MenuPattern>();
1619 ASSERT_NE(menuPattern, nullptr);
1620 auto menuAccessibilityProperty = frameNode->GetAccessibilityProperty<MenuAccessibilityProperty>();
1621 ASSERT_NE(menuAccessibilityProperty, nullptr);
1622 menuPattern->SetAccessibilityAction();
1623
1624 /**
1625 * @tc.steps: step2. When firstChild is null, call the callback function in menuAccessibilityProperty.
1626 * @tc.expected: Related function is called.
1627 */
1628 EXPECT_TRUE(menuAccessibilityProperty->ActActionScrollForward());
1629 EXPECT_TRUE(menuAccessibilityProperty->ActActionScrollBackward());
1630
1631 /**
1632 * @tc.steps: step3. When firstChild is not null and firstChild tag is SCROLL_ETS_TAG, call the callback function in
1633 * menuAccessibilityProperty.
1634 * @tc.expected: Related function is called.
1635 */
1636 auto scrollPattern = AceType::MakeRefPtr<ScrollPattern>();
1637 ASSERT_NE(scrollPattern, nullptr);
1638 scrollPattern->SetAxis(Axis::VERTICAL);
1639 scrollPattern->scrollableDistance_ = 1.0f;
1640 auto scroll =
1641 FrameNode::CreateFrameNode(V2::SCROLL_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), scrollPattern);
1642 ASSERT_NE(scroll, nullptr);
1643 scroll->MountToParent(frameNode, 0);
1644 scroll->MarkModifyDone();
1645 EXPECT_TRUE(menuAccessibilityProperty->ActActionScrollForward());
1646 EXPECT_TRUE(menuAccessibilityProperty->ActActionScrollBackward());
1647
1648 /**
1649 * @tc.steps: step4. When firstChild is not null and firstChild tag is not SCROLL_ETS_TAG, call the callback
1650 * function in menuAccessibilityProperty.
1651 * @tc.expected: Related function is called.
1652 */
1653 auto textNode = FrameNode::CreateFrameNode(
1654 V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<TextPattern>());
1655 ASSERT_NE(textNode, nullptr);
1656 textNode->MountToParent(frameNode, 0);
1657 textNode->MarkModifyDone();
1658 EXPECT_TRUE(menuAccessibilityProperty->ActActionScrollForward());
1659 EXPECT_TRUE(menuAccessibilityProperty->ActActionScrollBackward());
1660 }
1661
1662 /**
1663 * @tc.name: MenuAccessibilityEventTestNg001
1664 * @tc.desc: Test Click Event for Option of Menu.
1665 */
1666 HWTEST_F(MenuTestNg, MenuAccessibilityEventTestNg001, TestSize.Level1)
1667 {
1668 /**
1669 * @tc.steps: step1. Create Option for Menu.
1670 */
1671 auto frameNode = FrameNode::GetOrCreateFrameNode(V2::OPTION_ETS_TAG,
__anon51d21a700802() 1672 ViewStackProcessor::GetInstance()->ClaimNodeId(), []() { return AceType::MakeRefPtr<OptionPattern>(0); });
1673 ASSERT_NE(frameNode, nullptr);
1674 auto optionPattern = frameNode->GetPattern<OptionPattern>();
1675 ASSERT_NE(optionPattern, nullptr);
1676
1677 /**
1678 * @tc.steps: step2. set callback function.
1679 */
1680 int testIndex = SELECTED_INDEX;
__anon51d21a700902(int index) 1681 auto selectFunc = [optionPattern, testIndex](int index) { optionPattern->index_ = testIndex; };
1682 auto optionEventHub = frameNode->GetEventHub<OptionEventHub>();
1683 optionEventHub->SetOnSelect(selectFunc);
1684 optionPattern->RegisterOnClick();
1685
1686 /**
1687 * @tc.steps: step3. call callback function.
1688 * @tc.expected: index_ is SELECTED_INDEX.
1689 */
1690 auto gestureHub = frameNode->GetOrCreateGestureEventHub();
1691 auto clickEventActuator = gestureHub->clickEventActuator_;
1692 ASSERT_NE(clickEventActuator, nullptr);
1693 auto event = clickEventActuator->GetClickEvent();
1694 ASSERT_NE(event, nullptr);
1695 GestureEvent gestureEvent;
1696 event(gestureEvent);
1697 EXPECT_EQ(optionPattern->index_, SELECTED_INDEX);
1698 }
1699
1700 /**
1701 * @tc.name: DesktopMenuPattern001
1702 * @tc.desc: Test MenuPattern onModifyDone, switch between DesktopMenu and regular menu.
1703 */
1704 HWTEST_F(MenuTestNg, DesktopMenuPattern001, TestSize.Level1)
1705 {
1706 MenuModelNG model;
1707 model.Create();
1708 auto menu1 = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
1709 ASSERT_NE(menu1, nullptr);
1710 model.Create();
1711 auto menu2 = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
1712 auto container = FrameNode::CreateFrameNode("", -1, AceType::MakeRefPtr<MenuPattern>(-1, "", MenuType::MENU));
1713 auto mockScroll = FrameNode::CreateFrameNode("", -1, AceType::MakeRefPtr<Pattern>());
1714 mockScroll->MountToParent(container);
1715 menu1->MountToParent(mockScroll);
1716 menu2->MountToParent(mockScroll);
1717
1718 auto pattern1 = menu1->GetPattern<InnerMenuPattern>();
1719 auto pattern2 = menu2->GetPattern<InnerMenuPattern>();
1720 auto containerPattern = container->GetPattern<MenuPattern>();
1721 containerPattern->OnModifyDone();
1722 pattern1->OnModifyDone();
1723 pattern2->OnModifyDone();
1724 pattern1->BeforeCreateLayoutWrapper();
1725 pattern2->BeforeCreateLayoutWrapper();
1726 EXPECT_EQ(pattern1->type_, MenuType::DESKTOP_MENU);
1727 EXPECT_EQ(pattern2->type_, MenuType::DESKTOP_MENU);
1728 EXPECT_EQ(container->GetRenderContext()->GetBackShadow(), ShadowConfig::NoneShadow);
1729
1730 mockScroll->RemoveChildAtIndex(1);
1731 pattern1->OnModifyDone();
1732 pattern1->BeforeCreateLayoutWrapper();
1733 containerPattern->OnModifyDone();
1734 EXPECT_EQ(pattern1->type_, MenuType::MULTI_MENU);
1735 EXPECT_EQ(container->GetRenderContext()->GetBackShadow(), ShadowConfig::DefaultShadowM);
1736 }
1737
1738 /**
1739 * @tc.name: MenuAccessibilityPropertyIsScrollable001
1740 * @tc.desc: Test IsScrollable of menuAccessibilityProperty.
1741 * @tc.type: FUNC
1742 */
1743 HWTEST_F(MenuTestNg, MenuAccessibilityPropertyIsScrollable001, TestSize.Level1)
1744 {
1745 InitMenuTestNg();
1746
1747 EXPECT_FALSE(menuAccessibilityProperty_->IsScrollable());
1748
1749 auto scrollPattern = AceType::MakeRefPtr<ScrollPattern>();
1750 ASSERT_NE(scrollPattern, nullptr);
1751 scrollPattern->SetAxis(Axis::VERTICAL);
1752 scrollPattern->scrollableDistance_ = 1.0f;
1753 auto scroll =
1754 FrameNode::CreateFrameNode(V2::SCROLL_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), scrollPattern);
1755 ASSERT_NE(scroll, nullptr);
1756 scroll->MountToParent(menuFrameNode_, 0);
1757 scroll->MarkModifyDone();
1758 EXPECT_TRUE(menuAccessibilityProperty_->IsScrollable());
1759 }
1760
1761 /**
1762 * @tc.name: MenuAccessibilityPropertyGetSupportAction001
1763 * @tc.desc: Test GetSupportAction of menuAccessibilityProperty.
1764 * @tc.type: FUNC
1765 */
1766 HWTEST_F(MenuTestNg, MenuAccessibilityPropertyGetSupportAction001, TestSize.Level1)
1767 {
1768 InitMenuTestNg();
1769
1770 auto scrollPattern = AceType::MakeRefPtr<ScrollPattern>();
1771 ASSERT_NE(scrollPattern, nullptr);
1772 scrollPattern->SetAxis(Axis::VERTICAL);
1773 scrollPattern->scrollableDistance_ = 1.0f;
1774 scrollPattern->currentOffset_ = CURRENT_OFFSET;
1775 auto scroll =
1776 FrameNode::CreateFrameNode(V2::SCROLL_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), scrollPattern);
1777 ASSERT_NE(scroll, nullptr);
1778 scroll->MountToParent(menuFrameNode_, 0);
1779
1780 menuAccessibilityProperty_->ResetSupportAction();
1781 std::unordered_set<AceAction> supportAceActions = menuAccessibilityProperty_->GetSupportAction();
1782 uint64_t actions = 0, expectActions = 0;
1783 expectActions |= 1UL << static_cast<uint32_t>(AceAction::ACTION_SCROLL_FORWARD);
1784 expectActions |= 1UL << static_cast<uint32_t>(AceAction::ACTION_SCROLL_BACKWARD);
1785 for (auto action : supportAceActions) {
1786 actions |= 1UL << static_cast<uint32_t>(action);
1787 }
1788 EXPECT_EQ(actions, expectActions);
1789 }
1790
1791 /**
1792 * @tc.name: MenuItemAccessibilityPropertyGetText001
1793 * @tc.desc: Test GetText of menuItem.
1794 * @tc.type: FUNC
1795 */
1796 HWTEST_F(MenuTestNg, MenuItemAccessibilityPropertyGetText001, TestSize.Level1)
1797 {
1798 InitMenuItemTestNg();
1799
1800 EXPECT_EQ(menuItemAccessibilityProperty_->GetText(), EMPTY_TEXT);
1801
1802 auto menuItemLayoutProperty = menuItemFrameNode_->GetLayoutProperty<MenuItemLayoutProperty>();
1803 ASSERT_NE(menuItemLayoutProperty, nullptr);
1804 menuItemLayoutProperty->UpdateContent(MENU_ITEM_TEXT);
1805
1806 EXPECT_EQ(menuItemAccessibilityProperty_->GetText(), MENU_ITEM_TEXT);
1807 }
1808
1809 /**
1810 * @tc.name: MenuItemAccessibilityPropertyIsSelected001
1811 * @tc.desc: Test IsSelected of menuitem.
1812 * @tc.type: FUNC
1813 */
1814 HWTEST_F(MenuTestNg, MenuItemAccessibilityPropertyIsSelected001, TestSize.Level1)
1815 {
1816 InitMenuItemTestNg();
1817
1818 EXPECT_FALSE(menuItemAccessibilityProperty_->IsSelected());
1819
1820 menuItemPattern_->SetSelected(true);
1821 EXPECT_TRUE(menuItemAccessibilityProperty_->IsSelected());
1822 }
1823
1824 /**
1825 * @tc.name: MenuItemAccessibilityPropertyGetSupportAction001
1826 * @tc.desc: Test GetSupportAction of menuitem.
1827 * @tc.type: FUNC
1828 */
1829 HWTEST_F(MenuTestNg, MenuItemAccessibilityPropertyGetSupportAction001, TestSize.Level1)
1830 {
1831 InitMenuItemTestNg();
1832
1833 menuItemAccessibilityProperty_->ResetSupportAction();
1834 std::unordered_set<AceAction> supportAceActions = menuItemAccessibilityProperty_->GetSupportAction();
1835 uint64_t actions = 0, expectActions = 0;
1836 expectActions |= 1UL << static_cast<uint32_t>(AceAction::ACTION_SELECT);
1837 for (auto action : supportAceActions) {
1838 actions |= 1UL << static_cast<uint32_t>(action);
1839 }
1840 EXPECT_EQ(actions, expectActions);
1841 }
1842
1843 /**
1844 * @tc.name: MenuItemGroupAccessibilityPropertyGetText001
1845 * @tc.desc: Test GetText of menuItemGroup.
1846 * @tc.type: FUNC
1847 */
1848 HWTEST_F(MenuTestNg, MenuItemGroupAccessibilityPropertyGetText001, TestSize.Level1)
1849 {
1850 auto frameNode = FrameNode::GetOrCreateFrameNode(V2::MENU_ITEM_GROUP_ETS_TAG,
__anon51d21a700a02() 1851 ViewStackProcessor::GetInstance()->ClaimNodeId(), []() { return AceType::MakeRefPtr<MenuItemGroupPattern>(); });
1852 ASSERT_NE(frameNode, nullptr);
1853
1854 auto menuItemGroupPattern = frameNode->GetPattern<MenuItemGroupPattern>();
1855 ASSERT_NE(menuItemGroupPattern, nullptr);
1856
1857 auto menuItemGroupAccessibilityProperty = frameNode->GetAccessibilityProperty<MenuItemGroupAccessibilityProperty>();
1858 ASSERT_NE(menuItemGroupAccessibilityProperty, nullptr);
1859 EXPECT_EQ(menuItemGroupAccessibilityProperty->GetText(), EMPTY_TEXT);
1860
1861 auto content = FrameNode::CreateFrameNode(
1862 V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<TextPattern>());
1863 ASSERT_NE(content, nullptr);
1864 menuItemGroupPattern->AddHeaderContent(content);
1865
1866 auto textLayoutProperty = content->GetLayoutProperty<TextLayoutProperty>();
1867 ASSERT_NE(textLayoutProperty, nullptr);
1868 textLayoutProperty->UpdateContent(MENU_ITEM_GROUP_TEXT);
1869 EXPECT_EQ(menuItemGroupAccessibilityProperty->GetText(), MENU_ITEM_GROUP_TEXT);
1870 }
1871
1872 /**
1873 * @tc.name: MenuItemLayoutPropertyTestNg001
1874 * @tc.desc: Verify GetStartIcon.
1875 * @tc.type: FUNC
1876 */
1877 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg001, TestSize.Level1)
1878 {
1879 MenuItemLayoutProperty property;
1880 EXPECT_FALSE(property.GetStartIcon().has_value());
1881 property.UpdateStartIcon("test.png");
1882 ASSERT_TRUE(property.GetStartIcon().has_value());
1883 EXPECT_EQ(property.GetStartIcon().value(), "test.png");
1884 }
1885
1886 /**
1887 * @tc.name: MenuItemLayoutPropertyTestNg002
1888 * @tc.desc: Verify GetContent.
1889 * @tc.type: FUNC
1890 */
1891 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg002, TestSize.Level1)
1892 {
1893 MenuItemLayoutProperty property;
1894 EXPECT_FALSE(property.GetContent().has_value());
1895 property.UpdateContent("content");
1896 ASSERT_TRUE(property.GetContent().has_value());
1897 EXPECT_EQ(property.GetContent().value(), "content");
1898 }
1899
1900 /**
1901 * @tc.name: MenuItemLayoutPropertyTestNg003
1902 * @tc.desc: Verify GetEndIcon.
1903 * @tc.type: FUNC
1904 */
1905 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg003, TestSize.Level1)
1906 {
1907 MenuItemLayoutProperty property;
1908 EXPECT_FALSE(property.GetEndIcon().has_value());
1909 property.UpdateEndIcon("test.png");
1910 ASSERT_TRUE(property.GetEndIcon().has_value());
1911 EXPECT_EQ(property.GetEndIcon().value(), "test.png");
1912 }
1913
1914 /**
1915 * @tc.name: MenuItemLayoutPropertyTestNg004
1916 * @tc.desc: Verify GetLabel.
1917 * @tc.type: FUNC
1918 */
1919 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg004, TestSize.Level1)
1920 {
1921 MenuItemLayoutProperty property;
1922 EXPECT_FALSE(property.GetLabel().has_value());
1923 property.UpdateLabel("label");
1924 ASSERT_TRUE(property.GetLabel().has_value());
1925 EXPECT_EQ(property.GetLabel().value(), "label");
1926 }
1927
1928 /**
1929 * @tc.name: MenuItemLayoutPropertyTestNg005
1930 * @tc.desc: Verify GetSelectIcon.
1931 * @tc.type: FUNC
1932 */
1933 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg005, TestSize.Level1)
1934 {
1935 MenuItemLayoutProperty property;
1936 EXPECT_FALSE(property.GetSelectIcon().has_value());
1937 property.UpdateSelectIcon(true);
1938 ASSERT_TRUE(property.GetSelectIcon().has_value());
1939 EXPECT_TRUE(property.GetSelectIcon().value());
1940 }
1941
1942 /**
1943 * @tc.name: MenuItemLayoutPropertyTestNg006
1944 * @tc.desc: Verify GetSelectIconSrc.
1945 * @tc.type: FUNC
1946 */
1947 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg006, TestSize.Level1)
1948 {
1949 MenuItemLayoutProperty property;
1950 EXPECT_FALSE(property.GetSelectIconSrc().has_value());
1951 property.UpdateSelectIconSrc("test.png");
1952 ASSERT_TRUE(property.GetSelectIconSrc().has_value());
1953 EXPECT_EQ(property.GetSelectIconSrc().value(), "test.png");
1954 }
1955
1956 /**
1957 * @tc.name: MenuItemLayoutPropertyTestNg007
1958 * @tc.desc: Verify GetFontSize.
1959 * @tc.type: FUNC
1960 */
1961 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg007, TestSize.Level1)
1962 {
1963 MenuItemLayoutProperty property;
1964 EXPECT_FALSE(property.GetFontSize().has_value());
1965 property.UpdateFontSize(Dimension(25.0f));
1966 ASSERT_TRUE(property.GetFontSize().has_value());
1967 EXPECT_EQ(property.GetFontSize().value(), Dimension(25.0f));
1968 }
1969
1970 /**
1971 * @tc.name: MenuItemLayoutPropertyTestNg008
1972 * @tc.desc: Verify GetFontColor.
1973 * @tc.type: FUNC
1974 */
1975 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg008, TestSize.Level1)
1976 {
1977 MenuItemLayoutProperty property;
1978 EXPECT_FALSE(property.GetFontColor().has_value());
1979 property.UpdateFontColor(Color::RED);
1980 ASSERT_TRUE(property.GetFontColor().has_value());
1981 EXPECT_EQ(property.GetFontColor().value(), Color::RED);
1982 }
1983
1984 /**
1985 * @tc.name: MenuItemLayoutPropertyTestNg009
1986 * @tc.desc: Verify GetFontWeight.
1987 * @tc.type: FUNC
1988 */
1989 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg009, TestSize.Level1)
1990 {
1991 MenuItemLayoutProperty property;
1992 EXPECT_FALSE(property.GetFontWeight().has_value());
1993 property.UpdateFontWeight(FontWeight::BOLD);
1994 ASSERT_TRUE(property.GetFontWeight().has_value());
1995 EXPECT_EQ(property.GetFontWeight().value(), FontWeight::BOLD);
1996 }
1997
1998 /**
1999 * @tc.name: MenuItemLayoutPropertyTestNg0010
2000 * @tc.desc: Verify GetLabelFontSize.
2001 * @tc.type: FUNC
2002 */
2003 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg0010, TestSize.Level1)
2004 {
2005 MenuItemLayoutProperty property;
2006 EXPECT_FALSE(property.GetLabelFontSize().has_value());
2007 property.UpdateLabelFontSize(Dimension(25.0f));
2008 ASSERT_TRUE(property.GetLabelFontSize().has_value());
2009 EXPECT_EQ(property.GetLabelFontSize().value(), Dimension(25.0f));
2010 }
2011
2012 /**
2013 * @tc.name: MenuItemLayoutPropertyTestNg011
2014 * @tc.desc: Verify GetLabelFontColor.
2015 * @tc.type: FUNC
2016 */
2017 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg011, TestSize.Level1)
2018 {
2019 MenuItemLayoutProperty property;
2020 EXPECT_FALSE(property.GetLabelFontColor().has_value());
2021 property.UpdateLabelFontColor(Color::RED);
2022 ASSERT_TRUE(property.GetLabelFontColor().has_value());
2023 EXPECT_EQ(property.GetLabelFontColor().value(), Color::RED);
2024 }
2025
2026 /**
2027 * @tc.name: MenuItemLayoutPropertyTestNg012
2028 * @tc.desc: Verify GetLabelFontWeight.
2029 * @tc.type: FUNC
2030 */
2031 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg012, TestSize.Level1)
2032 {
2033 MenuItemLayoutProperty property;
2034 EXPECT_FALSE(property.GetLabelFontWeight().has_value());
2035 property.UpdateLabelFontWeight(FontWeight::BOLD);
2036 ASSERT_TRUE(property.GetLabelFontWeight().has_value());
2037 EXPECT_EQ(property.GetLabelFontWeight().value(), FontWeight::BOLD);
2038 }
2039
2040 /**
2041 * @tc.name: MenuItemLayoutPropertyTestNg013
2042 * @tc.desc: Verify Reset.
2043 * @tc.type: FUNC
2044 */
2045 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg013, TestSize.Level1)
2046 {
2047 MenuItemLayoutProperty property;
2048 property.UpdateStartIcon("test.png");
2049 property.UpdateContent("content");
2050 property.UpdateEndIcon("test.png");
2051 property.UpdateLabel("label");
2052 property.UpdateSelectIcon(true);
2053 property.UpdateSelectIconSrc("test.png");
2054 property.UpdateFontSize(Dimension(25.0f));
2055 property.UpdateFontColor(Color::RED);
2056 property.UpdateFontWeight(FontWeight::BOLD);
2057 property.UpdateLabelFontSize(Dimension(25.0f));
2058 property.UpdateLabelFontColor(Color::RED);
2059 property.UpdateLabelFontWeight(FontWeight::BOLD);
2060 EXPECT_TRUE(property.GetStartIcon().has_value());
2061 EXPECT_TRUE(property.GetContent().has_value());
2062 EXPECT_TRUE(property.GetLabel().has_value());
2063 EXPECT_TRUE(property.GetEndIcon().has_value());
2064 EXPECT_TRUE(property.GetSelectIcon().has_value());
2065 EXPECT_TRUE(property.GetSelectIconSrc().has_value());
2066 EXPECT_TRUE(property.GetFontSize().has_value());
2067 EXPECT_TRUE(property.GetFontColor().has_value());
2068 EXPECT_TRUE(property.GetFontWeight().has_value());
2069 EXPECT_TRUE(property.GetLabelFontSize().has_value());
2070 EXPECT_TRUE(property.GetLabelFontColor().has_value());
2071 EXPECT_TRUE(property.GetLabelFontWeight().has_value());
2072 property.Reset();
2073 EXPECT_FALSE(property.GetStartIcon().has_value());
2074 EXPECT_FALSE(property.GetContent().has_value());
2075 EXPECT_FALSE(property.GetLabel().has_value());
2076 EXPECT_FALSE(property.GetEndIcon().has_value());
2077 EXPECT_FALSE(property.GetSelectIcon().has_value());
2078 EXPECT_FALSE(property.GetSelectIconSrc().has_value());
2079 EXPECT_FALSE(property.GetFontSize().has_value());
2080 EXPECT_FALSE(property.GetFontColor().has_value());
2081 EXPECT_FALSE(property.GetFontWeight().has_value());
2082 EXPECT_FALSE(property.GetLabelFontSize().has_value());
2083 EXPECT_FALSE(property.GetLabelFontColor().has_value());
2084 EXPECT_FALSE(property.GetLabelFontWeight().has_value());
2085 }
2086
2087 /**
2088 * @tc.name: MenuItemLayoutPropertyTestNg014
2089 * @tc.desc: Verify Clone.
2090 * @tc.type: FUNC
2091 */
2092 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg014, TestSize.Level1)
2093 {
2094 MenuItemLayoutProperty property;
2095 property.UpdateStartIcon("start.png");
2096 property.UpdateContent("content");
2097 property.UpdateEndIcon("end.png");
2098 property.UpdateLabel("label");
2099 property.UpdateSelectIcon(true);
2100 property.UpdateSelectIconSrc("select.png");
2101 property.UpdateFontSize(Dimension(25.0f));
2102 property.UpdateFontColor(Color::RED);
2103 property.UpdateFontWeight(FontWeight::BOLD);
2104 property.UpdateLabelFontSize(Dimension(35.0f));
2105 property.UpdateLabelFontColor(Color::BLUE);
2106 property.UpdateLabelFontWeight(FontWeight::LIGHTER);
2107
2108 auto cloneProperty = AceType::DynamicCast<MenuItemLayoutProperty>(property.Clone());
2109 ASSERT_NE(cloneProperty, nullptr);
2110 EXPECT_EQ(property.GetStartIcon().value(), cloneProperty->GetStartIcon().value());
2111 EXPECT_EQ(property.GetContent().value(), cloneProperty->GetContent().value());
2112 EXPECT_EQ(property.GetEndIcon().value(), cloneProperty->GetEndIcon().value());
2113 EXPECT_EQ(property.GetLabel().value(), cloneProperty->GetLabel().value());
2114 EXPECT_EQ(property.GetSelectIcon().value(), cloneProperty->GetSelectIcon().value());
2115 EXPECT_EQ(property.GetSelectIconSrc().value(), cloneProperty->GetSelectIconSrc().value());
2116 EXPECT_EQ(property.GetFontSize().value(), cloneProperty->GetFontSize().value());
2117 EXPECT_EQ(property.GetFontColor().value(), cloneProperty->GetFontColor().value());
2118 EXPECT_EQ(property.GetFontWeight().value(), cloneProperty->GetFontWeight().value());
2119 EXPECT_EQ(property.GetLabelFontSize().value(), cloneProperty->GetLabelFontSize().value());
2120 EXPECT_EQ(property.GetLabelFontColor().value(), cloneProperty->GetLabelFontColor().value());
2121 EXPECT_EQ(property.GetLabelFontWeight().value(), cloneProperty->GetLabelFontWeight().value());
2122 }
2123
2124 /**
2125 * @tc.name: MenuItemLayoutPropertyTestNg015
2126 * @tc.desc: Verify ToJsonValue.
2127 * @tc.type: FUNC
2128 */
2129 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg015, TestSize.Level1)
2130 {
2131 MenuItemLayoutProperty property;
2132 property.UpdateStartIcon("start.png");
2133 property.UpdateContent("content");
2134 property.UpdateEndIcon("end.png");
2135 property.UpdateLabel("label");
2136 property.UpdateSelectIcon(true);
2137 property.UpdateSelectIconSrc("select.png");
2138 property.UpdateFontSize(Dimension(25.0f));
2139 property.UpdateFontColor(Color::RED);
2140 property.UpdateFontWeight(FontWeight::BOLD);
2141 property.UpdateLabelFontSize(Dimension(35.0f));
2142 property.UpdateLabelFontColor(Color::BLUE);
2143 property.UpdateLabelFontWeight(FontWeight::LIGHTER);
2144
2145 auto json = JsonUtil::Create(true);
2146 property.ToJsonValue(json);
2147 auto labelFontJson = json->GetObject("labelFont");
2148 auto contentFontJson = json->GetObject("contentFont");
2149 EXPECT_EQ(json->GetString("startIcon"), "start.png");
2150 EXPECT_EQ(json->GetString("content"), "content");
2151 EXPECT_EQ(json->GetString("endIcon"), "end.png");
2152 EXPECT_EQ(json->GetString("labelInfo"), "label");
2153 EXPECT_EQ(json->GetString("selectIcon"), "select.png");
2154 EXPECT_EQ(contentFontJson->GetString("size"), Dimension(25.0f).ToString());
2155 EXPECT_EQ(json->GetString("contentFontColor"), Color::RED.ColorToString());
2156 EXPECT_EQ(contentFontJson->GetString("weight"), V2::ConvertWrapFontWeightToStirng(FontWeight::BOLD));
2157 EXPECT_EQ(labelFontJson->GetString("size"), Dimension(35.0f).ToString());
2158 EXPECT_EQ(json->GetString("labelFontColor"), Color::BLUE.ColorToString());
2159 EXPECT_EQ(labelFontJson->GetString("weight"), V2::ConvertWrapFontWeightToStirng(FontWeight::LIGHTER));
2160 }
2161
2162 /**
2163 * @tc.name: MenuItemLayoutPropertyTestNg016
2164 * @tc.desc: Verify ToJsonValue.
2165 * @tc.type: FUNC
2166 */
2167 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg016, TestSize.Level1)
2168 {
2169 MenuItemLayoutProperty property;
2170 property.UpdateSelectIcon(false);
2171 property.UpdateSelectIconSrc("select.png");
2172
2173 auto json = JsonUtil::Create(true);
2174 property.ToJsonValue(json);
2175 EXPECT_EQ(json->GetString("selectIcon"), "false");
2176 }
2177
2178 /**
2179 * @tc.name: MenuItemLayoutPropertyTestNg017
2180 * @tc.desc: Verify ToJsonValue.
2181 * @tc.type: FUNC
2182 */
2183 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg017, TestSize.Level1)
2184 {
2185 MenuItemLayoutProperty property;
2186 property.UpdateSelectIcon(true);
2187
2188 auto json = JsonUtil::Create(true);
2189 property.ToJsonValue(json);
2190 EXPECT_EQ(json->GetString("selectIcon"), "true");
2191 }
2192
2193 /**
2194 * @tc.name: MenuItemLayoutPropertyTestNg018
2195 * @tc.desc: Verify ToJsonValue.
2196 * @tc.type: FUNC
2197 */
2198 HWTEST_F(MenuTestNg, MenuItemLayoutPropertyTestNg018, TestSize.Level1)
2199 {
2200 MenuItemModelNG MneuItemModelInstance;
2201 MenuItemProperties itemOption;
2202 MneuItemModelInstance.Create(itemOption);
2203 MneuItemModelInstance.SetSelectIcon(true);
2204 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
2205 ASSERT_NE(itemNode, nullptr);
2206 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
2207 ASSERT_NE(itemProperty, nullptr);
2208
2209 auto json = JsonUtil::Create(true);
2210 itemProperty->ToJsonValue(json);
2211 EXPECT_EQ(json->GetString("selectIcon"), "true");
2212 }
2213
2214 /**
2215 * @tc.name: MenuItemPatternTestNgAddSelectIcon001
2216 * @tc.desc: Verify AddSelectIcon.
2217 * @tc.type: FUNC
2218 */
2219 HWTEST_F(MenuTestNg, MenuItemPatternTestNgAddSelectIcon001, TestSize.Level1)
2220 {
2221 MenuItemModelNG MenuItemModelInstance;
2222 MenuItemProperties itemOption;
2223 MenuItemModelInstance.Create(itemOption);
2224 MenuItemModelInstance.SetSelectIcon(false);
2225 MenuItemModelInstance.SetSelectIconSrc("selectIcon.png");
2226 MenuItemModelInstance.SetSelected(true);
2227 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
2228 ASSERT_NE(itemNode, nullptr);
2229 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
2230 ASSERT_NE(itemPattern, nullptr);
2231 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
2232 ASSERT_NE(itemProperty, nullptr);
2233
2234 ASSERT_EQ(itemNode->GetChildren().size(), 2u);
2235 auto leftRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(0));
2236 EXPECT_EQ(leftRow->GetChildren().size(), 0u);
2237 auto rightRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(1));
2238 EXPECT_EQ(rightRow->GetChildren().size(), 0u);
2239
2240 // call AddSelectIcon
2241 itemPattern->OnModifyDone();
2242
2243 EXPECT_EQ(leftRow->GetChildren().size(), 0u);
2244 EXPECT_EQ(rightRow->GetChildren().size(), 0u);
2245 }
2246
2247 /**
2248 * @tc.name: MenuItemPatternTestNgAddSelectIcon002
2249 * @tc.desc: Verify AddSelectIcon.
2250 * @tc.type: FUNC
2251 */
2252 HWTEST_F(MenuTestNg, MenuItemPatternTestNgAddSelectIcon002, TestSize.Level1)
2253 {
2254 MenuItemModelNG MenuItemModelInstance;
2255 auto themeManager = AceType::MakeRefPtr<MockThemeManager>();
2256 MockPipelineBase::GetCurrent()->SetThemeManager(themeManager);
2257 EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<SelectTheme>()));
2258 MenuItemProperties itemOption;
2259 MenuItemModelInstance.Create(itemOption);
2260 MenuItemModelInstance.SetSelectIcon(true);
2261 MenuItemModelInstance.SetSelectIconSrc("");
2262 MenuItemModelInstance.SetSelected(true);
2263 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
2264 ASSERT_NE(itemNode, nullptr);
2265 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
2266 ASSERT_NE(itemPattern, nullptr);
2267 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
2268 ASSERT_NE(itemProperty, nullptr);
2269
2270 ASSERT_EQ(itemNode->GetChildren().size(), 2u);
2271 auto leftRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(0));
2272 EXPECT_EQ(leftRow->GetChildren().size(), 0u);
2273 auto rightRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(1));
2274 EXPECT_EQ(rightRow->GetChildren().size(), 0u);
2275
2276 EXPECT_CALL(*themeManager, GetTheme(_))
2277 .WillOnce(Return(AceType::MakeRefPtr<IconTheme>()))
2278 .WillOnce(Return(AceType::MakeRefPtr<SelectTheme>()));
2279 // call AddSelectIcon
2280 itemPattern->OnModifyDone();
2281
2282 EXPECT_EQ(rightRow->GetChildren().size(), 0u);
2283 ASSERT_EQ(leftRow->GetChildren().size(), 1u);
2284 auto selectIconNode = AceType::DynamicCast<FrameNode>(leftRow->GetChildAtIndex(0));
2285 ASSERT_NE(selectIconNode, nullptr);
2286 EXPECT_EQ(selectIconNode->GetTag(), V2::IMAGE_ETS_TAG);
2287 auto imagePattern = selectIconNode->GetPattern<ImagePattern>();
2288 ASSERT_NE(imagePattern, nullptr);
2289 auto imageLayoutProperty = selectIconNode->GetLayoutProperty<ImageLayoutProperty>();
2290 ASSERT_NE(imageLayoutProperty, nullptr);
2291 auto sourceInfo = imageLayoutProperty->GetImageSourceInfo();
2292 EXPECT_TRUE(sourceInfo.has_value());
2293 }
2294
2295 /**
2296 * @tc.name: MenuItemPatternTestNgAddSelectIcon003
2297 * @tc.desc: Verify AddSelectIcon.
2298 * @tc.type: FUNC
2299 */
2300 HWTEST_F(MenuTestNg, MenuItemPatternTestNgAddSelectIcon003, TestSize.Level1)
2301 {
2302 MenuItemModelNG MenuItemModelInstance;
2303 auto themeManager = AceType::MakeRefPtr<MockThemeManager>();
2304 MockPipelineBase::GetCurrent()->SetThemeManager(themeManager);
2305 EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<SelectTheme>()));
2306 MenuItemProperties itemOption;
2307 MenuItemModelInstance.Create(itemOption);
2308 MenuItemModelInstance.SetSelectIcon(true);
2309 MenuItemModelInstance.SetSelectIconSrc("selectIcon.png");
2310 MenuItemModelInstance.SetSelected(false);
2311 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
2312 ASSERT_NE(itemNode, nullptr);
2313 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
2314 ASSERT_NE(itemPattern, nullptr);
2315 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
2316 ASSERT_NE(itemProperty, nullptr);
2317
2318 ASSERT_EQ(itemNode->GetChildren().size(), 2u);
2319 auto leftRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(0));
2320 EXPECT_EQ(leftRow->GetChildren().size(), 0u);
2321 auto rightRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(1));
2322 EXPECT_EQ(rightRow->GetChildren().size(), 0u);
2323
2324 EXPECT_CALL(*themeManager, GetTheme(_))
2325 .WillOnce(Return(AceType::MakeRefPtr<IconTheme>()))
2326 .WillOnce(Return(AceType::MakeRefPtr<SelectTheme>()));
2327 // call AddSelectIcon
2328 itemPattern->OnModifyDone();
2329
2330 EXPECT_EQ(rightRow->GetChildren().size(), 0u);
2331 ASSERT_EQ(leftRow->GetChildren().size(), 1u);
2332 auto selectIconNode = AceType::DynamicCast<FrameNode>(leftRow->GetChildAtIndex(0));
2333 ASSERT_NE(selectIconNode, nullptr);
2334 EXPECT_EQ(selectIconNode->GetTag(), V2::IMAGE_ETS_TAG);
2335 auto imagePattern = selectIconNode->GetPattern<ImagePattern>();
2336 ASSERT_NE(imagePattern, nullptr);
2337 auto imageLayoutProperty = selectIconNode->GetLayoutProperty<ImageLayoutProperty>();
2338 ASSERT_NE(imageLayoutProperty, nullptr);
2339 auto sourceInfo = imageLayoutProperty->GetImageSourceInfo();
2340 ASSERT_TRUE(sourceInfo.has_value());
2341 EXPECT_EQ(sourceInfo.value().GetSrc(), "selectIcon.png");
2342 }
2343
2344 /**
2345 * @tc.name: MenuItemPatternTestNgAddSelectIcon004
2346 * @tc.desc: Verify AddSelectIcon.
2347 * @tc.type: FUNC
2348 */
2349 HWTEST_F(MenuTestNg, MenuItemPatternTestNgAddSelectIcon004, TestSize.Level1)
2350 {
2351 MenuItemModelNG MenuItemModelInstance;
2352 auto themeManager = AceType::MakeRefPtr<MockThemeManager>();
2353 MockPipelineBase::GetCurrent()->SetThemeManager(themeManager);
2354 EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<SelectTheme>()));
2355 MenuItemProperties itemOption;
2356 MenuItemModelInstance.Create(itemOption);
2357 MenuItemModelInstance.SetSelectIcon(true);
2358 MenuItemModelInstance.SetSelectIconSrc("selectIcon.png");
2359 MenuItemModelInstance.SetSelected(false);
2360 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
2361 ASSERT_NE(itemNode, nullptr);
2362 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
2363 ASSERT_NE(itemPattern, nullptr);
2364 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
2365 ASSERT_NE(itemProperty, nullptr);
2366
2367 ASSERT_EQ(itemNode->GetChildren().size(), 2u);
2368 auto leftRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(0));
2369 EXPECT_EQ(leftRow->GetChildren().size(), 0u);
2370 auto rightRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(1));
2371 EXPECT_EQ(rightRow->GetChildren().size(), 0u);
2372 EXPECT_CALL(*themeManager, GetTheme(_))
2373 .WillOnce(Return(AceType::MakeRefPtr<IconTheme>()))
2374 .WillOnce(Return(AceType::MakeRefPtr<SelectTheme>()));
2375 // call AddSelectIcon
2376 itemPattern->OnModifyDone();
2377
2378 EXPECT_EQ(rightRow->GetChildren().size(), 0u);
2379 ASSERT_EQ(leftRow->GetChildren().size(), 1u);
2380 auto selectIconNode = AceType::DynamicCast<FrameNode>(leftRow->GetChildAtIndex(0));
2381 ASSERT_NE(selectIconNode, nullptr);
2382 EXPECT_EQ(selectIconNode->GetTag(), V2::IMAGE_ETS_TAG);
2383
2384 itemProperty->UpdateSelectIcon(false);
2385 EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<SelectTheme>()));
2386 // call AddSelectIcon
2387 itemPattern->OnModifyDone();
2388 ASSERT_EQ(leftRow->GetChildren().size(), 0u);
2389 }
2390
2391 /**
2392 * @tc.name: MenuItemPatternTestNgUpdateIcon001
2393 * @tc.desc: Verify UpdateIcon.
2394 * @tc.type: FUNC
2395 */
2396 HWTEST_F(MenuTestNg, MenuItemPatternTestNgUpdateIcon001, TestSize.Level1)
2397 {
2398 MenuItemModelNG MenuItemModelInstance;
2399 MenuItemProperties itemOption;
2400 itemOption.startIcon = "startIcon.png";
2401 MenuItemModelInstance.Create(itemOption);
2402 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
2403 ASSERT_NE(itemNode, nullptr);
2404 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
2405 ASSERT_NE(itemPattern, nullptr);
2406 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
2407 ASSERT_NE(itemProperty, nullptr);
2408
2409 ASSERT_EQ(itemNode->GetChildren().size(), 2u);
2410 auto leftRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(0));
2411 auto rightRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(1));
2412
2413 // call UpdateIcon
2414 itemPattern->OnModifyDone();
2415
2416 EXPECT_EQ(rightRow->GetChildren().size(), 0u);
2417 ASSERT_EQ(leftRow->GetChildren().size(), 1u);
2418 auto startIconNode = AceType::DynamicCast<FrameNode>(leftRow->GetChildAtIndex(0));
2419 ASSERT_NE(startIconNode, nullptr);
2420 EXPECT_EQ(startIconNode->GetTag(), V2::IMAGE_ETS_TAG);
2421 auto imagePattern = startIconNode->GetPattern<ImagePattern>();
2422 ASSERT_NE(imagePattern, nullptr);
2423 auto imageLayoutProperty = startIconNode->GetLayoutProperty<ImageLayoutProperty>();
2424 ASSERT_NE(imageLayoutProperty, nullptr);
2425 auto sourceInfo = imageLayoutProperty->GetImageSourceInfo();
2426 ASSERT_TRUE(sourceInfo.has_value());
2427 EXPECT_EQ(sourceInfo.value().GetSrc(), "startIcon.png");
2428 }
2429
2430 /**
2431 * @tc.name: MenuItemPatternTestNgUpdateIcon002
2432 * @tc.desc: Verify UpdateIcon.
2433 * @tc.type: FUNC
2434 */
2435 HWTEST_F(MenuTestNg, MenuItemPatternTestNgUpdateIcon002, TestSize.Level1)
2436 {
2437 MenuItemModelNG MenuItemModelInstance;
2438 MenuItemProperties itemOption;
2439 itemOption.endIcon = "endIcon.png";
2440 MenuItemModelInstance.Create(itemOption);
2441 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
2442 ASSERT_NE(itemNode, nullptr);
2443 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
2444 ASSERT_NE(itemPattern, nullptr);
2445 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
2446 ASSERT_NE(itemProperty, nullptr);
2447
2448 ASSERT_EQ(itemNode->GetChildren().size(), 2u);
2449 auto leftRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(0));
2450 auto rightRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(1));
2451
2452 // call UpdateIcon
2453 itemPattern->OnModifyDone();
2454
2455 EXPECT_EQ(leftRow->GetChildren().size(), 0u);
2456 ASSERT_EQ(rightRow->GetChildren().size(), 1u);
2457 auto endIconNode = AceType::DynamicCast<FrameNode>(rightRow->GetChildAtIndex(0));
2458 ASSERT_NE(endIconNode, nullptr);
2459 EXPECT_EQ(endIconNode->GetTag(), V2::IMAGE_ETS_TAG);
2460 auto imagePattern = endIconNode->GetPattern<ImagePattern>();
2461 ASSERT_NE(imagePattern, nullptr);
2462 auto imageLayoutProperty = endIconNode->GetLayoutProperty<ImageLayoutProperty>();
2463 ASSERT_NE(imageLayoutProperty, nullptr);
2464 auto sourceInfo = imageLayoutProperty->GetImageSourceInfo();
2465 ASSERT_TRUE(sourceInfo.has_value());
2466 EXPECT_EQ(sourceInfo.value().GetSrc(), "endIcon.png");
2467
2468 itemProperty->UpdateEndIcon("endIcon2.png");
2469 // call UpdateIcon
2470 itemPattern->OnModifyDone();
2471 imagePattern = endIconNode->GetPattern<ImagePattern>();
2472 ASSERT_NE(imagePattern, nullptr);
2473 imageLayoutProperty = endIconNode->GetLayoutProperty<ImageLayoutProperty>();
2474 ASSERT_NE(imageLayoutProperty, nullptr);
2475 sourceInfo = imageLayoutProperty->GetImageSourceInfo();
2476 ASSERT_TRUE(sourceInfo.has_value());
2477 EXPECT_EQ(sourceInfo.value().GetSrc(), "endIcon2.png");
2478 }
2479
2480 /**
2481 * @tc.name: MenuItemPatternTestNgUpdateText001
2482 * @tc.desc: Verify UpdateText.
2483 * @tc.type: FUNC
2484 */
2485 HWTEST_F(MenuTestNg, MenuItemPatternTestNgUpdateText001, TestSize.Level1)
2486 {
2487 MenuItemModelNG MenuItemModelInstance;
2488 MenuItemProperties itemOption;
2489 itemOption.content = "content";
2490 MenuItemModelInstance.Create(itemOption);
2491 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
2492 ASSERT_NE(itemNode, nullptr);
2493 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
2494 ASSERT_NE(itemPattern, nullptr);
2495 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
2496 ASSERT_NE(itemProperty, nullptr);
2497
2498 ASSERT_EQ(itemNode->GetChildren().size(), 2u);
2499 auto leftRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(0));
2500 auto rightRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(1));
2501
2502 // call UpdateText
2503 itemPattern->OnModifyDone();
2504
2505 EXPECT_EQ(rightRow->GetChildren().size(), 0u);
2506 ASSERT_EQ(leftRow->GetChildren().size(), 1u);
2507 auto contentNode = AceType::DynamicCast<FrameNode>(leftRow->GetChildAtIndex(0));
2508 ASSERT_NE(contentNode, nullptr);
2509 EXPECT_EQ(contentNode->GetTag(), V2::TEXT_ETS_TAG);
2510 auto textLayoutProperty = contentNode->GetLayoutProperty<TextLayoutProperty>();
2511 ASSERT_NE(textLayoutProperty, nullptr);
2512 auto content = textLayoutProperty->GetContent();
2513 ASSERT_TRUE(content.has_value());
2514 EXPECT_EQ(content.value(), "content");
2515 }
2516
2517 /**
2518 * @tc.name: MenuItemPatternTestNgUpdateText002
2519 * @tc.desc: Verify UpdateText.
2520 * @tc.type: FUNC
2521 */
2522 HWTEST_F(MenuTestNg, MenuItemPatternTestNgUpdateText002, TestSize.Level1)
2523 {
2524 MenuItemModelNG MenuItemModelInstance;
2525 MenuItemProperties itemOption;
2526 itemOption.labelInfo = "label";
2527 MenuItemModelInstance.Create(itemOption);
2528 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
2529 ASSERT_NE(itemNode, nullptr);
2530 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
2531 ASSERT_NE(itemPattern, nullptr);
2532 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
2533 ASSERT_NE(itemProperty, nullptr);
2534
2535 ASSERT_EQ(itemNode->GetChildren().size(), 2u);
2536 auto leftRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(0));
2537 auto rightRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(1));
2538
2539 // call UpdateText
2540 itemPattern->OnModifyDone();
2541
2542 EXPECT_EQ(leftRow->GetChildren().size(), 0u);
2543 ASSERT_EQ(rightRow->GetChildren().size(), 1u);
2544 auto labelNode = AceType::DynamicCast<FrameNode>(rightRow->GetChildAtIndex(0));
2545 ASSERT_NE(labelNode, nullptr);
2546 EXPECT_EQ(labelNode->GetTag(), V2::TEXT_ETS_TAG);
2547 auto textLayoutProperty = labelNode->GetLayoutProperty<TextLayoutProperty>();
2548 ASSERT_NE(textLayoutProperty, nullptr);
2549 auto content = textLayoutProperty->GetContent();
2550 ASSERT_TRUE(content.has_value());
2551 EXPECT_EQ(content.value(), "label");
2552 }
2553
2554 /**
2555 * @tc.name: MenuItemPatternTestNgUpdateText003
2556 * @tc.desc: Verify update text when item is disabled.
2557 * @tc.type: FUNC
2558 */
2559 HWTEST_F(MenuTestNg, MenuItemPatternTestNgUpdateText003, TestSize.Level1)
2560 {
2561 // mock theme
2562 auto selectTheme = MockPipelineBase::GetCurrent()->GetTheme<SelectTheme>();
2563 selectTheme->SetDisabledMenuFontColor(ITEM_DISABLED_COLOR);
2564
2565 // create menu item
2566 MenuItemModelNG MenuItemModelInstance;
2567 MenuItemProperties itemOption;
2568 itemOption.startIcon = "startIcon.png";
2569 itemOption.content = "item content";
2570 MenuItemModelInstance.Create(itemOption);
2571 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
2572 ASSERT_NE(itemNode, nullptr);
2573 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
2574 ASSERT_NE(itemPattern, nullptr);
2575 auto itemEventHub = itemNode->GetEventHub<MenuItemEventHub>();
2576 ASSERT_NE(itemEventHub, nullptr);
2577 itemEventHub->SetEnabled(false);
2578 // update item
2579 itemPattern->OnModifyDone();
2580
2581 EXPECT_FALSE(itemEventHub->IsEnabled());
2582 ASSERT_EQ(itemNode->GetChildren().size(), 2u);
2583 auto leftRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(0));
2584 EXPECT_EQ(leftRow->GetChildren().size(), 2u);
2585 auto startIconNode = AceType::DynamicCast<FrameNode>(leftRow->GetChildAtIndex(0));
2586 ASSERT_NE(startIconNode, nullptr);
2587 EXPECT_EQ(startIconNode->GetTag(), V2::IMAGE_ETS_TAG);
2588 auto imageLayoutProperty = startIconNode->GetLayoutProperty<ImageLayoutProperty>();
2589 ASSERT_NE(imageLayoutProperty, nullptr);
2590 auto sourceInfo = imageLayoutProperty->GetImageSourceInfo();
2591 ASSERT_TRUE(sourceInfo.has_value());
2592 EXPECT_EQ(sourceInfo.value().GetSrc(), "startIcon.png");
2593
2594 auto contentNode = AceType::DynamicCast<FrameNode>(leftRow->GetChildAtIndex(1));
2595 ASSERT_NE(contentNode, nullptr);
2596 EXPECT_EQ(contentNode->GetTag(), V2::TEXT_ETS_TAG);
2597 auto textLayoutProperty = contentNode->GetLayoutProperty<TextLayoutProperty>();
2598 ASSERT_NE(textLayoutProperty, nullptr);
2599 auto content = textLayoutProperty->GetContent();
2600 ASSERT_TRUE(content.has_value());
2601 EXPECT_EQ(content.value(), "item content");
2602 auto textRenderContext = contentNode->GetRenderContext();
2603 EXPECT_EQ(textRenderContext->GetForegroundColor(), ITEM_DISABLED_COLOR);
2604 }
2605
2606 /**
2607 * @tc.name: MenuItemPatternTestEvent001
2608 * @tc.desc: Test Click Event
2609 * @tc.type: FUNC
2610 */
2611 HWTEST_F(MenuTestNg, MenuItemPatternTestEvent001, TestSize.Level1)
2612 {
2613 /**
2614 * @tc.steps: step1. Create menuItem.
2615 */
2616 MenuItemModelNG MenuItemModelInstance;
2617 MenuItemProperties itemOption;
2618 MenuItemModelInstance.Create(itemOption);
2619 MenuItemModelInstance.SetSelected(true);
2620 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
2621 ASSERT_NE(itemNode, nullptr);
2622
2623 /**
2624 * @tc.steps: step2. set callback function.
2625 */
2626 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
2627 ASSERT_NE(itemPattern, nullptr);
2628 auto gestureHub = itemNode->GetOrCreateGestureEventHub();
2629 ASSERT_NE(gestureHub, nullptr);
2630 auto clickEventActuator = gestureHub->clickEventActuator_;
2631 ASSERT_NE(clickEventActuator, nullptr);
2632 auto event = clickEventActuator->GetClickEvent();
2633 ASSERT_NE(event, nullptr);
2634
2635 /**
2636 * @tc.steps: step3. call callback function.
2637 * @tc.expected: isSelected_ is false.
2638 */
2639 GestureEvent gestureEvent;
2640 event(gestureEvent);
2641 EXPECT_FALSE(itemPattern->isSelected_);
2642 }
2643
2644 /**
2645 * @tc.name: CustomMenuItemPattern001
2646 * @tc.desc: Test CustomMenuItem creation
2647 * @tc.type: FUNC
2648 */
2649 HWTEST_F(MenuTestNg, CustomMenuItemPattern001, TestSize.Level1)
2650 {
2651 MenuItemModelNG model;
2652 auto customNode = FrameNode::CreateFrameNode("", -1, AceType::MakeRefPtr<Pattern>());
2653 model.Create(customNode);
2654 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
2655 ASSERT_TRUE(itemNode);
2656 auto pattern = itemNode->GetPattern<CustomMenuItemPattern>();
2657 ASSERT_TRUE(pattern);
2658 ASSERT_TRUE(itemNode->GetEventHub<EventHub>());
2659 auto touch = itemNode->GetOrCreateGestureEventHub()->touchEventActuator_;
2660 ASSERT_TRUE(touch);
2661 ASSERT_FALSE(touch->touchEvents_.empty());
2662 }
2663
2664 /**
2665 * @tc.name: MenuItemPatternTestNg001
2666 * @tc.desc: Verify GetMenuWrapper.
2667 * @tc.type: FUNC
2668 */
2669 HWTEST_F(MenuTestNg, MenuItemPatternTestNg001, TestSize.Level1)
2670 {
2671 /**
2672 * @tc.steps: step1. prepare wrapperNode, menuNode, itemNode
2673 * @tc.expected: itemPattern is not null
2674 */
2675 auto wrapperNode =
2676 FrameNode::CreateFrameNode(V2::MENU_WRAPPER_ETS_TAG, 1, AceType::MakeRefPtr<MenuWrapperPattern>(1));
2677 auto mainMenu =
2678 FrameNode::CreateFrameNode(V2::MENU_ETS_TAG, 2, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::MENU));
2679 auto menuItemNode = FrameNode::CreateFrameNode(V2::MENU_ITEM_ETS_TAG, 3, AceType::MakeRefPtr<MenuItemPattern>());
2680 auto menuItemPattern = menuItemNode->GetPattern<MenuItemPattern>();
2681 ASSERT_NE(menuItemPattern, nullptr);
2682 /**
2683 * @tc.steps: step2. excute GetMenuWrapper
2684 * @tc.expected: result as expected
2685 */
2686 ASSERT_EQ(menuItemPattern->GetMenuWrapper(), nullptr);
2687 menuItemNode->MountToParent(wrapperNode);
2688 ASSERT_NE(menuItemPattern->GetMenuWrapper(), nullptr);
2689 wrapperNode->RemoveChildAtIndex(0);
2690 EXPECT_EQ(wrapperNode->GetChildren().size(), 0);
2691 menuItemNode->MountToParent(mainMenu);
2692 mainMenu->MountToParent(wrapperNode);
2693 ASSERT_NE(menuItemPattern->GetMenuWrapper(), nullptr);
2694 }
2695
2696 /**
2697 * @tc.name: MenuItemPatternTestNg002
2698 * @tc.desc: Verify ShowSubMenu, CloseMenu.
2699 * @tc.type: FUNC
2700 */
2701 HWTEST_F(MenuTestNg, MenuItemPatternTestNg002, TestSize.Level1)
2702 {
2703 /**
2704 * @tc.steps: step1. create cascade menu condition
2705 * @tc.expected: wrapper and child pattern is not null
2706 */
__anon51d21a700b02() 2707 std::function<void()> buildFun = []() {
2708 MenuModelNG MenuModelInstance;
2709 MenuModelInstance.Create();
2710 };
2711 auto wrapperNode =
2712 FrameNode::CreateFrameNode(V2::MENU_WRAPPER_ETS_TAG, 1, AceType::MakeRefPtr<MenuWrapperPattern>(1));
2713 auto mainMenu =
2714 FrameNode::CreateFrameNode(V2::MENU_ETS_TAG, 2, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::MENU));
2715 auto subMenu = FrameNode::CreateFrameNode(
2716 V2::MENU_ETS_TAG, 3, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::SUB_MENU));
2717 auto menuItemNode = FrameNode::CreateFrameNode(V2::MENU_ITEM_ETS_TAG, 4, AceType::MakeRefPtr<MenuItemPattern>());
2718 auto subMenuParent = FrameNode::CreateFrameNode(V2::MENU_ITEM_ETS_TAG, 5, AceType::MakeRefPtr<MenuItemPattern>());
2719 menuItemNode->MountToParent(mainMenu);
2720 mainMenu->MountToParent(wrapperNode);
2721 subMenu->MountToParent(wrapperNode);
2722 auto menuItemPattern = menuItemNode->GetPattern<MenuItemPattern>();
2723 ASSERT_NE(menuItemPattern, nullptr);
2724 menuItemPattern->ShowSubMenu();
2725 menuItemPattern->SetSubBuilder(buildFun);
2726 menuItemPattern->SetIsSubMenuShowed(false);
2727 auto mainMenuPattern = mainMenu->GetPattern<MenuPattern>();
2728 ASSERT_NE(mainMenuPattern, nullptr);
2729 mainMenuPattern->SetShowedSubMenu(subMenu);
2730 auto subMenuPattern = subMenu->GetPattern<MenuPattern>();
2731 ASSERT_NE(subMenuPattern, nullptr);
2732 subMenuPattern->SetParentMenuItem(subMenuParent);
2733 /**
2734 * @tc.steps: step2. prepare wrapperNode, menuNode, itemNode
2735 * @tc.expected: itemPattern is not null
2736 */
2737 menuItemPattern->ShowSubMenu();
2738 menuItemPattern->CloseMenu();
2739 EXPECT_EQ(wrapperNode->GetChildren().size(), 2);
2740 }
2741
2742 /**
2743 * @tc.name: MenuItemPatternTestNg003
2744 * @tc.desc: Verify RegisterOnClick.
2745 * @tc.type: FUNC
2746 */
2747 HWTEST_F(MenuTestNg, MenuItemPatternTestNg003, TestSize.Level1)
2748 {
2749 /**
2750 * @tc.steps: step1. create item node and eventhub
2751 * @tc.expected: pattern and eventhub is not null
2752 */
2753 auto menuItemNode = FrameNode::CreateFrameNode(V2::MENU_ITEM_ETS_TAG, 4, AceType::MakeRefPtr<MenuItemPattern>());
2754 auto menuItemPattern = menuItemNode->GetPattern<MenuItemPattern>();
2755 ASSERT_NE(menuItemPattern, nullptr);
2756 auto menuItemEventHub = menuItemNode->GetEventHub<MenuItemEventHub>();
2757 ASSERT_NE(menuItemEventHub, nullptr);
2758 /**
2759 * @tc.steps: step2. excute RegisterOnClick
2760 * @tc.expected: result as expected
2761 */
2762 bool isSelected = false;
__anon51d21a700c02(bool select) 2763 auto changeEvent = [&isSelected](bool select) { isSelected = !select; };
2764 menuItemEventHub->SetSelectedChangeEvent(changeEvent);
2765 menuItemEventHub->SetOnChange(changeEvent);
2766 menuItemPattern->RegisterOnClick();
2767 // trigger click
2768 auto gestureHub = menuItemNode->GetOrCreateGestureEventHub();
2769 auto clickEventActuator = gestureHub->clickEventActuator_;
2770 ASSERT_NE(clickEventActuator, nullptr);
2771 auto event = clickEventActuator->GetClickEvent();
2772 ASSERT_NE(event, nullptr);
2773 GestureEvent gestureEvent;
2774 event(gestureEvent);
2775 EXPECT_TRUE(isSelected);
2776 // update item pattern subbuilder, click item
__anon51d21a700d02null2777 std::function<void()> buildFun = [] {};
2778 menuItemPattern->SetSubBuilder(buildFun);
2779 menuItemPattern->RegisterOnClick();
2780 event(gestureEvent);
2781 EXPECT_FALSE(isSelected);
2782 }
2783
2784 /**
2785 * @tc.name: MenuItemPatternTestNg004
2786 * @tc.desc: Verify OnTouch.
2787 * @tc.type: FUNC
2788 */
2789 HWTEST_F(MenuTestNg, MenuItemPatternTestNg004, TestSize.Level1)
2790 {
2791 /**
2792 * @tc.steps: step1. create item node
2793 * @tc.expected: pattern is not null
2794 */
2795 auto menuItemNode = FrameNode::CreateFrameNode(V2::MENU_ITEM_ETS_TAG, 4, AceType::MakeRefPtr<MenuItemPattern>());
2796 auto menuItemPattern = menuItemNode->GetPattern<MenuItemPattern>();
2797 ASSERT_NE(menuItemPattern, nullptr);
2798 /**
2799 * @tc.steps: step2. excute OnTouch
2800 * @tc.expected: result as expected
2801 */
2802 // excute touch down event
2803 TouchEventInfo itemTouchDownEventInfo(MENU_TOUCH_EVENT_TYPE);
2804 TouchLocationInfo downLocationInfo(TARGET_ID);
2805 Offset touchDownGlobalLocation(1, 1);
2806 downLocationInfo.SetTouchType(TouchType::DOWN);
2807 auto touchDownLocationInfo = downLocationInfo.SetGlobalLocation(touchDownGlobalLocation);
2808 itemTouchDownEventInfo.touches_.emplace_back(touchDownLocationInfo);
2809 menuItemPattern->OnTouch(itemTouchDownEventInfo);
2810 EXPECT_EQ(itemTouchDownEventInfo.touches_.size(), 1);
2811 // excute touch up event
2812 TouchEventInfo itemTouchUpEventInfo(MENU_TOUCH_EVENT_TYPE);
2813 TouchLocationInfo upLocationInfo(TARGET_ID);
2814 Offset touchUpGlobalLocation(3, 3);
2815 upLocationInfo.SetTouchType(TouchType::UP);
2816 auto touchUpLocationInfo = upLocationInfo.SetGlobalLocation(touchUpGlobalLocation);
2817 itemTouchUpEventInfo.touches_.emplace_back(touchUpLocationInfo);
2818 menuItemPattern->OnTouch(itemTouchUpEventInfo);
2819 EXPECT_EQ(itemTouchUpEventInfo.touches_.size(), 1);
2820 // excute touch move event
2821 upLocationInfo.SetTouchType(TouchType::MOVE);
2822 touchUpLocationInfo = upLocationInfo.SetGlobalLocation(touchUpGlobalLocation);
2823 itemTouchUpEventInfo.touches_.clear();
2824 itemTouchUpEventInfo.touches_.emplace_back(touchUpLocationInfo);
2825 menuItemPattern->OnTouch(itemTouchUpEventInfo);
2826 EXPECT_EQ(itemTouchUpEventInfo.touches_.size(), 1);
2827 }
2828
2829 /**
2830 * @tc.name: MenuItemPatternTestNg005
2831 * @tc.desc: Verify OnHover.
2832 * @tc.type: FUNC
2833 */
2834 HWTEST_F(MenuTestNg, MenuItemPatternTestNg005, TestSize.Level1)
2835 {
2836 auto menuItemNode = FrameNode::CreateFrameNode(V2::MENU_ITEM_ETS_TAG, 4, AceType::MakeRefPtr<MenuItemPattern>());
2837 auto menuItemPattern = menuItemNode->GetPattern<MenuItemPattern>();
2838 ASSERT_NE(menuItemPattern, nullptr);
2839 menuItemPattern->OnHover(true);
2840 menuItemPattern->OnHover(false);
2841 EXPECT_EQ(menuItemPattern->GetBgBlendColor(), Color::TRANSPARENT);
2842 }
2843
2844 /**
2845 * @tc.name: MenuItemPatternTestNg006
2846 * @tc.desc: Verify OnKeyEvent,PlayBgColorAnimation.
2847 * @tc.type: FUNC
2848 */
2849 HWTEST_F(MenuTestNg, MenuItemPatternTestNg006, TestSize.Level1)
2850 {
2851 /**
2852 * @tc.steps: step1. create item node
2853 * @tc.expected: pattern is not null
2854 */
2855 auto menuItemNode = FrameNode::CreateFrameNode(V2::MENU_ITEM_ETS_TAG, 4, AceType::MakeRefPtr<MenuItemPattern>());
2856 auto menuItemPattern = menuItemNode->GetPattern<MenuItemPattern>();
2857 ASSERT_NE(menuItemPattern, nullptr);
2858 /**
2859 * @tc.steps: step2. update event type, excute OnKeyEvent
2860 * @tc.expected: result as expected
2861 */
2862 // use longPressEvent
2863 KeyEvent longPressEvent(KeyCode::KEY_ESCAPE, KeyAction::LONG_PRESS);
2864 EXPECT_FALSE(menuItemPattern->OnKeyEvent(longPressEvent));
2865 // use enterEvent
2866 KeyEvent enterEvent(KeyCode::KEY_ENTER, KeyAction::DOWN);
2867 EXPECT_TRUE(menuItemPattern->OnKeyEvent(enterEvent));
2868 // use rightEvent
2869 KeyEvent rightEvent(KeyCode::KEY_DPAD_RIGHT, KeyAction::DOWN);
__anon51d21a700e02() 2870 std::function<void()> buildFun = []() {};
2871 menuItemPattern->SetSubBuilder(buildFun);
2872 menuItemPattern->SetIsSubMenuShowed(false);
2873 EXPECT_TRUE(menuItemPattern->OnKeyEvent(rightEvent));
2874 // use fnEvent
2875 KeyEvent fnEvent(KeyCode::KEY_FN, KeyAction::DOWN);
2876 menuItemPattern->PlayBgColorAnimation(false);
2877 menuItemPattern->PlayBgColorAnimation(true);
2878 EXPECT_FALSE(menuItemPattern->OnKeyEvent(fnEvent));
2879 }
2880
2881 /**
2882 * @tc.name: MenuItemPatternTestNg007
2883 * @tc.desc: Verify RegisterWrapperMouseEvent.
2884 * @tc.type: FUNC
2885 */
2886 HWTEST_F(MenuTestNg, MenuItemPatternTestNg007, TestSize.Level1)
2887 {
2888 auto wrapperNode =
2889 FrameNode::CreateFrameNode(V2::MENU_WRAPPER_ETS_TAG, 1, AceType::MakeRefPtr<MenuWrapperPattern>(1));
2890 auto menuItemNode = FrameNode::CreateFrameNode(V2::MENU_ITEM_ETS_TAG, 4, AceType::MakeRefPtr<MenuItemPattern>());
2891 auto mainMenu =
2892 FrameNode::CreateFrameNode(V2::MENU_ETS_TAG, 2, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::MENU));
2893 menuItemNode->MountToParent(mainMenu);
2894 mainMenu->MountToParent(wrapperNode);
2895 auto menuItemPattern = menuItemNode->GetPattern<MenuItemPattern>();
2896 ASSERT_NE(menuItemPattern, nullptr);
2897 menuItemPattern->RegisterWrapperMouseEvent();
2898 EXPECT_EQ(wrapperNode->GetChildren().size(), 1);
2899 }
2900
2901 /**
2902 * @tc.name: MenuItemPatternTestNg008
2903 * @tc.desc: Verify AddSelfHoverRegion, IsInHoverRegions.
2904 * @tc.type: FUNC
2905 */
2906 HWTEST_F(MenuTestNg, MenuItemPatternTestNg008, TestSize.Level1)
2907 {
2908 auto menuItemNode = FrameNode::CreateFrameNode(V2::MENU_ITEM_ETS_TAG, 4, AceType::MakeRefPtr<MenuItemPattern>());
2909 auto mainMenu =
2910 FrameNode::CreateFrameNode(V2::MENU_ETS_TAG, 2, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::MENU));
2911 auto menuItemPattern = menuItemNode->GetPattern<MenuItemPattern>();
2912 ASSERT_NE(menuItemPattern, nullptr);
2913 mainMenu->GetGeometryNode()->SetFrameSize(SizeF(100, 100));
2914 menuItemPattern->AddSelfHoverRegion(mainMenu);
2915 EXPECT_EQ(menuItemPattern->hoverRegions_.size(), 1);
2916 EXPECT_TRUE(menuItemPattern->IsInHoverRegions(40, 40));
2917 EXPECT_FALSE(menuItemPattern->IsInHoverRegions(200, 200));
2918 }
2919
2920 /**
2921 * @tc.name: MenuItemPatternTestNg009
2922 * @tc.desc: Verify AddSelfHoverRegion.
2923 * @tc.type: FUNC
2924 */
2925 HWTEST_F(MenuTestNg, MenuItemPatternTestNg009, TestSize.Level1)
2926 {
2927 auto menuItemNode = FrameNode::CreateFrameNode(V2::MENU_ITEM_ETS_TAG, 4, AceType::MakeRefPtr<MenuItemPattern>());
2928 auto mainMenu =
2929 FrameNode::CreateFrameNode(V2::MENU_ETS_TAG, 2, AceType::MakeRefPtr<MenuPattern>(1, TEXT_TAG, MenuType::MENU));
2930 auto menuItemPattern = menuItemNode->GetPattern<MenuItemPattern>();
2931 ASSERT_NE(menuItemPattern, nullptr);
2932 mainMenu->GetGeometryNode()->SetFrameSize(SizeF(100, 100));
2933 auto position = menuItemPattern->GetSubMenuPostion(mainMenu);
2934 EXPECT_EQ(position, OffsetF(100, 0));
2935 }
2936
2937 /**
2938 * @tc.name: MenuItemViewTestNgCreate001
2939 * @tc.desc: Verify Create.
2940 * @tc.type: FUNC
2941 */
2942 HWTEST_F(MenuTestNg, MenuItemViewTestNgCreate001, TestSize.Level1)
2943 {
2944 MenuItemModelNG MneuItemModelInstance;
2945 MenuItemProperties itemOption;
2946 MneuItemModelInstance.Create(itemOption);
2947 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
2948 ASSERT_NE(itemNode, nullptr);
2949 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
2950 ASSERT_NE(itemPattern, nullptr);
2951 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
2952 ASSERT_NE(itemProperty, nullptr);
2953
2954 ASSERT_EQ(itemNode->GetChildren().size(), 2);
2955 auto leftRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(0));
2956 EXPECT_EQ(leftRow->GetChildren().size(), 0);
2957 auto rightRow = AceType::DynamicCast<FrameNode>(itemNode->GetChildAtIndex(1));
2958 EXPECT_EQ(rightRow->GetChildren().size(), 0);
2959
2960 ASSERT_TRUE(itemProperty->GetStartIcon().has_value());
2961 EXPECT_EQ(itemProperty->GetStartIcon().value(), "");
2962 ASSERT_TRUE(itemProperty->GetEndIcon().has_value());
2963 EXPECT_EQ(itemProperty->GetEndIcon().value(), "");
2964 ASSERT_TRUE(itemProperty->GetContent().has_value());
2965 EXPECT_EQ(itemProperty->GetContent().value(), "");
2966 ASSERT_TRUE(itemProperty->GetLabel().has_value());
2967 EXPECT_EQ(itemProperty->GetLabel().value(), "");
2968 }
2969
2970 /**
2971 * @tc.name: MenuItemViewTestNgCreate002
2972 * @tc.desc: Verify Create.
2973 * @tc.type: FUNC
2974 */
2975 HWTEST_F(MenuTestNg, MenuItemViewTestNgCreate002, TestSize.Level1)
2976 {
2977 MenuItemModelNG MneuItemModelInstance;
2978 MenuItemProperties itemOption;
2979 itemOption.content = "content";
2980 itemOption.startIcon = "startIcon";
2981 itemOption.endIcon = "endIcon";
2982 itemOption.labelInfo = "label";
2983 MneuItemModelInstance.Create(itemOption);
2984 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
2985 ASSERT_NE(itemNode, nullptr);
2986 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
2987 ASSERT_NE(itemPattern, nullptr);
2988 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
2989 ASSERT_NE(itemProperty, nullptr);
2990
2991 ASSERT_TRUE(itemProperty->GetStartIcon().has_value());
2992 EXPECT_EQ(itemProperty->GetStartIcon().value(), "startIcon");
2993 ASSERT_TRUE(itemProperty->GetEndIcon().has_value());
2994 EXPECT_EQ(itemProperty->GetEndIcon().value(), "endIcon");
2995 ASSERT_TRUE(itemProperty->GetContent().has_value());
2996 EXPECT_EQ(itemProperty->GetContent().value(), "content");
2997 ASSERT_TRUE(itemProperty->GetLabel().has_value());
2998 EXPECT_EQ(itemProperty->GetLabel().value(), "label");
2999 }
3000
3001 /**
3002 * @tc.name: MenuItemViewTestNgSetSelectIcon001
3003 * @tc.desc: Verify SetSelectIcon.
3004 * @tc.type: FUNC
3005 */
3006 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetSelectIcon001, TestSize.Level1)
3007 {
3008 MenuItemModelNG MneuItemModelInstance;
3009 MenuItemProperties itemOption;
3010 MneuItemModelInstance.Create(itemOption);
3011 MneuItemModelInstance.SetSelectIcon(true);
3012 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3013 ASSERT_NE(itemNode, nullptr);
3014 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3015 ASSERT_NE(itemProperty, nullptr);
3016 EXPECT_EQ(itemProperty->GetSelectIcon().value_or(false), true);
3017 }
3018
3019 /**
3020 * @tc.name: MenuItemViewTestNgSetSelectIcon002
3021 * @tc.desc: Verify SetSelectIcon.
3022 * @tc.type: FUNC
3023 */
3024 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetSelectIcon002, TestSize.Level1)
3025 {
3026 MenuItemModelNG MneuItemModelInstance;
3027 MenuItemProperties itemOption;
3028 MneuItemModelInstance.SetSelectIcon(true);
3029 MneuItemModelInstance.Create(itemOption);
3030 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3031 ASSERT_NE(itemNode, nullptr);
3032 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3033 ASSERT_NE(itemProperty, nullptr);
3034 EXPECT_FALSE(itemProperty->GetSelectIcon().has_value());
3035 }
3036
3037 /**
3038 * @tc.name: MenuItemViewTestNgSetSelectIconSrc001
3039 * @tc.desc: Verify SetSelectIconSrc.
3040 * @tc.type: FUNC
3041 */
3042 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetSelectIconSrc001, TestSize.Level1)
3043 {
3044 MenuItemModelNG MneuItemModelInstance;
3045 MenuItemProperties itemOption;
3046 MneuItemModelInstance.Create(itemOption);
3047 MneuItemModelInstance.SetSelectIconSrc("selectIcon.png");
3048 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3049 ASSERT_NE(itemNode, nullptr);
3050 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
3051 ASSERT_NE(itemPattern, nullptr);
3052 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3053 ASSERT_NE(itemProperty, nullptr);
3054
3055 EXPECT_EQ(itemProperty->GetSelectIconSrc().value_or(""), "selectIcon.png");
3056 }
3057
3058 /**
3059 * @tc.name: MenuItemViewTestNgSetSelectIconSrc002
3060 * @tc.desc: Verify SetSelectIconSrc.
3061 * @tc.type: FUNC
3062 */
3063 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetSelectIconSrc002, TestSize.Level1)
3064 {
3065 MenuItemModelNG MneuItemModelInstance;
3066 MenuItemProperties itemOption;
3067 MneuItemModelInstance.SetSelectIconSrc("selectIcon.png");
3068 MneuItemModelInstance.Create(itemOption);
3069 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3070 ASSERT_NE(itemNode, nullptr);
3071 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3072 ASSERT_NE(itemProperty, nullptr);
3073 EXPECT_FALSE(itemProperty->GetSelectIconSrc().has_value());
3074 }
3075
3076 /**
3077 * @tc.name: MenuItemViewTestNgSetFontSize001
3078 * @tc.desc: Verify SetFontSize.
3079 * @tc.type: FUNC
3080 */
3081 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetFontSize001, TestSize.Level1)
3082 {
3083 MenuItemModelNG MneuItemModelInstance;
3084 MenuItemProperties itemOption;
3085 MneuItemModelInstance.Create(itemOption);
3086 MneuItemModelInstance.SetFontSize(Dimension());
3087 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3088 ASSERT_NE(itemNode, nullptr);
3089 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
3090 ASSERT_NE(itemPattern, nullptr);
3091 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3092 ASSERT_NE(itemProperty, nullptr);
3093
3094 EXPECT_FALSE(itemProperty->GetFontSize().has_value());
3095 }
3096
3097 /**
3098 * @tc.name: MenuItemViewTestNgSetFontSize002
3099 * @tc.desc: Verify SetFontSize.
3100 * @tc.type: FUNC
3101 */
3102 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetFontSize002, TestSize.Level1)
3103 {
3104 MenuItemModelNG MneuItemModelInstance;
3105 MenuItemProperties itemOption;
3106 MneuItemModelInstance.Create(itemOption);
3107 MneuItemModelInstance.SetFontSize(Dimension(40.0));
3108 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3109 ASSERT_NE(itemNode, nullptr);
3110 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
3111 ASSERT_NE(itemPattern, nullptr);
3112 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3113 ASSERT_NE(itemProperty, nullptr);
3114
3115 ASSERT_TRUE(itemProperty->GetFontSize().has_value());
3116 EXPECT_EQ(itemProperty->GetFontSize().value(), Dimension(40.0));
3117 }
3118
3119 /**
3120 * @tc.name: MenuItemViewTestNgSetFontSize003
3121 * @tc.desc: Verify SetFontSize.
3122 * @tc.type: FUNC
3123 */
3124 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetFontSize003, TestSize.Level1)
3125 {
3126 MenuItemModelNG MneuItemModelInstance;
3127 MenuItemProperties itemOption;
3128 MneuItemModelInstance.SetFontSize(Dimension(40.0));
3129 MneuItemModelInstance.Create(itemOption);
3130 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3131 ASSERT_NE(itemNode, nullptr);
3132 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3133 ASSERT_NE(itemProperty, nullptr);
3134 ASSERT_FALSE(itemProperty->GetFontSize().has_value());
3135 }
3136
3137 /**
3138 * @tc.name: MenuItemViewTestNgSetFontWeight001
3139 * @tc.desc: Verify SetFontWeight.
3140 * @tc.type: FUNC
3141 */
3142 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetFontWeight001, TestSize.Level1)
3143 {
3144 MenuItemModelNG MneuItemModelInstance;
3145 MenuItemProperties itemOption;
3146 MneuItemModelInstance.Create(itemOption);
3147 MneuItemModelInstance.SetFontWeight(FontWeight::BOLD);
3148 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3149 ASSERT_NE(itemNode, nullptr);
3150 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
3151 ASSERT_NE(itemPattern, nullptr);
3152 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3153 ASSERT_NE(itemProperty, nullptr);
3154
3155 ASSERT_TRUE(itemProperty->GetFontWeight().has_value());
3156 EXPECT_EQ(itemProperty->GetFontWeight().value(), FontWeight::BOLD);
3157 }
3158
3159 /**
3160 * @tc.name: MenuItemViewTestNgSetFontWeight002
3161 * @tc.desc: Verify SetFontWeight.
3162 * @tc.type: FUNC
3163 */
3164 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetFontWeight002, TestSize.Level1)
3165 {
3166 MenuItemModelNG MneuItemModelInstance;
3167 MenuItemProperties itemOption;
3168 MneuItemModelInstance.SetFontWeight(FontWeight::BOLD);
3169 MneuItemModelInstance.Create(itemOption);
3170 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3171 ASSERT_NE(itemNode, nullptr);
3172 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3173 ASSERT_NE(itemProperty, nullptr);
3174 ASSERT_FALSE(itemProperty->GetFontWeight().has_value());
3175 }
3176
3177 /**
3178 * @tc.name: MenuItemViewTestNgSetFontColor001
3179 * @tc.desc: Verify SetFontColor.
3180 * @tc.type: FUNC
3181 */
3182 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetFontColor001, TestSize.Level1)
3183 {
3184 MenuItemModelNG MneuItemModelInstance;
3185 MenuItemProperties itemOption;
3186 MneuItemModelInstance.Create(itemOption);
3187 MneuItemModelInstance.SetFontColor(Color::RED);
3188 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3189 ASSERT_NE(itemNode, nullptr);
3190 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
3191 ASSERT_NE(itemPattern, nullptr);
3192 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3193 ASSERT_NE(itemProperty, nullptr);
3194
3195 ASSERT_TRUE(itemProperty->GetFontColor().has_value());
3196 EXPECT_EQ(itemProperty->GetFontColor().value(), Color::RED);
3197 }
3198
3199 /**
3200 * @tc.name: MenuItemViewTestNgSetFontColor002
3201 * @tc.desc: Verify SetFontColor.
3202 * @tc.type: FUNC
3203 */
3204 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetFontColor002, TestSize.Level1)
3205 {
3206 MenuItemModelNG MneuItemModelInstance;
3207 MenuItemProperties itemOption;
3208 MneuItemModelInstance.SetFontColor(Color::RED);
3209 MneuItemModelInstance.Create(itemOption);
3210 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3211 ASSERT_NE(itemNode, nullptr);
3212 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3213 ASSERT_NE(itemProperty, nullptr);
3214 ASSERT_FALSE(itemProperty->GetFontColor().has_value());
3215 }
3216
3217 /**
3218 * @tc.name: MenuItemViewTestNgSetFontColor003
3219 * @tc.desc: Verify SetFontColor.
3220 * @tc.type: FUNC
3221 */
3222 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetFontColor003, TestSize.Level1)
3223 {
3224 MenuItemModelNG MneuItemModelInstance;
3225 MenuItemProperties itemOption;
3226 MneuItemModelInstance.Create(itemOption);
3227 MneuItemModelInstance.SetFontColor(Color::RED);
3228 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3229 ASSERT_NE(itemNode, nullptr);
3230 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
3231 ASSERT_NE(itemPattern, nullptr);
3232 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3233 ASSERT_NE(itemProperty, nullptr);
3234
3235 ASSERT_TRUE(itemProperty->GetFontColor().has_value());
3236 EXPECT_EQ(itemProperty->GetFontColor().value(), Color::RED);
3237
3238 ViewStackProcessor::GetInstance()->Push(itemNode);
3239 MneuItemModelInstance.SetFontColor(std::nullopt);
3240 ASSERT_FALSE(itemProperty->GetFontColor().has_value());
3241 ViewStackProcessor::GetInstance()->Finish();
3242 }
3243
3244 /**
3245 * @tc.name: MenuItemViewTestNgSetLabelFontSize001
3246 * @tc.desc: Verify SetLabelFontSize.
3247 * @tc.type: FUNC
3248 */
3249 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetLabelFontSize001, TestSize.Level1)
3250 {
3251 MenuItemModelNG MneuItemModelInstance;
3252 MenuItemProperties itemOption;
3253 MneuItemModelInstance.Create(itemOption);
3254 MneuItemModelInstance.SetLabelFontSize(Dimension());
3255 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3256 ASSERT_NE(itemNode, nullptr);
3257 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
3258 ASSERT_NE(itemPattern, nullptr);
3259 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3260 ASSERT_NE(itemProperty, nullptr);
3261
3262 EXPECT_FALSE(itemProperty->GetLabelFontSize().has_value());
3263 }
3264
3265 /**
3266 * @tc.name: MenuItemViewTestNgSetLabelFontSize002
3267 * @tc.desc: Verify SetLabelFontSize.
3268 * @tc.type: FUNC
3269 */
3270 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetLabelFontSize002, TestSize.Level1)
3271 {
3272 MenuItemModelNG MneuItemModelInstance;
3273 MenuItemProperties itemOption;
3274 MneuItemModelInstance.Create(itemOption);
3275 MneuItemModelInstance.SetLabelFontSize(Dimension(40.0));
3276 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3277 ASSERT_NE(itemNode, nullptr);
3278 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
3279 ASSERT_NE(itemPattern, nullptr);
3280 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3281 ASSERT_NE(itemProperty, nullptr);
3282
3283 ASSERT_TRUE(itemProperty->GetLabelFontSize().has_value());
3284 EXPECT_EQ(itemProperty->GetLabelFontSize().value(), Dimension(40.0));
3285 }
3286
3287 /**
3288 * @tc.name: MenuItemViewTestNgSetLabelFontSize003
3289 * @tc.desc: Verify SetLabelFontSize.
3290 * @tc.type: FUNC
3291 */
3292 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetLabelFontSize003, TestSize.Level1)
3293 {
3294 MenuItemModelNG MneuItemModelInstance;
3295 MenuItemProperties itemOption;
3296 MneuItemModelInstance.SetLabelFontSize(Dimension(40.0));
3297 MneuItemModelInstance.Create(itemOption);
3298 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3299 ASSERT_NE(itemNode, nullptr);
3300 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3301 ASSERT_NE(itemProperty, nullptr);
3302 ASSERT_FALSE(itemProperty->GetLabelFontSize().has_value());
3303 }
3304
3305 /**
3306 * @tc.name: MenuItemViewTestNgSetLabelFontWeight001
3307 * @tc.desc: Verify SetLabelFontWeight.
3308 * @tc.type: FUNC
3309 */
3310 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetLabelFontWeight001, TestSize.Level1)
3311 {
3312 MenuItemModelNG MneuItemModelInstance;
3313 MenuItemProperties itemOption;
3314 MneuItemModelInstance.Create(itemOption);
3315 MneuItemModelInstance.SetLabelFontWeight(FontWeight::BOLD);
3316 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3317 ASSERT_NE(itemNode, nullptr);
3318 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
3319 ASSERT_NE(itemPattern, nullptr);
3320 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3321 ASSERT_NE(itemProperty, nullptr);
3322
3323 ASSERT_TRUE(itemProperty->GetLabelFontWeight().has_value());
3324 EXPECT_EQ(itemProperty->GetLabelFontWeight().value(), FontWeight::BOLD);
3325 }
3326
3327 /**
3328 * @tc.name: MenuItemViewTestNgSetLabelFontWeight002
3329 * @tc.desc: Verify SetLabelFontWeight.
3330 * @tc.type: FUNC
3331 */
3332 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetLabelFontWeight002, TestSize.Level1)
3333 {
3334 MenuItemModelNG MneuItemModelInstance;
3335 MenuItemProperties itemOption;
3336 MneuItemModelInstance.SetLabelFontWeight(FontWeight::BOLD);
3337 MneuItemModelInstance.Create(itemOption);
3338 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3339 ASSERT_NE(itemNode, nullptr);
3340 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3341 ASSERT_NE(itemProperty, nullptr);
3342 ASSERT_FALSE(itemProperty->GetLabelFontWeight().has_value());
3343 }
3344
3345 /**
3346 * @tc.name: MenuItemViewTestNgSetLabelFontColor001
3347 * @tc.desc: Verify SetLabelFontColor.
3348 * @tc.type: FUNC
3349 */
3350 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetLabelFontColor001, TestSize.Level1)
3351 {
3352 MenuItemModelNG MneuItemModelInstance;
3353 MenuItemProperties itemOption;
3354 MneuItemModelInstance.Create(itemOption);
3355 MneuItemModelInstance.SetLabelFontColor(Color::RED);
3356 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3357 ASSERT_NE(itemNode, nullptr);
3358 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
3359 ASSERT_NE(itemPattern, nullptr);
3360 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3361 ASSERT_NE(itemProperty, nullptr);
3362
3363 ASSERT_TRUE(itemProperty->GetLabelFontColor().has_value());
3364 EXPECT_EQ(itemProperty->GetLabelFontColor().value(), Color::RED);
3365 }
3366
3367 /**
3368 * @tc.name: MenuItemViewTestNgSetLabelFontColor002
3369 * @tc.desc: Verify SetLabelFontColor.
3370 * @tc.type: FUNC
3371 */
3372 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetLabelFontColor002, TestSize.Level1)
3373 {
3374 MenuItemModelNG MneuItemModelInstance;
3375 MenuItemProperties itemOption;
3376 MneuItemModelInstance.SetLabelFontColor(Color::RED);
3377 MneuItemModelInstance.Create(itemOption);
3378 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3379 ASSERT_NE(itemNode, nullptr);
3380 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3381 ASSERT_NE(itemProperty, nullptr);
3382 ASSERT_FALSE(itemProperty->GetLabelFontColor().has_value());
3383 }
3384
3385 /**
3386 * @tc.name: MenuItemViewTestNgSetLabelFontColor003
3387 * @tc.desc: Verify SetLabelFontColor.
3388 * @tc.type: FUNC
3389 */
3390 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetLabelFontColor003, TestSize.Level1)
3391 {
3392 MenuItemModelNG MneuItemModelInstance;
3393 MenuItemProperties itemOption;
3394 MneuItemModelInstance.Create(itemOption);
3395 MneuItemModelInstance.SetLabelFontColor(Color::RED);
3396 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3397 ASSERT_NE(itemNode, nullptr);
3398 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
3399 ASSERT_NE(itemPattern, nullptr);
3400 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3401 ASSERT_NE(itemProperty, nullptr);
3402
3403 ASSERT_TRUE(itemProperty->GetLabelFontColor().has_value());
3404 EXPECT_EQ(itemProperty->GetLabelFontColor().value(), Color::RED);
3405
3406 ViewStackProcessor::GetInstance()->Push(itemNode);
3407 MneuItemModelInstance.SetLabelFontColor(std::nullopt);
3408 ASSERT_FALSE(itemProperty->GetLabelFontColor().has_value());
3409 ViewStackProcessor::GetInstance()->Finish();
3410 }
3411 /**
3412 * @tc.name: MenuItemSetSelectedChangeEvent001
3413 * @tc.desc: Verify SetFontSize.
3414 * @tc.type: FUNC
3415 */
3416 HWTEST_F(MenuTestNg, MenuItemSetSelectedChangeEvent001, TestSize.Level1)
3417 {
3418 MenuItemModelNG MneuItemModelInstance;
3419 bool isSelected = false;
__anon51d21a700f02(bool select) 3420 auto changeEvent = [&isSelected](bool select) { isSelected = select; };
3421 MenuItemProperties itemOption;
3422 MneuItemModelInstance.Create(itemOption);
3423 MneuItemModelInstance.SetSelectedChangeEvent(changeEvent);
3424
3425 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3426 ASSERT_NE(itemNode, nullptr);
3427
3428 auto itemProperty = itemNode->GetEventHub<NG::MenuItemEventHub>();
3429 ASSERT_NE(itemProperty, nullptr);
3430 EXPECT_TRUE(itemProperty->GetSelectedChangeEvent());
3431 }
3432
3433 /**
3434 * @tc.name: MenuItemViewTestNgSetLabelFontStyle001
3435 * @tc.desc: Verify SetLabelFontStyle.
3436 * @tc.type: FUNC
3437 */
3438 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetLabelFontStyle001, TestSize.Level1)
3439 {
3440 /**
3441 * @tc.steps: step1. create MenuItemModelNG object and set LabelFontStyle properties.
3442 */
3443 MenuItemModelNG MneuItemModelInstance;
3444 MenuItemProperties itemOption;
3445 MneuItemModelInstance.Create(itemOption);
3446 MneuItemModelInstance.SetLabelFontStyle(Ace::FontStyle::ITALIC);
3447
3448 /**
3449 * @tc.steps: step2. get the frameNode, menuItemPattern and menuItemLayoutProperty.
3450 * @tc.expected: step2. check whether the objects is available.
3451 */
3452 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3453 ASSERT_NE(itemNode, nullptr);
3454 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
3455 ASSERT_NE(itemPattern, nullptr);
3456 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3457 ASSERT_NE(itemProperty, nullptr);
3458
3459 /**
3460 * @tc.steps: step3. get the labelFontStyle properties of menuItemLayoutProperty.
3461 * @tc.expected: step3. check whether the labelFontStyle properties is is correct.
3462 */
3463 ASSERT_TRUE(itemProperty->GetLabelItalicFontStyle().has_value());
3464 EXPECT_EQ(itemProperty->GetLabelItalicFontStyle().value(), Ace::FontStyle::ITALIC);
3465 }
3466
3467 /**
3468 * @tc.name: MenuItemViewTestNgSetFontStyle001
3469 * @tc.desc: Verify SetFontStyle.
3470 * @tc.type: FUNC
3471 */
3472 HWTEST_F(MenuTestNg, MenuItemViewTestNgSetFontStyle001, TestSize.Level1)
3473 {
3474 /**
3475 * @tc.steps: step1. create MenuItemModelNG object and set FontStyle properties.
3476 */
3477 MenuItemModelNG MneuItemModelInstance;
3478 MenuItemProperties itemOption;
3479 MneuItemModelInstance.Create(itemOption);
3480 MneuItemModelInstance.SetFontStyle(Ace::FontStyle::ITALIC);
3481
3482 /**
3483 * @tc.steps: step2. get the frameNode, menuItemPattern and menuItemLayoutProperty.
3484 * @tc.expected: step2. check whether the objects is available.
3485 */
3486 auto itemNode = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
3487 ASSERT_NE(itemNode, nullptr);
3488 auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
3489 ASSERT_NE(itemPattern, nullptr);
3490 auto itemProperty = itemNode->GetLayoutProperty<MenuItemLayoutProperty>();
3491 ASSERT_NE(itemProperty, nullptr);
3492
3493 /**
3494 * @tc.steps: step3. get the FontStyle properties of menuItemLayoutProperty.
3495 * @tc.expected: step3. check whether the FontStyle properties is is correct.
3496 */
3497 ASSERT_TRUE(itemProperty->GetItalicFontStyle().has_value());
3498 EXPECT_EQ(itemProperty->GetItalicFontStyle().value(), Ace::FontStyle::ITALIC);
3499 }
3500
3501 /**
3502 * @tc.name: MenuLayoutAlgorithmTestNg001
3503 * @tc.desc: Verify HorizontalLayout.
3504 * @tc.type: FUNC
3505 */
3506 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg001, TestSize.Level1)
3507 {
3508 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
3509 SizeF size(50, 100);
3510 SizeF size_f(100, 200);
3511 float clickPosition = 50.0f;
3512 menuLayoutAlgorithm->wrapperSize_ = size_f;
3513 auto result = menuLayoutAlgorithm->HorizontalLayout(size, clickPosition);
3514 EXPECT_EQ(result, clickPosition);
3515 }
3516
3517 /**
3518 * @tc.name: MenuLayoutAlgorithmTestNg002
3519 * @tc.desc: Verify HorizontalLayout.
3520 * @tc.type: FUNC
3521 */
3522 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg002, TestSize.Level1)
3523 {
3524 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
3525 SizeF size(50, 100);
3526 SizeF size_f(100, 200);
3527 float clickPosition = 60.0f;
3528 menuLayoutAlgorithm->wrapperSize_ = size_f;
3529 auto result = menuLayoutAlgorithm->HorizontalLayout(size, clickPosition);
3530 EXPECT_EQ(result, size_f.Width() - size.Width());
3531 }
3532
3533 /**
3534 * @tc.name: MenuLayoutAlgorithmTestNg003
3535 * @tc.desc: Verify HorizontalLayout.
3536 * @tc.type: FUNC
3537 */
3538 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg003, TestSize.Level1)
3539 {
3540 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
3541 SizeF size(70, 100);
3542 SizeF size_f(100, 200);
3543 float clickPosition = 60.0f;
3544 menuLayoutAlgorithm->wrapperSize_ = size_f;
3545 auto result = menuLayoutAlgorithm->HorizontalLayout(size, clickPosition);
3546 EXPECT_EQ(result, menuLayoutAlgorithm->wrapperSize_.Width() - size.Width());
3547 }
3548
3549 /**
3550 * @tc.name: MenuLayoutAlgorithmTestNg004
3551 * @tc.desc: Verify HorizontalLayout.
3552 * @tc.type: FUNC
3553 */
3554 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg004, TestSize.Level1)
3555 {
3556 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
3557 SizeF size(100, 100);
3558 SizeF size_f(100, 200);
3559 float clickPosition = 60.0f;
3560 menuLayoutAlgorithm->wrapperSize_ = size_f;
3561 auto result = menuLayoutAlgorithm->HorizontalLayout(size, clickPosition);
3562 EXPECT_EQ(result, 0.0);
3563 }
3564
3565 /**
3566 * @tc.name: MenuLayoutAlgorithmTestNg005
3567 * @tc.desc: Verify VerticalLayout.
3568 * @tc.type: FUNC
3569 */
3570 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg005, TestSize.Level1)
3571 {
3572 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
3573 SizeF size(100, 100);
3574 SizeF size_f(100, 200);
3575 float clickPosition = 100.0f;
3576 menuLayoutAlgorithm->wrapperSize_ = size_f;
3577 auto result = menuLayoutAlgorithm->VerticalLayout(size, clickPosition);
3578 EXPECT_EQ(result, clickPosition);
3579 }
3580
3581 /**
3582 * @tc.name: MenuLayoutAlgorithmTestNg006
3583 * @tc.desc: Verify VerticalLayout.
3584 * @tc.type: FUNC
3585 */
3586 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg006, TestSize.Level1)
3587 {
3588 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
3589 SizeF size(100, 100);
3590 float clickPosition = 150.0f;
3591 menuLayoutAlgorithm->topSpace_ = 200.0f;
3592 auto result = menuLayoutAlgorithm->VerticalLayout(size, clickPosition);
3593 EXPECT_EQ(result, menuLayoutAlgorithm->topSpace_ - size.Height());
3594 }
3595
3596 /**
3597 * @tc.name: MenuLayoutAlgorithmTestNg007
3598 * @tc.desc: Verify VerticalLayout.
3599 * @tc.type: FUNC
3600 */
3601 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg007, TestSize.Level1)
3602 {
3603 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
3604 SizeF size(100, 150);
3605 SizeF size_f(100, 200);
3606 float clickPosition = 100.0f;
3607 menuLayoutAlgorithm->wrapperSize_ = size_f;
3608 auto result = menuLayoutAlgorithm->VerticalLayout(size, clickPosition);
3609 EXPECT_EQ(result, menuLayoutAlgorithm->wrapperSize_.Height() - size.Height());
3610 }
3611
3612 /**
3613 * @tc.name: MenuLayoutAlgorithmTestNg008
3614 * @tc.desc: Verify VerticalLayout.
3615 * @tc.type: FUNC
3616 */
3617 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg008, TestSize.Level1)
3618 {
3619 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
3620 SizeF size(100, 200);
3621 SizeF size_f(100, 200);
3622 float clickPosition = 100.0f;
3623 menuLayoutAlgorithm->wrapperSize_ = size_f;
3624 auto result = menuLayoutAlgorithm->VerticalLayout(size, clickPosition);
3625 EXPECT_EQ(result, 0.0);
3626 }
3627
3628 /**
3629 * @tc.name: MenuLayoutAlgorithmTestNg009
3630 * @tc.desc: Verify Initialize.
3631 * @tc.type: FUNC
3632 */
3633 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg009, TestSize.Level1)
3634 {
__anon51d21a701002null3635 std::function<void()> action = [] {};
3636 std::vector<OptionParam> optionParams;
3637 optionParams.emplace_back("MenuItem1", "", action);
3638 optionParams.emplace_back("MenuItem2", "", action);
3639 MenuParam menuParam;
3640 auto menuWrapperNode = MenuView::Create(std::move(optionParams),
3641 1, "", MenuType::SELECT_OVERLAY_EXTENSION_MENU, menuParam);
3642 ASSERT_NE(menuWrapperNode, nullptr);
3643 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
3644 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
3645 ASSERT_NE(menuNode, nullptr);
3646 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
3647 ASSERT_NE(property, nullptr);
3648 ASSERT_TRUE(property->GetPositionOffset().has_value());
3649 EXPECT_EQ(property->GetPositionOffset().value(), OffsetF());
3650 RefPtr<MenuLayoutAlgorithm> layoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
3651 RefPtr<GeometryNode> geometryNode = AceType::MakeRefPtr<GeometryNode>();
3652 LayoutWrapperNode layoutWrapper(menuNode, geometryNode, menuNode->GetLayoutProperty());
3653
3654 layoutWrapper.GetLayoutProperty()->UpdateUserDefinedIdealSize(
3655 CalcSize(CalcLength(FULL_SCREEN_WIDTH), CalcLength(FULL_SCREEN_HEIGHT)));
3656 LayoutConstraintF parentLayoutConstraint;
3657 parentLayoutConstraint.maxSize = FULL_SCREEN_SIZE;
3658 parentLayoutConstraint.percentReference = FULL_SCREEN_SIZE;
3659 parentLayoutConstraint.selfIdealSize.SetSize(SizeF(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT));
3660 layoutWrapper.GetLayoutProperty()->UpdateLayoutConstraint(parentLayoutConstraint);
3661 layoutWrapper.GetLayoutProperty()->UpdateContentConstraint();
3662
3663 layoutAlgorithm->Initialize(&layoutWrapper);
3664 layoutAlgorithm->Measure(&layoutWrapper);
3665 EXPECT_EQ(layoutAlgorithm->position_, OffsetF());
3666 EXPECT_EQ(layoutAlgorithm->positionOffset_, OffsetF());
3667 EXPECT_EQ(layoutAlgorithm->wrapperSize_, SizeF(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT));
3668 layoutAlgorithm->Layout(&layoutWrapper);
3669 EXPECT_EQ(geometryNode->GetMarginFrameOffset(), OffsetF(0.0f, 0.0f));
3670 }
3671
3672 /**
3673 * @tc.name: MenuLayoutAlgorithmTestNg010
3674 * @tc.desc: Verify Layout.
3675 * @tc.type: FUNC
3676 */
3677 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg010, TestSize.Level1)
3678 {
3679 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
3680 OffsetF offset(10, 10);
3681 menuLayoutAlgorithm->position_ = offset;
3682 const std::string tag = "tag";
3683 int32_t nodeId = 1;
3684 RefPtr<Pattern> pattern = AceType::MakeRefPtr<Pattern>();
3685 bool isRoot = false;
3686 RefPtr<GeometryNode> geometryNode = AceType::MakeRefPtr<GeometryNode>();
3687 RefPtr<FrameNode> frameNode = AceType::MakeRefPtr<FrameNode>(tag, nodeId, pattern, isRoot);
3688 LayoutWrapperNode* layoutWrapper = new LayoutWrapperNode(frameNode, geometryNode, frameNode->GetLayoutProperty());
3689 RefPtr<LazyForEachActuator> actuator = AceType::MakeRefPtr<LazyForEachActuator>();
3690 auto builder = AceType::DynamicCast<LazyForEachBuilder>(actuator);
3691 RefPtr<LazyForEachNode> host_ = AceType::MakeRefPtr<LazyForEachNode>(nodeId, builder);
3692 WeakPtr<LazyForEachNode> host(host_);
3693 RefPtr<LazyLayoutWrapperBuilder> wrapperBuilder = AceType::MakeRefPtr<LazyLayoutWrapperBuilder>(builder, host);
3694 wrapperBuilder->OnGetOrCreateWrapperByIndex(nodeId);
3695 auto children = wrapperBuilder->OnExpandChildLayoutWrapper();
3696 auto layoutWrapper_ = wrapperBuilder->GetOrCreateWrapperByIndex(nodeId);
3697 menuLayoutAlgorithm->Layout(layoutWrapper);
3698 EXPECT_EQ(menuLayoutAlgorithm->position_.GetX(), 10);
3699 delete layoutWrapper;
3700 layoutWrapper = nullptr;
3701 }
3702
3703 /**
3704 * @tc.name: MenuLayoutAlgorithmTestNg011
3705 * @tc.desc: Verify positionOffset of Layout.
3706 * @tc.type: FUNC
3707 */
3708 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg011, TestSize.Level1)
3709 {
__anon51d21a701102null3710 std::function<void()> action = [] {};
3711 std::vector<OptionParam> optionParams;
3712 optionParams.emplace_back("MenuItem1", "", action);
3713 optionParams.emplace_back("MenuItem2", "", action);
3714 MenuParam menuParam;
3715 auto menuWrapperNode = MenuView::Create(std::move(optionParams), 1, "", MenuType::MENU, menuParam);
3716 ASSERT_NE(menuWrapperNode, nullptr);
3717 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
3718 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
3719 ASSERT_NE(menuNode, nullptr);
3720 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
3721 ASSERT_NE(property, nullptr);
3722 ASSERT_TRUE(property->GetPositionOffset().has_value());
3723 EXPECT_EQ(property->GetPositionOffset().value(), OffsetF());
3724 RefPtr<MenuLayoutAlgorithm> layoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
3725 RefPtr<GeometryNode> geometryNode = AceType::MakeRefPtr<GeometryNode>();
3726 LayoutWrapperNode layoutWrapper(menuNode, geometryNode, menuNode->GetLayoutProperty());
3727
3728 layoutWrapper.GetLayoutProperty()->UpdateUserDefinedIdealSize(
3729 CalcSize(CalcLength(FULL_SCREEN_WIDTH), CalcLength(FULL_SCREEN_HEIGHT)));
3730 LayoutConstraintF parentLayoutConstraint;
3731 parentLayoutConstraint.maxSize = FULL_SCREEN_SIZE;
3732 parentLayoutConstraint.percentReference = FULL_SCREEN_SIZE;
3733 parentLayoutConstraint.selfIdealSize.SetSize(SizeF(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT));
3734 layoutWrapper.GetLayoutProperty()->UpdateLayoutConstraint(parentLayoutConstraint);
3735 layoutWrapper.GetLayoutProperty()->UpdateContentConstraint();
3736
3737 layoutAlgorithm->Measure(&layoutWrapper);
3738 EXPECT_EQ(layoutAlgorithm->position_, OffsetF());
3739 EXPECT_EQ(layoutAlgorithm->positionOffset_, OffsetF());
3740 EXPECT_EQ(layoutAlgorithm->wrapperSize_, SizeF(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT));
3741 layoutAlgorithm->Layout(&layoutWrapper);
3742 EXPECT_EQ(geometryNode->GetMarginFrameOffset(), OffsetF(0.0f, 0.0f));
3743 }
3744
3745 /**
3746 * @tc.name: MenuLayoutAlgorithmTestNg012
3747 * @tc.desc: Verify positionOffset of Layout.
3748 * @tc.type: FUNC
3749 */
3750 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg012, TestSize.Level1)
3751 {
3752 std::vector<OptionParam> optionParams;
3753 optionParams.emplace_back("MenuItem1", "", nullptr);
3754 optionParams.emplace_back("MenuItem2", "", nullptr);
3755 MenuParam menuParam = { "", { POSITION_OFFSET, POSITION_OFFSET } };
3756 auto menuWrapperNode = MenuView::Create(std::move(optionParams), 1, "", MenuType::MENU, menuParam);
3757 ASSERT_NE(menuWrapperNode, nullptr);
3758 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
3759 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
3760 ASSERT_NE(menuNode, nullptr);
3761 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
3762 ASSERT_NE(property, nullptr);
3763 ASSERT_TRUE(property->GetPositionOffset().has_value());
3764 EXPECT_EQ(property->GetPositionOffset().value(), OffsetF(POSITION_OFFSET, POSITION_OFFSET));
3765
3766 RefPtr<MenuLayoutAlgorithm> layoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
3767 RefPtr<GeometryNode> geometryNode = AceType::MakeRefPtr<GeometryNode>();
3768 LayoutWrapperNode layoutWrapper(menuNode, geometryNode, menuNode->GetLayoutProperty());
3769 layoutWrapper.GetLayoutProperty()->UpdateUserDefinedIdealSize(
3770 CalcSize(CalcLength(FULL_SCREEN_WIDTH), CalcLength(FULL_SCREEN_HEIGHT)));
3771 LayoutConstraintF parentLayoutConstraint;
3772 parentLayoutConstraint.maxSize = FULL_SCREEN_SIZE;
3773 parentLayoutConstraint.percentReference = FULL_SCREEN_SIZE;
3774 parentLayoutConstraint.selfIdealSize.SetSize(SizeF(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT));
3775 layoutWrapper.GetLayoutProperty()->UpdateLayoutConstraint(parentLayoutConstraint);
3776 layoutWrapper.GetLayoutProperty()->UpdateContentConstraint();
3777
3778 layoutAlgorithm->Measure(&layoutWrapper);
3779 EXPECT_EQ(layoutAlgorithm->position_, OffsetF());
3780 EXPECT_EQ(layoutAlgorithm->positionOffset_, OffsetF(POSITION_OFFSET, POSITION_OFFSET));
3781 EXPECT_EQ(layoutAlgorithm->wrapperSize_, SizeF(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT));
3782 layoutAlgorithm->Layout(&layoutWrapper);
3783 EXPECT_EQ(geometryNode->GetMarginFrameOffset(), OffsetF(POSITION_OFFSET, POSITION_OFFSET));
3784 }
3785
3786 /**
3787 * @tc.name: MenuLayoutAlgorithmTestNg013
3788 * @tc.desc: Verify ComputeMenuPositionByAlignType.
3789 * @tc.type: FUNC
3790 */
3791 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg013, TestSize.Level1)
3792 {
3793 std::vector<SelectParam> selectParams;
3794 selectParams.emplace_back(std::make_pair("MenuItem1", "Icon1"));
3795 selectParams.emplace_back(std::make_pair("MenuItem2", "Icon2"));
3796 // create select menu
3797 auto menuWrapperNode = MenuView::Create(std::move(selectParams), 1);
3798 ASSERT_NE(menuWrapperNode, nullptr);
3799 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
3800 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
3801 ASSERT_NE(menuNode, nullptr);
3802 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
3803 ASSERT_NE(property, nullptr);
3804 SizeF targetSize(TARGET_SIZE_WIDTH, TARGET_SIZE_HEIGHT);
3805 property->UpdateTargetSize(targetSize);
3806
3807 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
3808 ASSERT_NE(menuLayoutAlgorithm, nullptr);
3809
3810 /**
3811 * @tc.cases: case1. the menu align type is start.
3812 */
3813 SizeF menuSize(MENU_SIZE_WIDTH, MENU_SIZE_HEIGHT);
3814 menuLayoutAlgorithm->position_ = OffsetF(0, 0);
3815 property->UpdateAlignType(MenuAlignType::START);
3816 menuLayoutAlgorithm->ComputeMenuPositionByAlignType(property, menuSize);
3817 EXPECT_EQ(menuLayoutAlgorithm->position_.GetX(), 0);
3818
3819 /**
3820 * @tc.cases: case2. the menu align type is center.
3821 */
3822 menuLayoutAlgorithm->position_ = OffsetF(0, 0);
3823 property->UpdateAlignType(MenuAlignType::CENTER);
3824 menuLayoutAlgorithm->ComputeMenuPositionByAlignType(property, menuSize);
3825 float expectResult = -25.0f;
3826 EXPECT_EQ(menuLayoutAlgorithm->position_.GetX(), expectResult);
3827
3828 /**
3829 * @tc.cases: case3. the menu align type is end.
3830 */
3831 menuLayoutAlgorithm->position_ = OffsetF(0, 0);
3832 property->UpdateAlignType(MenuAlignType::END);
3833 menuLayoutAlgorithm->ComputeMenuPositionByAlignType(property, menuSize);
3834 expectResult = -50.0f;
3835 EXPECT_EQ(menuLayoutAlgorithm->position_.GetX(), expectResult);
3836 }
3837
3838 /**
3839 * @tc.name: MenuLayoutAlgorithmTestNg014
3840 * @tc.desc: Verify ComputeMenuPositionByOffset.
3841 * @tc.type: FUNC
3842 */
3843 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg014, TestSize.Level1)
3844 {
3845 std::vector<SelectParam> selectParams;
3846 selectParams.emplace_back(std::make_pair("MenuItem1", "Icon1"));
3847 selectParams.emplace_back(std::make_pair("MenuItem2", "Icon2"));
3848 // create select menu
3849 auto menuWrapperNode = MenuView::Create(std::move(selectParams), 1);
3850 ASSERT_NE(menuWrapperNode, nullptr);
3851 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
3852 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
3853 ASSERT_NE(menuNode, nullptr);
3854 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
3855 ASSERT_NE(property, nullptr);
3856 LayoutConstraintF parentLayoutConstraint;
3857 parentLayoutConstraint.maxSize = FULL_SCREEN_SIZE;
3858 parentLayoutConstraint.percentReference = FULL_SCREEN_SIZE;
3859 parentLayoutConstraint.selfIdealSize.SetSize(SizeF(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT));
3860 property->UpdateLayoutConstraint(parentLayoutConstraint);
3861
3862 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
3863 ASSERT_NE(menuLayoutAlgorithm, nullptr);
3864 RefPtr<GeometryNode> geometryNode = AceType::MakeRefPtr<GeometryNode>();
3865 ASSERT_NE(geometryNode, nullptr);
3866 geometryNode->SetFrameSize(SizeF(MENU_SIZE_WIDTH, MENU_SIZE_HEIGHT));
3867
3868 /**
3869 * @tc.cases: case1. parameter is valid, return the valid offset.
3870 */
3871 property->UpdateOffset(
3872 DimensionOffset(Dimension(MENU_OFFSET_X, DimensionUnit::VP), Dimension(MENU_OFFSET_Y, DimensionUnit::VP)));
3873 auto resultOffset = menuLayoutAlgorithm->ComputeMenuPositionByOffset(property, geometryNode);
3874 EXPECT_EQ(resultOffset, OffsetF(MENU_OFFSET_X, MENU_OFFSET_Y));
3875
3876 /**
3877 * @tc.cases: case2. parameter property is nullptr, return OffsetF(0.0, 0.0).
3878 */
3879 resultOffset = menuLayoutAlgorithm->ComputeMenuPositionByOffset(nullptr, geometryNode);
3880 EXPECT_EQ(resultOffset, OffsetF(0.0, 0.0));
3881
3882 /**
3883 * @tc.cases: case3. parameter geometryNode is nullptr, return OffsetF(0.0, 0.0).
3884 */
3885 resultOffset = menuLayoutAlgorithm->ComputeMenuPositionByOffset(property, nullptr);
3886 EXPECT_EQ(resultOffset, OffsetF(0.0, 0.0));
3887 }
3888
3889 /**
3890 * @tc.name: MenuLayoutAlgorithmTestNg015
3891 * @tc.desc: Verify ComputeMenuPositionByOffset.
3892 * @tc.type: FUNC
3893 */
3894 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg015, TestSize.Level1)
3895 {
3896 std::vector<SelectParam> selectParams;
3897 selectParams.emplace_back(std::make_pair("MenuItem1", "Icon1"));
3898 selectParams.emplace_back(std::make_pair("MenuItem2", "Icon2"));
3899 // create select menu
3900 auto menuWrapperNode = MenuView::Create(std::move(selectParams), 1);
3901 ASSERT_NE(menuWrapperNode, nullptr);
3902 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
3903 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
3904 ASSERT_NE(menuNode, nullptr);
3905 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
3906 ASSERT_NE(property, nullptr);
3907 LayoutConstraintF parentLayoutConstraint;
3908 parentLayoutConstraint.maxSize = FULL_SCREEN_SIZE;
3909 parentLayoutConstraint.percentReference = FULL_SCREEN_SIZE;
3910 parentLayoutConstraint.selfIdealSize.SetSize(SizeF(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT));
3911 property->UpdateLayoutConstraint(parentLayoutConstraint);
3912
3913 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>(NODEID, "menu");
3914 ASSERT_NE(menuLayoutAlgorithm, nullptr);
3915
3916 auto menuPattern = menuNode->GetPattern<MenuPattern>();
3917 CHECK_NULL_VOID(menuPattern);
3918 menuPattern->isSelectMenu_ = true;
3919 SizeF size(MENU_SIZE_WIDTH, MENU_SIZE_HEIGHT);
3920
3921 /**
3922 * @tc.cases: case1. parameter property is nullptr, return OffsetF(0.0, 0.0).
3923 */
3924 auto resultOffset = menuLayoutAlgorithm->MenuLayoutAvoidAlgorithm(nullptr, menuPattern, size);
3925 EXPECT_EQ(resultOffset, OffsetF(0.0, 0.0));
3926
3927 /**
3928 * @tc.cases: case2. parameter menuPattern is nullptr, return OffsetF(0.0, 0.0).
3929 */
3930 resultOffset = menuLayoutAlgorithm->MenuLayoutAvoidAlgorithm(property, nullptr, size);
3931 EXPECT_EQ(resultOffset, OffsetF(0.0, 0.0));
3932
3933 /**
3934 * @tc.cases: case3. menu property has placement value and has targetSize.
3935 */
3936 property->UpdateMenuPlacement(Placement::RIGHT);
3937 menuLayoutAlgorithm->placement_ = Placement::RIGHT;
3938 menuLayoutAlgorithm->targetSize_ = SizeF(TARGET_SIZE_WIDTH, TARGET_SIZE_HEIGHT);
3939 menuLayoutAlgorithm->targetOffset_ = OffsetF(POSITION_OFFSET, POSITION_OFFSET);
3940 menuLayoutAlgorithm->wrapperSize_ = SizeF(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT);
3941 resultOffset = menuLayoutAlgorithm->MenuLayoutAvoidAlgorithm(property, menuPattern, size);
3942 float expectOffsetX = POSITION_OFFSET + TARGET_SIZE_WIDTH + TARGET_SECURITY.ConvertToPx();
3943 float expectOffsetY = MENU_SIZE_HEIGHT / 2;
3944 EXPECT_EQ(resultOffset, OffsetF(expectOffsetX, expectOffsetY));
3945
3946 /**
3947 * @tc.cases: case4. target size is (0.0, 0.0)
3948 */
3949 menuLayoutAlgorithm->targetSize_ = SizeF(0.0f, 0.0f);
3950 resultOffset = menuLayoutAlgorithm->MenuLayoutAvoidAlgorithm(property, menuPattern, size);
3951 EXPECT_EQ(resultOffset, OffsetF(FULL_SCREEN_WIDTH - MENU_SIZE_WIDTH, FULL_SCREEN_HEIGHT - MENU_SIZE_HEIGHT));
3952 }
3953
3954 /**
3955 * @tc.name: MenuLayoutAlgorithmTestNg016
3956 * @tc.desc: Test MultiMenu layout algorithm.
3957 * @tc.type: FUNC
3958 */
3959 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg016, TestSize.Level1)
3960 {
3961 auto menuPattern = AceType::MakeRefPtr<MenuPattern>(-1, "", MenuType::MULTI_MENU);
3962 auto multiMenu = AceType::MakeRefPtr<FrameNode>("", -1, menuPattern);
3963 auto algorithm = AceType::MakeRefPtr<MultiMenuLayoutAlgorithm>();
3964 auto geometryNode = AceType::MakeRefPtr<GeometryNode>();
3965 geometryNode->SetFrameSize(SizeF(MENU_SIZE_WIDTH, MENU_SIZE_HEIGHT));
3966 auto layoutProp = AceType::MakeRefPtr<MenuLayoutProperty>();
3967 auto* wrapper = new LayoutWrapperNode(multiMenu, geometryNode, layoutProp);
3968
3969 for (int32_t i = 0; i < 3; ++i) {
3970 auto itemPattern = AceType::MakeRefPtr<MenuItemPattern>();
3971 auto menuItem = AceType::MakeRefPtr<FrameNode>("", -1, itemPattern);
3972 auto itemGeoNode = AceType::MakeRefPtr<GeometryNode>();
3973 itemGeoNode->SetFrameSize(SizeF(MENU_SIZE_WIDTH, MENU_SIZE_HEIGHT / 3));
3974 auto childWrapper = AceType::MakeRefPtr<LayoutWrapperNode>(menuItem, itemGeoNode, layoutProp);
3975 wrapper->AppendChild(childWrapper);
3976 }
3977
3978 algorithm->Layout(wrapper);
3979 // default padding from theme is zero, so the offset on the first child is zero.
3980 OffsetF offset;
3981 for (auto&& child : wrapper->GetAllChildrenWithBuild()) {
3982 EXPECT_EQ(child->GetGeometryNode()->GetMarginFrameOffset(), offset);
3983 offset.AddY(MENU_SIZE_HEIGHT / 3);
3984 }
3985 }
3986
3987 /**
3988 * @tc.name: MenuLayoutAlgorithmTestNg017
3989 * @tc.desc: Verify GetPositionWithPlacement.
3990 * @tc.type: FUNC
3991 */
3992 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg017, TestSize.Level1)
3993 {
3994 /**
3995 * @tc.steps: step1. create menuLayoutAlgorithm, set childSize, topPosition, bottomPosition
3996 */
3997 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
3998 ASSERT_NE(menuLayoutAlgorithm, nullptr);
3999 SizeF childSize(CHILD_SIZE_X, CHILD_SIZE_Y);
4000 OffsetF topPosition(TOP_POSITION_X, TOP_POSITION_Y);
4001 OffsetF bottomPosition(BOTTOM_POSITION_X, BOTTOM_POSITION_Y);
4002 auto result = menuLayoutAlgorithm->GetPositionWithPlacement(childSize, topPosition, bottomPosition);
4003 EXPECT_EQ(result.GetX(), 0);
4004 EXPECT_EQ(result.GetY(), 0);
4005 }
4006
4007 /**
4008 * @tc.name: MenuLayoutAlgorithmTestNg018
4009 * @tc.desc: Verify GetPositionWithPlacement with targetNodeId, targetTag
4010 * @tc.type: FUNC
4011 */
4012 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg018, TestSize.Level1)
4013 {
4014 /**
4015 * @tc.steps: step1. create menuLayoutAlgorithm, set childSize, topPosition, bottomPosition, targetNodeId, targetTag
4016 */
4017 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
4018 ASSERT_NE(menuLayoutAlgorithm, nullptr);
4019 SizeF childSize(CHILD_SIZE_X, CHILD_SIZE_Y);
4020 OffsetF topPosition(TOP_POSITION_X, TOP_POSITION_Y);
4021 OffsetF bottomPosition(BOTTOM_POSITION_X, BOTTOM_POSITION_Y);
4022 const std::string tag = "tag";
4023 MenuLayoutAlgorithm menu(NODEID, tag);
4024 auto result = menu.GetPositionWithPlacement(childSize, topPosition, bottomPosition);
4025 EXPECT_EQ(result.GetX(), 0);
4026 EXPECT_EQ(result.GetY(), 0);
4027 }
4028
4029 /**
4030 * @tc.name: MenuLayoutAlgorithmTestNg019
4031 * @tc.desc: Verify AddTargetSpace with placement
4032 * @tc.type: FUNC
4033 */
4034 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg019, TestSize.Level1)
4035 {
4036 /**
4037 * @tc.steps: step1. create menuLayoutAlgorithm, placement is BOTTOM_LEFT
4038 * @tc.expected: result y offset is add 8vp space
4039 */
4040 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
4041 ASSERT_NE(menuLayoutAlgorithm, nullptr);
4042 menuLayoutAlgorithm->placement_ = Placement::BOTTOM_LEFT;
4043 auto result = menuLayoutAlgorithm->AddTargetSpace(OffsetF(OFFSET_FIRST, OFFSET_FIRST));
4044 EXPECT_EQ(result, OffsetF(OFFSET_FIRST, OFFSET_FIRST + TARGET_SECURITY.ConvertToPx()));
4045
4046 /**
4047 * @tc.steps: step2. create menuLayoutAlgorithm, placement is TOP_LEFT
4048 * @tc.expected: result y offset is reduce 8vp space
4049 */
4050 menuLayoutAlgorithm->placement_ = Placement::TOP_LEFT;
4051 result = menuLayoutAlgorithm->AddTargetSpace(OffsetF(OFFSET_FIRST, OFFSET_FIRST));
4052 EXPECT_EQ(result, OffsetF(OFFSET_FIRST, OFFSET_FIRST - TARGET_SECURITY.ConvertToPx()));
4053
4054 /**
4055 * @tc.steps: step3. create menuLayoutAlgorithm, placement is RIGHT_TOP
4056 * @tc.expected: result x offset is add 8vp space
4057 */
4058 menuLayoutAlgorithm->placement_ = Placement::RIGHT_TOP;
4059 result = menuLayoutAlgorithm->AddTargetSpace(OffsetF(OFFSET_FIRST, OFFSET_FIRST));
4060 EXPECT_EQ(result, OffsetF(OFFSET_FIRST + TARGET_SECURITY.ConvertToPx(), OFFSET_FIRST));
4061
4062 /**
4063 * @tc.steps: step4. create menuLayoutAlgorithm, placement is LEFT_TOP
4064 * @tc.expected: result x offset is reduce 8vp space
4065 */
4066 menuLayoutAlgorithm->placement_ = Placement::LEFT_TOP;
4067 result = menuLayoutAlgorithm->AddTargetSpace(OffsetF(OFFSET_FIRST, OFFSET_FIRST));
4068 EXPECT_EQ(result, OffsetF(OFFSET_FIRST - TARGET_SECURITY.ConvertToPx(), OFFSET_FIRST));
4069
4070 /**
4071 * @tc.steps: step5. create menuLayoutAlgorithm, placement is NONE
4072 * @tc.expected: the offset of the result is consistent with when the placement is BOTTOM_LEFT
4073 */
4074 menuLayoutAlgorithm->placement_ = Placement::NONE;
4075 result = menuLayoutAlgorithm->AddTargetSpace(OffsetF(OFFSET_FIRST, OFFSET_FIRST));
4076 EXPECT_EQ(result, OffsetF(OFFSET_FIRST, OFFSET_FIRST + TARGET_SECURITY.ConvertToPx()));
4077 }
4078
4079 /**
4080 * @tc.name: MenuLayoutAlgorithmTestNg020
4081 * @tc.desc: Verify AddOffset with placement
4082 * @tc.type: FUNC
4083 */
4084 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg020, TestSize.Level1)
4085 {
4086 /**
4087 * @tc.steps: step1. create menuLayoutAlgorithm, placement is BOTTOM_LEFT
4088 * @tc.expected: result x and y offset is add position offset
4089 */
4090 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
4091 ASSERT_NE(menuLayoutAlgorithm, nullptr);
4092 menuLayoutAlgorithm->positionOffset_ = OffsetF(OFFSET_SECOND, OFFSET_SECOND);
4093 menuLayoutAlgorithm->placement_ = Placement::BOTTOM_LEFT;
4094 auto result = menuLayoutAlgorithm->AddOffset(OffsetF(OFFSET_FIRST, OFFSET_FIRST));
4095 EXPECT_EQ(result, OffsetF(OFFSET_FIRST + OFFSET_SECOND, OFFSET_FIRST + OFFSET_SECOND));
4096
4097 /**
4098 * @tc.steps: step2. create menuLayoutAlgorithm, placement is TOP_LEFT
4099 * @tc.expected: result x offset add position offset and y offset is reduce position offset
4100 */
4101 menuLayoutAlgorithm->placement_ = Placement::TOP_LEFT;
4102 result = menuLayoutAlgorithm->AddOffset(OffsetF(OFFSET_FIRST, OFFSET_FIRST));
4103 EXPECT_EQ(result, OffsetF(OFFSET_FIRST + OFFSET_SECOND, OFFSET_FIRST - OFFSET_SECOND));
4104
4105 /**
4106 * @tc.steps: step3. create menuLayoutAlgorithm, placement is RIGHT_TOP
4107 * @tc.expected: result x offset add position offset and y offset is add position offset
4108 */
4109 menuLayoutAlgorithm->placement_ = Placement::RIGHT_TOP;
4110 result = menuLayoutAlgorithm->AddOffset(OffsetF(OFFSET_FIRST, OFFSET_FIRST));
4111 EXPECT_EQ(result, OffsetF(OFFSET_FIRST + OFFSET_SECOND, OFFSET_FIRST + OFFSET_SECOND));
4112
4113 /**
4114 * @tc.steps: step4. create menuLayoutAlgorithm, placement is LEFT_TOP
4115 * @tc.expected: result x offset reduce position offset and y offset is add position offset
4116 */
4117 menuLayoutAlgorithm->placement_ = Placement::LEFT_TOP;
4118 result = menuLayoutAlgorithm->AddOffset(OffsetF(OFFSET_FIRST, OFFSET_FIRST));
4119 EXPECT_EQ(result, OffsetF(OFFSET_FIRST - OFFSET_SECOND, OFFSET_FIRST + OFFSET_SECOND));
4120
4121 /**
4122 * @tc.steps: step5. create menuLayoutAlgorithm, placement is NONE
4123 * @tc.expected: the offset of the result is consistent with when the placement is BOTTOM_LEFT
4124 */
4125 menuLayoutAlgorithm->placement_ = Placement::NONE;
4126 result = menuLayoutAlgorithm->AddOffset(OffsetF(OFFSET_FIRST, OFFSET_FIRST));
4127 EXPECT_EQ(result, OffsetF(OFFSET_FIRST + OFFSET_SECOND, OFFSET_FIRST + OFFSET_SECOND));
4128 }
4129
4130 /**
4131 * @tc.name: MenuLayoutAlgorithmTestNg021
4132 * @tc.desc: Verify GetPositionWithPlacementTop.
4133 * @tc.type: FUNC
4134 */
4135 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg021, TestSize.Level1)
4136 {
4137 /**
4138 * @tc.steps: step1. create menuLayoutAlgorithm, set childSize, topPosition, bottomPosition
4139 * @tc.expected: step1. position is topPosition
4140 */
4141 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
4142 SizeF childSize(CHILD_SIZE_X, CHILD_SIZE_Y);
4143 OffsetF topPosition(TOP_POSITION_X, TOP_POSITION_Y);
4144 OffsetF bottomPosition(BOTTOM_POSITION_X, BOTTOM_POSITION_Y);
4145 auto result = menuLayoutAlgorithm->GetPositionWithPlacementTop(childSize, topPosition, bottomPosition);
4146 EXPECT_EQ(result.GetX(), TOP_POSITION_X);
4147 EXPECT_EQ(result.GetY(), TOP_POSITION_Y);
4148 }
4149
4150 /**
4151 * @tc.name: MenuLayoutAlgorithmTestNg022
4152 * @tc.desc: Verify GetPositionWithPlacementTopLeft.
4153 * @tc.type: FUNC
4154 */
4155 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg022, TestSize.Level1)
4156 {
4157 /**
4158 * @tc.steps: step1. create menuLayoutAlgorithm, set childSize, topPosition, bottomPosition and targetOffset
4159 * @tc.expected: step1. position is (targetOffset_.GetX(), targetOffset_.GetY() - childSize.Height())
4160 */
4161 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
4162 SizeF childSize(CHILD_SIZE_X, CHILD_SIZE_Y);
4163 OffsetF topPosition(TOP_POSITION_X, TOP_POSITION_Y);
4164 OffsetF bottomPosition(BOTTOM_POSITION_X, BOTTOM_POSITION_Y);
4165 OffsetF offset(OFFSET_X_THIRD, OFFSET_Y_THIRD);
4166 menuLayoutAlgorithm->targetOffset_ = offset;
4167 auto result = menuLayoutAlgorithm->GetPositionWithPlacementTopLeft(childSize, topPosition, bottomPosition);
4168 EXPECT_EQ(result.GetX(), TOP_LEFT_X);
4169 EXPECT_EQ(result.GetY(), TOP_LEFT_Y);
4170 }
4171
4172 /**
4173 * @tc.name: MenuLayoutAlgorithmTestNg023
4174 * @tc.desc: Verify GetPositionWithPlacementTopRight.
4175 * @tc.type: FUNC
4176 */
4177 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg023, TestSize.Level1)
4178 {
4179 /**
4180 * @tc.steps: step1. create menuLayoutAlgorithm, set childSize, topPosition, bottomPosition and targetOffset
4181 * @tc.expected: step1. position is (targetOffset_.GetX() + targetSize_.Width() - childSize.Width(),
4182 * targetOffset_.GetY() - childSize.Height())
4183 */
4184 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
4185 SizeF childSize(CHILD_SIZE_X, CHILD_SIZE_Y);
4186 OffsetF topPosition(TOP_POSITION_X, TOP_POSITION_Y);
4187 OffsetF bottomPosition(BOTTOM_POSITION_X, BOTTOM_POSITION_Y);
4188 OffsetF offset(OFFSET_X_THIRD, OFFSET_Y_THIRD);
4189 menuLayoutAlgorithm->targetOffset_ = offset;
4190 SizeF size(SIZE_X_SECOND, SIZE_Y_SECOND);
4191 menuLayoutAlgorithm->targetSize_ = size;
4192 auto result = menuLayoutAlgorithm->GetPositionWithPlacementTopRight(childSize, topPosition, bottomPosition);
4193 EXPECT_EQ(result.GetX(), TOP_RIGHT_X);
4194 EXPECT_EQ(result.GetY(), TOP_RIGHT_Y);
4195 }
4196
4197 /**
4198 * @tc.name: MenuLayoutAlgorithmTestNg024
4199 * @tc.desc: Verify GetPositionWithPlacementBottom.
4200 * @tc.type: FUNC
4201 */
4202 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg024, TestSize.Level1)
4203 {
4204 /**
4205 * @tc.steps: step1. create menuLayoutAlgorithm, set childSize, topPosition, bottomPosition
4206 * @tc.expected: step1. position is bottomPosition
4207 */
4208 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
4209 SizeF childSize(CHILD_SIZE_X, CHILD_SIZE_Y);
4210 OffsetF topPosition(TOP_POSITION_X, TOP_POSITION_Y);
4211 OffsetF bottomPosition(BOTTOM_POSITION_X, BOTTOM_POSITION_Y);
4212 auto result = menuLayoutAlgorithm->GetPositionWithPlacementBottom(childSize, topPosition, bottomPosition);
4213 EXPECT_EQ(result.GetX(), BOTTOM_POSITION_X);
4214 EXPECT_EQ(result.GetY(), BOTTOM_POSITION_Y);
4215 }
4216
4217 /**
4218 * @tc.name: MenuLayoutAlgorithmTestNg025
4219 * @tc.desc: Verify GetPositionWithPlacementBottomLeft.
4220 * @tc.type: FUNC
4221 */
4222 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg025, TestSize.Level1)
4223 {
4224 /**
4225 * @tc.steps: step1. create menuLayoutAlgorithm, set childSize, topPosition, bottomPosition, targetOffset and
4226 * targetSize
4227 * @tc.expected: step1. position is (targetOffset_.GetX(), targetOffset_.GetY() + targetSize_.Height())
4228 */
4229 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
4230 SizeF childSize(CHILD_SIZE_X, CHILD_SIZE_Y);
4231 OffsetF topPosition(TOP_POSITION_X, TOP_POSITION_Y);
4232 OffsetF bottomPosition(BOTTOM_POSITION_X, BOTTOM_POSITION_Y);
4233 OffsetF offset(OFFSET_X_THIRD, OFFSET_Y_THIRD);
4234 menuLayoutAlgorithm->targetOffset_ = offset;
4235 SizeF size(SIZE_X_SECOND, SIZE_Y_SECOND);
4236 menuLayoutAlgorithm->targetSize_ = size;
4237 auto result = menuLayoutAlgorithm->GetPositionWithPlacementBottomLeft(childSize, topPosition, bottomPosition);
4238 EXPECT_EQ(result.GetX(), BOTTOM_LEFT_X);
4239 EXPECT_EQ(result.GetY(), BOTTOM_LEFT_Y);
4240 }
4241
4242 /**
4243 * @tc.name: MenuLayoutAlgorithmTestNg026
4244 * @tc.desc: Verify GetPositionWithPlacementBottomRight.
4245 * @tc.type: FUNC
4246 */
4247 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg026, TestSize.Level1)
4248 {
4249 /**
4250 * @tc.steps: step1. create menuLayoutAlgorithm, set childSize, topPosition, bottomPosition, targetOffset and
4251 * targetSize
4252 * @tc.expected: step1. position is (targetOffset_.GetX() + targetSize_.Width() - childSize.Width(),
4253 * targetOffset_.GetY() + targetSize_.Height())
4254 */
4255 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
4256 SizeF childSize(CHILD_SIZE_X, CHILD_SIZE_Y);
4257 OffsetF topPosition(TOP_POSITION_X, TOP_POSITION_Y);
4258 OffsetF bottomPosition(BOTTOM_POSITION_X, BOTTOM_POSITION_Y);
4259 OffsetF offset(OFFSET_X_THIRD, OFFSET_Y_THIRD);
4260 menuLayoutAlgorithm->targetOffset_ = offset;
4261 SizeF size(SIZE_X_SECOND, SIZE_Y_SECOND);
4262 menuLayoutAlgorithm->targetSize_ = size;
4263 auto result = menuLayoutAlgorithm->GetPositionWithPlacementBottomRight(childSize, topPosition, bottomPosition);
4264 EXPECT_EQ(result.GetX(), BOTTOM_RIGHT_X);
4265 EXPECT_EQ(result.GetY(), BOTTOM_RIGHT_Y);
4266 }
4267
4268 /**
4269 * @tc.name: MenuLayoutAlgorithmTestNg027
4270 * @tc.desc: Verify GetPositionWithPlacementLeft.
4271 * @tc.type: FUNC
4272 */
4273 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg027, TestSize.Level1)
4274 {
4275 /**
4276 * @tc.steps: step1. create menuLayoutAlgorithm, set childSize, topPosition, bottomPosition, targetOffset and
4277 * targetSize
4278 * @tc.expected: step1. position is (targetOffset_.GetX() - childSize.Width(),
4279 * targetOffset_.GetY() + targetSize_.Height() / 2.0 - childSize.Height() / 2.0)
4280 */
4281 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
4282 SizeF childSize(CHILD_SIZE_X, CHILD_SIZE_Y);
4283 OffsetF topPosition(TOP_POSITION_X, TOP_POSITION_Y);
4284 OffsetF bottomPosition(BOTTOM_POSITION_X, BOTTOM_POSITION_Y);
4285 OffsetF offset(OFFSET_X_THIRD, OFFSET_Y_THIRD);
4286 menuLayoutAlgorithm->targetOffset_ = offset;
4287 SizeF size(SIZE_X_SECOND, SIZE_Y_SECOND);
4288 menuLayoutAlgorithm->targetSize_ = size;
4289 auto result = menuLayoutAlgorithm->GetPositionWithPlacementLeft(childSize, topPosition, bottomPosition);
4290 EXPECT_EQ(result.GetX(), PLACEMENT_LEFT_X);
4291 EXPECT_EQ(result.GetY(), PLACEMENT_LEFT_Y);
4292 }
4293
4294 /**
4295 * @tc.name: MenuLayoutAlgorithmTestNg028
4296 * @tc.desc: Verify GetPositionWithPlacementLeftTop.
4297 * @tc.type: FUNC
4298 */
4299 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg028, TestSize.Level1)
4300 {
4301 /**
4302 * @tc.steps: step1. create menuLayoutAlgorithm, set childSize, topPosition, bottomPosition, targetOffset and
4303 * targetSize
4304 * @tc.expected: step1. position is (targetOffset_.GetX() - childSize.Width(),
4305 * targetOffset_.GetY())
4306 */
4307 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
4308 SizeF childSize(CHILD_SIZE_X, CHILD_SIZE_Y);
4309 OffsetF topPosition(TOP_POSITION_X, TOP_POSITION_Y);
4310 OffsetF bottomPosition(BOTTOM_POSITION_X, BOTTOM_POSITION_Y);
4311 OffsetF offset(OFFSET_X_THIRD, OFFSET_Y_THIRD);
4312 menuLayoutAlgorithm->targetOffset_ = offset;
4313 SizeF size(SIZE_X_SECOND, SIZE_Y_SECOND);
4314 menuLayoutAlgorithm->targetSize_ = size;
4315 auto result = menuLayoutAlgorithm->GetPositionWithPlacementLeftTop(childSize, topPosition, bottomPosition);
4316 EXPECT_EQ(result.GetX(), PLACEMENT_LEFT_TOP_X);
4317 EXPECT_EQ(result.GetY(), PLACEMENT_LEFT_TOP_Y);
4318 }
4319
4320 /**
4321 * @tc.name: MenuLayoutAlgorithmTestNg029
4322 * @tc.desc: Verify GetPositionWithPlacementLeftBottom.
4323 * @tc.type: FUNC
4324 */
4325 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg029, TestSize.Level1)
4326 {
4327 /**
4328 * @tc.steps: step1. create menuLayoutAlgorithm, set childSize, topPosition, bottomPosition, targetOffset and
4329 * targetSize
4330 * @tc.expected: step1. position is (targetOffset_.GetX() - childSize.Width(),
4331 * targetOffset_.GetY() + targetSize_.Height() - childSize.Height())
4332 */
4333 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
4334 SizeF childSize(CHILD_SIZE_X, CHILD_SIZE_Y);
4335 OffsetF topPosition(TOP_POSITION_X, TOP_POSITION_Y);
4336 OffsetF bottomPosition(BOTTOM_POSITION_X, BOTTOM_POSITION_Y);
4337 OffsetF offset(OFFSET_X_THIRD, OFFSET_Y_THIRD);
4338 menuLayoutAlgorithm->targetOffset_ = offset;
4339 SizeF size(SIZE_X_SECOND, SIZE_Y_SECOND);
4340 menuLayoutAlgorithm->targetSize_ = size;
4341 auto result = menuLayoutAlgorithm->GetPositionWithPlacementLeftBottom(childSize, topPosition, bottomPosition);
4342 EXPECT_EQ(result.GetX(), PLACEMENT_LEFT_BOTTOM_X);
4343 EXPECT_EQ(result.GetY(), PLACEMENT_LEFT_BOTTOM_Y);
4344 }
4345
4346 /**
4347 * @tc.name: MenuLayoutAlgorithmTestNg030
4348 * @tc.desc: Verify GetPositionWithPlacementRight.
4349 * @tc.type: FUNC
4350 */
4351 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg030, TestSize.Level1)
4352 {
4353 /**
4354 * @tc.steps: step1. create menuLayoutAlgorithm, set childSize, topPosition, bottomPosition, targetOffset and
4355 * targetSize
4356 * @tc.expected: step1. position is (targetOffset_.GetX() + targetSize_.Width(),
4357 * targetOffset_.GetY() + targetSize_.Height() / 2.0 - childSize.Height() / 2.0)
4358 */
4359 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
4360 SizeF childSize(CHILD_SIZE_X, CHILD_SIZE_Y);
4361 OffsetF topPosition(TOP_POSITION_X, TOP_POSITION_Y);
4362 OffsetF bottomPosition(BOTTOM_POSITION_X, BOTTOM_POSITION_Y);
4363 OffsetF offset(OFFSET_X_THIRD, OFFSET_Y_THIRD);
4364 menuLayoutAlgorithm->targetOffset_ = offset;
4365 SizeF size(SIZE_X_SECOND, SIZE_Y_SECOND);
4366 menuLayoutAlgorithm->targetSize_ = size;
4367 auto result = menuLayoutAlgorithm->GetPositionWithPlacementRight(childSize, topPosition, bottomPosition);
4368 EXPECT_EQ(result.GetX(), PLACEMENT_RIGHT_X);
4369 EXPECT_EQ(result.GetY(), PLACEMENT_RIGHT_Y);
4370 }
4371
4372 /**
4373 * @tc.name: MenuLayoutAlgorithmTestNg031
4374 * @tc.desc: Verify GetPositionWithPlacementRightTop.
4375 * @tc.type: FUNC
4376 */
4377 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg031, TestSize.Level1)
4378 {
4379 /**
4380 * @tc.steps: step1. create menuLayoutAlgorithm, set childSize, topPosition, bottomPosition, targetOffset and
4381 * targetSize
4382 * @tc.expected: step1. position is (targetOffset_.GetX() + targetSize_.Width(),
4383 * targetOffset_.GetY())
4384 */
4385 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
4386 SizeF childSize(CHILD_SIZE_X, CHILD_SIZE_Y);
4387 OffsetF topPosition(TOP_POSITION_X, TOP_POSITION_Y);
4388 OffsetF bottomPosition(BOTTOM_POSITION_X, BOTTOM_POSITION_Y);
4389 OffsetF offset(OFFSET_X_THIRD, OFFSET_Y_THIRD);
4390 menuLayoutAlgorithm->targetOffset_ = offset;
4391 SizeF size(SIZE_X_SECOND, SIZE_Y_SECOND);
4392 menuLayoutAlgorithm->targetSize_ = size;
4393 auto result = menuLayoutAlgorithm->GetPositionWithPlacementRightTop(childSize, topPosition, bottomPosition);
4394 EXPECT_EQ(result.GetX(), PLACEMENT_RIGHT_TOP_X);
4395 EXPECT_EQ(result.GetY(), PLACEMENT_RIGHT_TOP_Y);
4396 }
4397
4398 /**
4399 * @tc.name: MenuLayoutAlgorithmTestNg032
4400 * @tc.desc: Verify GetPositionWithPlacementRightBottom.
4401 * @tc.type: FUNC
4402 */
4403 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg032, TestSize.Level1)
4404 {
4405 /**
4406 * @tc.steps: step1. create menuLayoutAlgorithm, set childSize, topPosition, bottomPosition, targetOffset and
4407 * targetSize
4408 * @tc.expected: step1. position is (targetOffset_.GetX() + targetSize_.Width(),
4409 * targetOffset_.GetY() + targetSize_.Height() - childSize.Height())
4410 */
4411 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>();
4412 SizeF childSize(CHILD_SIZE_X, CHILD_SIZE_Y);
4413 OffsetF topPosition(TOP_POSITION_X, TOP_POSITION_Y);
4414 OffsetF bottomPosition(BOTTOM_POSITION_X, BOTTOM_POSITION_Y);
4415 OffsetF offset(OFFSET_X_THIRD, OFFSET_Y_THIRD);
4416 menuLayoutAlgorithm->targetOffset_ = offset;
4417 SizeF size(SIZE_X_SECOND, SIZE_Y_SECOND);
4418 menuLayoutAlgorithm->targetSize_ = size;
4419 auto result = menuLayoutAlgorithm->GetPositionWithPlacementRightBottom(childSize, topPosition, bottomPosition);
4420 EXPECT_EQ(result.GetX(), PLACEMENT_RIGHT_BOTTOM_X);
4421 EXPECT_EQ(result.GetY(), PLACEMENT_RIGHT_BOTTOM_Y);
4422 }
4423
4424 /**
4425 * @tc.name: MenuLayoutAlgorithmTestNg033
4426 * @tc.desc: Test SubMenu layout algorithm.
4427 * @tc.type: FUNC
4428 */
4429 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg033, TestSize.Level1)
4430 {
4431 // create parent menu item
4432 auto itemPattern = AceType::MakeRefPtr<MenuItemPattern>();
4433 auto item = AceType::MakeRefPtr<FrameNode>("MenuItem", -1, itemPattern);
4434 // set parent item size
4435 auto itemGeometryNode = item->GetGeometryNode();
4436 ASSERT_TRUE(itemGeometryNode);
4437 item->GetGeometryNode()->SetFrameSize(SizeF(MENU_ITEM_SIZE_WIDTH, MENU_ITEM_SIZE_HEIGHT));
4438
4439 // create submenu
4440 auto menuPattern = AceType::MakeRefPtr<MenuPattern>(-1, "", MenuType::SUB_MENU);
4441 auto subMenu = AceType::MakeRefPtr<FrameNode>("", -1, menuPattern);
4442 auto algorithm = AceType::DynamicCast<SubMenuLayoutAlgorithm>(menuPattern->CreateLayoutAlgorithm());
4443 ASSERT_TRUE(algorithm);
4444 auto geometryNode = AceType::MakeRefPtr<GeometryNode>();
4445 geometryNode->SetFrameSize(SizeF(MENU_SIZE_WIDTH, MENU_SIZE_HEIGHT));
4446 auto layoutProp = AceType::MakeRefPtr<MenuLayoutProperty>();
4447 auto* wrapper = new LayoutWrapperNode(subMenu, geometryNode, layoutProp);
4448 // link parent menu item and sub menu
4449 ASSERT_TRUE(menuPattern);
4450 menuPattern->SetParentMenuItem(item);
4451 item->GetGeometryNode()->SetFrameOffset(OffsetF(MENU_OFFSET_X, MENU_OFFSET_Y));
4452 algorithm->wrapperSize_ = SizeF(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT);
4453
4454 // @tc.cases: case1. sub menu show on the right side of item
4455 algorithm->position_ = OffsetF(MENU_OFFSET_X + MENU_ITEM_SIZE_WIDTH, MENU_OFFSET_Y);
4456 algorithm->Layout(wrapper);
4457
4458 EXPECT_EQ(wrapper->GetGeometryNode()->GetMarginFrameOffset().GetX(),
4459 MENU_ITEM_SIZE_WIDTH);
4460
4461 // @tc.cases: case2. sub menu show on the left side of item
4462 algorithm->position_ = OffsetF(FULL_SCREEN_WIDTH, MENU_OFFSET_Y);
4463 algorithm->Layout(wrapper);
4464
4465 EXPECT_EQ(wrapper->GetGeometryNode()->GetMarginFrameOffset().GetX(),
4466 MENU_ITEM_SIZE_WIDTH);
4467 EXPECT_EQ(wrapper->GetGeometryNode()->GetMarginFrameOffset().GetY(), 0);
4468 }
4469
4470 /**
4471 * @tc.name: MenuLayoutAlgorithmTestNg034
4472 * @tc.desc: Test MultiMenu measure algorithm.
4473 * @tc.type: FUNC
4474 */
4475 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg034, TestSize.Level1)
4476 {
4477 // set screen width for grid column
4478 ScreenSystemManager::GetInstance().SetWindowInfo(FULL_SCREEN_WIDTH, 1.0, 1.0);
4479 // create multi menu
4480 auto menuPattern = AceType::MakeRefPtr<MenuPattern>(-1, "", MenuType::MULTI_MENU);
4481 auto multiMenu = AceType::MakeRefPtr<FrameNode>("", -1, menuPattern);
4482 auto algorithm = AceType::MakeRefPtr<MultiMenuLayoutAlgorithm>();
4483 ASSERT_TRUE(algorithm);
4484 auto geometryNode = AceType::MakeRefPtr<GeometryNode>();
4485 auto layoutProp = AceType::MakeRefPtr<MenuLayoutProperty>();
4486 LayoutConstraintF parentLayoutConstraint;
4487 parentLayoutConstraint.maxSize = FULL_SCREEN_SIZE;
4488 parentLayoutConstraint.percentReference = FULL_SCREEN_SIZE;
4489 layoutProp->UpdateLayoutConstraint(parentLayoutConstraint);
4490 layoutProp->UpdateContentConstraint();
4491 auto* wrapper = new LayoutWrapperNode(multiMenu, geometryNode, layoutProp);
4492 // create menu item
4493 for (int32_t i = 0; i < 3; ++i) {
4494 auto itemPattern = AceType::MakeRefPtr<MenuItemPattern>();
4495 auto menuItem = AceType::MakeRefPtr<FrameNode>("", -1, itemPattern);
4496 auto itemGeoNode = AceType::MakeRefPtr<GeometryNode>();
4497 itemGeoNode->SetFrameSize(SizeF(MENU_ITEM_SIZE_WIDTH, MENU_ITEM_SIZE_HEIGHT));
4498 auto childWrapper = AceType::MakeRefPtr<LayoutWrapperNode>(menuItem, itemGeoNode, layoutProp);
4499 wrapper->AppendChild(childWrapper);
4500 }
4501
4502 algorithm->Measure(wrapper);
4503 // @tc.expected: menu content width = item width, height = sum(item height)
4504 auto expectedSize = SizeF(MENU_ITEM_SIZE_WIDTH, MENU_ITEM_SIZE_HEIGHT * 3);
4505 EXPECT_EQ(wrapper->GetGeometryNode()->GetContentSize(), expectedSize);
4506 }
4507
4508 /**
4509 * @tc.name: MenuLayoutAlgorithmTestNg035
4510 * @tc.desc: Test GetChildPosition
4511 * @tc.type: FUNC
4512 */
4513 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg035, TestSize.Level1)
4514 {
4515 /**
4516 * @tc.steps: step1. create menuLayoutAlgorithm
4517 * @tc.expected: menuLayoutAlgorithm is not null
4518 */
4519 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>(NODEID, "menu");
4520 ASSERT_NE(menuLayoutAlgorithm, nullptr);
4521 SizeF size(MENU_SIZE_WIDTH, MENU_SIZE_HEIGHT);
4522
4523 /**
4524 * @tc.steps: step2. the placement of menuLayoutAlgorithm is Placement::NONE
4525 * @tc.expected: GetChildPosition get result offset is menu defaultPositon
4526 */
4527 menuLayoutAlgorithm->placement_ = Placement::NONE;
4528 menuLayoutAlgorithm->targetSize_ = SizeF(TARGET_SIZE_WIDTH, TARGET_SIZE_HEIGHT);
4529 menuLayoutAlgorithm->targetOffset_ = OffsetF(POSITION_OFFSET, POSITION_OFFSET);
4530 auto resultOffset = menuLayoutAlgorithm->GetChildPosition(size, false);
4531 float expectOffsetX = MENU_SIZE_WIDTH / 2;
4532 float expectOffsetY = MENU_SIZE_HEIGHT / 2;
4533 EXPECT_EQ(resultOffset, OffsetF(expectOffsetX, expectOffsetY));
4534
4535 /**
4536 * @tc.steps: step3. the placement of menuLayoutAlgorithm is Placement::BOTTOM_LEFT
4537 * @tc.expected: GetChildPosition get result offset is adjust position
4538 */
4539 menuLayoutAlgorithm->placement_ = Placement::BOTTOM_LEFT;
4540 menuLayoutAlgorithm->targetSize_ = SizeF(TARGET_SIZE_WIDTH / 2, TARGET_SIZE_HEIGHT);
4541 menuLayoutAlgorithm->wrapperSize_ = SizeF(OFFSET_THIRD, OFFSET_FORTH);
4542 menuLayoutAlgorithm->targetOffset_ = OffsetF(OFFSET_SIXTH, 0.0f);
4543
4544 resultOffset = menuLayoutAlgorithm->GetChildPosition(size);
4545 EXPECT_EQ(resultOffset, OffsetF(OFFSET_FIFTH, POSITION_OFFSET + TARGET_SECURITY.ConvertToPx()));
4546 }
4547
4548 /**
4549 * @tc.name: MenuLayoutAlgorithmTestNg036
4550 * @tc.desc: Test FitToScreen
4551 * @tc.type: FUNC
4552 */
4553 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg036, TestSize.Level1)
4554 {
4555 /**
4556 * @tc.steps: step1. create menuLayoutAlgorithm
4557 * @tc.expected: menuLayoutAlgorithm is not null
4558 */
4559 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>(NODEID, "menu");
4560 ASSERT_NE(menuLayoutAlgorithm, nullptr);
4561 SizeF size(MENU_SIZE_WIDTH, MENU_SIZE_HEIGHT);
4562
4563 /**
4564 * @tc.steps: step2. the placement of menuLayoutAlgorithm is Placement::NONE and need arrow
4565 * @tc.expected: FitToScreen get result offset is (0.0f, 0.0f)
4566 */
4567 OffsetF position = OffsetF(POSITION_OFFSET, POSITION_OFFSET);
4568 menuLayoutAlgorithm->positionOffset_ = OffsetF(OFFSET_FIRST, OFFSET_FIRST);
4569 menuLayoutAlgorithm->arrowPlacement_ = Placement::NONE;
4570 auto resultOffset = menuLayoutAlgorithm->FitToScreen(position, size, true);
4571 EXPECT_EQ(resultOffset, OffsetF(0.0f, 0.0f));
4572 }
4573
4574 /**
4575 * @tc.name: MenuLayoutAlgorithmTestNg037
4576 * @tc.desc: Test CheckPosition
4577 * @tc.type: FUNC
4578 */
4579 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg037, TestSize.Level1)
4580 {
4581 /**
4582 * @tc.steps: step1. create menuLayoutAlgorithm
4583 * @tc.expected: menuLayoutAlgorithm is not null
4584 */
4585 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>(NODEID, "menu");
4586 ASSERT_NE(menuLayoutAlgorithm, nullptr);
4587 SizeF size(MENU_SIZE_WIDTH, MENU_SIZE_HEIGHT);
4588
4589 /**
4590 * @tc.steps: step2. the placement of menuLayoutAlgorithm is Placement::BOTTOM_LEFT and menu position is normal
4591 * @tc.expected: CheckPosition result is true
4592 */
4593 menuLayoutAlgorithm->placement_ = Placement::BOTTOM_LEFT;
4594 menuLayoutAlgorithm->targetSize_ = SizeF(TARGET_SIZE_WIDTH, TARGET_SIZE_HEIGHT);
4595 menuLayoutAlgorithm->wrapperSize_ = SizeF(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT);
4596 OffsetF position = OffsetF(POSITION_OFFSET, OFFSET_FORTH);
4597 menuLayoutAlgorithm->targetOffset_ = OffsetF(OFFSET_THIRD, OFFSET_THIRD);
4598 auto result = menuLayoutAlgorithm->CheckPosition(position, size);
4599 EXPECT_TRUE(result);
4600
4601 /**
4602 * @tc.steps: step3. the placement of menuLayoutAlgorithm is Placement::TOP_LEFT and menu position is normal
4603 * @tc.expected: CheckPosition result is true
4604 */
4605 position = OffsetF(POSITION_OFFSET, OFFSET_FIFTH);
4606 menuLayoutAlgorithm->placement_ = Placement::TOP_LEFT;
4607 result = menuLayoutAlgorithm->CheckPosition(position, size);
4608 EXPECT_TRUE(result);
4609
4610 /**
4611 * @tc.steps: step4. the placement of menuLayoutAlgorithm is Placement::LEFT_TOP and menu position is normal
4612 * @tc.expected: CheckPosition result is true
4613 */
4614 position = OffsetF(OFFSET_FIFTH, OFFSET_THIRD);
4615 menuLayoutAlgorithm->placement_ = Placement::LEFT_TOP;
4616 result = menuLayoutAlgorithm->CheckPosition(position, size);
4617 EXPECT_TRUE(result);
4618
4619 /**
4620 * @tc.steps: step5. the placement of menuLayoutAlgorithm is Placement::NONE
4621 * @tc.expected: CheckPosition result is false
4622 */
4623 menuLayoutAlgorithm->placement_ = Placement::NONE;
4624 result = menuLayoutAlgorithm->CheckPosition(position, size);
4625 EXPECT_FALSE(result);
4626
4627 /**
4628 * @tc.steps: step6. the placement of menuLayoutAlgorithm is Placement::BOTTOM_LEFT and menu position is not normal
4629 * @tc.expected: CheckPosition result is false
4630 */
4631 menuLayoutAlgorithm->placement_ = Placement::BOTTOM_LEFT;
4632 position = OffsetF(POSITION_OFFSET, FULL_SCREEN_HEIGHT);
4633 result = menuLayoutAlgorithm->CheckPosition(position, size);
4634 EXPECT_FALSE(result);
4635 }
4636
4637 /**
4638 * @tc.name: MenuLayoutAlgorithmTestNg038
4639 * @tc.desc: Test InitTargetSizeAndPosition
4640 * @tc.type: FUNC
4641 */
4642 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg038, TestSize.Level1)
4643 {
4644 /**
4645 * @tc.steps: step1. create menuLayoutAlgorithm and target is null
4646 * @tc.expected: menuLayoutAlgorithm is not null
4647 */
4648 RefPtr<MenuLayoutAlgorithm> menuLayoutAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>(NODEID, "menu");
4649 ASSERT_NE(menuLayoutAlgorithm, nullptr);
4650
4651 menuLayoutAlgorithm->InitTargetSizeAndPosition(nullptr, true);
4652 menuLayoutAlgorithm->targetNodeId_ = NODEID;
4653 menuLayoutAlgorithm->targetTag_ = "text";
__anon51d21a701202() 4654 auto target = FrameNode::GetOrCreateFrameNode("text", NODEID, []() { return AceType::MakeRefPtr<Pattern>(); });
4655 ASSERT_NE(target, nullptr);
4656
4657 /**
4658 * @tc.steps: step2. target is null but the geometry node of target is null
4659 */
4660 auto mockRenderContext = AceType::DynamicCast<MockRenderContext>(target->GetRenderContext());
4661 ASSERT_NE(mockRenderContext, nullptr);
4662 EXPECT_CALL(*mockRenderContext, GetPaintRectWithTransform()).WillRepeatedly(Return(RectF(0.0f, 0.0f, 0.0f, 0.0f)));
4663 menuLayoutAlgorithm->InitTargetSizeAndPosition(nullptr, true);
4664
4665 /**
4666 * @tc.steps: step3. layoutWrapper, target node and the geometry node of target is not null, isContextMenu is false
4667 * @tc.expected: targetOffset_ is OffsetF(0.0f, 0.0f)
4668 */
4669 std::vector<SelectParam> params;
4670 params.emplace_back(std::make_pair("MenuItem", "Icon"));
4671 auto frameNode = MenuView::Create(params, 1);
4672 ASSERT_NE(frameNode, nullptr);
4673 auto menuGeometryNode = AceType::MakeRefPtr<GeometryNode>();
4674 LayoutWrapperNode* layoutWrapper =
4675 new LayoutWrapperNode(frameNode, menuGeometryNode, frameNode->GetLayoutProperty());
4676 ASSERT_NE(layoutWrapper, nullptr);
4677 RefPtr<GeometryNode> geometryNode = AceType::MakeRefPtr<GeometryNode>();
4678 ASSERT_NE(geometryNode, nullptr);
4679 GeometryProperty geometryProperty;
4680 geometryProperty.rect_ = RectF(0.0f, 0.0f, 0.0f, 0.0f);
4681 geometryNode->frame_ = geometryProperty;
4682 target->geometryNode_ = geometryNode;
4683 menuLayoutAlgorithm->InitTargetSizeAndPosition(layoutWrapper, false);
4684 EXPECT_EQ(menuLayoutAlgorithm->targetOffset_, OffsetF(0.0f, 0.0f));
4685
4686 /**
4687 * @tc.steps: step4. layoutWrapper, target and the geometry node of target is not null, isContextMenu and
4688 * isContainerModal is true
4689 * @tc.expected: targetOffset_ is OffsetF(-5.0f, -38.0f)
4690 */
4691 MockPipelineBase::GetCurrent()->SetWindowModal(WindowModal::CONTAINER_MODAL);
4692 MockPipelineBase::GetCurrent()->windowManager_ = AceType::MakeRefPtr<WindowManager>();
4693 MockPipelineBase::GetCurrent()->windowManager_->SetWindowGetModeCallBack(
__anon51d21a701302() 4694 []() -> WindowMode { return WindowMode::WINDOW_MODE_FLOATING; });
4695
4696 menuLayoutAlgorithm->InitTargetSizeAndPosition(layoutWrapper, true);
4697 auto pipelineContext = menuLayoutAlgorithm->GetCurrentPipelineContext();
4698 auto windowGlobalRect = pipelineContext->GetDisplayWindowRectInfo();
4699 float windowsOffsetX = static_cast<float>(windowGlobalRect.GetOffset().GetX());
4700 float windowsOffsetY = static_cast<float>(windowGlobalRect.GetOffset().GetY());
4701 OffsetF offset = OffsetF(windowsOffsetX, windowsOffsetY);
4702 OffsetF offset2 = menuLayoutAlgorithm->GetMenuWrapperOffset(layoutWrapper);
4703 offset -= offset2;
4704 EXPECT_EQ(menuLayoutAlgorithm->targetOffset_, offset);
4705
4706 /**
4707 * @tc.steps: step5. layoutWrapper, target and the geometry node of target is not null, isContextMenu is false
4708 * @tc.expected: targetOffset_ is OffsetF(-5.0f, -38.0f)
4709 */
4710 menuLayoutAlgorithm->InitTargetSizeAndPosition(layoutWrapper, false);
4711 EXPECT_EQ(menuLayoutAlgorithm->targetOffset_,
4712 OffsetF(-static_cast<float>(CONTAINER_BORDER_WIDTH.ConvertToPx() + CONTENT_PADDING.ConvertToPx()),
4713 -static_cast<float>(CONTAINER_TITLE_HEIGHT.ConvertToPx() + CONTAINER_BORDER_WIDTH.ConvertToPx())));
4714 delete layoutWrapper;
4715 layoutWrapper = nullptr;
4716 }
4717
4718 /**
4719 * @tc.name: MenuLayoutAlgorithmTestNg039
4720 * @tc.desc: Test GetIfNeedArrow
4721 * @tc.type: FUNC
4722 */
4723 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg039, TestSize.Level1)
4724 {
4725 /**
4726 * @tc.steps: step1. create menuLayoutAlgorithm
4727 * @tc.expected: menuLayoutAlgorithm is not null
4728 */
4729 auto menuPattern = AceType::MakeRefPtr<MenuPattern>(NODEID, TEXT_TAG, MenuType::CONTEXT_MENU);
4730 auto contextMenu = AceType::MakeRefPtr<FrameNode>(MENU_TAG, -1, menuPattern);
4731 auto menuAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>(NODEID, TEXT_TAG);
4732 ASSERT_TRUE(menuAlgorithm);
4733 auto geometryNode = AceType::MakeRefPtr<GeometryNode>();
4734 auto layoutProp = AceType::MakeRefPtr<MenuLayoutProperty>();
4735 auto* layoutWrapper = new LayoutWrapperNode(contextMenu, geometryNode, layoutProp);
4736 const SizeF menuSize = SizeF(MENU_SIZE_WIDTH, MENU_SIZE_HEIGHT);
4737 /**
4738 * @tc.steps: step2. excute GetIfNeedArrow
4739 * @tc.expected: ifNeedArrow is as expected.
4740 */
4741 menuAlgorithm->GetPaintProperty(layoutWrapper)->UpdateEnableArrow(true);
4742 auto result = menuAlgorithm->GetIfNeedArrow(layoutWrapper, menuSize);
4743 EXPECT_FALSE(result);
4744 menuAlgorithm->placement_ = Placement::LEFT;
4745 result = menuAlgorithm->GetIfNeedArrow(layoutWrapper, menuSize);
4746 EXPECT_FALSE(result);
4747 }
4748
4749 /**
4750 * @tc.name: MenuLayoutAlgorithmTestNg040
4751 * @tc.desc: Test UpdatePropArrowOffset
4752 * @tc.type: FUNC
4753 */
4754 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg040, TestSize.Level1)
4755 {
4756 /**
4757 * @tc.steps: step1. create menuLayoutAlgorithm
4758 * @tc.expected: menuLayoutAlgorithm is not null
4759 */
4760 auto menuAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>(NODEID, TEXT_TAG);
4761 ASSERT_TRUE(menuAlgorithm);
4762 std::unordered_set<Placement> placements = { Placement::TOP, Placement::TOP_LEFT, Placement::TOP_RIGHT,
4763 Placement::BOTTOM, Placement::BOTTOM_LEFT, Placement::BOTTOM_RIGHT, Placement::LEFT, Placement::LEFT_TOP,
4764 Placement::LEFT_BOTTOM, Placement::RIGHT, Placement::RIGHT_TOP, Placement::RIGHT_BOTTOM };
4765 std::unordered_set<Placement> offsetCondition1 = { Placement::LEFT, Placement::RIGHT, Placement::TOP,
4766 Placement::BOTTOM };
4767 std::unordered_set<Placement> offsetCondition2 = { Placement::TOP_LEFT, Placement::BOTTOM_LEFT, Placement::LEFT_TOP,
4768 Placement::RIGHT_TOP };
4769 std::unordered_set<Placement> offsetCondition3 = { Placement::TOP_RIGHT, Placement::BOTTOM_RIGHT,
4770 Placement::LEFT_BOTTOM, Placement::RIGHT_BOTTOM };
4771 /**
4772 * @tc.steps: step2. input unit is px, excute UpdatePropArrowOffset
4773 * @tc.expected: propArrowOffset_ is as expected.
4774 */
4775 menuAlgorithm->propArrowOffset_ = Dimension(10, DimensionUnit::PX);
4776 menuAlgorithm->UpdatePropArrowOffset();
4777 EXPECT_EQ(menuAlgorithm->propArrowOffset_, Dimension(10, DimensionUnit::PX));
4778 /**
4779 * @tc.steps: step3. input unit is percent, excute UpdatePropArrowOffset
4780 * @tc.expected: propArrowOffset_ is as expected.
4781 */
4782 menuAlgorithm->propArrowOffset_ = Dimension(0.5, DimensionUnit::PERCENT);
4783 menuAlgorithm->UpdatePropArrowOffset();
4784 EXPECT_EQ(menuAlgorithm->propArrowOffset_, Dimension(0.5, DimensionUnit::PERCENT));
4785 /**
4786 * @tc.steps: step4. intput is invalid, excute UpdatePropArrowOffset
4787 * @tc.expected: propArrowOffset_ is as expected.
4788 */
4789 for (Placement placementValue : placements) {
4790 menuAlgorithm->propArrowOffset_ = Dimension(-1, DimensionUnit::PX);
4791 menuAlgorithm->arrowPlacement_ = placementValue;
4792 menuAlgorithm->UpdatePropArrowOffset();
4793 if (offsetCondition1.find(placementValue) != offsetCondition1.end()) {
4794 EXPECT_EQ(menuAlgorithm->propArrowOffset_, ARROW_HALF_PERCENT_VALUE);
4795 }
4796 if (offsetCondition2.find(placementValue) != offsetCondition2.end()) {
4797 EXPECT_EQ(menuAlgorithm->propArrowOffset_, ARROW_ZERO_PERCENT_VALUE);
4798 }
4799 if (offsetCondition3.find(placementValue) != offsetCondition3.end()) {
4800 EXPECT_EQ(menuAlgorithm->propArrowOffset_, ARROW_ONE_HUNDRED_PERCENT_VALUE);
4801 }
4802 }
4803 }
4804
4805 /**
4806 * @tc.name: MenuLayoutAlgorithmTestNg041
4807 * @tc.desc: Test GetArrowPositionWithPlacement
4808 * @tc.type: FUNC
4809 */
4810 HWTEST_F(MenuTestNg, MenuLayoutAlgorithmTestNg041, TestSize.Level1)
4811 {
4812 /**
4813 * @tc.steps: step1. create menuLayoutAlgorithm
4814 * @tc.expected: menuLayoutAlgorithm is not null
4815 */
4816 auto menuAlgorithm = AceType::MakeRefPtr<MenuLayoutAlgorithm>(NODEID, TEXT_TAG);
4817 ASSERT_TRUE(menuAlgorithm);
4818 const SizeF menuSize = SizeF(MENU_SIZE_WIDTH, MENU_SIZE_HEIGHT);
4819 std::unordered_set<Placement> placements = { Placement::TOP, Placement::TOP_LEFT, Placement::TOP_RIGHT,
4820 Placement::BOTTOM, Placement::BOTTOM_LEFT, Placement::BOTTOM_RIGHT, Placement::LEFT, Placement::LEFT_TOP,
4821 Placement::LEFT_BOTTOM, Placement::RIGHT, Placement::RIGHT_TOP, Placement::RIGHT_BOTTOM };
4822 std::unordered_set<Placement> positionCondition1 = { Placement::TOP, Placement::TOP_LEFT, Placement::TOP_RIGHT };
4823 std::unordered_set<Placement> positionCondition2 = { Placement::BOTTOM, Placement::BOTTOM_LEFT,
4824 Placement::BOTTOM_RIGHT };
4825 std::unordered_set<Placement> positionCondition3 = { Placement::LEFT, Placement::LEFT_TOP, Placement::LEFT_BOTTOM };
4826 std::unordered_set<Placement> positionCondition4 = { Placement::RIGHT, Placement::RIGHT_TOP,
4827 Placement::RIGHT_BOTTOM };
4828 /**
4829 * @tc.steps: step2. excute GetArrowPositionWithPlacement
4830 * @tc.expected: arrowPosition is as expected.
4831 */
4832 float arrowOffsetMax = 0.0f;
4833 for (Placement placementValue : placements) {
4834 if (menuAlgorithm->setHorizontal_.find(placementValue) != menuAlgorithm->setHorizontal_.end()) {
4835 arrowOffsetMax = menuSize.Height() - menuAlgorithm->menuRadius_ * 2 - menuAlgorithm->arrowWidth_;
4836 }
4837 if (menuAlgorithm->setVertical_.find(placementValue) != menuAlgorithm->setVertical_.end()) {
4838 arrowOffsetMax = menuSize.Width() - menuAlgorithm->menuRadius_ * 2 - menuAlgorithm->arrowWidth_;
4839 }
4840
4841 menuAlgorithm->propArrowOffset_ = Dimension(0.5, DimensionUnit::PX);
4842 menuAlgorithm->arrowPlacement_ = placementValue;
4843 auto result = menuAlgorithm->GetArrowPositionWithPlacement(menuSize);
4844 EXPECT_EQ(menuAlgorithm->propArrowOffset_, Dimension(0.5, DimensionUnit::PX));
4845 auto arrowOffsetValue = (menuAlgorithm->propArrowOffset_).ConvertToPx();
4846 EXPECT_EQ(menuAlgorithm->arrowOffset_, arrowOffsetValue);
4847 if (positionCondition1.find(placementValue) != positionCondition1.end()) {
4848 EXPECT_EQ(result, OffsetF(arrowOffsetValue, menuSize.Height() + ARROW_HIGHT.ConvertToPx()));
4849 }
4850 if (positionCondition2.find(placementValue) != positionCondition2.end()) {
4851 EXPECT_EQ(result, OffsetF(arrowOffsetValue, -ARROW_HIGHT.ConvertToPx()));
4852 }
4853 if (positionCondition3.find(placementValue) != positionCondition3.end()) {
4854 EXPECT_EQ(result, OffsetF(menuSize.Width() + ARROW_HIGHT.ConvertToPx(), arrowOffsetValue));
4855 }
4856 if (positionCondition4.find(placementValue) != positionCondition4.end()) {
4857 EXPECT_EQ(result, OffsetF(-ARROW_HIGHT.ConvertToPx(), arrowOffsetValue));
4858 }
4859 }
4860 }
4861
4862 /**
4863 * @tc.name: MenuItemGroupLayoutAlgorithmTestNg001
4864 * @tc.desc: Test MenuItemGroup measure algorithm.
4865 * @tc.type: FUNC
4866 */
4867 HWTEST_F(MenuTestNg, MenuItemGroupLayoutAlgorithmTestNg001, TestSize.Level1)
4868 {
4869 // create menu item group
4870 auto menuItemGroupPattern = AceType::MakeRefPtr<MenuItemGroupPattern>();
4871 auto menuItemGroup = FrameNode::CreateFrameNode(V2::MENU_ITEM_GROUP_ETS_TAG, -1, menuItemGroupPattern);
4872 auto algorithm = AceType::MakeRefPtr<MenuItemGroupLayoutAlgorithm>(-1, -1, 0);
4873 ASSERT_TRUE(algorithm);
4874 auto geometryNode = AceType::MakeRefPtr<GeometryNode>();
4875 auto layoutProp = AceType::MakeRefPtr<LayoutProperty>();
4876 auto* layoutWrapper = new LayoutWrapperNode(menuItemGroup, geometryNode, layoutProp);
4877
4878 LayoutConstraintF parentLayoutConstraint;
4879 parentLayoutConstraint.maxSize = FULL_SCREEN_SIZE;
4880 parentLayoutConstraint.percentReference = FULL_SCREEN_SIZE;
4881 auto props = layoutWrapper->GetLayoutProperty();
4882 props->UpdateLayoutConstraint(parentLayoutConstraint);
4883 props->UpdateContentConstraint();
4884 // create menu item
4885 for (int32_t i = 0; i < 3; ++i) {
4886 auto itemPattern = AceType::MakeRefPtr<MenuItemPattern>();
4887 auto menuItem = AceType::MakeRefPtr<FrameNode>("", -1, itemPattern);
4888 auto itemGeoNode = AceType::MakeRefPtr<GeometryNode>();
4889 itemGeoNode->SetFrameSize(SizeF(MENU_ITEM_SIZE_WIDTH, MENU_ITEM_SIZE_HEIGHT));
4890 auto childWrapper = AceType::MakeRefPtr<LayoutWrapperNode>(menuItem, itemGeoNode, layoutProp);
4891 layoutWrapper->AppendChild(childWrapper);
4892 }
4893 // set selectTheme to themeManager before using themeManager to get selectTheme
4894 // test measure
4895 algorithm->Measure(layoutWrapper);
4896 // @tc.expected: menu content width = item width, height = sum(item height)
4897 auto expectedSize = SizeF(MENU_ITEM_SIZE_WIDTH, MENU_ITEM_SIZE_HEIGHT * 3);
4898 EXPECT_EQ(layoutWrapper->GetGeometryNode()->GetFrameSize(), expectedSize);
4899
4900 // test layout
4901 algorithm->Layout(layoutWrapper);
4902 // @tc.expected: menu item offset y = item height * (index - 1)
4903 OffsetF offset;
4904 for (auto&& child : layoutWrapper->GetAllChildrenWithBuild()) {
4905 EXPECT_EQ(child->GetGeometryNode()->GetMarginFrameOffset(), offset);
4906 offset.AddY(MENU_SIZE_HEIGHT / 3);
4907 }
4908 }
4909
4910 /**
4911 * @tc.name: MenuLayoutPropertyTestNg001
4912 * @tc.desc: Verify GetPositionOffset.
4913 * @tc.type: FUNC
4914 */
4915 HWTEST_F(MenuTestNg, MenuLayoutPropertyTestNg001, TestSize.Level1)
4916 {
4917 MenuLayoutProperty property;
4918 EXPECT_FALSE(property.GetPositionOffset().has_value());
4919 property.UpdatePositionOffset(OffsetF(25.0f, 30.0f));
4920 ASSERT_TRUE(property.GetPositionOffset().has_value());
4921 EXPECT_EQ(property.GetPositionOffset().value(), OffsetF(25.0f, 30.0f));
4922 }
4923
4924 /**
4925 * @tc.name: MenuLayoutPropertyTestNg002
4926 * @tc.desc: Verify GetTitle.
4927 * @tc.type: FUNC
4928 */
4929 HWTEST_F(MenuTestNg, MenuLayoutPropertyTestNg002, TestSize.Level1)
4930 {
4931 MenuLayoutProperty property;
4932 EXPECT_FALSE(property.GetTitle().has_value());
4933 property.UpdateTitle("title");
4934 ASSERT_TRUE(property.GetTitle().has_value());
4935 EXPECT_EQ(property.GetTitle().value(), "title");
4936 }
4937
4938 /**
4939 * @tc.name: MenuLayoutPropertyTestNg003
4940 * @tc.desc: Verify GetFontSize.
4941 * @tc.type: FUNC
4942 */
4943 HWTEST_F(MenuTestNg, MenuLayoutPropertyTestNg003, TestSize.Level1)
4944 {
4945 MenuLayoutProperty property;
4946 EXPECT_FALSE(property.GetFontSize().has_value());
4947 property.UpdateFontSize(Dimension(25.0f));
4948 ASSERT_TRUE(property.GetFontSize().has_value());
4949 EXPECT_EQ(property.GetFontSize().value(), Dimension(25.0f));
4950 }
4951
4952 /**
4953 * @tc.name: MenuLayoutPropertyTestNg004
4954 * @tc.desc: Verify GetFontColor.
4955 * @tc.type: FUNC
4956 */
4957 HWTEST_F(MenuTestNg, MenuLayoutPropertyTestNg004, TestSize.Level1)
4958 {
4959 MenuLayoutProperty property;
4960 EXPECT_FALSE(property.GetFontColor().has_value());
4961 property.UpdateFontColor(Color::RED);
4962 ASSERT_TRUE(property.GetFontColor().has_value());
4963 EXPECT_EQ(property.GetFontColor().value(), Color::RED);
4964 }
4965
4966 /**
4967 * @tc.name: MenuLayoutPropertyTestNg005
4968 * @tc.desc: Verify GetFontWeight.
4969 * @tc.type: FUNC
4970 */
4971 HWTEST_F(MenuTestNg, MenuLayoutPropertyTestNg005, TestSize.Level1)
4972 {
4973 MenuLayoutProperty property;
4974 EXPECT_FALSE(property.GetFontWeight().has_value());
4975 property.UpdateFontWeight(FontWeight::BOLD);
4976 ASSERT_TRUE(property.GetFontWeight().has_value());
4977 EXPECT_EQ(property.GetFontWeight().value(), FontWeight::BOLD);
4978 }
4979
4980 /**
4981 * @tc.name: MenuLayoutPropertyTestNg006
4982 * @tc.desc: Verify GetMenuOffset.
4983 * @tc.type: FUNC
4984 */
4985 HWTEST_F(MenuTestNg, MenuLayoutPropertyTestNg006, TestSize.Level1)
4986 {
4987 MenuLayoutProperty property;
4988 EXPECT_FALSE(property.GetMenuOffset().has_value());
4989 property.UpdateMenuOffset(OffsetF(25.0f, 30.0f));
4990 ASSERT_TRUE(property.GetMenuOffset().has_value());
4991 EXPECT_EQ(property.GetMenuOffset().value(), OffsetF(25.0f, 30.0f));
4992 }
4993
4994 /**
4995 * @tc.name: MenuLayoutPropertyTestNg007
4996 * @tc.desc: Verify Reset.
4997 * @tc.type: FUNC
4998 */
4999 HWTEST_F(MenuTestNg, MenuLayoutPropertyTestNg007, TestSize.Level1)
5000 {
5001 MenuLayoutProperty property;
5002 property.UpdateMenuOffset(OffsetF(25.0f, 30.0f));
5003 property.UpdatePositionOffset(OffsetF(25.0f, 30.0f));
5004 property.UpdateTitle("title");
5005 property.UpdateFontSize(Dimension(25.0f));
5006 property.UpdateFontColor(Color::RED);
5007 property.UpdateFontWeight(FontWeight::BOLD);
5008 property.UpdateAlignType(MenuAlignType::START);
5009 property.UpdateOffset(DimensionOffset(Dimension(0, DimensionUnit::VP), Dimension(0, DimensionUnit::VP)));
5010 EXPECT_TRUE(property.GetMenuOffset().has_value());
5011 EXPECT_TRUE(property.GetPositionOffset().has_value());
5012 EXPECT_TRUE(property.GetTitle().has_value());
5013 EXPECT_TRUE(property.GetFontSize().has_value());
5014 EXPECT_TRUE(property.GetFontColor().has_value());
5015 EXPECT_TRUE(property.GetFontWeight().has_value());
5016 EXPECT_TRUE(property.GetAlignType().has_value());
5017 EXPECT_TRUE(property.GetOffset().has_value());
5018 property.Reset();
5019 EXPECT_FALSE(property.GetMenuOffset().has_value());
5020 EXPECT_FALSE(property.GetPositionOffset().has_value());
5021 EXPECT_FALSE(property.GetTitle().has_value());
5022 EXPECT_FALSE(property.GetFontSize().has_value());
5023 EXPECT_FALSE(property.GetFontColor().has_value());
5024 EXPECT_FALSE(property.GetFontWeight().has_value());
5025 EXPECT_FALSE(property.GetAlignType().has_value());
5026 EXPECT_FALSE(property.GetOffset().has_value());
5027 }
5028
5029 /**
5030 * @tc.name: MenuLayoutPropertyTestNg008
5031 * @tc.desc: Verify Clone.
5032 * @tc.type: FUNC
5033 */
5034 HWTEST_F(MenuTestNg, MenuLayoutPropertyTestNg008, TestSize.Level1)
5035 {
5036 MenuLayoutProperty property;
5037 property.UpdateMenuOffset(OffsetF(25.0f, 30.0f));
5038 property.UpdatePositionOffset(OffsetF(25.0f, 30.0f));
5039 property.UpdateTitle("title");
5040 property.UpdateFontSize(Dimension(25.0f));
5041 property.UpdateFontColor(Color::RED);
5042 property.UpdateFontWeight(FontWeight::BOLD);
5043 property.UpdateAlignType(MenuAlignType::START);
5044 property.UpdateOffset(DimensionOffset(Dimension(10.0, DimensionUnit::VP), Dimension(10.0, DimensionUnit::VP)));
5045
5046 auto cloneProperty = AceType::DynamicCast<MenuLayoutProperty>(property.Clone());
5047 ASSERT_NE(cloneProperty, nullptr);
5048 EXPECT_EQ(property.GetMenuOffset().value(), cloneProperty->GetMenuOffset().value());
5049 EXPECT_EQ(property.GetPositionOffset().value(), cloneProperty->GetPositionOffset().value());
5050 EXPECT_EQ(property.GetTitle().value(), cloneProperty->GetTitle().value());
5051 EXPECT_EQ(property.GetFontSize().value(), cloneProperty->GetFontSize().value());
5052 EXPECT_EQ(property.GetFontColor().value(), cloneProperty->GetFontColor().value());
5053 EXPECT_EQ(property.GetFontWeight().value(), cloneProperty->GetFontWeight().value());
5054 EXPECT_EQ(property.GetAlignType().value(), cloneProperty->GetAlignType().value());
5055 EXPECT_EQ(property.GetOffset().value(), cloneProperty->GetOffset().value());
5056 }
5057
5058 /**
5059 * @tc.name: MenuLayoutPropertyTestNg009
5060 * @tc.desc: Verify ToJsonValue.
5061 * @tc.type: FUNC
5062 */
5063 HWTEST_F(MenuTestNg, MenuLayoutPropertyTestNg009, TestSize.Level1)
5064 {
5065 MenuLayoutProperty property;
5066 property.UpdateMenuOffset(OffsetF(25.0f, 30.0f));
5067 property.UpdatePositionOffset(OffsetF(25.0f, 30.0f));
5068 property.UpdateTitle("title");
5069 property.UpdateFontSize(Dimension(25.0f));
5070 property.UpdateFontColor(Color::RED);
5071 property.UpdateFontWeight(FontWeight::BOLD);
5072
5073 auto json = JsonUtil::Create(true);
5074 property.ToJsonValue(json);
5075 auto fontJsonObject = json->GetObject("font");
5076 EXPECT_EQ(json->GetString("title"), "title");
5077 EXPECT_EQ(json->GetString("offset"), OffsetF(25.0f, 30.0f).ToString());
5078 EXPECT_EQ(json->GetString("fontSize"), Dimension(25.0f).ToString());
5079 EXPECT_EQ(json->GetString("fontColor"), Color::RED.ColorToString());
5080 EXPECT_EQ(fontJsonObject->GetString("weight"), V2::ConvertWrapFontWeightToStirng(FontWeight::BOLD));
5081 }
5082
5083 /**
5084 * @tc.name: MenuLayoutPropertyTestNg010
5085 * @tc.desc: Verify ToJsonValue.
5086 * @tc.type: FUNC
5087 */
5088 HWTEST_F(MenuTestNg, MenuLayoutPropertyTestNg010, TestSize.Level1)
5089 {
5090 std::vector<OptionParam> optionParams;
5091 optionParams.emplace_back("MenuItem1", "fakeIcon", nullptr);
5092 optionParams.emplace_back("MenuItem2", "", nullptr);
5093 MenuParam menuParam;
5094 auto menuWrapperNode = MenuView::Create(std::move(optionParams), TARGET_ID, "", TYPE, menuParam);
5095 ASSERT_NE(menuWrapperNode, nullptr);
5096 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
5097 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
5098 ASSERT_NE(menuNode, nullptr);
5099 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
5100 ASSERT_NE(property, nullptr);
5101
5102 auto json = JsonUtil::Create(true);
5103 property->ToJsonValue(json);
5104 auto itemArray = json->GetValue("bindMenu");
5105 ASSERT_EQ(itemArray->GetArraySize(), 2u);
5106 auto item1 = itemArray->GetArrayItem(0);
5107 EXPECT_EQ(item1->GetString("value"), "MenuItem1");
5108 EXPECT_EQ(item1->GetString("icon"), "fakeIcon");
5109 auto item2 = itemArray->GetArrayItem(1);
5110 EXPECT_EQ(item2->GetString("value"), "MenuItem2");
5111 EXPECT_EQ(item2->GetString("icon"), "");
5112 }
5113
5114 /**
5115 * @tc.name: MenuLayoutPropertyTestNg011
5116 * @tc.desc: Verify SelectMenuAlignOption AlignType.
5117 * @tc.type: FUNC
5118 */
5119 HWTEST_F(MenuTestNg, MenuLayoutPropertyTestNg011, TestSize.Level1)
5120 {
5121 MenuLayoutProperty property;
5122 EXPECT_FALSE(property.GetAlignType().has_value());
5123 /**
5124 * @tc.cases: case1. verify the alignType property.
5125 */
5126 property.UpdateAlignType(MenuAlignType::CENTER);
5127 ASSERT_TRUE(property.GetAlignType().has_value());
5128 EXPECT_EQ(property.GetAlignType().value(), MenuAlignType::CENTER);
5129 }
5130
5131 /**
5132 * @tc.name: MenuLayoutPropertyTestNg012
5133 * @tc.desc: Verify SelectMenuAlignOption Offset.
5134 * @tc.type: FUNC
5135 */
5136 HWTEST_F(MenuTestNg, MenuLayoutPropertyTestNg012, TestSize.Level1)
5137 {
5138 MenuLayoutProperty property;
5139 EXPECT_FALSE(property.GetOffset().has_value());
5140 DimensionOffset offset(Dimension(MENU_OFFSET_X, DimensionUnit::VP), Dimension(MENU_OFFSET_Y, DimensionUnit::VP));
5141 /**
5142 * @tc.cases: case1. verify the offset property.
5143 */
5144 property.UpdateOffset(offset);
5145 ASSERT_TRUE(property.GetOffset().has_value());
5146 EXPECT_EQ(property.GetOffset().value(), offset);
5147 }
5148
5149 /**
5150 * @tc.name: MenuViewTestNgCreate001
5151 * @tc.desc: Verify Create.
5152 * @tc.type: FUNC
5153 */
5154 HWTEST_F(MenuTestNg, MenuViewTestNgCreate001, TestSize.Level1)
5155 {
5156 std::vector<OptionParam> optionParams;
5157 optionParams.emplace_back("MenuItem1", "fakeIcon", nullptr);
5158 optionParams.emplace_back("MenuItem2", "", nullptr);
5159 MenuParam menuParam;
5160 auto menuWrapperNode = MenuView::Create(std::move(optionParams), TARGET_ID, "", TYPE, menuParam);
5161 ASSERT_NE(menuWrapperNode, nullptr);
5162 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
5163 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
5164 ASSERT_NE(menuNode, nullptr);
5165 auto menuPattern = menuNode->GetPattern<MenuPattern>();
5166 ASSERT_NE(menuPattern, nullptr);
5167 ASSERT_EQ(menuPattern->GetOptions().size(), 2);
5168
5169 auto firstOption = menuPattern->GetOptions()[0];
5170 ASSERT_NE(firstOption, nullptr);
5171 EXPECT_EQ(firstOption->GetTag(), V2::OPTION_ETS_TAG);
5172 auto firstPattern = firstOption->GetPattern<OptionPattern>();
5173 ASSERT_NE(firstPattern, nullptr);
5174 EXPECT_EQ(firstPattern->GetText(), "MenuItem1");
5175 EXPECT_EQ(firstPattern->GetIcon(), "fakeIcon");
5176 auto secondOption = menuPattern->GetOptions()[1];
5177 ASSERT_NE(secondOption, nullptr);
5178 EXPECT_EQ(secondOption->GetTag(), V2::OPTION_ETS_TAG);
5179 auto secondPattern = secondOption->GetPattern<OptionPattern>();
5180 ASSERT_NE(secondPattern, nullptr);
5181 EXPECT_EQ(secondPattern->GetText(), "MenuItem2");
5182 EXPECT_EQ(secondPattern->GetIcon(), "");
5183 }
5184
5185 /**
5186 * @tc.name: MenuViewTestNgCreate002
5187 * @tc.desc: Verify Create.
5188 * @tc.type: FUNC
5189 */
5190 HWTEST_F(MenuTestNg, MenuViewTestNgCreate002, TestSize.Level1)
5191 {
5192 std::vector<OptionParam> optionParams;
5193 optionParams.emplace_back("MenuItem1", "fakeIcon", nullptr);
5194 optionParams.emplace_back("MenuItem2", "", nullptr);
5195 MenuParam menuParam;
5196 menuParam.title = "Title";
5197 auto menuWrapperNode = MenuView::Create(std::move(optionParams), TARGET_ID, "", TYPE, menuParam);
5198 ASSERT_NE(menuWrapperNode, nullptr);
5199 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
5200 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
5201 ASSERT_NE(menuNode, nullptr);
5202 auto menuPattern = menuNode->GetPattern<MenuPattern>();
5203 ASSERT_NE(menuPattern, nullptr);
5204 menuPattern->OnModifyDone();
5205 auto column = menuPattern->GetMenuColumn();
5206 ASSERT_NE(column, nullptr);
5207 auto children = column->GetChildren();
5208 ASSERT_EQ(children.size(), 3);
5209 auto titleChild = AceType::DynamicCast<FrameNode>(column->GetChildAtIndex(0));
5210 ASSERT_NE(titleChild, nullptr);
5211 EXPECT_EQ(titleChild->GetTag(), V2::TEXT_ETS_TAG);
5212 auto textProperty = titleChild->GetLayoutProperty<TextLayoutProperty>();
5213 ASSERT_NE(textProperty, nullptr);
5214 EXPECT_TRUE(textProperty->GetContent().has_value());
5215 EXPECT_EQ(textProperty->GetContent().value(), "Title");
5216 }
5217
5218 /**
5219 * @tc.name: MenuViewTestNgCreate003
5220 * @tc.desc: Verify Create.
5221 * @tc.type: FUNC
5222 */
5223 HWTEST_F(MenuTestNg, MenuViewTestNgCreate003, TestSize.Level1)
5224 {
5225 auto textNode = FrameNode::CreateFrameNode(
5226 V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<TextPattern>());
5227 ASSERT_NE(textNode, nullptr);
5228 MenuParam menuParam;
5229 menuParam.positionOffset = { 10.0f, 10.0f };
5230 auto menuWrapperNode = MenuView::Create(textNode, TARGET_ID, "", TYPE, menuParam);
5231 ASSERT_NE(menuWrapperNode, nullptr);
5232 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
5233 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
5234 ASSERT_NE(menuNode, nullptr);
5235 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
5236 ASSERT_NE(property, nullptr);
5237 EXPECT_TRUE(property->GetPositionOffset().has_value());
5238 EXPECT_EQ(property->GetPositionOffset().value(), OffsetF(10.0f, 10.0f));
5239 }
5240
5241 /**
5242 * @tc.name: MenuViewTestNgCreate004
5243 * @tc.desc: Verify Create SubMenu.
5244 * @tc.type: FUNC
5245 */
5246 HWTEST_F(MenuTestNg, MenuViewTestNgCreate004, TestSize.Level1)
5247 {
5248 /**
5249 * @tc.steps: step1: create custom node and menu node
5250 * @tc.expected: menuNode not null
5251 */
5252 auto textNode = FrameNode::CreateFrameNode(
5253 V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<TextPattern>());
5254 ASSERT_NE(textNode, nullptr);
5255 MenuParam menuParam;
5256 menuParam.positionOffset = { 10.0f, 10.0f };
5257 auto menuNode = MenuView::Create(textNode, TARGET_ID, "", MenuType::SUB_MENU, menuParam);
5258 ASSERT_NE(menuNode, nullptr);
5259 /**
5260 * @tc.steps: step2: get menuNode layoutProperty
5261 * @tc.expected: layoutProperty as expected
5262 */
5263 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
5264 ASSERT_NE(property, nullptr);
5265 EXPECT_TRUE(property->GetPositionOffset().has_value());
5266 EXPECT_EQ(property->GetPositionOffset().value(), OffsetF(10.0f, 10.0f));
5267 }
5268
5269 /**
5270 * @tc.name: MenuViewTestNgSetFontSize001
5271 * @tc.desc: Verify SetFontSize.
5272 * @tc.type: FUNC
5273 */
5274 HWTEST_F(MenuTestNg, MenuViewTestNgSetFontSize001, TestSize.Level1)
5275 {
5276 MenuModelNG MneuModelInstance;
5277 std::vector<OptionParam> optionParams;
5278 optionParams.emplace_back("MenuItem", "", nullptr);
5279 MenuParam menuParam;
5280 auto menuWrapperNode = MenuView::Create(std::move(optionParams), TARGET_ID, "", TYPE, menuParam);
5281 ASSERT_NE(menuWrapperNode, nullptr);
5282 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
5283 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
5284 ASSERT_NE(menuNode, nullptr);
5285 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
5286 ASSERT_NE(property, nullptr);
5287 ViewStackProcessor::GetInstance()->Push(menuNode);
5288 MneuModelInstance.SetFontSize(Dimension(50.0));
5289 ASSERT_TRUE(property->GetFontSize().has_value());
5290 EXPECT_EQ(property->GetFontSize().value(), Dimension(50.0));
5291 ViewStackProcessor::GetInstance()->Finish();
5292 }
5293
5294 /**
5295 * @tc.name: MenuViewTestNgSetFontSize002
5296 * @tc.desc: Verify SetFontSize.
5297 * @tc.type: FUNC
5298 */
5299 HWTEST_F(MenuTestNg, MenuViewTestNgSetFontSize002, TestSize.Level1)
5300 {
5301 MenuModelNG MneuModelInstance;
5302 std::vector<OptionParam> optionParams;
5303 optionParams.emplace_back("MenuItem", "", nullptr);
5304 MenuParam menuParam;
5305 auto menuWrapperNode = MenuView::Create(std::move(optionParams), TARGET_ID, "", TYPE, menuParam);
5306 ASSERT_NE(menuWrapperNode, nullptr);
5307 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
5308 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
5309 ASSERT_NE(menuNode, nullptr);
5310 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
5311 ASSERT_NE(property, nullptr);
5312 ViewStackProcessor::GetInstance()->Push(menuNode);
5313 MneuModelInstance.SetFontSize(Dimension());
5314 EXPECT_FALSE(property->GetFontSize().has_value());
5315 ViewStackProcessor::GetInstance()->Finish();
5316 }
5317
5318 /**
5319 * @tc.name: MenuViewTestNgSetFontSize003
5320 * @tc.desc: Verify SetFontSize.
5321 * @tc.type: FUNC
5322 */
5323 HWTEST_F(MenuTestNg, MenuViewTestNgSetFontSize003, TestSize.Level1)
5324 {
5325 MenuModelNG MneuModelInstance;
5326 std::vector<OptionParam> optionParams;
5327 optionParams.emplace_back("MenuItem", "", nullptr);
5328 MenuParam menuParam;
5329 auto menuWrapperNode = MenuView::Create(std::move(optionParams), TARGET_ID, "", TYPE, menuParam);
5330 ASSERT_NE(menuWrapperNode, nullptr);
5331 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
5332 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
5333 ASSERT_NE(menuNode, nullptr);
5334 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
5335 ASSERT_NE(property, nullptr);
5336 MneuModelInstance.SetFontSize(Dimension(50.0));
5337 ASSERT_FALSE(property->GetFontSize().has_value());
5338 }
5339
5340 /**
5341 * @tc.name: MenuViewTestNgSetFontColor001
5342 * @tc.desc: Verify SetFontColor.
5343 * @tc.type: FUNC
5344 */
5345 HWTEST_F(MenuTestNg, MenuViewTestNgSetFontColor001, TestSize.Level1)
5346 {
5347 MenuModelNG MneuModelInstance;
5348 std::vector<OptionParam> optionParams;
5349 optionParams.emplace_back("MenuItem", "", nullptr);
5350 MenuParam menuParam;
5351 auto menuWrapperNode = MenuView::Create(std::move(optionParams), TARGET_ID, "", TYPE, menuParam);
5352 ASSERT_NE(menuWrapperNode, nullptr);
5353 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
5354 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
5355 ASSERT_NE(menuNode, nullptr);
5356 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
5357 ASSERT_NE(property, nullptr);
5358 ViewStackProcessor::GetInstance()->Push(menuNode);
5359 MneuModelInstance.SetFontColor(Color::RED);
5360 ASSERT_TRUE(property->GetFontColor().has_value());
5361 EXPECT_EQ(property->GetFontColor().value(), Color::RED);
5362 ViewStackProcessor::GetInstance()->Finish();
5363 }
5364
5365 /**
5366 * @tc.name: MenuViewTestNgSetFontColor002
5367 * @tc.desc: Verify SetFontColor.
5368 * @tc.type: FUNC
5369 */
5370 HWTEST_F(MenuTestNg, MenuViewTestNgSetFontColor002, TestSize.Level1)
5371 {
5372 MenuModelNG MneuModelInstance;
5373 std::vector<OptionParam> optionParams;
5374 optionParams.emplace_back("MenuItem", "", nullptr);
5375 MenuParam menuParam;
5376 auto menuWrapperNode = MenuView::Create(std::move(optionParams), TARGET_ID, "", TYPE, menuParam);
5377 ASSERT_NE(menuWrapperNode, nullptr);
5378 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
5379 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
5380 ASSERT_NE(menuNode, nullptr);
5381 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
5382 ASSERT_NE(property, nullptr);
5383 MneuModelInstance.SetFontColor(Color::RED);
5384 ASSERT_FALSE(property->GetFontColor().has_value());
5385 }
5386
5387 /**
5388 * @tc.name: MenuViewTestNgSetFontColor003
5389 * @tc.desc: Verify SetFontColor.
5390 * @tc.type: FUNC
5391 */
5392 HWTEST_F(MenuTestNg, MenuViewTestNgSetFontColor003, TestSize.Level1)
5393 {
5394 MenuModelNG MneuModelInstance;
5395 std::vector<OptionParam> optionParams;
5396 optionParams.emplace_back("MenuItem", "", nullptr);
5397 MenuParam menuParam;
5398 auto menuWrapperNode = MenuView::Create(std::move(optionParams), TARGET_ID, "", TYPE, menuParam);
5399 ASSERT_NE(menuWrapperNode, nullptr);
5400 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
5401 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
5402 ASSERT_NE(menuNode, nullptr);
5403 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
5404 ASSERT_NE(property, nullptr);
5405 ViewStackProcessor::GetInstance()->Push(menuNode);
5406 MneuModelInstance.SetFontColor(Color::RED);
5407 ASSERT_TRUE(property->GetFontColor().has_value());
5408 EXPECT_EQ(property->GetFontColor().value(), Color::RED);
5409 MneuModelInstance.SetFontColor(std::nullopt);
5410 ASSERT_FALSE(property->GetFontColor().has_value());
5411 ViewStackProcessor::GetInstance()->Finish();
5412 }
5413
5414 /**
5415 * @tc.name: MenuViewTestNgSetFontWeight001
5416 * @tc.desc: Verify SetFontWeight.
5417 * @tc.type: FUNC
5418 */
5419 HWTEST_F(MenuTestNg, MenuViewTestNgSetFontWeight001, TestSize.Level1)
5420 {
5421 MenuModelNG MneuModelInstance;
5422 std::vector<OptionParam> optionParams;
5423 optionParams.emplace_back("MenuItem", "", nullptr);
5424 MenuParam menuParam;
5425 auto menuWrapperNode = MenuView::Create(std::move(optionParams), TARGET_ID, "", TYPE, menuParam);
5426 ASSERT_NE(menuWrapperNode, nullptr);
5427 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
5428 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
5429 ASSERT_NE(menuNode, nullptr);
5430 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
5431 ASSERT_NE(property, nullptr);
5432 ViewStackProcessor::GetInstance()->Push(menuNode);
5433 MneuModelInstance.SetFontWeight(FontWeight::BOLDER);
5434 ASSERT_TRUE(property->GetFontWeight().has_value());
5435 EXPECT_EQ(property->GetFontWeight().value(), FontWeight::BOLDER);
5436 ViewStackProcessor::GetInstance()->Finish();
5437 }
5438
5439 /**
5440 * @tc.name: MenuViewTestNgSetFontWeight002
5441 * @tc.desc: Verify SetFontWeight.
5442 * @tc.type: FUNC
5443 */
5444 HWTEST_F(MenuTestNg, MenuViewTestNgSetFontWeight002, TestSize.Level1)
5445 {
5446 MenuModelNG MneuModelInstance;
5447 std::vector<OptionParam> optionParams;
5448 optionParams.emplace_back("MenuItem", "", nullptr);
5449 MenuParam menuParam;
5450 auto menuWrapperNode = MenuView::Create(std::move(optionParams), TARGET_ID, "", TYPE, menuParam);
5451 ASSERT_NE(menuWrapperNode, nullptr);
5452 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
5453 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
5454 ASSERT_NE(menuNode, nullptr);
5455 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
5456 ASSERT_NE(property, nullptr);
5457 MneuModelInstance.SetFontWeight(FontWeight::BOLDER);
5458 ASSERT_FALSE(property->GetFontWeight().has_value());
5459 }
5460
5461 /**
5462 * @tc.name: MenuViewTestNgSetMenuPlacement001
5463 * @tc.desc: Verify SetMenuPlacement.
5464 * @tc.type: FUNC
5465 */
5466 HWTEST_F(MenuTestNg, MenuViewTestNgSetMenuPlacement001, TestSize.Level1)
5467 {
5468 /**
5469 * @tc.steps: step1. create menuWrapperNode with menuItems and set MenuPlacement to Placement::TOP
5470 * @tc.expected: step1. Get menuPlacement is Placement::TOP
5471 */
5472 std::vector<OptionParam> optionParams;
5473 optionParams.emplace_back("MenuItem", "", nullptr);
5474 MenuParam menuParam;
5475 menuParam.placement = OHOS::Ace::Placement::TOP;
5476 auto menuWrapperNode = MenuView::Create(std::move(optionParams), TARGET_ID, "", TYPE, menuParam);
5477 ASSERT_NE(menuWrapperNode, nullptr);
5478 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
5479 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
5480 ASSERT_NE(menuNode, nullptr);
5481 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
5482 ASSERT_NE(property, nullptr);
5483 EXPECT_TRUE(property->GetMenuPlacement().has_value());
5484 EXPECT_EQ(property->GetMenuPlacement().value(), OHOS::Ace::Placement::TOP);
5485 }
5486
5487 /**
5488 * @tc.name: MenuViewTestNgSetMenuPlacement002
5489 * @tc.desc: Verify SetMenuPlacement.
5490 * @tc.type: FUNC
5491 */
5492 HWTEST_F(MenuTestNg, MenuViewTestNgSetMenuPlacement002, TestSize.Level1)
5493 {
5494 /**
5495 * @tc.steps: step1. create menuWrapperNode with custom node from a builder and set MenuPlacement to
5496 * Placement::BOTTOM
5497 * @tc.expected: step1. Get menuPlacement is Placement::BOTTOM
5498 */
5499 auto textNode = FrameNode::CreateFrameNode(
5500 V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<TextPattern>());
5501 ASSERT_NE(textNode, nullptr);
5502 MenuParam menuParam;
5503 menuParam.placement = OHOS::Ace::Placement::BOTTOM;
5504 auto menuWrapperNode = MenuView::Create(textNode, TARGET_ID, "", TYPE, menuParam);
5505 ASSERT_NE(menuWrapperNode, nullptr);
5506 ASSERT_EQ(menuWrapperNode->GetChildren().size(), 1);
5507 auto menuNode = AceType::DynamicCast<FrameNode>(menuWrapperNode->GetChildAtIndex(0));
5508 ASSERT_NE(menuNode, nullptr);
5509 auto property = menuNode->GetLayoutProperty<MenuLayoutProperty>();
5510 ASSERT_NE(property, nullptr);
5511 EXPECT_TRUE(property->GetMenuPlacement().has_value());
5512 EXPECT_EQ(property->GetMenuPlacement().value(), OHOS::Ace::Placement::BOTTOM);
5513 }
5514
5515 /**
5516 * @tc.name: MenuPaintMethodTestNg001
5517 * @tc.desc: Verify UpdateArrowPath.
5518 * @tc.type: FUNC
5519 */
5520 HWTEST_F(MenuTestNg, MenuPaintMethodTestNg001, TestSize.Level1)
5521 {
5522 /**
5523 * @tc.steps: step1. prepare paint method object.
5524 */
5525 RefPtr<MenuPaintProperty> paintProp = AceType::MakeRefPtr<MenuPaintProperty>();
5526 RefPtr<MenuPaintMethod> paintMethod = AceType::MakeRefPtr<MenuPaintMethod>();
5527 Testing::MockCanvas canvas;
5528 EXPECT_CALL(canvas, AttachBrush(_)).WillRepeatedly(ReturnRef(canvas));
5529 EXPECT_CALL(canvas, DrawPath(_)).Times(AtLeast(1));
5530 /**
5531 * @tc.steps: step2. update paint property and excute GetOverlayDrawFunction.
5532 * @tc.expected: return value are as expected.
5533 */
5534 paintProp->UpdateEnableArrow(true);
5535 paintProp->UpdateArrowPosition(OffsetF(10.0f, 10.0f));
5536 Placement placements[] = { Placement::TOP, Placement::BOTTOM, Placement::RIGHT, Placement::LEFT };
5537 for (Placement placementValue : placements) {
5538 paintProp->UpdateArrowPlacement(placementValue);
5539 PaintWrapper* paintWrapper = GetPaintWrapper(paintProp);
5540 auto result = paintMethod->GetOverlayDrawFunction(paintWrapper);
5541 EXPECT_NE(result, nullptr);
5542 result(canvas);
5543 delete paintWrapper;
5544 paintWrapper = nullptr;
5545 }
5546 /**
5547 * @tc.steps: step3. update enableArrow to false.
5548 * @tc.expected: return value are as expected.
5549 */
5550 paintProp->UpdateEnableArrow(false);
5551 PaintWrapper* paintWrapper = GetPaintWrapper(paintProp);
5552 auto result = paintMethod->GetOverlayDrawFunction(paintWrapper);
5553 EXPECT_NE(result, nullptr);
5554 result(canvas);
5555 delete paintWrapper;
5556 paintWrapper = nullptr;
5557 }
5558
5559 /**
5560 * @tc.name: MenuPaintMethodTestNg003
5561 * @tc.desc: Verify GetOverlayDrawFunction.
5562 * @tc.type: FUNC
5563 */
5564 HWTEST_F(MenuTestNg, MenuPaintMethodTestNg003, TestSize.Level1)
5565 {
5566 /**
5567 * @tc.steps: step1. prepare paint method object.
5568 */
5569 RefPtr<MenuPaintProperty> paintProp = AceType::MakeRefPtr<MenuPaintProperty>();
5570 RefPtr<MenuPaintMethod> paintMethod = AceType::MakeRefPtr<MenuPaintMethod>();
5571 PaintWrapper* paintWrapper = GetPaintWrapper(paintProp);
5572 /**
5573 * @tc.steps: step2. excute functions.
5574 * @tc.expected: return value are as expected.
5575 */
5576 auto arrowX = 0.0;
5577 auto arrowY = 0.0;
5578 RSPath path;
5579 Placement placements[] = { Placement::TOP, Placement::TOP_RIGHT, Placement::BOTTOM_RIGHT, Placement::RIGHT_TOP,
5580 Placement::LEFT_TOP, Placement::NONE };
5581 for (Placement placementValue : placements) {
5582 paintMethod->UpdateArrowPath(placementValue, arrowX, arrowY, path);
5583 }
5584 auto result = paintMethod->GetOverlayDrawFunction(paintWrapper);
5585 EXPECT_NE(result, nullptr);
5586 delete paintWrapper;
5587 paintWrapper = nullptr;
5588 }
5589
5590 /**
5591 * @tc.name: MenuPaintPropertyTestNg001
5592 * @tc.desc: Verify GetEnableArrow.
5593 * @tc.type: FUNC
5594 */
5595 HWTEST_F(MenuTestNg, MenuPaintPropertyTestNg001, TestSize.Level1)
5596 {
5597 /**
5598 * @tc.steps: step1. Build a object MenuPaintProperty.
5599 * @tc.expected: enableArrow has not value.
5600 */
5601 MenuPaintProperty property;
5602 EXPECT_FALSE(property.GetEnableArrow().has_value());
5603 /**
5604 * @tc.steps: step2. update enableArrow
5605 * @tc.expected: enableArrow value are as expected.
5606 */
5607 property.UpdateEnableArrow(true);
5608 ASSERT_TRUE(property.GetEnableArrow().has_value());
5609 EXPECT_TRUE(property.GetEnableArrow().value());
5610 }
5611
5612 /**
5613 * @tc.name: MenuPaintPropertyTestNg002
5614 * @tc.desc: Verify GetArrowOffset.
5615 * @tc.type: FUNC
5616 */
5617 HWTEST_F(MenuTestNg, MenuPaintPropertyTestNg002, TestSize.Level1)
5618 {
5619 /**
5620 * @tc.steps: step1. Build a object MenuPaintProperty.
5621 * @tc.expected: arrowOffset has not value.
5622 */
5623 MenuPaintProperty property;
5624 EXPECT_FALSE(property.GetArrowOffset().has_value());
5625 /**
5626 * @tc.steps: step2. update arrowOffset
5627 * @tc.expected: arrowOffset value are as expected.
5628 */
5629 property.UpdateArrowOffset(Dimension(10.0f));
5630 ASSERT_TRUE(property.GetArrowOffset().has_value());
5631 EXPECT_EQ(property.GetArrowOffset().value(), Dimension(10.0f));
5632 }
5633
5634 /**
5635 * @tc.name: MenuPaintPropertyTestNg003
5636 * @tc.desc: Verify GetArrowPosition.
5637 * @tc.type: FUNC
5638 */
5639 HWTEST_F(MenuTestNg, MenuPaintPropertyTestNg003, TestSize.Level1)
5640 {
5641 /**
5642 * @tc.steps: step1. Build a object MenuPaintProperty.
5643 * @tc.expected: arrowPosition has not value.
5644 */
5645 MenuPaintProperty property;
5646 EXPECT_FALSE(property.GetArrowPosition().has_value());
5647 /**
5648 * @tc.steps: step2. update arrowPosition
5649 * @tc.expected: arrowPosition value are as expected.
5650 */
5651 property.UpdateArrowPosition(OffsetF(10.0f, 10.0f));
5652 ASSERT_TRUE(property.GetArrowPosition().has_value());
5653 EXPECT_EQ(property.GetArrowPosition().value(), OffsetF(10.0f, 10.0f));
5654 }
5655
5656 /**
5657 * @tc.name: MenuPaintPropertyTestNg004
5658 * @tc.desc: Verify GetArrowPlacement.
5659 * @tc.type: FUNC
5660 */
5661 HWTEST_F(MenuTestNg, MenuPaintPropertyTestNg004, TestSize.Level1)
5662 {
5663 /**
5664 * @tc.steps: step1. Build a object MenuPaintProperty.
5665 * @tc.expected: arrowPlacement has not value.
5666 */
5667 MenuPaintProperty property;
5668 EXPECT_FALSE(property.GetArrowPlacement().has_value());
5669 /**
5670 * @tc.steps: step2. update arrowPlacement
5671 * @tc.expected: arrowPlacement value are as expected.
5672 */
5673 property.UpdateArrowPlacement(Placement::TOP);
5674 ASSERT_TRUE(property.GetArrowPlacement().has_value());
5675 EXPECT_EQ(property.GetArrowPlacement().value(), Placement::TOP);
5676 }
5677
5678 /**
5679 * @tc.name: MenuPaintPropertyTestNg005
5680 * @tc.desc: Verify Reset.
5681 * @tc.type: FUNC
5682 */
5683 HWTEST_F(MenuTestNg, MenuPaintPropertyTestNg005, TestSize.Level1)
5684 {
5685 /**
5686 * @tc.steps: step1. Build a object MenuPaintProperty, update property
5687 * @tc.expected: property value are as expected.
5688 */
5689 MenuPaintProperty property;
5690 property.UpdateEnableArrow(true);
5691 property.UpdateArrowOffset(Dimension(10.0f));
5692 property.UpdateArrowPosition(OffsetF(10.0f, 10.0f));
5693 property.UpdateArrowPlacement(Placement::TOP);
5694 EXPECT_TRUE(property.GetEnableArrow().has_value());
5695 EXPECT_TRUE(property.GetArrowOffset().has_value());
5696 EXPECT_TRUE(property.GetArrowPosition().has_value());
5697 EXPECT_TRUE(property.GetArrowPlacement().has_value());
5698 /**
5699 * @tc.steps: step2. reset property
5700 * @tc.expected: property value are as expected.
5701 */
5702 property.Reset();
5703 EXPECT_FALSE(property.GetEnableArrow().has_value());
5704 EXPECT_FALSE(property.GetArrowOffset().has_value());
5705 EXPECT_FALSE(property.GetArrowPosition().has_value());
5706 EXPECT_FALSE(property.GetArrowPlacement().has_value());
5707 }
5708
5709 /**
5710 * @tc.name: MenuPaintPropertyTestNg006
5711 * @tc.desc: Verify Clone.
5712 * @tc.type: FUNC
5713 */
5714 HWTEST_F(MenuTestNg, MenuPaintPropertyTestNg006, TestSize.Level1)
5715 {
5716 /**
5717 * @tc.steps: step1. Build a object MenuPaintProperty, update property
5718 */
5719 MenuPaintProperty property;
5720 property.UpdateEnableArrow(true);
5721 property.UpdateArrowOffset(Dimension(10.0f));
5722 property.UpdateArrowPosition(OffsetF(10.0f, 10.0f));
5723 property.UpdateArrowPlacement(Placement::TOP);
5724 /**
5725 * @tc.steps: step2. clone property
5726 * @tc.expected: property value are as expected.
5727 */
5728 auto cloneProperty = AceType::DynamicCast<MenuPaintProperty>(property.Clone());
5729 ASSERT_NE(cloneProperty, nullptr);
5730 EXPECT_EQ(property.GetEnableArrow().value(), cloneProperty->GetEnableArrow().value());
5731 EXPECT_EQ(property.GetArrowOffset().value(), cloneProperty->GetArrowOffset().value());
5732 EXPECT_EQ(property.GetArrowPosition().value(), cloneProperty->GetArrowPosition().value());
5733 EXPECT_EQ(property.GetArrowPlacement().value(), cloneProperty->GetArrowPlacement().value());
5734 }
5735
5736 /**
5737 * @tc.name: MenuPaintPropertyTestNg007
5738 * @tc.desc: Verify ToJsonValue.
5739 * @tc.type: FUNC
5740 */
5741 HWTEST_F(MenuTestNg, MenuPaintPropertyTestNg007, TestSize.Level1)
5742 {
5743 /**
5744 * @tc.steps: step1. Build a object MenuPaintProperty, update property
5745 */
5746 MenuPaintProperty property;
5747 property.UpdateEnableArrow(true);
5748 property.UpdateArrowOffset(Dimension(10.0f));
5749 property.UpdateArrowPosition(OffsetF(10.0f, 10.0f));
5750 property.UpdateArrowPlacement(Placement::TOP);
5751 /**
5752 * @tc.steps: step2. property to json
5753 * @tc.expected: property jsonValue are as expected.
5754 */
5755 auto json = JsonUtil::Create(true);
5756 property.ToJsonValue(json);
5757 EXPECT_EQ(json->GetString("enableArrow"), V2::ConvertBoolToString(true).c_str());
5758 EXPECT_EQ(json->GetString("arrowOffset"), Dimension(10.0f).ToString().c_str());
5759 EXPECT_EQ(json->GetString("arrowPosition"), OffsetF(10.0f, 10.0f).ToString().c_str());
5760 EXPECT_EQ(json->GetString("arrowPlacement"), property.ConvertPlacementToString(Placement::TOP).c_str());
5761 }
5762
5763 /**
5764 * @tc.name: MenuPaintPropertyTestNg008
5765 * @tc.desc: Verify ToJsonValue default value.
5766 * @tc.type: FUNC
5767 */
5768 HWTEST_F(MenuTestNg, MenuPaintPropertyTestNg008, TestSize.Level1)
5769 {
5770 /**
5771 * @tc.steps: step1. Build a object MenuPaintProperty
5772 */
5773 MenuPaintProperty property;
5774 /**
5775 * @tc.steps: step2. property to json
5776 * @tc.expected: property jsonValue are as expected.
5777 */
5778 auto json = JsonUtil::Create(true);
5779 property.ToJsonValue(json);
5780 EXPECT_EQ(json->GetString("enableArrow"), V2::ConvertBoolToString(false).c_str());
5781 EXPECT_EQ(json->GetString("arrowOffset"), Dimension(0.0, DimensionUnit::VP).ToString().c_str());
5782 EXPECT_EQ(json->GetString("arrowPosition"), OffsetF(0.0f, 0.0f).ToString().c_str());
5783 EXPECT_EQ(json->GetString("arrowPlacement"), property.ConvertPlacementToString(Placement::NONE).c_str());
5784 }
5785 } // namespace OHOS::Ace::NG
5786