removed reliance on dali-adaptor
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-unmanaged / utc-Dali-NinePatchImages.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 <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23
24 using namespace Dali;
25
26 namespace {
27
28 Integration::Bitmap* CreateBitmap( unsigned int imageHeight, unsigned int imageWidth, unsigned int initialColor, Pixel::Format pixelFormat )
29 {
30   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, false );
31   Integration::PixelBuffer* pixbuffer = bitmap->GetPackedPixelsProfile()->ReserveBuffer( pixelFormat,  imageWidth,imageHeight,imageWidth,imageHeight );
32   unsigned int bytesPerPixel = GetBytesPerPixel(  pixelFormat );
33
34   memset( pixbuffer,  initialColor , imageHeight*imageWidth*bytesPerPixel);
35
36   return bitmap;
37 }
38
39 void InitialiseRegionsToZeroAlpha( Integration::Bitmap* image, unsigned int imageWidth, unsigned int imageHeight, Pixel::Format pixelFormat  )
40 {
41   PixelBuffer* pixbuffer = image->GetBuffer();
42   unsigned int bytesPerPixel = GetBytesPerPixel(  pixelFormat );
43
44   for ( unsigned int row = 0; row < imageWidth; ++row )
45   {
46     unsigned int pixelOffset = (row*bytesPerPixel);
47     pixbuffer[pixelOffset+3] = 0x00;
48     pixelOffset += ( imageHeight-1 ) * imageWidth * bytesPerPixel;
49     pixbuffer[pixelOffset+3] = 0x00;
50   }
51
52   for ( unsigned int column = 0; column < imageHeight; ++column )
53   {
54     unsigned int pixelOffset = (column*imageWidth*bytesPerPixel);
55     pixbuffer[pixelOffset+3] = 0x00;
56     pixelOffset += (imageWidth-1)*bytesPerPixel;
57     pixbuffer[pixelOffset+3] = 0x00;
58   }
59 }
60
61 void AddStretchRegionsToImage( Integration::Bitmap* image, unsigned int imageWidth, unsigned int imageHeight, const Vector4& requiredStretchBorder , Pixel::Format pixelFormat )
62 {
63   PixelBuffer* pixbuffer = image->GetBuffer();
64   unsigned int bytesPerPixel = GetBytesPerPixel(  pixelFormat );
65
66   for ( unsigned int column = requiredStretchBorder.x; column < imageWidth - requiredStretchBorder.z; ++column )
67   {
68     unsigned int pixelOffset = (column*bytesPerPixel);
69     pixbuffer[pixelOffset] = 0x00;
70     pixbuffer[pixelOffset+1] = 0x00;
71     pixbuffer[pixelOffset+2] = 0x00;
72     pixbuffer[pixelOffset+3] = 0xFF;
73   }
74
75   for ( unsigned int row = requiredStretchBorder.y; row < imageHeight - requiredStretchBorder.w; ++row )
76   {
77     unsigned int pixelOffset = (row*imageWidth*bytesPerPixel);
78     pixbuffer[pixelOffset] = 0x00;
79     pixbuffer[pixelOffset+1] = 0x00;
80     pixbuffer[pixelOffset+2] = 0x00;
81     pixbuffer[pixelOffset+3] = 0xFF;
82   }
83 }
84
85 void AddChildRegionsToImage( Integration::Bitmap* image, unsigned int imageWidth, unsigned int imageHeight, const Vector4& requiredChildRegion, Pixel::Format pixelFormat )
86 {
87   PixelBuffer* pixbuffer = image->GetBuffer();
88   unsigned int bytesPerPixel = GetBytesPerPixel(  pixelFormat );
89
90   Integration::Bitmap::PackedPixelsProfile* srcProfile = image->GetPackedPixelsProfile();
91   unsigned int bufferStride = srcProfile->GetBufferStride();
92
93   // Add bottom child region
94   for ( unsigned int column = requiredChildRegion.x; column < imageWidth - requiredChildRegion.z; ++column )
95   {
96     unsigned int pixelOffset = (column*bytesPerPixel);
97     pixelOffset += ( imageHeight-1 ) * bufferStride;
98     pixbuffer[pixelOffset] = 0x00;
99     pixbuffer[pixelOffset+1] = 0x00;
100     pixbuffer[pixelOffset+2] = 0x00;
101     pixbuffer[pixelOffset+3] = 0xFF;
102   }
103
104   // Add right child region
105   for ( unsigned int row = requiredChildRegion.y; row < imageHeight - requiredChildRegion.w; ++row )
106   {
107     unsigned int pixelOffset = row*bufferStride + (imageWidth-1)*bytesPerPixel;
108     pixbuffer[pixelOffset] = 0x00;
109     pixbuffer[pixelOffset+1] = 0x00;
110     pixbuffer[pixelOffset+2] = 0x00;
111     pixbuffer[pixelOffset+3] = 0xFF;
112   }
113 }
114
115 } // namespace
116
117 int UtcDaliNinePatch01(void)
118 {
119   /* Stretch region left(3) top(2) right (3) bottom (2)
120    *    ss
121    *  OOOOOO
122    *  OOOOOOc
123    * sOOooOOc
124    * sOOooOOc
125    *  OOOOOOc
126    *  OOOOOO
127    *   cccc
128    */
129
130   TestApplication application;
131   TestPlatformAbstraction& platform = application.GetPlatform();
132
133   tet_infoline("UtcDaliNinePatchImage - Stretch Regions");
134
135   const unsigned int ninePatchImageHeight = 8;
136   const unsigned int ninePatchImageWidth = 8;
137   Pixel::Format pixelFormat = Pixel::RGBA8888;
138   const Vector4 requiredStretchBorder( 3, 3, 3, 3);
139
140   tet_infoline("Create Bitmap");
141   platform.SetClosestImageSize(Vector2( ninePatchImageWidth, ninePatchImageHeight));
142   Integration::Bitmap* bitmap = CreateBitmap( ninePatchImageWidth, ninePatchImageHeight, 0xFF, pixelFormat );
143
144   tet_infoline("Clear border regions");
145   InitialiseRegionsToZeroAlpha( bitmap, ninePatchImageWidth, ninePatchImageHeight, pixelFormat );
146
147   tet_infoline("Add Stretch regions to Bitmap");
148   AddStretchRegionsToImage( bitmap, ninePatchImageWidth, ninePatchImageHeight, requiredStretchBorder, pixelFormat );
149
150   PixelBuffer* srcPixels = bitmap->GetBuffer();
151
152   tet_infoline("Getting resource");
153   Integration::ResourcePointer resourcePtr(bitmap);
154   platform.SetResourceLoaded( 0, Dali::Integration::ResourceBitmap, resourcePtr );
155
156   Image image = Image::New( "blah.#.png" );
157   DALI_TEST_CHECK( image );
158
159   tet_infoline("Assign image to ImageActor");
160   ImageActor imageActor = ImageActor::New( image );
161   DALI_TEST_CHECK( imageActor );
162   Stage::GetCurrent().Add( imageActor );
163
164   tet_infoline("Downcast Image to a nine-patch image\n");
165   NinePatchImage ninePatchImage = NinePatchImage::DownCast( image );
166   DALI_TEST_CHECK( ninePatchImage );
167
168   if ( ninePatchImage )
169   {
170     tet_infoline("Get Stretch regions from NinePatch");
171     Vector4 stretchBorders = ninePatchImage.GetStretchBorders();
172     tet_printf("stretchBorders left(%f) right(%f) top(%f) bottom(%f)\n", stretchBorders.x, stretchBorders.z, stretchBorders.y, stretchBorders.w );
173     DALI_TEST_CHECK( stretchBorders == requiredStretchBorder );
174   }
175   else
176   {
177     tet_infoline("Image not NinePatch");
178     test_return_value = TET_FAIL;
179   }
180
181   END_TEST;
182 }
183
184 int UtcDaliNinePatch02(void)
185 {
186   /* Child region x(2) y(2) width (4) height (4)
187    *
188    *    ss
189    *  OOOOOO
190    *  OOOOOOc
191    * sOOooOOc
192    * sOOooOOc
193    *  OOOOOOc
194    *  OOOOOO
195    *   cccc
196    */
197
198   TestApplication application;
199   TestPlatformAbstraction& platform = application.GetPlatform();
200
201   tet_infoline("UtcDaliNinePatchImage - Child Regions");
202
203   const unsigned int ninePatchImageHeight = 8;
204   const unsigned int ninePatchImageWidth = 8;
205   Pixel::Format pixelFormat = Pixel::RGBA8888;
206   const Vector4 requiredChildRegion( 2, 2, 2, 2 );
207   const Vector4 requiredStretchBorder( 3, 3, 3, 3);
208
209   tet_infoline("Create Bitmap");
210   platform.SetClosestImageSize(Vector2( ninePatchImageWidth, ninePatchImageHeight));
211   Integration::Bitmap* bitmap = CreateBitmap( ninePatchImageWidth, ninePatchImageHeight, 0xFF, pixelFormat );
212
213   tet_infoline("Clear border regions");
214   InitialiseRegionsToZeroAlpha( bitmap, ninePatchImageWidth, ninePatchImageHeight, pixelFormat );
215
216   tet_infoline("Add Stretch regions to Bitmap");
217   AddStretchRegionsToImage( bitmap, ninePatchImageWidth, ninePatchImageHeight, requiredStretchBorder, pixelFormat );
218
219   tet_infoline("Add Child regions to Bitmap");
220   AddChildRegionsToImage( bitmap, ninePatchImageWidth, ninePatchImageHeight, requiredChildRegion, pixelFormat );
221
222   tet_infoline("Getting resource");
223   Integration::ResourcePointer resourcePtr(bitmap);
224   platform.SetResourceLoaded( 0, Dali::Integration::ResourceBitmap, resourcePtr );
225   Image image = Image::New( "blah.#.png" );
226   DALI_TEST_CHECK( image );
227
228   tet_infoline("Assign image to ImageActor");
229   ImageActor imageActor = ImageActor::New( image );
230   DALI_TEST_CHECK( imageActor );
231   Stage::GetCurrent().Add( imageActor );
232
233   tet_infoline("Downcast Image to a nine-patch image\n");
234   NinePatchImage ninePatchImage = NinePatchImage::DownCast( image );
235   DALI_TEST_CHECK( ninePatchImage );
236
237   if ( ninePatchImage )
238   {
239     tet_infoline("Get Child regions from NinePatch");
240     Rect<int> childRectangle = ninePatchImage.GetChildRectangle();
241     tet_printf("childRectange x(%d) y(%d) width(%d) height(%d)\n", childRectangle.x, childRectangle.y, childRectangle.width, childRectangle.height );
242     Rect<int> childRegion(requiredChildRegion.x, requiredChildRegion.y, ninePatchImageWidth - requiredChildRegion.x - requiredChildRegion.z, ninePatchImageHeight - requiredChildRegion.y - requiredChildRegion.w );
243     DALI_TEST_CHECK( childRegion == childRectangle );
244   }
245   else
246   {
247     tet_infoline("Image not NinePatch");
248     test_return_value = TET_FAIL;
249   }
250
251   END_TEST;
252 }