Revert "[Tizen] Move DevelHandle::GetCurrentProperty to public"
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Path.cpp
1 /*
2  * Copyright (c) 2017 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 <dali/public-api/dali-core.h>
22 #include <dali/devel-api/object/handle-devel.h>
23 #include <dali-test-suite-utils.h>
24
25 using namespace Dali;
26 using namespace Dali::Internal;
27
28 namespace
29 {
30 // Knots fed into Allegro, which generates control points
31 static void SetupPath( Dali::Path& path)
32 {
33   path.AddPoint(Vector3( 30.0,  80.0, 0.0));
34   path.AddPoint(Vector3( 70.0, 120.0, 0.0));
35   path.AddPoint(Vector3(100.0, 100.0, 0.0));
36
37   //Control points for first segment
38   path.AddControlPoint( Vector3( 39.0,  90.0, 0.0) );
39   path.AddControlPoint(Vector3( 56.0, 119.0, 0.0) );
40
41   //Control points for second segment
42   path.AddControlPoint(Vector3( 78.0, 120.0, 0.0) );
43   path.AddControlPoint(Vector3( 93.0, 104.0, 0.0) );
44 }
45
46 } // anonymous namespace
47
48 int utcDaliPathGetPoint(void)
49 {
50   TestApplication application;
51
52   Dali::Path path = Dali::Path::New();
53   path.AddPoint(Vector3( 50.0,  50.0, 0.0));
54   path.AddPoint(Vector3(120.0,  70.0, 0.0));
55   path.AddPoint(Vector3(190.0, 250.0, 0.0));
56   path.AddPoint(Vector3(260.0, 260.0, 0.0));
57   path.AddPoint(Vector3(330.0, 220.0, 0.0));
58   path.AddPoint(Vector3(400.0,  50.0, 0.0));
59
60   DALI_TEST_EQUALS(path.GetPoint(0), Vector3( 50.0,  50.0, 0.0), TEST_LOCATION);
61   DALI_TEST_EQUALS(path.GetPoint(1), Vector3(120.0,  70.0, 0.0), TEST_LOCATION);
62   DALI_TEST_EQUALS(path.GetPoint(2), Vector3(190.0, 250.0, 0.0), TEST_LOCATION);
63   DALI_TEST_EQUALS(path.GetPoint(3), Vector3(260.0, 260.0, 0.0), TEST_LOCATION);
64   DALI_TEST_EQUALS(path.GetPoint(4), Vector3(330.0, 220.0, 0.0), TEST_LOCATION);
65   DALI_TEST_EQUALS(path.GetPoint(5), Vector3(400.0,  50.0, 0.0), TEST_LOCATION);
66   END_TEST;
67 }
68
69 int utcDaliPathGetPoint02(void)
70 {
71   TestApplication application;
72
73   Dali::Path path = Dali::Path::New();
74   path.AddPoint(Vector3( 50.0,  50.0, 0.0f));
75
76   try
77   {
78     path.GetPoint(1);
79     tet_result(TET_FAIL);
80   }
81   catch (Dali::DaliException& e)
82   {
83     DALI_TEST_PRINT_ASSERT( e );
84     DALI_TEST_ASSERT(e, "index < mPoint.Size()", TEST_LOCATION);
85   }
86   END_TEST;
87 }
88
89 int utcDaliPathGetPoint03(void)
90 {
91   TestApplication application;
92
93   Dali::Path path = Dali::Path::New();
94
95   try
96   {
97     path.GetPoint(0);
98     tet_result(TET_FAIL);
99   }
100   catch (Dali::DaliException& e)
101   {
102     DALI_TEST_PRINT_ASSERT( e );
103     DALI_TEST_ASSERT(e, "index < mPoint.Size()", TEST_LOCATION);
104   }
105   END_TEST;
106 }
107
108 int utcDaliPathGetControlPoints(void)
109 {
110   TestApplication application;
111
112   Dali::Path path = Dali::Path::New();
113   path.AddControlPoint( Vector3(0.0f, 0.0f, 0.0) );
114   path.AddControlPoint( Vector3(108.0f, 57.0f, 0.0) );
115
116   DALI_TEST_EQUALS(path.GetControlPoint(0), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION);
117   DALI_TEST_EQUALS(path.GetControlPoint(1), Vector3(108.0f, 57.0f, 0.0f), TEST_LOCATION);
118   END_TEST;
119 }
120
121 int utcDaliPathGetControlPoints01(void)
122 {
123   TestApplication application;
124
125   Dali::Path path = Dali::Path::New();
126   path.AddControlPoint(Vector3(0.0f, 0.0f, 0.0) );
127   path.AddControlPoint(Vector3(108.0f, 57.0f, 0.0) );
128
129   try
130   {
131     path.GetControlPoint(5);
132     tet_result(TET_FAIL);
133   }
134   catch (Dali::DaliException& e)
135   {
136     DALI_TEST_PRINT_ASSERT( e );
137     DALI_TEST_ASSERT(e, "index < mControlPoint.Size()", TEST_LOCATION);
138   }
139   END_TEST;
140 }
141
142 int utcDaliPathGetControlPoints02(void)
143 {
144   TestApplication application;
145
146   Dali::Path path = Dali::Path::New();
147   try
148   {
149     path.GetControlPoint(0);
150     tet_result(TET_FAIL);
151   }
152   catch (Dali::DaliException& e)
153   {
154     DALI_TEST_PRINT_ASSERT( e );
155     DALI_TEST_ASSERT(e, "index < mControlPoint.Size()", TEST_LOCATION);
156   }
157   END_TEST;
158 }
159
160 int utcDaliPathGenerateControlPoints01(void)
161 {
162   TestApplication application;
163
164   Dali::Path path = Dali::Path::New();
165
166   path.AddPoint(Vector3( 50.0,  50.0, 0.0));
167   path.AddPoint(Vector3(120.0,  70.0, 0.0));
168   path.AddPoint(Vector3(190.0, 250.0, 0.0));
169   path.AddPoint(Vector3(260.0, 260.0, 0.0));
170   path.AddPoint(Vector3(330.0, 220.0, 0.0));
171   path.AddPoint(Vector3(400.0,  50.0, 0.0));
172
173   path.GenerateControlPoints(0.25);
174
175   DALI_TEST_EQUALS(path.GetControlPoint(0), Vector3( 68.0,  55.0, 0.0), 1.0, TEST_LOCATION);
176   DALI_TEST_EQUALS(path.GetControlPoint(1), Vector3(107.0,  58.0, 0.0), 1.0, TEST_LOCATION);
177
178   DALI_TEST_EQUALS(path.GetControlPoint(2), Vector3(156.0, 102.0, 0.0), 1.0, TEST_LOCATION);
179   DALI_TEST_EQUALS(path.GetControlPoint(3), Vector3(152.0, 220.0, 0.0), 1.0, TEST_LOCATION);
180
181   DALI_TEST_EQUALS(path.GetControlPoint(4), Vector3(204.0, 261.0, 0.0), 1.0, TEST_LOCATION);
182   DALI_TEST_EQUALS(path.GetControlPoint(5), Vector3(243.0, 263.0, 0.0), 1.0, TEST_LOCATION);
183
184   DALI_TEST_EQUALS(path.GetControlPoint(6), Vector3(280.0, 256.0, 0.0), 1.0, TEST_LOCATION);
185   DALI_TEST_EQUALS(path.GetControlPoint(7), Vector3(317.0, 235.0, 0.0), 1.0, TEST_LOCATION);
186
187   DALI_TEST_EQUALS(path.GetControlPoint(8), Vector3(360.0, 185.0, 0.0), 1.0, TEST_LOCATION);
188   DALI_TEST_EQUALS(path.GetControlPoint(9), Vector3(383.0,  93.0, 0.0), 1.0, TEST_LOCATION);
189
190   END_TEST;
191 }
192
193 int utcDaliPathGetPointCount(void)
194 {
195   TestApplication application;
196   Dali::Path path = Dali::Path::New();
197
198   DALI_TEST_EQUALS(path.GetPointCount(), 0u, TEST_LOCATION);
199
200   path.AddPoint(Vector3( 50.0,  50.0, 0.0));
201   path.AddPoint(Vector3(120.0,  70.0, 0.0));
202   path.AddPoint(Vector3(190.0, 250.0, 0.0));
203   path.AddPoint(Vector3(260.0, 260.0, 0.0));
204
205   DALI_TEST_EQUALS(path.GetPointCount(), 4u, TEST_LOCATION);
206
207   path.AddPoint(Vector3(330.0, 220.0, 0.0));
208   path.AddPoint(Vector3(400.0,  50.0, 0.0));
209
210   DALI_TEST_EQUALS(path.GetPointCount(), 6u, TEST_LOCATION);
211   END_TEST;
212 }
213
214 int utcDaliPathGenerateControlPoints02(void)
215 {
216   TestApplication application;
217
218   Dali::Path path = Dali::Path::New();
219   try
220   {
221     path.GenerateControlPoints(0.25);
222     tet_result(TET_FAIL);
223   }
224   catch (Dali::DaliException& e)
225   {
226     DALI_TEST_PRINT_ASSERT( e );
227     DALI_TEST_ASSERT(e, "numSegments > 0", TEST_LOCATION);
228   }
229   END_TEST;
230 }
231
232 int utcDaliPathGenerateControlPoints03(void)
233 {
234   TestApplication application;
235
236   Dali::Path path = Dali::Path::New();
237   path.AddPoint(Vector3(400.0,  50.0, 0.0f));
238   try
239   {
240     path.GenerateControlPoints(0.25);
241     tet_result(TET_FAIL);
242   }
243   catch (Dali::DaliException& e)
244   {
245     DALI_TEST_PRINT_ASSERT( e );
246     DALI_TEST_ASSERT(e, "numSegments > 0", TEST_LOCATION);
247   }
248   END_TEST;
249 }
250
251 int UtcDaliPathSample01(void)
252 {
253   TestApplication application;
254   Dali::Path path = Dali::Path::New();
255   SetupPath(path);
256
257   //t = 0
258   Vector3 position, tangent;
259   path.Sample(0.0f, position, tangent );
260   DALI_TEST_EQUALS(position.x, 30.0f, TEST_LOCATION);
261   DALI_TEST_EQUALS(position.y, 80.0f, TEST_LOCATION);
262   DALI_TEST_EQUALS(tangent.x,  0.6f, 0.1f, TEST_LOCATION);
263   DALI_TEST_EQUALS(tangent.y,  0.7f, 0.1f, TEST_LOCATION);
264
265   //t = 0.25
266   path.Sample(0.25f, position, tangent );
267   DALI_TEST_EQUALS(position.x,  48.0f, 2.0f, TEST_LOCATION);
268   DALI_TEST_EQUALS(position.y, 102.0f, 2.0f, TEST_LOCATION);
269   DALI_TEST_EQUALS(tangent.x,  0.6f, 0.1f, TEST_LOCATION);
270   DALI_TEST_EQUALS(tangent.y,  0.7f, 0.1f, TEST_LOCATION);
271
272   // t = 0.5
273   path.Sample(0.5f, position, tangent );
274   DALI_TEST_EQUALS(position.x,  70.0f, TEST_LOCATION);
275   DALI_TEST_EQUALS(position.y, 120.0f, TEST_LOCATION);
276   DALI_TEST_EQUALS(tangent.x,  1.0f, 0.1f, TEST_LOCATION);
277   DALI_TEST_EQUALS(tangent.y,  0.0f, 0.1f, TEST_LOCATION);
278
279
280   //t = 0.75
281   path.Sample(0.75f, position, tangent );
282   DALI_TEST_EQUALS(position.x,  85.0f, 2.0f, TEST_LOCATION);
283   DALI_TEST_EQUALS(position.y, 112.0f, 2.0f, TEST_LOCATION);
284   DALI_TEST_EQUALS(tangent.x,  0.7f, 0.1f, TEST_LOCATION);
285   DALI_TEST_EQUALS(tangent.y,  -0.6f, 0.1f, TEST_LOCATION);
286
287   // t = 1
288   path.Sample(1.0f, position, tangent );
289   DALI_TEST_EQUALS(position.x, 100.0f, TEST_LOCATION);
290   DALI_TEST_EQUALS(position.y, 100.0f, TEST_LOCATION);
291   DALI_TEST_EQUALS(tangent.x,  0.8f, 0.1f, TEST_LOCATION);
292   DALI_TEST_EQUALS(tangent.y,  -0.4f, 0.1f, TEST_LOCATION);
293
294   END_TEST;
295 }
296
297 int UtcDaliPathDownCast(void)
298 {
299   TestApplication application;
300
301   Dali::Path path = Dali::Path::New();
302   Handle handle = path;
303   SetupPath(path);
304
305   Dali::Path path2 = Dali::Path::DownCast(handle);
306   DALI_TEST_CHECK(path2);
307
308   //t = 0
309   Vector3 position, tangent;
310   path2.Sample(0.0f, position, tangent );
311   DALI_TEST_EQUALS(position.x, 30.0f, TEST_LOCATION);
312   DALI_TEST_EQUALS(position.y, 80.0f, TEST_LOCATION);
313   DALI_TEST_EQUALS(tangent.x,  0.6f, 0.1f, TEST_LOCATION);
314   DALI_TEST_EQUALS(tangent.y,  0.7f, 0.1f, TEST_LOCATION);
315
316   //t = 0.25
317   path2.Sample(0.25f, position, tangent );
318   DALI_TEST_EQUALS(position.x,  48.0f, 2.0f, TEST_LOCATION);
319   DALI_TEST_EQUALS(position.y, 102.0f, 2.0f, TEST_LOCATION);
320   DALI_TEST_EQUALS(tangent.x,  0.6f, 0.1f, TEST_LOCATION);
321   DALI_TEST_EQUALS(tangent.y,  0.7f, 0.1f, TEST_LOCATION);
322
323   // t = 0.5
324   path2.Sample(0.5f, position, tangent );
325   DALI_TEST_EQUALS(position.x,  70.0f, TEST_LOCATION);
326   DALI_TEST_EQUALS(position.y, 120.0f, TEST_LOCATION);
327   DALI_TEST_EQUALS(tangent.x,  1.0f, 0.1f, TEST_LOCATION);
328   DALI_TEST_EQUALS(tangent.y,  0.0f, 0.1f, TEST_LOCATION);
329
330
331   //t = 0.75
332   path2.Sample(0.75f, position, tangent );
333   DALI_TEST_EQUALS(position.x,  85.0f, 2.0f, TEST_LOCATION);
334   DALI_TEST_EQUALS(position.y, 112.0f, 2.0f, TEST_LOCATION);
335   DALI_TEST_EQUALS(tangent.x,  0.7f, 0.1f, TEST_LOCATION);
336   DALI_TEST_EQUALS(tangent.y,  -0.6f, 0.1f, TEST_LOCATION);
337
338   // t = 1
339   path2.Sample(1.0f, position, tangent );
340   DALI_TEST_EQUALS(position.x, 100.0f, TEST_LOCATION);
341   DALI_TEST_EQUALS(position.y, 100.0f, TEST_LOCATION);
342   DALI_TEST_EQUALS(tangent.x,  0.8f, 0.1f, TEST_LOCATION);
343   DALI_TEST_EQUALS(tangent.y,  -0.4f, 0.1f, TEST_LOCATION);
344
345   END_TEST;
346 }
347
348 int UtcDaliPathAssignment(void)
349 {
350   TestApplication application;
351
352   Dali::Path path = Dali::Path::New();
353   SetupPath(path);
354
355   Dali::Path path2;
356   path2 = path;
357   DALI_TEST_CHECK(path2);
358
359   //t = 0
360   Vector3 position, tangent;
361   path2.Sample(0.0f, position, tangent );
362   DALI_TEST_EQUALS(position.x, 30.0f, TEST_LOCATION);
363   DALI_TEST_EQUALS(position.y, 80.0f, TEST_LOCATION);
364   DALI_TEST_EQUALS(tangent.x,  0.6f, 0.1f, TEST_LOCATION);
365   DALI_TEST_EQUALS(tangent.y,  0.7f, 0.1f, TEST_LOCATION);
366
367   //t = 0.25
368   path2.Sample(0.25f, position, tangent );
369   DALI_TEST_EQUALS(position.x,  48.0f, 2.0f, TEST_LOCATION);
370   DALI_TEST_EQUALS(position.y, 102.0f, 2.0f, TEST_LOCATION);
371   DALI_TEST_EQUALS(tangent.x,  0.6f, 0.1f, TEST_LOCATION);
372   DALI_TEST_EQUALS(tangent.y,  0.7f, 0.1f, TEST_LOCATION);
373
374   // t = 0.5
375   path2.Sample(0.5f, position, tangent );
376   DALI_TEST_EQUALS(position.x,  70.0f, TEST_LOCATION);
377   DALI_TEST_EQUALS(position.y, 120.0f, TEST_LOCATION);
378   DALI_TEST_EQUALS(tangent.x,  1.0f, 0.1f, TEST_LOCATION);
379   DALI_TEST_EQUALS(tangent.y,  0.0f, 0.1f, TEST_LOCATION);
380
381
382   //t = 0.75
383   path2.Sample(0.75f, position, tangent );
384   DALI_TEST_EQUALS(position.x,  85.0f, 2.0f, TEST_LOCATION);
385   DALI_TEST_EQUALS(position.y, 112.0f, 2.0f, TEST_LOCATION);
386   DALI_TEST_EQUALS(tangent.x,  0.7f, 0.1f, TEST_LOCATION);
387   DALI_TEST_EQUALS(tangent.y,  -0.6f, 0.1f, TEST_LOCATION);
388
389   // t = 1
390   path2.Sample(1.0f, position, tangent );
391   DALI_TEST_EQUALS(position.x, 100.0f, TEST_LOCATION);
392   DALI_TEST_EQUALS(position.y, 100.0f, TEST_LOCATION);
393   DALI_TEST_EQUALS(tangent.x,  0.8f, 0.1f, TEST_LOCATION);
394   DALI_TEST_EQUALS(tangent.y,  -0.4f, 0.1f, TEST_LOCATION);
395
396   END_TEST;
397 }
398
399 int UtcDaliPathPropertyPoints(void)
400 {
401   TestApplication application;
402
403   Dali::Path path = Dali::Path::New();
404
405   Dali::Property::Array points;
406   points.Add( Vector3( 100.0f, 100.0f, 100.0f ) )
407         .Add( Vector3( 200.0f, 200.0f, 200.0f ) )
408         .Add( Vector3( 300.0f, 300.0f, 300.0f ) );
409   path.SetProperty( Dali::Path::Property::POINTS, points );
410
411   {
412     Property::Value value = path.GetProperty( Dali::Path::Property::POINTS );
413     Property::Array* array = value.GetArray();
414     DALI_TEST_CHECK( array );
415
416     const unsigned int noOfPoints = points.Size();
417     for( unsigned int i = 0; i < noOfPoints; ++i )
418     {
419       DALI_TEST_EQUALS( ( *array )[i].Get< Vector3 >(), points[i].Get< Vector3 >(), TEST_LOCATION );
420     }
421   }
422
423   {
424     Property::Value value = DevelHandle::GetCurrentProperty( path, Dali::Path::Property::POINTS );
425     Property::Array* array = value.GetArray();
426     DALI_TEST_CHECK( array );
427
428     const unsigned int noOfPoints = points.Size();
429     for( unsigned int i = 0; i < noOfPoints; ++i )
430     {
431       DALI_TEST_EQUALS( ( *array )[i].Get< Vector3 >(), points[i].Get< Vector3 >(), TEST_LOCATION );
432     }
433   }
434
435   END_TEST;
436 }
437
438 int UtcDaliPathPropertyControlPoints(void)
439 {
440   TestApplication application;
441
442   Dali::Path path = Dali::Path::New();
443
444   Dali::Property::Array points;
445   points.Add( Vector3( 0.1f, 0.1f, 0.1f ) )
446         .Add( Vector3( 0.2f, 0.2f, 0.2f ) )
447         .Add( Vector3( 0.3f, 0.3f, 0.3f ) );
448   path.SetProperty( Dali::Path::Property::CONTROL_POINTS, points );
449
450   {
451     Property::Value value = path.GetProperty( Dali::Path::Property::CONTROL_POINTS );
452     Property::Array* array = value.GetArray();
453     DALI_TEST_CHECK( array );
454
455     const unsigned int noOfPoints = points.Size();
456     for( unsigned int i = 0; i < noOfPoints; ++i )
457     {
458       DALI_TEST_EQUALS( ( *array )[i].Get< Vector3 >(), points[i].Get< Vector3 >(), TEST_LOCATION );
459     }
460   }
461
462   {
463     Property::Value value = DevelHandle::GetCurrentProperty( path, Dali::Path::Property::CONTROL_POINTS );
464     Property::Array* array = value.GetArray();
465     DALI_TEST_CHECK( array );
466
467     const unsigned int noOfPoints = points.Size();
468     for( unsigned int i = 0; i < noOfPoints; ++i )
469     {
470       DALI_TEST_EQUALS( ( *array )[i].Get< Vector3 >(), points[i].Get< Vector3 >(), TEST_LOCATION );
471     }
472   }
473
474   END_TEST;
475 }