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 <optional>
17 
18 #include "gtest/gtest.h"
19 
20 #include "base/geometry/ng/offset_t.h"
21 #include "base/geometry/ng/size_t.h"
22 #include "base/memory/ace_type.h"
23 #include "core/components_ng/base/frame_node.h"
24 #include "core/components_ng/base/geometry_node.h"
25 #include "core/components_ng/manager/full_screen/full_screen_manager.h"
26 #include "core/components_ng/pattern/pattern.h"
27 #include "test/mock/core/pipeline/mock_pipeline_context.h"
28 
29 using namespace testing;
30 using namespace testing::ext;
31 
32 namespace OHOS::Ace::NG {
33 namespace {
34 const float MAX_WIDTH = 400.0f;
35 const float MAX_HEIGHT = 400.0f;
36 const SizeF MAX_SIZE(MAX_WIDTH, MAX_HEIGHT);
37 const OffsetF FRAME_OFFSET(10.0f, 10.0f);
38 const std::string ROOT_TAG("root");
39 const std::string CURRENT_TAG("current");
40 const std::string PARENT_TAG("parent");
41 const float ROOT_WIDTH = 1.0f;
42 const float ROOT_HEIGHT = 1.0f;
43 } // namespace
44 
45 class FullScreenManagerTestNg : public testing::Test {
46 public:
47     static void SetUpTestCase();
48     static void TearDownTestCase();
49 };
50 
SetUpTestCase()51 void FullScreenManagerTestNg::SetUpTestCase()
52 {
53     MockPipelineContext::SetUp();
54 }
55 
TearDownTestCase()56 void FullScreenManagerTestNg::TearDownTestCase()
57 {
58     MockPipelineContext::TearDown();
59 }
60 
61 /**
62  * @tc.name: FullScreenManagerTest001
63  * @tc.desc: RequestFullScreen Successfully and then ExitFullScreen Successfully
64  * @tc.type: FUNC
65  * @tc.author:
66  */
67 HWTEST_F(FullScreenManagerTestNg, FullScreenManagerTest001, TestSize.Level1)
68 {
69     /**
70      * @tc.steps: step1. construct a FullScreenManager
71      */
72     auto root = AceType::MakeRefPtr<FrameNode>(ROOT_TAG, -1, AceType::MakeRefPtr<Pattern>(), true);
73     auto fullScreenManager = AceType::MakeRefPtr<FullScreenManager>(root);
74 
75     /**
76      * @tc.steps: step2. construct a FrameNode needed to be fullScreened
77      */
78     auto frameNode = AceType::MakeRefPtr<FrameNode>(CURRENT_TAG, -1, AceType::MakeRefPtr<Pattern>(), false);
79 
80     /**
81      * @tc.steps: step3. set the parentNode and layout property of the FrameNode constructed on step2
82      */
83     auto parentNode = AceType::MakeRefPtr<FrameNode>(PARENT_TAG, -1, AceType::MakeRefPtr<Pattern>(), false);
84     frameNode->SetParent(AceType::WeakClaim(AceType::RawPtr(parentNode)));
85     auto geometryNode = AceType::MakeRefPtr<GeometryNode>();
86     geometryNode->SetMarginFrameOffset(FRAME_OFFSET);
87     geometryNode->SetFrameSize(MAX_SIZE);
88     CalcSize idealSize = { CalcLength(MAX_WIDTH), CalcLength(MAX_HEIGHT) };
89     frameNode->SetGeometryNode(geometryNode);
90 
91     /**
92      * @tc.steps: step4. call RequestFullScreen function
93      */
94     CalcSize idealSizeFS = { CalcLength(ROOT_WIDTH), CalcLength(ROOT_HEIGHT) };
95     MockPipelineContext::GetCurrent()->SetRootSize(ROOT_WIDTH, ROOT_HEIGHT);
96     MeasureProperty layoutConstraint;
97     layoutConstraint.selfIdealSize = idealSizeFS;
98     layoutConstraint.maxSize = idealSizeFS;
99     frameNode->UpdateLayoutConstraint(layoutConstraint);
100     fullScreenManager->RequestFullScreen(frameNode);
101 
102     /**
103      * @tc.expected: step4. the layout property of the FrameNode match the demand of full screen
104      */
105     auto selfIdealSizeFullScreen = frameNode->GetLayoutProperty()->GetCalcLayoutConstraint()->selfIdealSize;
106     auto maxSizeFullScreen = frameNode->GetLayoutProperty()->GetCalcLayoutConstraint()->maxSize;
107     EXPECT_EQ(idealSizeFS, selfIdealSizeFullScreen);
108     EXPECT_EQ(idealSizeFS, maxSizeFullScreen);
109     auto geometryNodeRequestFS = frameNode->GetGeometryNode();
110     auto marginFrameOffsetRequestFS = geometryNodeRequestFS->GetMarginFrameOffset();
111     EXPECT_EQ(marginFrameOffsetRequestFS, OffsetF(0, 0));
112 
113     /**
114      * @tc.expected: step4. the parent node of the FrameNode is the root
115      */
116     auto parentTag = frameNode->GetParent()->GetTag();
117     EXPECT_EQ(parentTag, ROOT_TAG);
118 
119     /**
120      * @tc.steps: step5. call ExitFullScreen function
121      */
122     fullScreenManager->ExitFullScreen(frameNode);
123 
124     /**
125      * @tc.expected: step5. the layout property and the parent node of the FrameNode has recovered
126      */
127     auto selfIdealSizeExitFullScreen = frameNode->GetLayoutProperty()->GetCalcLayoutConstraint()->selfIdealSize;
128     auto geometryNodeExitFS = frameNode->GetGeometryNode();
129     auto marginFrameOffsetExitFS = geometryNodeExitFS->GetMarginFrameOffset();
130     EXPECT_EQ(marginFrameOffsetExitFS, FRAME_OFFSET);
131     parentTag = frameNode->GetParent()->GetTag();
132     EXPECT_EQ(parentTag, PARENT_TAG);
133 }
134 
135 /**
136  * @tc.name: FullScreenManagerTest002
137  * @tc.desc: ExitFullScreen without RequestFullScreen
138  * @tc.type: FUNC
139  * @tc.author:
140  */
141 HWTEST_F(FullScreenManagerTestNg, FullScreenManagerTest002, TestSize.Level1)
142 {
143     /**
144      * @tc.steps: step1. construct a FullScreenManager
145      */
146     auto root = AceType::MakeRefPtr<FrameNode>(ROOT_TAG, -1, AceType::MakeRefPtr<Pattern>(), true);
147     auto fullScreenManager = AceType::MakeRefPtr<FullScreenManager>(root);
148 
149     /**
150      * @tc.steps: step2. construct a FrameNode needed to be fullScreened
151      */
152     auto frameNode = AceType::MakeRefPtr<FrameNode>(CURRENT_TAG, -1, AceType::MakeRefPtr<Pattern>(), false);
153 
154     /**
155      * @tc.steps: step3. set the parentNode and layout property of the FrameNode constructed on step2
156      */
157     auto parentNode = AceType::MakeRefPtr<FrameNode>(PARENT_TAG, -1, AceType::MakeRefPtr<Pattern>(), false);
158     frameNode->SetParent(AceType::WeakClaim(AceType::RawPtr(parentNode)));
159     auto geometryNode = AceType::MakeRefPtr<GeometryNode>();
160     geometryNode->SetMarginFrameOffset(FRAME_OFFSET);
161     geometryNode->SetFrameSize(MAX_SIZE);
162     CalcSize idealSize = { CalcLength(MAX_WIDTH), CalcLength(MAX_HEIGHT) };
163     frameNode->SetGeometryNode(geometryNode);
164 
165     /**
166      * @tc.steps: step4. call the ExitFullScreen function without calling RequestFullScreen function before
167      */
168     fullScreenManager->ExitFullScreen(frameNode);
169 
170     /**
171      * @tc.expected: step4. all property of the FrameNode has no changes
172      */
173     EXPECT_FALSE(frameNode->GetLayoutProperty()->GetCalcLayoutConstraint());
174     auto geometryNodeExitFS = frameNode->GetGeometryNode();
175     auto marginFrameOffsetExitFS = geometryNodeExitFS->GetMarginFrameOffset();
176     EXPECT_EQ(marginFrameOffsetExitFS, FRAME_OFFSET);
177     auto parentTag = frameNode->GetParent()->GetTag();
178     EXPECT_EQ(parentTag, PARENT_TAG);
179 }
180 
181 /**
182  * @tc.name: FullScreenManagerTest003
183  * @tc.desc: RequestFullScreen twice in a row
184  * @tc.type: FUNC
185  * @tc.author:
186  */
187 HWTEST_F(FullScreenManagerTestNg, FullScreenManagerTest003, TestSize.Level1)
188 {
189     /**
190      * @tc.steps: step1. construct a FullScreenManager
191      */
192     auto root = AceType::MakeRefPtr<FrameNode>(ROOT_TAG, -1, AceType::MakeRefPtr<Pattern>(), true);
193     auto fullScreenManager = AceType::MakeRefPtr<FullScreenManager>(root);
194 
195     /**
196      * @tc.steps: step2. construct a FrameNode needed to be fullScreened
197      */
198     auto frameNode = AceType::MakeRefPtr<FrameNode>(CURRENT_TAG, -1, AceType::MakeRefPtr<Pattern>(), false);
199 
200     /**
201      * @tc.steps: step3. set the parentNode and layout property of the FrameNode constructed on step2
202      */
203     auto parentNode = AceType::MakeRefPtr<FrameNode>(PARENT_TAG, -1, AceType::MakeRefPtr<Pattern>(), false);
204     frameNode->SetParent(AceType::WeakClaim(AceType::RawPtr(parentNode)));
205     auto geometryNode = AceType::MakeRefPtr<GeometryNode>();
206     geometryNode->SetMarginFrameOffset(FRAME_OFFSET);
207     geometryNode->SetFrameSize(MAX_SIZE);
208     CalcSize idealSize = { CalcLength(MAX_WIDTH), CalcLength(MAX_HEIGHT) };
209     frameNode->SetGeometryNode(geometryNode);
210 
211     /**
212      * @tc.steps: step4. call RequestFullScreen function firstly
213      * @tc.expected: step4. the layout property of the FrameNode match the demand of full screen and
214      *                      the parent node of the FrameNode is the root
215      */
216     MockPipelineContext::GetCurrent()->SetRootSize(ROOT_WIDTH, ROOT_HEIGHT);
217     CalcSize idealSizeFS = { CalcLength(ROOT_WIDTH), CalcLength(ROOT_HEIGHT) };
218     MeasureProperty layoutConstraint;
219     layoutConstraint.selfIdealSize = idealSizeFS;
220     layoutConstraint.maxSize = idealSizeFS;
221     frameNode->UpdateLayoutConstraint(layoutConstraint);
222     fullScreenManager->RequestFullScreen(frameNode);
223     auto selfIdealSizeFullScreen = frameNode->GetLayoutProperty()->GetCalcLayoutConstraint()->selfIdealSize;
224     auto maxSizeFullScreen = frameNode->GetLayoutProperty()->GetCalcLayoutConstraint()->maxSize;
225     EXPECT_EQ(idealSizeFS, selfIdealSizeFullScreen);
226     EXPECT_EQ(idealSizeFS, maxSizeFullScreen);
227     auto geometryNodeRequestFS = frameNode->GetGeometryNode();
228     auto marginFrameOffsetRequestFS = geometryNodeRequestFS->GetMarginFrameOffset();
229     EXPECT_EQ(marginFrameOffsetRequestFS, OffsetF(0, 0));
230     auto parentTag = frameNode->GetParent()->GetTag();
231     EXPECT_EQ(parentTag, ROOT_TAG);
232 
233     /**
234      * @tc.steps: step5. set the MarginFrameOffset of the FrameNode not (0, 0) value
235      */
236     frameNode->GetGeometryNode()->SetMarginFrameOffset(FRAME_OFFSET);
237 
238     /**
239      * @tc.steps: step6. call RequestFullScreen function again
240      * @tc.expected: step6. RequestFullScreen failed, the MarginFrameOffset wouldn't turn to (0, 0)
241      */
242     fullScreenManager->RequestFullScreen(frameNode);
243     marginFrameOffsetRequestFS = geometryNodeRequestFS->GetMarginFrameOffset();
244     EXPECT_EQ(marginFrameOffsetRequestFS, FRAME_OFFSET);
245 }
246 } // namespace OHOS::Ace::NG
247