Updates following 'Rename BitmapImage as BufferImage'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-Button.cpp
1 /*
2  * Copyright (c) 2014 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
21 // Need to override adaptor classes for toolkit test harness, so include
22 // test harness headers before dali headers.
23 #include <dali-toolkit-test-suite-utils.h>
24
25 #include <dali.h>
26 #include <dali-toolkit/dali-toolkit.h>
27 #include <dali/integration-api/events/touch-event-integ.h>
28
29 using namespace Dali;
30 using namespace Toolkit;
31
32
33 void utc_dali_toolkit_button_startup(void)
34 {
35   test_return_value = TET_UNDEF;
36 }
37
38 void utc_dali_toolkit_button_cleanup(void)
39 {
40   test_return_value = TET_PASS;
41 }
42
43 namespace
44 {
45 static bool gButtonClicked = false;
46
47 static bool ButtonClicked( Button button )
48 {
49   gButtonClicked = true;
50   return false;
51 }
52
53 const Dali::TouchPoint pointDownInside( 0, TouchPoint::Down, 240, 400 );
54 const Dali::TouchPoint pointUpInside( 0, TouchPoint::Up, 240, 400 );
55 const Dali::TouchPoint pointLeave( 0, TouchPoint::Leave, 240, 400 );
56 const Dali::TouchPoint pointEnter( 0, TouchPoint::Motion, 240, 400 );
57 const Dali::TouchPoint pointDownOutside( 0, TouchPoint::Down, 10, 10 );
58 const Dali::TouchPoint pointUpOutside( 0, TouchPoint::Up, 10, 10 );
59
60 static float ANIMATION_TIME( 0.5f );
61 } // namespace
62
63
64
65 // Positive test case for a method
66 int UtcDaliButtonNew(void)
67 {
68   ToolkitTestApplication application;
69   tet_infoline(" UtcDaliButtonNew");
70
71   PushButton pushButton = PushButton::New();
72
73   DALI_TEST_CHECK( pushButton );
74
75   PushButton pushButton2( pushButton );
76
77   DALI_TEST_CHECK( pushButton2 );
78
79   pushButton2.Reset();
80
81   // Test down cast
82   Handle handleButton;
83   handleButton = pushButton;
84   Button downCastPushButton = Button::DownCast( handleButton );
85   DALI_TEST_CHECK( downCastPushButton );
86   PushButton downCastPushButton2 = PushButton::DownCast( handleButton );
87   DALI_TEST_CHECK( downCastPushButton2 );
88
89   END_TEST;
90 }
91
92 int UtcDaliButtonSetProperty(void)
93 {
94   tet_infoline("UtcDaliButtonSetProperty: ");
95   ToolkitTestApplication application;
96
97   PushButton pushButton = PushButton::New();
98
99   pushButton.SetProperty(pushButton.GetPropertyIndex("disabled"), false);
100   DALI_TEST_CHECK( false == pushButton.IsDisabled() );
101   pushButton.SetProperty(pushButton.GetPropertyIndex("disabled"), true);
102   DALI_TEST_CHECK( true == pushButton.IsDisabled() );
103   END_TEST;
104 }
105
106 int UtcDaliButtonSetGetDimmed(void)
107 {
108   ToolkitTestApplication application;
109   tet_infoline(" UtcDaliButtonSetGetDimmed");
110
111   PushButton pushButton = PushButton::New();
112
113   pushButton.SetDisabled( true );
114
115   DALI_TEST_CHECK( pushButton.IsDisabled() );
116
117   pushButton.SetDisabled( false );
118
119   DALI_TEST_CHECK( !pushButton.IsDisabled() );
120
121   pushButton.SetDisabled( true );
122
123   DALI_TEST_CHECK( pushButton.IsDisabled() );
124
125   pushButton.SetDisabled( false );
126
127   DALI_TEST_CHECK( !pushButton.IsDisabled() );
128   END_TEST;
129 }
130
131 int UtcDaliButtonSize(void)
132 {
133   ToolkitTestApplication application;
134   tet_infoline(" UtcDaliButtonSize");
135
136   ImageActor image01 = ImageActor::New(CreateBufferImage());
137   image01.SetSize( 100, 50 );
138
139   PushButton pushButton;
140
141   Vector3 size;
142
143   // Test1 Size is set through Actor API
144
145   // First an image is set, then SetSize is called.
146   pushButton = PushButton::New();
147
148   pushButton.SetBackgroundImage( image01 );
149   pushButton.SetSize( 10.f, 10.f );
150
151   application.SendNotification();
152   application.Render();
153
154   size = pushButton.GetCurrentSize();
155
156   DALI_TEST_EQUALS( size.width, 10.f, TEST_LOCATION );
157   DALI_TEST_EQUALS( size.height, 10.f, TEST_LOCATION );
158   END_TEST;
159 }
160
161 int UtcDaliButtonClicked(void)
162 {
163   ToolkitTestApplication application;
164   tet_infoline(" UtcDaliButtonClicked");
165
166   PushButton pushButton = PushButton::New();
167   pushButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
168   pushButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
169   pushButton.SetPosition( 240, 400 );
170   pushButton.SetSize( 100, 100 );
171
172   Stage::GetCurrent().Add( pushButton );
173
174   application.SendNotification();
175   application.Render();
176
177   // connect to its touch signal
178   pushButton.ClickedSignal().Connect( &ButtonClicked );
179
180   Dali::Integration::TouchEvent event;
181
182   // Test1. Touch point down and up inside the button.
183
184   gButtonClicked = false;
185   event = Dali::Integration::TouchEvent();
186   event.AddPoint( pointDownInside );
187   application.ProcessEvent( event );
188
189   event = Dali::Integration::TouchEvent();
190   event.AddPoint( pointUpInside );
191   application.ProcessEvent( event );
192
193   DALI_TEST_CHECK( gButtonClicked );
194
195   // Test2. Touch point down and up outside the button.
196
197   gButtonClicked = false;
198   event = Dali::Integration::TouchEvent();
199   event.AddPoint( pointDownOutside );
200   application.ProcessEvent( event );
201
202   event = Dali::Integration::TouchEvent();
203   event.AddPoint( pointUpOutside );
204   application.ProcessEvent( event );
205
206   DALI_TEST_CHECK( !gButtonClicked );
207
208   // Test3. Touch point down inside and up outside the button.
209
210   gButtonClicked = false;
211   event = Dali::Integration::TouchEvent();
212   event.AddPoint( pointDownInside );
213   application.ProcessEvent( event );
214
215   event = Dali::Integration::TouchEvent();
216   event.AddPoint( pointLeave );
217   application.ProcessEvent( event );
218
219   event = Dali::Integration::TouchEvent();
220   event.AddPoint( pointUpOutside );
221   application.ProcessEvent( event );
222
223   DALI_TEST_CHECK( !gButtonClicked );
224
225   // Test4. Touch point down outside and up inside the button.
226
227   gButtonClicked = false;
228   event = Dali::Integration::TouchEvent();
229   event.AddPoint( pointDownOutside );
230   application.ProcessEvent( event );
231
232   event = Dali::Integration::TouchEvent();
233   event.AddPoint( pointEnter );
234   application.ProcessEvent( event );
235
236   event = Dali::Integration::TouchEvent();
237   event.AddPoint( pointUpInside );
238   application.ProcessEvent( event );
239
240   DALI_TEST_CHECK( !gButtonClicked );
241   END_TEST;
242 }
243
244 namespace
245 {
246
247 static bool gClickedCallBackCalled;
248
249 static bool TestClickedCallback(Button button)
250 {
251   gClickedCallBackCalled = true;
252   return true;
253 }
254
255 } // namespace
256
257 int UtcDaliButtonConnectSignal(void)
258 {
259   ToolkitTestApplication application;
260   tet_infoline("UtcDaliButtonConnectSignal()");
261
262   gClickedCallBackCalled = false;
263
264   PushButton pushButton = PushButton::New();
265   pushButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
266   pushButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
267   pushButton.SetPosition( 240, 400 );
268   pushButton.SetSize( 100, 100 );
269
270   Stage::GetCurrent().Add( pushButton );
271
272   application.SendNotification();
273   application.Render();
274
275   // connect to its clicked signal
276   pushButton.ClickedSignal().Connect(TestClickedCallback);
277
278   Dali::Integration::TouchEvent event;
279
280   // Touch point down and up inside the button.
281
282   event = Dali::Integration::TouchEvent();
283   event.AddPoint( pointDownInside );
284   application.ProcessEvent( event );
285
286   event = Dali::Integration::TouchEvent();
287   event.AddPoint( pointUpInside );
288   application.ProcessEvent( event );
289
290   DALI_TEST_CHECK( gClickedCallBackCalled == true );
291
292   gClickedCallBackCalled = false;
293   pushButton.ClickedSignal().Disconnect(TestClickedCallback);
294
295   // simulate another touch event
296   application.ProcessEvent( event );
297
298   DALI_TEST_CHECK( gClickedCallBackCalled == false );
299   END_TEST;
300 }
301
302 int UtcDaliButtonSetGetAnimationTime(void)
303 {
304   ToolkitTestApplication application;
305   tet_infoline(" UtcDaliButtonSetGetAnimationTime");
306
307   PushButton pushButton = PushButton::New();
308
309   pushButton.SetAnimationTime( ANIMATION_TIME );
310
311   DALI_TEST_EQUALS( pushButton.GetAnimationTime(), ANIMATION_TIME, TEST_LOCATION );
312
313   END_TEST;
314 }