Property refactor in dali-toolkit: Toolkit changes
[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   ToolkitTestApplication application;
107
108   TextView actor1 = TextView::New( "test actor 1" );
109
110   RadioButton radioButton = RadioButton::New( actor1 );
111   DALI_TEST_CHECK( actor1 == radioButton.GetLabel() );
112
113   TextView actor2 = TextView::New( "test actor 2" );
114   radioButton.SetLabel( actor2 );
115   DALI_TEST_CHECK( actor2 == radioButton.GetLabel() );
116
117   END_TEST;
118 }
119
120 int UtcDaliRadioButtonSelected(void)
121 {
122   ToolkitTestApplication application;
123
124   RadioButton radioButton = RadioButton::New();
125
126   // Default selected
127   DALI_TEST_CHECK( radioButton.IsSelected() == false );
128
129   // False
130   radioButton.SetSelected( false );
131   DALI_TEST_CHECK( radioButton.IsSelected() == false );
132
133   // True
134   radioButton.SetSelected( true );
135   DALI_TEST_CHECK( radioButton.IsSelected() == true );
136
137   // False
138   radioButton.SetSelected( false );
139   DALI_TEST_CHECK( radioButton.IsSelected() == false );
140
141   END_TEST;
142 }
143
144 int UtcDaliRadioButtonSelectedProperty(void)
145 {
146   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
147   tet_infoline(" UtcDaliRadioButtonSelectedProperty");
148
149   // Create the RadioButton actor
150   RadioButton radioButton = RadioButton::New();
151   Stage::GetCurrent().Add( radioButton );
152   radioButton.SetParentOrigin(ParentOrigin::TOP_LEFT);
153   radioButton.SetAnchorPoint(ParentOrigin::TOP_LEFT);
154   radioButton.SetPosition( 0.0f, 0.0f );
155
156   // Default selected
157   DALI_TEST_CHECK( radioButton.GetProperty<bool>( Button::Property::SELECTED ) == false );
158
159   // Setting false selected
160   radioButton.SetProperty( Button::Property::SELECTED, false );
161   DALI_TEST_CHECK( radioButton.GetProperty<bool>( Button::Property::SELECTED ) == false );
162
163   // Setting true selected
164   radioButton.SetProperty( Button::Property::SELECTED, true );
165   DALI_TEST_CHECK( radioButton.GetProperty<bool>( Button::Property::SELECTED ) == true );
166
167   // Setting false again
168   radioButton.SetProperty( Button::Property::SELECTED, false );
169   DALI_TEST_CHECK( radioButton.GetProperty<bool>( Button::Property::SELECTED ) == false );
170
171   // Test selecting radio buttons
172   RadioButton radioButton2 = RadioButton::New( "label" );
173   radioButton2.SetParentOrigin(ParentOrigin::TOP_LEFT);
174   radioButton2.SetAnchorPoint(ParentOrigin::TOP_LEFT);
175   radioButton2.SetPosition( 0.0f, 0.0f );
176
177   RadioButton radioButton3 = RadioButton::New( "label" );
178   radioButton3.SetParentOrigin(ParentOrigin::TOP_LEFT);
179   radioButton3.SetAnchorPoint(ParentOrigin::TOP_LEFT);
180   radioButton3.SetPosition( 0.0f, 40.0f );
181
182   Actor radioGroup = Actor::New();
183   Stage::GetCurrent().Add( radioGroup );
184   radioGroup.SetParentOrigin(ParentOrigin::TOP_LEFT);
185   radioGroup.SetAnchorPoint(ParentOrigin::TOP_LEFT);
186   radioGroup.SetPosition( 0.0f, 0.0f );
187   radioGroup.SetSize( 400.0f, 400.0 );
188
189   radioGroup.Add( radioButton2 );
190   radioGroup.Add( radioButton3 );
191
192   application.SendNotification();
193   application.Render();
194
195   // Simulate touch events
196   DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == false );
197   DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == false );
198
199   // Select first radio
200   {
201     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
202     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
203
204     const Dali::TouchPoint pointDown( 0, TouchPoint::Down, 10.0f, 10.0f );
205     const Dali::TouchPoint pointUp( 0, TouchPoint::Up, 10.0f, 10.0f );
206
207     event1.AddPoint( pointDown );
208     application.ProcessEvent( event1 );
209
210     event2.AddPoint( pointUp );
211     application.ProcessEvent( event2 );
212
213     application.SendNotification();
214     application.Render();
215
216     DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == true );
217     DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == false );
218   }
219
220   // Select an already selected radio
221   {
222     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
223     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
224
225     const Dali::TouchPoint pointDown( 0, TouchPoint::Down, 10.0f, 10.0f );
226     const Dali::TouchPoint pointUp( 0, TouchPoint::Up, 10.0f, 10.0f );
227
228     event1.AddPoint( pointDown );
229     application.ProcessEvent( event1 );
230
231     event2.AddPoint( pointUp );
232     application.ProcessEvent( event2 );
233
234     application.SendNotification();
235     application.Render();
236
237     DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == true );
238     DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == false );
239   }
240
241   // Select second radio
242   {
243     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
244     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
245
246     const Dali::TouchPoint pointDown( 0, TouchPoint::Down, 10.0f, 50.0f );
247     const Dali::TouchPoint pointUp( 0, TouchPoint::Up, 10.0f, 50.0f );
248
249     event1.AddPoint( pointDown );
250     application.ProcessEvent( event1 );
251
252     event2.AddPoint( pointUp );
253     application.ProcessEvent( event2 );
254
255     application.SendNotification();
256     application.Render();
257
258     DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == false );
259     DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == true );
260   }
261
262   // Select outside radio group
263   {
264     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
265     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
266
267     const Dali::TouchPoint pointDown( 0, TouchPoint::Down, 10.0f, 500.0f );
268     const Dali::TouchPoint pointUp( 0, TouchPoint::Up, 10.0f, 500.0f );
269
270     event1.AddPoint( pointDown );
271     application.ProcessEvent( event1 );
272
273     event2.AddPoint( pointUp );
274     application.ProcessEvent( event2 );
275
276     application.SendNotification();
277     application.Render();
278
279     DALI_TEST_CHECK( radioButton2.GetProperty<bool>( Button::Property::SELECTED ) == false );
280     DALI_TEST_CHECK( radioButton3.GetProperty<bool>( Button::Property::SELECTED ) == true );
281   }
282
283   END_TEST;
284 }