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