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