• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "gtest/gtest.h"
17 #define private public
18 #define protected public
19 #include "core/components_ng/base/ui_node_gc.h"
20 #include "core/components_ng/base/frame_node.h"
21 #include "core/components_ng/pattern/linear_layout/linear_layout_pattern.h"
22 #undef private
23 #undef protected
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS::Ace::NG {
28 class UINodeGCTestNg : public testing::Test {
29 public:
SetUpTestCase()30     static void SetUpTestCase() {}
TearDownTestCase()31     static void TearDownTestCase() {}
SetUp()32     void SetUp() {};
TearDown()33     void TearDown() {};
34 };
35 
36 /**
37  * @tc.name: IsTooLateTest
38  * @tc.desc: IsTooLate test
39  * @tc.type: FUNC
40  */
41 HWTEST_F(UINodeGCTestNg, IsTooLateTest, TestSize.Level1)
42 {
43     auto ret = UiNodeGc::IsTooLate(-1);
44     EXPECT_EQ(ret, false);
45 
46     ret = UiNodeGc::IsTooLate(0);
47     EXPECT_EQ(ret, true);
48 }
49 
50 /**
51  * @tc.name: ReleaseInner
52  * @tc.desc: ReleaseInner test
53  * @tc.type: FUNC
54  */
55 HWTEST_F(UINodeGCTestNg, ReleaseInnerTest, TestSize.Level1)
56 {
57     auto& nodeRawBucket = UiNodeGc::MockGetNodeRawBucket();
58     nodeRawBucket.push({
59         nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // 11 rawptr
60     });
61     EXPECT_EQ(nodeRawBucket.size(), 1);
62     UiNodeGc::ReleaseInner(nullptr);
63     EXPECT_EQ(nodeRawBucket.size(), 2);
64     UiNodeGc::ReleaseNodeRawBucket();
65     EXPECT_EQ(nodeRawBucket.size(), 1);
66 }
67 
68 /**
69  * @tc.name: JudgeGCLevelTest
70  * @tc.desc: JudgeGCLevel test
71  * @tc.type: FUNC
72  */
73 HWTEST_F(UINodeGCTestNg, JudgeGCLevelTest, TestSize.Level1)
74 {
75     auto ret = UiNodeGc::JudgeGCLevel(10, -1);
76     EXPECT_EQ(ret, PriorityType::IDLE);
77 
78     ret = UiNodeGc::JudgeGCLevel(10, 0);
79     EXPECT_EQ(ret, PriorityType::LOW);
80 
81     ret = UiNodeGc::JudgeGCLevel(80, 0);
82     EXPECT_EQ(ret, PriorityType::HIGH);
83 
84     ret = UiNodeGc::JudgeGCLevel(300, 0);
85     EXPECT_EQ(ret, PriorityType::IMMEDIATE);
86 
87     ret = UiNodeGc::JudgeGCLevel(600, 0);
88     EXPECT_EQ(ret, PriorityType::VIP);
89 }
90 
91 /**
92  * @tc.name: ReleaseNodeRawBucketTest
93  * @tc.desc: ReleaseNodeRawBucket test
94  * @tc.type: FUNC
95  */
96 HWTEST_F(UINodeGCTestNg, ReleaseNodeRawBucketTest, TestSize.Level1)
97 {
98     auto& nodeRawBucket = UiNodeGc::MockGetNodeRawBucket();
99     UiNodeGc::ReleaseNodeRawBucket();
100     EXPECT_EQ(nodeRawBucket.size(), 0);
101 
102     RefPtr<Pattern> pattern;
103     auto frameNode1 = FrameNode::CreateFrameNode(V2::COLUMN_ETS_TAG,
104                 ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<LinearLayoutPattern>(true));
105     frameNode1->uiNodeGcEnable_ = true;
106     frameNode1 = nullptr;
107     EXPECT_EQ(nodeRawBucket.size(), 1);
108 
109     auto frameNode2 = FrameNode::CreateFrameNode(V2::COLUMN_ETS_TAG,
110                 ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<LinearLayoutPattern>(true));
111     frameNode2->uiNodeGcEnable_ = true;
112     frameNode2 = nullptr;
113     EXPECT_EQ(nodeRawBucket.size(), 1);
114 
115     auto& bucket = nodeRawBucket.back();
116     EXPECT_EQ(bucket.size(), 2);
117 
118     UiNodeGc::ReleaseNodeRawBucket();
119     EXPECT_EQ(nodeRawBucket.size(), 0);
120 }
121 
122 /**
123  * @tc.name: ReleaseNodeRawMemoryInnerTest
124  * @tc.desc: ReleaseNodeRawMemoryInner test
125  * @tc.type: FUNC
126  */
127 HWTEST_F(UINodeGCTestNg, ReleaseNodeRawMemoryInnerTest, TestSize.Level1)
128 {
129     auto& nodeRawBucket = UiNodeGc::MockGetNodeRawBucket();
130     UiNodeGc::ReleaseNodeRawMemoryInner(nullptr);
131 
132     RefPtr<Pattern> pattern;
133     auto frameNode1 = FrameNode::CreateFrameNode(V2::COLUMN_ETS_TAG,
134                 ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<LinearLayoutPattern>(true));
135     frameNode1->uiNodeGcEnable_ = true;
136     frameNode1 = nullptr;
137     EXPECT_EQ(nodeRawBucket.size(), 1);
138     UiNodeGc::ReleaseNodeRawMemory(1, nullptr);
139     EXPECT_EQ(nodeRawBucket.size(), 1);
140 
141     UiNodeGc::ReleaseNodeRawMemory(-1, nullptr);
142     EXPECT_EQ(nodeRawBucket.size(), 0);
143 }
144 
145 /**
146  * @tc.name: OnReleaseFunc
147  * @tc.desc: OnReleaseFunc test
148  * @tc.type: FUNC
149  */
150 HWTEST_F(UINodeGCTestNg, OnReleaseFuncTest, TestSize.Level1)
151 {
152     auto& nodeRawBucket = UiNodeGc::MockGetNodeRawBucket();
153     UiNodeGc::OnReleaseFunc(nullptr);
154     EXPECT_EQ(nodeRawBucket.size(), 0);
155 }
156 }