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