ResourceImage/Image split
[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
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/internal/event/modeling/model-data-impl.h>
37 #include <dali/integration-api/resource-cache.h>
38 #include <dali/internal/render/gl-resources/texture-declarations.h>
39 #include <dali/internal/render/shaders/shader.h>
40 #include <dali/internal/common/owner-pointer.h>
41 #include <dali/public-api/shader-effects/shader-effect.h>
42
43
44 using namespace Dali;
45 #include <mesh-builder.h>
46
47 namespace
48 {
49
50 class TestTicketObserver : public Internal::ResourceTicketObserver
51 {
52 public:
53   TestTicketObserver()
54   : mLoadingFailedCalled(0), mLoadingSucceededCalled(0),
55     mSavingFailedCalled(0), mSavingSucceededCalled(0),
56     mUploadedCount(0)
57   {}
58
59   int LoadFailedCalled() {
60     tet_printf("TicketObserver: LoadingFailed() called %d times", mLoadingFailedCalled);
61     return mLoadingFailedCalled;
62   }
63   int LoadSucceededCalled() {
64     tet_printf("TicketObserver: LoadingSucceeded()  called %d times", mLoadingSucceededCalled);
65     return mLoadingSucceededCalled;
66   }
67   int SaveFailedCalled() {
68     tet_printf("TicketObserver: SavingFailed() called %d times", mSavingFailedCalled);
69     return mSavingFailedCalled;
70   }
71   int SaveSucceededCalled() {
72     tet_printf("TicketObserver: SavingSucceeded() called %d times", mSavingSucceededCalled);
73     return mSavingSucceededCalled;
74   }
75   int  UploadCalled() {
76     tet_printf("TicketObserver: Uploaded() called %d times", mUploadedCount);
77     return mUploadedCount;
78   }
79   void Reset() {
80     mLoadingFailedCalled    = 0;
81     mLoadingSucceededCalled = 0;
82     mSavingFailedCalled     = 0;
83     mSavingSucceededCalled  = 0;
84     mUploadedCount           = 0;
85   }
86
87 public: // From ResourceTicketObserver
88   virtual void ResourceLoadingFailed(const Internal::ResourceTicket& ticket) {mLoadingFailedCalled++;}
89   virtual void ResourceLoadingSucceeded(const Internal::ResourceTicket& ticket) {mLoadingSucceededCalled++;}
90   virtual void ResourceSavingFailed(const Internal::ResourceTicket& ticket) {mSavingFailedCalled++;}
91   virtual void ResourceSavingSucceeded(const Internal::ResourceTicket& ticket) {mSavingSucceededCalled++;}
92   virtual void ResourceUploaded(const Internal::ResourceTicket& ticket) {mUploadedCount++;}
93
94 private:
95   int mLoadingFailedCalled;
96   int mLoadingSucceededCalled;
97   int mSavingFailedCalled;
98   int mSavingSucceededCalled;
99   int mUploadedCount;
100 };
101
102 class TestTicketLifetimeObserver : public Internal::ResourceTicketLifetimeObserver
103 {
104 public:
105   TestTicketLifetimeObserver() : resourceTicketDiscarded(false) {}
106
107   virtual void ResourceTicketDiscarded( const Internal::ResourceTicket& ticket )
108   { resourceTicketDiscarded = true; }
109
110   void Reset() { resourceTicketDiscarded = false; }
111   bool resourceTicketDiscarded;
112 };
113
114 static TestTicketObserver testTicketObserver;
115 static TestTicketLifetimeObserver testTicketLifetimeObserver;
116
117
118 Internal::ImagePtr LoadImage(TestApplication& application, char* name)
119 {
120   Internal::ResourceImagePtr image = Internal::ResourceImage::New( name, Dali::ImageAttributes::DEFAULT_ATTRIBUTES );
121   application.SendNotification(); // Flush update messages
122   application.Render();           // Process resource request
123   Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
124   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::RETAIN );
125   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 80,80,80,80 );
126   Integration::ResourcePointer resourcePtr(bitmap); // reference it
127   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
128   application.Render();           // Process LoadComplete
129   application.SendNotification(); // Process event messages
130   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
131   req=NULL;
132   application.GetPlatform().ResetTrace();
133   return image;
134 }
135
136
137 Internal::ResourceTicketPtr CheckLoadBitmap(TestApplication& application, const char* name, int w, int h)
138 {
139   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
140   ImageAttributes attr;
141   Integration::BitmapResourceType bitmapRequest(attr);
142   Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, name );
143   ticket->AddObserver(testTicketObserver);
144   application.SendNotification(); // Flush update messages
145   application.Render();           // Process resource request
146   Integration::ResourceRequest*   req = application.GetPlatform().GetRequest();
147   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::RETAIN );
148   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, w, h, w, h );
149   Integration::ResourcePointer resourcePtr(bitmap); // reference it
150   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
151   application.Render();           // Process LoadComplete
152   application.SendNotification(); // Process event messages
153   DALI_TEST_CHECK( ticket->GetLoadingState() == ResourceLoadingSucceeded );
154   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
155   req=NULL;
156   application.GetPlatform().ResetTrace();
157
158   return ticket;
159 }
160
161 Internal::ResourceTicketPtr CheckLoadModel(TestApplication& application, const char* name)
162 {
163   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
164   Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( Integration::ModelResourceType(), name );
165   ticket->AddObserver(testTicketObserver);
166
167   application.SendNotification(); // Flush update messages
168   application.Render();           // Process resource request
169   Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
170   Dali::ModelData modelData = BuildTreeModel();
171   Internal::ModelData& modelDataImpl = GetImplementation(modelData);
172   Integration::ResourcePointer resourcePtr(&modelDataImpl);
173   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
174   application.Render();           // Process LoadComplete
175   application.SendNotification(); // Process event messages
176   DALI_TEST_CHECK(ticket->GetLoadingState() == ResourceLoadingSucceeded);
177   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
178   req=NULL;
179   application.GetPlatform().ResetTrace();
180
181   return ticket;
182 }
183
184 } //anonymous namespace
185
186
187 void utc_dali_internal_resource_client_startup()
188 {
189   test_return_value = TET_UNDEF;
190 }
191
192 void utc_dali_internal_resource_client_cleanup()
193 {
194   test_return_value = TET_PASS;
195 }
196
197 // Load a bitmap resource successfully, then discard it's ticket
198 int UtcDaliInternalRequestResourceBitmapRequests01(void)
199 {
200   TestApplication application; // Reset all test adapter return codes
201
202   tet_infoline("Testing bitmap requests");
203
204   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
205   ImageAttributes attr;
206   Integration::BitmapResourceType bitmapRequest (attr);
207   Internal::ResourceId id(0);
208
209   testTicketObserver.Reset();
210
211   {
212     Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
213     /************************************************************
214      * FUNCTION UNDER TEST
215      ***********************************************************/
216     Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
217     ticket->AddObserver(testTicketObserver);
218
219     // Update thread will request the bitmap resource:
220     // Sets application.GetPlatform().mRequest
221     application.SendNotification(); // Run flush update queue
222     application.Render(1);          // Process update messages
223     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
224
225     application.SendNotification(); // Send any event messages
226     DALI_TEST_CHECK( testTicketObserver.LoadFailedCalled() == 0 );
227     DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
228     DALI_TEST_CHECK( ticket->GetLoadingState() == ResourceLoading );
229
230     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
231     DALI_TEST_CHECK( imageTicket );
232     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
233     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
234
235     // Create a resource
236     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
237     Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::RETAIN );
238     bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 80, 80, 80, 80 );
239     Integration::ResourcePointer resourcePtr(bitmap); // reference it
240
241     // Set up platform abstraction to load it
242     id=req->GetId();
243     application.GetPlatform().SetResourceLoaded( id, Integration::ResourceBitmap, resourcePtr );
244
245     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(req->GetId()) );
246
247     // load the cache, which will immediately have the loaded resource
248     application.Render(0);
249
250     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
251
252     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(req->GetId()) );
253     Internal::BitmapMetadata bitmapData = resourceManager.GetBitmapMetadata(req->GetId());
254     DALI_TEST_CHECK( bitmapData.GetWidth() == 80 );
255     DALI_TEST_CHECK( bitmapData.GetHeight() == 80 );
256
257     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
258     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
259
260     // Trigger the event thread to process notify messages. Should then trigger the signals
261     // in the ticket observer
262     application.SendNotification();
263
264     DALI_TEST_CHECK( ticket->GetLoadingState() == ResourceLoadingSucceeded );
265     DALI_TEST_EQUALS(testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
266
267     // Check that the image ticket was updated with the image attributes
268     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
269     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
270   } // Discard ticket
271
272   application.SendNotification(); // Flush update queue (with ticket discarded message
273   application.Render(1);          // Process update messages
274   application.SendNotification(); // Send event notifications
275   application.Render(1);          // Process update messages
276
277   // Resource should have been discarded.
278   DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::CancelLoadFunc ) );
279   DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
280
281   DALI_TEST_EQUALS(testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
282   DALI_TEST_EQUALS(testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
283   END_TEST;
284 }
285
286 // Starting Loading a bitmap resource, then discard it's ticket before loading complete.
287 int UtcDaliInternalRequestResourceBitmapRequests02(void)
288 {
289   TestApplication application; // Reset all test adapter return codes
290
291   tet_infoline("Testing bitmap request ticket discard before load complete");
292
293   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
294   ImageAttributes attr;
295   Integration::BitmapResourceType bitmapRequest (attr);
296   Internal::ResourceId id(0);
297
298   testTicketObserver.Reset();
299
300   {
301     Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
302     /************************************************************
303      * FUNCTION UNDER TEST
304      ***********************************************************/
305     Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
306     ticket->AddObserver(testTicketObserver);
307     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
308     DALI_TEST_CHECK( imageTicket );
309     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
310     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
311
312     // Update thread will request the bitmap resource:
313     // Sets application.GetPlatform().mRequest
314     application.SendNotification(); // Run flush update queue
315     application.Render(1);
316     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
317     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
318     id=req->GetId();
319
320     application.SendNotification(); // Should produce no messages
321     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 0, TEST_LOCATION );
322     DALI_TEST_EQUALS( testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
323
324     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
325
326     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
327     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
328     DALI_TEST_CHECK( ticket->GetLoadingState() == ResourceLoading );
329
330   } // Discard ticket
331
332   // Ensure ticket discarded message is sent to update thread
333   application.SendNotification(); // Flush update queue
334   application.Render(0);          // Process update messages
335
336   DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::CancelLoadFunc ) );
337   DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
338
339   DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
340
341   // Trigger the event thread to process notify messages. Should then trigger the signals
342   // in the ticket observer
343   application.SendNotification();
344
345   DALI_TEST_EQUALS(testTicketObserver.LoadSucceededCalled(), 0, TEST_LOCATION );
346   DALI_TEST_EQUALS(testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
347   END_TEST;
348 }
349
350 // start loading a bitmap resource that doesn't exist, then discard it's ticket after failure
351 int UtcDaliInternalRequestResourceBitmapRequests03(void)
352 {
353   TestApplication application; // Reset all test adapter return codes
354
355   tet_infoline("Load bitmap that doesn't exist, followed by ticket discard. Expect LoadingFailed");
356
357   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
358   ImageAttributes attr;
359   Integration::BitmapResourceType bitmapRequest (attr);
360   Internal::ResourceId id(0);
361
362   testTicketObserver.Reset();
363   { // Scope lifetime of ticket
364     Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
365
366     /************************************************************
367      * FUNCTION UNDER TEST
368      ***********************************************************/
369     Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
370     ticket->AddObserver(testTicketObserver);
371     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
372     DALI_TEST_CHECK( imageTicket );
373     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
374     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
375
376     // Update thread will request the bitmap resource:
377     // Sets application.GetPlatform().mRequest
378     application.SendNotification(); // Run flush update queue
379     application.Render(1);          // process update messages
380     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
381     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
382     id=req->GetId();
383     application.SendNotification(); // Should produce no messages
384     DALI_TEST_CHECK( testTicketObserver.LoadFailedCalled() == 0 );
385     DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
386
387     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
388
389     application.GetPlatform().SetResourceLoadFailed(id, Integration::FailureFileNotFound );
390
391     application.Render(0); // Get failed result
392     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
393     application.SendNotification(); // send failed
394     DALI_TEST_CHECK( testTicketObserver.LoadFailedCalled() != 0 );
395     DALI_TEST_CHECK( ticket->GetLoadingState() == ResourceLoadingFailed );
396
397     DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
398     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
399     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
400
401     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
402   } // Discard ticket
403
404   application.Render(0); // Send DiscardTicket
405   application.SendNotification();
406
407   DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
408   END_TEST;
409 }
410
411
412
413 // Load a bitmap resource successfully, then reload it
414 int UtcDaliInternalRequestReloadBitmapRequests01(void)
415 {
416   TestApplication application; // Reset all test adapter return codes
417
418   tet_infoline("Testing bitmap reload after successful load");
419
420   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
421   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
422
423   Internal::ResourceId id(0);
424   testTicketObserver.Reset();
425
426   {
427     Internal::ResourceTicketPtr ticket = CheckLoadBitmap( application, "image.png", 80, 80 );
428     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
429     id = ticket->GetId();
430
431     // Reset call statistics - test that resource is reloaded
432     application.GetPlatform().ResetTrace();
433
434     /************************************************************
435      * FUNCTION UNDER TEST
436      ***********************************************************/
437     resourceClient.ReloadResource( ticket->GetId() );
438
439     DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
440     application.SendNotification(); // Flush update messages
441     application.Render(0);  // Process update messages
442     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
443     application.SendNotification(); // Process event messages
444
445     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoading, TEST_LOCATION );
446     DALI_TEST_EQUALS( testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
447     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
448     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
449
450     // Create a new resource - the image size could have changed in the meantime
451     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
452     Integration::Bitmap* bitmap2 = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::RETAIN );
453     bitmap2->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 120, 120, 120, 120 );
454     Integration::ResourcePointer resourcePtr2(bitmap2); // reference it
455     DALI_TEST_CHECK( req->GetId() == ticket->GetId() );
456     application.GetPlatform().SetResourceLoaded(id, Integration::ResourceBitmap, resourcePtr2);
457
458     application.Render(0);  // Process update messages / UpdateCache
459     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
460
461     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
462     Internal::BitmapMetadata bitmapData = resourceManager.GetBitmapMetadata(id);
463     DALI_TEST_CHECK( bitmapData.GetWidth() == 120 );
464     DALI_TEST_CHECK( bitmapData.GetHeight() == 120 );
465
466     // Ticket can't have been updated yet - it should still have old values
467     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
468     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
469
470     application.SendNotification(); // Process event messages
471     application.Render(0);          // Process update messages / UpdateCache
472     application.SendNotification(); // Process event messages
473
474     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 2, TEST_LOCATION );
475     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
476     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
477     DALI_TEST_EQUALS( imageTicket->GetWidth(), 120, TEST_LOCATION );
478     DALI_TEST_EQUALS( imageTicket->GetHeight(), 120, TEST_LOCATION );
479
480   } // Discard ticket
481
482   application.SendNotification(); // Flush update queue (with ticket discarded message
483   application.Render(1);          // Process update messages
484   application.SendNotification(); // Send event notifications
485   application.Render(1);          // Process update messages
486
487   // Resource should have been discarded.
488   DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::CancelLoadFunc ) );
489   DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
490
491   DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 2, TEST_LOCATION );
492   DALI_TEST_EQUALS( testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
493   DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
494   END_TEST;
495 }
496
497
498 int UtcDaliInternalRequestReloadBitmapRequests02(void)
499 {
500   TestApplication application; // Reset all test adapter return codes
501
502   tet_infoline("Testing bitmap reload during first load");
503
504   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
505   ImageAttributes attr;
506   Integration::BitmapResourceType bitmapRequest (attr);
507   Internal::ResourceId id(0);
508
509   testTicketObserver.Reset();
510
511   {
512     Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
513     Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
514     ticket->AddObserver(testTicketObserver);
515
516     // Update thread will request the bitmap resource:
517     // Sets application.GetPlatform().mRequest
518     application.SendNotification(); // Run flush update queue
519     application.Render(1);          // Process update messages
520     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
521
522     application.SendNotification(); // Send any event messages
523     DALI_TEST_CHECK( testTicketObserver.LoadFailedCalled() == 0 );
524     DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
525
526     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
527     DALI_TEST_CHECK( imageTicket );
528     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
529     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
530
531
532     /************************************************************
533      * FUNCTION UNDER TEST
534      ***********************************************************/
535     resourceClient.ReloadResource( ticket->GetId() );
536     /************************************************************
537      * Expected result - current load completes as usual, no reload requested
538      ************************************************************/
539
540     application.SendNotification(); // Flush update methods
541
542     // Reset call statistics - test that resource is not reloaded
543     application.GetPlatform().ResetTrace();
544
545     application.Render(0); // Process reload message (nothing for UpdateCache yet)
546
547     DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
548     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
549     // Create a resource
550     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
551     Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::RETAIN );
552     bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 80, 80, 80, 80 );
553     Integration::ResourcePointer resourcePtr(bitmap); // reference it
554
555     // Set up platform abstraction to load it
556     id=req->GetId();
557
558     application.GetPlatform().SetResourceLoaded(id, Integration::ResourceBitmap, resourcePtr);
559
560     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
561
562     application.GetPlatform().ResetTrace();
563     // load the cache, which will immediately have the loaded resource
564     application.Render(0);
565     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
566     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
567     Internal::BitmapMetadata bitmapData = resourceManager.GetBitmapMetadata(id);
568     DALI_TEST_CHECK( bitmapData.GetWidth() == 80 );
569     DALI_TEST_CHECK( bitmapData.GetHeight() == 80 );
570
571     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
572     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
573
574     // Trigger the event thread to process notify messages. Should then trigger the signals
575     // in the ticket observer
576     application.SendNotification();
577
578     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
579     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
580     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
581
582     // Check that the image ticket was updated with the image attributes
583     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
584     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
585
586     DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
587
588     application.SendNotification(); // Flush update messages
589     application.Render(0);  // Process update messages
590
591     // There should be no reload
592     DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
593     application.SendNotification(); // Process event messages
594
595     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
596     DALI_TEST_EQUALS( testTicketObserver.LoadFailedCalled(), 0, TEST_LOCATION );
597     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
598     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
599
600     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
601     bitmapData = resourceManager.GetBitmapMetadata(id);
602     DALI_TEST_CHECK( bitmapData.GetWidth() == 80 );
603     DALI_TEST_CHECK( bitmapData.GetHeight() == 80 );
604     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
605     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
606
607   } // Discard ticket
608   END_TEST;
609 }
610
611
612 int UtcDaliInternalRequestReloadBitmapRequests03(void)
613 {
614   TestApplication application; // Reset all test adapter return codes
615
616   tet_infoline("Testing bitmap reload at end of first load");
617
618   Internal::ResourceManager& resourceManager = Internal::ThreadLocalStorage::Get().GetResourceManager();
619   ImageAttributes attr;
620   Integration::BitmapResourceType bitmapRequest (attr);
621   Internal::ResourceId id(0);
622
623   testTicketObserver.Reset();
624
625   {
626     Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
627     Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( bitmapRequest, "image.png" );
628     ticket->AddObserver(testTicketObserver);
629
630     // Update thread will request the bitmap resource:
631     // Sets application.GetPlatform().mRequest
632     application.SendNotification(); // Run flush update queue
633     application.Render(1);          // Process update messages
634     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
635
636     application.SendNotification(); // Send any event messages
637     DALI_TEST_CHECK( testTicketObserver.LoadFailedCalled() == 0 );
638     DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
639
640     Internal::ImageTicketPtr imageTicket(dynamic_cast<Internal::ImageTicket*>(ticket.Get()));
641     DALI_TEST_CHECK( imageTicket );
642     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
643     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
644
645
646     /************************************************************
647      * FUNCTION UNDER TEST
648      ***********************************************************/
649     resourceClient.ReloadResource( ticket->GetId() );
650     /************************************************************
651      * Expected result - current load completes as usual, no reload requested
652      ************************************************************/
653
654     application.SendNotification(); // Flush update methods
655
656     // Reset call statistics - test that resource is not reloaded
657     application.GetPlatform().ResetTrace();
658
659     // Create a resource
660     Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
661     Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::RETAIN );
662     bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 80, 80, 80, 80 );
663     Integration::ResourcePointer resourcePtr(bitmap); // reference it
664
665     // Set up platform abstraction to load it
666     id=req->GetId();
667
668     application.GetPlatform().SetResourceLoaded(id, Integration::ResourceBitmap, resourcePtr);
669
670     DALI_TEST_CHECK( ! resourceManager.IsResourceLoaded(id));
671
672     application.GetPlatform().ResetTrace();
673     // load the cache, which will immediately have the loaded resource
674     application.Render(0);
675
676     // UpdateCache runs before ProcessMessages, so the loading resource completes before
677     // the reload request is handled.
678     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc ) );
679     DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::GetResourcesFunc ) );
680
681     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
682     Internal::BitmapMetadata bitmapData = resourceManager.GetBitmapMetadata(id);
683     DALI_TEST_CHECK( bitmapData.GetWidth() == 80 );
684     DALI_TEST_CHECK( bitmapData.GetHeight() == 80 );
685
686     DALI_TEST_EQUALS( imageTicket->GetWidth(), 0, TEST_LOCATION );
687     DALI_TEST_EQUALS( imageTicket->GetHeight(), 0, TEST_LOCATION );
688
689     // Trigger the event thread to process notify messages. Should then trigger the signals
690     // in the ticket observer
691     application.SendNotification();
692
693     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 1, TEST_LOCATION );
694     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
695     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoading, TEST_LOCATION );
696
697     // Check that the image ticket was updated with the image attributes
698     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
699     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
700
701     DALI_TEST_EQUALS( resourceManager.ResourcesToProcess(), true, TEST_LOCATION );
702
703     // Create a new resource - the image size could have changed in the meantime
704     req = application.GetPlatform().GetRequest();
705     Integration::Bitmap* bitmap2 = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::RETAIN );
706     bitmap2->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 120, 120, 120, 120 );
707     Integration::ResourcePointer resourcePtr2(bitmap2); // reference it
708     DALI_TEST_CHECK( req->GetId() == id );
709     application.GetPlatform().SetResourceLoaded(id, Integration::ResourceBitmap, resourcePtr2);
710
711     application.Render(0);  // Process update messages / UpdateCache
712
713     DALI_TEST_CHECK( resourceManager.IsResourceLoaded(id));
714     bitmapData = resourceManager.GetBitmapMetadata(id);
715     DALI_TEST_CHECK( bitmapData.GetWidth() == 120 );
716     DALI_TEST_CHECK( bitmapData.GetHeight() == 120 );
717     DALI_TEST_EQUALS( imageTicket->GetWidth(), 80, TEST_LOCATION );
718     DALI_TEST_EQUALS( imageTicket->GetHeight(), 80, TEST_LOCATION );
719
720     application.SendNotification(); // Process event messages
721
722     DALI_TEST_EQUALS( testTicketObserver.LoadSucceededCalled(), 2, TEST_LOCATION );
723
724     // Not staged - no GL upload
725     DALI_TEST_EQUALS( testTicketObserver.UploadCalled(), 0, TEST_LOCATION );
726
727     DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
728     DALI_TEST_EQUALS( imageTicket->GetWidth(), 120, TEST_LOCATION );
729     DALI_TEST_EQUALS( imageTicket->GetHeight(), 120, TEST_LOCATION );
730     DALI_TEST_EQUALS( resourceManager.ResourcesToProcess(), false, TEST_LOCATION );
731   }
732   END_TEST;
733 }
734
735
736 int UtcDaliInternalSaveResource01(void)
737 {
738   TestApplication application;
739   tet_infoline("Testing SaveResource() with valid id, and valid filename");
740
741   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
742
743   Dali::ModelData modelData = BuildTreeModel();
744   testTicketObserver.Reset();
745
746   Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( Integration::ModelResourceType(), "model.dae" );
747   ticket->AddObserver(testTicketObserver);
748
749   // First, load a model resource
750   application.SendNotification(); // Flush update messages
751   application.Render();           // Process resource request
752   Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
753   Internal::ModelData& modelDataImpl = GetImplementation(modelData);
754   Integration::ResourcePointer resourcePtr(&modelDataImpl);
755
756   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
757   application.Render();           // Process LoadComplete
758   application.SendNotification(); // Process event messages
759   DALI_TEST_CHECK( ticket->GetLoadingState() == ResourceLoadingSucceeded );
760
761   // Try saving it
762   resourceClient.SaveResource( ticket, "model.dali-bin" );
763   application.SendNotification(); // Flush update messages
764   application.Render();           // Process save resource request
765
766   DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::SaveResourceFunc ) );
767   req = application.GetPlatform().GetRequest();
768   DALI_TEST_CHECK( req->GetType()->id == Integration::ResourceModel );
769   DALI_TEST_CHECK( req->GetPath().compare("model.dali-bin") == 0 );
770
771   // Set up success response
772   application.GetPlatform().SetResourceSaved(req->GetId(), req->GetType()->id);
773   application.Render();           // GetResources --> SaveComplete
774   application.SendNotification(); // Send event messages
775
776   DALI_TEST_CHECK( testTicketObserver.SaveSucceededCalled() == 1 );
777   DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION )
778 ;
779   END_TEST;
780 }
781
782
783 int UtcDaliInternalSaveResource02(void)
784 {
785   TestApplication application;
786   tet_infoline("Testing SaveResource() with invalid id");
787   testTicketObserver.Reset();
788
789   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
790
791   Dali::ModelData modelData = BuildTreeModel();
792
793   Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( Integration::ModelResourceType(), "model.dae" );
794   ticket->AddObserver(testTicketObserver);
795
796   // First, load a model resource
797   application.SendNotification(); // Flush update messages
798   application.Render();           // Process resource request
799   Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
800   Internal::ModelData& modelDataImpl = GetImplementation(modelData);
801   Integration::ResourcePointer resourcePtr(&modelDataImpl);
802   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
803   application.Render();           // Process LoadComplete
804   application.SendNotification(); // Process event messages
805   DALI_TEST_CHECK(ticket->GetLoadingState() == ResourceLoadingSucceeded);
806   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
807
808   Internal::ResourceTypePath typePath(Integration::ModelResourceType(), "");
809   Internal::ResourceTicketPtr aTicket = new Internal::ResourceTicket( testTicketLifetimeObserver, 2000,  typePath );
810   try
811   {
812     resourceClient.SaveResource( aTicket, "model.dali-bin" ); // Should be outside range of valid resources!
813   }
814   catch (DaliException& e)
815   {
816     // Tests that a negative test of an assertion succeeds
817     DALI_TEST_PRINT_ASSERT( e );
818     DALI_TEST_ASSERT(e, "mImpl->mTickets.find(ticket->GetId()) != mImpl->mTickets.end()", TEST_LOCATION );
819   }
820
821   application.SendNotification(); // Flush update messages
822   application.Render();           // Process save resource request
823
824   // Ensure no request sent to platform abstraction
825   DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::SaveResourceFunc ) );
826   req = application.GetPlatform().GetRequest();
827   DALI_TEST_CHECK ( req == NULL );
828   END_TEST;
829 }
830
831 int UtcDaliInternalSaveResource03(void)
832 {
833   TestApplication application;
834   tet_infoline("Testing SaveResource() with invalid id");
835   testTicketObserver.Reset();
836
837   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
838
839   Dali::ModelData modelData = BuildTreeModel();
840
841   Internal::ResourceTicketPtr ticket = resourceClient.RequestResource( Integration::ModelResourceType(), "model.dae" );
842   ticket->AddObserver(testTicketObserver);
843
844   // First, load a model resource
845   application.SendNotification(); // Flush update messages
846   application.Render();           // Process resource request
847   Integration::ResourceRequest* req = application.GetPlatform().GetRequest();
848   Internal::ModelData& modelDataImpl = GetImplementation(modelData);
849   Integration::ResourcePointer resourcePtr(&modelDataImpl);
850   application.GetPlatform().SetResourceLoaded(req->GetId(), req->GetType()->id, resourcePtr);
851   application.Render();           // Process LoadComplete
852   application.SendNotification(); // Process event messages
853   DALI_TEST_CHECK(ticket->GetLoadingState() == ResourceLoadingSucceeded);
854   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
855
856   Internal::ResourceTicketPtr aTicket;
857   try
858   {
859     resourceClient.SaveResource( aTicket, "model.dali-bin" ); // Should be outside range of valid resources!
860   }
861   catch (DaliException& e)
862   {
863     // Tests that a negative test of an assertion succeeds
864     DALI_TEST_PRINT_ASSERT( e );
865     DALI_TEST_EQUALS(e.condition, "ticket", TEST_LOCATION);
866   }
867
868   application.SendNotification(); // Flush update messages
869   application.Render();           // Process save resource request
870
871   // Ensure no request sent to platform abstraction
872   DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::SaveResourceFunc ) );
873   req = application.GetPlatform().GetRequest();
874   DALI_TEST_CHECK ( req == NULL );
875   END_TEST;
876 }
877
878
879 int UtcDaliInternalSaveResource04(void)
880 {
881   TestApplication application;
882   tet_infoline("Testing SaveResource() with valid id, but invalid filename");
883   testTicketObserver.Reset();
884
885   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
886
887   // First, load a model resource
888   Internal::ResourceTicketPtr ticket = CheckLoadModel(application, "model.dae");
889
890   resourceClient.SaveResource( ticket, "model.dali-bin" );
891   application.SendNotification(); // Flush update messages
892   application.Render();           // Process save resource request
893
894   // Set up fail response
895   application.GetPlatform().SetResourceSaveFailed(ticket->GetId(), Integration::FailureInvalidPath );
896   application.Render();           // GetResources --> SaveFailed
897   application.SendNotification(); // Send event messages
898
899   DALI_TEST_CHECK( testTicketObserver.SaveSucceededCalled() == 0 );
900   DALI_TEST_CHECK( testTicketObserver.SaveFailedCalled() == 1 );
901   DALI_TEST_EQUALS( ticket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION )
902 ;
903   END_TEST;
904 }
905
906 int UtcDaliInternalSaveResource05(void)
907 {
908   TestApplication application;
909   tet_infoline("Testing SaveResource() with valid id, but invalid resource type");
910   testTicketObserver.Reset();
911
912   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
913
914   // First, load a bitmap resource
915   Internal::ResourceTicketPtr ticket = CheckLoadBitmap(application, "bitmap.jpg", 80, 80);
916
917   // Try saving it
918   resourceClient.SaveResource( ticket, "bitmap.png" );
919   application.SendNotification(); // Flush update messages
920   application.Render();           // Process save resource request
921
922   DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::SaveResourceFunc ) );
923   END_TEST;
924 }
925
926 int UtcDaliInternalRequestResourceTicket01(void)
927 {
928   TestApplication application;
929   tet_infoline("Testing RequestResourceTicket() with valid id");
930
931   testTicketObserver.Reset();
932
933   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
934
935   // First, load a bitmap resource
936   Internal::ResourceTicketPtr ticket = CheckLoadBitmap(application, "bitmap.jpg", 80, 80);
937
938   Internal::ResourceTicketPtr newTicket = resourceClient.RequestResourceTicket( ticket->GetId() );
939   DALI_TEST_CHECK( newTicket );
940   DALI_TEST_CHECK( newTicket->GetId() == ticket->GetId() );
941   DALI_TEST_CHECK( newTicket->GetTypePath().type->id == ticket->GetTypePath().type->id );
942   END_TEST;
943 }
944
945 int UtcDaliInternalRequestResourceTicket02(void)
946 {
947   TestApplication application;
948   tet_infoline("Testing RequestResourceTicket() with invalid id");
949
950   testTicketObserver.Reset();
951
952   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
953
954   // First, load a bitmap resource
955   Internal::ResourceTicketPtr ticket = CheckLoadBitmap(application, "bitmap.jpg", 80, 80);
956
957   Internal::ResourceTicketPtr newTicket = resourceClient.RequestResourceTicket( ticket->GetId() + 2000 );
958   DALI_TEST_CHECK( ! newTicket );
959   END_TEST;
960 }
961
962 int UtcDaliInternalLoadShaderRequest01(void)
963 {
964   TestApplication application;
965   tet_infoline("Testing LoadShader() success");
966   testTicketObserver.Reset();
967
968   // Clear through all of the outstanding shader load requests from the default shader effect
969   std::vector< unsigned char > buffer;
970   for( int i=0; i<10; i++ )
971   {
972     buffer.push_back((unsigned char)i);
973   }
974   application.GetPlatform().SetLoadFileResult( true, buffer );
975   application.GetGlAbstraction().SetLinkStatus(1);
976   application.SendNotification(); // Flush update messages
977   application.Render();           // Process load shader request (immediately)
978   application.SendNotification();
979   application.Render();
980   application.SendNotification();
981   application.Render();
982   application.SendNotification();
983
984   Internal::ResourceClient& resourceClient = Internal::ThreadLocalStorage::Get().GetResourceClient();
985
986   Integration::ShaderResourceType shaderRequest(123, "vertex src", "frag src");
987   std::string shaderBinaryFile("shader.bin");
988   Internal::ResourceTicketPtr ticket = resourceClient.LoadShader(shaderRequest, shaderBinaryFile);
989   DALI_TEST_CHECK( ticket );
990
991   application.GetPlatform().SetLoadFileResult( true, buffer );
992   application.GetGlAbstraction().EnableShaderCallTrace( true );
993   application.GetGlAbstraction().SetLinkStatus(1);
994
995   application.SendNotification(); // Flush update messages
996   application.Render();           // Process load shader request (immediately)
997
998   application.SendNotification();
999   application.Render();
1000
1001   application.SendNotification();
1002   application.Render();
1003
1004   // If shader program loads OK, we shouldn't see any calls to CompileShader or SaveResource
1005   TraceCallStack& shaderTrace = application.GetGlAbstraction().GetShaderTrace();
1006   DALI_TEST_CHECK( ! shaderTrace.FindMethod("CompileShader") );
1007
1008   // Ensure no request sent to platform abstraction
1009   DALI_TEST_CHECK( ! application.GetPlatform().WasCalled(TestPlatformAbstraction::SaveResourceFunc ) );
1010   END_TEST;
1011 }
1012
1013 int UtcDaliInternalAllocateBitmapImage01(void)
1014 {
1015   TestApplication application;
1016   tet_infoline("Testing AllocateBitmapImage()");
1017   testTicketObserver.Reset();
1018
1019   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1020   Internal::ImageTicketPtr imageTicket = resourceClient.AllocateBitmapImage(80, 80, 80, 80, Pixel::RGB565);
1021   imageTicket->AddObserver( testTicketObserver );
1022
1023   DALI_TEST_CHECK( imageTicket );
1024   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1025   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
1026   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
1027   DALI_TEST_EQUALS ( imageTicket->GetAttributes().GetPixelFormat(), Pixel::RGB565, TEST_LOCATION );
1028
1029   application.SendNotification(); // Flush update queue
1030   application.Render(0); // Process message
1031   application.SendNotification(); // Send message to tickets
1032
1033   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
1034   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1035   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
1036   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
1037   DALI_TEST_EQUALS ( imageTicket->GetAttributes().GetPixelFormat(), Pixel::RGB565, TEST_LOCATION );
1038
1039   Integration::Bitmap* bitmap = resourceClient.GetBitmap(imageTicket);
1040   DALI_TEST_CHECK ( bitmap != NULL );
1041   DALI_TEST_EQUALS ( bitmap->GetImageWidth(), 80u, TEST_LOCATION );
1042   DALI_TEST_EQUALS ( bitmap->GetImageHeight(), 80u, TEST_LOCATION );
1043   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferWidth(), 80u, TEST_LOCATION );
1044   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferHeight(), 80u, TEST_LOCATION );
1045   DALI_TEST_EQUALS ( bitmap->GetPixelFormat(), Pixel::RGB565, TEST_LOCATION );
1046   END_TEST;
1047 }
1048
1049 int UtcDaliInternalAddBitmapImage01(void)
1050 {
1051   TestApplication application;
1052   tet_infoline("Testing AddBitmap with reserved buffer()");
1053   testTicketObserver.Reset();
1054   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1055   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,  ResourcePolicy::RETAIN  );
1056   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGB565, 80, 80, 80, 80 );
1057
1058   Internal::ImageTicketPtr imageTicket = resourceClient.AddBitmapImage( bitmap );
1059   DALI_TEST_CHECK( imageTicket );
1060   imageTicket->AddObserver( testTicketObserver );
1061
1062   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1063   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
1064   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
1065   DALI_TEST_EQUALS ( imageTicket->GetAttributes().GetPixelFormat(), Pixel::RGB565, TEST_LOCATION );
1066
1067   application.SendNotification(); // Flush update queue
1068   application.Render(0); // Process message
1069   application.SendNotification(); // Send message to tickets
1070
1071   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
1072   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1073   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
1074   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
1075   DALI_TEST_EQUALS ( imageTicket->GetAttributes().GetPixelFormat(), Pixel::RGB565, TEST_LOCATION );
1076
1077   Integration::Bitmap* theBitmap = resourceClient.GetBitmap(imageTicket);
1078   DALI_TEST_CHECK ( theBitmap != NULL );
1079   DALI_TEST_CHECK ( bitmap == theBitmap );
1080   DALI_TEST_EQUALS ( bitmap->GetImageWidth(), 80u, TEST_LOCATION );
1081   DALI_TEST_EQUALS ( bitmap->GetImageHeight(), 80u, TEST_LOCATION );
1082   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferWidth(), 80u, TEST_LOCATION );
1083   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferHeight(), 80u, TEST_LOCATION );
1084   DALI_TEST_EQUALS ( bitmap->GetPixelFormat(), Pixel::RGB565, TEST_LOCATION );
1085   END_TEST;
1086 }
1087
1088 int UtcDaliInternalAddBitmapImage02(void)
1089 {
1090   TestApplication application;
1091   tet_infoline("Testing AddBitmap without reserved buffer()");
1092   testTicketObserver.Reset();
1093   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1094   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,  ResourcePolicy::RETAIN  );
1095
1096   Internal::ImageTicketPtr imageTicket = resourceClient.AddBitmapImage( bitmap );
1097   DALI_TEST_CHECK( imageTicket );
1098   imageTicket->AddObserver( testTicketObserver );
1099
1100   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1101   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 0, TEST_LOCATION );
1102   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 0, TEST_LOCATION );
1103   DALI_TEST_EQUALS ( imageTicket->GetAttributes().GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION );
1104   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
1105
1106   application.SendNotification(); // Flush update queue
1107   application.Render(0); // Process message
1108   application.SendNotification(); // Send message to tickets
1109
1110   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1111   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 0, TEST_LOCATION );
1112   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 0, TEST_LOCATION );
1113   DALI_TEST_EQUALS ( imageTicket->GetAttributes().GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION );
1114   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
1115
1116   Integration::Bitmap* theBitmap = resourceClient.GetBitmap(imageTicket);
1117   DALI_TEST_CHECK ( theBitmap != NULL );
1118   DALI_TEST_CHECK ( bitmap == theBitmap );
1119   DALI_TEST_EQUALS ( bitmap->GetImageWidth(), 0u, TEST_LOCATION );
1120   DALI_TEST_EQUALS ( bitmap->GetImageHeight(), 0u, TEST_LOCATION );
1121   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferWidth(), 0u, TEST_LOCATION );
1122   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferHeight(), 0u, TEST_LOCATION );
1123   DALI_TEST_EQUALS ( bitmap->GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION );
1124
1125   // There is no way for the ticket's image attributes to be updated if the bitmap
1126   // reserves a buffer after ticket generation.
1127   // Probably not an issue - there is no public API in BufferImage to change the image size.
1128   END_TEST;
1129 }
1130
1131
1132 int UtcDaliInternalAddBitmapImage03(void)
1133 {
1134   TestApplication application;
1135   tet_infoline("Testing AddBitmap() with invalid bitmap");
1136   testTicketObserver.Reset();
1137
1138   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1139   Internal::ImageTicketPtr imageTicket;
1140   bool exceptionRaised=false;
1141   try
1142   {
1143     imageTicket = resourceClient.AddBitmapImage( NULL );
1144   }
1145   catch( DaliException& e )
1146   {
1147     exceptionRaised = true;
1148   }
1149   DALI_TEST_CHECK( exceptionRaised );
1150   DALI_TEST_CHECK( ! imageTicket );
1151   END_TEST;
1152 }
1153
1154 int UtcDaliInternalGetBitmapImage01(void)
1155 {
1156   TestApplication application;
1157   tet_infoline("Testing GetBitmap() with valid ticket");
1158   testTicketObserver.Reset();
1159
1160   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1161   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,  ResourcePolicy::RETAIN  );
1162   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 20, 20, 80, 80 );
1163   Internal::ImageTicketPtr imageTicket = resourceClient.AddBitmapImage( bitmap );
1164
1165   Integration::Bitmap* theBitmap = resourceClient.GetBitmap(imageTicket);
1166   DALI_TEST_CHECK ( theBitmap != NULL );
1167   DALI_TEST_CHECK ( bitmap == theBitmap );
1168   DALI_TEST_EQUALS ( bitmap->GetImageWidth(),   20u, TEST_LOCATION );
1169   DALI_TEST_EQUALS ( bitmap->GetImageHeight(),  20u, TEST_LOCATION );
1170   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferWidth(),  80u, TEST_LOCATION );
1171   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferHeight(), 80u, TEST_LOCATION );
1172   DALI_TEST_EQUALS ( bitmap->GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION );
1173
1174   imageTicket->AddObserver( testTicketObserver );
1175   application.SendNotification(); // Flush update queue
1176   application.Render(0);          // Process message
1177   application.SendNotification(); // Send message to tickets
1178
1179   theBitmap = resourceClient.GetBitmap(imageTicket);
1180   DALI_TEST_CHECK ( theBitmap != NULL );
1181   DALI_TEST_CHECK ( bitmap == theBitmap );
1182   DALI_TEST_EQUALS ( bitmap->GetImageWidth(),   20u, TEST_LOCATION );
1183   DALI_TEST_EQUALS ( bitmap->GetImageHeight(),  20u, TEST_LOCATION );
1184   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferWidth(),  80u, TEST_LOCATION );
1185   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferHeight(), 80u, TEST_LOCATION );
1186   DALI_TEST_EQUALS ( bitmap->GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION );
1187   END_TEST;
1188 }
1189
1190 int UtcDaliInternalGetBitmapImage02(void)
1191 {
1192   TestApplication application;
1193   tet_infoline("Testing GetBitmap() with invalid ticket");
1194
1195   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1196   Internal::ImageTicketPtr imageTicket;
1197   Integration::Bitmap* theBitmap = NULL;
1198   bool exceptionRaised = false;
1199   try
1200   {
1201     theBitmap = resourceClient.GetBitmap(imageTicket);
1202   } catch (DaliException& e)
1203   {
1204     exceptionRaised = true;
1205   }
1206   DALI_TEST_CHECK( exceptionRaised );
1207   DALI_TEST_CHECK( ! theBitmap );
1208   END_TEST;
1209 }
1210
1211 int UtcDaliInternalGetBitmapImage03(void)
1212 {
1213   TestApplication application;
1214   tet_infoline("Testing GetBitmap() with valid ticket for incorrect type");
1215
1216   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();  Internal::ResourceTicketPtr imageTicket = CheckLoadBitmap( application, "Stuff.png", 100, 100 );
1217   Internal::ResourceTicketPtr modelTicket = CheckLoadModel( application, "Stuff.dae");
1218
1219   Integration::Bitmap* theBitmap = NULL;
1220   theBitmap = resourceClient.GetBitmap(imageTicket);
1221   DALI_TEST_CHECK( ! theBitmap );
1222
1223   theBitmap = resourceClient.GetBitmap(modelTicket);
1224   DALI_TEST_CHECK( ! theBitmap );
1225   END_TEST;
1226 }
1227
1228 int UtcDaliInternalAllocateTexture01(void)
1229 {
1230   TestApplication application;
1231   tet_infoline("Testing AllocateTexture()");
1232   testTicketObserver.Reset();
1233
1234   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1235   Internal::ResourceTicketPtr resourceTicket = resourceClient.AllocateTexture(80, 80, Pixel::L8 );
1236   resourceTicket->AddObserver( testTicketObserver );
1237
1238   DALI_TEST_CHECK( resourceTicket );
1239   DALI_TEST_EQUALS ( resourceTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1240   DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
1241
1242   application.SendNotification(); // Flush update queue
1243   application.Render(0); // Process message
1244   application.SendNotification(); // Send message to tickets
1245
1246   DALI_TEST_EQUALS ( resourceTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1247   DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
1248   END_TEST;
1249 }
1250
1251 int UtcDaliInternalAddNativeImage(void)
1252 {
1253   TestApplication application;
1254   tet_infoline("Testing AddNativeImage()");
1255
1256   testTicketObserver.Reset();
1257   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1258   Internal::ResourceTicketPtr ticket;
1259   Internal::ImageTicketPtr imageTicket;
1260   { // Test image going out of scope after ticket creation (message to Update thread holds a ref)
1261     TestNativeImagePointer nativeImage = TestNativeImage::New( 80, 80 );
1262     ticket = resourceClient.AddNativeImage( *nativeImage );
1263     imageTicket = dynamic_cast<Internal::ImageTicket*>(ticket.Get());
1264     DALI_TEST_CHECK( imageTicket );
1265     imageTicket->AddObserver( testTicketObserver );
1266   }
1267
1268   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1269   DALI_TEST_EQUALS ( imageTicket->GetWidth(),  80, TEST_LOCATION );
1270   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
1271   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
1272
1273   application.SendNotification(); // Flush update queue
1274   application.Render(0); // Process message
1275   application.SendNotification(); // Send message to tickets
1276
1277   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1278   DALI_TEST_EQUALS ( imageTicket->GetWidth(),  80, TEST_LOCATION );
1279   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
1280   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
1281
1282   Integration::Bitmap* theBitmap = NULL;
1283   theBitmap = resourceClient.GetBitmap(imageTicket);
1284
1285   DALI_TEST_CHECK ( theBitmap == NULL );
1286   END_TEST;
1287 }
1288
1289 int UtcDaliInternalAddFrameBufferImage(void)
1290 {
1291   TestApplication application;
1292   tet_infoline("Testing AddFrameBufferImage()");
1293
1294   testTicketObserver.Reset();
1295   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1296   Internal::ImageTicketPtr imageTicket = resourceClient.AddFrameBufferImage(80, 80, Pixel::A8 );
1297   DALI_TEST_CHECK( imageTicket );
1298   imageTicket->AddObserver( testTicketObserver );
1299
1300   DALI_TEST_EQUALS ( imageTicket->GetWidth(),  80, TEST_LOCATION );
1301   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
1302   DALI_TEST_EQUALS ( imageTicket->GetAttributes().GetPixelFormat(), Pixel::A8, TEST_LOCATION );
1303   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1304   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
1305
1306   application.SendNotification(); // Flush update queue
1307   application.Render(0); // Process message
1308   application.SendNotification(); // Send message to tickets
1309
1310   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1311   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
1312   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
1313   DALI_TEST_EQUALS ( imageTicket->GetAttributes().GetPixelFormat(), Pixel::A8, TEST_LOCATION );
1314   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
1315
1316   Integration::Bitmap* theBitmap = NULL;
1317   theBitmap = resourceClient.GetBitmap(imageTicket);
1318   DALI_TEST_CHECK ( theBitmap == NULL );
1319   END_TEST;
1320 }
1321
1322 int UtcDaliInternalAllocateMesh01(void)
1323 {
1324   TestApplication application;
1325   tet_infoline("Testing AllocateMesh() with vald mesh data");
1326
1327   MeshData publicMeshData;
1328   MeshData::VertexContainer    vertices;
1329   MeshData::FaceIndices        faces;
1330   BoneContainer                bones;
1331   ConstructVertices(vertices, 60);
1332   ConstructFaces(vertices, faces);
1333   Material customMaterial = ConstructMaterial();
1334   publicMeshData.SetData(vertices, faces, bones, customMaterial);
1335   publicMeshData.SetHasNormals(true);
1336   publicMeshData.SetHasTextureCoords(true);
1337
1338   testTicketObserver.Reset();
1339   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1340   Internal::OwnerPointer<Internal::MeshData> meshDataPtr( new Internal::MeshData( publicMeshData, ResourcePolicy::DISCARD, true ) );
1341   Internal::ResourceTicketPtr meshTicket = resourceClient.AllocateMesh(meshDataPtr);
1342   DALI_TEST_CHECK( meshTicket );
1343   meshTicket->AddObserver( testTicketObserver );
1344
1345   DALI_TEST_EQUALS ( meshTicket->GetLoadingState(), ResourceLoading, TEST_LOCATION );
1346
1347   application.SendNotification(); // Flush update queue
1348   application.Render(0); // Process message
1349   application.SendNotification(); // Send message to tickets
1350
1351   DALI_TEST_EQUALS ( meshTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1352   END_TEST;
1353 }