• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 "test/unittest/core/event/mouse_style_manager_test_ng.h"
17 #include <optional>
18 
19 #include "test/mock/core/pipeline/mock_pipeline_context.h"
20 
21 using namespace testing;
22 using namespace testing::ext;
23 
24 namespace OHOS::Ace::NG {
SetUpTestSuite()25 void MouseStyleManagerTestNG::SetUpTestSuite()
26 {
27     GTEST_LOG_(INFO) << "MouseStyleManagerTestNG SetUpTestCase";
28 }
29 
TearDownTestSuite()30 void MouseStyleManagerTestNG::TearDownTestSuite()
31 {
32     GTEST_LOG_(INFO) << "MouseStyleManagerTestNG TearDownTestCase";
33 }
34 
SetUp()35 void MouseStyleManagerTestNG::SetUp()
36 {
37     MockPipelineContext::SetUp();
38 }
39 
TearDown()40 void MouseStyleManagerTestNG::TearDown()
41 {
42     MockPipelineContext::TearDown();
43 }
44 
45 /**
46  * @tc.name: MouseStyleManager001
47  * @tc.desc: Test case userSetCursor_ is false.
48  * @tc.type: FUNC
49  */
50 HWTEST_F(MouseStyleManagerTestNG, MouseStyleManager001, TestSize.Level1)
51 {
52     /**
53      * @tc.steps: step1. Create mouseStyleManager.
54      * @tc.expected: mouseStyleManager is not null.
55      */
56     auto mouseStyleManager = AceType::MakeRefPtr<MouseStyleManager>();
57     ASSERT_NE(mouseStyleManager, nullptr);
58 
59     /*
60      * @tc.steps: step2. Create testCases.
61      * @tc.expected: result is correct.
62      */
63     const std::vector<MockMouseFormatCase> mockMouseFormatCases = {
64         {false, std::nullopt, 0, MouseFormat::EAST, false,
65             MouseStyleChangeReason::INNER_SET_MOUSESTYLE, false, MouseFormat::DEFAULT},
66         {false, std::nullopt, 0, MouseFormat::EAST, false,
67             MouseStyleChangeReason::USER_SET_MOUSESTYLE, false, MouseFormat::DEFAULT},
68         {false, 0, 0, MouseFormat::EAST, false,
69             MouseStyleChangeReason::INNER_SET_MOUSESTYLE, true, MouseFormat::EAST},
70         {false, 0, 0, MouseFormat::EAST, false,
71             MouseStyleChangeReason::USER_SET_MOUSESTYLE, true, MouseFormat::EAST},
72         {false, 0, 1, MouseFormat::EAST, false,
73             MouseStyleChangeReason::INNER_SET_MOUSESTYLE, false, MouseFormat::DEFAULT},
74         {false, 0, 1, MouseFormat::EAST, false,
75             MouseStyleChangeReason::USER_SET_MOUSESTYLE, false, MouseFormat::DEFAULT}
76     };
77 
78     /*
79      * @tc.steps: step3. Run testCases.
80      * @tc.expected: result is correct.
81      */
82     for (auto i = 0; i < mockMouseFormatCases.size(); i++) {
83         mouseStyleManager->mouseFormat_ = MouseFormat::DEFAULT;
84         mouseStyleManager->userSetCursor_ = mockMouseFormatCases[i].userSetCursor;
85         mouseStyleManager->mouseStyleNodeId_ = mockMouseFormatCases[i].mouseStyleNodeId;
86         auto result = mouseStyleManager->SetMouseFormat(1, mockMouseFormatCases[i].nodeId,
87             mockMouseFormatCases[i].mouseFormat,
88             mockMouseFormatCases[i].isByPass,
89             mockMouseFormatCases[i].reason);
90         EXPECT_EQ(result, mockMouseFormatCases[i].expectSetResult);
91         mouseStyleManager->VsyncMouseFormat();
92         EXPECT_EQ(mouseStyleManager->mouseFormat_,
93             mockMouseFormatCases[i].expectMouseFormat);
94     }
95 }
96 
97 /**
98  * @tc.name: MouseStyleManager002
99  * @tc.desc: Test case userSetCursor_ is true.
100  * @tc.type: FUNC
101  */
102 HWTEST_F(MouseStyleManagerTestNG, MouseStyleManager002, TestSize.Level1)
103 {
104     /**
105      * @tc.steps: step1. Create mouseStyleManager.
106      * @tc.expected: mouseStyleManager is not null.
107      */
108     auto mouseStyleManager = AceType::MakeRefPtr<MouseStyleManager>();
109     ASSERT_NE(mouseStyleManager, nullptr);
110 
111     /*
112      * @tc.steps: step2. Create testCases.
113      * @tc.expected: result is correct.
114      */
115     const std::vector<MockMouseFormatCase> mockMouseFormatCases = {
116         {true, std::nullopt, 0, MouseFormat::EAST, false,
117             MouseStyleChangeReason::INNER_SET_MOUSESTYLE, false, MouseFormat::DEFAULT},
118         {true, std::nullopt, 0, MouseFormat::EAST, false,
119             MouseStyleChangeReason::USER_SET_MOUSESTYLE, true, MouseFormat::EAST},
120         {true, 0, 0, MouseFormat::EAST, false,
121             MouseStyleChangeReason::INNER_SET_MOUSESTYLE, false, MouseFormat::DEFAULT},
122         {true, 0, 0, MouseFormat::EAST, false,
123             MouseStyleChangeReason::USER_SET_MOUSESTYLE, true, MouseFormat::EAST},
124         {true, 0, 1, MouseFormat::EAST, false,
125             MouseStyleChangeReason::INNER_SET_MOUSESTYLE, false, MouseFormat::DEFAULT},
126         {true, 0, 1, MouseFormat::EAST, false,
127             MouseStyleChangeReason::USER_SET_MOUSESTYLE, true, MouseFormat::EAST}
128     };
129 
130     /*
131      * @tc.steps: step3. Run testCases.
132      * @tc.expected: result is correct.
133      */
134     for (auto i = 0; i < mockMouseFormatCases.size(); i++) {
135         mouseStyleManager->mouseFormat_ = MouseFormat::DEFAULT;
136         mouseStyleManager->userSetCursor_ = mockMouseFormatCases[i].userSetCursor;
137         mouseStyleManager->mouseStyleNodeId_ = mockMouseFormatCases[i].mouseStyleNodeId;
138         auto result = mouseStyleManager->SetMouseFormat(1, mockMouseFormatCases[i].nodeId,
139             mockMouseFormatCases[i].mouseFormat,
140             mockMouseFormatCases[i].isByPass,
141             mockMouseFormatCases[i].reason);
142         EXPECT_EQ(result, mockMouseFormatCases[i].expectSetResult);
143         mouseStyleManager->VsyncMouseFormat();
144         EXPECT_EQ(mouseStyleManager->mouseFormat_,
145             mockMouseFormatCases[i].expectMouseFormat);
146     }
147 }
148 
149 /**
150  * @tc.name: MouseStyleManager003
151  * @tc.desc: Test case isByPass is true.
152  * @tc.type: FUNC
153  */
154 HWTEST_F(MouseStyleManagerTestNG, MouseStyleManager003, TestSize.Level1)
155 {
156     /**
157      * @tc.steps: step1. Create mouseStyleManager.
158      * @tc.expected: mouseStyleManager is not null.
159      */
160     auto mouseStyleManager = AceType::MakeRefPtr<MouseStyleManager>();
161     ASSERT_NE(mouseStyleManager, nullptr);
162 
163     /*
164      * @tc.steps: step2. Create testCases.
165      * @tc.expected: result is correct.
166      */
167     const std::vector<MockMouseFormatCase> mockMouseFormatCases = {
168         {false, std::nullopt, 0, MouseFormat::EAST, true,
169             MouseStyleChangeReason::INNER_SET_MOUSESTYLE, false, MouseFormat::DEFAULT},
170         {false, std::nullopt, 0, MouseFormat::EAST, true,
171             MouseStyleChangeReason::USER_SET_MOUSESTYLE, false, MouseFormat::DEFAULT}
172     };
173 
174     /*
175      * @tc.steps: step3. Run testCases.
176      * @tc.expected: result is correct.
177      */
178     for (auto i = 0; i < mockMouseFormatCases.size(); i++) {
179         mouseStyleManager->mouseFormat_ = MouseFormat::DEFAULT;
180         mouseStyleManager->userSetCursor_ = mockMouseFormatCases[i].userSetCursor;
181         mouseStyleManager->mouseStyleNodeId_ = mockMouseFormatCases[i].mouseStyleNodeId;
182         auto result = mouseStyleManager->SetMouseFormat(1, mockMouseFormatCases[i].nodeId,
183             mockMouseFormatCases[i].mouseFormat,
184             mockMouseFormatCases[i].isByPass,
185             mockMouseFormatCases[i].reason);
186         EXPECT_EQ(result, mockMouseFormatCases[i].expectSetResult);
187         mouseStyleManager->VsyncMouseFormat();
188         EXPECT_EQ(mouseStyleManager->mouseFormat_,
189             mockMouseFormatCases[i].expectMouseFormat);
190     }
191 }
192 
193 /**
194  * @tc.name: MouseStyleManager004
195  * @tc.desc: Test case one vsync have different input.
196  * @tc.type: FUNC
197  */
198 HWTEST_F(MouseStyleManagerTestNG, MouseStyleManager004, TestSize.Level1)
199 {
200     /**
201      * @tc.steps: step1. Create mouseStyleManager.
202      * @tc.expected: mouseStyleManager is not null.
203      */
204     auto mouseStyleManager = AceType::MakeRefPtr<MouseStyleManager>();
205     ASSERT_NE(mouseStyleManager, nullptr);
206     bool result = false;
207 
208     /*
209      * @tc.steps: step2. Create testCases.
210      * @tc.expected: result is correct.
211      */
212     const std::vector<MockMouseFormatCase> mockMouseFormatCases = {
213         {false, std::nullopt, 0, MouseFormat::EAST, false,
214             MouseStyleChangeReason::INNER_SET_MOUSESTYLE, false, MouseFormat::DEFAULT},
215         {false, std::nullopt, 0, MouseFormat::EAST, false,
216             MouseStyleChangeReason::USER_SET_MOUSESTYLE, false, MouseFormat::DEFAULT},
217         {false, 0, 0, MouseFormat::EAST, false,
218             MouseStyleChangeReason::INNER_SET_MOUSESTYLE, false, MouseFormat::DEFAULT},
219         {false, 0, 0, MouseFormat::EAST, false,
220             MouseStyleChangeReason::USER_SET_MOUSESTYLE, true, MouseFormat::EAST},
221         {false, 0, 1, MouseFormat::EAST, false,
222             MouseStyleChangeReason::INNER_SET_MOUSESTYLE, false, MouseFormat::EAST},
223         {false, 0, 1, MouseFormat::EAST, false,
224             MouseStyleChangeReason::USER_SET_MOUSESTYLE, false, MouseFormat::DEFAULT}
225     };
226 
227     /*
228      * @tc.steps: step3. Run testCases.
229      * @tc.expected: result is correct.
230      */
231     for (auto i = 0; i < mockMouseFormatCases.size() - 1; i++) {
232         mouseStyleManager->mouseFormat_ = MouseFormat::DEFAULT;
233         mouseStyleManager->userSetCursor_ = mockMouseFormatCases[i].userSetCursor;
234         mouseStyleManager->mouseStyleNodeId_ = mockMouseFormatCases[i].mouseStyleNodeId;
235         result = mouseStyleManager->SetMouseFormat(1, mockMouseFormatCases[i].nodeId,
236             mockMouseFormatCases[i].mouseFormat,
237             mockMouseFormatCases[i].isByPass,
238             mockMouseFormatCases[i].reason);
239         result = mouseStyleManager->SetMouseFormat(1, mockMouseFormatCases[i + 1].nodeId,
240             mockMouseFormatCases[i + 1].mouseFormat,
241             mockMouseFormatCases[i + 1].isByPass,
242             mockMouseFormatCases[i + 1].reason);
243         EXPECT_EQ(result, mockMouseFormatCases[i + 1].expectSetResult);
244         mouseStyleManager->VsyncMouseFormat();
245         EXPECT_EQ(mouseStyleManager->mouseFormat_,
246             mockMouseFormatCases[i + 1].expectMouseFormat);
247     }
248 }
249 
250 /**
251  * @tc.name: MouseStyleManager005
252  * @tc.desc: Test case userSetCursor_ is different.
253  * @tc.type: FUNC
254  */
255 HWTEST_F(MouseStyleManagerTestNG, MouseStyleManager005, TestSize.Level1)
256 {
257     /**
258      * @tc.steps: step1. Create mouseStyleManager.
259      * @tc.expected: mouseStyleManager is not null.
260      */
261     auto mouseStyleManager = AceType::MakeRefPtr<MouseStyleManager>();
262     ASSERT_NE(mouseStyleManager, nullptr);
263     bool result = false;
264 
265     /*
266      * @tc.steps: step2. Create testCases.
267      * @tc.expected: result is correct.
268      */
269     const std::vector<MockMouseFormatCase> mockMouseFormatCases = {
270         {false, std::nullopt, 0, MouseFormat::EAST, false,
271             MouseStyleChangeReason::INNER_SET_MOUSESTYLE, false, MouseFormat::DEFAULT},
272         {true, std::nullopt, 0, MouseFormat::EAST, false,
273             MouseStyleChangeReason::USER_SET_MOUSESTYLE, true, MouseFormat::EAST},
274         {true, 0, 0, MouseFormat::EAST, false,
275             MouseStyleChangeReason::INNER_SET_MOUSESTYLE, false, MouseFormat::DEFAULT},
276         {false, 0, 0, MouseFormat::EAST, false,
277             MouseStyleChangeReason::USER_SET_MOUSESTYLE, true, MouseFormat::EAST},
278         {false, 0, 1, MouseFormat::EAST, false,
279             MouseStyleChangeReason::INNER_SET_MOUSESTYLE, false, MouseFormat::DEFAULT},
280         {true, 0, 1, MouseFormat::EAST, false,
281             MouseStyleChangeReason::USER_SET_MOUSESTYLE, true, MouseFormat::EAST}
282     };
283 
284     /*
285      * @tc.steps: step3. Run testCases.
286      * @tc.expected: result is correct.
287      */
288     for (auto i = 0; i < mockMouseFormatCases.size(); i++) {
289         mouseStyleManager->mouseFormat_ = MouseFormat::DEFAULT;
290         mouseStyleManager->userSetCursor_ = !mockMouseFormatCases[i].userSetCursor;
291         mouseStyleManager->mouseStyleNodeId_ = mockMouseFormatCases[i].mouseStyleNodeId;
292         mouseStyleManager->SetUserSetCursor(mockMouseFormatCases[i].userSetCursor);
293         result = mouseStyleManager->SetMouseFormat(1, mockMouseFormatCases[i].nodeId,
294             mockMouseFormatCases[i].mouseFormat,
295             mockMouseFormatCases[i].isByPass,
296             mockMouseFormatCases[i].reason);
297         EXPECT_EQ(result, mockMouseFormatCases[i].expectSetResult);
298         mouseStyleManager->VsyncMouseFormat();
299         EXPECT_EQ(mouseStyleManager->mouseFormat_,
300             mockMouseFormatCases[i].expectMouseFormat);
301     }
302 }
303 } // namespace OHOS::Ace::NG
304