c2d7f304ee13a81e0208a3f0496ed43d936f4422
[platform/core/uifw/dali-toolkit.git] / automated-tests / TET / dali-test-suite / buttons / utc-Dali-CheckBoxButton.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
20 #include <stdlib.h>
21 #include <tet_api.h>
22
23 #include <dali/public-api/dali-core.h>
24 #include <dali-toolkit/public-api/controls/buttons/check-box-button.h>
25 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
26
27 #include <dali-toolkit-test-suite-utils.h>
28
29 using namespace Dali;
30 using namespace Toolkit;
31
32 namespace
33 {
34 Image CreateSolidColorImage( const Vector4& color, unsigned int width, unsigned int height )
35 {
36   BitmapImage imageData = BitmapImage::New( width, height, Pixel::RGBA8888 );
37
38   // Create the image
39   PixelBuffer* pixbuf = imageData.GetBuffer();
40   unsigned int size = width * height;
41
42   for( size_t i = 0; i < size; i++ )
43     {
44       pixbuf[i*4+0] = 0xFF * color.r;
45       pixbuf[i*4+1] = 0xFF * color.g;
46       pixbuf[i*4+2] = 0xFF * color.b;
47       pixbuf[i*4+3] = 0xFF * color.a;
48     }
49
50   imageData.Update();
51
52   return imageData;
53 }
54
55 static bool gCheckBoxButtonState = false;
56 bool CheckBoxButtonClicked( Button button )
57 {
58   gCheckBoxButtonState = static_cast<CheckBoxButton&>( button ).IsChecked();
59   return true;
60 }
61 } // namespace
62
63 static void Startup();
64 static void Cleanup();
65
66 extern "C" {
67   void (*tet_startup)() = Startup;
68   void (*tet_cleanup)() = Cleanup;
69 }
70
71 enum {
72   POSITIVE_TC_IDX = 0x01,
73   NEGATIVE_TC_IDX,
74 };
75 #define MAX_NUMBER_OF_TESTS 10000
76 extern "C" {
77   struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
78 }
79
80 // Add test functionality for all APIs in the class (Positive and Negative)
81 TEST_FUNCTION( UtcDaliCheckBoxButtonSetGetChecked, POSITIVE_TC_IDX );
82 TEST_FUNCTION( UtcDaliCheckBoxButtonSetImages, POSITIVE_TC_IDX );
83
84 // Called only once before first test is run.
85 static void Startup()
86 {
87 }
88
89 // Called only once after last test is run
90 static void Cleanup()
91 {
92 }
93
94 static void UtcDaliCheckBoxButtonSetGetChecked()
95 {
96   ToolkitTestApplication application;
97   tet_infoline(" UtcDaliCheckBoxButtonSetGetChecked");
98
99   CheckBoxButton checkBoxButton = CheckBoxButton::New();
100   checkBoxButton.ClickedSignal().Connect( &CheckBoxButtonClicked );
101
102   // global var used to check if CheckBoxButtonClicked is called;
103   gCheckBoxButtonState = false;
104
105   checkBoxButton.SetChecked( true );
106
107   DALI_TEST_CHECK( checkBoxButton.IsChecked() );
108   DALI_TEST_CHECK( gCheckBoxButtonState );
109
110   checkBoxButton.SetChecked( false );
111
112   DALI_TEST_CHECK( !checkBoxButton.IsChecked() );
113   DALI_TEST_CHECK( !gCheckBoxButtonState );
114
115   checkBoxButton.SetChecked( true );
116
117   DALI_TEST_CHECK( checkBoxButton.IsChecked() );
118   DALI_TEST_CHECK( gCheckBoxButtonState );
119 }
120
121 static void UtcDaliCheckBoxButtonSetImages()
122 {
123   ToolkitTestApplication application;
124   tet_infoline(" UtcDaliCheckBoxButtonSetImages");
125
126   Actor imageActor;
127
128   Image image01 = CreateSolidColorImage( Color::RED, 10, 10 );
129   ImageActor imageActor01 = CreateSolidColorActor( Color::RED );
130   imageActor01.SetSize( 20, 20 );
131
132   Image image02 = CreateSolidColorImage( Color::RED, 30, 30 );
133   ImageActor imageActor02 = CreateSolidColorActor( Color::RED );
134   imageActor02.SetSize( 40, 40 );
135
136   Image image03 = CreateSolidColorImage( Color::RED, 50, 50 );
137   ImageActor imageActor03 = CreateSolidColorActor( Color::RED );
138   imageActor03.SetSize( 60, 60 );
139
140   Image image04 = CreateSolidColorImage( Color::RED, 70, 70 );
141   ImageActor imageActor04 = CreateSolidColorActor( Color::RED );
142   imageActor04.SetSize( 80, 80 );
143
144   Vector3 size;
145   CheckBoxButton checkBoxButton = CheckBoxButton::New();
146
147   application.SendNotification();
148   application.Render();
149
150   // Just check if check box button size changes when a bigger image is set.
151
152   checkBoxButton.SetBackgroundImage( image01 );
153
154   application.SendNotification();
155   application.Render();
156
157   size = checkBoxButton.GetBackgroundImage().GetCurrentSize();
158
159   DALI_TEST_EQUALS( size.width, 10.f, TEST_LOCATION );
160   DALI_TEST_EQUALS( size.height, 10.f, TEST_LOCATION );
161
162   checkBoxButton.SetBackgroundImage( imageActor01 );
163
164   application.SendNotification();
165   application.Render();
166
167   size = checkBoxButton.GetBackgroundImage().GetCurrentSize();
168
169   DALI_TEST_EQUALS( size.width, 20.f, TEST_LOCATION );
170   DALI_TEST_EQUALS( size.height, 20.f, TEST_LOCATION );
171
172   checkBoxButton.SetCheckedImage( image02 );
173
174   application.SendNotification();
175   application.Render();
176
177   size = checkBoxButton.GetCheckedImage().GetCurrentSize();
178
179   DALI_TEST_EQUALS( size.width, 30.f, TEST_LOCATION );
180   DALI_TEST_EQUALS( size.height, 30.f, TEST_LOCATION );
181
182   checkBoxButton.SetCheckedImage( imageActor02 );
183
184   application.SendNotification();
185   application.Render();
186
187   size = checkBoxButton.GetCheckedImage().GetCurrentSize();
188
189   DALI_TEST_EQUALS( size.width, 40.f, TEST_LOCATION );
190   DALI_TEST_EQUALS( size.height, 40.f, TEST_LOCATION );
191
192   checkBoxButton.SetDimmedBackgroundImage( image03 );
193
194   application.SendNotification();
195   application.Render();
196
197   size = checkBoxButton.GetDimmedBackgroundImage().GetCurrentSize();
198
199   DALI_TEST_EQUALS( size.width, 50.f, TEST_LOCATION );
200   DALI_TEST_EQUALS( size.height, 50.f, TEST_LOCATION );
201
202   checkBoxButton.SetDimmedBackgroundImage( imageActor03 );
203
204   application.SendNotification();
205   application.Render();
206
207   size = checkBoxButton.GetDimmedBackgroundImage().GetCurrentSize();
208
209   DALI_TEST_EQUALS( size.width, 60.f, TEST_LOCATION );
210   DALI_TEST_EQUALS( size.height, 60.f, TEST_LOCATION );
211
212   checkBoxButton.SetDimmedCheckedImage( image04 );
213
214   application.SendNotification();
215   application.Render();
216
217   size = checkBoxButton.GetDimmedCheckedImage().GetCurrentSize();
218
219   DALI_TEST_EQUALS( size.width, 70.f, TEST_LOCATION );
220   DALI_TEST_EQUALS( size.height, 70.f, TEST_LOCATION );
221
222   checkBoxButton.SetDimmedCheckedImage( imageActor04 );
223
224   application.SendNotification();
225   application.Render();
226
227   size = checkBoxButton.GetDimmedCheckedImage().GetCurrentSize();
228
229   DALI_TEST_EQUALS( size.width, 80.f, TEST_LOCATION );
230   DALI_TEST_EQUALS( size.height, 80.f, TEST_LOCATION );
231 }