Formatting automated-tests
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor-internal / utc-Dali-TiltSensor.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <iostream>
19
20 #include <adaptor-test-application.h>
21 #include <dali-test-suite-utils.h>
22 #include <dali/dali.h>
23 #include <dali/internal/sensor/common/tilt-sensor-factory.h>
24 #include <dali/internal/sensor/common/tilt-sensor-impl.h>
25 #include <dali/internal/system/linux/dali-ecore.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28
29 using namespace Dali;
30
31 namespace
32 {
33 static const float ROTATION_EPSILON = 0.0001f;
34
35 /**
36  * Helper to test whether timeout or tilt signal is received first
37  */
38 struct SignalHelper : public ConnectionTracker
39 {
40   SignalHelper()
41   : mTiltSignalReceived(false),
42     mTimeoutOccurred(false)
43   {
44   }
45
46   void OnTilted(const TiltSensor& sensor)
47   {
48     tet_printf("tilted signal received\n");
49
50     mTiltSignalReceived = true;
51
52     // quit the main loop to continue test
53     //ecore_main_loop_quit();
54   }
55
56   bool OnTimeout()
57   {
58     tet_printf("timeout occurred\n");
59
60     mTimeoutOccurred = true;
61
62     // quit the main loop to continue test
63     //ecore_main_loop_quit();
64
65     return false;
66   }
67
68   bool mTiltSignalReceived; // True if tilted signal was received
69   bool mTimeoutOccurred;    // True if timeout occured
70 };
71
72 TiltSensor GetTiltSensor()
73 {
74   return Dali::TiltSensor(Dali::Internal::Adaptor::TiltSensorFactory::Create());
75 }
76
77 bool          ecore_timer_running = false;
78 Ecore_Task_Cb timer_callback_func = NULL;
79 const void*   timer_callback_data = NULL;
80 intptr_t      timerId             = 8; // intptr_t has the same size as a pointer and is platform independent so this can be returned as a pointer in ecore_timer_add below without compilation warnings
81 } // namespace
82 extern "C"
83 {
84   Ecore_Timer* ecore_timer_add(double        in,
85                                Ecore_Task_Cb func,
86                                const void*   data)
87   {
88     ecore_timer_running = true;
89     timer_callback_func = func;
90     timer_callback_data = data;
91     timerId += 8;
92     return (Ecore_Timer*)timerId;
93   }
94
95   void* ecore_timer_del(Ecore_Timer* timer)
96   {
97     ecore_timer_running = false;
98     timer_callback_func = NULL;
99     return NULL;
100   }
101 }
102
103 void tilt_sensor_startup(void)
104 {
105 }
106
107 void tilt_sensor_cleanup(void)
108 {
109 }
110
111 int UtcDaliTiltSensorStart(void)
112 {
113   AdaptorTestApplication application;
114
115   tet_infoline("UtcDaliTiltSensorStart");
116
117   TiltSensor sensor = GetTiltSensor();
118   DALI_TEST_CHECK(sensor);
119
120   sensor.Start();
121   DALI_TEST_CHECK(sensor.IsStarted());
122
123   END_TEST;
124 }
125
126 int UtcDaliTiltSensorStop(void)
127 {
128   AdaptorTestApplication application;
129
130   tet_infoline("UtcDaliTiltSensorStop");
131
132   TiltSensor sensor = GetTiltSensor();
133   DALI_TEST_CHECK(sensor);
134
135   sensor.Start();
136   DALI_TEST_CHECK(sensor.IsStarted());
137
138   sensor.Stop();
139   DALI_TEST_CHECK(!sensor.IsStarted());
140   END_TEST;
141 }
142
143 int UtcDaliTiltSensorIsStarted(void)
144 {
145   AdaptorTestApplication application;
146
147   tet_infoline("UtcDaliTiltSensorIsStarted");
148
149   TiltSensor sensor = GetTiltSensor();
150   DALI_TEST_CHECK(sensor);
151
152   // Should be disabled by default
153   DALI_TEST_CHECK(!sensor.IsStarted());
154   END_TEST;
155 }
156
157 int UtcDaliTiltSensorGetRoll(void)
158 {
159   AdaptorTestApplication application;
160
161   tet_infoline("UtcDaliTiltSensorGetRoll");
162
163   TiltSensor sensor = GetTiltSensor();
164   DALI_TEST_CHECK(sensor);
165
166   float roll = sensor.GetRoll();
167   DALI_TEST_CHECK(roll <= 1.0f && roll >= -1.0f); // range check
168   END_TEST;
169 }
170
171 int UtcDaliTiltSensorGetPitch(void)
172 {
173   AdaptorTestApplication application;
174
175   tet_infoline("UtcDaliTiltSensorGetPitch");
176
177   TiltSensor sensor = GetTiltSensor();
178   DALI_TEST_CHECK(sensor);
179
180   float pitch = sensor.GetPitch();
181   DALI_TEST_CHECK(pitch <= 1.0f && pitch >= -1.0f); // range check
182   END_TEST;
183 }
184
185 int UtcDaliTiltSensorGetRotation(void)
186 {
187   AdaptorTestApplication application;
188
189   tet_infoline("UtcDaliTiltSensorGetRotation");
190
191   TiltSensor sensor = GetTiltSensor();
192   DALI_TEST_CHECK(sensor);
193
194   Quaternion rotation = sensor.GetRotation();
195
196   Radian roll(sensor.GetRoll());
197   Radian pitch(sensor.GetPitch());
198
199   Quaternion expectedRotation = Quaternion(roll * Math::PI * -0.5f, Vector3::YAXIS) *
200                                 Quaternion(pitch * Math::PI * -0.5f, Vector3::XAXIS);
201
202   DALI_TEST_EQUALS(rotation, expectedRotation, ROTATION_EPSILON, TEST_LOCATION);
203   END_TEST;
204 }
205
206 int UtcDaliTiltSensorSignalTilted(void)
207 {
208   AdaptorTestApplication application;
209
210   tet_infoline("UtcDaliTiltSensorSignalTilted");
211
212   TiltSensor sensor = GetTiltSensor();
213   DALI_TEST_CHECK(sensor);
214   sensor.Start();
215
216   Radian angle(Degree(-45));
217   //Setting a negative threshold for testing purpose
218   sensor.SetRotationThreshold(angle);
219
220   END_TEST;
221 }
222
223 int UtcDaliTiltSensorSetUpdateFrequency(void)
224 {
225   AdaptorTestApplication application;
226
227   tet_infoline("UtcDaliTiltSensorSetUpdateFrequency");
228
229   TiltSensor sensor = GetTiltSensor();
230   DALI_TEST_CHECK(sensor);
231   sensor.SetUpdateFrequency(1.0f /*hertz*/);
232   DALI_TEST_EQUALS(sensor.GetUpdateFrequency(), 1.0f, TEST_LOCATION);
233
234   sensor.SetUpdateFrequency(60.0f /*hertz*/);
235   DALI_TEST_EQUALS(sensor.GetUpdateFrequency(), 60.0f, TEST_LOCATION);
236
237   END_TEST;
238 }
239
240 int UtcDaliTiltSensorSetRotationThreshold01(void)
241 {
242   AdaptorTestApplication application;
243
244   tet_infoline("UtcDaliTiltSensorSetRotationThreshold01");
245
246   TiltSensor sensor = GetTiltSensor();
247   DALI_TEST_CHECK(sensor);
248   sensor.Start();
249
250   Radian angle(Degree(-45));
251   sensor.SetRotationThreshold(angle);
252   DALI_TEST_EQUALS(sensor.GetRotationThreshold(), angle, TEST_LOCATION);
253
254   angle = Degree(90);
255   sensor.SetRotationThreshold(angle);
256   DALI_TEST_EQUALS(sensor.GetRotationThreshold(), angle, TEST_LOCATION);
257   END_TEST;
258 }