ResourceImage/Image split
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Image.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 <algorithm>
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23 #include <test-native-image.h>
24
25 using namespace Dali;
26
27 void utc_dali_image_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void utc_dali_image_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37 static const char* gTestImageFilename = "icon_wrt.png";
38
39
40 // 1.1
41 int UtcDaliImageNew01(void)
42 {
43   TestApplication application;
44
45   tet_infoline("UtcDaliImageNew01 - Image::New(const std::string&)");
46
47   // invoke default handle constructor
48   Image image;
49
50   DALI_TEST_CHECK( !image );
51
52   // initialise handle
53   image = ResourceImage::New(gTestImageFilename);
54
55   DALI_TEST_CHECK( image );
56   END_TEST;
57 }
58
59 // 1.2
60 int UtcDaliImageNew02(void)
61 {
62   TestApplication application;
63
64   tet_infoline("UtcDaliImageNew02 - Image::New(const std::string&, const ImageAttributes&)");
65
66   // invoke default handle constructor
67   Image image;
68
69   DALI_TEST_CHECK( !image );
70
71   // initialise handle
72   Dali::ImageAttributes imageAttributes;
73   imageAttributes.SetSize(128, 256);
74   imageAttributes.SetScalingMode(Dali::ImageAttributes::FitHeight);
75   image = ResourceImage::New(gTestImageFilename, imageAttributes);
76
77   DALI_TEST_CHECK( image );
78   END_TEST;
79 }
80
81 // 1.3
82 int UtcDaliImageNew03(void)
83 {
84   TestApplication application;
85
86   tet_infoline("UtcDaliImageNew03 - Image::New(NativeImage&)");
87
88   // invoke default handle constructor
89   Image image;
90   TestNativeImagePointer nativeImage = TestNativeImage::New(16, 16);
91
92   DALI_TEST_CHECK( !image );
93
94   // initialise handle
95   image = Image::New(*(nativeImage.Get()));
96
97   DALI_TEST_CHECK( image );
98   END_TEST;
99 }
100
101 // 1.4
102 int UtcDaliImageNewWithPolicies01(void)
103 {
104   TestApplication application;
105
106   // testing delayed loading
107   tet_infoline("UtcDaliImageNewWithPolicies01 - Load image with LoadPolicy::OnDemand, ReleasePolicy::Never");
108   DALI_TEST_CHECK( !application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
109   Image image = ResourceImage::New(gTestImageFilename, ResourceImage::ON_DEMAND, Image::NEVER);
110
111   DALI_TEST_CHECK( image );
112
113   application.SendNotification();
114   application.Render(16);
115
116   // request file loading only when actor added to stage
117   DALI_TEST_CHECK( !application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
118
119   ImageActor actor = ImageActor::New(image);
120
121   Stage::GetCurrent().Add(actor);
122
123   application.SendNotification();
124   application.Render(16);
125
126   DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
127
128   // testing ReleasePolicy::Never
129   // fake loading image
130   std::vector<GLuint> ids;
131   ids.push_back( 23 );
132   application.GetGlAbstraction().SetNextTextureIds( ids );
133   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
134   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
135   Integration::ResourcePointer resource(bitmap);
136   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
137
138   if(request)
139   {
140     application.GetPlatform().SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
141   }
142   application.Render(16);
143   application.SendNotification();
144
145   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
146
147   // never discard texture
148   Stage::GetCurrent().Remove(actor);
149   application.Render(16);
150   application.SendNotification();
151   application.Render(16);
152   application.SendNotification();
153   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
154   END_TEST;
155 }
156
157 // 1.5
158 int UtcDaliImageNewWithPolicies02(void)
159 {
160   TestApplication application;
161   const Vector2 closestImageSize( 80, 45);
162   application.GetPlatform().SetClosestImageSize(closestImageSize);
163
164   // testing resource deletion when taken off stage
165   tet_infoline("UtcDaliImageNewWithPolicies02 - Load image with LoadPolicy::OnDemand, ReleasePolicy::Unused");
166
167   Image image = ResourceImage::New(gTestImageFilename, ResourceImage::ON_DEMAND, Image::UNUSED);
168
169   DALI_TEST_CHECK( image );
170
171   application.SendNotification();
172   application.Render(16);
173
174   // request file loading only when actor added to stage
175   DALI_TEST_CHECK( !application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
176
177   ImageActor actor = ImageActor::New(image);
178
179   Stage::GetCurrent().Add(actor);
180
181   application.SendNotification();
182   application.Render(16);
183
184   DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
185
186   // testing ReleasePolicy::Unused
187   // fake loading image
188   std::vector<GLuint> ids;
189   ids.push_back( 23 );
190   application.GetGlAbstraction().SetNextTextureIds( ids );
191   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
192   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
193   Integration::ResourcePointer resource(bitmap);
194   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
195
196   if(request)
197   {
198     application.GetPlatform().SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
199   }
200   application.Render(16);
201   application.SendNotification();
202
203   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
204
205   // discard texture when actor comes off stage
206   Stage::GetCurrent().Remove(actor);
207   application.Render(16);
208   application.SendNotification();
209   application.Render(16);
210   application.SendNotification();
211   DALI_TEST_CHECK ( application.GetGlAbstraction().CheckTextureDeleted(23) );
212   END_TEST;
213 }
214
215 // 1.6
216 int UtcDaliImageNewWithPolicies03(void)
217 {
218   TestApplication application;
219   const Vector2 closestImageSize( 80, 45);
220   application.GetPlatform().SetClosestImageSize(closestImageSize);
221
222   // load immediately -> resource deletion when taken off stage -> put actor back on stage -> load resource again
223   tet_infoline("UtcDaliImageNewWithPolicies03 - Load image with LoadPolicy::Immediate, ReleasePolicy::Unused");
224
225   Image image = ResourceImage::New(gTestImageFilename, ResourceImage::IMMEDIATE, Image::UNUSED);
226
227   DALI_TEST_CHECK( image );
228
229   application.SendNotification();
230   application.Render(16);
231
232   // request file loading immediately
233   DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
234
235   ImageActor actor = ImageActor::New(image);
236
237   Stage::GetCurrent().Add(actor);
238
239   application.SendNotification();
240   application.Render(16);
241
242   // testing ReleasePolicy::Unused
243   // fake loading image
244   std::vector<GLuint> ids;
245   ids.push_back( 23 );
246   application.GetGlAbstraction().SetNextTextureIds( ids );
247   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
248   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
249   Integration::ResourcePointer resource(bitmap);
250   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
251
252   if(request)
253   {
254     application.GetPlatform().SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
255   }
256   application.Render(16);
257   application.SendNotification();
258
259   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
260
261   // discard texture when actor comes off stage
262   Stage::GetCurrent().Remove(actor);
263   application.Render(16);
264   application.SendNotification();
265   application.Render(16);
266   application.SendNotification();
267   DALI_TEST_CHECK ( application.GetGlAbstraction().CheckTextureDeleted(23) );
268
269   // check load request when actor added back to stage
270   application.GetPlatform().ResetTrace();
271
272   Stage::GetCurrent().Add(actor);
273
274   application.SendNotification();
275   application.Render(16);
276   application.SendNotification();
277   application.Render(16);
278
279   DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
280   END_TEST;
281 }
282
283 // 1.7
284 int UtcDaliImageNewWithPolicies04(void)
285 {
286   TestApplication application;
287
288   // load immediately, don't release texture when off stage
289   tet_infoline("UtcDaliImageNewWithPolicies03 - Load image with LoadPolicy::Immediate, ReleasePolicy::Never");
290
291   Image image = ResourceImage::New(gTestImageFilename, ResourceImage::IMMEDIATE, Image::NEVER);
292
293   DALI_TEST_CHECK( image );
294
295   application.SendNotification();
296   application.Render(16);
297
298   // request file loading immediately
299   DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
300
301   ImageActor actor = ImageActor::New(image);
302
303   Stage::GetCurrent().Add(actor);
304
305   application.SendNotification();
306   application.Render(16);
307
308   // testing ReleasePolicy::Never
309   // fake loading image
310   std::vector<GLuint> ids;
311   ids.push_back( 23 );
312   application.GetGlAbstraction().SetNextTextureIds( ids );
313   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
314   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
315   Integration::ResourcePointer resource(bitmap);
316   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
317
318   if(request)
319   {
320     application.GetPlatform().SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
321   }
322   application.Render(16);
323   application.SendNotification();
324
325   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
326
327   // texture is not discarded
328   Stage::GetCurrent().Remove(actor);
329   application.Render(16);
330   application.SendNotification();
331   application.Render(16);
332   application.SendNotification();
333   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
334
335   // no load request when actor added back to stage
336   application.GetPlatform().ResetTrace();
337
338   Stage::GetCurrent().Add(actor);
339
340   application.SendNotification();
341   application.Render(16);
342   application.SendNotification();
343   application.Render(16);
344
345   DALI_TEST_CHECK( !application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
346   END_TEST;
347 }
348
349 // 1.8
350 int UtcDaliImageDownCast(void)
351 {
352   TestApplication application;
353   tet_infoline("Testing Dali::Image::DownCast()");
354
355   Image image = ResourceImage::New(gTestImageFilename);
356
357   BaseHandle object(image);
358
359   Image image2 = Image::DownCast(object);
360   DALI_TEST_CHECK(image2);
361
362   Image image3 = DownCast< Image >(object);
363   DALI_TEST_CHECK(image3);
364
365   BaseHandle unInitializedObject;
366   Image image4 = Image::DownCast(unInitializedObject);
367   DALI_TEST_CHECK(!image4);
368
369   Image image5 = DownCast< Image >(unInitializedObject);
370   DALI_TEST_CHECK(!image5);
371   END_TEST;
372 }
373
374 // 1.9
375 int UtcDaliImageGetImageSize(void)
376 {
377   TestApplication application;
378   TestPlatformAbstraction& platform = application.GetPlatform();
379
380   tet_infoline("UtcDaliImageGetImageSize - Image::GetImageSize()");
381
382   Vector2 testSize(8.0f, 16.0f);
383   platform.SetClosestImageSize(testSize);
384
385   Vector2 size = ResourceImage::GetImageSize(gTestImageFilename);
386
387   DALI_TEST_CHECK( application.GetPlatform().GetTrace().FindMethod("GetClosestImageSize"));
388   DALI_TEST_EQUALS( size, testSize, TEST_LOCATION);
389   END_TEST;
390 }
391
392 // 1.10
393 int UtcDaliImageGetUrl(void)
394 {
395   TestApplication application;
396
397   tet_infoline("UtcDaliImageGetFilename");
398
399   // invoke default handle constructor
400   ResourceImage image;
401
402   DALI_TEST_CHECK( !image );
403
404   // initialise handle
405   image = ResourceImage::New(gTestImageFilename);
406
407   DALI_TEST_EQUALS( image.GetUrl(), gTestImageFilename, TEST_LOCATION);
408   END_TEST;
409 }
410
411 // 1.11
412 int UtcDaliImageGetLoadingState01(void)
413 {
414   TestApplication application;
415   tet_infoline("UtcDaliImageGetLoadingState01");
416
417   ResourceImage image = ResourceImage::New(gTestImageFilename);
418   DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoading);
419   application.SendNotification();
420   application.Render(16);
421
422   // simulate load success
423   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
424   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
425   Integration::ResourcePointer resource(bitmap);
426   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
427
428   if(request)
429   {
430     application.GetPlatform().SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
431   }
432   application.Render(16);
433   application.SendNotification();
434
435   // Test state == ResourceLoadingSucceeded
436   DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoadingSucceeded);
437   END_TEST;
438 }
439
440 // 1.12
441 int UtcDaliImageGetLoadingState02(void)
442 {
443   TestApplication application;
444
445   tet_infoline("UtcDaliImageGetLoadingState02");
446
447   // invoke default handle constructor
448   ResourceImage image;
449
450   DALI_TEST_CHECK( !image );
451
452   // initialise handle
453   image = ResourceImage::New(gTestImageFilename);
454
455   // Test state == ResourceLoading
456   DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoading);
457   application.SendNotification();
458   application.Render(16);
459
460   // simulate load failure
461   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
462   if(request)
463   {
464     application.GetPlatform().SetResourceLoadFailed(request->GetId(), Integration::FailureUnknown);
465   }
466   application.Render(16);
467   application.SendNotification();
468
469   // Test state == ResourceLoadingFailed
470   DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoadingFailed);
471   END_TEST;
472 }
473
474 // 1.13
475 int UtcDaliImageGetReleasePolicy(void)
476 {
477   TestApplication application;
478
479   tet_infoline("UtcDaliImageGetReleasePolicy");
480
481   Image image = ResourceImage::New(gTestImageFilename, ResourceImage::IMMEDIATE, Image::UNUSED);
482
483   DALI_TEST_CHECK( image );
484
485   DALI_TEST_CHECK( Image::UNUSED == image.GetReleasePolicy() );
486
487   END_TEST;
488 }
489
490 // 1.14
491 int UtcDaliImageGetLoadPolicy(void)
492 {
493   TestApplication application;
494
495   tet_infoline("UtcDaliImageGetLoadPolicy");
496
497   ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::ON_DEMAND, Image::NEVER);
498
499   DALI_TEST_CHECK( image );
500
501   DALI_TEST_CHECK( ResourceImage::ON_DEMAND == image.GetLoadPolicy());
502   END_TEST;
503 }
504
505 static bool SignalLoadFlag = false;
506
507 static void SignalLoadHandler(ResourceImage image)
508 {
509   tet_infoline("Received image load finished signal");
510
511   SignalLoadFlag = true;
512 }
513
514 static bool SignalUploadedFlag = false;
515
516 static void SignalUploadedHandler(Image image)
517 {
518   tet_infoline("Received image uploaded signal");
519
520   SignalUploadedFlag = true;
521 }
522
523 // 1.15
524 int UtcDaliImageSignalLoadingFinished(void)
525 {
526   TestApplication application;
527
528   tet_infoline("UtcDaliImageSignalLoadingFinished");
529
530   SignalLoadFlag = false;
531
532   ResourceImage image = ResourceImage::New(gTestImageFilename);
533
534   image.LoadingFinishedSignal().Connect( SignalLoadHandler );
535   application.SendNotification();
536   application.Render(16);
537
538   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
539   if(request)
540   {
541     application.GetPlatform().SetResourceLoaded(request->GetId(), request->GetType()->id, Integration::ResourcePointer(Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD)));
542   }
543
544   application.Render(16);
545   application.SendNotification();
546
547   DALI_TEST_CHECK( SignalLoadFlag == true );
548   END_TEST;
549 }
550
551 // 1.16
552 int UtcDaliImageSignalUploaded(void)
553 {
554   TestApplication application;
555   tet_infoline("UtcDaliImageSignalUploaded - Image::SignalUploaded()");
556
557   // set up image in fake platform abstraction
558   Vector2 testSize(80.0f, 80.0f);
559   application.GetPlatform().SetClosestImageSize(testSize);
560
561   ResourceImage image = ResourceImage::New(gTestImageFilename);
562   image.LoadingFinishedSignal().Connect( SignalLoadHandler );
563
564   // Load image
565   application.SendNotification();
566   application.Render(16);
567   std::vector<GLuint> ids;
568   ids.push_back( 23 );
569   application.GetGlAbstraction().SetNextTextureIds( ids );
570   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
571   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
572   Integration::ResourcePointer resource(bitmap);
573   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
574   if(request)
575   {
576     application.GetPlatform().SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
577   }
578   application.Render(16);
579   application.SendNotification();
580
581   image.UploadedSignal().Connect( SignalUploadedHandler );
582
583   Dali::ImageActor imageActor = ImageActor::New(image);
584   Stage::GetCurrent().Add(imageActor);
585   imageActor.SetSize(80, 80);
586   imageActor.SetVisible(true);
587
588   application.SendNotification();
589   application.Render(0);
590   application.Render(16);
591   application.SendNotification();
592   application.Render(16);
593   application.SendNotification();
594   application.Render(16);
595   application.SendNotification();
596   application.Render(16);
597   application.SendNotification();
598
599   DALI_TEST_CHECK( SignalLoadFlag == true );
600   DALI_TEST_CHECK( SignalUploadedFlag == true );
601   SignalLoadFlag = false;
602   SignalUploadedFlag = false;
603
604   image.Reload();
605   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 160, 160, 160, 160);
606
607   application.SendNotification();
608   application.Render(16);
609   application.Render(16);
610   application.SendNotification();
611   DALI_TEST_CHECK( SignalLoadFlag == true );
612
613   application.Render(16);
614   application.SendNotification();
615   application.Render(16);
616   application.SendNotification();
617   DALI_TEST_CHECK( SignalUploadedFlag == true );
618   END_TEST;
619 }
620
621 // 1.17
622 int UtcDaliImageDiscard01(void)
623 {
624   TestApplication application;
625   tet_infoline("UtcDaliImageDiscard01 - no actors");
626
627   {
628     Image image = ResourceImage::New(gTestImageFilename);
629
630     // Load image
631     application.SendNotification();
632     application.Render(16);
633     std::vector<GLuint> ids;
634     ids.push_back( 23 );
635     application.GetGlAbstraction().SetNextTextureIds( ids );
636     Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
637     Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
638     Integration::ResourcePointer resource(bitmap);
639     bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
640     if(request)
641     {
642     application.GetPlatform().SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
643     }
644     application.Render(16);
645     application.SendNotification();
646   } // Drop image handle
647
648   application.SendNotification();
649   application.Render(16);
650   application.Render(16);
651   application.SendNotification();
652
653   // Shouldn't have been sent to GL...
654   const std::vector<GLuint>& texIds = application.GetGlAbstraction().GetNextTextureIds();
655   DALI_TEST_CHECK( texIds.size() == 1 );
656   DALI_TEST_CHECK( texIds[0] == 23 );
657   END_TEST;
658 }
659
660 // 1.18
661 int UtcDaliImageDiscard02(void)
662 {
663   TestApplication application;
664   application.GetGlAbstraction().EnableTextureCallTrace( true );
665   tet_infoline("UtcDaliImageDiscard02 - one actor, tests TextureCache::DiscardTexture");
666
667   {
668     {
669       ImageActor actor;
670       {
671         ImageAttributes attrs;
672         const Vector2 requestedSize( 40, 30 );
673         attrs.SetSize( requestedSize.width, requestedSize.height );
674         Image image = ResourceImage::New(gTestImageFilename, attrs);
675         actor = ImageActor::New(image);
676         Stage::GetCurrent().Add(actor);
677
678         application.SendNotification();
679         application.Render(16);
680
681         std::vector<GLuint> ids;
682         ids.push_back( 23 );
683         application.GetGlAbstraction().SetNextTextureIds( ids );
684
685         Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
686         Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
687         Integration::ResourcePointer resource(bitmap);
688         bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
689         if(request)
690         {
691           application.GetPlatform().SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
692         }
693         application.Render(16);
694         application.SendNotification();
695         DALI_TEST_CHECK( application.GetGlAbstraction().GetTextureTrace().FindMethod("BindTexture") );
696       } // lose image handle, actor should still keep one
697       application.SendNotification();
698       application.Render(16);
699
700       Stage::GetCurrent().Remove(actor);
701       application.SendNotification();
702       application.Render(16);
703     } // lose actor
704     application.SendNotification();
705     application.Render(16);
706   }
707
708   // Cleanup
709   application.SendNotification();
710   application.Render(16);
711   application.Render(16);
712   application.SendNotification();
713
714   // texture should have been removed:
715   DALI_TEST_CHECK( application.GetGlAbstraction().CheckTextureDeleted( 23 ));
716   END_TEST;
717 }
718
719 // 1.19
720 int UtcDaliImageDiscard03(void)
721 {
722   TestApplication application;
723   tet_infoline("UtcDaliImageDiscard03 - one actor, tests TextureCache::RemoveObserver");
724
725   const Vector2 closestImageSize( 1, 1);
726   application.GetPlatform().SetClosestImageSize(closestImageSize);
727
728   Image image = ResourceImage::New(gTestImageFilename);
729   ImageActor actor = ImageActor::New(image);
730   Stage::GetCurrent().Add(actor);
731
732   application.SendNotification();
733   application.Render(16);
734
735   std::vector<GLuint> ids;
736   ids.push_back( 23 );
737   application.GetGlAbstraction().SetNextTextureIds( ids );
738
739   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
740   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
741
742   Integration::ResourcePointer resource(bitmap);
743   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
744   if(request)
745   {
746     application.GetPlatform().SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
747   }
748   application.Render(16);
749   application.SendNotification();
750   application.SendNotification();
751   application.Render(16);
752
753   const std::vector<GLuint>& texIds = application.GetGlAbstraction().GetNextTextureIds();
754   DALI_TEST_CHECK( texIds.size() == 0 );
755   const std::vector<GLuint>& boundTexIds = application.GetGlAbstraction().GetBoundTextures();
756   DALI_TEST_CHECK( boundTexIds[0] == 23 );
757
758   Stage::GetCurrent().Remove(actor);
759   application.SendNotification();
760   application.Render(16);
761   application.SendNotification();
762   application.Render(16);
763   application.SendNotification();
764   application.Render(16); // Should remove image renderer
765
766   END_TEST;
767 }
768
769
770 namespace
771 {
772 void LoadBitmapResource(TestPlatformAbstraction& platform)
773 {
774   Integration::ResourceRequest* request = platform.GetRequest();
775   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
776   Integration::ResourcePointer resource(bitmap);
777   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
778
779   if(request)
780   {
781     platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
782   }
783 }
784
785 }
786
787 int UtcDaliImageContextLoss(void)
788 {
789   TestApplication application; // Default config: DALI_DISCARDS_ALL_DATA
790
791   const Vector2 closestImageSize( 80, 80 );
792   TestPlatformAbstraction& platform = application.GetPlatform();
793   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
794
795   platform.SetClosestImageSize(closestImageSize);
796
797   tet_infoline("UtcDaliImageContextLoss - Load image with LoadPolicy::Immediate, ReleasePolicy::Never, bitmap discard. Check that the image is re-requested on context regain\n");
798
799   Image image = ResourceImage::New("image.png", ResourceImage::IMMEDIATE, Image::NEVER);
800
801   DALI_TEST_CHECK( image );
802
803   application.SendNotification();
804   application.Render(16);
805
806   // request file loading immediately
807
808   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
809   ImageActor actor = ImageActor::New(image);
810   Stage::GetCurrent().Add(actor);
811
812   application.SendNotification();
813   application.Render(16);
814
815   TraceCallStack& textureTrace = glAbstraction.GetTextureTrace();
816   textureTrace.Enable(true);
817
818   std::vector<GLuint> ids;
819   ids.push_back( 23 );
820   glAbstraction.SetNextTextureIds( ids );
821
822   LoadBitmapResource(platform);
823
824   application.Render(16);
825   application.SendNotification();
826
827   DALI_TEST_CHECK( textureTrace.FindMethod("GenTextures") );
828
829   textureTrace.Reset();
830   textureTrace.Enable(true);
831   platform.ResetTrace();
832   platform.EnableTrace(true);
833
834   // Lose & regain context (in render 'thread')
835   application.ResetContext();
836
837   application.GetCore().RecoverFromContextLoss(); // start the recovery process
838   application.SendNotification();
839
840   // Run update/render loop
841   application.Render(16);
842   application.SendNotification();
843
844   // Expect new load request
845   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
846
847   // Finish loading image
848   LoadBitmapResource(platform);
849   ids.clear();
850   ids.push_back( 57 );
851   glAbstraction.SetNextTextureIds(ids);
852
853   // Run update/render loop
854   application.Render(16);
855   application.SendNotification();
856
857   // Expect new GenTextures
858   DALI_TEST_CHECK( textureTrace.FindMethod("GenTextures") );
859
860   END_TEST;
861 }