Updating code formatting
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-ToggleButton.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-toolkit/devel-api/controls/buttons/toggle-button.h>
21 #include <dali/integration-api/events/touch-event-integ.h>
22 #include <stdlib.h>
23 #include <iostream>
24
25 using namespace Dali;
26 using namespace Dali::Toolkit;
27
28 void dali_toggle_button_startup(void)
29 {
30   test_return_value = TET_UNDEF;
31 }
32
33 void dali_toggle_button_cleanup(void)
34 {
35   test_return_value = TET_PASS;
36 }
37
38 namespace
39 {
40 static const char* TEST_IMAGE_ONE   = TEST_RESOURCE_DIR "/icon-delete.png";
41 static const char* TEST_IMAGE_TWO   = TEST_RESOURCE_DIR "/icon-edit.png";
42 static const char* TEST_IMAGE_THREE = TEST_RESOURCE_DIR "/popup_tail_down.png";
43 static const char* TEST_IMAGE_FOUR  = TEST_RESOURCE_DIR "/popup_tail_up.png";
44
45 static const Vector2 INSIDE_TOUCH_POINT_POSITON                = Vector2(240, 400);
46 static const Vector3 BUTTON_POSITON_TO_GET_INSIDE_TOUCH_EVENTS = Vector3(200, 360, 0);
47 static const Size    BUTTON_SIZE_TO_GET_INSIDE_TOUCH_EVENTS    = Size(100, 100);
48
49 static bool gObjectCreatedCallBackCalled;
50
51 static void TestCallback(BaseHandle handle)
52 {
53   gObjectCreatedCallBackCalled = true;
54 }
55
56 Dali::Integration::Point GetPointDownInside()
57 {
58   Dali::Integration::Point point;
59   point.SetState(PointState::DOWN);
60   point.SetScreenPosition(INSIDE_TOUCH_POINT_POSITON);
61   return point;
62 }
63
64 Dali::Integration::Point GetPointUpInside()
65 {
66   Dali::Integration::Point point;
67   point.SetState(PointState::UP);
68   point.SetScreenPosition(INSIDE_TOUCH_POINT_POSITON);
69   return point;
70 }
71
72 } // namespace
73
74 int UtcDaliToggleButtonConstructorP(void)
75 {
76   ToolkitTestApplication application;
77   tet_infoline(" UtcDaliToggleButtonConstructorP");
78
79   ToggleButton button;
80   DALI_TEST_CHECK(!button);
81   END_TEST;
82 }
83
84 int UtcDaliToggleButtonCopyConstructorP(void)
85 {
86   ToolkitTestApplication application;
87   tet_infoline(" UtcDaliToggleButtonCopyConstructorP");
88
89   // Initialize an object, ref count == 1
90   ToggleButton button = ToggleButton::New();
91
92   ToggleButton copy(button);
93   DALI_TEST_CHECK(copy);
94   END_TEST;
95 }
96
97 int UtcDaliToggleButtonAssignmentOperatorP(void)
98 {
99   ToolkitTestApplication application;
100   tet_infoline(" UtcDaliToggleButtonAssignmentOperatorP");
101
102   ToggleButton button = ToggleButton::New();
103
104   ToggleButton copy(button);
105   DALI_TEST_CHECK(copy);
106
107   DALI_TEST_CHECK(button == copy);
108   END_TEST;
109 }
110
111 int UtcDaliToggleButtonNewP(void)
112 {
113   ToolkitTestApplication application;
114   tet_infoline(" UtcDaliToggleButtonNewP");
115
116   // Create the Slider actor
117   ToggleButton toggleButton;
118
119   DALI_TEST_CHECK(!toggleButton);
120
121   toggleButton = ToggleButton::New();
122
123   DALI_TEST_CHECK(toggleButton);
124
125   ToggleButton toggleButton2(toggleButton);
126
127   DALI_TEST_CHECK(toggleButton2 == toggleButton);
128
129   //Additional check to ensure object is created by checking if it's registered
130   ObjectRegistry registry = application.GetCore().GetObjectRegistry();
131   DALI_TEST_CHECK(registry);
132
133   gObjectCreatedCallBackCalled = false;
134   registry.ObjectCreatedSignal().Connect(&TestCallback);
135   {
136     ToggleButton toggleButton = ToggleButton::New();
137   }
138   DALI_TEST_CHECK(gObjectCreatedCallBackCalled);
139   END_TEST;
140 }
141
142 int UtcDaliToggleButtonDestructorP(void)
143 {
144   ToolkitTestApplication application;
145   tet_infoline("UtcDaliToggleButtonDestructorP");
146
147   ToggleButton* toggleButton = new ToggleButton();
148   delete toggleButton;
149
150   DALI_TEST_CHECK(true);
151   END_TEST;
152 }
153
154 int UtcDaliToggleButtonDownCast(void)
155 {
156   ToolkitTestApplication application;
157   tet_infoline("UtcDaliToggleButtonDownCast");
158
159   Handle       handle       = ToggleButton::New();
160   ToggleButton toggleButton = ToggleButton::DownCast(handle);
161
162   DALI_TEST_CHECK(toggleButton == handle);
163   END_TEST;
164 }
165
166 int UtcDaliToggleButtonToggleStatesProperty(void)
167 {
168   ToolkitTestApplication application; // Exceptions require ToolkitTestApplication
169   tet_infoline(" UtcDaliToggleButtonToggleStatesProperty");
170
171   // Create the ToggleButton actor
172   ToggleButton toggleButton = ToggleButton::New();
173   application.GetScene().Add(toggleButton);
174   toggleButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
175   toggleButton.SetProperty(Actor::Property::ANCHOR_POINT, ParentOrigin::TOP_LEFT);
176   toggleButton.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
177
178   { // Check empty array
179     Property::Array toggleIcons;
180     toggleButton.SetProperty(Toolkit::ToggleButton::Property::STATE_VISUALS, toggleIcons);
181
182     application.SendNotification();
183     application.Render();
184
185     Property::Array resultIcons;
186     resultIcons = toggleButton.GetProperty(Toolkit::ToggleButton::Property::STATE_VISUALS).Get<Property::Array>();
187     DALI_TEST_CHECK(resultIcons.Count() == 0);
188   }
189
190   { // Check non-empty Array
191     Property::Array toggleIcons;
192     toggleIcons.PushBack(TEST_IMAGE_ONE); //Icons path
193     toggleIcons.PushBack(TEST_IMAGE_TWO);
194     toggleIcons.PushBack(TEST_IMAGE_THREE);
195     toggleIcons.PushBack(TEST_IMAGE_FOUR);
196     toggleButton.SetProperty(Toolkit::ToggleButton::Property::STATE_VISUALS, toggleIcons);
197
198     application.SendNotification();
199     application.Render();
200
201     Property::Array resultIcons;
202     resultIcons = toggleButton.GetProperty(Toolkit::ToggleButton::Property::STATE_VISUALS).Get<Property::Array>();
203
204     // Check that the result is the same as
205     DALI_TEST_EQUALS(toggleIcons.Count(), resultIcons.Count(), TEST_LOCATION);
206     DALI_TEST_CHECK(toggleIcons[0].Get<std::string>() == resultIcons[0].Get<std::string>());
207     DALI_TEST_CHECK(toggleIcons[1].Get<std::string>() == resultIcons[1].Get<std::string>());
208     DALI_TEST_CHECK(toggleIcons[2].Get<std::string>() == resultIcons[2].Get<std::string>());
209     DALI_TEST_CHECK(toggleIcons[3].Get<std::string>() == resultIcons[3].Get<std::string>());
210   }
211
212   { // Check property::map
213     Property::Map propertyMap1;
214     Vector4       testColor1(1.f, 0.5f, 0.3f, 0.2f);
215     propertyMap1.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR);
216     propertyMap1.Insert(Toolkit::ColorVisual::Property::MIX_COLOR, testColor1);
217
218     Property::Map propertyMap2;
219     Vector4       testColor2(0.5f, 1.f, 0.3f, 0.2f);
220     propertyMap2.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR);
221     propertyMap2.Insert(Toolkit::ColorVisual::Property::MIX_COLOR, testColor2);
222
223     Property::Map propertyMap3;
224     Vector4       testColor3(1.f, 0.5f, 1.f, 0.2f);
225     propertyMap3.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR);
226     propertyMap3.Insert(Toolkit::ColorVisual::Property::MIX_COLOR, testColor3);
227
228     Property::Array toggleMaps;
229     toggleMaps.Add(propertyMap1);
230     toggleMaps.Add(propertyMap2);
231     toggleMaps.Add(propertyMap3);
232     toggleButton.SetProperty(Toolkit::ToggleButton::Property::STATE_VISUALS, toggleMaps);
233
234     application.SendNotification();
235     application.Render();
236
237     Property::Array resultMaps;
238     resultMaps = toggleButton.GetProperty(Toolkit::ToggleButton::Property::STATE_VISUALS).Get<Property::Array>();
239
240     // Check that the result
241     DALI_TEST_EQUALS(toggleMaps.Count(), resultMaps.Count(), TEST_LOCATION);
242   }
243
244   END_TEST;
245 }
246
247 int UtcDaliToggleButtonToggleTipsProperty(void)
248 {
249   ToolkitTestApplication application; // Exceptions require ToolkitTestApplication
250   tet_infoline(" UtcDaliToggleButtonToggleTipsProperty");
251
252   // Create the ToggleButton actor
253   ToggleButton toggleButton = ToggleButton::New();
254   application.GetScene().Add(toggleButton);
255   toggleButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
256   toggleButton.SetProperty(Actor::Property::ANCHOR_POINT, ParentOrigin::TOP_LEFT);
257   toggleButton.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
258
259   { // Check empty tip array
260     Property::Array toggleIcons;
261     toggleIcons.PushBack(TEST_IMAGE_ONE); //Icons path
262     toggleIcons.PushBack(TEST_IMAGE_TWO);
263     toggleIcons.PushBack(TEST_IMAGE_THREE);
264     toggleButton.SetProperty(Toolkit::ToggleButton::Property::STATE_VISUALS, toggleIcons);
265
266     Property::Array toggleTips;
267     toggleButton.SetProperty(Toolkit::ToggleButton::Property::TOOLTIPS, toggleTips);
268
269     application.SendNotification();
270     application.Render();
271
272     Property::Array resultTips;
273     resultTips = toggleButton.GetProperty(Toolkit::ToggleButton::Property::TOOLTIPS).Get<Property::Array>();
274     DALI_TEST_CHECK(resultTips.Count() == 0);
275   }
276
277   { // Check non-empty tip array
278     Property::Array toggleIcons;
279     toggleIcons.PushBack(TEST_IMAGE_ONE); //Icons path
280     toggleIcons.PushBack(TEST_IMAGE_TWO);
281     toggleIcons.PushBack(TEST_IMAGE_THREE);
282     toggleButton.SetProperty(Toolkit::ToggleButton::Property::STATE_VISUALS, toggleIcons);
283
284     Property::Array toggleTips;
285     toggleTips.PushBack("Button State A"); //Tooltip string
286     toggleTips.PushBack("Button State B");
287     toggleTips.PushBack("Button State C");
288     toggleButton.SetProperty(Toolkit::ToggleButton::Property::TOOLTIPS, toggleTips);
289
290     application.SendNotification();
291     application.Render();
292
293     Property::Array resultTips;
294     resultTips = toggleButton.GetProperty(Toolkit::ToggleButton::Property::TOOLTIPS).Get<Property::Array>();
295
296     //Check that the result is the same as
297     DALI_TEST_EQUALS(toggleTips.Count(), resultTips.Count(), TEST_LOCATION);
298     DALI_TEST_CHECK(toggleTips[0].Get<std::string>() == resultTips[0].Get<std::string>());
299     DALI_TEST_CHECK(toggleTips[1].Get<std::string>() == resultTips[1].Get<std::string>());
300     DALI_TEST_CHECK(toggleTips[2].Get<std::string>() == resultTips[2].Get<std::string>());
301     DALI_TEST_CHECK(toggleTips[3].Get<std::string>() == resultTips[3].Get<std::string>());
302   }
303   END_TEST;
304 }
305
306 int UtcDaliToggleButtonStateChange(void)
307 {
308   ToolkitTestApplication application; // Exceptions require ToolkitTestApplication
309   tet_infoline(" UtcDaliToggleButtonStateChange");
310
311   // Create the ToggleButton actor
312   ToggleButton toggleButton = ToggleButton::New();
313   application.GetScene().Add(toggleButton);
314   toggleButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
315   toggleButton.SetProperty(Actor::Property::ANCHOR_POINT, ParentOrigin::TOP_LEFT);
316   toggleButton.SetProperty(Actor::Property::POSITION, BUTTON_POSITON_TO_GET_INSIDE_TOUCH_EVENTS);
317   toggleButton.SetProperty(Actor::Property::SIZE, BUTTON_SIZE_TO_GET_INSIDE_TOUCH_EVENTS);
318
319   Property::Array toggleIcons;
320   toggleIcons.PushBack(TEST_IMAGE_ONE); //Icons path
321   toggleIcons.PushBack(TEST_IMAGE_TWO);
322   toggleIcons.PushBack(TEST_IMAGE_THREE);
323   toggleButton.SetProperty(Toolkit::ToggleButton::Property::STATE_VISUALS, toggleIcons);
324
325   Property::Array toggleTips;
326   toggleTips.PushBack("Button State A"); //Tooltip string
327   toggleTips.PushBack("Button State B");
328   toggleTips.PushBack("Button State C");
329   toggleButton.SetProperty(Toolkit::ToggleButton::Property::TOOLTIPS, toggleTips);
330
331   application.SendNotification();
332   application.Render();
333
334   Property::Array resultIcons;
335   resultIcons = toggleButton.GetProperty(Toolkit::ToggleButton::Property::STATE_VISUALS).Get<Property::Array>();
336   DALI_TEST_EQUALS(toggleIcons.Count(), resultIcons.Count(), TEST_LOCATION);
337
338   Property::Array resultTips;
339   resultTips = toggleButton.GetProperty(Toolkit::ToggleButton::Property::TOOLTIPS).Get<Property::Array>();
340   DALI_TEST_EQUALS(toggleTips.Count(), resultTips.Count(), TEST_LOCATION);
341
342   int index;
343   DALI_TEST_CHECK(toggleButton.GetProperty(Toolkit::ToggleButton::Property::CURRENT_STATE_INDEX).Get(index));
344   DALI_TEST_EQUALS(index, 0, TEST_LOCATION);
345
346   Dali::Integration::TouchEvent event;
347
348   // Touch point down and up inside the button 3 times.
349   event = Dali::Integration::TouchEvent();
350   event.AddPoint(GetPointDownInside());
351   application.ProcessEvent(event);
352
353   event = Dali::Integration::TouchEvent();
354   event.AddPoint(GetPointUpInside());
355   application.ProcessEvent(event);
356
357   DALI_TEST_CHECK(toggleButton.GetProperty(Toolkit::ToggleButton::Property::CURRENT_STATE_INDEX).Get(index));
358   DALI_TEST_EQUALS(index, 1, TEST_LOCATION);
359
360   event = Dali::Integration::TouchEvent();
361   event.AddPoint(GetPointDownInside());
362   application.ProcessEvent(event);
363
364   event = Dali::Integration::TouchEvent();
365   event.AddPoint(GetPointUpInside());
366   application.ProcessEvent(event);
367
368   DALI_TEST_CHECK(toggleButton.GetProperty(Toolkit::ToggleButton::Property::CURRENT_STATE_INDEX).Get(index));
369   DALI_TEST_EQUALS(index, 2, TEST_LOCATION);
370
371   event = Dali::Integration::TouchEvent();
372   event.AddPoint(GetPointDownInside());
373   application.ProcessEvent(event);
374
375   event = Dali::Integration::TouchEvent();
376   event.AddPoint(GetPointUpInside());
377   application.ProcessEvent(event);
378
379   DALI_TEST_CHECK(toggleButton.GetProperty(Toolkit::ToggleButton::Property::CURRENT_STATE_INDEX).Get(index));
380   DALI_TEST_EQUALS(index, 0, TEST_LOCATION);
381
382   END_TEST;
383 }