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