Merge "Fix for TextSelectionToolbar overshootEffectColor styling" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-RadioButton.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 #include <dali-toolkit-test-suite-utils.h>
21 #include <dali-toolkit/dali-toolkit.h>
22 #include <dali/integration-api/events/touch-event-integ.h>
23
24 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
25 #include <dali-toolkit/devel-api/visuals/text-visual-properties.h>
26
27 using namespace Dali;
28 using namespace Dali::Toolkit;
29
30 void dali_radio_button_startup(void)
31 {
32   test_return_value = TET_UNDEF;
33 }
34
35 void dali_radio_button_cleanup(void)
36 {
37   test_return_value = TET_PASS;
38 }
39
40 namespace
41 {
42
43 static bool gObjectCreatedCallBackCalled;
44
45 static void TestCallback(BaseHandle handle)
46 {
47   gObjectCreatedCallBackCalled = true;
48 }
49
50 static std::string GetButtonText( Button button )
51 {
52   Property::Value value = button.GetProperty( Toolkit::Button::Property::LABEL );
53
54   Property::Map *labelProperty = value.GetMap();
55
56   std::string textLabel;
57
58   if ( labelProperty )
59   {
60     Property::Value* value = labelProperty->Find( Toolkit::TextVisual::Property::TEXT );
61     value->Get( textLabel );
62   }
63
64   return textLabel;
65 }
66
67 }
68
69 int UtcDaliRadioButtonConstructorP(void)
70 {
71   TestApplication application;
72
73   RadioButton button;
74
75   DALI_TEST_CHECK( !button );
76   END_TEST;
77 }
78
79 int UtcDaliRadioButtonCopyConstructorP(void)
80 {
81   TestApplication application;
82
83   // Initialize an object, ref count == 1
84   RadioButton button = RadioButton::New();
85
86   RadioButton copy( button );
87   DALI_TEST_CHECK( copy );
88   END_TEST;
89 }
90
91 int UtcDaliRadioButtonAssignmentOperatorP(void)
92 {
93   TestApplication application;
94
95   RadioButton button = RadioButton::New();
96
97   RadioButton copy( button );
98   DALI_TEST_CHECK( copy );
99
100   DALI_TEST_CHECK( button == copy );
101   END_TEST;
102 }
103
104 int UtcDaliRadioButtonNewP(void)
105 {
106   ToolkitTestApplication application;
107   tet_infoline(" UtcDaliRadioButtonNewP");
108
109   // Create the Slider actor
110   RadioButton radioButton;
111
112   DALI_TEST_CHECK( !radioButton );
113
114   radioButton = RadioButton::New();
115
116   DALI_TEST_CHECK( radioButton );
117
118   RadioButton radioButton2(radioButton);
119
120   DALI_TEST_CHECK( radioButton2 == radioButton );
121
122   //Additional check to ensure object is created by checking if it's registered
123   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
124   DALI_TEST_CHECK( registry );
125
126   gObjectCreatedCallBackCalled = false;
127   registry.ObjectCreatedSignal().Connect( &TestCallback );
128   {
129     RadioButton radioButton = RadioButton::New();
130   }
131   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
132   END_TEST;
133 }
134
135 int UtcDaliRadioButtonDestructorP(void)
136 {
137   ToolkitTestApplication application;
138
139   RadioButton* radioButton = new RadioButton();
140   delete radioButton;
141
142   DALI_TEST_CHECK( true );
143   END_TEST;
144 }
145
146 int UtcDaliRadioButtonDownCast(void)
147 {
148   ToolkitTestApplication application;
149
150   Handle handle = RadioButton::New();
151
152   RadioButton radioButton = RadioButton::DownCast( handle );
153
154   DALI_TEST_CHECK( radioButton == handle );
155   END_TEST;
156 }
157
158 int UtcDaliRadioButtonLabelProperty(void)
159 {
160   ToolkitTestApplication application;
161
162   const std::string labelText = "test actor 1";
163
164   RadioButton radioButton = RadioButton::New();
165
166   radioButton.SetProperty( Toolkit::Button::Property::LABEL,
167                           Property::Map().Add( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::TEXT )
168                                          .Add( Toolkit::TextVisual::Property::POINT_SIZE, 15.0f )
169                         );
170
171   radioButton.SetProperty( Toolkit::Button::Property::LABEL, labelText );
172   DALI_TEST_EQUALS( GetButtonText( radioButton ), labelText, TEST_LOCATION );
173
174
175   std::string labelText2 = "test actor 2";
176   radioButton.SetProperty( Toolkit::Button::Property::LABEL, labelText2 );
177
178   DALI_TEST_EQUALS( GetButtonText( radioButton ), labelText2, TEST_LOCATION );
179
180   END_TEST;
181 }
182
183 int UtcDaliRadioButtonSelectedProperty(void)
184 {
185   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
186   tet_infoline(" UtcDaliRadioButtonSelectedProperty");
187
188   // Create the RadioButton actor
189   RadioButton radioButton = RadioButton::New();
190   Stage::GetCurrent().Add( radioButton );
191   radioButton.SetParentOrigin(ParentOrigin::TOP_LEFT);
192   radioButton.SetAnchorPoint(ParentOrigin::TOP_LEFT);
193   radioButton.SetPosition( 0.0f, 0.0f );
194
195   // Default selected
196   DALI_TEST_CHECK( radioButton.GetProperty<bool>( Button::Property::SELECTED ) == false );
197
198   // Setting false selected
199   radioButton.SetProperty( Button::Property::SELECTED, false );
200   DALI_TEST_CHECK( radioButton.GetProperty<bool>( Button::Property::SELECTED ) == false );
201
202   // Setting true selected
203   radioButton.SetProperty( Button::Property::SELECTED, true );
204   DALI_TEST_CHECK( radioButton.GetProperty<bool>( Button::Property::SELECTED ) == true );
205
206   // Setting false again
207   radioButton.SetProperty( Button::Property::SELECTED, false );
208   DALI_TEST_CHECK( radioButton.GetProperty<bool>( Button::Property::SELECTED ) == false );
209
210   // Test selecting radio buttons
211   RadioButton radioButton2 = RadioButton::New( "label" );
212   radioButton2.SetParentOrigin(ParentOrigin::TOP_LEFT);
213   radioButton2.SetAnchorPoint(ParentOrigin::TOP_LEFT);
214   radioButton2.SetPosition( 0.0f, 0.0f );
215
216   RadioButton radioButton3 = RadioButton::New( "label" );
217   radioButton3.SetParentOrigin(ParentOrigin::TOP_LEFT);
218   radioButton3.SetAnchorPoint(ParentOrigin::TOP_LEFT);
219   radioButton3.SetPosition( 0.0f, 40.0f );
220
221   Actor radioGroup = Actor::New();
222   Stage::GetCurrent().Add( radioGroup );
223   radioGroup.SetParentOrigin(ParentOrigin::TOP_LEFT);
224   radioGroup.SetAnchorPoint(ParentOrigin::TOP_LEFT);
225   radioGroup.SetPosition( 0.0f, 0.0f );
226   radioGroup.SetSize( 400.0f, 400.0 );
227
228   radioGroup.Add( radioButton2 );
229   radioGroup.Add( radioButton3 );
230
231   application.SendNotification();
232   application.Render();
233
234   // Simulate touch events
235   DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == false );
236   DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == false );
237
238   // Select first radio
239   {
240     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
241     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
242
243     Dali::Integration::Point pointDown;
244     pointDown.SetState( PointState::DOWN );
245     pointDown.SetScreenPosition( Vector2( 1.0f, 1.0f ) );
246
247     Dali::Integration::Point pointUp( pointDown );
248     pointUp.SetState( PointState::UP );
249
250     event1.AddPoint( pointDown );
251     application.ProcessEvent( event1 );
252
253     event2.AddPoint( pointUp );
254     application.ProcessEvent( event2 );
255
256     application.SendNotification();
257     application.Render();
258
259     DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == true );
260     DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == false );
261   }
262
263   // Select an already selected radio
264   {
265     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
266     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
267
268     Dali::Integration::Point pointDown;
269     pointDown.SetState( PointState::DOWN );
270     pointDown.SetScreenPosition( Vector2( 1.0f, 1.0f ) );
271
272     Dali::Integration::Point pointUp( pointDown );
273     pointUp.SetState( PointState::UP );
274
275     event1.AddPoint( pointDown );
276     application.ProcessEvent( event1 );
277
278     event2.AddPoint( pointUp );
279     application.ProcessEvent( event2 );
280
281     application.SendNotification();
282     application.Render();
283
284     DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == true );
285     DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == false );
286   }
287
288   // Select second radio
289   {
290     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
291     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
292
293     Dali::Integration::Point pointDown;
294     pointDown.SetState( PointState::DOWN );
295     pointDown.SetScreenPosition( Vector2( 1.0f, 41.0f ) );
296
297     Dali::Integration::Point pointUp( pointDown );
298     pointUp.SetState( PointState::UP );
299
300     event1.AddPoint( pointDown );
301     application.ProcessEvent( event1 );
302
303     event2.AddPoint( pointUp );
304     application.ProcessEvent( event2 );
305
306     application.SendNotification();
307     application.Render();
308
309     DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == false );
310     DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == true );
311   }
312
313   // Select outside radio group
314   {
315     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
316     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
317
318     Dali::Integration::Point pointDown;
319     pointDown.SetState( PointState::DOWN );
320     pointDown.SetScreenPosition( Vector2( 1.0f, 500.0f ) );
321
322     Dali::Integration::Point pointUp( pointDown );
323     pointUp.SetState( PointState::UP );
324
325     event1.AddPoint( pointDown );
326     application.ProcessEvent( event1 );
327
328     event2.AddPoint( pointUp );
329     application.ProcessEvent( event2 );
330
331     application.SendNotification();
332     application.Render();
333
334     DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == false );
335     DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == true );
336   }
337
338   END_TEST;
339 }
340
341
342 // Deprecated API Tests
343
344 int UtcDaliRadioButtonLabelActor(void)
345 {
346   ToolkitTestApplication application;
347
348   std::string labelText = "test actor 1";
349
350   RadioButton radioButton = RadioButton::New();
351
352   radioButton.SetProperty( Toolkit::Button::Property::LABEL,
353                           Property::Map().Add( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::TEXT )
354                                          .Add( Toolkit::TextVisual::Property::POINT_SIZE, 15.0f )
355                         );
356
357   radioButton.SetLabelText( labelText );
358
359   DALI_TEST_EQUALS( radioButton.GetLabelText(), labelText, TEST_LOCATION );
360
361   std::string labelText2 = "test actor 2";
362   radioButton.SetLabelText( labelText2 );
363   DALI_TEST_EQUALS( radioButton.GetLabelText(), labelText2, TEST_LOCATION );
364
365   END_TEST;
366 }
367
368 int UtcDaliRadioButtonSelected(void)
369 {
370   ToolkitTestApplication application;
371
372   RadioButton radioButton = RadioButton::New();
373
374   // Default selected
375   DALI_TEST_CHECK( radioButton.IsSelected() == false );
376
377   // False
378   radioButton.SetSelected( false );
379   DALI_TEST_CHECK( radioButton.IsSelected() == false );
380
381   // True
382   radioButton.SetSelected( true );
383   DALI_TEST_CHECK( radioButton.IsSelected() == true );
384
385   // False
386   radioButton.SetSelected( false );
387   DALI_TEST_CHECK( radioButton.IsSelected() == false );
388
389   END_TEST;
390 }
391