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