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 #define private public
20 #define protected public
21 
22 #include "test/mock/base/mock_task_executor.h"
23 #include "test/mock/core/common/mock_container.h"
24 #include "test/mock/core/pipeline/mock_pipeline_context.h"
25 
26 #include "base/geometry/ng/offset_t.h"
27 #include "base/geometry/ng/rect_t.h"
28 #include "base/geometry/ng/size_t.h"
29 #include "base/memory/ace_type.h"
30 #include "core/common/container.h"
31 #include "core/components_ng/base/frame_node.h"
32 #include "core/components_ng/base/geometry_node.h"
33 #include "core/components_ng/manager/select_overlay/select_overlay_manager.h"
34 #include "core/components_ng/manager/select_overlay/select_overlay_proxy.h"
35 #include "core/components_ng/pattern/pattern.h"
36 #include "core/components_ng/pattern/select_overlay/select_overlay_pattern.h"
37 #include "core/components_ng/pattern/select_overlay/select_overlay_property.h"
38 
39 using namespace testing;
40 using namespace testing::ext;
41 namespace OHOS::Ace::NG {
42 namespace {
43 constexpr int32_t NODE_ID = 143;
44 constexpr int32_t NODE_ID_2 = 601;
45 const RectF FIRST_HANDLE_INFO_RECT(0, 0, 100.0f, 100.0f);
46 const RectF SECOND_HANDLE_INFO_RECT(0, 0, 200.0f, 200.0f);
47 } // namespace
48 
49 class SelectOverlayProxyTestNg : public testing::Test {
50 public:
51     static void SetUpTestCase();
52     static void TearDownTestCase();
53 };
54 
SetUpTestCase()55 void SelectOverlayProxyTestNg::SetUpTestCase()
56 {
57     MockContainer::SetUp();
58     MockPipelineContext::SetUp();
59     MockContainer::Current()->taskExecutor_ = AceType::MakeRefPtr<MockTaskExecutor>();
60 }
61 
TearDownTestCase()62 void SelectOverlayProxyTestNg::TearDownTestCase()
63 {
64     MockPipelineContext::TearDown();
65     MockContainer::TearDown();
66 }
67 
68 /**
69  * @tc.name: SelectOverlayProxyTest001
70  * @tc.desc: test the Functions about updating the property of selectOverlayInfo
71  * @tc.type: FUNC
72  * @tc.author:
73  */
74 HWTEST_F(SelectOverlayProxyTestNg, SelectOverlayProxyTest001, TestSize.Level1)
75 {
76     /**
77      * @tc.steps: step1. construct a SelectOverlayProxy
78      * @tc.expected: step1. selectOverlayId = NODE_ID
79      */
80     auto selectOverlayProxy = AceType::MakeRefPtr<SelectOverlayProxy>(NODE_ID);
81     auto overlayId = selectOverlayProxy->GetSelectOverlayId();
82     EXPECT_EQ(overlayId, NODE_ID);
83 
84     /**
85      * @tc.steps: step2. call the relevant Functions to update the property of selectOverlayInfo
86      * @tc.expected: step2. no fatal failures occour, some logs will be print
87      *                      they are defined in "mock_select_overlay_pattern.cpp"
88      */
89     // call UpdateFirstSelectHandleInfo
90     SelectHandleInfo selectHandleInfo;
91     selectHandleInfo.paintRect = FIRST_HANDLE_INFO_RECT;
92     selectOverlayProxy->UpdateFirstSelectHandleInfo(selectHandleInfo);
93 
94     // call UpdateSecondSelectHandleInfo
95     SelectHandleInfo selectHandleInfo2;
96     selectHandleInfo.paintRect = SECOND_HANDLE_INFO_RECT;
97     selectOverlayProxy->UpdateSecondSelectHandleInfo(selectHandleInfo2);
98 
99     // call UpdateSelectMenuInfo
100     SelectMenuInfo selectMenuInfo;
101     selectOverlayProxy->UpdateSelectMenuInfo(selectMenuInfo);
102 
103     // call UpdateShowArea
104     RectF showArea;
105     selectOverlayProxy->UpdateShowArea(showArea);
106 }
107 
108 /**
109  * @tc.name: SelectOverlayProxyTest002
110  * @tc.desc: test the Functions about closing the proxy
111  * @tc.type: FUNC
112  * @tc.author:
113  */
114 HWTEST_F(SelectOverlayProxyTestNg, SelectOverlayProxyTest002, TestSize.Level1)
115 {
116     /**
117      * @tc.steps: step1. construct a SelectOverlayProxy
118      * @tc.expected: step1. selectOverlayId = NODE_ID
119      */
120     auto selectOverlayProxy = AceType::MakeRefPtr<SelectOverlayProxy>(NODE_ID);
121     auto overlayId = selectOverlayProxy->GetSelectOverlayId();
122     EXPECT_EQ(overlayId, NODE_ID);
123 
124     /**
125      * @tc.steps: step2. call IsClosed() without calling Close()
126      * @tc.expected: step2. hasn't closed, return true
127      */
128     auto isClosed = selectOverlayProxy->IsClosed();
129     EXPECT_TRUE(isClosed);
130 
131     /**
132      * @tc.steps: step3. call Close()
133      */
134     selectOverlayProxy->Close();
135 
136     /**
137      * @tc.steps: step4. call IsClosed() after calling Close()
138      * @tc.expected: step4. has closed, return false
139      */
140     auto selectOverlayProxy2 = AceType::MakeRefPtr<SelectOverlayProxy>(NODE_ID_2);
141     auto isClosed2 = selectOverlayProxy2->IsClosed();
142     EXPECT_TRUE(isClosed2);
143 }
144 
145 /**
146  * @tc.name: SelectOverlayProxyIsMenuShow001
147  * @tc.desc: test the IsMenuShow function
148  * @tc.type: FUNC
149  * @tc.author:
150  */
151 HWTEST_F(SelectOverlayProxyTestNg, SelectOverlayProxyIsMenuShow001, TestSize.Level1)
152 {
153     /**
154      * @tc.steps: step1. construct a SelectOverlayProxy
155      * @tc.expected: step1. IsMenuShow method returns true.
156      */
157     auto selectOverlayProxy = AceType::MakeRefPtr<SelectOverlayProxy>(NODE_ID);
158     EXPECT_FALSE(selectOverlayProxy->IsMenuShow());
159 }
160 
161 /**
162  * @tc.name: SelectOverlayProxyIsHandleShow001
163  * @tc.desc: test the IsHandleShow function
164  * @tc.type: FUNC
165  * @tc.author:
166  */
167 HWTEST_F(SelectOverlayProxyTestNg, SelectOverlayProxyIsHandleShow001, TestSize.Level1)
168 {
169     /**
170      * @tc.steps: step1. construct a SelectOverlayProxy
171      * @tc.expected: step1. IsHandleShow method returns true.
172      */
173     auto selectOverlayProxy = AceType::MakeRefPtr<SelectOverlayProxy>(NODE_ID);
174     EXPECT_FALSE(selectOverlayProxy->IsHandleShow());
175 }
176 } // namespace OHOS::Ace::NG
177