[3.0] Remove/move experimental features
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-NinePatchImages.cpp
1 /*
2  * Copyright (c) 2016 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/devel-api/images/nine-patch-image.h>
23 #include <dali-test-suite-utils.h>
24
25 using namespace Dali;
26
27 namespace {
28
29 Integration::Bitmap* CreateBitmap( unsigned int imageWidth, unsigned int imageHeight, unsigned int initialColor, Pixel::Format pixelFormat )
30 {
31   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
32   Integration::PixelBuffer* pixbuffer = bitmap->GetPackedPixelsProfile()->ReserveBuffer( pixelFormat, imageWidth, imageHeight, imageWidth, imageHeight );
33   unsigned int bytesPerPixel = GetBytesPerPixel( pixelFormat );
34
35   memset( pixbuffer, initialColor, imageHeight * imageWidth * bytesPerPixel );
36
37   return bitmap;
38 }
39
40 void InitialiseRegionsToZeroAlpha( Integration::Bitmap* image, unsigned int imageWidth, unsigned int imageHeight, Pixel::Format pixelFormat )
41 {
42   PixelBuffer* pixbuffer = image->GetBuffer();
43   unsigned int bytesPerPixel = GetBytesPerPixel( pixelFormat );
44
45   for( unsigned int row = 0; row < imageWidth; ++row )
46   {
47     unsigned int pixelOffset = row * bytesPerPixel;
48     pixbuffer[ pixelOffset + 3 ] = 0x00;
49     pixelOffset += ( imageHeight - 1 ) * imageWidth * bytesPerPixel;
50     pixbuffer[ pixelOffset + 3 ] = 0x00;
51   }
52
53   for ( unsigned int column = 0; column < imageHeight; ++column )
54   {
55     unsigned int pixelOffset = column * imageWidth * bytesPerPixel;
56     pixbuffer[ pixelOffset + 3 ] = 0x00;
57     pixelOffset += ( imageWidth -1 ) * bytesPerPixel;
58     pixbuffer[ pixelOffset + 3 ] = 0x00;
59   }
60 }
61
62 void AddStretchRegionsToImage( Integration::Bitmap* image, unsigned int imageWidth, unsigned int imageHeight, const Vector4& requiredStretchBorder, Pixel::Format pixelFormat )
63 {
64   PixelBuffer* pixbuffer = image->GetBuffer();
65   unsigned int bytesPerPixel = GetBytesPerPixel( pixelFormat );
66
67   for( unsigned int column = requiredStretchBorder.x; column < imageWidth - requiredStretchBorder.z; ++column )
68   {
69     unsigned int pixelOffset = column * bytesPerPixel;
70     pixbuffer[ pixelOffset ] = 0x00;
71     pixbuffer[ pixelOffset + 1 ] = 0x00;
72     pixbuffer[ pixelOffset + 2 ] = 0x00;
73     pixbuffer[ pixelOffset + 3 ] = 0xFF;
74   }
75
76   for( unsigned int row = requiredStretchBorder.y; row < imageHeight - requiredStretchBorder.w; ++row )
77   {
78     unsigned int pixelOffset = row * imageWidth * bytesPerPixel;
79     pixbuffer[ pixelOffset ] = 0x00;
80     pixbuffer[ pixelOffset + 1 ] = 0x00;
81     pixbuffer[ pixelOffset + 2 ] = 0x00;
82     pixbuffer[ pixelOffset + 3 ] = 0xFF;
83   }
84 }
85
86 void AddChildRegionsToImage( Integration::Bitmap* image, unsigned int imageWidth, unsigned int imageHeight, const Vector4& requiredChildRegion, Pixel::Format pixelFormat )
87 {
88   PixelBuffer* pixbuffer = image->GetBuffer();
89   unsigned int bytesPerPixel = GetBytesPerPixel( pixelFormat );
90
91   Integration::Bitmap::PackedPixelsProfile* srcProfile = image->GetPackedPixelsProfile();
92   unsigned int bufferStride = srcProfile->GetBufferStride();
93
94   // Add bottom child region
95   for( unsigned int column = requiredChildRegion.x; column < imageWidth - requiredChildRegion.z; ++column )
96   {
97     unsigned int pixelOffset = column * bytesPerPixel;
98     pixelOffset += ( imageHeight - 1 ) * bufferStride;
99     pixbuffer[ pixelOffset ] = 0x00;
100     pixbuffer[ pixelOffset + 1 ] = 0x00;
101     pixbuffer[ pixelOffset + 2 ] = 0x00;
102     pixbuffer[ pixelOffset + 3 ] = 0xFF;
103   }
104
105   // Add right child region
106   for ( unsigned int row = requiredChildRegion.y; row < imageHeight - requiredChildRegion.w; ++row )
107   {
108     unsigned int pixelOffset = row * bufferStride + ( imageWidth - 1 ) * bytesPerPixel;
109     pixbuffer[ pixelOffset ] = 0x00;
110     pixbuffer[ pixelOffset + 1 ] = 0x00;
111     pixbuffer[ pixelOffset + 2 ] = 0x00;
112     pixbuffer[ pixelOffset + 3 ] = 0xFF;
113   }
114 }
115
116 NinePatchImage CustomizeNinePatch( TestApplication& application,
117                                 unsigned int ninePatchImageWidth,
118                                 unsigned int ninePatchImageHeight,
119                                 const Vector4& requiredStretchBorder,
120                                 bool addChildRegion = false,
121                                 Vector4 requiredChildRegion = Vector4::ZERO )
122 {
123   TestPlatformAbstraction& platform = application.GetPlatform();
124
125   Pixel::Format pixelFormat = Pixel::RGBA8888;
126
127   tet_infoline("Create Bitmap");
128   platform.SetClosestImageSize(Vector2( ninePatchImageWidth, ninePatchImageHeight));
129   Integration::Bitmap* bitmap = CreateBitmap( ninePatchImageWidth, ninePatchImageHeight, 0xFF, pixelFormat );
130
131   tet_infoline("Clear border regions");
132   InitialiseRegionsToZeroAlpha( bitmap, ninePatchImageWidth, ninePatchImageHeight, pixelFormat );
133
134   tet_infoline("Add Stretch regions to Bitmap");
135   AddStretchRegionsToImage( bitmap, ninePatchImageWidth, ninePatchImageHeight, requiredStretchBorder, pixelFormat );
136
137   if( addChildRegion )
138   {
139     tet_infoline("Add Child regions to Bitmap");
140     AddChildRegionsToImage( bitmap, ninePatchImageWidth, ninePatchImageHeight, requiredChildRegion, pixelFormat );
141   }
142
143   tet_infoline("Setting resource as it's loaded synchronously");
144   Integration::ResourcePointer resourcePtr(bitmap);
145   platform.SetSynchronouslyLoadedResource( resourcePtr );
146
147   Image image = ResourceImage::New( "blah.#.png" );
148
149   tet_infoline("Assign image to image rendering actor");
150   Actor actor = CreateRenderableActor( image );
151   Stage::GetCurrent().Add( actor );
152
153   tet_infoline("Downcast Image to a nine-patch image\n");
154   NinePatchImage ninePatchImage = NinePatchImage::DownCast( image );
155
156   return ninePatchImage;
157
158 }
159
160 } // namespace
161
162 int UtcDaliNinePatchImageNew(void)
163 {
164   TestApplication application;
165   tet_infoline("UtcDaliNinePatchImageNew - NinePatchImage::New(const std::string&)");
166
167   // invoke default handle constructor
168   NinePatchImage image;
169
170   DALI_TEST_CHECK( !image );
171
172   // initialise handle
173   image = NinePatchImage::New( "blah.#.png" );
174
175   DALI_TEST_CHECK( image );
176   END_TEST;
177 }
178
179 int UtcDaliNinePatchImageDowncast(void)
180 {
181   TestApplication application;
182   tet_infoline("UtcDaliNinePatchImageDowncast - NinePatchImage::DownCast(BaseHandle)");
183
184   NinePatchImage image = NinePatchImage::New( "blah.#.png" );
185
186   BaseHandle object( image );
187
188   NinePatchImage image2 = NinePatchImage::DownCast( object );
189   DALI_TEST_CHECK( image2 );
190
191   NinePatchImage image3 = DownCast< NinePatchImage >( object );
192   DALI_TEST_CHECK( image3 );
193
194   BaseHandle unInitializedObject;
195   NinePatchImage image4 = NinePatchImage::DownCast( unInitializedObject );
196   DALI_TEST_CHECK( !image4 );
197
198   NinePatchImage image5 = DownCast< NinePatchImage >( unInitializedObject );
199   DALI_TEST_CHECK( !image5 );
200
201   Image image6 = NinePatchImage::New( "blah.#.png" );
202   NinePatchImage image7 = NinePatchImage::DownCast( image6 );
203   DALI_TEST_CHECK( image7 );
204   END_TEST;
205 }
206
207 int UtcDaliNinePatchImageCopyConstructor(void)
208 {
209   TestApplication application;
210
211   tet_infoline("UtcDaliNinePatchImageCopyConstructor - NinePatchImage::NinePatchImage( const NinePatchImage& )");
212
213   NinePatchImage image1;
214   DALI_TEST_CHECK( !image1 );
215
216   image1 = NinePatchImage::New( "blah.#.png" );
217   NinePatchImage image2( image1 );
218
219   DALI_TEST_CHECK( image2 );
220   DALI_TEST_EQUALS( image1, image2, TEST_LOCATION );
221
222   END_TEST;
223 }
224
225 int UtcDaliNinePatchImageGetStrechBorders(void)
226 {
227   TestApplication application;
228   tet_infoline("UtcDaliNinePatchImageGetStrechBorders - NinePatchImage::GetStretchBorders()");
229
230   /* Stretch region left(2) top(2) right (2) bottom (2)
231     *    ss
232     *  OOOOOO
233     *  OOOOOOc
234     * sOOooOOc
235     * sOOooOOc
236     *  OOOOOOc
237     *  OOOOOO
238     *   cccc
239     */
240
241    const unsigned int ninePatchImageHeight = 18;
242    const unsigned int ninePatchImageWidth = 28;
243    const Vector4 requiredStretchBorder( 3, 4, 5, 6 );
244
245    NinePatchImage ninePatchImage = CustomizeNinePatch( application, ninePatchImageWidth, ninePatchImageHeight, requiredStretchBorder );
246    DALI_TEST_CHECK( ninePatchImage );
247
248    if( ninePatchImage )
249    {
250      tet_infoline("Get Stretch regions from NinePatch");
251
252      const NinePatchImage::StretchRanges& stretchPixelsX = ninePatchImage.GetStretchPixelsX();
253      const NinePatchImage::StretchRanges& stretchPixelsY = ninePatchImage.GetStretchPixelsY();
254
255      DALI_TEST_CHECK( stretchPixelsX.Size() == 1 );
256      DALI_TEST_CHECK( stretchPixelsY.Size() == 1 );
257
258      Vector4 stretchBorders;
259      //The NinePatchImage stretch pixels are in the cropped image space, inset by 1 to get it to uncropped image space
260      stretchBorders.x = stretchPixelsX[ 0 ].GetX() + 1;
261      stretchBorders.y = stretchPixelsY[ 0 ].GetX() + 1;
262      stretchBorders.z = ninePatchImageWidth - stretchPixelsX[ 0 ].GetY() - 1;
263      stretchBorders.w = ninePatchImageHeight - stretchPixelsY[ 0 ].GetY() - 1;
264
265      tet_printf("stretchBorders left(%f) right(%f) top(%f) bottom(%f)\n", stretchBorders.x, stretchBorders.z, stretchBorders.y, stretchBorders.w );
266      DALI_TEST_CHECK( stretchBorders == requiredStretchBorder );
267
268      Vector4 actualStretchBorders = ninePatchImage.GetStretchBorders();
269      DALI_TEST_EQUALS( actualStretchBorders, requiredStretchBorder, 0.001, TEST_LOCATION );
270    }
271    else
272    {
273      tet_infoline("Image not NinePatch");
274      test_return_value = TET_FAIL;
275    }
276
277   END_TEST;
278 }
279
280 int UtcDaliNinePatchImageGetChildRectangle(void)
281 {
282   TestApplication application;
283   tet_infoline("UtcDaliNinePatchImageGetChildRectangle - NinePatchImage::GetChildRectangle()");
284
285   /* Child region x(2) y(2) width (4) height (4)
286    *
287    *    ss
288    *  OOOOOO
289    *  OOOOOOc
290    * sOOooOOc
291    * sOOooOOc
292    *  OOOOOOc
293    *  OOOOOO
294    *   cccc
295    */
296
297   const unsigned int ninePatchImageHeight = 18;
298   const unsigned int ninePatchImageWidth = 28;
299   const Vector4 requiredChildRegion( 2, 2, 2, 2 );
300   const Vector4 requiredStretchBorder( 3, 4, 5, 6 );
301
302   NinePatchImage ninePatchImage = CustomizeNinePatch( application,ninePatchImageWidth, ninePatchImageHeight, requiredStretchBorder, true, requiredChildRegion );
303   DALI_TEST_CHECK( ninePatchImage );
304
305   if ( ninePatchImage )
306   {
307     tet_infoline("Get Child regions from NinePatch");
308     Rect< int > childRectangle = ninePatchImage.GetChildRectangle();
309     tet_printf("childRectange x(%d) y(%d) width(%d) height(%d)\n", childRectangle.x, childRectangle.y, childRectangle.width, childRectangle.height );
310     Rect< int > childRegion(requiredChildRegion.x, requiredChildRegion.y, ninePatchImageWidth - requiredChildRegion.x - requiredChildRegion.z, ninePatchImageHeight - requiredChildRegion.y - requiredChildRegion.w );
311     DALI_TEST_CHECK( childRegion == childRectangle );
312   }
313   else
314   {
315     tet_infoline("Image not NinePatch");
316     test_return_value = TET_FAIL;
317   }
318
319   END_TEST;
320 }
321
322 int UtcDaliNinePatchImageCreateCroppedBufferImage(void)
323 {
324   TestApplication application;
325   tet_infoline("UtcDaliNinePatchImageCreateCroppedBufferImage - NinePatchImage::CreateCroppedBufferImage()");
326
327   const unsigned int ninePatchImageHeight = 8;
328   const unsigned int ninePatchImageWidth = 8;
329   const Vector4 requiredStretchBorder( 1, 1, 1, 1 );
330
331   NinePatchImage ninePatchImage = CustomizeNinePatch( application, ninePatchImageWidth, ninePatchImageHeight, requiredStretchBorder  );
332   DALI_TEST_CHECK( ninePatchImage );
333
334   if ( ninePatchImage )
335   {
336     BufferImage newImage = ninePatchImage.CreateCroppedBufferImage();
337     DALI_TEST_CHECK( newImage );
338
339     DALI_TEST_EQUALS( newImage.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION );
340
341     DALI_TEST_EQUALS( newImage.GetBufferSize(), 144u/* 36*4 */, TEST_LOCATION );
342   }
343   else
344   {
345     tet_infoline("Image not NinePatch");
346     test_return_value = TET_FAIL;
347   }
348
349   END_TEST;
350 }
351
352 int UtcDaliNinePatchImageIsNinePatchUrl(void)
353 {
354   TestApplication application;
355   tet_infoline("UtcDaliNinePatchImageIsNinePatchUrl - NinePatchImage::IsNinePatchUrl(const std::string&)");
356
357   DALI_TEST_CHECK( NinePatchImage::IsNinePatchUrl( "test.9.jpg" ) );
358   DALI_TEST_CHECK( NinePatchImage::IsNinePatchUrl( "test.#.jpg" ) );
359   DALI_TEST_CHECK( !NinePatchImage::IsNinePatchUrl( "test.9" ) );
360   DALI_TEST_CHECK( !NinePatchImage::IsNinePatchUrl( "test.#" ) );
361   DALI_TEST_CHECK( !NinePatchImage::IsNinePatchUrl( "test" ) );
362
363   END_TEST;
364 }