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-Slider.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_slider_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void dali_slider_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 } // namespace
47
48 int UtcDaliSliderNew(void)
49 {
50   ToolkitTestApplication application;
51   tet_infoline(" UtcDaliSliderNew");
52
53   // Create the Slider actor
54   Slider slider;
55
56   DALI_TEST_CHECK(!slider);
57
58   slider = Slider::New();
59
60   DALI_TEST_CHECK(slider);
61
62   Slider slider2(slider);
63
64   DALI_TEST_CHECK(slider2 == slider);
65
66   //Additional check to ensure object is created by checking if it's registered
67   ObjectRegistry registry = application.GetCore().GetObjectRegistry();
68   DALI_TEST_CHECK(registry);
69
70   gObjectCreatedCallBackCalled = false;
71   registry.ObjectCreatedSignal().Connect(&TestCallback);
72   {
73     Slider slider = Slider::New();
74   }
75   DALI_TEST_CHECK(gObjectCreatedCallBackCalled);
76   END_TEST;
77 }
78
79 int UtcDaliSliderCopyConstructor(void)
80 {
81   ToolkitTestApplication application;
82
83   Slider slider = Slider::New();
84   DALI_TEST_CHECK(slider);
85
86   Slider copy(slider);
87   DALI_TEST_CHECK(copy);
88
89   END_TEST;
90 }
91
92 int UtcDaliSliderCopyAssignment(void)
93 {
94   ToolkitTestApplication application;
95
96   Slider slider = Slider::New();
97   DALI_TEST_CHECK(slider);
98
99   Slider copy;
100   copy = slider;
101   DALI_TEST_CHECK(copy);
102   DALI_TEST_EQUALS(slider, copy, TEST_LOCATION);
103
104   END_TEST;
105 }
106
107 int UtcDaliSliderMoveConstructor(void)
108 {
109   ToolkitTestApplication application;
110
111   Slider slider = Slider::New();
112   DALI_TEST_EQUALS(1, slider.GetBaseObject().ReferenceCount(), TEST_LOCATION);
113   slider.SetProperty(Actor::Property::SENSITIVE, false);
114   DALI_TEST_CHECK(false == slider.GetProperty<bool>(Actor::Property::SENSITIVE));
115
116   Slider moved = std::move(slider);
117   DALI_TEST_CHECK(moved);
118   DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
119   DALI_TEST_CHECK(false == moved.GetProperty<bool>(Actor::Property::SENSITIVE));
120   DALI_TEST_CHECK(!slider);
121
122   END_TEST;
123 }
124
125 int UtcDaliSliderMoveAssignment(void)
126 {
127   ToolkitTestApplication application;
128
129   Slider slider = Slider::New();
130   DALI_TEST_EQUALS(1, slider.GetBaseObject().ReferenceCount(), TEST_LOCATION);
131   slider.SetProperty(Actor::Property::SENSITIVE, false);
132   DALI_TEST_CHECK(false == slider.GetProperty<bool>(Actor::Property::SENSITIVE));
133
134   Slider moved;
135   moved = std::move(slider);
136   DALI_TEST_CHECK(moved);
137   DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
138   DALI_TEST_CHECK(false == moved.GetProperty<bool>(Actor::Property::SENSITIVE));
139   DALI_TEST_CHECK(!slider);
140
141   END_TEST;
142 }
143
144 int UtcDaliSliderDestructor(void)
145 {
146   ToolkitTestApplication application;
147
148   Slider* slider = new Slider();
149   delete slider;
150
151   DALI_TEST_CHECK(true);
152   END_TEST;
153 }
154
155 int UtcDaliSliderDownCast(void)
156 {
157   ToolkitTestApplication application;
158
159   Handle handle = Slider::New();
160
161   Slider slider = Slider::DownCast(handle);
162
163   DALI_TEST_CHECK(slider == handle);
164   END_TEST;
165 }
166
167 static bool gSliderValueChangedCallBackCalled = false;
168 static bool OnSliderValueChanged(Slider slider, float value)
169 {
170   gSliderValueChangedCallBackCalled = true;
171   return true;
172 }
173
174 static bool gSliderMarkCallBackCalled = false;
175 static bool OnSliderMark(Slider slider, int value)
176 {
177   gSliderMarkCallBackCalled = true;
178   return true;
179 }
180
181 static bool gSliderSlidingFinishedCallBackCalled = false;
182 static bool OnSlidingFinished(Slider slider, float value)
183 {
184   gSliderSlidingFinishedCallBackCalled = true;
185   return true;
186 }
187
188 int UtcDaliSliderSignals1(void)
189 {
190   ToolkitTestApplication application; // Exceptions require ToolkitTestApplication
191   tet_infoline(" UtcDaliSliderSignals1");
192
193   // Create the Popup actor
194   Slider slider = Slider::New();
195   application.GetScene().Add(slider);
196   slider.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
197   slider.SetProperty(Actor::Property::ANCHOR_POINT, ParentOrigin::TOP_LEFT);
198   slider.SetProperty(Actor::Property::SIZE, Vector2(application.GetScene().GetSize().x, 20.0f));
199   slider.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
200
201   const float     MIN_BOUND = 0.0f;
202   const float     MAX_BOUND = 1.0f;
203   const int       NUM_MARKS = 5;
204   Property::Array marks;
205   for(int i = 0; i < NUM_MARKS; ++i)
206   {
207     marks.PushBack(MIN_BOUND + (static_cast<float>(i) / (NUM_MARKS - 1)) * (MAX_BOUND - MIN_BOUND));
208   }
209   slider.SetProperty(Slider::Property::MARKS, marks);
210   slider.SetProperty(Slider::Property::MARK_TOLERANCE, 0.1f);
211
212   slider.ValueChangedSignal().Connect(&OnSliderValueChanged);
213   slider.MarkReachedSignal().Connect(&OnSliderMark);
214   slider.SlidingFinishedSignal().Connect(&OnSlidingFinished);
215
216   application.SendNotification();
217   application.Render();
218
219   gSliderValueChangedCallBackCalled    = false;
220   gSliderMarkCallBackCalled            = false;
221   gSliderSlidingFinishedCallBackCalled = false;
222
223   {
224     Dali::Integration::TouchEvent event = Dali::Integration::TouchEvent();
225     Integration::Point            pointDown;
226     pointDown.SetState(PointState::DOWN);
227     pointDown.SetScreenPosition(Vector2(10.0f, 10.0f));
228     event.AddPoint(pointDown);
229
230     application.ProcessEvent(event);
231     application.SendNotification();
232     application.Render();
233   }
234
235   for(int i = 0; i < 5; ++i)
236   {
237     Dali::Integration::TouchEvent event = Dali::Integration::TouchEvent();
238     Integration::Point            pointMotion;
239     pointMotion.SetState(PointState::MOTION);
240     pointMotion.SetScreenPosition(Vector2(10.0f + i * 10.0f, 10.0f));
241     event.AddPoint(pointMotion);
242
243     application.ProcessEvent(event);
244     application.SendNotification();
245     application.Render();
246   }
247
248   {
249     Dali::Integration::TouchEvent event = Dali::Integration::TouchEvent();
250     Integration::Point            pointUp;
251     pointUp.SetState(PointState::UP);
252     pointUp.SetScreenPosition(Vector2(10.0f, 10.0f));
253     event.AddPoint(pointUp);
254
255     application.ProcessEvent(event);
256     application.SendNotification();
257     application.Render();
258   }
259
260   DALI_TEST_CHECK(gSliderValueChangedCallBackCalled);
261   DALI_TEST_CHECK(gSliderMarkCallBackCalled);
262   DALI_TEST_CHECK(gSliderSlidingFinishedCallBackCalled);
263
264   UnparentAndReset(slider);
265   END_TEST;
266 }
267
268 namespace
269 {
270 bool gSliderSignal = false;
271 struct SliderSignalFunctor
272 {
273   SliderSignalFunctor()
274   {
275   }
276
277   void operator()()
278   {
279     gSliderSignal = true;
280   }
281 };
282 } // namespace
283
284 int UtcDaliSliderSignals2(void)
285 {
286   ToolkitTestApplication application; // Exceptions require ToolkitTestApplication
287   tet_infoline(" UtcDaliSliderSignals1");
288
289   // Create the Popup actor
290   Slider slider = Slider::New();
291   application.GetScene().Add(slider);
292   slider.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
293   slider.SetProperty(Actor::Property::ANCHOR_POINT, ParentOrigin::TOP_LEFT);
294   slider.SetProperty(Actor::Property::SIZE, Vector2(application.GetScene().GetSize().x, 20.0f));
295   slider.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
296
297   const float     MIN_BOUND = 0.0f;
298   const float     MAX_BOUND = 1.0f;
299   const int       NUM_MARKS = 5;
300   Property::Array marks;
301   for(int i = 0; i < NUM_MARKS; ++i)
302   {
303     marks.PushBack(MIN_BOUND + (static_cast<float>(i) / (NUM_MARKS - 1)) * (MAX_BOUND - MIN_BOUND));
304   }
305   slider.SetProperty(Slider::Property::MARKS, marks);
306   slider.SetProperty(Slider::Property::MARK_TOLERANCE, 0.1f);
307
308   gSliderSignal                  = false;
309   ConnectionTracker* testTracker = new ConnectionTracker();
310   slider.ConnectSignal(testTracker, "valueChanged", SliderSignalFunctor());
311
312   application.SendNotification();
313   application.Render();
314
315   gSliderValueChangedCallBackCalled = false;
316   gSliderMarkCallBackCalled         = false;
317
318   Dali::Integration::TouchEvent event;
319
320   event = Dali::Integration::TouchEvent();
321
322   Integration::Point pointDown;
323   pointDown.SetState(PointState::DOWN);
324   pointDown.SetScreenPosition(Vector2(10.0f, 10.0f));
325   event.AddPoint(pointDown);
326
327   for(int i = 0; i < 5; ++i)
328   {
329     Integration::Point pointMotion;
330     pointMotion.SetState(PointState::MOTION);
331     pointMotion.SetScreenPosition(Vector2(10.0f + i * 10.0f, 10.0f));
332     event.AddPoint(pointMotion);
333   }
334
335   Integration::Point pointUp;
336   pointUp.SetState(PointState::UP);
337   pointUp.SetScreenPosition(Vector2(10.0f, 10.0f));
338   event.AddPoint(pointUp);
339
340   application.ProcessEvent(event);
341
342   application.SendNotification();
343   application.Render();
344
345   DALI_TEST_CHECK(gSliderSignal);
346   END_TEST;
347 }
348
349 int UtcDaliSetPropertyP(void)
350 {
351   ToolkitTestApplication application;
352   tet_infoline("UtcDaliSetPropertyP");
353
354   Slider slider = Slider::New();
355   slider.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
356   slider.SetProperty(Actor::Property::ANCHOR_POINT, ParentOrigin::TOP_LEFT);
357   slider.SetProperty(Actor::Property::SIZE, Vector2(application.GetScene().GetSize().x, 20.0f));
358   slider.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
359
360   application.GetScene().Add(slider);
361   application.SendNotification();
362   application.Render();
363
364   slider.SetProperty(Slider::Property::LOWER_BOUND, 1.0f);
365   slider.SetProperty(Slider::Property::UPPER_BOUND, 5.0f);
366   slider.SetProperty(Slider::Property::VALUE, 3.0f);
367   slider.SetProperty(Slider::Property::DISABLED_COLOR, Color::BLACK);
368   slider.SetProperty(Slider::Property::VALUE_PRECISION, 4);
369   slider.SetProperty(Slider::Property::SHOW_POPUP, true);
370   slider.SetProperty(Slider::Property::SHOW_VALUE, true);
371   slider.SetProperty(Slider::Property::MARKS, false);
372   slider.SetProperty(Slider::Property::SNAP_TO_MARKS, false);
373   slider.SetProperty(Slider::Property::MARK_TOLERANCE, 0.5f);
374
375   float lb = slider.GetProperty<float>(Slider::Property::LOWER_BOUND);
376   DALI_TEST_EQUALS(lb, 1.0f, TEST_LOCATION);
377   float ub = slider.GetProperty<float>(Slider::Property::UPPER_BOUND);
378   DALI_TEST_EQUALS(ub, 5.0f, TEST_LOCATION);
379   float val = slider.GetProperty<float>(Slider::Property::VALUE);
380   DALI_TEST_EQUALS(val, 3.0f, TEST_LOCATION);
381   Vector4 color = slider.GetProperty<Vector4>(Slider::Property::DISABLED_COLOR);
382   DALI_TEST_EQUALS(color, Color::BLACK, TEST_LOCATION);
383   int precision = slider.GetProperty<int>(Slider::Property::VALUE_PRECISION);
384   DALI_TEST_EQUALS(precision, 4, TEST_LOCATION);
385   bool showPopup = slider.GetProperty<bool>(Slider::Property::SHOW_POPUP);
386   DALI_TEST_EQUALS(showPopup, true, TEST_LOCATION);
387   bool showValue = slider.GetProperty<bool>(Slider::Property::SHOW_VALUE);
388   DALI_TEST_EQUALS(showValue, true, TEST_LOCATION);
389   bool marks = slider.GetProperty<bool>(Slider::Property::MARKS);
390   DALI_TEST_EQUALS(marks, false, TEST_LOCATION);
391   bool snapToMarks = slider.GetProperty<bool>(Slider::Property::SNAP_TO_MARKS);
392   DALI_TEST_EQUALS(snapToMarks, false, TEST_LOCATION);
393   float tolerance = slider.GetProperty<float>(Slider::Property::MARK_TOLERANCE);
394   DALI_TEST_EQUALS(tolerance, 0.5f, TEST_LOCATION);
395
396   {
397     Property::Map map;
398     map["visualType"] = "IMAGE";
399     map["size"]       = Vector2(200, 200);
400     map["url"]        = "track2.png";
401     slider.SetProperty(Slider::Property::TRACK_VISUAL, map);
402     map["url"] = "handle2.png";
403     slider.SetProperty(Slider::Property::HANDLE_VISUAL, map);
404     map["url"] = "progress2.png";
405     slider.SetProperty(Slider::Property::PROGRESS_VISUAL, map);
406     map["url"] = "popup2.png";
407     slider.SetProperty(Slider::Property::POPUP_VISUAL, map);
408     map["url"] = "popupArrow2.png";
409     slider.SetProperty(Slider::Property::POPUP_ARROW_VISUAL, map);
410
411     Property::Value value     = slider.GetProperty(Slider::Property::TRACK_VISUAL);
412     Property::Map*  resultMap = value.GetMap();
413     DALI_TEST_CHECK(resultMap);
414     Property::Value* url = resultMap->Find("url");
415     DALI_TEST_CHECK(url);
416     DALI_TEST_EQUALS(*url, "track2.png", TEST_LOCATION);
417
418     value     = slider.GetProperty(Slider::Property::HANDLE_VISUAL);
419     resultMap = value.GetMap();
420     DALI_TEST_CHECK(resultMap);
421     url = resultMap->Find("url");
422     DALI_TEST_CHECK(url);
423     DALI_TEST_EQUALS(*url, "handle2.png", TEST_LOCATION);
424
425     value     = slider.GetProperty(Slider::Property::PROGRESS_VISUAL);
426     resultMap = value.GetMap();
427     DALI_TEST_CHECK(resultMap);
428     url = resultMap->Find("url");
429     DALI_TEST_CHECK(url);
430     DALI_TEST_EQUALS(*url, "progress2.png", TEST_LOCATION);
431
432     value     = slider.GetProperty(Slider::Property::POPUP_VISUAL);
433     resultMap = value.GetMap();
434     DALI_TEST_CHECK(resultMap);
435     url = resultMap->Find("url");
436     DALI_TEST_CHECK(url);
437     DALI_TEST_EQUALS(*url, "popup2.png", TEST_LOCATION);
438
439     value     = slider.GetProperty(Slider::Property::POPUP_ARROW_VISUAL);
440     resultMap = value.GetMap();
441     DALI_TEST_CHECK(resultMap);
442     url = resultMap->Find("url");
443     DALI_TEST_CHECK(url);
444     DALI_TEST_EQUALS(*url, "popupArrow2.png", TEST_LOCATION);
445   }
446
447   UnparentAndReset(slider);
448   END_TEST;
449 }
450
451 // DestroyHandleVisualDisplay
452 // CreateValueDisplay
453 // SlidingFinishedSignal()
454 // UpdateSkin disabled
455 // AddPopup
456 // RemovePopup
457 // SnapToMark
458 // HideValueView
459 // GetDisabledColor
460 // GetShowPopup
461 // GetShowVisual
462 // DisplayPopup (with set valueText label)