[dali_1.0.36] Merge branch 'tizen'
[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   Stage::GetCurrent().Add( pushButton );
148
149   pushButton.SetBackgroundImage( image01 );
150   pushButton.SetSize( 10.f, 10.f );
151
152   application.SendNotification();
153   application.Render();
154
155   size = pushButton.GetCurrentSize();
156
157   DALI_TEST_EQUALS( size.width, 10.f, TEST_LOCATION );
158   DALI_TEST_EQUALS( size.height, 10.f, TEST_LOCATION );
159   END_TEST;
160 }
161
162 int UtcDaliButtonClicked(void)
163 {
164   ToolkitTestApplication application;
165   tet_infoline(" UtcDaliButtonClicked");
166
167   PushButton pushButton = PushButton::New();
168   pushButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
169   pushButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
170   pushButton.SetPosition( 240, 400 );
171   pushButton.SetSize( 100, 100 );
172
173   Stage::GetCurrent().Add( pushButton );
174
175   application.SendNotification();
176   application.Render();
177
178   // connect to its touch signal
179   pushButton.ClickedSignal().Connect( &ButtonClicked );
180
181   Dali::Integration::TouchEvent event;
182
183   // Test1. Touch point down and up inside the button.
184
185   gButtonClicked = false;
186   event = Dali::Integration::TouchEvent();
187   event.AddPoint( pointDownInside );
188   application.ProcessEvent( event );
189
190   event = Dali::Integration::TouchEvent();
191   event.AddPoint( pointUpInside );
192   application.ProcessEvent( event );
193
194   DALI_TEST_CHECK( gButtonClicked );
195
196   // Test2. Touch point down and up outside the button.
197
198   gButtonClicked = false;
199   event = Dali::Integration::TouchEvent();
200   event.AddPoint( pointDownOutside );
201   application.ProcessEvent( event );
202
203   event = Dali::Integration::TouchEvent();
204   event.AddPoint( pointUpOutside );
205   application.ProcessEvent( event );
206
207   DALI_TEST_CHECK( !gButtonClicked );
208
209   // Test3. Touch point down inside and up outside the button.
210
211   gButtonClicked = false;
212   event = Dali::Integration::TouchEvent();
213   event.AddPoint( pointDownInside );
214   application.ProcessEvent( event );
215
216   event = Dali::Integration::TouchEvent();
217   event.AddPoint( pointLeave );
218   application.ProcessEvent( event );
219
220   event = Dali::Integration::TouchEvent();
221   event.AddPoint( pointUpOutside );
222   application.ProcessEvent( event );
223
224   DALI_TEST_CHECK( !gButtonClicked );
225
226   // Test4. Touch point down outside and up inside the button.
227
228   gButtonClicked = false;
229   event = Dali::Integration::TouchEvent();
230   event.AddPoint( pointDownOutside );
231   application.ProcessEvent( event );
232
233   event = Dali::Integration::TouchEvent();
234   event.AddPoint( pointEnter );
235   application.ProcessEvent( event );
236
237   event = Dali::Integration::TouchEvent();
238   event.AddPoint( pointUpInside );
239   application.ProcessEvent( event );
240
241   DALI_TEST_CHECK( !gButtonClicked );
242   END_TEST;
243 }
244
245 namespace
246 {
247
248 static bool gClickedCallBackCalled;
249
250 static bool TestClickedCallback(Button button)
251 {
252   gClickedCallBackCalled = true;
253   return true;
254 }
255
256 } // namespace
257
258 int UtcDaliButtonConnectSignal(void)
259 {
260   ToolkitTestApplication application;
261   tet_infoline("UtcDaliButtonConnectSignal()");
262
263   gClickedCallBackCalled = false;
264
265   PushButton pushButton = PushButton::New();
266   pushButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
267   pushButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
268   pushButton.SetPosition( 240, 400 );
269   pushButton.SetSize( 100, 100 );
270
271   Stage::GetCurrent().Add( pushButton );
272
273   application.SendNotification();
274   application.Render();
275
276   // connect to its clicked signal
277   pushButton.ClickedSignal().Connect(TestClickedCallback);
278
279   Dali::Integration::TouchEvent event;
280
281   // Touch point down and up inside the button.
282
283   event = Dali::Integration::TouchEvent();
284   event.AddPoint( pointDownInside );
285   application.ProcessEvent( event );
286
287   event = Dali::Integration::TouchEvent();
288   event.AddPoint( pointUpInside );
289   application.ProcessEvent( event );
290
291   DALI_TEST_CHECK( gClickedCallBackCalled == true );
292
293   gClickedCallBackCalled = false;
294   pushButton.ClickedSignal().Disconnect(TestClickedCallback);
295
296   // simulate another touch event
297   application.ProcessEvent( event );
298
299   DALI_TEST_CHECK( gClickedCallBackCalled == false );
300   END_TEST;
301 }
302
303 int UtcDaliButtonSetGetAnimationTime(void)
304 {
305   ToolkitTestApplication application;
306   tet_infoline(" UtcDaliButtonSetGetAnimationTime");
307
308   PushButton pushButton = PushButton::New();
309
310   pushButton.SetAnimationTime( ANIMATION_TIME );
311
312   DALI_TEST_EQUALS( pushButton.GetAnimationTime(), ANIMATION_TIME, TEST_LOCATION );
313
314   END_TEST;
315 }