License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-toolkit.git] / automated-tests / TET / dali-test-suite / view / utc-Dali-View.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 <tet_api.h>
22
23 #include <dali/public-api/dali-core.h>
24 #include <dali-toolkit/public-api/controls/view/view.h>
25 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
26
27 #include <dali-toolkit-test-suite-utils.h>
28
29 using namespace Dali;
30 using namespace Toolkit;
31
32 namespace
33 {
34
35 static bool gAnimationStarted = false;
36
37 void StartAnimation( View, Animation& animation, const Orientation& orientation )
38 {
39   gAnimationStarted = true;
40 }
41
42
43 static bool gObjectCreatedCallBackCalled;
44 static void TestCallback(BaseHandle handle)
45 {
46   gObjectCreatedCallBackCalled = true;
47 }
48
49
50 }
51
52 static void Startup();
53 static void Cleanup();
54
55 extern "C" {
56   void (*tet_startup)() = Startup;
57   void (*tet_cleanup)() = Cleanup;
58 }
59
60 enum {
61   POSITIVE_TC_IDX = 0x01,
62   NEGATIVE_TC_IDX,
63 };
64
65 #define MAX_NUMBER_OF_TESTS 10000
66 extern "C" {
67   struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
68 }
69
70 // Add test functionality for all APIs in the class (Positive and Negative)
71 TEST_FUNCTION( UtcDaliViewNew, POSITIVE_TC_IDX );
72 TEST_FUNCTION( UtcDaliViewAddGetRemoveContentLayer01, POSITIVE_TC_IDX );
73 TEST_FUNCTION( UtcDaliViewAddGetRemoveContentLayer02, NEGATIVE_TC_IDX );
74 TEST_FUNCTION( UtcDaliViewSetGetBackgroundLayer01, POSITIVE_TC_IDX );
75 TEST_FUNCTION( UtcDaliViewSetGetBackgroundLayer02, NEGATIVE_TC_IDX );
76 TEST_FUNCTION( UtcDaliViewSetOrientationFunction, POSITIVE_TC_IDX );
77 TEST_FUNCTION( UtcDaliViewOrientationChanged, POSITIVE_TC_IDX );
78 TEST_FUNCTION( UtcSetAutoRotate, POSITIVE_TC_IDX );
79
80 // Called only once before first test is run.
81 static void Startup()
82 {
83 }
84
85 // Called only once after last test is run
86 static void Cleanup()
87 {
88 }
89
90 static void UtcDaliViewNew()
91 {
92   ToolkitTestApplication application;
93   tet_infoline(" UtcDaliViewNew");
94
95   View view1;
96   DALI_TEST_CHECK( !view1 );
97
98   view1 = View::New();
99   DALI_TEST_CHECK( view1 );
100
101   View view2( view1 );
102   DALI_TEST_CHECK( view2 );
103
104   View view3 = view2;
105   DALI_TEST_CHECK( view3 );
106
107   view1 = NULL;
108   view2 = NULL;
109   view3 = NULL;
110
111   //Additional check to ensure object is created by checking if it's registered
112   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
113   DALI_TEST_CHECK( registry );
114
115   gObjectCreatedCallBackCalled = false;
116   registry.ObjectCreatedSignal().Connect( &TestCallback );
117   {
118     View view = View::New();
119   }
120   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
121 }
122
123 static void UtcDaliViewAddGetRemoveContentLayer01()
124 {
125   ToolkitTestApplication application;
126   tet_infoline(" UtcDaliViewAddGetRemoveContentLayer01");
127
128   View view = View::New();
129   Layer layer1;
130   Layer layer2;
131   Layer layer3;
132   Layer layer4;
133
134   // Test: add and get layers.
135   try
136   {
137     layer1 = Layer::New();
138     layer1.SetName( "Layer1" );
139     layer2 = Layer::New();
140     layer2.SetName( "Layer2" );
141
142     unsigned int layerId1 = view.AddContentLayer( layer1 );
143     unsigned int layerId2 = view.AddContentLayer( layer2 );
144
145     layer3 = view.GetContentLayer( layerId1 );
146     layer4 = view.GetContentLayer( layerId2 );
147
148     DALI_TEST_EQUALS( layer1.GetName(), layer3.GetName(), TEST_LOCATION );
149     DALI_TEST_EQUALS( layer2.GetName(), layer4.GetName(), TEST_LOCATION );
150   }
151   catch( ... )
152   {
153     tet_printf( "UtcDaliViewAddGetRemoveContentLayer: Exception while adding and geting layers to/from view.\n" );
154     tet_result(TET_FAIL);
155   }
156
157   bool test1 = false;
158   bool test2 = false;
159   // Test: remove layers.
160   try
161   {
162     view.RemoveContentLayer( layer3 );
163     view.RemoveContentLayer( layer4 );
164     test1 = true;
165   }
166   catch( ... )
167   {
168     tet_printf( "UtcDaliViewAddGetRemoveContentLayer: Exception while removing layers from view.\n" );
169     tet_result(TET_FAIL);
170   }
171
172   // Test: add same layers again.
173   try
174   {
175     view.AddContentLayer( layer1 );
176     view.AddContentLayer( layer2 );
177     test2 = true;
178   }
179   catch( ... )
180   {
181     tet_printf( "UtcDaliViewAddGetRemoveContentLayer: Exception while adding layers from view after have been removed.\n" );
182     tet_result(TET_FAIL);
183   }
184
185   DALI_TEST_CHECK( test1 && test2 );
186 }
187
188 static void UtcDaliViewAddGetRemoveContentLayer02()
189 {
190   ToolkitTestApplication application;
191   tet_infoline(" UtcDaliViewAddGetRemoveContentLayer02");
192
193   View view = View::New();
194
195   Layer layer1 = Layer::New();
196   layer1.SetName( "Layer1" );
197   Layer layer2 = Layer::New();
198   layer2.SetName( "Layer2" );
199
200   view.AddContentLayer( layer1 );
201   view.AddContentLayer( layer2 );
202
203   // Test: add a layer twice.
204   try
205   {
206     view.AddContentLayer( layer1 );
207   }
208   catch( ... )
209   {
210     tet_result(TET_FAIL);
211   }
212
213   // Test: add an unitialized layer.
214   try
215   {
216     Layer layer;
217     view.AddContentLayer( layer );
218   }
219   catch( DaliException& e )
220   {
221     tet_printf("Assertion %s failed at %s when an unitialized layer is added.\n", e.mCondition.c_str(), e.mLocation.c_str());
222     DALI_TEST_EQUALS( e.mCondition, "layer", TEST_LOCATION );
223   }
224
225   // Test: get a layer which was not added before.
226   Layer layer = view.GetContentLayer( 100 );
227   DALI_TEST_CHECK( !layer );
228
229   // Test: Remove a layer which was not added before.
230   try
231   {
232     Layer layer = Layer::New();
233     view.RemoveContentLayer( layer );
234   }
235   catch( ... )
236   {
237     tet_result(TET_FAIL);
238   }
239
240   tet_result(TET_PASS);
241 }
242
243 static void UtcDaliViewSetGetBackgroundLayer01()
244 {
245   ToolkitTestApplication application;
246   tet_infoline(" UtcDaliViewSetGetBackgroundLayer01");
247
248   View view;
249   Layer layer1, layer2;
250
251   // Test with an actor.
252
253   view = View::New();
254   Stage::GetCurrent().Add( view );
255
256   ImageActor background = CreateSolidColorActor( Color::RED );
257
258   view.SetBackground( background );
259
260   layer1 = view.GetBackgroundLayer();
261
262   DALI_TEST_CHECK( layer1 );
263
264   background = CreateSolidColorActor( Color::GREEN );
265
266   view.SetBackground( background );
267
268   layer2 = view.GetBackgroundLayer();
269
270   DALI_TEST_CHECK( layer2 );
271
272   Stage::GetCurrent().Remove( view );
273 }
274
275 static void UtcDaliViewSetGetBackgroundLayer02()
276 {
277   ToolkitTestApplication application;
278   tet_infoline(" UtcDaliViewSetGetBackgroundLayer02");
279
280   bool assert = false;
281
282   try
283   {
284     View view = View::New();
285
286     ImageActor background = CreateSolidColorActor( Color::RED );
287
288     view.SetBackground( background );
289   }
290   catch( DaliException& e )
291   {
292     tet_printf("Assertion %s failed at %s when trying to add background to the view and the view is not on the stage.\n", e.mCondition.c_str(), e.mLocation.c_str());
293     DALI_TEST_EQUALS( e.mCondition, "mBackgroundLayer.OnStage()", TEST_LOCATION );
294     assert = true;
295   }
296
297   DALI_TEST_CHECK( assert );
298 }
299
300 static void UtcDaliViewSetOrientationFunction()
301 {
302   ToolkitTestApplication application;
303   tet_infoline(" UtcDaliViewSetOrientationFunction");
304
305   // Test it doesn't crash
306   try
307   {
308     View view = View::New();
309     Stage::GetCurrent().Add( view );
310
311     view.SetSize( 480, 800 );
312     view.SetOrientationFunction( Degree( 0.f ), Degree( 90.f ), Degree( 180.f ), Degree( 270.f ) );
313   }
314   catch( ... )
315   {
316     tet_result(TET_FAIL);
317   }
318
319   tet_result(TET_PASS);
320 }
321
322 static void UtcDaliViewOrientationChanged()
323 {
324   ToolkitTestApplication application;
325   tet_infoline(" UtcDaliViewOrientationChanged");
326
327   gAnimationStarted = false;
328
329   // Test it doesn't crash
330   try
331   {
332     View view = View::New();
333     Stage::GetCurrent().Add( view );
334
335     view.SetSize( 480, 800 );
336
337     view.OrientationAnimationStartedSignal().Connect( &StartAnimation );
338
339     application.SendNotification(); // Remove these two lines causes
340     application.Render();           // ToolkitTestApplication destructor to crash
341
342     Orientation orientation = application.GetOrientation().GetHandle();
343     application.GetOrientation().SetDegrees( 90 );
344     view.OrientationChanged( orientation );
345   }
346   catch( ... )
347   {
348     tet_result(TET_FAIL);
349   }
350
351   // Check the view animation started.
352   DALI_TEST_CHECK( gAnimationStarted );
353 }
354
355 static void UtcSetAutoRotate()
356 {
357   ToolkitTestApplication application;
358   tet_infoline(" UtcSetAutoRotate");
359
360   gAnimationStarted = false;
361
362   View view;
363
364   // Test it doesn't crash
365   try
366   {
367     view = View::New();
368     Stage::GetCurrent().Add( view );
369
370     view.SetSize( 480, 800 );
371
372     view.OrientationAnimationStartedSignal().Connect( &StartAnimation );
373
374     application.SendNotification();
375     application.Render();
376
377     Orientation orientation = application.GetOrientation().GetHandle();
378     application.GetOrientation().SetDegrees( 90 );
379     view.OrientationChanged( orientation );
380   }
381   catch( ... )
382   {
383     tet_result(TET_FAIL);
384   }
385
386   // Check the view animation started.
387   DALI_TEST_CHECK( gAnimationStarted );
388
389
390   gAnimationStarted = false;
391
392   try
393   {
394     view = View::New();
395     view.SetAutoRotate( false ); // Animation shouldn't start.
396     Stage::GetCurrent().Add( view );
397
398     view.SetSize( 480, 800 );
399
400     application.SendNotification();
401     application.Render();
402
403     Orientation orientation = application.GetOrientation().GetHandle();
404     application.GetOrientation().SetDegrees( 180 );
405     view.OrientationChanged( orientation );
406   }
407   catch( ... )
408   {
409     tet_result(TET_FAIL);
410   }
411
412   // Check the view animation didn't start.
413   DALI_TEST_CHECK( !gAnimationStarted );
414 }