Merge "Stability/Performance for update buffer index" 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/bitmap-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::BitmapMetadata bitmapData = resourceManager.GetBitmapMetadata(req->GetId());
213     DALI_TEST_CHECK( bitmapData.GetWidth() == 80 );
214     DALI_TEST_CHECK( bitmapData.GetHeight() == 80 );
215
216     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
217     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
218
219     // Trigger the event thread to process notify messages. Should then trigger the signals
220     // in the ticket observer
221     application.SendNotification();
222
223     DALI_TEST_CHECK( ticket->GetLoadingState() == ResourceLoadingSucceeded );
224     DALI_TEST_EQUALS(testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
225
226     // Check that the image ticket was updated with the image attributes
227     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
228     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
229   } // Discard ticket
230
231   application.SendNotification(); // Flush update queue (with ticket discarded message
232   application.Render(1);          // Process update messages
233   application.SendNotification(); // Send event notifications
234   application.Render(1);          // Process update messages
235
236   // Resource should have been discarded.
237   DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::CancelLoadFunc ) );
238   DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
239
240   DALI_TEST_EQUALS(testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
241   DALI_TEST_EQUALS(testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
242   END_TEST;
243 }
244
245 // Starting Loading a bitmap resource, then discard it's ticket before loading complete.
246 int UtcDaliInternalRequestResourceBitmapRequests02(void)
247 {
248   TestApplication application; // Reset all test adapter return codes
249
250   tet_infoline("Testing bitmap request ticket discard before load complete");
251
252   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
253   Integration::BitmapResourceType bitmapRequest;
254   Internal::ResourceId id(0);
255
256   testTicketObserver.Reset();
257
258   {
259     Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
260     /************************************************************
261      * FUNCTION UNDER TEST
262      ***********************************************************/
263     Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
264     ticket->AddObserver(testTicketObserver);
265     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
266     DALI_TEST_CHECK( imageTicket );
267     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
268     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
269
270     // Update thread will request the bitmap resource:
271     // Sets application.GetPlatform().mRequest
272     application.SendNotification(); // Run flush update queue
273     application.Render(1);
274     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
275     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
276     id=req->GetId();
277
278     application.SendNotification(); // Should produce no messages
279     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 0, TEST_LOCATION );
280     DALI_TEST_EQUALS( testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
281
282     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
283
284     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
285     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
286     DALI_TEST_CHECK( ticket->GetLoadingState() == ResourceLoading );
287
288   } // Discard ticket
289
290   // Ensure ticket discarded message is sent to update thread
291   application.SendNotification(); // Flush update queue
292   application.Render(0);          // Process update messages
293
294   DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::CancelLoadFunc ) );
295   DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
296
297   DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
298
299   // Trigger the event thread to process notify messages. Should then trigger the signals
300   // in the ticket observer
301   application.SendNotification();
302
303   DALI_TEST_EQUALS(testTicketObserver.LoadSucceededCalled(), 0, TEST_LOCATION );
304   DALI_TEST_EQUALS(testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
305   END_TEST;
306 }
307
308 // start loading a bitmap resource that doesn't exist, then discard it's ticket after failure
309 int UtcDaliInternalRequestResourceBitmapRequests03(void)
310 {
311   TestApplication application; // Reset all test adapter return codes
312
313   tet_infoline("Load bitmap that doesn't exist, followed by ticket discard. Expect LoadingFailed");
314
315   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
316   Integration::BitmapResourceType bitmapRequest;
317   Internal::ResourceId id(0);
318
319   testTicketObserver.Reset();
320   { // Scope lifetime of ticket
321     Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
322
323     /************************************************************
324      * FUNCTION UNDER TEST
325      ***********************************************************/
326     Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
327     ticket->AddObserver(testTicketObserver);
328     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
329     DALI_TEST_CHECK( imageTicket );
330     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
331     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
332
333     // Update thread will request the bitmap resource:
334     // Sets application.GetPlatform().mRequest
335     application.SendNotification(); // Run flush update queue
336     application.Render(1);          // process update messages
337     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
338     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
339     id=req->GetId();
340     application.SendNotification(); // Should produce no messages
341     DALI_TEST_CHECK( testTicketObserver.LoadFailedCalled() == 0 );
342     DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
343
344     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
345
346     application.GetPlatform().SetResourceLoadFailed(id, Integration::FailureFileNotFound );
347
348     application.Render(0); // Get failed result
349     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
350     application.SendNotification(); // send failed
351     DALI_TEST_CHECK( testTicketObserver.LoadFailedCalled() != 0 );
352     DALI_TEST_CHECK( ticket->GetLoadingState() == ResourceLoadingFailed );
353
354     DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
355     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
356     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
357
358     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
359   } // Discard ticket
360
361   application.Render(0); // Send DiscardTicket
362   application.SendNotification();
363
364   DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
365   END_TEST;
366 }
367
368
369
370 // Load a bitmap resource successfully, then reload it
371 int UtcDaliInternalRequestReloadBitmapRequests01(void)
372 {
373   TestApplication application; // Reset all test adapter return codes
374
375   tet_infoline("Testing bitmap reload after successful load");
376
377   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
378   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
379
380   Internal::ResourceId id(0);
381   testTicketObserver.Reset();
382
383   {
384     Internal::ResourceTicketPtr ticket = CheckLoadBitmap( application, "image.png", 80, 80 );
385     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
386     id = ticket->GetId();
387
388     // Reset call statistics - test that resource is reloaded
389     application.GetPlatform().ResetTrace();
390
391     /************************************************************
392      * FUNCTION UNDER TEST
393      ***********************************************************/
394     resourceClient.ReloadResource( ticket->GetId() );
395
396     DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
397     application.SendNotification(); // Flush update messages
398     application.Render(0);  // Process update messages
399     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
400     application.SendNotification(); // Process event messages
401
402     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoading, TEST_LOCATION );
403     DALI_TEST_EQUALS( testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
404     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
405     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
406
407     // Create a new resource - the image size could have changed in the meantime
408     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
409     Integration::Bitmap* bitmap2 = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
410     bitmap2->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 120, 120, 120, 120 );
411     Integration::ResourcePointer resourcePtr2(bitmap2); // reference it
412     DALI_TEST_CHECK( req->GetId() == ticket->GetId() );
413     application.GetPlatform().SetResourceLoaded(id, Integration::ResourceBitmap, resourcePtr2);
414
415     application.Render(0);  // Process update messages / UpdateCache
416     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
417
418     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
419     Internal::BitmapMetadata bitmapData = resourceManager.GetBitmapMetadata(id);
420     DALI_TEST_CHECK( bitmapData.GetWidth() == 120 );
421     DALI_TEST_CHECK( bitmapData.GetHeight() == 120 );
422
423     // Ticket can't have been updated yet - it should still have old values
424     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
425     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
426
427     application.SendNotification(); // Process event messages
428     application.Render(0);          // Process update messages / UpdateCache
429     application.SendNotification(); // Process event messages
430
431     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 2, TEST_LOCATION );
432     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
433     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
434     DALI_TEST_EQUALS( imageTicket->GetWidth(), 120, TEST_LOCATION );
435     DALI_TEST_EQUALS( imageTicket->GetHeight(), 120, TEST_LOCATION );
436
437   } // Discard ticket
438
439   application.SendNotification(); // Flush update queue (with ticket discarded message
440   application.Render(1);          // Process update messages
441   application.SendNotification(); // Send event notifications
442   application.Render(1);          // Process update messages
443
444   // Resource should have been discarded.
445   DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::CancelLoadFunc ) );
446   DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
447
448   DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 2, TEST_LOCATION );
449   DALI_TEST_EQUALS( testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
450   DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
451   END_TEST;
452 }
453
454
455 int UtcDaliInternalRequestReloadBitmapRequests02(void)
456 {
457   TestApplication application; // Reset all test adapter return codes
458
459   tet_infoline("Testing bitmap reload during first load");
460
461   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
462   Integration::BitmapResourceType bitmapRequest;
463   Internal::ResourceId id(0);
464
465   testTicketObserver.Reset();
466
467   {
468     Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
469     Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
470     ticket->AddObserver(testTicketObserver);
471
472     // Update thread will request the bitmap resource:
473     // Sets application.GetPlatform().mRequest
474     application.SendNotification(); // Run flush update queue
475     application.Render(1);          // Process update messages
476     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
477
478     application.SendNotification(); // Send any event messages
479     DALI_TEST_CHECK( testTicketObserver.LoadFailedCalled() == 0 );
480     DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
481
482     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
483     DALI_TEST_CHECK( imageTicket );
484     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
485     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
486
487
488     /************************************************************
489      * FUNCTION UNDER TEST
490      ***********************************************************/
491     resourceClient.ReloadResource( ticket->GetId() );
492     /************************************************************
493      * Expected result - current load completes as usual, no reload requested
494      ************************************************************/
495
496     application.SendNotification(); // Flush update methods
497
498     // Reset call statistics - test that resource is not reloaded
499     application.GetPlatform().ResetTrace();
500
501     application.Render(0); // Process reload message (nothing for UpdateCache yet)
502
503     DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
504     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
505     // Create a resource
506     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
507     Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
508     bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 80, 80, 80, 80 );
509     Integration::ResourcePointer resourcePtr(bitmap); // reference it
510
511     // Set up platform abstraction to load it
512     id=req->GetId();
513
514     application.GetPlatform().SetResourceLoaded(id, Integration::ResourceBitmap, resourcePtr);
515
516     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
517
518     application.GetPlatform().ResetTrace();
519     // load the cache, which will immediately have the loaded resource
520     application.Render(0);
521     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
522     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
523     Internal::BitmapMetadata bitmapData = resourceManager.GetBitmapMetadata(id);
524     DALI_TEST_CHECK( bitmapData.GetWidth() == 80 );
525     DALI_TEST_CHECK( bitmapData.GetHeight() == 80 );
526
527     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
528     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
529
530     // Trigger the event thread to process notify messages. Should then trigger the signals
531     // in the ticket observer
532     application.SendNotification();
533
534     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
535     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
536     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
537
538     // Check that the image ticket was updated with the image attributes
539     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
540     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
541
542     DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
543
544     application.SendNotification(); // Flush update messages
545     application.Render(0);  // Process update messages
546
547     // There should be no reload
548     DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
549     application.SendNotification(); // Process event messages
550
551     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
552     DALI_TEST_EQUALS( testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
553     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
554     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
555
556     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
557     bitmapData = resourceManager.GetBitmapMetadata(id);
558     DALI_TEST_CHECK( bitmapData.GetWidth() == 80 );
559     DALI_TEST_CHECK( bitmapData.GetHeight() == 80 );
560     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
561     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
562
563   } // Discard ticket
564   END_TEST;
565 }
566
567
568 int UtcDaliInternalRequestReloadBitmapRequests03(void)
569 {
570   TestApplication application; // Reset all test adapter return codes
571
572   tet_infoline("Testing bitmap reload at end of first load");
573
574   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
575   Integration::BitmapResourceType bitmapRequest;
576   Internal::ResourceId id(0);
577
578   testTicketObserver.Reset();
579
580   {
581     Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
582     Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
583     ticket->AddObserver(testTicketObserver);
584
585     // Update thread will request the bitmap resource:
586     // Sets application.GetPlatform().mRequest
587     application.SendNotification(); // Run flush update queue
588     application.Render(1);          // Process update messages
589     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
590
591     application.SendNotification(); // Send any event messages
592     DALI_TEST_CHECK( testTicketObserver.LoadFailedCalled() == 0 );
593     DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
594
595     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
596     DALI_TEST_CHECK( imageTicket );
597     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
598     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
599
600
601     /************************************************************
602      * FUNCTION UNDER TEST
603      ***********************************************************/
604     resourceClient.ReloadResource( ticket->GetId() );
605     /************************************************************
606      * Expected result - current load completes as usual, no reload requested
607      ************************************************************/
608
609     application.SendNotification(); // Flush update methods
610
611     // Reset call statistics - test that resource is not reloaded
612     application.GetPlatform().ResetTrace();
613
614     // Create a resource
615     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
616     Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
617     bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 80, 80, 80, 80 );
618     Integration::ResourcePointer resourcePtr(bitmap); // reference it
619
620     // Set up platform abstraction to load it
621     id=req->GetId();
622
623     application.GetPlatform().SetResourceLoaded(id, Integration::ResourceBitmap, resourcePtr);
624
625     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
626
627     application.GetPlatform().ResetTrace();
628     // load the cache, which will immediately have the loaded resource
629     application.Render(0);
630
631     // UpdateCache runs before ProcessMessages, so the loading resource completes before
632     // the reload request is handled.
633     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
634     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
635
636     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
637     Internal::BitmapMetadata bitmapData = resourceManager.GetBitmapMetadata(id);
638     DALI_TEST_CHECK( bitmapData.GetWidth() == 80 );
639     DALI_TEST_CHECK( bitmapData.GetHeight() == 80 );
640
641     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
642     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
643
644     // Trigger the event thread to process notify messages. Should then trigger the signals
645     // in the ticket observer
646     application.SendNotification();
647
648     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
649     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
650     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoading, TEST_LOCATION );
651
652     // Check that the image ticket was updated with the image attributes
653     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
654     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
655
656     DALI_TEST_EQUALS( resourceManager.ResourcesToProcess(), true, TEST_LOCATION );
657
658     // Create a new resource - the image size could have changed in the meantime
659     req = application.GetPlatform().GetRequest();
660     Integration::Bitmap* bitmap2 = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
661     bitmap2->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 120, 120, 120, 120 );
662     Integration::ResourcePointer resourcePtr2(bitmap2); // reference it
663     DALI_TEST_CHECK( req->GetId() == id );
664     application.GetPlatform().SetResourceLoaded(id, Integration::ResourceBitmap, resourcePtr2);
665
666     application.Render(0);  // Process update messages / UpdateCache
667
668     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
669     bitmapData = resourceManager.GetBitmapMetadata(id);
670     DALI_TEST_CHECK( bitmapData.GetWidth() == 120 );
671     DALI_TEST_CHECK( bitmapData.GetHeight() == 120 );
672     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
673     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
674
675     application.SendNotification(); // Process event messages
676
677     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 2, TEST_LOCATION );
678
679     // Not staged - no GL upload
680     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
681
682     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
683     DALI_TEST_EQUALS( imageTicket->GetWidth(), 120, TEST_LOCATION );
684     DALI_TEST_EQUALS( imageTicket->GetHeight(), 120, TEST_LOCATION );
685     DALI_TEST_EQUALS( resourceManager.ResourcesToProcess(), false, TEST_LOCATION );
686   }
687   END_TEST;
688 }
689
690 int UtcDaliInternalRequestResourceTicket01(void)
691 {
692   TestApplication application;
693   tet_infoline("Testing RequestResourceTicket() with valid id");
694
695   testTicketObserver.Reset();
696
697   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
698
699   // First, load a bitmap resource
700   Internal::ResourceTicketPtr ticket = CheckLoadBitmap(application, "bitmap.jpg", 80, 80);
701
702   Internal::ResourceTicketPtr newTicket = resourceClient.RequestResourceTicket( ticket->GetId() );
703   DALI_TEST_CHECK( newTicket );
704   DALI_TEST_CHECK( newTicket->GetId() == ticket->GetId() );
705   DALI_TEST_CHECK( newTicket->GetTypePath().type->id == ticket->GetTypePath().type->id );
706   END_TEST;
707 }
708
709 int UtcDaliInternalRequestResourceTicket02(void)
710 {
711   TestApplication application;
712   tet_infoline("Testing RequestResourceTicket() with invalid id");
713
714   testTicketObserver.Reset();
715
716   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
717
718   // First, load a bitmap resource
719   Internal::ResourceTicketPtr ticket = CheckLoadBitmap(application, "bitmap.jpg", 80, 80);
720
721   Internal::ResourceTicketPtr newTicket = resourceClient.RequestResourceTicket( ticket->GetId() + 2000 );
722   DALI_TEST_CHECK( ! newTicket );
723   END_TEST;
724 }
725
726 int UtcDaliInternalAddBitmapImage01(void)
727 {
728   TestApplication application;
729   tet_infoline("Testing AddBitmap with reserved buffer()");
730   testTicketObserver.Reset();
731   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
732   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,  ResourcePolicy::OWNED_RETAIN  );
733   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGB565, 80, 80, 80, 80 );
734
735   Internal::ImageTicketPtr imageTicket = resourceClient.AddBitmapImage( bitmap );
736   DALI_TEST_CHECK( imageTicket );
737   imageTicket->AddObserver( testTicketObserver );
738
739   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
740   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
741   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
742
743   application.SendNotification(); // Flush update queue
744   application.Render(0); // Process message
745   application.SendNotification(); // Send message to tickets
746
747   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
748   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
749   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
750   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
751   END_TEST;
752 }
753
754 int UtcDaliInternalAddBitmapImage02(void)
755 {
756   TestApplication application;
757   tet_infoline("Testing AddBitmap without reserved buffer()");
758   testTicketObserver.Reset();
759   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
760   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,  ResourcePolicy::OWNED_RETAIN  );
761
762   Internal::ImageTicketPtr imageTicket = resourceClient.AddBitmapImage( bitmap );
763   DALI_TEST_CHECK( imageTicket );
764   imageTicket->AddObserver( testTicketObserver );
765
766   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
767   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 0, TEST_LOCATION );
768   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 0, TEST_LOCATION );
769   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
770
771   application.SendNotification(); // Flush update queue
772   application.Render(0); // Process message
773   application.SendNotification(); // Send message to tickets
774
775   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
776   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 0, TEST_LOCATION );
777   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 0, TEST_LOCATION );
778   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
779   END_TEST;
780 }
781
782
783 int UtcDaliInternalAddBitmapImage03(void)
784 {
785   TestApplication application;
786   tet_infoline("Testing AddBitmap() with invalid bitmap");
787   testTicketObserver.Reset();
788
789   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
790   Internal::ImageTicketPtr imageTicket;
791   bool exceptionRaised=false;
792   try
793   {
794     imageTicket = resourceClient.AddBitmapImage( NULL );
795   }
796   catch( DaliException& e )
797   {
798     exceptionRaised = true;
799   }
800   DALI_TEST_CHECK( exceptionRaised );
801   DALI_TEST_CHECK( ! imageTicket );
802   END_TEST;
803 }
804
805 int UtcDaliInternalAllocateTexture01(void)
806 {
807   TestApplication application;
808   tet_infoline("Testing AllocateTexture()");
809   testTicketObserver.Reset();
810
811   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
812   Internal::ResourceTicketPtr resourceTicket = resourceClient.AllocateTexture(80, 80, Pixel::L8 );
813   resourceTicket->AddObserver( testTicketObserver );
814
815   DALI_TEST_CHECK( resourceTicket );
816   DALI_TEST_EQUALS ( resourceTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
817   DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
818
819   application.SendNotification(); // Flush update queue
820   application.Render(0); // Process message
821   application.SendNotification(); // Send message to tickets
822
823   DALI_TEST_EQUALS ( resourceTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
824   DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
825   END_TEST;
826 }
827
828 int UtcDaliInternalAddNativeImage(void)
829 {
830   TestApplication application;
831   tet_infoline("Testing AddNativeImage()");
832
833   testTicketObserver.Reset();
834   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
835   Internal::ResourceTicketPtr ticket;
836   Internal::ImageTicketPtr imageTicket;
837   { // Test image going out of scope after ticket creation (message to Update thread holds a ref)
838     TestNativeImagePointer nativeImage = TestNativeImage::New( 80, 80 );
839     ticket = resourceClient.AddNativeImage( *nativeImage );
840     imageTicket = dynamic_cast<Internal::ImageTicket*>(ticket.Get());
841     DALI_TEST_CHECK( imageTicket );
842     imageTicket->AddObserver( testTicketObserver );
843   }
844
845   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
846   DALI_TEST_EQUALS ( imageTicket->GetWidth(),  80, TEST_LOCATION );
847   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
848   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
849
850   application.SendNotification(); // Flush update queue
851   application.Render(0); // Process message
852   application.SendNotification(); // Send message to tickets
853
854   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
855   DALI_TEST_EQUALS ( imageTicket->GetWidth(),  80, TEST_LOCATION );
856   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
857   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
858   END_TEST;
859 }
860
861 int UtcDaliInternalAddFrameBufferImage(void)
862 {
863   TestApplication application;
864   tet_infoline("Testing AddFrameBufferImage()");
865
866   testTicketObserver.Reset();
867   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
868   Internal::ImageTicketPtr imageTicket = resourceClient.AddFrameBufferImage(80, 80, Pixel::A8, RenderBuffer::COLOR );
869   DALI_TEST_CHECK( imageTicket );
870   imageTicket->AddObserver( testTicketObserver );
871
872   DALI_TEST_EQUALS ( imageTicket->GetWidth(),  80, TEST_LOCATION );
873   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
874   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
875   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
876
877   application.SendNotification(); // Flush update queue
878   application.Render(0); // Process message
879   application.SendNotification(); // Send message to tickets
880
881   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
882   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
883   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
884   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
885   END_TEST;
886 }