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