1 /*
2  * Copyright (c) 2024 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 #include <hisysevent.h>
18 #include <parameters.h>
19 
20 #include "fold_screen_controller/fold_screen_sensor_manager.h"
21 #include "fold_screen_state_internel.h"
22 #include "window_manager_hilog.h"
23 #include "screen_session_manager.h"
24 #include "scene_board_judgement.h"
25 
26 using namespace testing;
27 using namespace testing::ext;
28 
29 namespace OHOS {
30 namespace Rosen {
31 namespace {
32     constexpr uint32_t SLEEP_TIME_US = 100000;
33     constexpr float ANGLE_MIN_VAL = 0.0F;
34 }
35 
36 class FoldScreenSensorManagerTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp() override;
41     void TearDown() override;
42 };
43 
SetUpTestCase()44 void FoldScreenSensorManagerTest::SetUpTestCase()
45 {
46 }
47 
TearDownTestCase()48 void FoldScreenSensorManagerTest::TearDownTestCase()
49 {
50 }
51 
SetUp()52 void FoldScreenSensorManagerTest::SetUp()
53 {
54 }
55 
TearDown()56 void FoldScreenSensorManagerTest::TearDown()
57 {
58     usleep(SLEEP_TIME_US);
59 }
60 
61 namespace {
62     /**
63      * @tc.name: RegisterPostureCallback
64      * @tc.desc: test function : RegisterPostureCallback
65      * @tc.type: FUNC
66      */
67     HWTEST_F(FoldScreenSensorManagerTest, RegisterPostureCallback, Function | SmallTest | Level3)
68     {
69         FoldScreenSensorManager mgr;
70         mgr.RegisterPostureCallback();
71         ASSERT_NE(mgr.postureUser.callback, nullptr);
72     }
73 
74     /**
75      * @tc.name: UnRegisterPostureCallback
76      * @tc.desc: test function : UnRegisterPostureCallback
77      * @tc.type: FUNC
78      */
79     HWTEST_F(FoldScreenSensorManagerTest, UnRegisterPostureCallback, Function | SmallTest | Level3)
80     {
81         FoldScreenSensorManager mgr;
82         mgr.UnRegisterPostureCallback();
83         ASSERT_EQ(mgr.postureUser.callback, nullptr);
84     }
85 
86     /**
87      * @tc.name: RegisterHallCallback
88      * @tc.desc: test function : RegisterHallCallback
89      * @tc.type: FUNC
90      */
91     HWTEST_F(FoldScreenSensorManagerTest, RegisterHallCallback, Function | SmallTest | Level3)
92     {
93         FoldScreenSensorManager mgr;
94         mgr.RegisterHallCallback();
95         ASSERT_NE(mgr.hallUser.callback, nullptr);
96     }
97 
98     /**
99      * @tc.name: UnRegisterHallCallback
100      * @tc.desc: test function : UnRegisterHallCallback
101      * @tc.type: FUNC
102      */
103     HWTEST_F(FoldScreenSensorManagerTest, UnRegisterHallCallback, Function | SmallTest | Level3)
104     {
105         FoldScreenSensorManager mgr;
106         mgr.UnRegisterHallCallback();
107         ASSERT_EQ(mgr.hallUser.callback, nullptr);
108     }
109 
110     /**
111      * @tc.name: HandlePostureData
112      * @tc.desc: test function : HandlePostureData
113      * @tc.type: FUNC
114      */
115     HWTEST_F(FoldScreenSensorManagerTest, HandlePostureData, Function | SmallTest | Level3)
116     {
117         FoldScreenSensorManager mgr;
118         mgr.HandlePostureData(nullptr);
119         EXPECT_EQ(mgr.globalAngle, -1.0F);
120 
121         SensorEvent event;
122         PostureData postureData;
123         postureData.angle = 45.0F;
124         mgr.HandlePostureData(&event);
125         EXPECT_EQ(mgr.globalAngle, -1.0F);
126 
127         event.data = reinterpret_cast<uint8_t*>(&postureData);
128         event.dataLen = 0;
129         mgr.HandlePostureData(&event);
130         EXPECT_EQ(mgr.globalAngle, -1.0F);
131 
132         event.dataLen = sizeof(PostureData);
133         mgr.HandlePostureData(&event);
134         EXPECT_EQ(mgr.globalAngle, 45.0F);
135     }
136 
137     /**
138      * @tc.name: notifyFoldAngleChanged
139      * @tc.desc: test function : notifyFoldAngleChanged
140      * @tc.type: FUNC
141      */
142     HWTEST_F(FoldScreenSensorManagerTest, notifyFoldAngleChanged, Function | SmallTest | Level3)
143     {
144         FoldScreenSensorManager mgr;
145         float foldAngle = 0.0F;
146         mgr.notifyFoldAngleChanged(foldAngle);
147         EXPECT_EQ(foldAngle, 0.0F);
148 
149         foldAngle = 30.0F;
150         mgr.notifyFoldAngleChanged(foldAngle);
151         EXPECT_EQ(foldAngle, 30.0F);
152         usleep(SLEEP_TIME_US);
153     }
154 
155     /**
156      * @tc.name: HandleHallData
157      * @tc.desc: test function : HandleHallData
158      * @tc.type: FUNC
159      */
160     HWTEST_F(FoldScreenSensorManagerTest, HandleHallData, Function | SmallTest | Level3)
161     {
162         FoldScreenSensorManager mgr;
163         mgr.HandleHallData(nullptr);
164         EXPECT_EQ(mgr.globalHall, USHRT_MAX);
165 
166         SensorEvent event;
167         mgr.HandleHallData(&event);
168         EXPECT_EQ(mgr.globalHall, USHRT_MAX);
169 
170         FoldScreenSensorManager::ExtHallData hallData;
171         event.data = reinterpret_cast<uint8_t*>(&hallData);
172         mgr.HandleHallData(&event);
173         EXPECT_EQ(mgr.globalHall, USHRT_MAX);
174 
175         event.dataLen = 0;
176         mgr.HandleHallData(&event);
177         EXPECT_EQ(mgr.globalHall, USHRT_MAX);
178 
179         event.dataLen = sizeof(FoldScreenSensorManager::ExtHallData);
180         mgr.HandleHallData(&event);
181         EXPECT_EQ(mgr.globalHall, USHRT_MAX);
182 
183         hallData.hall = 10;
184         mgr.HandleHallData(&event);
185         EXPECT_EQ(mgr.globalHall, 65535);
186     }
187 
188     /**
189      * @tc.name: TriggerDisplaySwitch
190      * @tc.desc: test function : TriggerDisplaySwitch
191      * @tc.type: FUNC
192      */
193     HWTEST_F(FoldScreenSensorManagerTest, TriggerDisplaySwitch, Function | SmallTest | Level3)
194     {
195         FoldScreenSensorManager mgr;
196         mgr.SetSensorFoldStateManager(new SensorFoldStateManager());
197         mgr.registerPosture_ = false;
198         mgr.TriggerDisplaySwitch();
199         EXPECT_EQ(mgr.globalAngle, ANGLE_MIN_VAL);
200 
201         mgr.registerPosture_ = true;
202         mgr.TriggerDisplaySwitch();
203         EXPECT_EQ(mgr.globalAngle, 25);
204         usleep(SLEEP_TIME_US);
205     }
206 }
207 } // namespace Rosen
208 } // namespace OHOS