a3fed658d0a8cb1e163a874b3854cb09c65be053
[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 UtcDaliRadioButtonNew(void)
51 {
52   ToolkitTestApplication application;
53   tet_infoline(" UtcDaliRadioButtonNew");
54
55   // Create the Slider actor
56   RadioButton radioButton;
57
58   DALI_TEST_CHECK( !radioButton );
59
60   radioButton = RadioButton::New();
61
62   DALI_TEST_CHECK( radioButton );
63
64   RadioButton radioButton2(radioButton);
65
66   DALI_TEST_CHECK( radioButton2 == radioButton );
67
68   //Additional check to ensure object is created by checking if it's registered
69   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
70   DALI_TEST_CHECK( registry );
71
72   gObjectCreatedCallBackCalled = false;
73   registry.ObjectCreatedSignal().Connect( &TestCallback );
74   {
75     RadioButton radioButton = RadioButton::New();
76   }
77   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
78   END_TEST;
79 }
80
81 int UtcDaliRadioButtonDestructor(void)
82 {
83   ToolkitTestApplication application;
84
85   RadioButton* radioButton = new RadioButton();
86   delete radioButton;
87
88   DALI_TEST_CHECK( true );
89   END_TEST;
90 }
91
92 int UtcDaliRadioButtonDownCast(void)
93 {
94   ToolkitTestApplication application;
95
96   Handle handle = RadioButton::New();
97
98   RadioButton radioButton = RadioButton::DownCast( handle );
99
100   DALI_TEST_CHECK( radioButton == handle );
101   END_TEST;
102 }
103
104 int UtcDaliRadioButtonLabelActor(void)
105 {
106   // TODO
107   END_TEST;
108 }
109
110 int UtcDaliRadioButtonSelected(void)
111 {
112   ToolkitTestApplication application;
113
114   RadioButton radioButton = RadioButton::New();
115
116   // Default selected
117   DALI_TEST_CHECK( radioButton.IsSelected() == false );
118
119   // False
120   radioButton.SetSelected( false );
121   DALI_TEST_CHECK( radioButton.IsSelected() == false );
122
123   // True
124   radioButton.SetSelected( true );
125   DALI_TEST_CHECK( radioButton.IsSelected() == true );
126
127   // False
128   radioButton.SetSelected( false );
129   DALI_TEST_CHECK( radioButton.IsSelected() == false );
130
131   END_TEST;
132 }
133
134 int UtcDaliRadioButtonSelectedProperty(void)
135 {
136   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
137   tet_infoline(" UtcDaliRadioButtonSelectedProperty");
138
139   // Create the RadioButton actor
140   RadioButton radioButton = RadioButton::New();
141   Stage::GetCurrent().Add( radioButton );
142   radioButton.SetParentOrigin(ParentOrigin::TOP_LEFT);
143   radioButton.SetAnchorPoint(ParentOrigin::TOP_LEFT);
144   radioButton.SetPosition( 0.0f, 0.0f );
145
146   // Default selected
147   DALI_TEST_CHECK( radioButton.GetProperty<bool>( Button::Property::SELECTED ) == false );
148
149   // Setting false selected
150   radioButton.SetProperty( Button::Property::SELECTED, false );
151   DALI_TEST_CHECK( radioButton.GetProperty<bool>( Button::Property::SELECTED ) == false );
152
153   // Setting true selected
154   radioButton.SetProperty( Button::Property::SELECTED, true );
155   DALI_TEST_CHECK( radioButton.GetProperty<bool>( Button::Property::SELECTED ) == true );
156
157   // Setting false again
158   radioButton.SetProperty( Button::Property::SELECTED, false );
159   DALI_TEST_CHECK( radioButton.GetProperty<bool>( Button::Property::SELECTED ) == false );
160
161   // Test selecting radio buttons
162   RadioButton radioButton2 = RadioButton::New( "label" );
163   radioButton2.SetParentOrigin(ParentOrigin::TOP_LEFT);
164   radioButton2.SetAnchorPoint(ParentOrigin::TOP_LEFT);
165   radioButton2.SetPosition( 0.0f, 0.0f );
166
167   RadioButton radioButton3 = RadioButton::New( "label" );
168   radioButton3.SetParentOrigin(ParentOrigin::TOP_LEFT);
169   radioButton3.SetAnchorPoint(ParentOrigin::TOP_LEFT);
170   radioButton3.SetPosition( 0.0f, 40.0f );
171
172   Actor radioGroup = Actor::New();
173   Stage::GetCurrent().Add( radioGroup );
174   radioGroup.SetParentOrigin(ParentOrigin::TOP_LEFT);
175   radioGroup.SetAnchorPoint(ParentOrigin::TOP_LEFT);
176   radioGroup.SetPosition( 0.0f, 0.0f );
177   radioGroup.SetSize( 400.0f, 400.0 );
178
179   radioGroup.Add( radioButton2 );
180   radioGroup.Add( radioButton3 );
181
182   application.SendNotification();
183   application.Render();
184
185   // Simulate touch events
186   DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == false );
187   DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == false );
188
189   // Select first radio
190   {
191     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
192     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
193
194     const Dali::TouchPoint pointDown( 0, TouchPoint::Down, 10.0f, 10.0f );
195     const Dali::TouchPoint pointUp( 0, TouchPoint::Up, 10.0f, 10.0f );
196
197     event1.AddPoint( pointDown );
198     application.ProcessEvent( event1 );
199
200     event2.AddPoint( pointUp );
201     application.ProcessEvent( event2 );
202
203     application.SendNotification();
204     application.Render();
205
206     DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == true );
207     DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == false );
208   }
209
210   // Select an already selected radio
211   {
212     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
213     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
214
215     const Dali::TouchPoint pointDown( 0, TouchPoint::Down, 10.0f, 10.0f );
216     const Dali::TouchPoint pointUp( 0, TouchPoint::Up, 10.0f, 10.0f );
217
218     event1.AddPoint( pointDown );
219     application.ProcessEvent( event1 );
220
221     event2.AddPoint( pointUp );
222     application.ProcessEvent( event2 );
223
224     application.SendNotification();
225     application.Render();
226
227     DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == true );
228     DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == false );
229   }
230
231   // Select second radio
232   {
233     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
234     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
235
236     const Dali::TouchPoint pointDown( 0, TouchPoint::Down, 10.0f, 50.0f );
237     const Dali::TouchPoint pointUp( 0, TouchPoint::Up, 10.0f, 50.0f );
238
239     event1.AddPoint( pointDown );
240     application.ProcessEvent( event1 );
241
242     event2.AddPoint( pointUp );
243     application.ProcessEvent( event2 );
244
245     application.SendNotification();
246     application.Render();
247
248     DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == false );
249     DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == true );
250   }
251
252   // Select outside radio group
253   {
254     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
255     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
256
257     const Dali::TouchPoint pointDown( 0, TouchPoint::Down, 10.0f, 500.0f );
258     const Dali::TouchPoint pointUp( 0, TouchPoint::Up, 10.0f, 500.0f );
259
260     event1.AddPoint( pointDown );
261     application.ProcessEvent( event1 );
262
263     event2.AddPoint( pointUp );
264     application.ProcessEvent( event2 );
265
266     application.SendNotification();
267     application.Render();
268
269     DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == false );
270     DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == true );
271   }
272
273   END_TEST;
274 }