Merge "fix issue in negative line spacing with key arrow down" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-RadioButton.cpp
1 /*
2  * Copyright (c) 2022 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 <dali-toolkit-test-suite-utils.h>
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <dali/integration-api/events/touch-event-integ.h>
21 #include <stdlib.h>
22 #include <iostream>
23
24 using namespace Dali;
25 using namespace Dali::Toolkit;
26
27 void dali_radio_button_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void dali_radio_button_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37 namespace
38 {
39 static bool gObjectCreatedCallBackCalled;
40
41 static void TestCallback(BaseHandle handle)
42 {
43   gObjectCreatedCallBackCalled = true;
44 }
45
46 static std::string GetButtonText(Button button)
47 {
48   Property::Value value = button.GetProperty(Toolkit::Button::Property::LABEL);
49
50   Property::Map* labelProperty = value.GetMap();
51
52   std::string textLabel;
53
54   if(labelProperty)
55   {
56     Property::Value* value = labelProperty->Find(Toolkit::TextVisual::Property::TEXT);
57     value->Get(textLabel);
58   }
59
60   return textLabel;
61 }
62
63 } // namespace
64
65 int UtcDaliRadioButtonConstructorP(void)
66 {
67   ToolkitTestApplication application;
68
69   RadioButton button;
70
71   DALI_TEST_CHECK(!button);
72   END_TEST;
73 }
74
75 int UtcDaliRadioButtonCopyConstructorP(void)
76 {
77   ToolkitTestApplication application;
78
79   // Initialize an object, ref count == 1
80   RadioButton button = RadioButton::New();
81
82   RadioButton copy(button);
83   DALI_TEST_CHECK(copy);
84   END_TEST;
85 }
86
87 int UtcDaliRadioButtonMoveConstructor(void)
88 {
89   ToolkitTestApplication application;
90
91   RadioButton button = RadioButton::New();
92   DALI_TEST_EQUALS(1, button.GetBaseObject().ReferenceCount(), TEST_LOCATION);
93   DALI_TEST_EQUALS(button.GetProperty<bool>(Button::Property::TOGGLABLE), true, TEST_LOCATION);
94   button.SetProperty(Button::Property::TOGGLABLE, false);
95   DALI_TEST_EQUALS(button.GetProperty<bool>(Button::Property::TOGGLABLE), false, TEST_LOCATION);
96
97   RadioButton moved = std::move(button);
98   DALI_TEST_CHECK(moved);
99   DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
100   DALI_TEST_EQUALS(moved.GetProperty<bool>(Button::Property::TOGGLABLE), false, TEST_LOCATION);
101   DALI_TEST_CHECK(!button);
102
103   END_TEST;
104 }
105
106 int UtcDaliRadioButtonAssignmentOperatorP(void)
107 {
108   ToolkitTestApplication application;
109
110   RadioButton button = RadioButton::New();
111
112   RadioButton copy(button);
113   DALI_TEST_CHECK(copy);
114
115   DALI_TEST_CHECK(button == copy);
116   END_TEST;
117 }
118
119 int UtcDaliRadioButtonMoveAssignment(void)
120 {
121   ToolkitTestApplication application;
122
123   RadioButton button = RadioButton::New();
124   DALI_TEST_EQUALS(1, button.GetBaseObject().ReferenceCount(), TEST_LOCATION);
125   DALI_TEST_EQUALS(button.GetProperty<bool>(Button::Property::TOGGLABLE), true, TEST_LOCATION);
126   button.SetProperty(Button::Property::TOGGLABLE, false);
127   DALI_TEST_EQUALS(button.GetProperty<bool>(Button::Property::TOGGLABLE), false, TEST_LOCATION);
128
129   RadioButton moved;
130   moved = std::move(button);
131   DALI_TEST_CHECK(moved);
132   DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
133   DALI_TEST_EQUALS(moved.GetProperty<bool>(Button::Property::TOGGLABLE), false, TEST_LOCATION);
134   DALI_TEST_CHECK(!button);
135
136   END_TEST;
137 }
138
139 int UtcDaliRadioButtonNewP(void)
140 {
141   ToolkitTestApplication application;
142   tet_infoline(" UtcDaliRadioButtonNewP");
143
144   // Create the Slider actor
145   RadioButton radioButton;
146
147   DALI_TEST_CHECK(!radioButton);
148
149   radioButton = RadioButton::New();
150
151   DALI_TEST_CHECK(radioButton);
152
153   RadioButton radioButton2(radioButton);
154
155   DALI_TEST_CHECK(radioButton2 == radioButton);
156
157   //Additional check to ensure object is created by checking if it's registered
158   ObjectRegistry registry = application.GetCore().GetObjectRegistry();
159   DALI_TEST_CHECK(registry);
160
161   gObjectCreatedCallBackCalled = false;
162   registry.ObjectCreatedSignal().Connect(&TestCallback);
163   {
164     RadioButton radioButton = RadioButton::New();
165   }
166   DALI_TEST_CHECK(gObjectCreatedCallBackCalled);
167   END_TEST;
168 }
169
170 int UtcDaliRadioButtonDestructorP(void)
171 {
172   ToolkitTestApplication application;
173
174   RadioButton* radioButton = new RadioButton();
175   delete radioButton;
176
177   DALI_TEST_CHECK(true);
178   END_TEST;
179 }
180
181 int UtcDaliRadioButtonDownCast(void)
182 {
183   ToolkitTestApplication application;
184
185   Handle handle = RadioButton::New();
186
187   RadioButton radioButton = RadioButton::DownCast(handle);
188
189   DALI_TEST_CHECK(radioButton == handle);
190   END_TEST;
191 }
192
193 int UtcDaliRadioButtonLabelProperty(void)
194 {
195   ToolkitTestApplication application;
196
197   const std::string labelText = "test actor 1";
198
199   RadioButton radioButton = RadioButton::New();
200
201   radioButton.SetProperty(Toolkit::Button::Property::LABEL,
202                           Property::Map().Add(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT).Add(Toolkit::TextVisual::Property::POINT_SIZE, 15.0f));
203
204   radioButton.SetProperty(Toolkit::Button::Property::LABEL, labelText);
205   DALI_TEST_EQUALS(GetButtonText(radioButton), labelText, TEST_LOCATION);
206
207   std::string labelText2 = "test actor 2";
208   radioButton.SetProperty(Toolkit::Button::Property::LABEL, labelText2);
209
210   DALI_TEST_EQUALS(GetButtonText(radioButton), labelText2, TEST_LOCATION);
211
212   END_TEST;
213 }
214
215 int UtcDaliRadioButtonSelectedProperty(void)
216 {
217   ToolkitTestApplication application; // Exceptions require ToolkitTestApplication
218   tet_infoline(" UtcDaliRadioButtonSelectedProperty");
219
220   // Create the RadioButton actor
221   RadioButton radioButton = RadioButton::New();
222   application.GetScene().Add(radioButton);
223   radioButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
224   radioButton.SetProperty(Actor::Property::ANCHOR_POINT, ParentOrigin::TOP_LEFT);
225   radioButton.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
226
227   // Default selected
228   DALI_TEST_CHECK(radioButton.GetProperty<bool>(Button::Property::SELECTED) == false);
229
230   // Setting false selected
231   radioButton.SetProperty(Button::Property::SELECTED, false);
232   DALI_TEST_CHECK(radioButton.GetProperty<bool>(Button::Property::SELECTED) == false);
233
234   // Setting true selected
235   radioButton.SetProperty(Button::Property::SELECTED, true);
236   DALI_TEST_CHECK(radioButton.GetProperty<bool>(Button::Property::SELECTED) == true);
237
238   // Setting false again
239   radioButton.SetProperty(Button::Property::SELECTED, false);
240   DALI_TEST_CHECK(radioButton.GetProperty<bool>(Button::Property::SELECTED) == false);
241
242   // Test selecting radio buttons
243   RadioButton radioButton2 = RadioButton::New("label");
244   radioButton2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
245   radioButton2.SetProperty(Actor::Property::ANCHOR_POINT, ParentOrigin::TOP_LEFT);
246   radioButton2.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
247
248   RadioButton radioButton3 = RadioButton::New("label");
249   radioButton3.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
250   radioButton3.SetProperty(Actor::Property::ANCHOR_POINT, ParentOrigin::TOP_LEFT);
251   radioButton3.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 40.0f));
252
253   Actor radioGroup = Actor::New();
254   application.GetScene().Add(radioGroup);
255   radioGroup.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
256   radioGroup.SetProperty(Actor::Property::ANCHOR_POINT, ParentOrigin::TOP_LEFT);
257   radioGroup.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
258   radioGroup.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0));
259
260   radioGroup.Add(radioButton2);
261   radioGroup.Add(radioButton3);
262
263   application.SendNotification();
264   application.Render();
265
266   // Simulate touch events
267   DALI_TEST_CHECK(radioButton2.GetProperty<bool>(Button::Property::SELECTED) == false);
268   DALI_TEST_CHECK(radioButton3.GetProperty<bool>(Button::Property::SELECTED) == false);
269
270   // Select first radio
271   {
272     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
273     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
274
275     Dali::Integration::Point pointDown;
276     pointDown.SetState(PointState::DOWN);
277     pointDown.SetScreenPosition(Vector2(1.0f, 1.0f));
278
279     Dali::Integration::Point pointUp(pointDown);
280     pointUp.SetState(PointState::UP);
281
282     event1.AddPoint(pointDown);
283     application.ProcessEvent(event1);
284
285     event2.AddPoint(pointUp);
286     application.ProcessEvent(event2);
287
288     application.SendNotification();
289     application.Render();
290
291     DALI_TEST_CHECK(radioButton2.GetProperty<bool>(Button::Property::SELECTED) == true);
292     DALI_TEST_CHECK(radioButton3.GetProperty<bool>(Button::Property::SELECTED) == false);
293   }
294
295   // Select an already selected radio
296   {
297     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
298     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
299
300     Dali::Integration::Point pointDown;
301     pointDown.SetState(PointState::DOWN);
302     pointDown.SetScreenPosition(Vector2(1.0f, 1.0f));
303
304     Dali::Integration::Point pointUp(pointDown);
305     pointUp.SetState(PointState::UP);
306
307     event1.AddPoint(pointDown);
308     application.ProcessEvent(event1);
309
310     event2.AddPoint(pointUp);
311     application.ProcessEvent(event2);
312
313     application.SendNotification();
314     application.Render();
315
316     DALI_TEST_CHECK(radioButton2.GetProperty<bool>(Button::Property::SELECTED) == true);
317     DALI_TEST_CHECK(radioButton3.GetProperty<bool>(Button::Property::SELECTED) == false);
318   }
319
320   // Select second radio
321   {
322     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
323     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
324
325     Dali::Integration::Point pointDown;
326     pointDown.SetState(PointState::DOWN);
327     pointDown.SetScreenPosition(Vector2(1.0f, 41.0f));
328
329     Dali::Integration::Point pointUp(pointDown);
330     pointUp.SetState(PointState::UP);
331
332     event1.AddPoint(pointDown);
333     application.ProcessEvent(event1);
334
335     event2.AddPoint(pointUp);
336     application.ProcessEvent(event2);
337
338     application.SendNotification();
339     application.Render();
340
341     DALI_TEST_CHECK(radioButton2.GetProperty<bool>(Button::Property::SELECTED) == false);
342     DALI_TEST_CHECK(radioButton3.GetProperty<bool>(Button::Property::SELECTED) == true);
343   }
344
345   // Select outside radio group
346   {
347     Dali::Integration::TouchEvent event1 = Dali::Integration::TouchEvent();
348     Dali::Integration::TouchEvent event2 = Dali::Integration::TouchEvent();
349
350     Dali::Integration::Point pointDown;
351     pointDown.SetState(PointState::DOWN);
352     pointDown.SetScreenPosition(Vector2(1.0f, 500.0f));
353
354     Dali::Integration::Point pointUp(pointDown);
355     pointUp.SetState(PointState::UP);
356
357     event1.AddPoint(pointDown);
358     application.ProcessEvent(event1);
359
360     event2.AddPoint(pointUp);
361     application.ProcessEvent(event2);
362
363     application.SendNotification();
364     application.Render();
365
366     DALI_TEST_CHECK(radioButton2.GetProperty<bool>(Button::Property::SELECTED) == false);
367     DALI_TEST_CHECK(radioButton3.GetProperty<bool>(Button::Property::SELECTED) == true);
368   }
369
370   END_TEST;
371 }