Merge "Reduced level of unnecessary debug" into tizen
[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 <stdlib.h>
21 #include <stdint.h>
22 #include <Ecore.h>
23 #include <dali/dali.h>
24 #include <dali-test-suite-utils.h>
25 #include <tilt-sensor-impl.h>
26
27 using namespace Dali;
28
29 namespace
30 {
31 static const float ROTATION_EPSILON = 0.0001f;
32
33 /**
34  * Helper to test whether timeout or tilt signal is received first
35  */
36 struct SignalHelper : public ConnectionTracker
37 {
38   SignalHelper()
39   : mTiltSignalReceived( false ),
40     mTimeoutOccurred( false )
41   {
42   }
43
44   void OnTilted(const TiltSensor& sensor)
45   {
46     tet_printf("tilted signal received\n");
47
48     mTiltSignalReceived = true;
49
50     // quit the main loop to continue test
51     //ecore_main_loop_quit();
52   }
53
54   bool OnTimeout()
55   {
56     tet_printf("timeout occurred\n");
57
58     mTimeoutOccurred = true;
59
60     // quit the main loop to continue test
61     //ecore_main_loop_quit();
62
63     return false;
64   }
65
66   bool mTiltSignalReceived; // True if tilted signal was received
67   bool mTimeoutOccurred;    // True if timeout occured
68 };
69
70 TiltSensor GetTiltSensor()
71 {
72   return Internal::Adaptor::TiltSensor::New();
73 }
74
75 bool ecore_timer_running = false;
76 Ecore_Task_Cb timer_callback_func=NULL;
77 const void* timer_callback_data=NULL;
78 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
79 }// anon namespace
80 extern "C"
81 {
82 Ecore_Timer* ecore_timer_add(double in,
83                              Ecore_Task_Cb func,
84                              const void   *data)
85 {
86   ecore_timer_running = true;
87   timer_callback_func = func;
88   timer_callback_data = data;
89   timerId+=8;
90   return (Ecore_Timer*)timerId;
91 }
92
93 void* ecore_timer_del(Ecore_Timer *timer)
94 {
95   ecore_timer_running = false;
96   timer_callback_func = NULL;
97   return NULL;
98 }
99
100 }
101
102
103
104 void tilt_sensor_startup(void)
105 {
106 }
107
108 void tilt_sensor_cleanup(void)
109 {
110 }
111
112
113 int UtcDaliTiltSensorEnable(void)
114 {
115   TestApplication application;
116
117   tet_infoline("UtcDaliTiltSensorEnable");
118
119   TiltSensor sensor = GetTiltSensor();
120   DALI_TEST_CHECK( sensor );
121
122   sensor.Enable();
123   DALI_TEST_CHECK( sensor.IsEnabled() );
124
125   END_TEST;
126 }
127
128 int UtcDaliTiltSensorDisable(void)
129 {
130   TestApplication application;
131
132   tet_infoline("UtcDaliTiltSensorDisable");
133
134   TiltSensor sensor = GetTiltSensor();
135   DALI_TEST_CHECK( sensor );
136
137   sensor.Enable();
138   DALI_TEST_CHECK( sensor.IsEnabled() );
139
140   sensor.Disable();
141   DALI_TEST_CHECK( !sensor.IsEnabled() );
142   END_TEST;
143 }
144
145 int UtcDaliTiltSensorIsEnabled(void)
146 {
147   TestApplication application;
148
149   tet_infoline("UtcDaliTiltSensorIsEnabled");
150
151   TiltSensor sensor = GetTiltSensor();
152   DALI_TEST_CHECK( sensor );
153
154   // Should be disabled by default
155   DALI_TEST_CHECK( !sensor.IsEnabled() );
156   END_TEST;
157 }
158
159 int UtcDaliTiltSensorGetRoll(void)
160 {
161   TestApplication application;
162
163   tet_infoline("UtcDaliTiltSensorGetRoll");
164
165   TiltSensor sensor = GetTiltSensor();
166   DALI_TEST_CHECK( sensor );
167
168   float roll = sensor.GetRoll();
169   DALI_TEST_CHECK( roll <= 1.0f && roll >= -1.0f ); // range check
170   END_TEST;
171 }
172
173 int UtcDaliTiltSensorGetPitch(void)
174 {
175   TestApplication application;
176
177   tet_infoline("UtcDaliTiltSensorGetPitch");
178
179   TiltSensor sensor = GetTiltSensor();
180   DALI_TEST_CHECK( sensor );
181
182   float pitch = sensor.GetPitch();
183   DALI_TEST_CHECK( pitch <= 1.0f && pitch >= -1.0f ); // range check
184   END_TEST;
185 }
186
187 int UtcDaliTiltSensorGetRotation(void)
188 {
189   TestApplication application;
190
191   tet_infoline("UtcDaliTiltSensorGetRotation");
192
193   TiltSensor sensor = GetTiltSensor();
194   DALI_TEST_CHECK( sensor );
195
196   Quaternion rotation = sensor.GetRotation();
197
198   float roll  = sensor.GetRoll();
199   float pitch = sensor.GetPitch();
200
201   Quaternion expectedRotation = Quaternion( roll  * Math::PI * -0.5f, Vector3::YAXIS ) *
202                                 Quaternion( pitch * Math::PI * -0.5f, Vector3::XAXIS );
203
204   DALI_TEST_EQUALS( rotation, expectedRotation, ROTATION_EPSILON, TEST_LOCATION );
205   END_TEST;
206 }
207
208
209 int UtcDaliTiltSensorSignalTilted(void)
210 {
211   TestApplication application;
212
213   tet_infoline("UtcDaliTiltSensorSignalTilted");
214
215   TiltSensor sensor = GetTiltSensor();
216   DALI_TEST_CHECK( sensor );
217   sensor.Enable();
218
219   Radian angle(Degree(-45));
220   //Setting a negative threshold for testing purpose
221   sensor.SetRotationThreshold( angle );
222
223   END_TEST;
224 }
225
226 int UtcDaliTiltSensorSetUpdateFrequency(void)
227 {
228   TestApplication application;
229
230   tet_infoline("UtcDaliTiltSensorSetUpdateFrequency");
231
232   TiltSensor sensor = GetTiltSensor();
233   DALI_TEST_CHECK( sensor );
234   sensor.SetUpdateFrequency( 1.0f/*hertz*/ );
235   DALI_TEST_EQUALS( sensor.GetUpdateFrequency(), 1.0f, TEST_LOCATION );
236
237   sensor.SetUpdateFrequency( 60.0f/*hertz*/ );
238   DALI_TEST_EQUALS( sensor.GetUpdateFrequency(), 60.0f, TEST_LOCATION );
239
240   END_TEST;
241 }
242
243
244 int UtcDaliTiltSensorSetRotationThreshold01(void)
245 {
246   TestApplication application;
247
248   tet_infoline("UtcDaliTiltSensorSetRotationThreshold01");
249
250   TiltSensor sensor = GetTiltSensor();
251   DALI_TEST_CHECK( sensor );
252   sensor.Enable();
253
254   Radian angle(Degree(-45));
255   sensor.SetRotationThreshold( angle );
256   DALI_TEST_EQUALS( sensor.GetRotationThreshold(), angle, TEST_LOCATION );
257
258   angle = Degree(90);
259   sensor.SetRotationThreshold( angle );
260   DALI_TEST_EQUALS( sensor.GetRotationThreshold(), angle, TEST_LOCATION );
261   END_TEST;
262 }