1 /*
2 * Copyright (c) 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 <cstddef>
17 #include <memory>
18 #include <optional>
19
20 #include "gtest/gtest.h"
21 #include "gmock/gmock-actions.h"
22 #include "gmock/gmock-matchers.h"
23
24 #include "core/common/container.h"
25
26 #define private public
27 #define protected public
28
29 #include "bundlemgr/bundle_mgr_interface.h"
30
31 #include "base/memory/ace_type.h"
32 #include "core/components/plugin/plugin_sub_container.h"
33 #include "core/components/plugin/resource/plugin_manager_delegate.h"
34 #include "core/components_ng/base/view_stack_processor.h"
35 #include "core/components_ng/pattern/pattern.h"
36 #include "core/components_ng/pattern/plugin/plugin_event_hub.h"
37 #include "core/components_ng/pattern/plugin/plugin_layout_property.h"
38 #include "core/components_ng/pattern/plugin/plugin_node.h"
39 #include "core/components_ng/pattern/plugin/plugin_pattern.h"
40 #include "test/mock/core/pipeline/mock_pipeline_context.h"
41
42 using namespace testing;
43 using namespace testing::ext;
44
45 namespace OHOS::Ace::NG {
46 namespace {
47 constexpr int32_t NODE_ID_OF_PLUGIN_NODE = 123456;
48 constexpr int32_t NODE_ID_OF_PARENT_NODE = 654321;
49 } // namespace
50
51 class PluginNodeTestNg : public testing::Test {
52 public:
53 static void SetUpTestCase();
54 static void TearDownTestCase();
SetUp()55 void SetUp() override {}
TearDown()56 void TearDown() override {}
57 };
58
SetUpTestCase()59 void PluginNodeTestNg::SetUpTestCase()
60 {
61 MockPipelineContext::SetUp();
62 }
63
TearDownTestCase()64 void PluginNodeTestNg::TearDownTestCase()
65 {
66 MockPipelineContext::TearDown();
67 }
68
69 /**
70 * @tc.name: GetOrCreatePluginNode
71 * @tc.desc: Test GetOrCreatePluginNode in plugin_node.
72 * @tc.type: FUNC
73 */
74 HWTEST_F(PluginNodeTestNg, GetOrCreatePluginNode, TestSize.Level1)
75 {
76 /**
77 * @tc.steps: step1. Create a plugin node by GetOrCreatePluginNode.
78 * @tc.expected: Create node success.
79 */
80 auto* stack = ViewStackProcessor::GetInstance();
81 auto frameNode = PluginNode::GetOrCreatePluginNode(
__anon00b11b380202() 82 "PluginComponent", NODE_ID_OF_PLUGIN_NODE, []() { return AceType::MakeRefPtr<PluginPattern>(); });
83 ASSERT_NE(frameNode, nullptr);
84 stack->Push(frameNode);
85
86 /**
87 * @tc.steps: step2. Get a Plugin node by GetOrCreatePluginNode with same nodeId.
88 * @tc.expected: Get same node success.
89 */
90 auto samepluginNode = PluginNode::GetOrCreatePluginNode(
__anon00b11b380302() 91 "PluginComponent", NODE_ID_OF_PLUGIN_NODE, []() { return AceType::MakeRefPtr<PluginPattern>(); });
92 EXPECT_EQ(frameNode == samepluginNode, true);
93
94 /**
95 * @tc.steps: step3. Remove a Plugin node by GetOrCreatePluginNode with different tag when parent is nullptr.
96 * @tc.expected: Get a different node success.
97 */
98 auto diffPluginNode = PluginNode::GetOrCreatePluginNode(
__anon00b11b380402() 99 "PluginComponent1", NODE_ID_OF_PLUGIN_NODE, []() { return AceType::MakeRefPtr<PluginPattern>(); });
100 EXPECT_NE(frameNode == diffPluginNode, true);
101
102 /**
103 * @tc.steps: step4. Remove a Plugin node by GetOrCreatePluginNode with different tag.
104 * @tc.expected: Get a different node success and remove the node from parent.
105 */
106 auto parentPluginNode = PluginNode::GetOrCreatePluginNode(
__anon00b11b380502() 107 "PluginComponent", NODE_ID_OF_PARENT_NODE, []() { return AceType::MakeRefPtr<PluginPattern>(); });
108 ASSERT_NE(parentPluginNode, nullptr);
109 auto PluginNode = PluginNode::GetOrCreatePluginNode(
__anon00b11b380602() 110 "PluginComponent", NODE_ID_OF_PLUGIN_NODE, []() { return AceType::MakeRefPtr<PluginPattern>(); });
111 PluginNode->SetParent(parentPluginNode);
112 parentPluginNode->AddChild(PluginNode);
113 ASSERT_EQ(parentPluginNode->GetFirstChild(), PluginNode);
114 diffPluginNode = PluginNode::GetOrCreatePluginNode(
__anon00b11b380702() 115 "PluginComponent1", NODE_ID_OF_PLUGIN_NODE, []() { return AceType::MakeRefPtr<PluginPattern>(); });
116 EXPECT_NE(PluginNode == diffPluginNode, true);
117 ASSERT_EQ(AceType::TypeName(diffPluginNode->GetPattern()), "PluginPattern");
118 ASSERT_EQ(parentPluginNode->GetFirstChild(), nullptr);
119
120 /**
121 * @tc.steps: step5. Remove a Plugin node by GetOrCreatePluginNode without patternCreator.
122 * @tc.expected: Get a different node success.
123 */
124 PluginNode = PluginNode::GetOrCreatePluginNode("PluginComponent", NODE_ID_OF_PLUGIN_NODE, nullptr);
125 auto pattrn = PluginNode->GetPattern();
126 ASSERT_EQ(AceType::TypeName(pattrn), "Pattern");
127 }
128 } // namespace OHOS::Ace::NG
129