154e47d1f3b4bbb7d47322c8df8d2a1053c9feb5
[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\n", mLoadingFailedCalled);
59     return mLoadingFailedCalled;
60   }
61   int LoadSucceededCalled() {
62     tet_printf("TicketObserver: LoadingSucceeded()  called %d times\n", mLoadingSucceededCalled);
63     return mLoadingSucceededCalled;
64   }
65   int  UploadCalled() {
66     tet_printf("TicketObserver: Uploaded() called %d times\n", 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 , TEST_LOCATION );  // 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, TEST_LOCATION ); // 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(), 1, 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, TEST_LOCATION );          // Process update messages
443   application.SendNotification(); // Send event notifications
444
445   // Resource should have been discarded.
446   DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::CancelLoadFunc ) );
447   DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
448
449   DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 2, TEST_LOCATION );
450   DALI_TEST_EQUALS( testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
451   DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 1, TEST_LOCATION );
452   END_TEST;
453 }
454
455
456 int UtcDaliInternalRequestReloadBitmapRequests02(void)
457 {
458   TestApplication application; // Reset all test adapter return codes
459
460   tet_infoline("Testing bitmap reload during first load");
461
462   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
463   Integration::BitmapResourceType bitmapRequest;
464   Internal::ResourceId id(0);
465
466   testTicketObserver.Reset();
467
468   {
469     Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
470     Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
471     ticket->AddObserver(testTicketObserver);
472
473     // Update thread will request the bitmap resource:
474     // Sets application.GetPlatform().mRequest
475     application.SendNotification(); // Run flush update queue
476     application.Render(1);          // Process update messages
477     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
478
479     application.SendNotification(); // Send any event messages
480     DALI_TEST_CHECK( testTicketObserver.LoadFailedCalled() == 0 );
481     DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
482
483     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
484     DALI_TEST_CHECK( imageTicket );
485     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
486     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
487
488
489     /************************************************************
490      * FUNCTION UNDER TEST
491      ***********************************************************/
492     resourceClient.ReloadResource( ticket->GetId() );
493     /************************************************************
494      * Expected result - current load completes as usual, no reload requested
495      ************************************************************/
496
497     application.SendNotification(); // Flush update methods
498
499     // Reset call statistics - test that resource is not reloaded
500     application.GetPlatform().ResetTrace();
501
502     application.Render(0); // Process reload message (nothing for UpdateCache yet)
503
504     DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
505     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
506     // Create a resource
507     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
508     Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
509     bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 80, 80, 80, 80 );
510     Integration::ResourcePointer resourcePtr(bitmap); // reference it
511
512     // Set up platform abstraction to load it
513     id=req->GetId();
514
515     application.GetPlatform().SetResourceLoaded(id, Integration::ResourceBitmap, resourcePtr);
516
517     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
518
519     application.GetPlatform().ResetTrace();
520     // load the cache, which will immediately have the loaded resource
521     application.Render(0);
522     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
523     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
524     Internal::TextureMetadata* bitmapData = NULL;
525     DALI_TEST_CHECK( resourceManager.GetTextureMetadata(req->GetId(), bitmapData ) );
526     DALI_TEST_CHECK( bitmapData->GetWidth() == 80 );
527     DALI_TEST_CHECK( bitmapData->GetHeight() == 80 );
528
529     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
530     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
531
532     // Trigger the event thread to process notify messages. Should then trigger the signals
533     // in the ticket observer
534     application.SendNotification();
535
536     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
537     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
538     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
539
540     // Check that the image ticket was updated with the image attributes
541     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
542     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
543
544     DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
545
546     application.SendNotification(); // Flush update messages
547     application.Render(0);  // Process update messages
548
549     // There should be no reload
550     DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
551     application.SendNotification(); // Process event messages
552
553     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
554     DALI_TEST_EQUALS( testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
555     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
556     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
557
558     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
559     DALI_TEST_CHECK( resourceManager.GetTextureMetadata(req->GetId(), bitmapData ) );
560     DALI_TEST_CHECK( bitmapData->GetWidth() == 80 );
561     DALI_TEST_CHECK( bitmapData->GetHeight() == 80 );
562     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
563     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
564
565   } // Discard ticket
566   END_TEST;
567 }
568
569
570 int UtcDaliInternalRequestReloadBitmapRequests03(void)
571 {
572   TestApplication application; // Reset all test adapter return codes
573
574   tet_infoline("Testing bitmap reload at end of first load");
575
576   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
577   Integration::BitmapResourceType bitmapRequest;
578   Internal::ResourceId id(0);
579
580   testTicketObserver.Reset();
581
582   {
583     Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
584     Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
585     ticket->AddObserver(testTicketObserver);
586
587     // Update thread will request the bitmap resource:
588     // Sets application.GetPlatform().mRequest
589     application.SendNotification(); // Run flush update queue
590     application.Render(1);          // Process update messages
591     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
592
593     application.SendNotification(); // Send any event messages
594     DALI_TEST_CHECK( testTicketObserver.LoadFailedCalled() == 0 );
595     DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
596
597     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
598     DALI_TEST_CHECK( imageTicket );
599     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
600     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
601
602
603     /************************************************************
604      * FUNCTION UNDER TEST
605      ***********************************************************/
606     resourceClient.ReloadResource( ticket->GetId() );
607     /************************************************************
608      * Expected result - current load completes as usual, no reload requested
609      ************************************************************/
610
611     application.SendNotification(); // Flush update methods
612
613     // Reset call statistics - test that resource is not reloaded
614     application.GetPlatform().ResetTrace();
615
616     // Create a resource
617     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
618     Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
619     bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 80, 80, 80, 80 );
620     Integration::ResourcePointer resourcePtr(bitmap); // reference it
621
622     // Set up platform abstraction to load it
623     id=req->GetId();
624
625     application.GetPlatform().SetResourceLoaded(id, Integration::ResourceBitmap, resourcePtr);
626
627     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
628
629     application.GetPlatform().ResetTrace();
630     // load the cache, which will immediately have the loaded resource
631     application.Render(0);
632
633     // UpdateCache runs before ProcessMessages, so the loading resource completes before
634     // the reload request is handled.
635     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
636     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
637
638     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
639     Internal::TextureMetadata* bitmapData = NULL;
640     DALI_TEST_CHECK( resourceManager.GetTextureMetadata(req->GetId(), bitmapData ) );
641     DALI_TEST_CHECK( bitmapData->GetWidth() == 80 );
642     DALI_TEST_CHECK( bitmapData->GetHeight() == 80 );
643
644     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
645     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
646
647     // Trigger the event thread to process notify messages. Should then trigger the signals
648     // in the ticket observer
649     application.SendNotification();
650
651     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
652     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
653     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoading, TEST_LOCATION );
654
655     // Check that the image ticket was updated with the image attributes
656     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
657     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
658
659     DALI_TEST_EQUALS( resourceManager.ResourcesToProcess(), true, TEST_LOCATION );
660
661     // Create a new resource - the image size could have changed in the meantime
662     req = application.GetPlatform().GetRequest();
663     Integration::Bitmap* bitmap2 = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
664     bitmap2->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 120, 120, 120, 120 );
665     Integration::ResourcePointer resourcePtr2(bitmap2); // reference it
666     DALI_TEST_CHECK( req->GetId() == id );
667     application.GetPlatform().SetResourceLoaded(id, Integration::ResourceBitmap, resourcePtr2);
668
669     application.Render(0);  // Process update messages / UpdateCache
670
671     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
672     DALI_TEST_CHECK( resourceManager.GetTextureMetadata(req->GetId(), bitmapData ) );
673     DALI_TEST_CHECK( bitmapData->GetWidth() == 120 );
674     DALI_TEST_CHECK( bitmapData->GetHeight() == 120 );
675     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
676     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
677
678     application.SendNotification(); // Process event messages
679
680     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 2, TEST_LOCATION );
681
682     // Not staged - no GL upload
683     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
684
685     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
686     DALI_TEST_EQUALS( imageTicket->GetWidth(), 120, TEST_LOCATION );
687     DALI_TEST_EQUALS( imageTicket->GetHeight(), 120, TEST_LOCATION );
688     DALI_TEST_EQUALS( resourceManager.ResourcesToProcess(), false, TEST_LOCATION );
689   }
690   END_TEST;
691 }
692
693 int UtcDaliInternalRequestResourceTicket01(void)
694 {
695   TestApplication application;
696   tet_infoline("Testing RequestResourceTicket() with valid id");
697
698   testTicketObserver.Reset();
699
700   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
701
702   // First, load a bitmap resource
703   Internal::ResourceTicketPtr ticket = CheckLoadBitmap(application, "bitmap.jpg", 80, 80);
704
705   Internal::ResourceTicketPtr newTicket = resourceClient.RequestResourceTicket( ticket->GetId() );
706   DALI_TEST_CHECK( newTicket );
707   DALI_TEST_CHECK( newTicket->GetId() == ticket->GetId() );
708   DALI_TEST_CHECK( newTicket->GetTypePath().type->id == ticket->GetTypePath().type->id );
709   END_TEST;
710 }
711
712 int UtcDaliInternalRequestResourceTicket02(void)
713 {
714   TestApplication application;
715   tet_infoline("Testing RequestResourceTicket() with invalid id");
716
717   testTicketObserver.Reset();
718
719   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
720
721   // First, load a bitmap resource
722   Internal::ResourceTicketPtr ticket = CheckLoadBitmap(application, "bitmap.jpg", 80, 80);
723
724   Internal::ResourceTicketPtr newTicket = resourceClient.RequestResourceTicket( ticket->GetId() + 2000 );
725   DALI_TEST_CHECK( ! newTicket );
726   END_TEST;
727 }
728
729 int UtcDaliInternalAddBitmapImage01(void)
730 {
731   TestApplication application;
732   tet_infoline("Testing AddBitmap with reserved buffer()");
733   testTicketObserver.Reset();
734   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
735   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,  ResourcePolicy::OWNED_RETAIN  );
736   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGB565, 80, 80, 80, 80 );
737
738   Internal::ImageTicketPtr imageTicket = resourceClient.AddBitmapImage( bitmap );
739   DALI_TEST_CHECK( imageTicket );
740   imageTicket->AddObserver( testTicketObserver );
741
742   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
743   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
744   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
745
746   application.SendNotification(); // Flush update queue
747   application.Render(0); // Process message
748   application.SendNotification(); // Send message to tickets
749
750   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
751   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
752   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
753   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
754   END_TEST;
755 }
756
757 int UtcDaliInternalAddBitmapImage02(void)
758 {
759   TestApplication application;
760   tet_infoline("Testing AddBitmap without reserved buffer()");
761   testTicketObserver.Reset();
762   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
763   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,  ResourcePolicy::OWNED_RETAIN  );
764
765   Internal::ImageTicketPtr imageTicket = resourceClient.AddBitmapImage( bitmap );
766   DALI_TEST_CHECK( imageTicket );
767   imageTicket->AddObserver( testTicketObserver );
768
769   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
770   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 0, TEST_LOCATION );
771   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 0, TEST_LOCATION );
772   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
773
774   application.SendNotification(); // Flush update queue
775   application.Render(0); // Process message
776   application.SendNotification(); // Send message to tickets
777
778   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
779   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 0, TEST_LOCATION );
780   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 0, TEST_LOCATION );
781   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
782   END_TEST;
783 }
784
785
786 int UtcDaliInternalAddBitmapImage03(void)
787 {
788   TestApplication application;
789   tet_infoline("Testing AddBitmap() with invalid bitmap");
790   testTicketObserver.Reset();
791
792   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
793   Internal::ImageTicketPtr imageTicket;
794   bool exceptionRaised=false;
795   try
796   {
797     imageTicket = resourceClient.AddBitmapImage( NULL );
798   }
799   catch( DaliException& e )
800   {
801     exceptionRaised = true;
802   }
803   DALI_TEST_CHECK( exceptionRaised );
804   DALI_TEST_CHECK( ! imageTicket );
805   END_TEST;
806 }
807
808 int UtcDaliInternalAllocateTexture01(void)
809 {
810   TestApplication application;
811   tet_infoline("Testing AllocateTexture()");
812   testTicketObserver.Reset();
813
814   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
815   Internal::ResourceTicketPtr resourceTicket = resourceClient.AllocateTexture(80, 80, Pixel::L8 );
816   resourceTicket->AddObserver( testTicketObserver );
817
818   DALI_TEST_CHECK( resourceTicket );
819   DALI_TEST_EQUALS ( resourceTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
820   DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
821
822   application.SendNotification(); // Flush update queue
823   application.Render(0); // Process message
824   application.SendNotification(); // Send message to tickets
825
826   DALI_TEST_EQUALS ( resourceTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
827   DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
828   END_TEST;
829 }
830
831 int UtcDaliInternalAddNativeImage(void)
832 {
833   TestApplication application;
834   tet_infoline("Testing AddNativeImage()");
835
836   testTicketObserver.Reset();
837   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
838   Internal::ResourceTicketPtr ticket;
839   Internal::ImageTicketPtr imageTicket;
840   { // Test image going out of scope after ticket creation (message to Update thread holds a ref)
841     TestNativeImagePointer nativeImage = TestNativeImage::New( 80, 80 );
842     ticket = resourceClient.AddNativeImage( *nativeImage );
843     imageTicket = dynamic_cast<Internal::ImageTicket*>(ticket.Get());
844     DALI_TEST_CHECK( imageTicket );
845     imageTicket->AddObserver( testTicketObserver );
846   }
847
848   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
849   DALI_TEST_EQUALS ( imageTicket->GetWidth(),  80, TEST_LOCATION );
850   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
851   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
852
853   application.SendNotification(); // Flush update queue
854   application.Render(0); // Process message
855   application.SendNotification(); // Send message to tickets
856
857   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
858   DALI_TEST_EQUALS ( imageTicket->GetWidth(),  80, TEST_LOCATION );
859   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
860   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
861   END_TEST;
862 }
863
864 int UtcDaliInternalAddFrameBufferImage(void)
865 {
866   TestApplication application;
867   tet_infoline("Testing AddFrameBufferImage()");
868
869   testTicketObserver.Reset();
870   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
871   Internal::ImageTicketPtr imageTicket = resourceClient.AddFrameBufferImage(80, 80, Pixel::A8, RenderBuffer::COLOR );
872   DALI_TEST_CHECK( imageTicket );
873   imageTicket->AddObserver( testTicketObserver );
874
875   DALI_TEST_EQUALS ( imageTicket->GetWidth(),  80, TEST_LOCATION );
876   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
877   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
878   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
879
880   application.SendNotification(); // Flush update queue
881   application.Render(0); // Process message
882   application.SendNotification(); // Send message to tickets
883
884   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
885   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
886   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
887   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
888   END_TEST;
889 }