42c4846b1223eb261367477b8c108f71605581a3
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-ImageActor.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 #include <dali/public-api/dali-core.h>
21 #include "dali-test-suite-utils/dali-test-suite-utils.h"
22
23 using namespace Dali;
24
25 static const char* TestImageFilename = "icon_wrt.png";
26
27 void image_actor_test_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void image_actor_test_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37
38 int UtcDaliImageActorConstructorVoid(void)
39 {
40   TestApplication application;
41   tet_infoline("Testing Dali::ImageActor::ImageActor()");
42
43   ImageActor actor;
44
45   DALI_TEST_CHECK(!actor);
46   END_TEST;
47 }
48
49 int UtcDaliImageActorDestructor(void)
50 {
51   TestApplication application;
52
53   ImageActor* actor = new ImageActor();
54   delete actor;
55
56   DALI_TEST_CHECK( true );
57   END_TEST;
58 }
59
60 int UtcDaliImageActorNew01(void)
61 {
62   TestApplication application;
63   tet_infoline("Positive test for Dali::ImageActor::New()");
64
65   Image image = Image::New(TestImageFilename);
66   ImageActor actor = ImageActor::New(image);
67   Stage::GetCurrent().Add(actor);
68
69   application.SendNotification();
70   application.Render();
71   application.Render();
72   application.SendNotification();
73
74   DALI_TEST_CHECK(application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc));
75
76   DALI_TEST_CHECK(actor);
77   END_TEST;
78 }
79
80 int UtcDaliImageActorNew02(void)
81 {
82   TestApplication application;
83   tet_infoline("Negative test for Dali::ImageActor::New()");
84
85   Image image = Image::New("hopefully-this-image-file-does-not-exist");
86   ImageActor actor = ImageActor::New(image);
87
88   DALI_TEST_CHECK(actor);
89   END_TEST;
90 }
91
92 int UtcDaliImageActorDownCast(void)
93 {
94   TestApplication application;
95   tet_infoline("Testing Dali::ImageActor::DownCast()");
96
97   Image image = Image::New("IncorrectImageName");
98   ImageActor actor1 = ImageActor::New(image);
99   Actor anActor = Actor::New();
100   anActor.Add(actor1);
101
102   Actor child = anActor.GetChildAt(0);
103   ImageActor imageActor = DownCast< ImageActor >(child);
104
105   DALI_TEST_CHECK(imageActor);
106   END_TEST;
107 }
108
109 int UtcDaliImageActorDownCast2(void)
110 {
111   TestApplication application;
112   tet_infoline("Testing Dali::ImageActor::DownCast()");
113
114   Actor actor1 = Actor::New();
115   Actor anActor = Actor::New();
116   anActor.Add(actor1);
117
118   Actor child = anActor.GetChildAt(0);
119   ImageActor imageActor = ImageActor::DownCast(child);
120   DALI_TEST_CHECK(!imageActor);
121
122   Actor unInitialzedActor;
123   imageActor = ImageActor::DownCast( unInitialzedActor );
124   DALI_TEST_CHECK(!imageActor);
125   END_TEST;
126 }
127
128 int UtcDaliImageActor9Patch(void)
129 {
130   TestApplication application;
131   tet_infoline("Positive test for Dali::ImageActor:: 9 patch api");
132
133   Image image = Image::New(TestImageFilename);
134   ImageActor actor = ImageActor::New(image);
135
136   actor.SetStyle(ImageActor::STYLE_NINE_PATCH);
137   Vector4 border(0.1,0.2,0.3,0.4);
138   actor.SetNinePatchBorder(border);
139
140   DALI_TEST_EQUALS( 0.1f, actor.GetNinePatchBorder().x, TEST_LOCATION );
141   DALI_TEST_EQUALS( 0.2f, actor.GetNinePatchBorder().y, TEST_LOCATION );
142   DALI_TEST_EQUALS( 0.3f, actor.GetNinePatchBorder().z, TEST_LOCATION );
143   DALI_TEST_EQUALS( 0.4f, actor.GetNinePatchBorder().w, TEST_LOCATION );
144   END_TEST;
145 }
146
147 int UtcDaliImageActorPixelArea(void)
148 {
149   TestApplication application;
150   tet_infoline("Positive test for Dali::ImageActor::UtcDaliImageActorPixelArea");
151
152   BitmapImage img = BitmapImage::New( 10, 10 );
153   ImageActor actor = ImageActor::New( img );
154
155   DALI_TEST_CHECK( actor.IsPixelAreaSet() == false );
156
157   ImageActor::PixelArea area( 1, 2, 3, 4 );
158   actor.SetPixelArea( area );
159
160   DALI_TEST_CHECK( actor.IsPixelAreaSet() == true );
161
162   DALI_TEST_EQUALS( 1, actor.GetPixelArea().x, TEST_LOCATION );
163   DALI_TEST_EQUALS( 2, actor.GetPixelArea().y, TEST_LOCATION );
164   DALI_TEST_EQUALS( 3, actor.GetPixelArea().width, TEST_LOCATION );
165   DALI_TEST_EQUALS( 4, actor.GetPixelArea().height, TEST_LOCATION );
166
167   ImageActor actor2 = ImageActor::New( img, ImageActor::PixelArea( 5, 6, 7, 8 ) );
168   DALI_TEST_CHECK( actor2.IsPixelAreaSet() == true );
169
170   DALI_TEST_EQUALS( 5, actor2.GetPixelArea().x, TEST_LOCATION );
171   DALI_TEST_EQUALS( 6, actor2.GetPixelArea().y, TEST_LOCATION );
172   DALI_TEST_EQUALS( 7, actor2.GetPixelArea().width, TEST_LOCATION );
173   DALI_TEST_EQUALS( 8, actor2.GetPixelArea().height, TEST_LOCATION );
174   END_TEST;
175 }
176
177 int UtcDaliImageActorGetCurrentSize01(void)
178 {
179   TestApplication application;
180   tet_infoline("Positive test for Dali::ImageActor::GetCurrentSize");
181
182   Vector2 initialImageSize(100, 50);
183   BitmapImage image = BitmapImage::New( initialImageSize.width, initialImageSize.height );
184   ImageActor actor = ImageActor::New( image );
185   Stage::GetCurrent().Add(actor);
186
187   application.SendNotification();
188   application.Render();
189
190   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), initialImageSize, TEST_LOCATION );
191
192   Vector2 size(200.0f, 200.0f);
193   actor.SetSize(size);
194
195   // flush the queue and render once
196   application.SendNotification();
197   application.Render();
198   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), size, TEST_LOCATION );
199
200   size.x = 200.0f;
201   size.y = 200.0f;
202   actor.SetSize(size);
203   application.Render(8);
204
205   // Test when a pixel area is set
206   ImageActor::PixelArea area(0, 0, 10, 10);
207   actor.SetPixelArea(area);
208   application.Render(9);
209   // natural size is not used as setsize is called
210   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), size, TEST_LOCATION );
211
212   actor.SetToNaturalSize();
213   application.SendNotification();
214   application.Render(9);
215   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), Vector2( area.width, area.height ), TEST_LOCATION );
216
217   END_TEST;
218 }
219
220
221 int UtcDaliImageActorGetCurrentSize02(void)
222 {
223   TestApplication application;
224   tet_infoline("Positive test for Dali::ImageActor::GetCurrentSize - Test that using an image resource sets the actor size with it's natural size immediately rather than on load");
225
226   Vector2 initialImageSize(100, 50);
227
228   application.GetPlatform().SetClosestImageSize(initialImageSize);
229
230   Image image = Image::New("image.jpg");
231   ImageActor actor = ImageActor::New( image );
232   Stage::GetCurrent().Add(actor);
233
234   application.SendNotification(); // Flush update messages
235   application.Render();           // Process resource request
236   application.SendNotification(); // Flush update messages
237   application.Render();           // Process resource request
238
239   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), initialImageSize, TEST_LOCATION );
240
241   // Now complete the image load
242   Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
243   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, false );
244   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888,  initialImageSize.width,initialImageSize.height, initialImageSize.width,initialImageSize.height );
245
246   Integration::ResourcePointer resourcePtr(bitmap); // reference it
247   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
248   application.Render();           // Process LoadComplete
249   application.SendNotification(); // Process event messages
250   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
251   application.GetPlatform().ClearReadyResources(); //
252
253   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), initialImageSize, TEST_LOCATION );
254
255   Vector2 size(200.0f, 200.0f);
256   actor.SetSize(size);
257
258   // flush the queue and render once
259   application.SendNotification();
260   application.Render();
261   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), size, TEST_LOCATION );
262
263   actor.SetToNaturalSize();
264   application.SendNotification();
265   application.Render();
266   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), initialImageSize, TEST_LOCATION );
267   END_TEST;
268 }
269
270
271 int UtcDaliImageActorGetCurrentSize03(void)
272 {
273   TestApplication application;
274   tet_infoline("Positive test for Dali::ImageActor::GetCurrentSize - Test that using an image resource with a requested size sets the actor size with it's nearest size immediately rather than on load");
275
276   const Vector2 closestImageSize( 80, 45);
277   application.GetPlatform().SetClosestImageSize(closestImageSize);
278
279   ImageAttributes attrs;
280   const Vector2 requestedSize( 40, 30 );
281   attrs.SetSize( requestedSize.width, requestedSize.height );
282   Image image = Image::New("image.jpg", attrs);
283   ImageActor actor = ImageActor::New( image );
284   Stage::GetCurrent().Add(actor);
285
286   application.SendNotification(); // Flush update messages
287   application.Render();           // Process resource request
288   application.SendNotification(); // Flush update messages
289   application.Render();           // Process resource request
290
291   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), requestedSize, TEST_LOCATION );
292
293   // Now complete the image load
294   Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
295   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, false );
296   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888,  closestImageSize.width, closestImageSize.height, closestImageSize.width, closestImageSize.height );
297
298   Integration::ResourcePointer resourcePtr(bitmap); // reference it
299   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
300   application.Render();           // Process LoadComplete
301   application.SendNotification(); // Process event messages
302   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
303   application.GetPlatform().ClearReadyResources(); //
304
305   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), requestedSize, TEST_LOCATION );
306
307   // Test that setting a size on the actor can be 'undone' with SetNaturalSize()
308   Vector2 size(200.0f, 200.0f);
309   actor.SetSize(size);
310
311   // flush the queue and render once
312   application.SendNotification();
313   application.Render();
314   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), size, TEST_LOCATION );
315
316   actor.SetToNaturalSize();
317   application.SendNotification();
318   application.Render();
319   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), requestedSize, TEST_LOCATION );
320   END_TEST;
321 }
322
323
324 int UtcDaliImageActorGetCurrentSize04(void)
325 {
326   TestApplication application;
327   tet_infoline("Positive test for Dali::ImageActor::GetCurrentSize - check a new image doesn't change a set actor size");
328
329   const Vector2 closestImageSize( 80, 45);
330   application.GetPlatform().SetClosestImageSize(closestImageSize);
331
332   const Vector2 requestedSize( 40, 30 );
333   ImageAttributes attrs;
334   attrs.SetSize( requestedSize.width, requestedSize.height );
335   Image image = Image::New("image.jpg", attrs);
336   ImageActor actor = ImageActor::New( image );
337   Stage::GetCurrent().Add(actor);
338
339   application.SendNotification(); // Flush update messages
340   application.Render();           // Process resource request
341
342   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), requestedSize, TEST_LOCATION );
343
344   // Now complete the image load
345   Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
346   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, false );
347   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888,  closestImageSize.width, closestImageSize.height, closestImageSize.width, closestImageSize.height );
348
349   Integration::ResourcePointer resourcePtr(bitmap); // reference it
350   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
351   application.Render();           // Process LoadComplete
352   application.SendNotification(); // Process event messages
353   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
354   application.GetPlatform().ClearReadyResources(); //
355
356   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), requestedSize, TEST_LOCATION );
357
358   Vector2 size(200.0f, 200.0f);
359   actor.SetSize(size);
360
361   // flush the queue and render once
362   application.SendNotification();
363   application.Render();
364   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), size, TEST_LOCATION );
365
366   // Load a different image
367
368   Vector2 image2ClosestSize = Vector2(240, 150); // The actual size image loader will return for the request below
369   application.GetPlatform().SetClosestImageSize(image2ClosestSize);
370
371   const Vector2 request2Size( 100, 100 );
372   attrs.SetSize( request2Size.width, request2Size.height );
373   Image image2 = Image::New("image2.jpg", attrs);
374   actor.SetImage(image2);
375
376   application.SendNotification(); // Flush update messages
377   application.Render();           // Process resource request
378   application.SendNotification(); // Flush update messages
379   application.Render();           // Process resource request
380
381   // Ensure the actor size is kept
382   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), size, TEST_LOCATION );
383
384   // Now complete the image load
385   req = application.GetPlatform().GetRequest();
386   Integration::Bitmap* bitmap2 = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, false );
387   bitmap2->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888,  image2ClosestSize.width, image2ClosestSize.height, image2ClosestSize.width, image2ClosestSize.height );
388
389   Integration::ResourcePointer resourcePtr2(bitmap2); // reference it
390   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr2);
391   application.Render();           // Process LoadComplete
392   application.SendNotification(); // Process event messages
393   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
394   application.GetPlatform().ClearReadyResources(); //
395
396   // Ensure the actor size is kept
397   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), size, TEST_LOCATION );
398
399   actor.SetToNaturalSize();
400   application.SendNotification();
401   application.Render();
402   // Ensure the actor size gets the new image's natural size
403   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), request2Size, TEST_LOCATION );
404   END_TEST;
405 }
406
407
408 int UtcDaliImageActorGetCurrentSize05(void)
409 {
410   TestApplication application;
411   tet_infoline("Positive test for Dali::ImageActor::GetCurrentSize - check a new image doens't change actor size until load complete");
412
413   Vector2 closestImageSize( 80, 45);
414   application.GetPlatform().SetClosestImageSize(closestImageSize);
415
416   ImageAttributes attrs;
417   const Vector2 requestedSize( 40, 30 );
418   attrs.SetSize( requestedSize.width, requestedSize.height );
419   Image image = Image::New("image.jpg", attrs);
420   ImageActor actor = ImageActor::New( image );
421   Stage::GetCurrent().Add(actor);
422
423   application.SendNotification(); // Flush update messages
424   application.Render();           // Process resource request
425
426   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), requestedSize, TEST_LOCATION );
427
428   // Now complete the image load
429   Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
430   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, false );
431   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888,  closestImageSize.width, closestImageSize.height, closestImageSize.width, closestImageSize.height );
432
433   Integration::ResourcePointer resourcePtr(bitmap); // reference it
434   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
435   application.Render();           // Process LoadComplete
436   application.SendNotification(); // Process event messages
437   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
438   application.GetPlatform().ClearReadyResources(); //
439
440   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), requestedSize, TEST_LOCATION );
441
442   // Load a different image
443
444   Vector2 image2ClosestSize = Vector2(240, 150);
445   application.GetPlatform().SetClosestImageSize(image2ClosestSize);
446
447   const Vector2 requestedSize2( 100, 100 );
448   attrs.SetSize( requestedSize2.width, requestedSize2.height );
449   Image image2 = Image::New("image2.jpg", attrs);
450   actor.SetImage(image2);
451
452   application.SendNotification(); // Flush update messages
453   application.Render();           // Process resource request
454   application.SendNotification(); // Flush update messages
455   application.Render();           // Process resource request
456
457   // Ensure the actor size is kept
458   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), requestedSize2, TEST_LOCATION );
459
460   // Now complete the image load
461   req = application.GetPlatform().GetRequest();
462   Integration::Bitmap* bitmap2 = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, false );
463   bitmap2->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888,  image2ClosestSize.width, image2ClosestSize.height, image2ClosestSize.width, image2ClosestSize.height );
464
465   Integration::ResourcePointer resourcePtr2(bitmap2); // reference it
466   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr2);
467   application.Render();           // Process LoadComplete
468   application.SendNotification(); // Process event messages
469   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
470   application.GetPlatform().ClearReadyResources(); //
471
472   application.SendNotification(); // Process event messages
473   application.Render();           // Process LoadComplete
474
475   // Ensure the actor size gets the new image's natural size
476   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), requestedSize2, TEST_LOCATION );
477   END_TEST;
478 }
479
480 int UtcDaliImageActorNaturalPixelAreaSize01(void)
481 {
482   TestApplication application;
483   tet_infoline("Positive test for Dali::ImageActor::GetCurrentSize - check a new image doens't change actor size until load complete");
484
485 //If an image is loaded without setting size, then the actor gets the natural size of the image
486 //Setting the pixel area will change the actor size to match the pixel area
487 //Setting the actor size will not change pixel area, and will cause the partial image to stretch
488 //to the new size.
489 //Clearing the pixel area will not change actor size, and the actor will show the whole image.
490
491
492   Vector2 closestImageSize( 80, 45);
493   application.GetPlatform().SetClosestImageSize(closestImageSize);
494
495   ImageAttributes attrs;
496   const Vector2 requestedSize( 40, 30 );
497   attrs.SetSize( requestedSize.width, requestedSize.height );
498   Image image = Image::New("image.jpg", attrs);
499   ImageActor actor = ImageActor::New( image );
500   Stage::GetCurrent().Add(actor);
501
502   application.SendNotification(); // Flush update messages
503   application.Render();           // Process resource request
504
505   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), requestedSize, TEST_LOCATION );
506
507   // Now complete the image load
508   Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
509   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, false );
510   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888,  closestImageSize.width, closestImageSize.height, closestImageSize.width, closestImageSize.height );
511
512   Integration::ResourcePointer resourcePtr(bitmap); // reference it
513   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
514   application.Render();           // Process LoadComplete
515   application.SendNotification(); // Process event messages
516   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
517   application.GetPlatform().ClearReadyResources(); //
518
519   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), requestedSize, TEST_LOCATION );
520
521   // Set a pixel area on a naturally sized actor - expect the actor to take the
522   // pixel area as size
523   actor.SetPixelArea(ImageActor::PixelArea(0, 0, 30, 30));
524   application.SendNotification(); // Process event messages
525   application.Render();           // Process LoadComplete
526   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), Vector2(30, 30), TEST_LOCATION );
527
528   // Set a size. Expect the partial image to stretch to fill the new size
529   actor.SetSize(100, 100);
530   application.SendNotification(); // Process event messages
531   application.Render();           // Process LoadComplete
532   application.Render();           // Process LoadComplete
533   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), Vector2(100, 100), TEST_LOCATION );
534
535   // Clear the pixel area. Expect the whole image to be shown, filling the set size.
536   actor.ClearPixelArea();
537   application.SendNotification(); // Process event messages
538   application.Render();           // Process LoadComplete
539   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), Vector2(100, 100), TEST_LOCATION );
540   END_TEST;
541 }
542
543 int UtcDaliImageActorNaturalPixelAreaSize02(void)
544 {
545   TestApplication application;
546   tet_infoline("Positive test for Dali::ImageActor::GetCurrentSize - check a new image doens't change actor size until load complete");
547
548 //If an image is loaded without setting size, then the actor gets the natural size of the image
549 //Setting the pixel area will change the actor size to match the pixel area
550 //Setting the actor size will not change pixel area, and will cause the partial image to stretch
551 //to the new size.
552 //Clearing the pixel area will not change actor size, and the actor will show the whole image.
553
554
555   Vector2 closestImageSize( 80, 45);
556   application.GetPlatform().SetClosestImageSize(closestImageSize);
557
558   ImageAttributes attrs;
559   const Vector2 requestedSize( 40, 30 );
560   attrs.SetSize( requestedSize.width, requestedSize.height );
561   Image image = Image::New("image.jpg", attrs);
562   ImageActor actor = ImageActor::New( image );
563   Stage::GetCurrent().Add(actor);
564
565   application.SendNotification(); // Flush update messages
566   application.Render();           // Process resource request
567
568   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), requestedSize, TEST_LOCATION );
569
570   // Now complete the image load
571   Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
572   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, false );
573   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888,  closestImageSize.width, closestImageSize.height, closestImageSize.width, closestImageSize.height );
574
575   Integration::ResourcePointer resourcePtr(bitmap); // reference it
576   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
577   application.Render();           // Process LoadComplete
578   application.SendNotification(); // Process event messages
579   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
580   application.GetPlatform().ClearReadyResources(); //
581
582   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), requestedSize, TEST_LOCATION );
583
584   // Set a pixel area on a naturally sized actor - expect the actor to take the
585   // pixel area as size
586   actor.SetPixelArea(ImageActor::PixelArea(0, 0, 30, 30));
587   application.SendNotification(); // Process event messages
588   application.Render();           // Process LoadComplete
589   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), Vector2(30, 30), TEST_LOCATION );
590
591   // Clear the pixel area. Expect the whole image to be shown, changing actor size
592   actor.ClearPixelArea();
593   application.SendNotification(); // Process event messages
594   application.Render();           // Process LoadComplete
595   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), requestedSize, TEST_LOCATION );
596
597   // Set a size. Expect the partial image to stretch to fill the new size
598   actor.SetSize(100, 100);
599   application.SendNotification(); // Process event messages
600   application.Render();           // Process LoadComplete
601   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), Vector2(100, 100), TEST_LOCATION );
602
603   // Set a pixel area, don't expect size to change
604   actor.SetPixelArea(ImageActor::PixelArea(0, 0, 40, 40));
605   application.SendNotification(); // Process event messages
606   application.Render();           // Process LoadComplete
607   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), Vector2(100, 100), TEST_LOCATION );
608
609   // Use natural size - expect actor to change to pixel area
610   actor.SetToNaturalSize();
611   application.SendNotification(); // Process event messages
612   application.Render();           // Process LoadComplete
613   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), Vector2(40, 40), TEST_LOCATION );
614
615   // Clearing pixel area should change actor size to image size
616   actor.ClearPixelArea();
617   application.SendNotification(); // Process event messages
618   application.Render();           // Process LoadComplete
619   DALI_TEST_EQUALS( Vector2(actor.GetCurrentSize()), requestedSize, TEST_LOCATION );
620   END_TEST;
621 }
622
623
624
625 int UtcDaliImageActorDefaultProperties(void)
626 {
627   TestApplication application;
628   tet_infoline("Testing Dali::ImageActor DefaultProperties");
629
630   BitmapImage img = BitmapImage::New( 10, 10 );
631   ImageActor actor = ImageActor::New( img );
632
633   std::vector<Property::Index> indices;
634   indices.push_back(ImageActor::PIXEL_AREA      );
635   indices.push_back(ImageActor::STYLE           );
636   indices.push_back(ImageActor::BORDER          );
637   indices.push_back(ImageActor::IMAGE           );
638
639   DALI_TEST_CHECK(actor.GetPropertyCount() == ( Actor::New().GetPropertyCount() + indices.size() ) );
640
641   for(std::vector<Property::Index>::iterator iter = indices.begin(); iter != indices.end(); ++iter)
642   {
643     DALI_TEST_CHECK( *iter == actor.GetPropertyIndex(actor.GetPropertyName(*iter)) );
644     DALI_TEST_CHECK( actor.IsPropertyWritable(*iter) );
645     DALI_TEST_CHECK( !actor.IsPropertyAnimatable(*iter) );
646     DALI_TEST_CHECK( actor.GetPropertyType(*iter) == actor.GetPropertyType(*iter) );  // just checking call succeeds
647   }
648
649   // set/get one of them
650   actor.SetPixelArea(ImageActor::PixelArea( 0, 0, 0, 0 ));
651
652   ImageActor::PixelArea area( 1, 2, 3, 4 );
653   actor.SetProperty(ImageActor::PIXEL_AREA, Property::Value(Rect<int>(area)));
654
655   DALI_TEST_CHECK(Property::RECTANGLE == actor.GetPropertyType(ImageActor::PIXEL_AREA));
656
657   Property::Value v = actor.GetProperty(ImageActor::PIXEL_AREA);
658
659   DALI_TEST_CHECK(v.Get<Rect<int> >() == area);
660
661   END_TEST;
662 }
663
664 int UtcDaliImageActorUseImageAlpha01(void)
665 {
666   TestApplication application;
667
668   tet_infoline("Testing Dali::RenderableActor::SetUseImageAlpha()");
669
670   BitmapImage image = BitmapImage::New( 100, 50 );
671   ImageActor actor = ImageActor::New( image );
672   actor.SetBlendMode( BlendingMode::ON );
673   actor.SetSize(100, 50);
674   application.GetGlAbstraction().EnableCullFaceCallTrace(true); // For Enable(GL_BLEND)
675   Stage::GetCurrent().Add(actor);
676
677   application.SendNotification();
678   application.Render();
679
680   const TraceCallStack& callTrace = application.GetGlAbstraction().GetCullFaceTrace();
681   DALI_TEST_EQUALS( BlendEnabled( callTrace), true, TEST_LOCATION );
682   DALI_TEST_EQUALS( BlendDisabled( callTrace ), false, TEST_LOCATION );
683   END_TEST;
684 }
685
686 int UtcDaliImageActorUseImageAlpha02(void)
687 {
688   TestApplication application;
689
690   tet_infoline("Testing Dali::RenderableActor::SetUseImageAlpha()");
691
692   BitmapImage image = BitmapImage::New( 100, 50 );
693   ImageActor actor = ImageActor::New( image );
694   actor.SetBlendMode( BlendingMode::OFF );
695   actor.SetSize(100, 50);
696   application.GetGlAbstraction().EnableCullFaceCallTrace(true); // For Enable(GL_BLEND)
697   Stage::GetCurrent().Add(actor);
698
699   application.SendNotification();
700   application.Render();
701
702   const TraceCallStack& callTrace = application.GetGlAbstraction().GetCullFaceTrace();
703   DALI_TEST_EQUALS( BlendDisabled( callTrace ), false, TEST_LOCATION );
704   DALI_TEST_EQUALS( BlendEnabled( callTrace), false, TEST_LOCATION );
705   END_TEST;
706 }
707
708 int UtcDaliImageActorUseImageAlpha03(void)
709 {
710   TestApplication application;
711
712   tet_infoline("Testing Dali::RenderableActor::SetUseImageAlpha()");
713
714   BitmapImage image = BitmapImage::New( 100, 50 );
715   ImageActor actor = ImageActor::New( image );
716   actor.SetBlendMode( BlendingMode::AUTO );
717   actor.SetColor(Vector4(1.0, 1.0, 1.0, 0.5));
718   actor.SetSize(100, 50);
719   application.GetGlAbstraction().EnableCullFaceCallTrace(true); // For Enable(GL_BLEND)
720   Stage::GetCurrent().Add(actor);
721
722   application.SendNotification();
723   application.Render();
724
725   const TraceCallStack& callTrace = application.GetGlAbstraction().GetCullFaceTrace();
726   DALI_TEST_EQUALS( BlendDisabled( callTrace ), false, TEST_LOCATION );
727   DALI_TEST_EQUALS( BlendEnabled( callTrace), true, TEST_LOCATION );
728   END_TEST;
729 }
730
731 int UtcDaliImageActorUseImageAlpha04(void)
732 {
733   TestApplication application;
734
735   tet_infoline("Testing Dali::RenderableActor::SetUseImageAlpha()");
736
737   FrameBufferImage image = FrameBufferImage::New( 100, 50, Pixel::RGBA8888 );
738   RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
739   RenderTask task = taskList.GetTask( 0u );
740   task.SetTargetFrameBuffer( image ); // To ensure frame buffer is connected
741   application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE );
742   application.SendNotification();
743   application.Render(0);
744
745   ImageActor actor = ImageActor::New( image );
746   application.SendNotification();
747   application.Render(0);
748
749   actor.SetBlendMode( BlendingMode::ON );
750   actor.SetColor(Vector4(1.0, 1.0, 1.0, 1.0));
751   actor.SetSize(100, 50);
752   application.GetGlAbstraction().EnableCullFaceCallTrace(true); // For Enable(GL_BLEND)
753   Stage::GetCurrent().Add(actor);
754
755   application.SendNotification();
756   application.Render();
757
758   const TraceCallStack& callTrace = application.GetGlAbstraction().GetCullFaceTrace();
759   DALI_TEST_EQUALS( BlendDisabled( callTrace ), false, TEST_LOCATION );
760   DALI_TEST_EQUALS( BlendEnabled( callTrace), true, TEST_LOCATION );
761   END_TEST;
762 }
763
764 int UtcDaliImageActorUseImageAlpha05(void)
765 {
766   TestApplication application;
767
768   tet_infoline("Testing Dali::RenderableActor::SetUseImageAlpha()");
769
770   BitmapImage image = BitmapImage::New( 100, 50, Pixel::RGB888 );
771   ImageActor actor = ImageActor::New( image );
772   actor.SetBlendMode( BlendingMode::AUTO );
773   actor.SetColor(Vector4(1.0, 1.0, 1.0, 1.0));
774   actor.SetSize(100, 50);
775   application.GetGlAbstraction().EnableCullFaceCallTrace(true); // For Enable(GL_BLEND)
776   Stage::GetCurrent().Add(actor);
777
778   application.SendNotification();
779   application.Render();
780
781   const TraceCallStack& callTrace = application.GetGlAbstraction().GetCullFaceTrace();
782   DALI_TEST_EQUALS( BlendDisabled( callTrace ), false, TEST_LOCATION );
783   DALI_TEST_EQUALS( BlendEnabled( callTrace), false, TEST_LOCATION );
784   END_TEST;
785 }
786
787 int UtcDaliImageActorClearPixelArea(void)
788 {
789   TestApplication application;
790
791   BitmapImage img = BitmapImage::New( 10, 10 );
792   ImageActor actor = ImageActor::New( img );
793
794   DALI_TEST_CHECK( actor.IsPixelAreaSet() == false );
795
796   ImageActor::PixelArea area( 1, 2, 3, 4 );
797   actor.SetPixelArea( area );
798
799   DALI_TEST_CHECK( actor.IsPixelAreaSet() == true );
800
801   actor.ClearPixelArea();
802
803   DALI_TEST_CHECK( actor.IsPixelAreaSet() == false );
804   END_TEST;
805 }
806
807 int UtcDaliImageGetStyle(void)
808 {
809   TestApplication application;
810
811   Image image = Image::New(TestImageFilename);
812   ImageActor actor = ImageActor::New(image);
813
814   actor.SetStyle(ImageActor::STYLE_NINE_PATCH);
815
816   // flush the queue and render once
817   application.SendNotification();
818   application.Render();
819
820   DALI_TEST_EQUALS( ImageActor::STYLE_NINE_PATCH, actor.GetStyle(), TEST_LOCATION );
821   END_TEST;
822 }
823
824 int UtcDaliImageSetNinePatchBorder(void)
825 {
826   TestApplication application;
827
828   Image image = Image::New(TestImageFilename);
829   ImageActor actor = ImageActor::New(image);
830
831   actor.SetStyle(ImageActor::STYLE_NINE_PATCH);
832   actor.SetNinePatchBorder(Vector4( 1.0f, 2.0f, 3.0f, 4.0f));
833
834   DALI_TEST_EQUALS( 1.0f, actor.GetNinePatchBorder().x, TEST_LOCATION );
835   DALI_TEST_EQUALS( 2.0f, actor.GetNinePatchBorder().y, TEST_LOCATION );
836   DALI_TEST_EQUALS( 3.0f, actor.GetNinePatchBorder().z, TEST_LOCATION );
837   DALI_TEST_EQUALS( 4.0f, actor.GetNinePatchBorder().w, TEST_LOCATION );
838   END_TEST;
839 }
840
841 int UtcDaliImageActorNewNull(void)
842 {
843   TestApplication application;
844
845   ImageActor actor = ImageActor::New(Image());
846
847   DALI_TEST_CHECK(actor);
848   END_TEST;
849 }
850
851 int UtcDaliImageActorNewNullWithArea(void)
852 {
853   TestApplication application;
854
855   ImageActor::PixelArea area( 1, 2, 3, 4 );
856
857   ImageActor actor = ImageActor::New(Image(), area);
858
859   DALI_TEST_CHECK(actor);
860   END_TEST;
861 }
862
863 int UtcDaliImageActorSetImage(void)
864 {
865   TestApplication application;
866
867   ImageActor actor = ImageActor::New(Image());
868
869   DALI_TEST_CHECK(actor);
870
871   actor.SetImage( Image() );
872
873   DALI_TEST_CHECK(!actor.GetImage());
874   END_TEST;
875 }
876
877 int UtcDaliImageActorPropertyIndices(void)
878 {
879   TestApplication application;
880   Actor basicActor = Actor::New();
881   ImageActor imageActor = ImageActor::New();
882
883   Property::IndexContainer indices;
884   imageActor.GetPropertyIndices( indices );
885   DALI_TEST_CHECK( indices.size() > basicActor.GetPropertyCount() );
886   DALI_TEST_EQUALS( indices.size(), imageActor.GetPropertyCount(), TEST_LOCATION );
887   END_TEST;
888 }
889
890 int UtcDaliImageActorImageProperty(void)
891 {
892   TestApplication application;
893   Image image = Image::New( "MY_PATH" );
894   ImageActor imageActor = ImageActor::New( image );
895
896   Stage::GetCurrent().Add( imageActor );
897   application.SendNotification();
898   application.Render();
899
900   Property::Value imageMap = imageActor.GetProperty( ImageActor::IMAGE );
901   DALI_TEST_CHECK( imageMap.HasKey( "filename" ) );
902   DALI_TEST_EQUALS( imageMap.GetValue( "filename" ).Get< std::string >(), "MY_PATH", TEST_LOCATION );
903   END_TEST;
904 }
905
906 int UtcDaliImageActorNinePatch01(void)
907 {
908   TestApplication application;
909   TestPlatformAbstraction& platform = application.GetPlatform();
910   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
911   TraceCallStack& textureTrace = glAbstraction.GetTextureTrace();
912   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
913
914   tet_infoline("Test the successful loading of a nine-patch image\n");
915
916   platform.SetClosestImageSize(Vector2(4, 4));
917   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, false );
918   Integration::PixelBuffer* pixels = bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888,  4,4,4,4 );
919   memset( pixels, 0, 64 );
920
921   Integration::ResourcePointer resourcePtr(bitmap); // reference it
922   platform.SetResourceLoaded( 0, Dali::Integration::ResourceBitmap, resourcePtr );
923
924   Image ninePatchImage = Image::New( "blah.#.png" );
925   DALI_TEST_CHECK( ninePatchImage );
926
927   ImageActor imageActor = ImageActor::New( ninePatchImage );
928   DALI_TEST_CHECK( imageActor );
929   Stage::GetCurrent().Add( imageActor );
930
931   drawTrace.Reset();
932   textureTrace.Reset();
933   drawTrace.Enable(true);
934   textureTrace.Enable(true);
935   glAbstraction.ClearBoundTextures();
936   std::vector<GLuint> ids;
937   ids.push_back( 23 );
938   glAbstraction.SetNextTextureIds( ids );
939
940   application.SendNotification();
941   application.Render();
942
943   DALI_TEST_CHECK( drawTrace.FindMethod( "DrawArrays" ) );
944   typedef std::vector<GLuint> TexVec;
945   const TexVec& textures = glAbstraction.GetBoundTextures(GL_TEXTURE0);
946   DALI_TEST_CHECK( textures.size() > 0 );
947   if( textures.size() > 0 )
948   {
949     DALI_TEST_EQUALS( textures[0], 23u, TEST_LOCATION );
950   }
951
952   END_TEST;
953 }
954
955
956 int UtcDaliImageActorNinePatch02(void)
957 {
958   TestApplication application;
959   TestPlatformAbstraction& platform = application.GetPlatform();
960   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
961   TraceCallStack& textureTrace = glAbstraction.GetTextureTrace();
962   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
963
964   tet_infoline("Test the failed loading of a nine-patch image\n");
965
966   platform.SetClosestImageSize(Vector2(0, 0));
967   Integration::ResourcePointer resourcePtr;
968   platform.SetResourceLoaded( 0, Dali::Integration::ResourceBitmap, resourcePtr );
969
970   Image ninePatchImage = Image::New( "blah.#.png" );
971   DALI_TEST_CHECK( ninePatchImage );
972
973   ImageActor imageActor = ImageActor::New( ninePatchImage );
974   DALI_TEST_CHECK( imageActor );
975   Stage::GetCurrent().Add( imageActor );
976
977   drawTrace.Reset();
978   textureTrace.Reset();
979   drawTrace.Enable(true);
980   textureTrace.Enable(true);
981   glAbstraction.ClearBoundTextures();
982   std::vector<GLuint> ids;
983   ids.push_back( 23 );
984   glAbstraction.SetNextTextureIds( ids );
985
986   application.SendNotification();
987   application.Render();
988
989   // Check that nothing was drawn.
990   DALI_TEST_CHECK( ! drawTrace.FindMethod( "DrawArrays" ) );
991   typedef std::vector<GLuint> TexVec;
992   const TexVec& textures = glAbstraction.GetBoundTextures(GL_TEXTURE0);
993   DALI_TEST_CHECK( textures.size() == 0u );
994
995   END_TEST;
996 }
997
998
999 int UtcDaliImageActorNinePatch03(void)
1000 {
1001   TestApplication application;
1002   TestPlatformAbstraction& platform = application.GetPlatform();
1003   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
1004   TraceCallStack& textureTrace = glAbstraction.GetTextureTrace();
1005   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
1006
1007   tet_infoline("Test the successful loading of a nine-patch image added using ImageActor::SetImage()\n");
1008
1009   platform.SetClosestImageSize(Vector2(4, 4));
1010   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, false );
1011   Integration::PixelBuffer* pixels = bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888,  4,4,4,4 );
1012   memset( pixels, 0, 64 );
1013
1014   Integration::ResourcePointer resourcePtr(bitmap); // reference it
1015   platform.SetResourceLoaded( 0, Dali::Integration::ResourceBitmap, resourcePtr );
1016
1017   Image ninePatchImage = Image::New( "blah.#.png" );
1018   DALI_TEST_CHECK( ninePatchImage );
1019
1020   ImageActor imageActor = ImageActor::New();
1021   DALI_TEST_CHECK( imageActor );
1022   Stage::GetCurrent().Add( imageActor );
1023
1024   imageActor.SetImage( ninePatchImage );
1025
1026   drawTrace.Reset();
1027   textureTrace.Reset();
1028   drawTrace.Enable(true);
1029   textureTrace.Enable(true);
1030   glAbstraction.ClearBoundTextures();
1031   std::vector<GLuint> ids;
1032   ids.push_back( 23 );
1033   glAbstraction.SetNextTextureIds( ids );
1034
1035   application.SendNotification();
1036   application.Render();
1037
1038   DALI_TEST_CHECK( drawTrace.FindMethod( "DrawArrays" ) );
1039   typedef std::vector<GLuint> TexVec;
1040   const TexVec& textures = glAbstraction.GetBoundTextures(GL_TEXTURE0);
1041   DALI_TEST_CHECK( textures.size() > 0 );
1042   if( textures.size() > 0 )
1043   {
1044     DALI_TEST_EQUALS( textures[0], 23u, TEST_LOCATION );
1045   }
1046
1047   END_TEST;
1048 }
1049
1050
1051 int UtcDaliImageActorNinePatch04(void)
1052 {
1053   TestApplication application;
1054   TestPlatformAbstraction& platform = application.GetPlatform();
1055   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
1056   TraceCallStack& textureTrace = glAbstraction.GetTextureTrace();
1057   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
1058
1059   tet_infoline("Test the failed loading of a nine-patch image using ImageActor::SetImage()\n");
1060
1061   platform.SetClosestImageSize(Vector2(0, 0));
1062   Integration::ResourcePointer resourcePtr;
1063   platform.SetResourceLoaded( 0, Dali::Integration::ResourceBitmap, resourcePtr );
1064
1065   Image ninePatchImage = Image::New( "blah.#.png" );
1066   DALI_TEST_CHECK( ninePatchImage );
1067
1068   ImageActor imageActor = ImageActor::New();
1069   DALI_TEST_CHECK( imageActor );
1070   Stage::GetCurrent().Add( imageActor );
1071
1072   imageActor.SetImage( ninePatchImage );
1073
1074   drawTrace.Reset();
1075   textureTrace.Reset();
1076   drawTrace.Enable(true);
1077   textureTrace.Enable(true);
1078   glAbstraction.ClearBoundTextures();
1079   std::vector<GLuint> ids;
1080   ids.push_back( 23 );
1081   glAbstraction.SetNextTextureIds( ids );
1082
1083   application.SendNotification();
1084   application.Render();
1085
1086   // Check that nothing was drawn.
1087   DALI_TEST_CHECK( ! drawTrace.FindMethod( "DrawArrays" ) );
1088   typedef std::vector<GLuint> TexVec;
1089   const TexVec& textures = glAbstraction.GetBoundTextures(GL_TEXTURE0);
1090   DALI_TEST_CHECK( textures.size() == 0u );
1091
1092   END_TEST;
1093 }
1094
1095 int UtcDaliImageActorGetNaturalSize(void)
1096 {
1097   TestApplication application;
1098
1099   // Standard image
1100   BitmapImage img = BitmapImage::New( 10, 10 );
1101   ImageActor actor = ImageActor::New( img );
1102
1103   DALI_TEST_CHECK( actor.GetNaturalSize().GetVectorXY() == Vector2( 10, 10 ) );
1104
1105   // Pixel area set
1106   ImageActor::PixelArea area( 1, 2, 3, 4 );
1107   actor.SetPixelArea( area );
1108
1109   DALI_TEST_CHECK( actor.GetNaturalSize().GetVectorXY() == Vector2( 3, 4 ) );
1110
1111   END_TEST;
1112 }