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