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