1 /*
2 * Copyright (c) 2022 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 <cstddef>
17 #include <optional>
18 #include <unistd.h>
19 #include <vector>
20
21 #include "gtest/gtest.h"
22
23 #define private public
24 #include "test/mock/core/pipeline/mock_pipeline_context.h"
25 #include "test/mock/core/render/mock_render_context.h"
26 #include "test/mock/core/render/mock_render_surface.h"
27
28 #include "base/memory/ace_type.h"
29 #include "base/utils/utils.h"
30 #include "core/common/ai/image_analyzer_mgr.h"
31 #include "core/components_ng/base/frame_node.h"
32 #include "core/components_ng/base/view_stack_processor.h"
33 #include "core/components_ng/pattern/xcomponent/xcomponent_controller_ng.h"
34 #include "core/components_ng/pattern/xcomponent/xcomponent_layout_algorithm.h"
35 #include "core/components_ng/pattern/xcomponent/xcomponent_model_ng.h"
36 #include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h"
37 #include "core/components_ng/property/measure_property.h"
38 #include "core/components_v2/inspector/inspector_constants.h"
39 #include "core/event/touch_event.h"
40 #include "frameworks/core/gestures/press_recognizer.h"
41 #include "frameworks/core/components_ng/pattern/node_container/node_container_pattern.h"
42 #include "core/components_ng/pattern/linear_layout/linear_layout_pattern.h"
43 #include "core/components_ng/pattern/xcomponent/xcomponent_ext_surface_callback_client.h"
44
45 using namespace testing;
46 using namespace testing::ext;
47
48 namespace OHOS::Ace::NG {
49 struct TestProperty {
50 std::optional<std::string> xcId = std::nullopt;
51 std::optional<XComponentType> xcType = std::nullopt;
52 std::optional<std::string> libraryName = std::nullopt;
53 std::optional<std::string> soPath = std::nullopt;
54 std::optional<LoadEvent> loadEvent = std::nullopt;
55 std::optional<DestroyEvent> destroyEvent = std::nullopt;
56 std::optional<SurfaceCreatedEvent> surfaceCreatedEvent = std::nullopt;
57 std::optional<SurfaceChangedEvent> surfaceChangedEvent = std::nullopt;
58 std::optional<SurfaceDestroyedEvent> surfaceDestroyedEvent = std::nullopt;
59 };
60 namespace {
61 const std::string CHECK_KEY = "HI";
62 const std::string XCOMPONENT_ID = "xcomponent";
63 const std::string XCOMPONENT_LIBRARY_NAME = "native_render";
64 const std::string XCOMPONENT_SO_PATH = "com.example.xcomponentmultihap/entry";
65 const std::string SURFACE_ID = "2430951489577";
66 constexpr XComponentType XCOMPONENT_SURFACE_TYPE_VALUE = XComponentType::SURFACE;
67 constexpr XComponentType XCOMPONENT_COMPONENT_TYPE_VALUE = XComponentType::COMPONENT;
68 constexpr XComponentType XCOMPONENT_TEXTURE_TYPE_VALUE = XComponentType::TEXTURE;
69 constexpr XComponentType XCOMPONENT_NODE_TYPE_VALUE = XComponentType::NODE;
70 const float CONTAINER_WIDTH = 300.0f;
71 const float CONTAINER_HEIGHT = 300.0f;
72 const SizeF CONTAINER_SIZE(CONTAINER_WIDTH, CONTAINER_HEIGHT);
73 const uint32_t XCOMPONENT_ID_LEN_MAX = 10;
74 const float MAX_WIDTH = 400.0f;
75 const float MAX_HEIGHT = 400.0f;
76 const SizeF MAX_SIZE(MAX_WIDTH, MAX_HEIGHT);
77 const float CHILD_WIDTH = 200.0f;
78 const float CHILD_HEIGHT = 200.0f;
79 const SizeF CHILD_SIZE(CHILD_WIDTH, CHILD_HEIGHT);
80 const float CHILD_OFFSET_WIDTH = 50.0f;
81 const float CHILD_OFFSET_HEIGHT = 0.0f;
82 const float FORCE = 3.0f;
83 TestProperty testProperty;
84 bool isFocus = false;
85 const float SURFACE_WIDTH = 250.0f;
86 const float SURFACE_HEIGHT = 150.0f;
87 const float SURFACE_OFFSETX = 10.0f;
88 const float SURFACE_OFFSETY = 20.0f;
89 int g_surfaceShowNum = 1;
90 bool isAxis = false;
91 bool isLock = true;
92
ConvertXComponentTouchType(const OH_NativeXComponent_TouchEventType & type)93 TouchType ConvertXComponentTouchType(const OH_NativeXComponent_TouchEventType& type)
94 {
95 switch (type) {
96 case OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_CANCEL:
97 return TouchType::CANCEL;
98 case OH_NATIVEXCOMPONENT_DOWN:
99 return TouchType::DOWN;
100 case OH_NATIVEXCOMPONENT_UP:
101 return TouchType::UP;
102 case OH_NATIVEXCOMPONENT_MOVE:
103 return TouchType::MOVE;
104 case OH_NATIVEXCOMPONENT_UNKNOWN:
105 return TouchType::UNKNOWN;
106 default:
107 return TouchType::UNKNOWN;
108 }
109 }
110
ConvertNativeXComponentKeyAction(const KeyAction & keyAction)111 OH_NativeXComponent_KeyAction ConvertNativeXComponentKeyAction(const KeyAction& keyAction)
112 {
113 switch (keyAction) {
114 case KeyAction::DOWN:
115 return OH_NativeXComponent_KeyAction::OH_NATIVEXCOMPONENT_KEY_ACTION_DOWN;
116 case KeyAction::UP:
117 return OH_NativeXComponent_KeyAction::OH_NATIVEXCOMPONENT_KEY_ACTION_UP;
118 default:
119 return OH_NativeXComponent_KeyAction::OH_NATIVEXCOMPONENT_KEY_ACTION_UNKNOWN;
120 }
121 }
122
ConvertNativeXComponentEventSourceType(const SourceType & sourceType)123 OH_NativeXComponent_EventSourceType ConvertNativeXComponentEventSourceType(const SourceType& sourceType)
124 {
125 switch (sourceType) {
126 case SourceType::MOUSE:
127 return OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_MOUSE;
128 case SourceType::TOUCH:
129 return OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_TOUCHSCREEN;
130 case SourceType::TOUCH_PAD:
131 return OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_TOUCHPAD;
132 case SourceType::KEYBOARD:
133 return OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_KEYBOARD;
134 default:
135 return OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_UNKNOWN;
136 }
137 }
138 } // namespace
139
140 class XComponentTestNg : public testing::Test {
141 public:
142 static void SetUpTestSuite();
143 static void TearDownTestSuite();
TearDown()144 void TearDown() override
145 {
146 testProperty.loadEvent = std::nullopt;
147 testProperty.destroyEvent = std::nullopt;
148 testProperty.surfaceCreatedEvent = std::nullopt;
149 testProperty.surfaceChangedEvent = std::nullopt;
150 testProperty.surfaceDestroyedEvent = std::nullopt;
151 }
152
153 protected:
154 static RefPtr<FrameNode> CreateXComponentNode(TestProperty& testProperty);
155 };
156
SetUpTestSuite()157 void XComponentTestNg::SetUpTestSuite()
158 {
159 MockPipelineContext::SetUp();
160 testProperty.xcId = XCOMPONENT_ID;
161 testProperty.libraryName = XCOMPONENT_LIBRARY_NAME;
162 testProperty.soPath = XCOMPONENT_SO_PATH;
163 }
164
TearDownTestSuite()165 void XComponentTestNg::TearDownTestSuite()
166 {
167 MockPipelineContext::TearDown();
168 }
169
CreateXComponentNode(TestProperty & testProperty)170 RefPtr<FrameNode> XComponentTestNg::CreateXComponentNode(TestProperty& testProperty)
171 {
172 auto xcId = testProperty.xcId;
173 auto xcType = testProperty.xcType.value();
174 auto libraryName = testProperty.libraryName;
175 auto xcomponentController = std::make_shared<XComponentControllerNG>();
176 XComponentModelNG().Create(xcId, xcType, libraryName, xcomponentController);
177
178 if (testProperty.soPath.has_value()) {
179 XComponentModelNG().SetSoPath(testProperty.soPath.value());
180 }
181 if (testProperty.loadEvent.has_value()) {
182 XComponentModelNG().SetOnLoad(std::move(testProperty.loadEvent.value()));
183 }
184 if (testProperty.destroyEvent.has_value()) {
185 XComponentModelNG().SetOnDestroy(std::move(testProperty.destroyEvent.value()));
186 }
187 if (testProperty.surfaceCreatedEvent.has_value()) {
188 XComponentModelNG().SetControllerOnCreated(std::move(testProperty.surfaceCreatedEvent.value()));
189 }
190 if (testProperty.surfaceChangedEvent.has_value()) {
191 XComponentModelNG().SetControllerOnChanged(std::move(testProperty.surfaceChangedEvent.value()));
192 }
193 if (testProperty.surfaceDestroyedEvent.has_value()) {
194 XComponentModelNG().SetControllerOnDestroyed(std::move(testProperty.surfaceDestroyedEvent.value()));
195 }
196 RefPtr<UINode> element = ViewStackProcessor::GetInstance()->Finish(); // pop
197 return AceType::DynamicCast<FrameNode>(element);
198 }
199
200 /**
201 * @tc.name: XComponentEventTest002
202 * @tc.desc: Test XComponent onLoad and onDestroy event.
203 * @tc.type: FUNC
204 */
205 HWTEST_F(XComponentTestNg, XComponentEventTest002, TestSize.Level1)
206 {
207 /**
208 * @tc.steps: step1. set the testProperty and CreateXComponentNode
209 * case: type = XCOMPONENT_SURFACE_TYPE
210 * @tc.expected: frameNode create successfully
211 */
212 std::string onLoadKey;
213 std::string onDestroyKey;
__anon3398720b0202(const std::string& ) 214 auto onLoad = [&onLoadKey](const std::string& /* xComponentId */) { onLoadKey = CHECK_KEY; };
__anon3398720b0302(const std::string& ) 215 auto onDestroy = [&onDestroyKey](const std::string& /* xComponentId */) { onDestroyKey = CHECK_KEY; };
216
217 testProperty.xcType = XCOMPONENT_SURFACE_TYPE_VALUE;
218 testProperty.loadEvent = std::move(onLoad);
219 testProperty.destroyEvent = std::move(onDestroy);
220 auto frameNode = CreateXComponentNode(testProperty);
221 EXPECT_TRUE(frameNode);
222 EXPECT_EQ(frameNode->GetTag(), V2::XCOMPONENT_ETS_TAG);
223
224 /**
225 * @tc.steps: step2. call FireLoadEvent, FireDestroyEvent
226 * @tc.expected: three checkKeys has changed
227 */
228 auto xComponentEventHub = frameNode->GetEventHub<XComponentEventHub>();
229 ASSERT_TRUE(xComponentEventHub);
230 xComponentEventHub->FireLoadEvent(XCOMPONENT_ID);
231 xComponentEventHub->FireDestroyEvent(XCOMPONENT_ID);
232 EXPECT_EQ(onLoadKey, CHECK_KEY);
233 EXPECT_EQ(onDestroyKey, CHECK_KEY);
234
235 /**
236 * @tc.steps: step3. reset the testProperty and rerun step1&2
237 * case: type = XCOMPONENT_COMPONENT_TYPE
238 * @tc.expected: three checkKeys has no change
239 */
240
__anon3398720b0402(const std::string& ) 241 auto onLoad2 = [&onLoadKey](const std::string& /* xComponentId */) { onLoadKey = ""; };
__anon3398720b0502(const std::string& ) 242 auto onDestroy2 = [&onDestroyKey](const std::string& /* xComponentId */) { onDestroyKey = ""; };
243 testProperty.xcType = XCOMPONENT_COMPONENT_TYPE_VALUE;
244 testProperty.loadEvent = std::move(onLoad2);
245 testProperty.destroyEvent = std::move(onDestroy2);
246
247 auto frameNode2 = CreateXComponentNode(testProperty);
248 EXPECT_TRUE(frameNode2);
249 xComponentEventHub = frameNode2->GetEventHub<XComponentEventHub>();
250 ASSERT_TRUE(xComponentEventHub);
251 xComponentEventHub->FireLoadEvent(XCOMPONENT_ID);
252 xComponentEventHub->FireDestroyEvent(XCOMPONENT_ID);
253 EXPECT_EQ(onLoadKey, CHECK_KEY);
254 EXPECT_EQ(onDestroyKey, CHECK_KEY);
255 }
256
257 /**
258 * @tc.name: XComponentLayoutAlgorithmTest006
259 * @tc.desc: Test BeforeSyncGeometryProperties
260 * @tc.type: FUNC
261 */
262 HWTEST_F(XComponentTestNg, XComponentLayoutAlgorithmTest006, TestSize.Level1)
263 {
264 /**
265 * @tc.steps: step1. set type = XCOMPONENT_SURFACE_TYPE and call CreateXComponentNode
266 * @tc.expected: frameNode create successfully
267 */
268 testProperty.xcType = XCOMPONENT_SURFACE_TYPE_VALUE;
269 auto frameNode = CreateXComponentNode(testProperty);
270 ASSERT_TRUE(frameNode);
271
272 /**
273 * @tc.steps: step2. call BeforeSyncGeometryProperties
274 * case: hasXComponentInit_ = false
275 * @tc.expected: hasXComponentInit_ = true
276 */
277 auto pattern = frameNode->GetPattern<XComponentPattern>();
278 ASSERT_TRUE(pattern);
279 DirtySwapConfig config;
280 auto xComponentLayoutAlgorithm = AceType::MakeRefPtr<XComponentLayoutAlgorithm>();
281 RefPtr<GeometryNode> geometryNode = AceType::MakeRefPtr<GeometryNode>();
282 geometryNode->SetFrameSize(MAX_SIZE);
283 geometryNode->SetContentSize(MAX_SIZE);
284 frameNode->geometryNode_ = geometryNode;
285 EXPECT_FALSE(pattern->hasXComponentInit_);
286 EXPECT_CALL(*AceType::DynamicCast<MockRenderSurface>(pattern->renderSurface_), IsSurfaceValid())
287 .WillOnce(Return(true))
288 .WillOnce(Return(true))
289 .WillRepeatedly(Return(false));
290 EXPECT_CALL(*AceType::DynamicCast<MockRenderSurface>(pattern->renderSurface_),
291 AdjustNativeWindowSize(MAX_WIDTH, MAX_HEIGHT))
292 .WillOnce(Return());
293 pattern->BeforeSyncGeometryProperties(config); // IsSurfaceValid=true
294 EXPECT_TRUE(pattern->hasXComponentInit_);
295 // test OnRebuildFrame
296 pattern->OnRebuildFrame(); // type="surface", IsSurfaceValid=true
297 pattern->OnRebuildFrame(); // type="surface", IsSurfaceValid=false
298 // goto other branch
299 pattern->type_ = XCOMPONENT_COMPONENT_TYPE_VALUE;
300 pattern->XComponentSizeInit(); // IsSurfaceValid=false
301 pattern->OnRebuildFrame(); // type="component"
302
303 /**
304 * @tc.steps: step3. call BeforeSyncGeometryProperties adjust frameOffsetChanges, contentOffsetChanges and
305 * contentSizeChanges
306 * @tc.expected: BeforeSyncGeometryProperties return false
307 */
308 bool frameOffsetChanges[2] = { false, true };
309 bool contentOffsetChanges[2] = { false, true };
310 pattern->type_ = XCOMPONENT_SURFACE_TYPE_VALUE;
311 for (bool frameOffsetChange : frameOffsetChanges) {
312 for (bool contentOffsetChange : contentOffsetChanges) {
313 config.frameOffsetChange = frameOffsetChange;
314 config.contentOffsetChange = contentOffsetChange;
315 config.contentSizeChange = frameOffsetChange && contentOffsetChange;
316 if (config.contentSizeChange) {
317 geometryNode->SetContentSize(CHILD_SIZE);
318 EXPECT_CALL(*AceType::DynamicCast<MockRenderSurface>(pattern->renderSurface_),
319 AdjustNativeWindowSize(CHILD_WIDTH, CHILD_HEIGHT))
320 .WillOnce(Return());
321 }
322 pattern->BeforeSyncGeometryProperties(config);
323 }
324 }
325
326 /**
327 * @tc.steps: step4. call BeforeSyncGeometryProperties
328 * case: size is zero
329 * @tc.expected: BeforeSyncGeometryProperties return false
330 */
331 geometryNode->SetContentSize({ 0.0f, 0.0f });
332 pattern->BeforeSyncGeometryProperties(config);
333
334 /**
335 * @tc.steps: step5. call BeforeSyncGeometryProperties
336 * case: type="component", config.skipMeasure = true, dirty->SkipMeasureContent() = true
337 * @tc.expected: BeforeSyncGeometryProperties return false
338 */
339 pattern->BeforeSyncGeometryProperties(config);
340 config.skipMeasure = true;
341 pattern->BeforeSyncGeometryProperties(config);
342 pattern->type_ = XCOMPONENT_COMPONENT_TYPE_VALUE;
343 pattern->BeforeSyncGeometryProperties(config);
344 }
345
346 /**
347 * @tc.name: XComponentMouseEventTest007
348 * @tc.desc: Test MouseEvent
349 * @tc.type: FUNC
350 */
351 HWTEST_F(XComponentTestNg, XComponentMouseEventTest007, TestSize.Level1)
352 {
353 /**
354 * @tc.steps: step1. set type = XCOMPONENT_SURFACE_TYPE and call CreateXComponentNode
355 * @tc.expected: xcomponent frameNode create successfully
356 */
357 testProperty.xcType = XCOMPONENT_SURFACE_TYPE_VALUE;
358 auto frameNode = CreateXComponentNode(testProperty);
359 ASSERT_TRUE(frameNode);
360 auto pattern = frameNode->GetPattern<XComponentPattern>();
361 ASSERT_TRUE(pattern);
362 pattern->hasXComponentInit_ = true;
363
364 /**
365 * @tc.steps: step2. prepare mouse info
366 */
367 MouseInfo mouseInfo;
368 std::vector<MouseAction> mouseActions { MouseAction::NONE, MouseAction::PRESS, MouseAction::RELEASE,
369 MouseAction::MOVE };
370 std::vector<MouseButton> mouseButtons {
371 MouseButton::NONE_BUTTON,
372 MouseButton::LEFT_BUTTON,
373 MouseButton::RIGHT_BUTTON,
374 MouseButton::MIDDLE_BUTTON,
375 MouseButton::BACK_BUTTON,
376 MouseButton::FORWARD_BUTTON,
377 };
378
379 /**
380 * @tc.steps: step3. call HandleMouseEvent
381 * @tc.expected: no error happens
382 */
383 for (MouseAction& action : mouseActions) {
384 mouseInfo.SetAction(action);
385 pattern->HandleMouseEvent(mouseInfo);
386 pattern->HandleMouseHoverEvent(true);
387 pattern->HandleMouseHoverEvent(false);
388 }
389 for (MouseButton& button : mouseButtons) {
390 mouseInfo.SetButton(button);
391 pattern->HandleMouseEvent(mouseInfo);
392 }
393 }
394
395 /**
396 * @tc.name: XComponentTouchEventTest008
397 * @tc.desc: Test TouchEvent
398 * @tc.type: FUNC
399 */
400 HWTEST_F(XComponentTestNg, XComponentTouchEventTest008, TestSize.Level1)
401 {
402 /**
403 * @tc.steps: step1. set type = XCOMPONENT_SURFACE_TYPE and call CreateXComponentNode
404 * @tc.expected: xcomponent frameNode create successfully
405 */
406 testProperty.xcType = XCOMPONENT_SURFACE_TYPE_VALUE;
407 auto frameNode = CreateXComponentNode(testProperty);
408 ASSERT_TRUE(frameNode);
409 auto pattern = frameNode->GetPattern<XComponentPattern>();
410 ASSERT_TRUE(pattern);
411
412 /**
413 * @tc.steps: step2. prepare touchEvent info
414 */
415 std::vector<TouchType> touchTypes { TouchType::DOWN, TouchType::UP, TouchType::MOVE, TouchType::CANCEL,
416 TouchType::UNKNOWN };
417 std::vector<SourceTool> sourceTools {
418 SourceTool::UNKNOWN,
419 SourceTool::FINGER,
420 SourceTool::PEN,
421 SourceTool::RUBBER,
422 SourceTool::BRUSH,
423 SourceTool::PENCIL,
424 SourceTool::AIRBRUSH,
425 SourceTool::MOUSE,
426 SourceTool::LENS,
427 };
428
429 /**
430 * @tc.steps: step3. call HandleTouchEvent
431 * case: touchEventInfo.GetChangedTouches is empty
432 * @tc.expected: pattern->touchEventPoint_.numPoints not change
433 */
434 TouchEventInfo touchEventInfoEmpty("onTouch");
435 uint32_t numPoints = pattern->touchEventPoint_.numPoints;
436 pattern->HandleTouchEvent(touchEventInfoEmpty);
437 EXPECT_EQ(pattern->touchEventPoint_.numPoints, numPoints);
438
439 /**
440 * @tc.steps: step4. call HandleTouchEvent
441 * case: different touchType
442 * @tc.expected: touchType fit
443 */
444 for (TouchType& touchType : touchTypes) {
445 TouchEventInfo touchEventInfo("onTouch");
446 TouchLocationInfo locationInfo(1);
447 locationInfo.SetTouchType(touchType);
448 touchEventInfo.AddChangedTouchLocationInfo(std::move(locationInfo));
449 pattern->HandleTouchEvent(touchEventInfo);
450 EXPECT_EQ(
451 static_cast<int>(ConvertXComponentTouchType(pattern->touchEventPoint_.type)), static_cast<int>(touchType));
452 }
453
454 /**
455 * @tc.steps: step5. call HandleTouchEvent
456 * case: different sourceToolType
457 * @tc.expected: sourceToolType fit
458 */
459 TouchEventInfo touchEventInfo("onTouch");
460 TouchLocationInfo locationInfo(0);
461 locationInfo.SetTouchType(TouchType::DOWN);
462 touchEventInfo.AddChangedTouchLocationInfo(std::move(locationInfo));
463 for (int i = 0; i < static_cast<int>(OH_MAX_TOUCH_POINTS_NUMBER) + 1; i++) { // over the OH_MAX_TOUCH_POINTS_NUMBER
464 TouchLocationInfo pointInfo(i);
465 pointInfo.SetSourceTool(sourceTools[i % sourceTools.size()]);
466 touchEventInfo.AddTouchLocationInfo(std::move(pointInfo));
467 }
468 pattern->HandleTouchEvent(touchEventInfo);
469 EXPECT_EQ(pattern->nativeXComponentTouchPoints_.size(), OH_MAX_TOUCH_POINTS_NUMBER);
470 for (int i = 0; i < static_cast<int>(OH_MAX_TOUCH_POINTS_NUMBER); i++) {
471 EXPECT_EQ(static_cast<int>(pattern->nativeXComponentTouchPoints_[i].sourceToolType),
472 static_cast<int>(sourceTools[i % sourceTools.size()]));
473 }
474 }
475
476 /**
477 * @tc.name: XComponentTouchEventTest009
478 * @tc.desc: Test TouchEvent
479 * @tc.type: FUNC
480 */
481 HWTEST_F(XComponentTestNg, XComponentTouchEventTest009, TestSize.Level1)
482 {
483 /**
484 * @tc.steps: step1. set type = XCOMPONENT_SURFACE_TYPE and call CreateXComponentNode
485 * @tc.expected: xcomponent frameNode create successfully
486 */
487 testProperty.xcType = XCOMPONENT_SURFACE_TYPE_VALUE;
488 auto frameNode = CreateXComponentNode(testProperty);
489 ASSERT_TRUE(frameNode);
490 auto pattern = frameNode->GetPattern<XComponentPattern>();
491 ASSERT_TRUE(pattern);
492
493 RefPtr<LayoutProperty> layoutPropertyTest = frameNode->GetLayoutProperty<LayoutProperty>();
494 ASSERT_TRUE(layoutPropertyTest);
495 RefPtr<Property> propertyTest = frameNode->GetPaintProperty<Property>();
496 ASSERT_TRUE(propertyTest);
497 }
498
499 /**
500 * @tc.name: XComponentKeyEventTest010
501 * @tc.desc: Test KeyEvent/FocusEvent/BlurEvent
502 * @tc.type: FUNC
503 */
504 HWTEST_F(XComponentTestNg, XComponentKeyEventTest010, TestSize.Level1)
505 {
506 /**
507 * @tc.steps: step1. set type = XCOMPONENT_SURFACE_TYPE and call CreateXComponentNode
508 * @tc.expected: xcomponent frameNode create successfully
509 */
510 testProperty.xcType = XCOMPONENT_SURFACE_TYPE_VALUE;
511 auto frameNode = CreateXComponentNode(testProperty);
512 ASSERT_TRUE(frameNode);
513 auto pattern = frameNode->GetPattern<XComponentPattern>();
514 ASSERT_TRUE(pattern);
515
516 /**
517 * @tc.steps: step2. create focusHub & nativeXComponent instance
518 * @tc.expected: focusHub & nativeXComponent instance create successfully
519 */
520 auto host = pattern->GetHost();
521 ASSERT_TRUE(host);
522 auto focusHub = host->GetFocusHub();
523 ASSERT_TRUE(focusHub);
524 auto pair = pattern->GetNativeXComponent();
525 auto weakNativeXComponent = pair.second;
526 auto nativeXComponent = weakNativeXComponent.lock();
527 auto nativeXComponentImpl = pair.first;
528 ASSERT_TRUE(nativeXComponent);
529 ASSERT_TRUE(nativeXComponentImpl);
530
531 /**
532 * @tc.steps: step3. register focus & blur event for nativeXComponent instance
533 */
534 nativeXComponent->RegisterFocusEventCallback(
__anon3398720b0602(OH_NativeXComponent* , void* ) 535 [](OH_NativeXComponent* /* nativeXComponent */, void* /* window */) { isFocus = true; });
536 nativeXComponent->RegisterBlurEventCallback(
__anon3398720b0702(OH_NativeXComponent* , void* ) 537 [](OH_NativeXComponent* /* nativeXComponent */, void* /* window */) { isFocus = false; });
538
539 /**
540 * @tc.steps: step4. call focusHub's focus & blur event
541 * @tc.expected: the callbacks registered in step3 are called
542 */
543 focusHub->onFocusInternal_();
544 EXPECT_TRUE(isFocus);
545 focusHub->onBlurInternal_();
546 EXPECT_FALSE(isFocus);
547
548 /**
549 * @tc.steps: step5. call HandleKeyEvent
550 * case: different sourceType & keyAction
551 * @tc.expected: sourceType & keyAction fit
552 */
553 std::vector<SourceType> sourceTypes { SourceType::NONE, SourceType::MOUSE, SourceType::TOUCH, SourceType::TOUCH_PAD,
554 SourceType::KEYBOARD };
555 std::vector<KeyAction> keyActions { KeyAction::UNKNOWN, KeyAction::DOWN, KeyAction::UP };
556 for (SourceType& sourceType : sourceTypes) {
557 for (KeyAction& keyAction : keyActions) {
558 KeyEvent keyEvent { KeyCode::KEY_0, keyAction, 0, 0, 0, sourceType };
559 focusHub->ProcessOnKeyEventInternal(keyEvent);
560 EXPECT_EQ(nativeXComponentImpl->keyEvent_.sourceType, ConvertNativeXComponentEventSourceType(sourceType));
561 EXPECT_EQ(nativeXComponentImpl->keyEvent_.action, ConvertNativeXComponentKeyAction(keyAction));
562 }
563 }
564 }
565
566 /**
567 * @tc.name: XComponentKeyEventTest011
568 * @tc.desc: Test XComponent type = XComponentType::TEXTURE
569 * @tc.type: FUNC
570 */
571 HWTEST_F(XComponentTestNg, XComponentTextureTypeTest011, TestSize.Level1)
572 {
573 /**
574 * @tc.steps: step1. set type = XCOMPONENT_TEXTURE_TYPE_VALUE and call CreateXComponentNode
575 * @tc.expected: xcomponent frameNode create successfully
576 */
577 testProperty.xcType = XCOMPONENT_TEXTURE_TYPE_VALUE;
578 auto frameNode = CreateXComponentNode(testProperty);
579 EXPECT_TRUE(frameNode);
580 EXPECT_EQ(frameNode->GetTag(), V2::XCOMPONENT_ETS_TAG);
581 auto pattern = frameNode->GetPattern<XComponentPattern>();
582 ASSERT_TRUE(pattern);
583 pattern->hasXComponentInit_ = true;
584 EXPECT_EQ(pattern->type_, XCOMPONENT_TEXTURE_TYPE_VALUE);
585 EXPECT_TRUE(pattern->IsAtomicNode());
586
587 /**
588 * @tc.steps: step2. call InitNativeWindow
589 * @tc.expected: renderSurface_->AdjustNativeWindowSize is called
590 */
591 EXPECT_CALL(*AceType::DynamicCast<MockRenderSurface>(pattern->renderSurface_), IsSurfaceValid())
592 .WillOnce(Return(true));
593 EXPECT_CALL(*AceType::DynamicCast<MockRenderSurface>(pattern->renderSurface_),
594 AdjustNativeWindowSize(MAX_WIDTH, MAX_HEIGHT))
595 .WillOnce(Return());
596 pattern->InitNativeWindow(MAX_WIDTH, MAX_HEIGHT);
597 }
598
599 /**
600 * @tc.name: XComponentControllerTest
601 * @tc.desc: Test XComponentController's interface
602 * @tc.type: FUNC
603 */
604 HWTEST_F(XComponentTestNg, XComponentControllerTest, TestSize.Level1)
605 {
606 /**
607 * @tc.steps: step1. set type = XCOMPONENT_SURFACE_TYPE_VALUE and call CreateXComponentNode
608 * @tc.expected: xcomponent frameNode create successfully
609 */
610 testProperty.xcType = XCOMPONENT_SURFACE_TYPE_VALUE;
611 auto frameNode = CreateXComponentNode(testProperty);
612 EXPECT_TRUE(frameNode);
613 EXPECT_EQ(frameNode->GetTag(), V2::XCOMPONENT_ETS_TAG);
614 auto pattern = frameNode->GetPattern<XComponentPattern>();
615 ASSERT_TRUE(pattern);
616 pattern->hasXComponentInit_ = true;
617 EXPECT_EQ(pattern->type_, XCOMPONENT_SURFACE_TYPE_VALUE);
618 EXPECT_TRUE(pattern->IsAtomicNode());
619 auto renderContext = AceType::MakeRefPtr<MockRenderContext>();
620 pattern->handlingSurfaceRenderContext_ = renderContext;
621
622 /**
623 * @tc.steps: step2. call XcomponentController's interface releative to SetSurfaceRect
624 * @tc.expected: handlingSurfaceRenderContext_->SetBounds(SURFACE_OFFSETX, SURFACE_OFFSETY,
625 * SURFACE_WIDTH, SURFACE_HEIGHT) is called
626 */
627 auto xcomponentController = pattern->xcomponentController_;
628 EXPECT_TRUE(xcomponentController);
629 pattern->drawSize_ = MAX_SIZE;
630 xcomponentController->SetIdealSurfaceWidth(SURFACE_WIDTH);
631 xcomponentController->SetIdealSurfaceHeight(SURFACE_HEIGHT);
632 xcomponentController->SetIdealSurfaceOffsetX(SURFACE_OFFSETX);
633 xcomponentController->SetIdealSurfaceOffsetY(SURFACE_OFFSETY);
634 EXPECT_CALL(*AceType::DynamicCast<MockRenderContext>(pattern->handlingSurfaceRenderContext_),
635 SetBounds(SURFACE_OFFSETX, SURFACE_OFFSETY, SURFACE_WIDTH, SURFACE_HEIGHT))
636 .WillOnce(Return());
637 xcomponentController->UpdateSurfaceBounds();
638
639 /**
640 * @tc.steps: step3. call XcomponentController's interface releative to GetSurfaceRect
641 * @tc.expected: the rect get from GetSurfaceRect equals the rect set by SetSurfaceRect
642 */
643 auto surfaceWidth = 0.0f;
644 auto surfaceHeight = 0.0f;
645 auto surfaceOffsetX = 0.0f;
646 auto surfaceOffsetY = 0.0f;
647 xcomponentController->GetSurfaceSize(surfaceWidth, surfaceHeight);
648 xcomponentController->GetSurfaceOffset(surfaceOffsetX, surfaceOffsetY);
649 EXPECT_EQ(surfaceOffsetX, SURFACE_OFFSETX);
650 EXPECT_EQ(surfaceOffsetY, SURFACE_OFFSETY);
651 EXPECT_EQ(surfaceWidth, SURFACE_WIDTH);
652 EXPECT_EQ(surfaceHeight, SURFACE_HEIGHT);
653
654 /**
655 * @tc.steps: step4. call XcomponentController's ClearIdealSurfaceOffset
656 * @tc.expected: handlingSurfaceRenderContext_->SetBounds(newSurfaceOffsetX, newSurfaceOffsetY,
657 * SURFACE_WIDTH, SURFACE_HEIGHT) is called
658 */
659 auto newSurfaceOffsetX = (MAX_WIDTH - SURFACE_WIDTH) / 2.0f;
660 auto newSurfaceOffsetY = (MAX_HEIGHT - SURFACE_HEIGHT) / 2.0f;
661 xcomponentController->ClearIdealSurfaceOffset(true);
662 xcomponentController->ClearIdealSurfaceOffset(false);
663 EXPECT_CALL(*AceType::DynamicCast<MockRenderContext>(pattern->handlingSurfaceRenderContext_),
664 SetBounds(newSurfaceOffsetX, newSurfaceOffsetY, SURFACE_WIDTH, SURFACE_HEIGHT))
665 .WillOnce(Return());
666 xcomponentController->UpdateSurfaceBounds();
667
668 /**
669 * @tc.steps: step5. call XcomponentController's interface relative to SetSurfaceRotation
670 * @tc.expected: handlingSurfaceRenderContext_->SetSurfaceRotation(isLock) is called
671 */
672 EXPECT_CALL(
673 *AceType::DynamicCast<MockRenderContext>(pattern->handlingSurfaceRenderContext_), SetSurfaceRotation(isLock))
674 .WillOnce(Return());
675 xcomponentController->SetSurfaceRotation(isLock);
676
677 /**
678 * @tc.steps: step6. call XcomponentController's interface relative to GetSurfaceRotation
679 * @tc.expected: the lock status get from GetSurfaceRotation equals the lock status set by SetSurfaceRotation
680 */
681 auto lock = xcomponentController->GetSurfaceRotation();
682 EXPECT_EQ(lock, isLock);
683 }
684
685 /**
686 * @tc.name: XComponentAxisEventTest012
687 * @tc.desc: Test AxisEvent
688 * @tc.type: FUNC
689 */
690 HWTEST_F(XComponentTestNg, XComponentAxisEventTest012, TestSize.Level1)
691 {
692 /**
693 * @tc.steps: step1. set type = XCOMPONENT_SURFACE_TYPE and call CreateXComponentNode
694 * @tc.expected: xcomponent frameNode create successfully
695 */
696 testProperty.xcType = XCOMPONENT_SURFACE_TYPE_VALUE;
697 auto frameNode = CreateXComponentNode(testProperty);
698 ASSERT_TRUE(frameNode);
699 auto pattern = frameNode->GetPattern<XComponentPattern>();
700 ASSERT_TRUE(pattern);
701
702 /**
703 * @tc.steps: step2. create nativeXComponent instance
704 * @tc.expected: focusHub & nativeXComponent instance create successfully
705 */
706 auto pair = pattern->GetNativeXComponent();
707 auto weakNativeXComponent = pair.second;
708 auto nativeXComponent = weakNativeXComponent.lock();
709 auto nativeXComponentImpl = pair.first;
710 ASSERT_TRUE(nativeXComponent);
711 ASSERT_TRUE(nativeXComponentImpl);
712
713 /**
714 * @tc.steps: step3. register axis event for nativeXComponent instance
715 */
716 auto callback = [](OH_NativeXComponent* /* nativeXComponent */, ArkUI_UIInputEvent* event,
__anon3398720b0802(OH_NativeXComponent* , ArkUI_UIInputEvent* event, ArkUI_UIInputEvent_Type type) 717 ArkUI_UIInputEvent_Type type) { isAxis = true; };
718 nativeXComponent->RegisterUIAxisEventCallback(callback);
719
720 /**
721 * @tc.steps: step4. call HandleAxisEvent
722 */
723 AxisInfo event;
724 pattern->HandleAxisEvent(event);
725 EXPECT_TRUE(isAxis);
726 }
727
728 /**
729 * @tc.name: XComponentOnAreaChangedInnerTest019
730 * @tc.desc: Test XComponent OnAreaChangedInner.
731 * @tc.type: FUNC
732 */
733 HWTEST_F(XComponentTestNg, XComponentOnAreaChangedInnerTest019, TestSize.Level1)
734 {
735 /**
736 * @tc.steps: step1. set the testProperty and CreateXComponentNode
737 * case: type = XCOMPONENT_SURFACE_TYPE
738 * @tc.expected: frameNode create successfully
739 */
740 std::string onLoadKey;
741 std::string onDestroyKey;
__anon3398720b0902(const std::string& ) 742 auto onLoad = [&onLoadKey](const std::string& /* xComponentId */) { onLoadKey = CHECK_KEY; };
__anon3398720b0a02(const std::string& ) 743 auto onDestroy = [&onDestroyKey](const std::string& /* xComponentId */) { onDestroyKey = CHECK_KEY; };
744
745 testProperty.xcType = XCOMPONENT_SURFACE_TYPE_VALUE;
746 testProperty.loadEvent = std::move(onLoad);
747 testProperty.destroyEvent = std::move(onDestroy);
748 auto frameNode = CreateXComponentNode(testProperty);
749 EXPECT_TRUE(frameNode);
750 EXPECT_EQ(frameNode->GetTag(), V2::XCOMPONENT_ETS_TAG);
751 auto pattern = frameNode->GetPattern<XComponentPattern>();
752 ASSERT_NE(pattern, nullptr);
753
754 /**
755 * @tc.steps: step2. call OnAreaChangedInner
756 * case: SystemProperties::GetExtSurfaceEnabled() == true
757 * @tc.expected: call SetExtSurfaceBounds
758 */
759 auto host = pattern->GetHost();
760 CHECK_NULL_VOID(host);
761 auto geometryNode = host->GetGeometryNode();
762 CHECK_NULL_VOID(geometryNode);
763 geometryNode->SetFrameSize(MAX_SIZE);
764 geometryNode->SetContentSize(MAX_SIZE);
765
766 EXPECT_CALL(*AceType::DynamicCast<MockRenderSurface>(pattern->renderSurface_),
767 SetExtSurfaceBounds(0, 0, MAX_WIDTH, MAX_HEIGHT))
768 .WillOnce(Return());
769 SystemProperties::SetExtSurfaceEnabled(true);
770 pattern->OnAreaChangedInner();
771 SystemProperties::SetExtSurfaceEnabled(false);
772 }
773
774 /**
775 * @tc.name: XComponentSetHistoryPointTest20
776 * @tc.desc: Test SetHistoryPoint
777 * @tc.type: FUNC
778 */
779 HWTEST_F(XComponentTestNg, XComponentSetHistoryPointTest20, TestSize.Level1)
780 {
781 /**
782 * @tc.steps: step1. set type = XCOMPONENT_SURFACE_TYPE and call CreateXComponentNode
783 * @tc.expected: xcomponent frameNode create successfully
784 */
785 testProperty.xcType = XCOMPONENT_SURFACE_TYPE_VALUE;
786 auto frameNode = CreateXComponentNode(testProperty);
787 ASSERT_TRUE(frameNode);
788 auto pattern = frameNode->GetPattern<XComponentPattern>();
789 ASSERT_TRUE(pattern);
790 pattern->hasXComponentInit_ = true;
791
792 /**
793 * @tc.steps: step2. prepare point info
794 */
795 std::vector<TouchLocationInfo> pList {
796 DragStartInfo(1),
797 DragUpdateInfo(2),
798 DragEndInfo(3),
799 ClickInfo(4),
800 PressInfo(5),
801 LongPressInfo(6),
802 };
803
804 std::list<TouchLocationInfo> touchInfoList;
805
806 for (auto&& item : pList) {
807 item.SetLocalLocation(Offset(CHILD_OFFSET_WIDTH, CHILD_OFFSET_HEIGHT));
808 item.SetScreenLocation(Offset(CHILD_OFFSET_WIDTH, CHILD_OFFSET_HEIGHT));
809 item.SetTouchType(TouchType::PULL_DOWN);
810 item.SetSize(XCOMPONENT_ID_LEN_MAX);
811 item.SetForce(FORCE);
812 item.SetTiltX(CHILD_OFFSET_WIDTH);
813 item.SetTiltY(CHILD_OFFSET_HEIGHT);
814 item.SetSourceTool(SourceTool::MOUSE);
815 touchInfoList.push_back(item);
816 }
817
818 auto pVector = pattern->SetHistoryPoint(touchInfoList);
819
820 /**
821 * @tc.steps: step3. check
822 */
823 EXPECT_EQ(touchInfoList.size(), pVector.size());
824 for (auto&& item : pVector) {
825 EXPECT_EQ(item.x, CHILD_OFFSET_WIDTH);
826 EXPECT_EQ(item.y, CHILD_OFFSET_HEIGHT);
827 EXPECT_EQ(item.screenX, CHILD_OFFSET_WIDTH);
828 EXPECT_EQ(item.screenY, CHILD_OFFSET_HEIGHT);
829 EXPECT_EQ(static_cast<int>(item.type), static_cast<int>(TouchType::PULL_DOWN));
830 EXPECT_EQ(item.size, XCOMPONENT_ID_LEN_MAX);
831 EXPECT_EQ(item.force, FORCE);
832 EXPECT_EQ(item.titlX, CHILD_OFFSET_WIDTH);
833 EXPECT_EQ(item.titlY, CHILD_OFFSET_HEIGHT);
834 EXPECT_EQ(static_cast<int>(item.sourceTool), static_cast<int>(SourceTool::MOUSE));
835 }
836 }
837
838 /**
839 * @tc.name: XComponentSetDetachEventTest021
840 * @tc.desc: Test XComponent detachEvent event.
841 * @tc.type: FUNC
842 */
843 HWTEST_F(XComponentTestNg, XComponentSetDetachEventTest021, TestSize.Level1)
844 {
845 /**
846 * @tc.steps: step1. set the testProperty and CreateXComponentNode
847 * case: type = XCOMPONENT_SURFACE_TYPE
848 * @tc.expected: frameNode create successfully
849 */
850 testProperty.xcType = XCOMPONENT_SURFACE_TYPE_VALUE;
851 auto frameNode = CreateXComponentNode(testProperty);
852 EXPECT_TRUE(frameNode);
853 EXPECT_EQ(frameNode->GetTag(), V2::XCOMPONENT_ETS_TAG);
854
855 /**
856 * @tc.steps: step2. call FireDetachEvent
857 * @tc.expected: three checkKeys has changed
858 */
859 auto xComponentEventHub = frameNode->GetEventHub<XComponentEventHub>();
860 ASSERT_TRUE(xComponentEventHub);
861
862 bool detachFlage = false;
__anon3398720b0b02(const std::string& xcomponentId) 863 auto detachCallback = [&detachFlage](const std::string& xcomponentId) {
864 detachFlage = true;
865 };
866 xComponentEventHub->SetDetachEvent(std::move(detachCallback));
867
868 xComponentEventHub->FireDetachEvent(XCOMPONENT_ID);
869 ASSERT_TRUE(detachFlage);
870 }
871
872 /**
873 * @tc.name: XComponentFrameCallbackTest022
874 * @tc.desc: Test XComponent RegisterOnFrameCallback and UnregisterOnFrameCallback.
875 * @tc.type: FUNC
876 */
877 HWTEST_F(XComponentTestNg, XComponentFrameCallbackTest022, TestSize.Level1)
878 {
879 /**
880 * @tc.steps: step1. set the testProperty and CreateXComponentNode
881 * case: type = XCOMPONENT_SURFACE_TYPE
882 * @tc.expected: frameNode create successfully
883 */
884 testProperty.xcType = XCOMPONENT_SURFACE_TYPE_VALUE;
885 auto frameNode = CreateXComponentNode(testProperty);
886 EXPECT_TRUE(frameNode);
887 EXPECT_EQ(frameNode->GetTag(), V2::XCOMPONENT_ETS_TAG);
888
889 auto xComponentPattern = frameNode->GetPattern<XComponentPattern>();
890 EXPECT_FALSE(xComponentPattern == nullptr);
891
892 auto pair = xComponentPattern->GetNativeXComponent();
893 auto weakNativeXComponent = pair.second;
894 auto nativeXComponent = weakNativeXComponent.lock();
895 auto nativeXComponentImpl = pair.first;
896 EXPECT_TRUE(nativeXComponent);
897 EXPECT_TRUE(nativeXComponentImpl);
898
899 /**
900 * @tc.steps: step2. call RegisterOnFrameCallback
901 */
902 auto frameCallback = [](OH_NativeXComponent* /* component */, uint64_t /* timestamp */,
__anon3398720b0c02(OH_NativeXComponent* , uint64_t , uint64_t ) 903 uint64_t /* targetTimestamp */) {};
904 nativeXComponent->RegisterOnFrameCallback(frameCallback);
905
906 xComponentPattern->NativeXComponentInit();
907 OH_NativeXComponent_ExpectedRateRange range = {0, 120, 90};
908 nativeXComponent->SetExpectedFrameRateRange(&range);
909
910 /**
911 * @tc.steps: step3. call UnregisterOnFrameCallback
912 */
913 nativeXComponent->UnregisterOnFrameCallback();
914 }
915
916 /**
917 * @tc.name: XComponentEventTest023
918 * @tc.desc: Test XComponent RegisterOnCreate and RegisterOnDestroy register event.
919 * @tc.type: FUNC
920 */
921 HWTEST_F(XComponentTestNg, XComponentEventTest023, TestSize.Level1)
922 {
923 std::string onLoadKey;
924 std::string onDestroyKey;
__anon3398720b0d02(const std::string& ) 925 auto onLoad = [&onLoadKey](const std::string& /* xComponentId */) { onLoadKey = CHECK_KEY; };
__anon3398720b0e02(const std::string& ) 926 auto onDestroy = [&onDestroyKey](const std::string& /* xComponentId */) { onDestroyKey = CHECK_KEY; };
927
928 /**
929 * @tc.steps: step1. set the testProperty and CreateXComponentNode
930 * case: type = XCOMPONENT_COMPONENT_TYPE_VALUE
931 * @tc.expected: frameNode create successfully
932 */
933 testProperty.xcType = XCOMPONENT_COMPONENT_TYPE_VALUE;
934 auto frameNode = CreateXComponentNode(testProperty);
935 EXPECT_TRUE(frameNode);
936 EXPECT_EQ(frameNode->GetTag(), V2::XCOMPONENT_ETS_TAG);
937
938 /**
939 * @tc.steps: step2. call RegisterOnCreate and RegisterOnDestroy register event.
940 * */
941 XComponentModelNG().RegisterOnCreate(frameNode, std::move(onLoad));
942 XComponentModelNG().RegisterOnDestroy(frameNode, std::move(onDestroy));
943
944 auto xComponentPattern = frameNode->GetPattern<XComponentPattern>();
945 EXPECT_FALSE(xComponentPattern == nullptr);
946
947 /**
948 * @tc.steps: step3. call FireLoadEvent, FireDestroyEvent
949 * @tc.expected: three checkKeys not changed
950 */
951 auto xComponentEventHub = frameNode->GetEventHub<XComponentEventHub>();
952 ASSERT_TRUE(xComponentEventHub);
953 xComponentEventHub->FireLoadEvent(XCOMPONENT_ID);
954 xComponentEventHub->FireDestroyEvent(XCOMPONENT_ID);
955 EXPECT_FALSE(onLoadKey == CHECK_KEY);
956 EXPECT_FALSE(onDestroyKey == CHECK_KEY);
957
958 // goto other branch
959 XComponentModelNG::SetXComponentType(Referenced::RawPtr(frameNode), XCOMPONENT_SURFACE_TYPE_VALUE);
960
961 /**
962 * @tc.steps: step4. call RegisterOnCreate and RegisterOnDestroy register event.
963 * */
964 XComponentModelNG().RegisterOnCreate(frameNode, std::move(onLoad));
965 XComponentModelNG().RegisterOnDestroy(frameNode, std::move(onDestroy));
966
967 /**
968 * @tc.steps: step5. call FireLoadEvent, FireDestroyEvent
969 * @tc.expected: three checkKeys has changed
970 */
971 ASSERT_TRUE(xComponentEventHub);
972 xComponentEventHub->FireLoadEvent(XCOMPONENT_ID);
973 xComponentEventHub->FireDestroyEvent(XCOMPONENT_ID);
974 EXPECT_EQ(onLoadKey, CHECK_KEY);
975 EXPECT_EQ(onDestroyKey, CHECK_KEY);
976 }
977
978 /**
979 * @tc.name: XComponentDetachCallbackTest024
980 * @tc.desc: Test XComponent SetDetachCallback test.
981 * @tc.type: FUNC
982 */
983 HWTEST_F(XComponentTestNg, XComponentDetachCallbackTest024, TestSize.Level1)
984 {
985 std::string onDetachKey;
__anon3398720b0f02(const std::string& ) 986 auto onDetach = [&onDetachKey](const std::string& /* xcomponentId */) { onDetachKey = CHECK_KEY; };
987
988 /**
989 * @tc.steps: step1. set the testProperty and CreateXComponentNode
990 * case: XCOMPONENT_SURFACE_TYPE_VALUE
991 * @tc.expected: frameNode create successfully
992 */
993 auto xComponentController = std::make_shared<XComponentControllerNG>();
994 XComponentModelNG xComponent;
995 xComponent.Create(XCOMPONENT_ID, XCOMPONENT_NODE_TYPE_VALUE, XCOMPONENT_LIBRARY_NAME, xComponentController);
996 xComponent.SetSoPath(XCOMPONENT_SO_PATH);
997 xComponent.SetDetachCallback(std::move(onDetach));
998
999 auto frameNode = AceType::Claim(ViewStackProcessor::GetInstance()->GetMainFrameNode());
1000 EXPECT_TRUE(frameNode != nullptr && frameNode->GetTag() == V2::XCOMPONENT_ETS_TAG);
1001
1002 /**
1003 * @tc.steps: step2. call FireDetachEvent
1004 * @tc.expected: three checkKeys has changed
1005 */
1006 auto xComponentEventHub = frameNode->GetEventHub<XComponentEventHub>();
1007 ASSERT_TRUE(xComponentEventHub);
1008 xComponentEventHub->FireDetachEvent(XCOMPONENT_ID);
1009 EXPECT_FALSE(onDetachKey == CHECK_KEY);
1010
1011 onDetachKey.clear();
1012 XComponentModelNG::SetXComponentType(Referenced::RawPtr(frameNode), XCOMPONENT_COMPONENT_TYPE_VALUE);
1013 xComponent.SetDetachCallback(std::move(onDetach));
1014 xComponentEventHub->FireDetachEvent(XCOMPONENT_ID);
1015 EXPECT_FALSE(onDetachKey == CHECK_KEY);
1016
1017 onDetachKey.clear();
1018 XComponentModelNG::SetXComponentType(Referenced::RawPtr(frameNode), XCOMPONENT_SURFACE_TYPE_VALUE);
1019 xComponent.SetDetachCallback(std::move(onDetach));
1020 xComponentEventHub->FireDetachEvent(XCOMPONENT_ID);
1021 EXPECT_EQ(onDetachKey, CHECK_KEY);
1022 }
1023
1024 /**
1025 * @tc.name: XComponentPropertyTest025
1026 * @tc.desc: Create XComponent, and test XComponent type, id, libraryName, soPath interface.
1027 * @tc.type: FUNC
1028 */
1029 HWTEST_F(XComponentTestNg, XComponentPropertyTest025, TestSize.Level1)
1030 {
1031 /**
1032 * @tc.steps: step1. construct a XComponentModelNG
1033 */
1034 const std::shared_ptr<InnerXComponentController> xComponentController;
1035 XComponentModelNG xComponent;
1036
1037 /**
1038 * @tc.steps: step2. call Create and SetSoPath
1039 * case: type = XCOMPONENT_SURFACE_TYPE
1040 * @tc.expected: the properties are expected
1041 */
1042 auto* stack = ViewStackProcessor::GetInstance();
1043 auto nodeId = stack->ClaimNodeId();
1044 auto frameNode = AceType::DynamicCast<FrameNode>(xComponent.Create(nodeId, MAX_WIDTH, MAX_HEIGHT, XCOMPONENT_ID,
1045 XCOMPONENT_SURFACE_TYPE_VALUE, XCOMPONENT_LIBRARY_NAME, xComponentController));
1046
1047 EXPECT_TRUE(frameNode != nullptr && frameNode->GetTag() == V2::XCOMPONENT_ETS_TAG);
1048 auto xComponentPattern = frameNode->GetPattern<XComponentPattern>();
1049 EXPECT_FALSE(xComponentPattern == nullptr);
1050 xComponentPattern->SetSoPath(XCOMPONENT_SO_PATH);
1051 auto xComponentLayoutProperty = frameNode->GetLayoutProperty<XComponentLayoutProperty>();
1052 EXPECT_FALSE(xComponentLayoutProperty == nullptr);
1053
1054 EXPECT_EQ(xComponentPattern->GetId(), XCOMPONENT_ID);
1055 EXPECT_EQ(xComponentPattern->GetLibraryName(), XCOMPONENT_LIBRARY_NAME);
1056 EXPECT_EQ(xComponentPattern->GetSoPath(), XCOMPONENT_SO_PATH);
1057 EXPECT_TRUE(xComponentPattern->IsAtomicNode()); // if xcomponentType = "surface"
1058 EXPECT_EQ(
1059 xComponentLayoutProperty->GetXComponentType().value_or(XComponentType::SURFACE), XCOMPONENT_SURFACE_TYPE_VALUE);
1060
1061 /**
1062 * @tc.steps: step3. call Create and SetSoPath
1063 * case: type = XCOMPONENT_COMPONENT_TYPE
1064 * @tc.expected: the properties are expected
1065 */
1066 const std::shared_ptr<InnerXComponentController> xComponentController2;
1067 XComponentModelNG xComponent2;
1068 xComponent2.Create(XCOMPONENT_ID, XCOMPONENT_COMPONENT_TYPE_VALUE, XCOMPONENT_LIBRARY_NAME, xComponentController);
1069 xComponent2.SetSoPath(XCOMPONENT_SO_PATH);
1070
1071 auto frameNode2 = AceType::DynamicCast<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
1072 EXPECT_TRUE(frameNode2 != nullptr && frameNode2->GetTag() == V2::XCOMPONENT_ETS_TAG);
1073 auto xComponentPattern2 = frameNode2->GetPattern<XComponentPattern>();
1074 EXPECT_FALSE(xComponentPattern2 == nullptr);
1075 auto xComponentLayoutProperty2 = frameNode2->GetLayoutProperty<XComponentLayoutProperty>();
1076 EXPECT_FALSE(xComponentLayoutProperty2 == nullptr);
1077 EXPECT_TRUE(xComponentPattern2->GetSoPath()->empty());
1078 EXPECT_FALSE(xComponentPattern2->IsAtomicNode());
1079 EXPECT_EQ(xComponentLayoutProperty2->GetXComponentType().value_or(XComponentType::SURFACE),
1080 XCOMPONENT_COMPONENT_TYPE_VALUE);
1081 }
1082
1083 /**
1084 * @tc.name: XComponentExtSurfaceCallbackClient026
1085 * @tc.desc: Create XComponentExtSurfaceCallbackClient, and test.
1086 * @tc.type: FUNC
1087 */
1088 HWTEST_F(XComponentTestNg, XComponentExtSurfaceCallbackClient026, TestSize.Level1)
1089 {
1090 /**
1091 * @tc.steps: step1. construct a XComponentModelNG
1092 */
1093 const std::shared_ptr<InnerXComponentController> xComponentController;
1094 XComponentModelNG xComponent;
1095
1096 /**
1097 * @tc.steps: step2. call Create and SetSoPath
1098 * case: type = XCOMPONENT_SURFACE_TYPE
1099 * @tc.expected: the properties are expected
1100 */
1101 auto* stack = ViewStackProcessor::GetInstance();
1102 auto nodeId = stack->ClaimNodeId();
1103 auto frameNode = AceType::DynamicCast<FrameNode>(xComponent.Create(nodeId, MAX_WIDTH, MAX_HEIGHT, XCOMPONENT_ID,
1104 XCOMPONENT_SURFACE_TYPE_VALUE, XCOMPONENT_LIBRARY_NAME, xComponentController));
1105
1106 EXPECT_TRUE(frameNode != nullptr && frameNode->GetTag() == V2::XCOMPONENT_ETS_TAG);
1107 auto xComponentPattern = frameNode->GetPattern<XComponentPattern>();
1108 EXPECT_FALSE(xComponentPattern == nullptr);
1109
1110 /**
1111 * @tc.steps: step2. call FireDetachEvent
1112 * @tc.expected: three checkKeys has changed
1113 */
1114 auto xComponentEventHub = frameNode->GetEventHub<XComponentEventHub>();
1115 ASSERT_TRUE(xComponentEventHub);
1116
1117 std::string surfaceInitFlage;
__anon3398720b1002(const std::string&, const uint32_t, const bool) 1118 auto surfaceInitCallback = [&surfaceInitFlage](const std::string&, const uint32_t, const bool) {
1119 surfaceInitFlage = CHECK_KEY;
1120 };
1121 xComponentEventHub->SetOnSurfaceInitEvent(std::move(surfaceInitCallback));
1122
1123 /**
1124 * @tc.steps: step3. call ProcessSurfaceCreate
1125 */
1126 auto extSurfaceClient = Referenced::MakeRefPtr<XComponentExtSurfaceCallbackClient>(xComponentPattern);
1127 extSurfaceClient->ProcessSurfaceCreate();
1128 EXPECT_EQ(surfaceInitFlage, CHECK_KEY);
1129
1130 /**
1131 * @tc.steps: step4. call ProcessSurfaceChange
1132 */
1133 EXPECT_CALL(*AceType::DynamicCast<MockRenderSurface>(xComponentPattern->renderSurface_),
1134 AdjustNativeWindowSize(MAX_WIDTH, MAX_HEIGHT))
1135 .WillOnce(Return());
1136 extSurfaceClient->ProcessSurfaceChange(MAX_WIDTH, MAX_HEIGHT);
1137
1138 /**
1139 * @tc.steps: step5. call ProcessSurfaceChange
1140 */
1141 extSurfaceClient->ProcessSurfaceDestroy();
1142
1143 // got other branch
1144 /**
1145 * @tc.steps: step6. call XComponentExtSurfaceCallbackClient func
1146 */
1147 auto extSurfaceClient2 = Referenced::MakeRefPtr<XComponentExtSurfaceCallbackClient>(nullptr);
1148 extSurfaceClient2->ProcessSurfaceCreate();
1149 extSurfaceClient2->ProcessSurfaceChange(MAX_WIDTH, MAX_HEIGHT);
1150 extSurfaceClient2->ProcessSurfaceDestroy();
1151 }
1152
1153 /**
1154 * @tc.name: XComponentSurfaceTest
1155 * @tc.desc: Test SurfaceHide/SurfaceShow callback
1156 * @tc.type: FUNC
1157 */
1158 HWTEST_F(XComponentTestNg, XComponentSurfaceTest, TestSize.Level1)
1159 {
1160 /**
1161 * @tc.steps: step1. set type = XCOMPONENT_SURFACE_TYPE and call CreateXComponentNode
1162 * @tc.expected: xcomponent frameNode create successfully
1163 */
1164 testProperty.xcType = XCOMPONENT_SURFACE_TYPE_VALUE;
1165 auto frameNode = CreateXComponentNode(testProperty);
1166 ASSERT_TRUE(frameNode);
1167 auto pattern = frameNode->GetPattern<XComponentPattern>();
1168 ASSERT_TRUE(pattern);
1169 MockPipelineContext::pipeline_->SetMinPlatformVersion(static_cast<int32_t>(PlatformVersion::VERSION_TWELVE));
1170
1171 /**
1172 * @tc.steps: step2. create nativeXComponent instance
1173 * @tc.expected: nativeXComponent instance create successfully
1174 */
1175 auto host = pattern->GetHost();
1176 ASSERT_TRUE(host);
1177 auto pair = pattern->GetNativeXComponent();
1178 auto weakNativeXComponent = pair.second;
1179 auto nativeXComponent = weakNativeXComponent.lock();
1180 auto nativeXComponentImpl = pair.first;
1181 ASSERT_TRUE(nativeXComponent);
1182 ASSERT_TRUE(nativeXComponentImpl);
1183 pattern->hasXComponentInit_ = true;
1184
1185 /**
1186 * @tc.steps: step3. call surfaceHide and surfaceShow event without register callbacks
1187 * @tc.expected: no error happens and g_surfaceShowNum remains the same
1188 */
1189 pattern->OnWindowHide();
1190 EXPECT_EQ(g_surfaceShowNum, 1);
1191 pattern->OnWindowShow();
1192 EXPECT_EQ(g_surfaceShowNum, 1);
1193
1194 /**
1195 * @tc.steps: step4. register surfaceHide/Show event for nativeXComponent instance and trigger callback
1196 * @tc.expected: callback is triggered successfully
1197 */
1198 nativeXComponent->RegisterSurfaceShowCallback(
__anon3398720b1102(OH_NativeXComponent* , void* ) 1199 [](OH_NativeXComponent* /* nativeXComponent */, void* /* window */) { g_surfaceShowNum += 1; });
1200 nativeXComponent->RegisterSurfaceHideCallback(
__anon3398720b1202(OH_NativeXComponent* , void* ) 1201 [](OH_NativeXComponent* /* nativeXComponent */, void* /* window */) { g_surfaceShowNum -= 1; });
1202 EXPECT_CALL(*AceType::DynamicCast<MockRenderSurface>(pattern->renderSurface_), ReleaseSurfaceBuffers())
1203 .WillOnce(Return());
1204 pattern->OnWindowHide();
1205 pattern->OnWindowHide(); // test when hasReleasedSurface_ is not satisfied
1206 EXPECT_EQ(g_surfaceShowNum, 0);
1207 pattern->OnWindowShow();
1208 pattern->OnWindowShow(); // test when hasReleasedSurface_ is not satisfied
1209 EXPECT_EQ(g_surfaceShowNum, 1);
1210
1211 /**
1212 * @tc.steps: step5. call OnWindowHide and OnWindowShoww when the pre-judgment of the function is not satisfied
1213 * @tc.expected: callback will be triggered only once
1214 */
1215 bool initConditions[2] = { true, false };
1216 bool typeConditions[2] = { true, false };
1217 EXPECT_CALL(*AceType::DynamicCast<MockRenderSurface>(pattern->renderSurface_), ReleaseSurfaceBuffers())
1218 .WillOnce(Return());
1219 for (bool initCondition : initConditions) {
1220 for (bool typeCondition : typeConditions) {
1221 pattern->hasXComponentInit_ = initCondition;
1222 pattern->type_ = typeCondition ? XCOMPONENT_TEXTURE_TYPE_VALUE : XCOMPONENT_COMPONENT_TYPE_VALUE;
1223 pattern->OnWindowHide();
1224 if (initCondition && typeCondition) {
1225 EXPECT_EQ(g_surfaceShowNum, 0);
1226 }
1227 pattern->OnWindowShow();
1228 EXPECT_EQ(g_surfaceShowNum, 1);
1229 }
1230 }
1231 }
1232
1233 /**
1234 * @tc.name: XComponentSourceTypeTest
1235 * @tc.desc: Test SourceType
1236 * @tc.type: FUNC
1237 */
1238 HWTEST_F(XComponentTestNg, XComponentSourceTypeTest, TestSize.Level1)
1239 {
1240 /**
1241 * @tc.steps: step1. set type = XCOMPONENT_SURFACE_TYPE and call CreateXComponentNode
1242 * @tc.expected: xcomponent frameNode create successfully
1243 */
1244 testProperty.xcType = XCOMPONENT_SURFACE_TYPE_VALUE;
1245 auto frameNode = CreateXComponentNode(testProperty);
1246 ASSERT_TRUE(frameNode);
1247 auto pattern = frameNode->GetPattern<XComponentPattern>();
1248 ASSERT_TRUE(pattern);
1249
1250 /**
1251 * @tc.steps: step2. call HandleTouchEvent
1252 * case: set source type
1253 * @tc.expected: sourceType fit
1254 */
1255 TouchEventInfo touchEventInfoSourceType("onTouch");
1256 TouchLocationInfo locationInfoSourceType(0);
1257 pattern->GetNativeXComponent();
1258 touchEventInfoSourceType.AddChangedTouchLocationInfo(std::move(locationInfoSourceType));
1259 std::vector<SourceType> sourceTypes { SourceType::NONE, SourceType::MOUSE, SourceType::TOUCH, SourceType::TOUCH_PAD,
1260 SourceType::KEYBOARD };
1261 for (SourceType& sourceType : sourceTypes) {
1262 touchEventInfoSourceType.SetSourceDevice(sourceType);
1263 pattern->HandleTouchEvent(touchEventInfoSourceType);
1264 EXPECT_EQ(pattern->nativeXComponentImpl_->curSourceType_.first, 0);
1265 EXPECT_EQ(static_cast<int>(pattern->nativeXComponentImpl_->curSourceType_.second),
1266 static_cast<int>(ConvertNativeXComponentEventSourceType(sourceType)));
1267 }
1268 }
1269
1270 /**
1271 * @tc.name: XComponentSurfaceLifeCycleCallback
1272 * @tc.desc: Test XComponentController's surface life cycle callback
1273 * @tc.type: FUNC
1274 */
1275 HWTEST_F(XComponentTestNg, XComponentSurfaceLifeCycleCallback, TestSize.Level1)
1276 {
1277 /**
1278 * @tc.steps: step1. set surface life cycle callback, set id&libraryname to null and create XComponent
1279 * @tc.expected: xcomponent frameNode create successfully
1280 */
1281 testProperty.xcType = XCOMPONENT_SURFACE_TYPE_VALUE;
1282 testProperty.xcId = std::nullopt;
1283 testProperty.libraryName = std::nullopt;
1284 std::string onSurfaceCreatedSurfaceId = "";
1285 std::string onSurfaceChangedSurfaceId = "";
1286 std::string onSurfaceDestroyedSurfaceId = "";
__anon3398720b1302(const std::string& surfaceId, const std::string& xcId) 1287 auto onSurfaceCreated = [&onSurfaceCreatedSurfaceId](const std::string& surfaceId, const std::string& xcId) {
1288 onSurfaceCreatedSurfaceId = surfaceId;
1289 };
__anon3398720b1402(const std::string& surfaceId, const RectF& ) 1290 auto onSurfaceChanged = [&onSurfaceChangedSurfaceId](const std::string& surfaceId, const RectF& /* rect */) {
1291 onSurfaceChangedSurfaceId = surfaceId;
1292 };
__anon3398720b1502(const std::string& surfaceId, const std::string& xcId) 1293 auto onSurfaceDestroyed = [&onSurfaceDestroyedSurfaceId](const std::string& surfaceId, const std::string& xcId) {
1294 onSurfaceDestroyedSurfaceId = surfaceId;
1295 };
1296 testProperty.surfaceCreatedEvent = std::move(onSurfaceCreated);
1297 testProperty.surfaceChangedEvent = std::move(onSurfaceChanged);
1298 testProperty.surfaceDestroyedEvent = std::move(onSurfaceDestroyed);
1299 auto frameNode = CreateXComponentNode(testProperty);
1300 ASSERT_TRUE(frameNode);
1301 auto xComponentEventHub = frameNode->GetEventHub<XComponentEventHub>();
1302 ASSERT_TRUE(xComponentEventHub);
1303 EXPECT_FALSE(xComponentEventHub->surfaceInitEvent_);
1304 auto pattern = frameNode->GetPattern<XComponentPattern>();
1305 ASSERT_TRUE(pattern);
1306 pattern->surfaceId_ = SURFACE_ID;
1307
1308 /**
1309 * @tc.steps: step2. call BeforeSyncGeometryProperties
1310 * @tc.expected: onSurfaceCreated & onSurfaceChanged has called and nativeXcomponent will not be created
1311 */
1312 DirtySwapConfig config;
1313 auto xComponentLayoutAlgorithm = AceType::MakeRefPtr<XComponentLayoutAlgorithm>();
1314 RefPtr<GeometryNode> geometryNode = AceType::MakeRefPtr<GeometryNode>();
1315 geometryNode->SetFrameSize(MAX_SIZE);
1316 geometryNode->SetContentSize(MAX_SIZE);
1317 frameNode->geometryNode_ = geometryNode;
1318 EXPECT_CALL(*AceType::DynamicCast<MockRenderSurface>(pattern->renderSurface_), IsSurfaceValid())
1319 .WillOnce(Return(true));
1320 EXPECT_CALL(*AceType::DynamicCast<MockRenderSurface>(pattern->renderSurface_),
1321 AdjustNativeWindowSize(MAX_WIDTH, MAX_HEIGHT))
1322 .WillOnce(Return());
1323 EXPECT_CALL(*AceType::DynamicCast<MockRenderContext>(pattern->handlingSurfaceRenderContext_),
1324 SetBounds(0, 0, MAX_WIDTH, MAX_HEIGHT))
1325 .WillOnce(Return());
1326 pattern->BeforeSyncGeometryProperties(config);
1327 EXPECT_STREQ(SURFACE_ID.c_str(), onSurfaceCreatedSurfaceId.c_str());
1328 EXPECT_STREQ(SURFACE_ID.c_str(), onSurfaceChangedSurfaceId.c_str());
1329 EXPECT_FALSE(pattern->nativeXComponent_);
1330 EXPECT_FALSE(pattern->nativeXComponentImpl_);
1331
1332 /**
1333 * @tc.steps: step3. call OnDetachFromFrameNode
1334 * @tc.expected: onSurfaceDestroyed has called
1335 */
1336 pattern->OnDetachFromFrameNode(AceType::RawPtr(frameNode));
1337 EXPECT_STREQ(SURFACE_ID.c_str(), onSurfaceDestroyedSurfaceId.c_str());
1338 }
1339
1340 /**
1341 * @tc.name: XComponentImageAnalyzerTest
1342 * @tc.desc: Test Enable Image Analyzer
1343 * @tc.type: FUNC
1344 */
1345 HWTEST_F(XComponentTestNg, XComponentImageAnalyzerTest, TestSize.Level1)
1346 {
1347 /**
1348 * @tc.steps: step1. set type = XCOMPONENT_SURFACE_TYPE and call CreateXComponentNode
1349 * @tc.expected: xcomponent frameNode create successfully
1350 */
1351 testProperty.xcType = XCOMPONENT_SURFACE_TYPE_VALUE;
1352 auto frameNode = CreateXComponentNode(testProperty);
1353 ASSERT_TRUE(frameNode);
1354 auto pattern = frameNode->GetPattern<XComponentPattern>();
1355 ASSERT_TRUE(pattern);
1356 pattern->surfaceId_ = SURFACE_ID;
1357
1358 /**
1359 * @tc.steps: step2. call EnableImageAnalyzer
1360 * @tc.expected: IsSupportImageAnalyzerFeature() return right value
1361 */
1362 pattern->EnableAnalyzer(true);
1363 EXPECT_TRUE(pattern->isEnableAnalyzer_);
1364
1365 if (ImageAnalyzerMgr::GetInstance().IsImageAnalyzerSupported()) {
1366 EXPECT_TRUE(pattern->IsSupportImageAnalyzerFeature());
1367 } else {
1368 EXPECT_FALSE(pattern->IsSupportImageAnalyzerFeature());
1369 }
1370
1371 pattern->imageAnalyzerManager_ = nullptr;
1372 EXPECT_FALSE(pattern->IsSupportImageAnalyzerFeature());
1373 }
1374 } // namespace OHOS::Ace::NG
1375