Merge remote-tracking branch 'origin/tizen' into new_text
[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
1028   application.SendNotification(); // Flush update queue
1029   application.Render(0); // Process message
1030   application.SendNotification(); // Send message to tickets
1031
1032   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
1033   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1034   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
1035   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
1036
1037   Integration::Bitmap* bitmap = resourceClient.GetBitmap(imageTicket);
1038   DALI_TEST_CHECK ( bitmap != NULL );
1039   DALI_TEST_EQUALS ( bitmap->GetImageWidth(), 80u, TEST_LOCATION );
1040   DALI_TEST_EQUALS ( bitmap->GetImageHeight(), 80u, TEST_LOCATION );
1041   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferWidth(), 80u, TEST_LOCATION );
1042   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferHeight(), 80u, TEST_LOCATION );
1043   DALI_TEST_EQUALS ( bitmap->GetPixelFormat(), Pixel::RGB565, TEST_LOCATION );
1044   END_TEST;
1045 }
1046
1047 int UtcDaliInternalAddBitmapImage01(void)
1048 {
1049   TestApplication application;
1050   tet_infoline("Testing AddBitmap with reserved buffer()");
1051   testTicketObserver.Reset();
1052   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1053   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,  ResourcePolicy::RETAIN  );
1054   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGB565, 80, 80, 80, 80 );
1055
1056   Internal::ImageTicketPtr imageTicket = resourceClient.AddBitmapImage( bitmap );
1057   DALI_TEST_CHECK( imageTicket );
1058   imageTicket->AddObserver( testTicketObserver );
1059
1060   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1061   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
1062   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
1063
1064   application.SendNotification(); // Flush update queue
1065   application.Render(0); // Process message
1066   application.SendNotification(); // Send message to tickets
1067
1068   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
1069   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1070   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
1071   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
1072
1073   Integration::Bitmap* theBitmap = resourceClient.GetBitmap(imageTicket);
1074   DALI_TEST_CHECK ( theBitmap != NULL );
1075   DALI_TEST_CHECK ( bitmap == theBitmap );
1076   DALI_TEST_EQUALS ( bitmap->GetImageWidth(), 80u, TEST_LOCATION );
1077   DALI_TEST_EQUALS ( bitmap->GetImageHeight(), 80u, TEST_LOCATION );
1078   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferWidth(), 80u, TEST_LOCATION );
1079   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferHeight(), 80u, TEST_LOCATION );
1080   DALI_TEST_EQUALS ( bitmap->GetPixelFormat(), Pixel::RGB565, TEST_LOCATION );
1081   END_TEST;
1082 }
1083
1084 int UtcDaliInternalAddBitmapImage02(void)
1085 {
1086   TestApplication application;
1087   tet_infoline("Testing AddBitmap without reserved buffer()");
1088   testTicketObserver.Reset();
1089   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1090   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,  ResourcePolicy::RETAIN  );
1091
1092   Internal::ImageTicketPtr imageTicket = resourceClient.AddBitmapImage( bitmap );
1093   DALI_TEST_CHECK( imageTicket );
1094   imageTicket->AddObserver( testTicketObserver );
1095
1096   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1097   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 0, TEST_LOCATION );
1098   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 0, TEST_LOCATION );
1099   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
1100
1101   application.SendNotification(); // Flush update queue
1102   application.Render(0); // Process message
1103   application.SendNotification(); // Send message to tickets
1104
1105   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1106   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 0, TEST_LOCATION );
1107   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 0, TEST_LOCATION );
1108   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
1109
1110   Integration::Bitmap* theBitmap = resourceClient.GetBitmap(imageTicket);
1111   DALI_TEST_CHECK ( theBitmap != NULL );
1112   DALI_TEST_CHECK ( bitmap == theBitmap );
1113   DALI_TEST_EQUALS ( bitmap->GetImageWidth(), 0u, TEST_LOCATION );
1114   DALI_TEST_EQUALS ( bitmap->GetImageHeight(), 0u, TEST_LOCATION );
1115   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferWidth(), 0u, TEST_LOCATION );
1116   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferHeight(), 0u, TEST_LOCATION );
1117   DALI_TEST_EQUALS ( bitmap->GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION );
1118
1119   // There is no way for the ticket's image attributes to be updated if the bitmap
1120   // reserves a buffer after ticket generation.
1121   // Probably not an issue - there is no public API in BufferImage to change the image size.
1122   END_TEST;
1123 }
1124
1125
1126 int UtcDaliInternalAddBitmapImage03(void)
1127 {
1128   TestApplication application;
1129   tet_infoline("Testing AddBitmap() with invalid bitmap");
1130   testTicketObserver.Reset();
1131
1132   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1133   Internal::ImageTicketPtr imageTicket;
1134   bool exceptionRaised=false;
1135   try
1136   {
1137     imageTicket = resourceClient.AddBitmapImage( NULL );
1138   }
1139   catch( DaliException& e )
1140   {
1141     exceptionRaised = true;
1142   }
1143   DALI_TEST_CHECK( exceptionRaised );
1144   DALI_TEST_CHECK( ! imageTicket );
1145   END_TEST;
1146 }
1147
1148 int UtcDaliInternalGetBitmapImage01(void)
1149 {
1150   TestApplication application;
1151   tet_infoline("Testing GetBitmap() with valid ticket");
1152   testTicketObserver.Reset();
1153
1154   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1155   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,  ResourcePolicy::RETAIN  );
1156   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 20, 20, 80, 80 );
1157   Internal::ImageTicketPtr imageTicket = resourceClient.AddBitmapImage( bitmap );
1158
1159   Integration::Bitmap* theBitmap = resourceClient.GetBitmap(imageTicket);
1160   DALI_TEST_CHECK ( theBitmap != NULL );
1161   DALI_TEST_CHECK ( bitmap == theBitmap );
1162   DALI_TEST_EQUALS ( bitmap->GetImageWidth(),   20u, TEST_LOCATION );
1163   DALI_TEST_EQUALS ( bitmap->GetImageHeight(),  20u, TEST_LOCATION );
1164   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferWidth(),  80u, TEST_LOCATION );
1165   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferHeight(), 80u, TEST_LOCATION );
1166   DALI_TEST_EQUALS ( bitmap->GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION );
1167
1168   imageTicket->AddObserver( testTicketObserver );
1169   application.SendNotification(); // Flush update queue
1170   application.Render(0);          // Process message
1171   application.SendNotification(); // Send message to tickets
1172
1173   theBitmap = resourceClient.GetBitmap(imageTicket);
1174   DALI_TEST_CHECK ( theBitmap != NULL );
1175   DALI_TEST_CHECK ( bitmap == theBitmap );
1176   DALI_TEST_EQUALS ( bitmap->GetImageWidth(),   20u, TEST_LOCATION );
1177   DALI_TEST_EQUALS ( bitmap->GetImageHeight(),  20u, TEST_LOCATION );
1178   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferWidth(),  80u, TEST_LOCATION );
1179   DALI_TEST_EQUALS ( bitmap->GetPackedPixelsProfile()->GetBufferHeight(), 80u, TEST_LOCATION );
1180   DALI_TEST_EQUALS ( bitmap->GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION );
1181   END_TEST;
1182 }
1183
1184 int UtcDaliInternalGetBitmapImage02(void)
1185 {
1186   TestApplication application;
1187   tet_infoline("Testing GetBitmap() with invalid ticket");
1188
1189   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1190   Internal::ImageTicketPtr imageTicket;
1191   Integration::Bitmap* theBitmap = NULL;
1192   bool exceptionRaised = false;
1193   try
1194   {
1195     theBitmap = resourceClient.GetBitmap(imageTicket);
1196   } catch (DaliException& e)
1197   {
1198     exceptionRaised = true;
1199   }
1200   DALI_TEST_CHECK( exceptionRaised );
1201   DALI_TEST_CHECK( ! theBitmap );
1202   END_TEST;
1203 }
1204
1205 int UtcDaliInternalGetBitmapImage03(void)
1206 {
1207   TestApplication application;
1208   tet_infoline("Testing GetBitmap() with valid ticket for incorrect type");
1209
1210   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();  Internal::ResourceTicketPtr imageTicket = CheckLoadBitmap( application, "Stuff.png", 100, 100 );
1211   Internal::ResourceTicketPtr modelTicket = CheckLoadModel( application, "Stuff.dae");
1212
1213   Integration::Bitmap* theBitmap = NULL;
1214   theBitmap = resourceClient.GetBitmap(imageTicket);
1215   DALI_TEST_CHECK( ! theBitmap );
1216
1217   theBitmap = resourceClient.GetBitmap(modelTicket);
1218   DALI_TEST_CHECK( ! theBitmap );
1219   END_TEST;
1220 }
1221
1222 int UtcDaliInternalAllocateTexture01(void)
1223 {
1224   TestApplication application;
1225   tet_infoline("Testing AllocateTexture()");
1226   testTicketObserver.Reset();
1227
1228   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1229   Internal::ResourceTicketPtr resourceTicket = resourceClient.AllocateTexture(80, 80, Pixel::L8 );
1230   resourceTicket->AddObserver( testTicketObserver );
1231
1232   DALI_TEST_CHECK( resourceTicket );
1233   DALI_TEST_EQUALS ( resourceTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1234   DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
1235
1236   application.SendNotification(); // Flush update queue
1237   application.Render(0); // Process message
1238   application.SendNotification(); // Send message to tickets
1239
1240   DALI_TEST_EQUALS ( resourceTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1241   DALI_TEST_CHECK( testTicketObserver.LoadSucceededCalled() == 0 );
1242   END_TEST;
1243 }
1244
1245 int UtcDaliInternalAddNativeImage(void)
1246 {
1247   TestApplication application;
1248   tet_infoline("Testing AddNativeImage()");
1249
1250   testTicketObserver.Reset();
1251   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1252   Internal::ResourceTicketPtr ticket;
1253   Internal::ImageTicketPtr imageTicket;
1254   { // Test image going out of scope after ticket creation (message to Update thread holds a ref)
1255     TestNativeImagePointer nativeImage = TestNativeImage::New( 80, 80 );
1256     ticket = resourceClient.AddNativeImage( *nativeImage );
1257     imageTicket = dynamic_cast<Internal::ImageTicket*>(ticket.Get());
1258     DALI_TEST_CHECK( imageTicket );
1259     imageTicket->AddObserver( testTicketObserver );
1260   }
1261
1262   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1263   DALI_TEST_EQUALS ( imageTicket->GetWidth(),  80, TEST_LOCATION );
1264   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
1265   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
1266
1267   application.SendNotification(); // Flush update queue
1268   application.Render(0); // Process message
1269   application.SendNotification(); // Send message to tickets
1270
1271   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1272   DALI_TEST_EQUALS ( imageTicket->GetWidth(),  80, TEST_LOCATION );
1273   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
1274   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
1275
1276   Integration::Bitmap* theBitmap = NULL;
1277   theBitmap = resourceClient.GetBitmap(imageTicket);
1278
1279   DALI_TEST_CHECK ( theBitmap == NULL );
1280   END_TEST;
1281 }
1282
1283 int UtcDaliInternalAddFrameBufferImage(void)
1284 {
1285   TestApplication application;
1286   tet_infoline("Testing AddFrameBufferImage()");
1287
1288   testTicketObserver.Reset();
1289   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1290   Internal::ImageTicketPtr imageTicket = resourceClient.AddFrameBufferImage(80, 80, Pixel::A8 );
1291   DALI_TEST_CHECK( imageTicket );
1292   imageTicket->AddObserver( testTicketObserver );
1293
1294   DALI_TEST_EQUALS ( imageTicket->GetWidth(),  80, TEST_LOCATION );
1295   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
1296   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1297   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
1298
1299   application.SendNotification(); // Flush update queue
1300   application.Render(0); // Process message
1301   application.SendNotification(); // Send message to tickets
1302
1303   DALI_TEST_EQUALS ( imageTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1304   DALI_TEST_EQUALS ( imageTicket->GetWidth(), 80, TEST_LOCATION );
1305   DALI_TEST_EQUALS ( imageTicket->GetHeight(), 80, TEST_LOCATION );
1306   DALI_TEST_CHECK ( 0 == testTicketObserver.LoadSucceededCalled() ); // Check no message was sent
1307
1308   Integration::Bitmap* theBitmap = NULL;
1309   theBitmap = resourceClient.GetBitmap(imageTicket);
1310   DALI_TEST_CHECK ( theBitmap == NULL );
1311   END_TEST;
1312 }
1313
1314 int UtcDaliInternalAllocateMesh01(void)
1315 {
1316   TestApplication application;
1317   tet_infoline("Testing AllocateMesh() with vald mesh data");
1318
1319   MeshData publicMeshData;
1320   MeshData::VertexContainer    vertices;
1321   MeshData::FaceIndices        faces;
1322   BoneContainer                bones;
1323   ConstructVertices(vertices, 60);
1324   ConstructFaces(vertices, faces);
1325   Material customMaterial = ConstructMaterial();
1326   publicMeshData.SetData(vertices, faces, bones, customMaterial);
1327   publicMeshData.SetHasNormals(true);
1328   publicMeshData.SetHasTextureCoords(true);
1329
1330   testTicketObserver.Reset();
1331   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
1332   Internal::OwnerPointer<Internal::MeshData> meshDataPtr( new Internal::MeshData( publicMeshData, ResourcePolicy::DISCARD, true ) );
1333   Internal::ResourceTicketPtr meshTicket = resourceClient.AllocateMesh(meshDataPtr);
1334   DALI_TEST_CHECK( meshTicket );
1335   meshTicket->AddObserver( testTicketObserver );
1336
1337   DALI_TEST_EQUALS ( meshTicket->GetLoadingState(), ResourceLoading, TEST_LOCATION );
1338
1339   application.SendNotification(); // Flush update queue
1340   application.Render(0); // Process message
1341   application.SendNotification(); // Send message to tickets
1342
1343   DALI_TEST_EQUALS ( meshTicket->GetLoadingState(), ResourceLoadingSucceeded, TEST_LOCATION );
1344   END_TEST;
1345 }