077d8362ee36c387fa3db73a821fe0b6ff10dad4
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-internal / utc-Dali-Internal-RelayoutController.cpp
1 /*
2  * Copyright (c) 2015 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-test-suite-utils.h>
23
24 #include <dali/internal/event/actors/actor-impl.h>
25 #include <dali/internal/event/size-negotiation/relayout-controller-impl.h>
26
27 using namespace Dali;
28
29 void utc_dali_internal_relayout_controller_startup(void)
30 {
31   test_return_value = TET_UNDEF;
32 }
33
34 void utc_dali_internal_relayout_controller_cleanup(void)
35 {
36   test_return_value = TET_PASS;
37 }
38
39 namespace
40 {
41
42 // Properties to attach to actors for testing
43 const std::string REQUEST_WIDTH( "REQUEST_WIDTH" );
44 const std::string REQUEST_HEIGHT( "REQUEST_HEIGHT" );
45 const std::string EXPECTED_WIDTH_DIRTY( "EXPECTED_WIDTH_DIRTY" );
46 const std::string EXPECTED_HEIGHT_DIRTY( "EXPECTED_HEIGHT_DIRTY" );
47
48 /**
49  * Check to see that the desired test results were achieved
50  */
51 void CheckResults( Actor root )
52 {
53   const bool expectedWidthDirty = root.GetProperty( root.GetPropertyIndex( EXPECTED_WIDTH_DIRTY ) ).Get< bool >();
54   const bool expectedHeightDirty = root.GetProperty( root.GetPropertyIndex( EXPECTED_HEIGHT_DIRTY ) ).Get< bool >();
55
56   Internal::Actor& rootImpl = GetImplementation( root );
57
58   DALI_TEST_CHECK( rootImpl.IsLayoutDirty( WIDTH ) == expectedWidthDirty );
59   DALI_TEST_CHECK( rootImpl.IsLayoutDirty( HEIGHT ) == expectedHeightDirty );
60
61   for( unsigned int i = 0, numChildren = root.GetChildCount(); i < numChildren; ++i )
62   {
63     Actor child = root.GetChildAt( i );
64
65     CheckResults( child );
66   }
67 }
68
69 /**
70  * Create a new actor and enable relayout on it
71  *
72  * @return Return the new actor
73  */
74 Actor NewRelayoutActor( bool expectedWidthDirty, bool expectedHeightDirty, ResizePolicy widthPolicy, ResizePolicy heightPolicy )
75 {
76   Actor actor = Actor::New();
77
78   actor.SetRelayoutEnabled( true );
79
80   actor.SetResizePolicy( widthPolicy, WIDTH );
81   actor.SetResizePolicy( heightPolicy, HEIGHT );
82
83   // Expected results for this actor
84   actor.RegisterProperty( EXPECTED_WIDTH_DIRTY, expectedWidthDirty, Property::READ_WRITE );
85   actor.RegisterProperty( EXPECTED_HEIGHT_DIRTY, expectedHeightDirty, Property::READ_WRITE );
86
87   return actor;
88 }
89
90 /**
91  * Create a new root actor and enable relayout on it
92  *
93  * @return Return the new actor
94  */
95 Actor NewRelayoutRootActor( bool requestWidth, bool requestHeight, bool expectedWidthDirty, bool expectedHeightDirty, ResizePolicy widthPolicy, ResizePolicy heightPolicy )
96 {
97   Actor actor = NewRelayoutActor( expectedWidthDirty, expectedHeightDirty, widthPolicy, heightPolicy );
98
99   // Add properties to configure testing
100   actor.RegisterProperty( REQUEST_WIDTH, requestWidth, Property::READ_WRITE );
101   actor.RegisterProperty( REQUEST_HEIGHT, requestHeight, Property::READ_WRITE );
102
103   return actor;
104 }
105
106 void TestTree( TestApplication& application, Actor root, Actor entryPoint = Actor() )
107 {
108   // Render and notify - clear the flags
109   application.SendNotification();
110   application.Render();
111
112   Internal::RelayoutController* controller = Internal::RelayoutController::Get();
113   controller->SetEnabled( true );
114
115   const bool widthRequest = root.GetProperty( root.GetPropertyIndex( REQUEST_WIDTH ) ).Get< bool >();
116   const bool heightRequest = root.GetProperty( root.GetPropertyIndex( REQUEST_HEIGHT ) ).Get< bool >();
117
118   const Dimension dimensions = Dimension( ( ( widthRequest ) ? WIDTH : 0 ) | ( ( heightRequest ) ? HEIGHT : 0 ) );
119
120   controller->RequestRelayout( ( entryPoint ) ? entryPoint : root, dimensions );
121
122   CheckResults( root );
123 }
124
125 } // anonymous namespace
126
127 int UtcDaliRelayoutControllerGet(void)
128 {
129   TestApplication application;
130
131   Internal::RelayoutController* relayoutController = Internal::RelayoutController::Get();
132
133   DALI_TEST_CHECK( relayoutController );
134
135   END_TEST;
136 }
137
138 int UtcDaliRelayoutControllerRequestRelayout(void)
139 {
140   TestApplication application;
141
142   Actor actor = Actor::New();
143   actor.SetRelayoutEnabled( true );
144
145   Internal::Actor& actorImpl = GetImplementation( actor );
146
147   // Request default enable (false)
148   Internal::RelayoutController* controller = Internal::RelayoutController::Get();
149   controller->RequestRelayout( actor );
150
151   DALI_TEST_CHECK( actorImpl.IsLayoutDirty() );
152
153   END_TEST;
154 }
155
156 int UtcDaliRelayoutController_Relayout_SingleActor(void)
157 {
158   TestApplication application;
159
160   // Construct scene
161   Actor parent = NewRelayoutRootActor( true, true, true, true, FIXED, FIXED );
162
163   TestTree( application, parent );
164
165   END_TEST;
166 }
167
168 int UtcDaliRelayoutController_Relayout_FixedParent(void)
169 {
170   TestApplication application;
171
172   Internal::RelayoutController* controller = Internal::RelayoutController::Get();
173   controller->SetEnabled( false );
174
175   // Construct scene
176   Actor parent = NewRelayoutRootActor( true, true, true, true, FIXED, FIXED );
177
178   // Add a child
179   Actor child = NewRelayoutActor( false, false, FIXED, FIXED );
180   parent.Add( child );
181
182   TestTree( application, parent );
183
184   END_TEST;
185 }
186
187 int UtcDaliRelayoutController_Relayout_NaturalParent(void)
188 {
189   TestApplication application;
190
191   Internal::RelayoutController* controller = Internal::RelayoutController::Get();
192   controller->SetEnabled( false );
193
194   // Construct scene
195   Actor parent = NewRelayoutRootActor( true, true, true, true, USE_NATURAL_SIZE, USE_NATURAL_SIZE );
196
197   // Add a child
198   Actor child = NewRelayoutActor( false, false, FIXED, FIXED );
199   parent.Add( child );
200
201   // Run the test
202   TestTree( application, parent );
203
204   END_TEST;
205 }
206
207 int UtcDaliRelayoutController_Relayout_FillParent(void)
208 {
209   TestApplication application;
210
211   Internal::RelayoutController* controller = Internal::RelayoutController::Get();
212   controller->SetEnabled( false );
213
214   // Construct scene
215   Actor parent = NewRelayoutRootActor( true, true, true, true, FILL_TO_PARENT, FILL_TO_PARENT );
216
217   // Add a child
218   Actor child = NewRelayoutActor( false, false, FIXED, FIXED );
219   parent.Add( child );
220
221   // Run the test
222   TestTree( application, parent );
223
224   END_TEST;
225 }
226
227 int UtcDaliRelayoutController_Relayout_FitParent(void)
228 {
229   TestApplication application;
230
231   Internal::RelayoutController* controller = Internal::RelayoutController::Get();
232   controller->SetEnabled( false );
233
234   // Construct scene
235   Actor parent = NewRelayoutRootActor( true, true, true, true, FIT_TO_CHILDREN, FIT_TO_CHILDREN );
236
237   // Add a child
238   Actor child = NewRelayoutActor( false, false, FIXED, FIXED );
239   parent.Add( child );
240
241   // Run the test
242   TestTree( application, parent );
243
244   END_TEST;
245 }
246
247 int UtcDaliRelayoutController_Relayout_DepParent1(void)
248 {
249   TestApplication application;
250
251   Internal::RelayoutController* controller = Internal::RelayoutController::Get();
252   controller->SetEnabled( false );
253
254   // Construct scene
255   Actor parent = NewRelayoutRootActor( true, true, true, true, DIMENSION_DEPENDENCY, FIT_TO_CHILDREN );
256
257   // Add a child
258   Actor child = NewRelayoutActor( false, false, FIXED, FIXED );
259   parent.Add( child );
260
261   // Run the test
262   TestTree( application, parent );
263
264   END_TEST;
265 }
266
267 int UtcDaliRelayoutController_Relayout_DepParent2(void)
268 {
269   TestApplication application;
270
271   Internal::RelayoutController* controller = Internal::RelayoutController::Get();
272   controller->SetEnabled( false );
273
274   // Construct scene
275   Actor parent = NewRelayoutRootActor( true, true, true, true, FIT_TO_CHILDREN, DIMENSION_DEPENDENCY );
276
277   Actor child = NewRelayoutActor( false, false, FIXED, FIXED );
278   parent.Add( child );
279
280
281   // Run the test
282   TestTree( application, parent );
283
284   END_TEST;
285 }
286
287 int UtcDaliRelayoutController_Relayout_Child1(void)
288 {
289   TestApplication application;
290
291   Internal::RelayoutController* controller = Internal::RelayoutController::Get();
292   controller->SetEnabled( false );
293
294   // Construct scene
295   Actor parent = NewRelayoutRootActor( true, true, true, true, FIT_TO_CHILDREN, FIT_TO_CHILDREN );
296
297   // Add a child
298   Actor child = NewRelayoutActor( true, true, FIXED, FIXED );
299   parent.Add( child );
300
301   // Run the test
302   TestTree( application, parent, child );
303
304   END_TEST;
305 }
306
307 int UtcDaliRelayoutController_Relayout_Child2(void)
308 {
309   TestApplication application;
310
311   Internal::RelayoutController* controller = Internal::RelayoutController::Get();
312   controller->SetEnabled( false );
313
314   // Construct scene
315   Actor parent = NewRelayoutRootActor( true, true, false, false, FIXED, FIXED );
316
317   // Add a child
318   Actor child = NewRelayoutActor( true, true, FIXED, FIXED );
319   parent.Add( child );
320
321   // Run the test
322   TestTree( application, parent, child );
323
324   END_TEST;
325 }
326
327 int UtcDaliRelayoutController_Relayout_Complex1(void)
328 {
329   TestApplication application;
330
331   Internal::RelayoutController* controller = Internal::RelayoutController::Get();
332   controller->SetEnabled( false );
333
334   // Construct scene
335   Actor parent = NewRelayoutRootActor( true, true, true, true, FIXED, FIXED );
336
337   // Add children
338   Actor child1 = NewRelayoutActor( true, false, FILL_TO_PARENT, FIXED );
339   parent.Add( child1 );
340
341   Actor child2 = NewRelayoutActor( false, true, FIXED, FILL_TO_PARENT );
342   parent.Add( child2 );
343
344   Actor child3 = NewRelayoutActor( false, false, USE_NATURAL_SIZE, FIXED );
345   parent.Add( child3 );
346
347   // Grand children 1
348   Actor grandChild1_1 = NewRelayoutActor( true, false, FILL_TO_PARENT, FIXED );
349   child1.Add( grandChild1_1 );
350
351   Actor grandChild1_2 = NewRelayoutActor( false, false, FIXED, FILL_TO_PARENT );
352   child1.Add( grandChild1_2 );
353
354   // Grand children 2
355   Actor grandChild2_1 = NewRelayoutActor( false, false, FIXED, FIXED );
356   child2.Add( grandChild2_1 );
357
358   Actor grandChild2_2 = NewRelayoutActor( false, false, FIXED, FIXED );
359   child2.Add( grandChild2_2 );
360
361   // Run the test
362   TestTree( application, parent );
363
364   END_TEST;
365 }
366
367 int UtcDaliRelayoutController_Relayout_Complex2(void)
368 {
369   TestApplication application;
370
371   Internal::RelayoutController* controller = Internal::RelayoutController::Get();
372   controller->SetEnabled( false );
373
374   // Construct scene
375   Actor parent = NewRelayoutRootActor( true, false, true, false, FIXED, FIXED );
376
377   // Add children
378   Actor child1 = NewRelayoutActor( true, false, FILL_TO_PARENT, FIXED );
379   parent.Add( child1 );
380
381   Actor child2 = NewRelayoutActor( false, false, FIXED, FILL_TO_PARENT );
382   parent.Add( child2 );
383
384   Actor child3 = NewRelayoutActor( false, false, USE_NATURAL_SIZE, FIXED );
385   parent.Add( child3 );
386
387   // Grand children 1
388   Actor grandChild1_1 = NewRelayoutActor( true, false, FILL_TO_PARENT, FIXED );
389   child1.Add( grandChild1_1 );
390
391   Actor grandChild1_2 = NewRelayoutActor( false, false, FIXED, FILL_TO_PARENT );
392   child1.Add( grandChild1_2 );
393
394   // Grand children 2
395   Actor grandChild2_1 = NewRelayoutActor( false, false, FIXED, FIXED );
396   child2.Add( grandChild2_1 );
397
398   Actor grandChild2_2 = NewRelayoutActor( false, false, FIXED, FIXED );
399   child2.Add( grandChild2_2 );
400
401   // Run the test
402   TestTree( application, parent );
403
404   END_TEST;
405 }
406
407 int UtcDaliRelayoutController_Relayout_Complex3(void)
408 {
409   TestApplication application;
410
411   Internal::RelayoutController* controller = Internal::RelayoutController::Get();
412   controller->SetEnabled( false );
413
414   // Construct scene
415   Actor parent = NewRelayoutRootActor( false, true, false, true, FIXED, FIXED );
416
417   // Add children
418   Actor child1 = NewRelayoutActor( false, false, FILL_TO_PARENT, FIXED );
419   parent.Add( child1 );
420
421   Actor child2 = NewRelayoutActor( false, true, FIXED, FILL_TO_PARENT );
422   parent.Add( child2 );
423
424   Actor child3 = NewRelayoutActor( false, false, USE_NATURAL_SIZE, FIXED );
425   parent.Add( child3 );
426
427   // Grand children 1
428   Actor grandChild1_1 = NewRelayoutActor( false, false, FILL_TO_PARENT, FIXED );
429   child1.Add( grandChild1_1 );
430
431   Actor grandChild1_2 = NewRelayoutActor( false, false, FIXED, FILL_TO_PARENT );
432   child1.Add( grandChild1_2 );
433
434   // Grand children 2
435   Actor grandChild2_1 = NewRelayoutActor( false, false, FIXED, FIXED );
436   child2.Add( grandChild2_1 );
437
438   Actor grandChild2_2 = NewRelayoutActor( false, false, FIXED, FIXED );
439   child2.Add( grandChild2_2 );
440
441   // Run the test
442   TestTree( application, parent );
443
444   END_TEST;
445 }
446
447 int UtcDaliRelayoutController_Relayout_Dependency(void)
448 {
449   TestApplication application;
450
451   Internal::RelayoutController* controller = Internal::RelayoutController::Get();
452   controller->SetEnabled( false );
453
454   // Construct scene
455   Actor parent = NewRelayoutRootActor( true, true, false, false, FIXED, FIXED );
456
457   // Add a child
458   Actor child = NewRelayoutActor( true, true, FILL_TO_PARENT, FIXED );
459   child.SetResizePolicy( DIMENSION_DEPENDENCY, HEIGHT );
460   parent.Add( child );
461
462   // Run the test
463   TestTree( application, parent, child );
464
465   END_TEST;
466 }
467
468 int UtcDaliRelayoutControllerRequestRelayoutTree(void)
469 {
470   TestApplication application;
471
472   Actor actor = Actor::New();
473   actor.SetRelayoutEnabled( true );
474
475   Internal::Actor& actorImpl = GetImplementation( actor );
476
477   // Check if flag is set
478   DALI_TEST_CHECK( !actorImpl.IsLayoutDirty() );
479
480   Internal::RelayoutController* controller = Internal::RelayoutController::Get();
481   controller->SetEnabled( true );
482
483   // Request default enable (false)
484   controller->RequestRelayoutTree( actor );
485
486   DALI_TEST_CHECK( actorImpl.IsLayoutDirty() );
487
488   END_TEST;
489 }
490