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 <gtest/gtest.h>
17 #include <hilog/log.h>
18 #include <memory>
19 #include <unistd.h>
20 
21 #include "pipeline/rs_base_render_node.h"
22 #include "pipeline/rs_context.h"
23 
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace Rosen {
28 static const int nodeId1 = 1;
29 static const int nodeId2 = 2;
30 
31 class RSDropframeTest : public testing::Test {
32 public:
33     static constexpr HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, 0, "RSDropframeTest" };
34 
35     static inline std::shared_ptr<RSContext> context = nullptr;
36     static inline std::shared_ptr<RSBaseRenderNode> node1 = nullptr;
37     static inline std::shared_ptr<RSBaseRenderNode> node2 = nullptr;
38 
39     static void SetUpTestCase();
40     static void TearDownTestCase();
SetUp()41     void SetUp() override {};
42     void TearDown() override;
43 };
44 
SetUpTestCase()45 void RSDropframeTest::SetUpTestCase()
46 {
47     context = std::make_shared<RSContext>();
48     node1 = std::make_shared<RSBaseRenderNode>(nodeId1, context->weak_from_this());
49     node2 = std::make_shared<RSBaseRenderNode>(nodeId2, context->weak_from_this());
50     context->GetMutableNodeMap().RegisterRenderNode(node1);
51     context->GetMutableNodeMap().RegisterRenderNode(node2);
52 }
53 
TearDownTestCase()54 void RSDropframeTest::TearDownTestCase()
55 {
56     context->GetMutableNodeMap().UnregisterRenderNode(node1->GetId());
57     context->GetMutableNodeMap().UnregisterRenderNode(node2->GetId());
58 
59     node1 = nullptr;
60     node2 = nullptr;
61     context = nullptr;
62 }
63 
TearDown()64 void RSDropframeTest::TearDown()
65 {
66     auto rootNode = context->GetGlobalRootRenderNode();
67     rootNode->ClearChildren();
68     node1->ClearChildren();
69     node2->ClearChildren();
70 }
71 
72 /*
73 * Function: GetGlobalRootRenderNode
74 * Type: Function
75 * Rank: Important(nodeId2)
76 * EnvConditions: N/A
77 * CaseDescription: 1. call GetGlobalRootRenderNode
78 *                  2. check IsOnTheTree
79 */
80 HWTEST_F(RSDropframeTest, GetGlobalRootRenderNode, Function | SmallTest | Level2)
81 {
82     // call GetGlobalRootRenderNode
83     auto rootNode = context->GetGlobalRootRenderNode();
84 
85     // check IsOnTheTree
86     bool rootNodeIsOnTheTree = rootNode->IsOnTheTree();
87     EXPECT_EQ(rootNodeIsOnTheTree, true);
88 }
89 
90 /*
91 * Function: AddChild001
92 * Type: Function
93 * Rank: Important(nodeId2)
94 * EnvConditions: N/A
95 * CaseDescription: 1. call AddChild
96 *                  2. check IsOnTheTree
97 */
98 HWTEST_F(RSDropframeTest, AddChild001, Function | SmallTest | Level2)
99 {
100     // call AddChild
101     auto rootNode = context->GetGlobalRootRenderNode();
102     auto child = context->GetMutableNodeMap().GetRenderNode<RSBaseRenderNode>(nodeId1);
103     rootNode->AddChild(child);
104 
105     // check IsOnTheTree
106     bool isOnTheTree = child->IsOnTheTree();
107     EXPECT_EQ(isOnTheTree, true);
108 }
109 
110 /*
111 * Function: AddChild002
112 * Type: Function
113 * Rank: Important(nodeId2)
114 * EnvConditions: N/A
115 * CaseDescription: 1. call AddChild
116 *                  2. check IsOnTheTree
117 */
118 HWTEST_F(RSDropframeTest, AddChild002, Function | SmallTest | Level2)
119 {
120     // call AddChild
121     auto parent = context->GetMutableNodeMap().GetRenderNode<RSBaseRenderNode>(nodeId1);
122     auto child = context->GetMutableNodeMap().GetRenderNode<RSBaseRenderNode>(nodeId2);
123     parent->AddChild(child, -1);
124 
125     // check IsOnTheTree
126     bool isOnTheTree = child->IsOnTheTree();
127     EXPECT_EQ(isOnTheTree, false);
128 }
129 
130 /*
131 * Function: RemoveChild
132 * Type: Function
133 * Rank: Important(nodeId2)
134 * EnvConditions: N/A
135 * CaseDescription: 1. call RemoveChild
136 *                  2. check IsOnTheTree
137 */
138 HWTEST_F(RSDropframeTest, RemoveChild, Function | SmallTest | Level2)
139 {
140     // build render tree
141     auto rootNode = context->GetGlobalRootRenderNode();
142     auto child1 = context->GetMutableNodeMap().GetRenderNode<RSBaseRenderNode>(nodeId1);
143     auto child2 = context->GetMutableNodeMap().GetRenderNode<RSBaseRenderNode>(nodeId2);
144     child1->AddChild(child2, -1);
145     rootNode->AddChild(child1, -1);
146 
147     // call RemoveChild
148     rootNode->RemoveChild(child1);
149 
150     // check IsOnTheTree
151     bool child1IsOnTheTree = child1->IsOnTheTree();
152     bool child2IsOnTheTree = child2->IsOnTheTree();
153     EXPECT_EQ(child1IsOnTheTree, false);
154     EXPECT_EQ(child2IsOnTheTree, false);
155 }
156 
157 /*
158 * Function: RemoveFromTree
159 * Type: Function
160 * Rank: Important(nodeId2)
161 * EnvConditions: N/A
162 * CaseDescription: 1. call RemoveFromTree
163 *                  2. check IsOnTheTree
164 */
165 HWTEST_F(RSDropframeTest, RemoveFromTree, Function | SmallTest | Level2)
166 {
167     // build render tree
168     auto rootNode = context->GetGlobalRootRenderNode();
169     auto child1 = context->GetMutableNodeMap().GetRenderNode<RSBaseRenderNode>(nodeId1);
170     auto child2 = context->GetMutableNodeMap().GetRenderNode<RSBaseRenderNode>(nodeId2);
171     child1->AddChild(child2, -1);
172     rootNode->AddChild(child1, -1);
173 
174     // call RemoveFromTree
175     child1->RemoveFromTree();
176 
177     // check IsOnTheTree
178     bool child1IsOnTheTree = child1->IsOnTheTree();
179     bool child2IsOnTheTree = child2->IsOnTheTree();
180     EXPECT_EQ(child1IsOnTheTree, false);
181     EXPECT_EQ(child2IsOnTheTree, false);
182 }
183 
184 /*
185 * Function: ClearChildren
186 * Type: Function
187 * Rank: Important(nodeId2)
188 * EnvConditions: N/A
189 * CaseDescription: 1. call ClearChildren
190 *                  2. check IsOnTheTree
191 */
192 HWTEST_F(RSDropframeTest, ClearChildren, Function | SmallTest | Level2)
193 {
194     // build render tree
195     auto rootNode = context->GetGlobalRootRenderNode();
196     auto child1 = context->GetMutableNodeMap().GetRenderNode<RSBaseRenderNode>(nodeId1);
197     auto child2 = context->GetMutableNodeMap().GetRenderNode<RSBaseRenderNode>(nodeId2);
198     rootNode->AddChild(child1, -1);
199     rootNode->AddChild(child2, -1);
200 
201     // call ClearChildren
202     rootNode->ClearChildren();
203 
204     // check IsOnTheTree
205     bool child1IsOnTheTree = child1->IsOnTheTree();
206     bool child2IsOnTheTree = child2->IsOnTheTree();
207     EXPECT_EQ(child1IsOnTheTree, false);
208     EXPECT_EQ(child2IsOnTheTree, false);
209 }
210 } // namespace Rosen
211 } // namespace OHOS