New size negotiation
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-Control.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 #include <stdlib.h>
20
21 // Need to override adaptor classes for toolkit test harness, so include
22 // test harness headers before dali headers.
23 #include <dali-toolkit-test-suite-utils.h>
24
25 #include <dali.h>
26 #include <dali-toolkit/dali-toolkit.h>
27 #include "dummy-control.h"
28
29 using namespace Dali;
30 using namespace Dali::Toolkit;
31
32 void utc_dali_toolkit_control_startup(void)
33 {
34   test_return_value = TET_UNDEF;
35 }
36
37 void utc_dali_toolkit_control_cleanup(void)
38 {
39   test_return_value = TET_PASS;
40 }
41
42 ///////////////////////////////////////////////////////////////////////////////////////////////////
43
44 namespace
45 {
46
47 bool gObjectCreatedCallBackCalled;
48
49 void TestCallback(BaseHandle handle)
50 {
51   gObjectCreatedCallBackCalled = true;
52 }
53
54 void TestVoidCallback()
55 {
56 }
57
58 } // namespace
59
60 ///////////////////////////////////////////////////////////////////////////////////////////////////
61
62
63 int UtcDaliControlConstructor(void)
64 {
65   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
66
67   DummyControl dummy;
68
69   DALI_TEST_CHECK( !Control::DownCast(dummy) );
70
71   dummy = DummyControl::New();
72
73   DALI_TEST_CHECK( Control::DownCast(dummy) );
74   END_TEST;
75 }
76
77 int UtcDaliControlNew(void)
78 {
79   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
80
81   Control control;
82
83   DALI_TEST_CHECK( !Control::DownCast(control) );
84
85   control = Control::New();
86
87   DALI_TEST_CHECK( Control::DownCast(control) );
88   END_TEST;
89 }
90
91
92 int UtcDaliControlRegister(void)
93 {
94   ToolkitTestApplication application;
95
96   // Ensure the object is registered after creation
97   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
98   DALI_TEST_CHECK( registry );
99
100   gObjectCreatedCallBackCalled = false;
101   registry.ObjectCreatedSignal().Connect( &TestCallback );
102   {
103     Alignment alignment = Alignment::New();
104   }
105   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
106   END_TEST;
107 }
108
109 int UtcDaliControlCopyAndAssignment(void)
110 {
111   ToolkitTestApplication application;
112
113   DummyControl control = DummyControl::New();
114   Control emptyControl;
115
116   Control controlCopy( control );
117   DALI_TEST_CHECK( control == controlCopy );
118
119   Control emptyControlCopy( emptyControl );
120   DALI_TEST_CHECK( emptyControl == emptyControlCopy );
121
122   Control controlEquals;
123   controlEquals = control;
124   DALI_TEST_CHECK( control == controlEquals );
125
126   Control emptyControlEquals;
127   emptyControlEquals = emptyControl;
128   DALI_TEST_CHECK( emptyControl == emptyControlEquals );
129
130   // Self assignment
131   control = control;
132   DALI_TEST_CHECK( control == controlCopy );
133   END_TEST;
134 }
135
136 int UtcDaliControlDownCast(void)
137 {
138   ToolkitTestApplication application;
139
140   DummyControl control;
141
142   DALI_TEST_CHECK( !Control::DownCast( control ) );
143
144   control = DummyControl::New();
145
146   DALI_TEST_CHECK( Control::DownCast( control ) );
147
148   Actor actor;
149
150   DALI_TEST_CHECK( !Control::DownCast( actor ) );
151
152   actor = Actor::New();
153
154   DALI_TEST_CHECK( !Control::DownCast( actor ) );
155   END_TEST;
156 }
157
158 int UtcDaliControlDownCastTemplate(void)
159 {
160   ToolkitTestApplication application;
161
162   DummyControl control;
163
164   DALI_TEST_CHECK( !DummyControl::DownCast( control ));
165
166   control = DummyControl::New();
167
168   DALI_TEST_CHECK( DummyControl::DownCast( control ) );
169
170   Actor actor;
171
172   DALI_TEST_CHECK( !DummyControl::DownCast( actor ) );
173
174   actor = Actor::New();
175
176   DALI_TEST_CHECK( !DummyControl::DownCast( actor ) );
177   END_TEST;
178 }
179
180 int UtcDaliControlKeyInputFocus(void)
181 {
182   ToolkitTestApplication application;
183   Stage stage = Stage::GetCurrent();
184
185   DummyControl control;
186
187   PushButton pushButton1 = PushButton::New();
188   stage.Add( pushButton1 );
189
190   pushButton1.SetKeyInputFocus();
191   DALI_TEST_CHECK( pushButton1.HasKeyInputFocus() );
192
193   pushButton1.ClearKeyInputFocus();
194   DALI_TEST_CHECK( !pushButton1.HasKeyInputFocus() );
195   END_TEST;
196 }
197
198 int UtcDaliControlGetImplementation(void)
199 {
200   ToolkitTestApplication application;
201
202   DummyControl control;
203
204   // Get Empty
205   {
206     try
207     {
208       Toolkit::Internal::Control& controlImpl = control.GetImplementation();
209       (void)controlImpl; // Avoid unused warning
210       tet_result(TET_FAIL);
211     }
212     catch (DaliException &exception)
213     {
214       tet_result(TET_PASS);
215     }
216   }
217
218   // Get Const Empty
219   {
220     try
221     {
222       const DummyControl constControl(control);
223       const Toolkit::Internal::Control& controlImpl = constControl.GetImplementation();
224       (void)controlImpl; // Avoid unused warning
225       tet_result(TET_FAIL);
226     }
227     catch (DaliException &exception)
228     {
229       tet_result(TET_PASS);
230     }
231   }
232
233   control = DummyControl::New();
234
235   // Get
236   {
237     try
238     {
239       Toolkit::Internal::Control& controlImpl = control.GetImplementation();
240       (void)controlImpl; // Avoid unused warning
241       tet_result(TET_PASS);
242     }
243     catch (DaliException &exception)
244     {
245       tet_result(TET_FAIL);
246     }
247   }
248
249   // Get Const
250   {
251     try
252     {
253       const DummyControl constControl(control);
254       const Toolkit::Internal::Control& controlImpl = constControl.GetImplementation();
255       (void)controlImpl; // Avoid unused warning
256       tet_result(TET_PASS);
257     }
258     catch (DaliException &exception)
259     {
260       tet_result(TET_FAIL);
261     }
262   }
263   END_TEST;
264 }
265
266 int UtcDaliControlSignalConnectDisconnect(void)
267 {
268   ToolkitTestApplication application;
269
270   {
271     DummyControl dummy = DummyControlImpl::New();
272
273     Actor actor = Actor::New();
274     DALI_TEST_EQUALS( actor.OnStageSignal().GetConnectionCount(), 0u, TEST_LOCATION );
275     Toolkit::Internal::Control& control = dummy.GetImplementation();
276     DummyControlImpl* dummyImpl = dynamic_cast<DummyControlImpl*>(&control);
277     DALI_TEST_CHECK( dummyImpl );
278
279     actor.OnStageSignal().Connect( dummyImpl, &DummyControlImpl::CustomSlot1 );
280     DALI_TEST_EQUALS( actor.OnStageSignal().GetConnectionCount(), 1u, TEST_LOCATION );
281     DALI_TEST_EQUALS( dummyImpl->mCustomSlot1Called, false, TEST_LOCATION );
282
283     Stage::GetCurrent().Add( actor );
284     DALI_TEST_EQUALS( dummyImpl->mCustomSlot1Called, true, TEST_LOCATION );
285
286     dummyImpl->mCustomSlot1Called = false;
287     actor.OnStageSignal().Disconnect( dummyImpl, &DummyControlImpl::CustomSlot1 );
288     DALI_TEST_EQUALS( actor.OnStageSignal().GetConnectionCount(), 0u, TEST_LOCATION );
289     Stage::GetCurrent().Remove( actor );
290     Stage::GetCurrent().Add( actor );
291     DALI_TEST_EQUALS( dummyImpl->mCustomSlot1Called, false, TEST_LOCATION );
292   }
293   END_TEST;
294 }
295
296 int UtcDaliControlSignalAutomaticDisconnect(void)
297 {
298   ToolkitTestApplication application;
299
300   Actor actor = Actor::New();
301
302   {
303     DummyControl dummy = DummyControlImpl::New();
304     Toolkit::Internal::Control& control = dummy.GetImplementation();
305     DummyControlImpl* dummyImpl = dynamic_cast<DummyControlImpl*>(&control);
306     DALI_TEST_CHECK( dummyImpl );
307
308     actor.OnStageSignal().Connect( dummyImpl, &DummyControlImpl::CustomSlot1 );
309     DALI_TEST_EQUALS( actor.OnStageSignal().GetConnectionCount(), 1u, TEST_LOCATION );
310     DALI_TEST_EQUALS( dummyImpl->mCustomSlot1Called, false, TEST_LOCATION );
311
312     Stage::GetCurrent().Add( actor );
313     DALI_TEST_EQUALS( dummyImpl->mCustomSlot1Called, true, TEST_LOCATION );
314     Stage::GetCurrent().Remove( actor );
315   }
316   // dummyControl automatically disconnects
317
318   DALI_TEST_EQUALS( actor.OnStageSignal().GetConnectionCount(), 0u, TEST_LOCATION );
319
320   const Vector3 ignoredSize( 20, 20, 0 );
321   actor.SetSize( ignoredSize );
322   END_TEST;
323 }
324
325 int UtcDaliControlTestParameters(void)
326 {
327   ToolkitTestApplication application;
328   DummyControl test = DummyControl::New();
329
330   test.SetSize( 0.7f, 0.7f, 0.7f );
331   float width = 640.0f;
332   float height = test.GetHeightForWidth( width );
333   DALI_TEST_CHECK( test.GetWidthForHeight( height ) == width );
334
335   test.KeyEventSignal();
336
337   // Provide coverage for pointer destructor
338   Control* testControlPtr = new Control;
339   DALI_TEST_CHECK( testControlPtr );
340   delete testControlPtr;
341   END_TEST;
342 }
343
344 int UtcDaliControlBackgroundColor(void)
345 {
346   ToolkitTestApplication application;
347   Control control = Control::New();
348
349   DALI_TEST_CHECK( !control.GetBackgroundActor() );
350   DALI_TEST_EQUALS( control.GetBackgroundColor(), Color::TRANSPARENT, TEST_LOCATION );
351
352   control.SetBackgroundColor( Color::RED );
353   DALI_TEST_CHECK( control.GetBackgroundActor() );
354   DALI_TEST_EQUALS( control.GetBackgroundColor(), Color::RED, TEST_LOCATION );
355
356   END_TEST;
357 }
358
359 int UtcDaliControlBackgroundImage(void)
360 {
361   ToolkitTestApplication application;
362   Control control = Control::New();
363
364   DALI_TEST_CHECK( !control.GetBackgroundActor() );
365   DALI_TEST_EQUALS( control.GetBackgroundColor(), Color::TRANSPARENT, TEST_LOCATION );
366
367   Image image = ResourceImage::New("TestImage");
368   control.SetBackgroundImage( image );
369   DALI_TEST_CHECK( control.GetBackgroundActor() );
370   DALI_TEST_EQUALS( control.GetBackgroundColor(), Color::WHITE, TEST_LOCATION );
371
372   control.SetBackgroundColor( Color::GREEN );
373   DALI_TEST_EQUALS( control.GetBackgroundColor(), Color::GREEN, TEST_LOCATION );
374
375   control.ClearBackground();
376   DALI_TEST_CHECK( !control.GetBackgroundActor() );
377   DALI_TEST_EQUALS( control.GetBackgroundColor(), Color::TRANSPARENT, TEST_LOCATION );
378
379   control.SetBackgroundColor( Color::YELLOW );
380   control.SetBackgroundImage( image );
381   DALI_TEST_CHECK( control.GetBackgroundActor() );
382   DALI_TEST_EQUALS( control.GetBackgroundColor(), Color::YELLOW, TEST_LOCATION );
383
384   END_TEST;
385 }
386
387 int UtcDaliControlBackgroundProperties(void)
388 {
389   ToolkitTestApplication application;
390   Control control = Control::New();
391
392   DALI_TEST_CHECK( !control.GetBackgroundActor() );
393   DALI_TEST_EQUALS( control.GetBackgroundColor(), Color::TRANSPARENT, TEST_LOCATION );
394   DALI_TEST_EQUALS( control.GetProperty( Control::Property::BACKGROUND_COLOR ).Get< Vector4 >(), Color::TRANSPARENT, TEST_LOCATION );
395   DALI_TEST_CHECK( control.GetProperty( Control::Property::BACKGROUND_IMAGE ).Get< Property::Map >().Empty() );
396
397   control.SetProperty( Control::Property::BACKGROUND_COLOR, Color::RED );
398   DALI_TEST_CHECK( control.GetBackgroundActor() );
399   DALI_TEST_EQUALS( control.GetBackgroundColor(), Color::RED, TEST_LOCATION );
400   DALI_TEST_EQUALS( control.GetProperty( Control::Property::BACKGROUND_COLOR ).Get< Vector4 >(), Color::RED, TEST_LOCATION );
401
402   Property::Map imageMap;
403   imageMap[ "filename" ] = "TestImage";
404   Property::Map map;
405   map[ "image" ] = imageMap;
406   control.SetProperty( Control::Property::BACKGROUND_IMAGE, map );
407   DALI_TEST_CHECK( control.GetBackgroundActor() );
408   DALI_TEST_EQUALS( control.GetBackgroundColor(), Color::RED, TEST_LOCATION );
409   DALI_TEST_EQUALS( control.GetProperty( Control::Property::BACKGROUND_COLOR ).Get< Vector4 >(), Color::RED, TEST_LOCATION );
410
411   Property::Value propValue = control.GetProperty( Control::Property::BACKGROUND_IMAGE );
412   DALI_TEST_CHECK( propValue.HasKey( "image" ) );
413   DALI_TEST_CHECK( propValue.GetValue( "image" ).HasKey( "filename" ) );
414   DALI_TEST_CHECK( propValue.GetValue( "image" ).GetValue( "filename" ).Get< std::string>() == "TestImage" );
415
416   Property::Map emptyMap;
417   control.SetProperty( Control::Property::BACKGROUND_IMAGE, emptyMap );
418   DALI_TEST_CHECK( !control.GetBackgroundActor() );
419   DALI_TEST_EQUALS( control.GetBackgroundColor(), Color::TRANSPARENT, TEST_LOCATION );
420   DALI_TEST_EQUALS( control.GetProperty( Control::Property::BACKGROUND_COLOR ).Get< Vector4 >(), Color::TRANSPARENT, TEST_LOCATION );
421   DALI_TEST_CHECK( control.GetProperty( Control::Property::BACKGROUND_IMAGE ).Get< Property::Map >().Empty() );
422
423   END_TEST;
424 }
425
426 int UtcDaliControlKeyProperties(void)
427 {
428   ToolkitTestApplication application;
429
430   Control control = Control::New();
431   Stage::GetCurrent().Add( control );
432
433   DALI_TEST_EQUALS( control.HasKeyInputFocus(), control.GetProperty( Control::Property::KEY_INPUT_FOCUS ).Get< bool >(), TEST_LOCATION );
434
435   control.SetKeyInputFocus();
436   DALI_TEST_EQUALS( true, control.GetProperty( Control::Property::KEY_INPUT_FOCUS ).Get< bool >(), TEST_LOCATION );
437
438   control.ClearKeyInputFocus();
439   DALI_TEST_EQUALS( false, control.GetProperty( Control::Property::KEY_INPUT_FOCUS ).Get< bool >(), TEST_LOCATION );
440
441   control.SetProperty( Control::Property::KEY_INPUT_FOCUS, true );
442   DALI_TEST_EQUALS( true, control.HasKeyInputFocus(), TEST_LOCATION );
443
444   END_TEST;
445 }
446
447 int UtcDaliControlGestureSignals(void)
448 {
449   ToolkitTestApplication application;
450   ConnectionTracker connectionTracker;
451   Control control = Control::New();
452
453   // Each gesture detector gets created when connecting to the gesture signals
454   DALI_TEST_CHECK( !control.GetTapGestureDetector() );
455   control.ConnectSignal( &connectionTracker, "tapped", &TestVoidCallback );
456   DALI_TEST_CHECK( control.GetTapGestureDetector() );
457
458   DALI_TEST_CHECK( !control.GetPanGestureDetector() );
459   control.ConnectSignal( &connectionTracker, "panned", &TestVoidCallback );
460   DALI_TEST_CHECK( control.GetPanGestureDetector() );
461
462   DALI_TEST_CHECK( !control.GetPinchGestureDetector() );
463   control.ConnectSignal( &connectionTracker, "pinched", &TestVoidCallback );
464   DALI_TEST_CHECK( control.GetPinchGestureDetector() );
465
466   DALI_TEST_CHECK( !control.GetLongPressGestureDetector() );
467   control.ConnectSignal( &connectionTracker, "long-pressed", &TestVoidCallback );
468   DALI_TEST_CHECK( control.GetLongPressGestureDetector() );
469
470   END_TEST;
471 }