Merge "fix buffer GpuBuffer" into devel/master
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-internal / utc-Dali-Internal-ResourceClient.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 #include <test-native-image.h>
24
25 // Internal headers are allowed here
26 #include <dali/public-api/shader-effects/shader-effect.h>
27 #include <dali/internal/event/common/thread-local-storage.h>
28 #include <dali/internal/update/resources/texture-metadata.h>
29 #include <dali/internal/update/resources/resource-manager.h>
30 #include <dali/internal/update/manager/update-manager.h>
31 #include <dali/internal/event/resources/resource-client.h>
32 #include <dali/internal/event/resources/resource-ticket.h>
33 #include <dali/internal/event/resources/image-ticket.h>
34 #include <dali/internal/event/resources/resource-ticket-observer.h>
35 #include <dali/internal/event/images/resource-image-impl.h>
36 #include <dali/integration-api/resource-cache.h>
37 #include <dali/internal/render/gl-resources/texture-declarations.h>
38 #include <dali/internal/render/shaders/scene-graph-shader.h>
39 #include <dali/internal/common/owner-pointer.h>
40 #include <dali/internal/common/image-attributes.h>
41
42 using namespace Dali;
43
44 #include <mesh-builder.h>
45
46 namespace
47 {
48
49 class TestTicketObserver : public Internal::ResourceTicketObserver
50 {
51 public:
52   TestTicketObserver()
53   : mLoadingFailedCalled(0), mLoadingSucceededCalled(0),
54     mUploadedCount(0)
55   {}
56
57   int LoadFailedCalled() {
58     tet_printf("TicketObserver: LoadingFailed() called %d times", mLoadingFailedCalled);
59     return mLoadingFailedCalled;
60   }
61   int LoadSucceededCalled() {
62     tet_printf("TicketObserver: LoadingSucceeded()  called %d times", mLoadingSucceededCalled);
63     return mLoadingSucceededCalled;
64   }
65   int  UploadCalled() {
66     tet_printf("TicketObserver: Uploaded() called %d times", mUploadedCount);
67     return mUploadedCount;
68   }
69   void Reset() {
70     mLoadingFailedCalled    = 0;
71     mLoadingSucceededCalled = 0;
72     mUploadedCount           = 0;
73   }
74
75 public: // From ResourceTicketObserver
76   virtual void ResourceLoadingFailed(const Internal::ResourceTicket& ticket) {mLoadingFailedCalled++;}
77   virtual void ResourceLoadingSucceeded(const Internal::ResourceTicket& ticket) {mLoadingSucceededCalled++;}
78   virtual void ResourceUploaded(const Internal::ResourceTicket& ticket) {mUploadedCount++;}
79
80 private:
81   int mLoadingFailedCalled;
82   int mLoadingSucceededCalled;
83   int mUploadedCount;
84 };
85
86 class TestTicketLifetimeObserver : public Internal::ResourceTicketLifetimeObserver
87 {
88 public:
89   TestTicketLifetimeObserver() : resourceTicketDiscarded(false) {}
90
91   virtual void ResourceTicketDiscarded( const Internal::ResourceTicket& ticket )
92   { resourceTicketDiscarded = true; }
93
94   void Reset() { resourceTicketDiscarded = false; }
95   bool resourceTicketDiscarded;
96 };
97
98 static TestTicketObserver testTicketObserver;
99 static TestTicketLifetimeObserver testTicketLifetimeObserver;
100
101
102 Internal::ImagePtr LoadImage(TestApplication& application, char* name)
103 {
104   Internal::ResourceImagePtr image = Internal::ResourceImage::New( name, Internal::ImageAttributes::DEFAULT_ATTRIBUTES );
105   application.SendNotification(); // Flush update messages
106   application.Render();           // Process resource request
107   Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
108   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
109   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 80,80,80,80 );
110   Integration::ResourcePointer resourcePtr(bitmap); // reference it
111   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
112   application.Render();           // Process LoadComplete
113   application.SendNotification(); // Process event messages
114   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
115   req=NULL;
116   application.GetPlatform().ResetTrace();
117   return image;
118 }
119
120
121 Internal::ResourceTicketPtr CheckLoadBitmap(TestApplication& application, const char* name, int w, int h)
122 {
123   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
124   Integration::BitmapResourceType bitmapRequest;
125   Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, name );
126   ticket->AddObserver(testTicketObserver);
127   application.SendNotification(); // Flush update messages
128   application.Render();           // Process resource request
129   Integration::ResourceRequest*   req = application.GetPlatform().GetRequest();
130   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
131   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, w, h, w, h );
132   Integration::ResourcePointer resourcePtr(bitmap); // reference it
133   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
134   application.Render();           // Process LoadComplete
135   application.SendNotification(); // Process event messages
136   DALI_TEST_CHECK( ticket->GetLoadingState() == ResourceLoadingSucceeded );
137   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
138   req=NULL;
139   application.GetPlatform().ResetTrace();
140
141   return ticket;
142 }
143
144 } //anonymous namespace
145
146
147 void utc_dali_internal_resource_client_startup()
148 {
149   test_return_value = TET_UNDEF;
150 }
151
152 void utc_dali_internal_resource_client_cleanup()
153 {
154   test_return_value = TET_PASS;
155 }
156
157 // Load a bitmap resource successfully, then discard it's ticket
158 int UtcDaliInternalRequestResourceBitmapRequests01(void)
159 {
160   TestApplication application; // Reset all test adapter return codes
161
162   tet_infoline("Testing bitmap requests");
163
164   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
165   Integration::BitmapResourceType bitmapRequest;
166   Internal::ResourceId id(0);
167
168   testTicketObserver.Reset();
169
170   {
171     Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
172     /************************************************************
173      * FUNCTION UNDER TEST
174      ***********************************************************/
175     Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
176     ticket->AddObserver(testTicketObserver);
177
178     // Update thread will request the bitmap resource:
179     // Sets application.GetPlatform().mRequest
180     application.SendNotification(); // Run flush update queue
181     application.Render(1);          // Process update messages
182     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
183
184     application.SendNotification(); // Send any event messages
185     DALI_TEST_CHECK( testTicketObserver.LoadFailedCalled() == 0 );
186     DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
187     DALI_TEST_CHECK( ticket->GetLoadingState() == ResourceLoading );
188
189     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
190     DALI_TEST_CHECK( imageTicket );
191     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
192     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
193
194     // Create a resource
195     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
196     Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
197     bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 80, 80, 80, 80 );
198     Integration::ResourcePointer resourcePtr(bitmap); // reference it
199
200     // Set up platform abstraction to load it
201     id=req->GetId();
202     application.GetPlatform().SetResourceLoaded( id, Integration::ResourceBitmap, resourcePtr );
203
204     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(req->GetId()) );
205
206     // load the cache, which will immediately have the loaded resource
207     application.Render(0);
208
209     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
210
211     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(req->GetId()) );
212     Internal::TextureMetadata* bitmapData = NULL;
213     DALI_TEST_CHECK( resourceManager.GetTextureMetadata(req->GetId(), bitmapData ) );
214     DALI_TEST_CHECK( bitmapData->GetWidth() == 80 );
215     DALI_TEST_CHECK( bitmapData->GetHeight() == 80 );
216
217     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
218     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
219
220     // Trigger the event thread to process notify messages. Should then trigger the signals
221     // in the ticket observer
222     application.SendNotification();
223
224     DALI_TEST_CHECK( ticket->GetLoadingState() == ResourceLoadingSucceeded );
225     DALI_TEST_EQUALS(testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
226
227     // Check that the image ticket was updated with the image attributes
228     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
229     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
230   } // Discard ticket
231
232   application.SendNotification(); // Flush update queue (with ticket discarded message
233   application.Render(1);          // Process update messages
234   application.SendNotification(); // Send event notifications
235   application.Render(1);          // Process update messages
236
237   // Resource should have been discarded.
238   DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::CancelLoadFunc ) );
239   DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
240
241   DALI_TEST_EQUALS(testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
242   DALI_TEST_EQUALS(testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
243   END_TEST;
244 }
245
246 // Starting Loading a bitmap resource, then discard it's ticket before loading complete.
247 int UtcDaliInternalRequestResourceBitmapRequests02(void)
248 {
249   TestApplication application; // Reset all test adapter return codes
250
251   tet_infoline("Testing bitmap request ticket discard before load complete");
252
253   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
254   Integration::BitmapResourceType bitmapRequest;
255   Internal::ResourceId id(0);
256
257   testTicketObserver.Reset();
258
259   {
260     Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
261     /************************************************************
262      * FUNCTION UNDER TEST
263      ***********************************************************/
264     Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
265     ticket->AddObserver(testTicketObserver);
266     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
267     DALI_TEST_CHECK( imageTicket );
268     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
269     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
270
271     // Update thread will request the bitmap resource:
272     // Sets application.GetPlatform().mRequest
273     application.SendNotification(); // Run flush update queue
274     application.Render(1);
275     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
276     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
277     id=req->GetId();
278
279     application.SendNotification(); // Should produce no messages
280     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 0, TEST_LOCATION );
281     DALI_TEST_EQUALS( testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
282
283     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
284
285     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
286     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
287     DALI_TEST_CHECK( ticket->GetLoadingState() == ResourceLoading );
288
289   } // Discard ticket
290
291   // Ensure ticket discarded message is sent to update thread
292   application.SendNotification(); // Flush update queue
293   application.Render(0);          // Process update messages
294
295   DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::CancelLoadFunc ) );
296   DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
297
298   DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
299
300   // Trigger the event thread to process notify messages. Should then trigger the signals
301   // in the ticket observer
302   application.SendNotification();
303
304   DALI_TEST_EQUALS(testTicketObserver.LoadSucceededCalled(), 0, TEST_LOCATION );
305   DALI_TEST_EQUALS(testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
306   END_TEST;
307 }
308
309 // start loading a bitmap resource that doesn't exist, then discard it's ticket after failure
310 int UtcDaliInternalRequestResourceBitmapRequests03(void)
311 {
312   TestApplication application; // Reset all test adapter return codes
313
314   tet_infoline("Load bitmap that doesn't exist, followed by ticket discard. Expect LoadingFailed");
315
316   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
317   Integration::BitmapResourceType bitmapRequest;
318   Internal::ResourceId id(0);
319
320   testTicketObserver.Reset();
321   { // Scope lifetime of ticket
322     Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
323
324     /************************************************************
325      * FUNCTION UNDER TEST
326      ***********************************************************/
327     Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
328     ticket->AddObserver(testTicketObserver);
329     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
330     DALI_TEST_CHECK( imageTicket );
331     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
332     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
333
334     // Update thread will request the bitmap resource:
335     // Sets application.GetPlatform().mRequest
336     application.SendNotification(); // Run flush update queue
337     application.Render(1);          // process update messages
338     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
339     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
340     id=req->GetId();
341     application.SendNotification(); // Should produce no messages
342     DALI_TEST_CHECK( testTicketObserver.LoadFailedCalled() == 0 );
343     DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
344
345     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
346
347     application.GetPlatform().SetResourceLoadFailed(id, Integration::FailureFileNotFound );
348
349     application.Render(0); // Get failed result
350     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
351     application.SendNotification(); // send failed
352     DALI_TEST_CHECK( testTicketObserver.LoadFailedCalled() != 0 );
353     DALI_TEST_CHECK( ticket->GetLoadingState() == ResourceLoadingFailed );
354
355     DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
356     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
357     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
358
359     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
360   } // Discard ticket
361
362   application.Render(0); // Send DiscardTicket
363   application.SendNotification();
364
365   DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
366   END_TEST;
367 }
368
369
370
371 // Load a bitmap resource successfully, then reload it
372 int UtcDaliInternalRequestReloadBitmapRequests01(void)
373 {
374   TestApplication application; // Reset all test adapter return codes
375
376   tet_infoline("Testing bitmap reload after successful load");
377
378   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
379   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
380
381   Internal::ResourceId id(0);
382   testTicketObserver.Reset();
383
384   {
385     Internal::ResourceTicketPtr ticket = CheckLoadBitmap( application, "image.png", 80, 80 );
386     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
387     id = ticket->GetId();
388
389     // Reset call statistics - test that resource is reloaded
390     application.GetPlatform().ResetTrace();
391
392     /************************************************************
393      * FUNCTION UNDER TEST
394      ***********************************************************/
395     resourceClient.ReloadResource( ticket->GetId() );
396
397     DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
398     application.SendNotification(); // Flush update messages
399     application.Render(0);  // Process update messages
400     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
401     application.SendNotification(); // Process event messages
402
403     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoading, TEST_LOCATION );
404     DALI_TEST_EQUALS( testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
405     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
406     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
407
408     // Create a new resource - the image size could have changed in the meantime
409     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
410     Integration::Bitmap* bitmap2 = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
411     bitmap2->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 120, 120, 120, 120 );
412     Integration::ResourcePointer resourcePtr2(bitmap2); // reference it
413     DALI_TEST_CHECK( req->GetId() == ticket->GetId() );
414     application.GetPlatform().SetResourceLoaded(id, Integration::ResourceBitmap, resourcePtr2);
415
416     application.Render(0);  // Process update messages / UpdateCache
417     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
418
419     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
420     Internal::TextureMetadata* bitmapData = NULL;
421     DALI_TEST_CHECK( resourceManager.GetTextureMetadata(req->GetId(), bitmapData ) );
422     DALI_TEST_CHECK( bitmapData->GetWidth() == 120 );
423     DALI_TEST_CHECK( bitmapData->GetHeight() == 120 );
424
425     // Ticket can't have been updated yet - it should still have old values
426     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
427     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
428
429     application.SendNotification(); // Process event messages
430     application.Render(0);          // Process update messages / UpdateCache
431     application.SendNotification(); // Process event messages
432
433     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 2, TEST_LOCATION );
434     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
435     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
436     DALI_TEST_EQUALS( imageTicket->GetWidth(), 120, TEST_LOCATION );
437     DALI_TEST_EQUALS( imageTicket->GetHeight(), 120, TEST_LOCATION );
438
439   } // Discard ticket
440
441   application.SendNotification(); // Flush update queue (with ticket discarded message
442   application.Render(1);          // Process update messages
443   application.SendNotification(); // Send event notifications
444   application.Render(1);          // Process update messages
445
446   // Resource should have been discarded.
447   DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::CancelLoadFunc ) );
448   DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
449
450   DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 2, TEST_LOCATION );
451   DALI_TEST_EQUALS( testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
452   DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
453   END_TEST;
454 }
455
456
457 int UtcDaliInternalRequestReloadBitmapRequests02(void)
458 {
459   TestApplication application; // Reset all test adapter return codes
460
461   tet_infoline("Testing bitmap reload during first load");
462
463   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
464   Integration::BitmapResourceType bitmapRequest;
465   Internal::ResourceId id(0);
466
467   testTicketObserver.Reset();
468
469   {
470     Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
471     Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
472     ticket->AddObserver(testTicketObserver);
473
474     // Update thread will request the bitmap resource:
475     // Sets application.GetPlatform().mRequest
476     application.SendNotification(); // Run flush update queue
477     application.Render(1);          // Process update messages
478     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
479
480     application.SendNotification(); // Send any event messages
481     DALI_TEST_CHECK( testTicketObserver.LoadFailedCalled() == 0 );
482     DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
483
484     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
485     DALI_TEST_CHECK( imageTicket );
486     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
487     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
488
489
490     /************************************************************
491      * FUNCTION UNDER TEST
492      ***********************************************************/
493     resourceClient.ReloadResource( ticket->GetId() );
494     /************************************************************
495      * Expected result - current load completes as usual, no reload requested
496      ************************************************************/
497
498     application.SendNotification(); // Flush update methods
499
500     // Reset call statistics - test that resource is not reloaded
501     application.GetPlatform().ResetTrace();
502
503     application.Render(0); // Process reload message (nothing for UpdateCache yet)
504
505     DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
506     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
507     // Create a resource
508     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
509     Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
510     bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 80, 80, 80, 80 );
511     Integration::ResourcePointer resourcePtr(bitmap); // reference it
512
513     // Set up platform abstraction to load it
514     id=req->GetId();
515
516     application.GetPlatform().SetResourceLoaded(id, Integration::ResourceBitmap, resourcePtr);
517
518     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
519
520     application.GetPlatform().ResetTrace();
521     // load the cache, which will immediately have the loaded resource
522     application.Render(0);
523     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
524     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
525     Internal::TextureMetadata* bitmapData = NULL;
526     DALI_TEST_CHECK( resourceManager.GetTextureMetadata(req->GetId(), bitmapData ) );
527     DALI_TEST_CHECK( bitmapData->GetWidth() == 80 );
528     DALI_TEST_CHECK( bitmapData->GetHeight() == 80 );
529
530     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
531     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
532
533     // Trigger the event thread to process notify messages. Should then trigger the signals
534     // in the ticket observer
535     application.SendNotification();
536
537     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
538     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
539     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
540
541     // Check that the image ticket was updated with the image attributes
542     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
543     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
544
545     DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
546
547     application.SendNotification(); // Flush update messages
548     application.Render(0);  // Process update messages
549
550     // There should be no reload
551     DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
552     application.SendNotification(); // Process event messages
553
554     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
555     DALI_TEST_EQUALS( testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
556     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
557     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
558
559     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
560     DALI_TEST_CHECK( resourceManager.GetTextureMetadata(req->GetId(), bitmapData ) );
561     DALI_TEST_CHECK( bitmapData->GetWidth() == 80 );
562     DALI_TEST_CHECK( bitmapData->GetHeight() == 80 );
563     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
564     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
565
566   } // Discard ticket
567   END_TEST;
568 }
569
570
571 int UtcDaliInternalRequestReloadBitmapRequests03(void)
572 {
573   TestApplication application; // Reset all test adapter return codes
574
575   tet_infoline("Testing bitmap reload at end of first load");
576
577   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
578   Integration::BitmapResourceType bitmapRequest;
579   Internal::ResourceId id(0);
580
581   testTicketObserver.Reset();
582
583   {
584     Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
585     Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
586     ticket->AddObserver(testTicketObserver);
587
588     // Update thread will request the bitmap resource:
589     // Sets application.GetPlatform().mRequest
590     application.SendNotification(); // Run flush update queue
591     application.Render(1);          // Process update messages
592     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
593
594     application.SendNotification(); // Send any event messages
595     DALI_TEST_CHECK( testTicketObserver.LoadFailedCalled() == 0 );
596     DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
597
598     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
599     DALI_TEST_CHECK( imageTicket );
600     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
601     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
602
603
604     /************************************************************
605      * FUNCTION UNDER TEST
606      ***********************************************************/
607     resourceClient.ReloadResource( ticket->GetId() );
608     /************************************************************
609      * Expected result - current load completes as usual, no reload requested
610      ************************************************************/
611
612     application.SendNotification(); // Flush update methods
613
614     // Reset call statistics - test that resource is not reloaded
615     application.GetPlatform().ResetTrace();
616
617     // Create a resource
618     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
619     Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
620     bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 80, 80, 80, 80 );
621     Integration::ResourcePointer resourcePtr(bitmap); // reference it
622
623     // Set up platform abstraction to load it
624     id=req->GetId();
625
626     application.GetPlatform().SetResourceLoaded(id, Integration::ResourceBitmap, resourcePtr);
627
628     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
629
630     application.GetPlatform().ResetTrace();
631     // load the cache, which will immediately have the loaded resource
632     application.Render(0);
633
634     // UpdateCache runs before ProcessMessages, so the loading resource completes before
635     // the reload request is handled.
636     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
637     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
638
639     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
640     Internal::TextureMetadata* bitmapData = NULL;
641     DALI_TEST_CHECK( resourceManager.GetTextureMetadata(req->GetId(), bitmapData ) );
642     DALI_TEST_CHECK( bitmapData->GetWidth() == 80 );
643     DALI_TEST_CHECK( bitmapData->GetHeight() == 80 );
644
645     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
646     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
647
648     // Trigger the event thread to process notify messages. Should then trigger the signals
649     // in the ticket observer
650     application.SendNotification();
651
652     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
653     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
654     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoading, TEST_LOCATION );
655
656     // Check that the image ticket was updated with the image attributes
657     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
658     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
659
660     DALI_TEST_EQUALS( resourceManager.ResourcesToProcess(), true, TEST_LOCATION );
661
662     // Create a new resource - the image size could have changed in the meantime
663     req = application.GetPlatform().GetRequest();
664     Integration::Bitmap* bitmap2 = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
665     bitmap2->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 120, 120, 120, 120 );
666     Integration::ResourcePointer resourcePtr2(bitmap2); // reference it
667     DALI_TEST_CHECK( req->GetId() == id );
668     application.GetPlatform().SetResourceLoaded(id, Integration::ResourceBitmap, resourcePtr2);
669
670     application.Render(0);  // Process update messages / UpdateCache
671
672     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
673     DALI_TEST_CHECK( resourceManager.GetTextureMetadata(req->GetId(), bitmapData ) );
674     DALI_TEST_CHECK( bitmapData->GetWidth() == 120 );
675     DALI_TEST_CHECK( bitmapData->GetHeight() == 120 );
676     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
677     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
678
679     application.SendNotification(); // Process event messages
680
681     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 2, TEST_LOCATION );
682
683     // Not staged - no GL upload
684     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
685
686     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
687     DALI_TEST_EQUALS( imageTicket->GetWidth(), 120, TEST_LOCATION );
688     DALI_TEST_EQUALS( imageTicket->GetHeight(), 120, TEST_LOCATION );
689     DALI_TEST_EQUALS( resourceManager.ResourcesToProcess(), false, TEST_LOCATION );
690   }
691   END_TEST;
692 }
693
694 int UtcDaliInternalRequestResourceTicket01(void)
695 {
696   TestApplication application;
697   tet_infoline("Testing RequestResourceTicket() with valid id");
698
699   testTicketObserver.Reset();
700
701   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
702
703   // First, load a bitmap resource
704   Internal::ResourceTicketPtr ticket = CheckLoadBitmap(application, "bitmap.jpg", 80, 80);
705
706   Internal::ResourceTicketPtr newTicket = resourceClient.RequestResourceTicket( ticket->GetId() );
707   DALI_TEST_CHECK( newTicket );
708   DALI_TEST_CHECK( newTicket->GetId() == ticket->GetId() );
709   DALI_TEST_CHECK( newTicket->GetTypePath().type->id == ticket->GetTypePath().type->id );
710   END_TEST;
711 }
712
713 int UtcDaliInternalRequestResourceTicket02(void)
714 {
715   TestApplication application;
716   tet_infoline("Testing RequestResourceTicket() with invalid id");
717
718   testTicketObserver.Reset();
719
720   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
721
722   // First, load a bitmap resource
723   Internal::ResourceTicketPtr ticket = CheckLoadBitmap(application, "bitmap.jpg", 80, 80);
724
725   Internal::ResourceTicketPtr newTicket = resourceClient.RequestResourceTicket( ticket->GetId() + 2000 );
726   DALI_TEST_CHECK( ! newTicket );
727   END_TEST;
728 }
729
730 int UtcDaliInternalAddBitmapImage01(void)
731 {
732   TestApplication application;
733   tet_infoline("Testing AddBitmap with reserved buffer()");
734   testTicketObserver.Reset();
735   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
736   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,  ResourcePolicy::OWNED_RETAIN  );
737   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGB565, 80, 80, 80, 80 );
738
739   Internal::ImageTicketPtr imageTicket = resourceClient.AddBitmapImage( bitmap );
740   DALI_TEST_CHECK( imageTicket );
741   imageTicket->AddObserver( testTicketObserver );
742
743   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
744   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
745   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
746
747   application.SendNotification(); // Flush update queue
748   application.Render(0); // Process message
749   application.SendNotification(); // Send message to tickets
750
751   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
752   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
753   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
754   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
755   END_TEST;
756 }
757
758 int UtcDaliInternalAddBitmapImage02(void)
759 {
760   TestApplication application;
761   tet_infoline("Testing AddBitmap without reserved buffer()");
762   testTicketObserver.Reset();
763   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
764   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,  ResourcePolicy::OWNED_RETAIN  );
765
766   Internal::ImageTicketPtr imageTicket = resourceClient.AddBitmapImage( bitmap );
767   DALI_TEST_CHECK( imageTicket );
768   imageTicket->AddObserver( testTicketObserver );
769
770   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
771   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 0, TEST_LOCATION );
772   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 0, TEST_LOCATION );
773   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
774
775   application.SendNotification(); // Flush update queue
776   application.Render(0); // Process message
777   application.SendNotification(); // Send message to tickets
778
779   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
780   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 0, TEST_LOCATION );
781   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 0, TEST_LOCATION );
782   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
783   END_TEST;
784 }
785
786
787 int UtcDaliInternalAddBitmapImage03(void)
788 {
789   TestApplication application;
790   tet_infoline("Testing AddBitmap() with invalid bitmap");
791   testTicketObserver.Reset();
792
793   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
794   Internal::ImageTicketPtr imageTicket;
795   bool exceptionRaised=false;
796   try
797   {
798     imageTicket = resourceClient.AddBitmapImage( NULL );
799   }
800   catch( DaliException& e )
801   {
802     exceptionRaised = true;
803   }
804   DALI_TEST_CHECK( exceptionRaised );
805   DALI_TEST_CHECK( ! imageTicket );
806   END_TEST;
807 }
808
809 int UtcDaliInternalAllocateTexture01(void)
810 {
811   TestApplication application;
812   tet_infoline("Testing AllocateTexture()");
813   testTicketObserver.Reset();
814
815   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
816   Internal::ResourceTicketPtr resourceTicket = resourceClient.AllocateTexture(80, 80, Pixel::L8 );
817   resourceTicket->AddObserver( testTicketObserver );
818
819   DALI_TEST_CHECK( resourceTicket );
820   DALI_TEST_EQUALS ( resourceTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
821   DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
822
823   application.SendNotification(); // Flush update queue
824   application.Render(0); // Process message
825   application.SendNotification(); // Send message to tickets
826
827   DALI_TEST_EQUALS ( resourceTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
828   DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
829   END_TEST;
830 }
831
832 int UtcDaliInternalAddNativeImage(void)
833 {
834   TestApplication application;
835   tet_infoline("Testing AddNativeImage()");
836
837   testTicketObserver.Reset();
838   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
839   Internal::ResourceTicketPtr ticket;
840   Internal::ImageTicketPtr imageTicket;
841   { // Test image going out of scope after ticket creation (message to Update thread holds a ref)
842     TestNativeImagePointer nativeImage = TestNativeImage::New( 80, 80 );
843     ticket = resourceClient.AddNativeImage( *nativeImage );
844     imageTicket = dynamic_cast<Internal::ImageTicket*>(ticket.Get());
845     DALI_TEST_CHECK( imageTicket );
846     imageTicket->AddObserver( testTicketObserver );
847   }
848
849   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
850   DALI_TEST_EQUALS ( imageTicket->GetWidth(),  80, TEST_LOCATION );
851   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
852   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
853
854   application.SendNotification(); // Flush update queue
855   application.Render(0); // Process message
856   application.SendNotification(); // Send message to tickets
857
858   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
859   DALI_TEST_EQUALS ( imageTicket->GetWidth(),  80, TEST_LOCATION );
860   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
861   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
862   END_TEST;
863 }
864
865 int UtcDaliInternalAddFrameBufferImage(void)
866 {
867   TestApplication application;
868   tet_infoline("Testing AddFrameBufferImage()");
869
870   testTicketObserver.Reset();
871   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
872   Internal::ImageTicketPtr imageTicket = resourceClient.AddFrameBufferImage(80, 80, Pixel::A8, RenderBuffer::COLOR );
873   DALI_TEST_CHECK( imageTicket );
874   imageTicket->AddObserver( testTicketObserver );
875
876   DALI_TEST_EQUALS ( imageTicket->GetWidth(),  80, TEST_LOCATION );
877   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
878   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
879   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
880
881   application.SendNotification(); // Flush update queue
882   application.Render(0); // Process message
883   application.SendNotification(); // Send message to tickets
884
885   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
886   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
887   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
888   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
889   END_TEST;
890 }