Merge "Fix SWIG C# Build" 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 }
51
52 int UtcDaliRadioButtonConstructorP(void)
53 {
54   TestApplication application;
55
56   RadioButton button;
57
58   DALI_TEST_CHECK( !button );
59   END_TEST;
60 }
61
62 int UtcDaliRadioButtonCopyConstructorP(void)
63 {
64   TestApplication application;
65
66   // Initialize an object, ref count == 1
67   RadioButton button = RadioButton::New();
68
69   RadioButton copy( button );
70   DALI_TEST_CHECK( copy );
71   END_TEST;
72 }
73
74 int UtcDaliRadioButtonAssignmentOperatorP(void)
75 {
76   TestApplication application;
77
78   RadioButton button = RadioButton::New();
79
80   RadioButton copy( button );
81   DALI_TEST_CHECK( copy );
82
83   DALI_TEST_CHECK( button == copy );
84   END_TEST;
85 }
86
87 int UtcDaliRadioButtonNewP(void)
88 {
89   ToolkitTestApplication application;
90   tet_infoline(" UtcDaliRadioButtonNewP");
91
92   // Create the Slider actor
93   RadioButton radioButton;
94
95   DALI_TEST_CHECK( !radioButton );
96
97   radioButton = RadioButton::New();
98
99   DALI_TEST_CHECK( radioButton );
100
101   RadioButton radioButton2(radioButton);
102
103   DALI_TEST_CHECK( radioButton2 == radioButton );
104
105   //Additional check to ensure object is created by checking if it's registered
106   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
107   DALI_TEST_CHECK( registry );
108
109   gObjectCreatedCallBackCalled = false;
110   registry.ObjectCreatedSignal().Connect( &TestCallback );
111   {
112     RadioButton radioButton = RadioButton::New();
113   }
114   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
115   END_TEST;
116 }
117
118 int UtcDaliRadioButtonDestructorP(void)
119 {
120   ToolkitTestApplication application;
121
122   RadioButton* radioButton = new RadioButton();
123   delete radioButton;
124
125   DALI_TEST_CHECK( true );
126   END_TEST;
127 }
128
129 int UtcDaliRadioButtonDownCast(void)
130 {
131   ToolkitTestApplication application;
132
133   Handle handle = RadioButton::New();
134
135   RadioButton radioButton = RadioButton::DownCast( handle );
136
137   DALI_TEST_CHECK( radioButton == handle );
138   END_TEST;
139 }
140
141 int UtcDaliRadioButtonLabelActor(void)
142 {
143   ToolkitTestApplication application;
144
145   std::string labelText = "test actor 1";
146
147   RadioButton radioButton = RadioButton::New();
148
149   radioButton.SetProperty( Toolkit::Button::Property::LABEL,
150                           Property::Map().Add( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::TEXT )
151                                          .Add( Toolkit::TextVisual::Property::POINT_SIZE, 15.0f )
152                         );
153
154   radioButton.SetLabelText( labelText );
155
156   DALI_TEST_EQUALS( radioButton.GetLabelText(), labelText, TEST_LOCATION );
157
158   std::string labelText2 = "test actor 2";
159   radioButton.SetLabelText( labelText2 );
160   DALI_TEST_EQUALS( radioButton.GetLabelText(), labelText2, TEST_LOCATION );
161
162   END_TEST;
163 }
164
165 int UtcDaliRadioButtonSelected(void)
166 {
167   ToolkitTestApplication application;
168
169   RadioButton radioButton = RadioButton::New();
170
171   // Default selected
172   DALI_TEST_CHECK( radioButton.IsSelected() == false );
173
174   // False
175   radioButton.SetSelected( false );
176   DALI_TEST_CHECK( radioButton.IsSelected() == false );
177
178   // True
179   radioButton.SetSelected( true );
180   DALI_TEST_CHECK( radioButton.IsSelected() == true );
181
182   // False
183   radioButton.SetSelected( false );
184   DALI_TEST_CHECK( radioButton.IsSelected() == false );
185
186   END_TEST;
187 }
188
189 int UtcDaliRadioButtonSelectedProperty(void)
190 {
191   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
192   tet_infoline(" UtcDaliRadioButtonSelectedProperty");
193
194   // Create the RadioButton actor
195   RadioButton radioButton = RadioButton::New();
196   Stage::GetCurrent().Add( radioButton );
197   radioButton.SetParentOrigin(ParentOrigin::TOP_LEFT);
198   radioButton.SetAnchorPoint(ParentOrigin::TOP_LEFT);
199   radioButton.SetPosition( 0.0f, 0.0f );
200
201   // Default selected
202   DALI_TEST_CHECK( radioButton.GetProperty<bool>( Button::Property::SELECTED ) == false );
203
204   // Setting false selected
205   radioButton.SetProperty( Button::Property::SELECTED, false );
206   DALI_TEST_CHECK( radioButton.GetProperty<bool>( Button::Property::SELECTED ) == false );
207
208   // Setting true selected
209   radioButton.SetProperty( Button::Property::SELECTED, true );
210   DALI_TEST_CHECK( radioButton.GetProperty<bool>( Button::Property::SELECTED ) == true );
211
212   // Setting false again
213   radioButton.SetProperty( Button::Property::SELECTED, false );
214   DALI_TEST_CHECK( radioButton.GetProperty<bool>( Button::Property::SELECTED ) == false );
215
216   // Test selecting radio buttons
217   RadioButton radioButton2 = RadioButton::New( "label" );
218   radioButton2.SetParentOrigin(ParentOrigin::TOP_LEFT);
219   radioButton2.SetAnchorPoint(ParentOrigin::TOP_LEFT);
220   radioButton2.SetPosition( 0.0f, 0.0f );
221
222   RadioButton radioButton3 = RadioButton::New( "label" );
223   radioButton3.SetParentOrigin(ParentOrigin::TOP_LEFT);
224   radioButton3.SetAnchorPoint(ParentOrigin::TOP_LEFT);
225   radioButton3.SetPosition( 0.0f, 40.0f );
226
227   Actor radioGroup = Actor::New();
228   Stage::GetCurrent().Add( radioGroup );
229   radioGroup.SetParentOrigin(ParentOrigin::TOP_LEFT);
230   radioGroup.SetAnchorPoint(ParentOrigin::TOP_LEFT);
231   radioGroup.SetPosition( 0.0f, 0.0f );
232   radioGroup.SetSize( 400.0f, 400.0 );
233
234   radioGroup.Add( radioButton2 );
235   radioGroup.Add( radioButton3 );
236
237   application.SendNotification();
238   application.Render();
239
240   // Simulate touch events
241   DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == false );
242   DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == false );
243
244   // Select first radio
245   {
246     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
247     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
248
249     Dali::Integration::Point pointDown;
250     pointDown.SetState( PointState::DOWN );
251     pointDown.SetScreenPosition( Vector2( 1.0f, 1.0f ) );
252
253     Dali::Integration::Point pointUp( pointDown );
254     pointUp.SetState( PointState::UP );
255
256     event1.AddPoint( pointDown );
257     application.ProcessEvent( event1 );
258
259     event2.AddPoint( pointUp );
260     application.ProcessEvent( event2 );
261
262     application.SendNotification();
263     application.Render();
264
265     DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == true );
266     DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == false );
267   }
268
269   // Select an already selected radio
270   {
271     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
272     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
273
274     Dali::Integration::Point pointDown;
275     pointDown.SetState( PointState::DOWN );
276     pointDown.SetScreenPosition( Vector2( 1.0f, 1.0f ) );
277
278     Dali::Integration::Point pointUp( pointDown );
279     pointUp.SetState( PointState::UP );
280
281     event1.AddPoint( pointDown );
282     application.ProcessEvent( event1 );
283
284     event2.AddPoint( pointUp );
285     application.ProcessEvent( event2 );
286
287     application.SendNotification();
288     application.Render();
289
290     DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == true );
291     DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == false );
292   }
293
294   // Select second radio
295   {
296     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
297     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
298
299     Dali::Integration::Point pointDown;
300     pointDown.SetState( PointState::DOWN );
301     pointDown.SetScreenPosition( Vector2( 1.0f, 41.0f ) );
302
303     Dali::Integration::Point pointUp( pointDown );
304     pointUp.SetState( PointState::UP );
305
306     event1.AddPoint( pointDown );
307     application.ProcessEvent( event1 );
308
309     event2.AddPoint( pointUp );
310     application.ProcessEvent( event2 );
311
312     application.SendNotification();
313     application.Render();
314
315     DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == false );
316     DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == true );
317   }
318
319   // Select outside radio group
320   {
321     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
322     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
323
324     Dali::Integration::Point pointDown;
325     pointDown.SetState( PointState::DOWN );
326     pointDown.SetScreenPosition( Vector2( 1.0f, 500.0f ) );
327
328     Dali::Integration::Point pointUp( pointDown );
329     pointUp.SetState( PointState::UP );
330
331     event1.AddPoint( pointDown );
332     application.ProcessEvent( event1 );
333
334     event2.AddPoint( pointUp );
335     application.ProcessEvent( event2 );
336
337     application.SendNotification();
338     application.Render();
339
340     DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == false );
341     DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == true );
342   }
343
344   END_TEST;
345 }