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 #include "gtest/gtest.h" 16 17 #define protected public 18 #define private public 19 20 #include "core/components_ng/base/group_node.h" 21 #include "core/components_ng/base/view_stack_processor.h" 22 #include "core/components_ng/pattern/pattern.h" 23 #include "core/components_ng/pattern/root/root_pattern.h" 24 25 using namespace testing; 26 using namespace testing::ext; 27 28 namespace OHOS::Ace::NG { 29 namespace { 30 constexpr char TAG_ROOT[] = "root"; 31 constexpr char TAG_CHILD[] = "child"; 32 const auto MOCK_PATTERN_ROOT = AceType::MakeRefPtr<Pattern>(); 33 const auto MOCK_PATTERN_CHILD = AceType::MakeRefPtr<Pattern>(); 34 const auto PATTERN_ROOT = AceType::MakeRefPtr<Pattern>(); 35 const auto FRAME_NODE_ROOT = FrameNode::CreateFrameNode(TAG_ROOT, 1, MOCK_PATTERN_ROOT, true); 36 const auto FRAME_NODE_CHILD = FrameNode::CreateFrameNode(TAG_CHILD, 2, MOCK_PATTERN_ROOT, false); 37 }; // namespace 38 39 class ViewStackProcessorTestNg : public testing::Test {}; 40 41 /** 42 * @tc.name: ViewStackProcessorTestNg001 43 * @tc.desc: Test the operation of view stack processor 44 * @tc.type: FUNC 45 */ 46 HWTEST_F(ViewStackProcessorTestNg, ViewStackProcessorTestNg001, TestSize.Level1) 47 { 48 /** 49 * @tc.steps: step1. push isCustomView = false 50 * @tc.expected: removeSilently_ is false 51 */ 52 bool customViews[2] = { true, false }; 53 for (int i = 0; i < 2; ++i) { 54 ViewStackProcessor::GetInstance()->Push(FRAME_NODE_ROOT, customViews[i]); 55 auto node = ViewStackProcessor::GetInstance()->Finish(); 56 EXPECT_FALSE(node->removeSilently_); 57 } 58 } 59 60 /** 61 * @tc.name: ViewStackProcessorTestNg002 62 * @tc.desc: Test the operation of view stack processor 63 * @tc.type: FUNC 64 */ 65 HWTEST_F(ViewStackProcessorTestNg, ViewStackProcessorTestNg002, TestSize.Level1) 66 { 67 /** 68 * @tc.steps: step1. push childFrameNode 69 * @tc.expected: mainFrameNode's tag is child 70 */ 71 ViewStackProcessor::GetInstance()->Push(FRAME_NODE_CHILD); 72 ViewStackProcessor::GetInstance()->Push(FRAME_NODE_ROOT); 73 ViewStackProcessor::GetInstance()->ImplicitPopBeforeContinue(); 74 auto topFrameNodeOne = ViewStackProcessor::GetInstance()->GetMainElementNode(); 75 EXPECT_EQ(strcmp(topFrameNodeOne->GetTag().c_str(), TAG_CHILD), 0); 76 /** 77 * @tc.steps: step2. ImplicitPopBeforeContinue 78 * @tc.expected: mainFrameNode's tag is child 79 */ 80 ViewStackProcessor::GetInstance()->ImplicitPopBeforeContinue(); 81 auto topFrameNodeTwo = ViewStackProcessor::GetInstance()->Finish(); 82 EXPECT_EQ(strcmp(topFrameNodeTwo->GetTag().c_str(), TAG_CHILD), 0); 83 } 84 85 /** 86 * @tc.name: ViewStackProcessorTestNg003 87 * @tc.desc: Test the operation of view stack processor 88 * @tc.type: FUNC 89 */ 90 HWTEST_F(ViewStackProcessorTestNg, ViewStackProcessorTestNg003, TestSize.Level1) 91 { 92 /** 93 * @tc.steps: step1. ImplicitPopBeforeContinue 94 * @tc.expected: frameNode's isMeasureBoundary_ is false 95 */ 96 ViewStackProcessor::GetInstance()->Push(FRAME_NODE_ROOT); 97 ViewStackProcessor::GetInstance()->FlushImplicitAnimation(); 98 FRAME_NODE_ROOT->onMainTree_ = true; 99 ViewStackProcessor::GetInstance()->FlushImplicitAnimation(); 100 ViewStackProcessor::GetInstance()->FlushRerenderTask(); 101 ViewStackProcessor::GetInstance()->Push(FRAME_NODE_ROOT); 102 ViewStackProcessor::GetInstance()->SetPredict(nullptr); 103 ViewStackProcessor::GetInstance()->FlushRerenderTask(); 104 ViewStackProcessor::GetInstance()->FlushRerenderTask(); 105 EXPECT_FALSE(FRAME_NODE_ROOT->isMeasureBoundary_); 106 } 107 108 /** 109 * @tc.name: ViewStackProcessorTestNg004 110 * @tc.desc: Test the operation of view stack processor 111 * @tc.type: FUNC 112 */ 113 HWTEST_F(ViewStackProcessorTestNg, ViewStackProcessorTestNg004, TestSize.Level1) 114 { 115 /** 116 * @tc.steps: step1. push key one and two 117 * @tc.expected: GetKey is "one_two" 118 */ 119 const std::string keyOne("one"); 120 const std::string keyTwo("two"); 121 ViewStackProcessor::GetInstance()->PushKey(keyOne); 122 ViewStackProcessor::GetInstance()->PushKey(keyTwo); 123 EXPECT_EQ(strcmp(ViewStackProcessor::GetInstance()->GetKey().c_str(), "one_two"), 0); 124 /** 125 * @tc.steps: step2. pop key one and two 126 * @tc.expected: GetKey is "" 127 */ 128 ViewStackProcessor::GetInstance()->PopKey(); 129 ViewStackProcessor::GetInstance()->PopKey(); 130 EXPECT_EQ(strcmp(ViewStackProcessor::GetInstance()->GetKey().c_str(), ""), 0); 131 ViewStackProcessor::GetInstance()->ProcessViewId("three"); 132 EXPECT_EQ(strcmp(ViewStackProcessor::GetInstance()->GetKey().c_str(), ""), 0); 133 /** 134 * @tc.steps: step3. push a empty key and do pop key 135 * @tc.expected: GetKey is "" 136 */ 137 ViewStackProcessor::GetInstance()->PushKey(""); 138 ViewStackProcessor::GetInstance()->PopKey(); 139 EXPECT_EQ(strcmp(ViewStackProcessor::GetInstance()->GetKey().c_str(), ""), 0); 140 /** 141 * @tc.steps: step4. create ScopedViewStackProcessor 142 * @tc.expected: not nullptr 143 */ 144 auto scoped = std::make_shared<ScopedViewStackProcessor>(); 145 EXPECT_NE(scoped->instance_, nullptr); 146 scoped = nullptr; 147 } 148 149 /** 150 * @tc.name: ViewStackProcessorTestNg005 151 * @tc.desc: Test the SetVisualState and GetVisualState in view stack processor 152 * @tc.type: FUNC 153 */ 154 HWTEST_F(ViewStackProcessorTestNg, ViewStackProcessorTestNg005, TestSize.Level1) 155 { 156 /** 157 * @tc.steps: step1. get instance and set different states and show result 158 * @tc.expected: GetVisualState value meeting expectations. 159 */ 160 auto instance = ViewStackProcessor::GetInstance(); 161 instance->SetVisualState(VisualState::DISABLED); 162 EXPECT_EQ(instance->GetVisualState(), 4); 163 instance->SetVisualState(VisualState::FOCUSED); 164 EXPECT_EQ(instance->GetVisualState(), 2); 165 instance->SetVisualState(VisualState::PRESSED); 166 EXPECT_EQ(instance->GetVisualState(), 1); 167 instance->SetVisualState(VisualState::NORMAL); 168 EXPECT_EQ(instance->GetVisualState(), 0); 169 instance->SetVisualState(VisualState::HOVER); 170 EXPECT_EQ(instance->GetVisualState(), 0); 171 /** 172 * @tc.steps: step2. clear visual state 173 * @tc.expected: IsCurrentVisualStateProcess meeting expectations. 174 */ 175 EXPECT_FALSE(instance->IsCurrentVisualStateProcess()); 176 instance->ClearVisualState(); 177 EXPECT_TRUE(instance->IsCurrentVisualStateProcess()); 178 } 179 180 /** 181 * @tc.name: ViewStackProcessorTestNg006 182 * @tc.desc: Test the Push and PopContainer in view stack processor 183 * @tc.type: FUNC 184 */ 185 HWTEST_F(ViewStackProcessorTestNg, ViewStackProcessorTestNg006, TestSize.Level1) 186 { 187 /** 188 * @tc.steps: step1. Get instance and try PopContainer 189 * @tc.expected: actually nothing happened because elementsStack no value. 190 */ 191 auto instance = ViewStackProcessor::GetInstance(); 192 instance->PopContainer(); 193 EXPECT_EQ(instance->elementsStack_.size(), 0); 194 /** 195 * @tc.steps: step2. Create some node 196 */ 197 const auto root = FrameNode::CreateFrameNode(TAG_CHILD, 0, AceType::MakeRefPtr<RootPattern>(), true); 198 const auto child = FrameNode::CreateFrameNode(TAG_CHILD, 1, AceType::MakeRefPtr<RootPattern>(), true); 199 const auto child2 = FrameNode::CreateFrameNode(TAG_CHILD, 2, AceType::MakeRefPtr<Pattern>(), true); 200 /** 201 * @tc.steps: step3. Push root into container 202 * @tc.expected: Pop failed elementsStack_ size still 1. 203 */ 204 instance->Push(root); 205 instance->PopContainer(); 206 EXPECT_EQ(instance->elementsStack_.size(), 1); 207 /** 208 * @tc.steps: step4. pop root and put FRAME_NODE_ROOT into container 209 * @tc.expected: Pop failed elementsStack_ size still 1. 210 */ 211 instance->elementsStack_.pop(); 212 instance->Push(FRAME_NODE_ROOT); 213 instance->PopContainer(); 214 EXPECT_EQ(instance->elementsStack_.size(), 1); 215 /** 216 * @tc.steps: step5. push child2 and child into instance 217 * @tc.expected: Pop success elementsStack_ size residue 1. 218 */ 219 instance->Push(child2); 220 instance->Push(child); 221 instance->ImplicitPopBeforeContinue(); 222 EXPECT_EQ(instance->elementsStack_.size(), 2); 223 instance->PopContainer(); 224 EXPECT_EQ(instance->elementsStack_.size(), 1); 225 /** 226 * @tc.steps: step6. push child2 again 227 * @tc.expected: Pop success elementsStack_ size residue 1. 228 */ 229 instance->Push(child2); 230 instance->PopContainer(); 231 EXPECT_EQ(instance->elementsStack_.size(), 1); 232 } 233 } // namespace OHOS::Ace::NG 234