Fix randomly failed UTC
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-TextureManager.cpp
1 /*
2  * Copyright (c) 2023 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
22 #include <dali-toolkit-test-suite-utils.h>
23 #include <toolkit-event-thread-callback.h>
24 #include <toolkit-timer.h>
25
26 #include <dali-toolkit/internal/texture-manager/texture-async-loading-helper.h>
27 #include <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
28 #include <dali-toolkit/internal/texture-manager/texture-upload-observer.h>
29 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
30 #include <dali-toolkit/internal/visuals/visual-factory-impl.h> ///< For VisualFactory's member TextureManager.
31 #include <dali-toolkit/public-api/image-loader/image-url.h>
32 #include <dali-toolkit/public-api/image-loader/image.h>
33 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
34
35 #include <test-encoded-image-buffer.h>
36
37 #if defined(ELDBUS_ENABLED)
38 #include <automated-tests/src/dali-toolkit-internal/dali-toolkit-test-utils/dbus-wrapper.h>
39 #endif
40
41 using namespace Dali::Toolkit::Internal;
42
43 void utc_dali_toolkit_texture_manager_startup(void)
44 {
45   setenv("LOG_TEXTURE_MANAGER", "3", 1);
46   test_return_value = TET_UNDEF;
47 #if defined(ELDBUS_ENABLED)
48   DBusWrapper::Install(std::unique_ptr<DBusWrapper>(new TestDBusWrapper));
49 #endif
50 }
51
52 void utc_dali_toolkit_texture_manager_cleanup(void)
53 {
54   test_return_value = TET_PASS;
55 }
56
57 namespace
58 {
59 const char* TEST_IMAGE_FILE_NAME   = TEST_RESOURCE_DIR "/gallery-small-1.jpg";
60 const char* TEST_IMAGE_2_FILE_NAME = TEST_RESOURCE_DIR "/icon-delete.png";
61 const char* TEST_IMAGE_3_FILE_NAME = TEST_RESOURCE_DIR "/icon-edit.png";
62 const char* TEST_IMAGE_4_FILE_NAME = TEST_RESOURCE_DIR "/application-icon-20.png";
63 const char* TEST_MASK_FILE_NAME    = TEST_RESOURCE_DIR "/mask.png";
64
65 class TestObserver : public Dali::Toolkit::TextureUploadObserver
66 {
67 public:
68   enum class CompleteType
69   {
70     NOT_COMPLETED = 0,
71     UPLOAD_COMPLETE,
72     LOAD_COMPLETE
73   };
74
75 public:
76   TestObserver()
77   : mCompleteType(CompleteType::NOT_COMPLETED),
78     mLoaded(false),
79     mObserverCalled(false),
80     mTextureSet()
81   {
82   }
83
84   virtual void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override
85   {
86     if(textureInformation.returnType == TextureUploadObserver::ReturnType::TEXTURE)
87     {
88       mCompleteType = CompleteType::UPLOAD_COMPLETE;
89     }
90     else
91     {
92       mCompleteType = CompleteType::LOAD_COMPLETE;
93     }
94     mLoaded         = loadSuccess;
95     mObserverCalled = true;
96     mTextureSet     = textureInformation.textureSet;
97   }
98
99   CompleteType mCompleteType;
100   bool         mLoaded;
101   bool         mObserverCalled;
102   TextureSet   mTextureSet;
103 };
104
105 class TestObserverRemoveAndGenerateUrl : public TestObserver
106 {
107 public:
108   TestObserverRemoveAndGenerateUrl(TextureManager* textureManagerPtr)
109   : TestObserver(),
110     mTextureManagerPtr(textureManagerPtr)
111   {
112   }
113
114   virtual void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override
115   {
116     if(textureInformation.returnType == TextureUploadObserver::ReturnType::TEXTURE)
117     {
118       mCompleteType = CompleteType::UPLOAD_COMPLETE;
119     }
120     else
121     {
122       mCompleteType = CompleteType::LOAD_COMPLETE;
123     }
124     mLoaded         = loadSuccess;
125     mObserverCalled = true;
126     mTextureSet     = textureInformation.textureSet;
127
128     // Remove during LoadComplete
129     mTextureManagerPtr->RequestRemove(textureInformation.textureId, nullptr);
130
131     // ...And generate string which using texture id.
132     mGeneratedExternalUrl = mTextureManagerPtr->AddExternalTexture(mTextureSet);
133   }
134
135 public:
136   std::string mGeneratedExternalUrl;
137
138 protected:
139   TextureManager* mTextureManagerPtr; // Keep the pointer of texture manager.
140 };
141
142 class TestObserverWithCustomFunction : public TestObserver
143 {
144 public:
145   TestObserverWithCustomFunction()
146   : TestObserver(),
147     mSignals{},
148     mData{nullptr},
149     mKeepSignal{false}
150   {
151   }
152
153   virtual void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override
154   {
155     if(textureInformation.returnType == TextureUploadObserver::ReturnType::TEXTURE)
156     {
157       mCompleteType = CompleteType::UPLOAD_COMPLETE;
158     }
159     else
160     {
161       mCompleteType = CompleteType::LOAD_COMPLETE;
162     }
163     mLoaded         = loadSuccess;
164     mObserverCalled = true;
165     mTextureSet     = textureInformation.textureSet;
166
167     // Execute signals.
168     for(size_t i = 0; i < mSignals.size(); i++)
169     {
170       mSignals[i](mData);
171     }
172
173     // Clear signals.
174     if(!mKeepSignal)
175     {
176       mSignals.clear();
177     }
178   }
179
180   void ConnectFunction(std::function<void(void*)> signal)
181   {
182     mSignals.push_back(signal);
183   }
184
185 public:
186   std::vector<std::function<void(void*)>> mSignals;
187   void*                                   mData;
188   bool                                    mKeepSignal;
189 };
190
191 } // namespace
192
193 int UtcTextureManagerRequestLoad(void)
194 {
195   ToolkitTestApplication application;
196
197   TextureManager textureManager; // Create new texture manager
198
199   TestObserver              observer;
200   std::string               filename("image.png");
201   auto                      preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
202   TextureManager::TextureId textureId   = textureManager.RequestLoad(
203     filename,
204     ImageDimensions(),
205     FittingMode::SCALE_TO_FILL,
206     SamplingMode::BOX_THEN_LINEAR,
207     TextureManager::UseAtlas::NO_ATLAS,
208     &observer,
209     true,
210     TextureManager::ReloadPolicy::CACHED,
211     preMultiply);
212
213   VisualUrl url = textureManager.GetVisualUrl(textureId);
214
215   DALI_TEST_EQUALS(url.GetUrl().compare(filename), 0, TEST_LOCATION);
216
217   END_TEST;
218 }
219
220 int UtcTextureManagerGenerateHash(void)
221 {
222   ToolkitTestApplication application;
223
224   TextureManager textureManager; // Create new texture manager
225
226   TestObserver              observer;
227   std::string               filename("image.png");
228   auto                      preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
229   TextureManager::TextureId textureId   = textureManager.RequestLoad(
230     filename,
231     ImageDimensions(),
232     FittingMode::SCALE_TO_FILL,
233     SamplingMode::BOX_THEN_LINEAR,
234     TextureManager::UseAtlas::NO_ATLAS,
235     &observer,
236     true,
237     TextureManager::ReloadPolicy::CACHED,
238     preMultiply);
239
240   VisualUrl url = textureManager.GetVisualUrl(textureId);
241
242   DALI_TEST_EQUALS(url.GetUrl().compare(filename), 0, TEST_LOCATION);
243
244   END_TEST;
245 }
246
247 int UtcTextureManagerEncodedImageBuffer(void)
248 {
249   ToolkitTestApplication application;
250   tet_infoline("UtcTextureManagerEncodedImageBuffer");
251
252   auto  visualFactory  = Toolkit::VisualFactory::Get();
253   auto& textureManager = GetImplementation(visualFactory).GetTextureManager(); // Use VisualFactory's texture manager
254
255   // Get encoded raw-buffer image and generate url
256   EncodedImageBuffer buffer1 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
257   EncodedImageBuffer buffer2 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
258
259   std::string url1 = textureManager.AddEncodedImageBuffer(buffer1);
260   std::string url2 = textureManager.AddEncodedImageBuffer(buffer1);
261   std::string url3 = VisualUrl::CreateBufferUrl(""); ///< Impossible Buffer URL. for coverage
262
263   // Check if same EncodedImageBuffer get same url
264   DALI_TEST_CHECK(url1 == url2);
265   // Reduce reference count
266   textureManager.RemoveEncodedImageBuffer(url1);
267   // Check whethere url1 still valid
268   DALI_TEST_CHECK(textureManager.GetEncodedImageBuffer(url1));
269
270   url2 = textureManager.AddEncodedImageBuffer(buffer2);
271   // Check if difference EncodedImageBuffer get difference url
272   DALI_TEST_CHECK(url1 != url2);
273
274   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
275
276   TestObserver observer1;
277   textureManager.RequestLoad(
278     url1,
279     ImageDimensions(),
280     FittingMode::SCALE_TO_FILL,
281     SamplingMode::BOX_THEN_LINEAR,
282     TextureManager::UseAtlas::NO_ATLAS,
283     &observer1,
284     true, ///< orientationCorrection
285     TextureManager::ReloadPolicy::CACHED,
286     preMultiply);
287
288   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
289   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
290
291   application.SendNotification();
292   application.Render();
293
294   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
295
296   application.SendNotification();
297   application.Render();
298
299   DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
300   DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
301   DALI_TEST_EQUALS(observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
302
303   TestObserver observer2;
304   // Syncload
305   Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
306     url2,
307     ImageDimensions(),
308     FittingMode::SCALE_TO_FILL,
309     SamplingMode::BOX_THEN_LINEAR,
310     true, ///< synchronousLoading
311     &observer2,
312     true, ///< orientationCorrection
313     preMultiply);
314
315   DALI_TEST_CHECK(pixelBuffer);
316   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
317   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
318
319   // Asyncload
320   pixelBuffer = textureManager.LoadPixelBuffer(
321     url2,
322     ImageDimensions(),
323     FittingMode::SCALE_TO_FILL,
324     SamplingMode::BOX_THEN_LINEAR,
325     false, ///< synchronousLoading
326     &observer2,
327     true, ///< orientationCorrection
328     preMultiply);
329
330   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
331   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
332
333   application.SendNotification();
334   application.Render();
335
336   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
337
338   application.SendNotification();
339   application.Render();
340
341   DALI_TEST_EQUALS(observer2.mLoaded, true, TEST_LOCATION);
342   DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
343   DALI_TEST_EQUALS(observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION);
344
345   textureManager.RemoveEncodedImageBuffer(url1);
346   textureManager.RemoveEncodedImageBuffer(url2);
347
348   // Now url1 and url2 is invalid type. mLoaded will return false
349
350   TestObserver observer3;
351   textureManager.RequestLoad(
352     url1,
353     ImageDimensions(),
354     FittingMode::SCALE_TO_FILL,
355     SamplingMode::BOX_THEN_LINEAR,
356     TextureManager::UseAtlas::NO_ATLAS,
357     &observer3,
358     true, ///< orientationCorrection
359     TextureManager::ReloadPolicy::CACHED,
360     preMultiply);
361
362   // Load will be success because url1 is cached
363   DALI_TEST_EQUALS(observer3.mLoaded, true, TEST_LOCATION);
364   DALI_TEST_EQUALS(observer3.mObserverCalled, true, TEST_LOCATION);
365   DALI_TEST_EQUALS(observer3.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
366
367   TestObserver observer4;
368   textureManager.RequestLoad(
369     url2,
370     ImageDimensions(),
371     FittingMode::SCALE_TO_FILL,
372     SamplingMode::BOX_THEN_LINEAR,
373     TextureManager::UseAtlas::NO_ATLAS,
374     &observer4,
375     true, ///< orientationCorrection
376     TextureManager::ReloadPolicy::FORCED,
377     preMultiply);
378
379   DALI_TEST_EQUALS(observer4.mLoaded, false, TEST_LOCATION);
380   DALI_TEST_EQUALS(observer4.mObserverCalled, false, TEST_LOCATION);
381   application.SendNotification();
382   application.Render();
383
384   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
385
386   application.SendNotification();
387   application.Render();
388
389   // Load will be failed becuase reloadpolicy is forced
390   DALI_TEST_EQUALS(observer4.mLoaded, false, TEST_LOCATION);
391   DALI_TEST_EQUALS(observer4.mObserverCalled, true, TEST_LOCATION);
392   DALI_TEST_EQUALS(observer4.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
393
394   TestObserver observer5;
395   pixelBuffer = textureManager.LoadPixelBuffer(
396     url2,
397     ImageDimensions(),
398     FittingMode::SCALE_TO_FILL,
399     SamplingMode::BOX_THEN_LINEAR,
400     true, ///< synchronousLoading
401     &observer5,
402     true, ///< orientationCorrection
403     preMultiply);
404
405   // Load will be faild because synchronousLoading doesn't use cached texture
406   DALI_TEST_CHECK(!pixelBuffer);
407   DALI_TEST_EQUALS(observer5.mLoaded, false, TEST_LOCATION);
408   DALI_TEST_EQUALS(observer5.mObserverCalled, false, TEST_LOCATION);
409
410   TestObserver observer6;
411   pixelBuffer = textureManager.LoadPixelBuffer(
412     url3,
413     ImageDimensions(),
414     FittingMode::SCALE_TO_FILL,
415     SamplingMode::BOX_THEN_LINEAR,
416     false, ///< synchronousLoading
417     &observer6,
418     true, ///< orientationCorrection
419     preMultiply);
420
421   DALI_TEST_EQUALS(observer6.mLoaded, false, TEST_LOCATION);
422   DALI_TEST_EQUALS(observer6.mObserverCalled, false, TEST_LOCATION);
423
424   application.SendNotification();
425   application.Render();
426
427   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
428
429   application.SendNotification();
430   application.Render();
431
432   // Load will be failed because url3 is invalid URL
433   DALI_TEST_EQUALS(observer6.mLoaded, false, TEST_LOCATION);
434   DALI_TEST_EQUALS(observer6.mObserverCalled, true, TEST_LOCATION);
435   DALI_TEST_EQUALS(observer6.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION);
436
437   END_TEST;
438 }
439
440 int UtcTextureManagerExternalTexture(void)
441 {
442   ToolkitTestApplication application;
443   tet_infoline("UtcTextureManagerExternalTexture check TextureManager using external texture works well");
444
445   auto  visualFactory  = Toolkit::VisualFactory::Get();
446   auto& textureManager = GetImplementation(visualFactory).GetTextureManager(); // Use VisualFactory's texture manager
447
448   TestObserver observer1;
449   TestObserver observer2;
450
451   auto                               textureId1(TextureManager::INVALID_TEXTURE_ID);
452   auto                               textureId2(TextureManager::INVALID_TEXTURE_ID);
453   std::string                        maskname(TEST_MASK_FILE_NAME);
454   TextureManager::MaskingDataPointer maskInfo = nullptr;
455   maskInfo.reset(new TextureManager::MaskingData());
456   maskInfo->mAlphaMaskUrl       = maskname;
457   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
458   maskInfo->mCropToMask         = true;
459   maskInfo->mContentScaleFactor = 1.0f;
460   Vector4                       atlasRect(0.f, 0.f, 1.f, 1.f);
461   Dali::ImageDimensions         atlasRectSize(0, 0);
462   bool                          synchronousLoading(false);
463   bool                          atlasingStatus(false);
464   bool                          loadingStatus(false);
465   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
466   ImageAtlasManagerPtr          atlasManager        = nullptr;
467   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
468
469   uint32_t width(64);
470   uint32_t height(64);
471   uint32_t bufferSize = width * height * Pixel::GetBytesPerPixel(Pixel::RGBA8888);
472
473   uint8_t*  buffer    = reinterpret_cast<uint8_t*>(malloc(bufferSize));
474   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
475
476   DALI_TEST_CHECK(pixelData);
477
478   Dali::Toolkit::ImageUrl imageUrl = Dali::Toolkit::Image::GenerateUrl(pixelData, true);
479   std::string             url      = imageUrl.GetUrl();
480
481   TextureSet texture1 = textureManager.LoadTexture(
482     url,
483     ImageDimensions(),
484     FittingMode::SCALE_TO_FILL,
485     SamplingMode::BOX_THEN_LINEAR,
486     maskInfo,
487     synchronousLoading,
488     textureId1,
489     atlasRect,
490     atlasRectSize,
491     atlasingStatus,
492     loadingStatus,
493     &observer1,
494     atlasUploadObserver,
495     atlasManager,
496     true,
497     TextureManager::ReloadPolicy::CACHED,
498     preMultiply);
499
500   TextureSet texture2 = textureManager.LoadTexture(
501     url,
502     ImageDimensions(),
503     FittingMode::SCALE_TO_FILL,
504     SamplingMode::BOX_THEN_LINEAR,
505     maskInfo,
506     synchronousLoading,
507     textureId2,
508     atlasRect,
509     atlasRectSize,
510     atlasingStatus,
511     loadingStatus,
512     &observer2,
513     atlasUploadObserver,
514     atlasManager,
515     true,
516     TextureManager::ReloadPolicy::CACHED,
517     preMultiply);
518
519   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
520   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
521   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
522   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
523
524   application.SendNotification();
525   application.Render();
526
527   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
528
529   DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
530   DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
531   DALI_TEST_EQUALS(observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
532
533   DALI_TEST_EQUALS(observer2.mLoaded, true, TEST_LOCATION);
534   DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
535   DALI_TEST_EQUALS(observer2.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
536
537   DALI_TEST_EQUALS(textureId1 == textureId2, true, TEST_LOCATION);
538
539   texture1 = textureManager.LoadTexture(
540     url,
541     ImageDimensions(),
542     FittingMode::SCALE_TO_FILL,
543     SamplingMode::BOX_THEN_LINEAR,
544     maskInfo,
545     synchronousLoading,
546     textureId1,
547     atlasRect,
548     atlasRectSize,
549     atlasingStatus,
550     loadingStatus,
551     &observer1,
552     atlasUploadObserver,
553     atlasManager,
554     true,
555     TextureManager::ReloadPolicy::CACHED,
556     preMultiply);
557
558   texture2 = textureManager.LoadTexture(
559     url,
560     ImageDimensions(),
561     FittingMode::SCALE_TO_FILL,
562     SamplingMode::BOX_THEN_LINEAR,
563     maskInfo,
564     synchronousLoading,
565     textureId2,
566     atlasRect,
567     atlasRectSize,
568     atlasingStatus,
569     loadingStatus,
570     &observer2,
571     atlasUploadObserver,
572     atlasManager,
573     true,
574     TextureManager::ReloadPolicy::CACHED,
575     preMultiply);
576
577   application.SendNotification();
578   application.Render();
579
580   DALI_TEST_EQUALS(textureId1 == textureId2, true, TEST_LOCATION);
581   DALI_TEST_EQUALS(texture1 != texture2, true, TEST_LOCATION);
582
583   END_TEST;
584 }
585
586 int UtcTextureManagerRemoveExternalTextureAndLoadAgain(void)
587 {
588   ToolkitTestApplication application;
589   tet_infoline("UtcTextureManagerRemoveExternalTextureAndLoadAgain check TextureManager RequestRemove and RemoveExternalTexture lifecycle works well");
590
591   auto  visualFactory  = Toolkit::VisualFactory::Get();
592   auto& textureManager = GetImplementation(visualFactory).GetTextureManager(); // Use VisualFactory's texture manager
593
594   TestObserver observer1;
595   TestObserver observer2;
596
597   auto                               textureId1(TextureManager::INVALID_TEXTURE_ID);
598   auto                               textureId2(TextureManager::INVALID_TEXTURE_ID);
599   TextureManager::MaskingDataPointer maskInfo = nullptr;
600
601   Vector4                       atlasRect(0.f, 0.f, 1.f, 1.f);
602   Dali::ImageDimensions         atlasRectSize(0, 0);
603   bool                          synchronousLoading(false);
604   bool                          atlasingStatus(false);
605   bool                          loadingStatus(false);
606   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
607   ImageAtlasManagerPtr          atlasManager        = nullptr;
608   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
609
610   uint32_t width(64);
611   uint32_t height(64);
612   uint32_t bufferSize = width * height * Pixel::GetBytesPerPixel(Pixel::RGBA8888);
613
614   uint8_t*  buffer    = reinterpret_cast<uint8_t*>(malloc(bufferSize));
615   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE);
616
617   DALI_TEST_CHECK(pixelData);
618
619   Dali::Toolkit::ImageUrl imageUrl = Dali::Toolkit::Image::GenerateUrl(pixelData, true);
620   std::string             url1     = imageUrl.GetUrl();
621   std::string             url2     = TEST_IMAGE_FILE_NAME;
622
623   // Step 1 : Load request for external url
624   TextureSet texture1 = textureManager.LoadTexture(
625     url1,
626     ImageDimensions(),
627     FittingMode::SCALE_TO_FILL,
628     SamplingMode::BOX_THEN_LINEAR,
629     maskInfo,
630     synchronousLoading,
631     textureId1,
632     atlasRect,
633     atlasRectSize,
634     atlasingStatus,
635     loadingStatus,
636     &observer1,
637     atlasUploadObserver,
638     atlasManager,
639     true,
640     TextureManager::ReloadPolicy::CACHED,
641     preMultiply);
642
643   DALI_TEST_CHECK(textureId1 != TextureManager::INVALID_TEXTURE_ID);
644
645   // Step 2 : Request remove for external url
646   textureManager.RequestRemove(textureId1, &observer1);
647
648   // Step 3 : Reduce imageUrl reference count.
649   imageUrl.Reset();
650
651   // Step 4 : Request new load by normal image.
652   TextureSet texture2 = textureManager.LoadTexture(
653     url2,
654     ImageDimensions(),
655     FittingMode::SCALE_TO_FILL,
656     SamplingMode::BOX_THEN_LINEAR,
657     maskInfo,
658     synchronousLoading,
659     textureId2,
660     atlasRect,
661     atlasRectSize,
662     atlasingStatus,
663     loadingStatus,
664     &observer2,
665     atlasUploadObserver,
666     atlasManager,
667     true,
668     TextureManager::ReloadPolicy::CACHED,
669     preMultiply);
670
671   DALI_TEST_CHECK(textureId2 != TextureManager::INVALID_TEXTURE_ID);
672
673   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
674   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
675   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
676   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
677
678   application.SendNotification();
679   application.Render();
680
681   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
682
683   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
684   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
685
686   DALI_TEST_EQUALS(observer2.mLoaded, true, TEST_LOCATION);
687   DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
688   DALI_TEST_EQUALS(observer2.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
689
690   END_TEST;
691 }
692
693 int UtcTextureManagerEncodedImageBufferReferenceCount(void)
694 {
695   ToolkitTestApplication application;
696   tet_infoline("UtcTextureManagerEncodedImageBuffer check reference count works well");
697
698   auto  visualFactory  = Toolkit::VisualFactory::Get();
699   auto& textureManager = GetImplementation(visualFactory).GetTextureManager(); // Use VisualFactory's texture manager
700
701   // Get encoded raw-buffer image and generate url
702   EncodedImageBuffer buffer1 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
703   EncodedImageBuffer buffer2 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
704
705   std::string url1 = textureManager.AddEncodedImageBuffer(buffer1);
706   std::string url2 = textureManager.AddEncodedImageBuffer(buffer1);
707
708   // Check if same EncodedImageBuffer get same url
709   DALI_TEST_CHECK(url1 == url2);
710
711   // Reduce reference count
712   textureManager.RemoveEncodedImageBuffer(url1);
713   // Check whethere url1 still valid
714   DALI_TEST_CHECK(textureManager.GetEncodedImageBuffer(url1));
715
716   // Reduce reference count
717   textureManager.RemoveEncodedImageBuffer(url1);
718   // Check whethere url1 is not valid anymore
719   DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url1));
720
721   // UseExternalTexture doesn't create new buffer.
722   // So, reference count is still zero.
723   textureManager.UseExternalResource(url1);
724   DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url1));
725
726   url1 = textureManager.AddEncodedImageBuffer(buffer1);
727
728   url2 = textureManager.AddEncodedImageBuffer(buffer2);
729   // Check if difference EncodedImageBuffer get difference url
730   DALI_TEST_CHECK(url1 != url2);
731
732   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
733
734   // url1 load image by cache
735   TestObserver observer1;
736   textureManager.RequestLoad(
737     url1,
738     ImageDimensions(),
739     FittingMode::SCALE_TO_FILL,
740     SamplingMode::BOX_THEN_LINEAR,
741     TextureManager::UseAtlas::NO_ATLAS,
742     &observer1,
743     true, ///< orientationCorrection
744     TextureManager::ReloadPolicy::CACHED,
745     preMultiply);
746
747   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
748   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
749
750   application.SendNotification();
751   application.Render();
752
753   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
754
755   application.SendNotification();
756   application.Render();
757
758   DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
759   DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
760   DALI_TEST_EQUALS(observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
761
762   // LoadPixelBuffer doen't use cache. url2 will not be cached
763   TestObserver       observer2;
764   Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
765     url2,
766     ImageDimensions(),
767     FittingMode::SCALE_TO_FILL,
768     SamplingMode::BOX_THEN_LINEAR,
769     false, ///< synchronousLoading
770     &observer2,
771     true, ///< orientationCorrection
772     preMultiply);
773
774   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
775   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
776
777   application.SendNotification();
778   application.Render();
779
780   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
781
782   application.SendNotification();
783   application.Render();
784
785   DALI_TEST_EQUALS(observer2.mLoaded, true, TEST_LOCATION);
786   DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
787   DALI_TEST_EQUALS(observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION);
788
789   // Decrease each url's reference count.
790   textureManager.RemoveEncodedImageBuffer(url1);
791   textureManager.RemoveEncodedImageBuffer(url2);
792
793   // url1 buffer is still have 1 reference count because it is cached.
794   // But url2 not valid because it is not cached.
795   DALI_TEST_CHECK(textureManager.GetEncodedImageBuffer(url1));
796   DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url2));
797
798   // Check url1 buffer have 1 reference count because it is cached.
799   textureManager.RemoveEncodedImageBuffer(url1);
800   DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url1));
801
802   END_TEST;
803 }
804
805 int UtcTextureManagerCachingForDifferentLoadingType(void)
806 {
807   ToolkitTestApplication application;
808   tet_infoline("UtcTextureManagerCachingForDifferentLoadingType");
809
810   TextureManager textureManager; // Create new texture manager
811
812   TestObserver observer1;
813   std::string  filename(TEST_IMAGE_FILE_NAME);
814   auto         preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
815   textureManager.RequestLoad(
816     filename,
817     ImageDimensions(),
818     FittingMode::SCALE_TO_FILL,
819     SamplingMode::BOX_THEN_LINEAR,
820     TextureManager::UseAtlas::NO_ATLAS,
821     &observer1,
822     true,
823     TextureManager::ReloadPolicy::CACHED,
824     preMultiply);
825
826   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
827   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
828
829   application.SendNotification();
830   application.Render();
831
832   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
833
834   application.SendNotification();
835   application.Render();
836
837   DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
838   DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
839   DALI_TEST_EQUALS(observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
840
841   TestObserver       observer2;
842   Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
843     filename,
844     ImageDimensions(),
845     FittingMode::SCALE_TO_FILL,
846     SamplingMode::BOX_THEN_LINEAR,
847     false,
848     &observer2,
849     true,
850     preMultiply);
851
852   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
853   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
854
855   application.SendNotification();
856   application.Render();
857
858   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
859
860   application.SendNotification();
861   application.Render();
862
863   DALI_TEST_EQUALS(observer2.mLoaded, true, TEST_LOCATION);
864   DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
865   DALI_TEST_EQUALS(observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION);
866
867   END_TEST;
868 }
869
870 int UtcTextureManagerUseInvalidMask(void)
871 {
872   ToolkitTestApplication application;
873   tet_infoline("UtcTextureManagerUseInvalidMask");
874
875   TextureManager textureManager; // Create new texture manager
876
877   TestObserver                       observer;
878   std::string                        filename(TEST_IMAGE_FILE_NAME);
879   std::string                        maskname("");
880   TextureManager::MaskingDataPointer maskInfo = nullptr;
881   maskInfo.reset(new TextureManager::MaskingData());
882   maskInfo->mAlphaMaskUrl       = maskname;
883   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
884   maskInfo->mCropToMask         = true;
885   maskInfo->mContentScaleFactor = 1.0f;
886
887   auto                          textureId(TextureManager::INVALID_TEXTURE_ID);
888   Vector4                       atlasRect(0.f, 0.f, 1.f, 1.f);
889   Dali::ImageDimensions         atlasRectSize(0, 0);
890   bool                          synchronousLoading(false);
891   bool                          atlasingStatus(false);
892   bool                          loadingStatus(false);
893   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
894   ImageAtlasManagerPtr          atlasManager        = nullptr;
895   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
896
897   textureManager.LoadTexture(
898     filename,
899     ImageDimensions(),
900     FittingMode::SCALE_TO_FILL,
901     SamplingMode::BOX_THEN_LINEAR,
902     maskInfo,
903     synchronousLoading,
904     textureId,
905     atlasRect,
906     atlasRectSize,
907     atlasingStatus,
908     loadingStatus,
909     &observer,
910     atlasUploadObserver,
911     atlasManager,
912     true,
913     TextureManager::ReloadPolicy::CACHED,
914     preMultiply);
915
916   DALI_TEST_EQUALS(observer.mLoaded, false, TEST_LOCATION);
917   DALI_TEST_EQUALS(observer.mObserverCalled, false, TEST_LOCATION);
918
919   application.SendNotification();
920   application.Render();
921
922   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
923
924   application.SendNotification();
925   application.Render();
926
927   DALI_TEST_EQUALS(observer.mLoaded, true, TEST_LOCATION);
928   DALI_TEST_EQUALS(observer.mObserverCalled, true, TEST_LOCATION);
929   DALI_TEST_EQUALS(observer.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
930
931   END_TEST;
932 }
933
934 int UtcTextureManagerUseInvalidMaskAndMaskLoadedFirst(void)
935 {
936   ToolkitTestApplication application;
937   tet_infoline("UtcTextureManagerUseInvalidMask when normal image loaded first, and mask image loaded first");
938   tet_infoline("Try to check PostLoad works well");
939
940   TextureManager textureManager; // Create new texture manager
941
942   TestObserver                       observer;
943   std::string                        filename(TEST_IMAGE_FILE_NAME);
944   std::string                        maskname("invalid.png");
945   TextureManager::MaskingDataPointer maskInfo = nullptr;
946   maskInfo.reset(new TextureManager::MaskingData());
947   maskInfo->mAlphaMaskUrl       = maskname;
948   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
949   maskInfo->mCropToMask         = true;
950   maskInfo->mContentScaleFactor = 1.0f;
951
952   auto                          textureId(TextureManager::INVALID_TEXTURE_ID);
953   Vector4                       atlasRect(0.f, 0.f, 1.f, 1.f);
954   Dali::ImageDimensions         atlasRectSize(0, 0);
955   bool                          synchronousLoading(false);
956   bool                          atlasingStatus(false);
957   bool                          loadingStatus(false);
958   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
959   ImageAtlasManagerPtr          atlasManager        = nullptr;
960   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
961
962   textureManager.LoadTexture(
963     filename,
964     ImageDimensions(),
965     FittingMode::SCALE_TO_FILL,
966     SamplingMode::BOX_THEN_LINEAR,
967     maskInfo,
968     synchronousLoading,
969     textureId,
970     atlasRect,
971     atlasRectSize,
972     atlasingStatus,
973     loadingStatus,
974     &observer,
975     atlasUploadObserver,
976     atlasManager,
977     true,
978     TextureManager::ReloadPolicy::CACHED,
979     preMultiply);
980
981   DALI_TEST_EQUALS(observer.mLoaded, false, TEST_LOCATION);
982   DALI_TEST_EQUALS(observer.mObserverCalled, false, TEST_LOCATION);
983
984   application.SendNotification();
985   application.Render();
986
987   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(2), true, TEST_LOCATION);
988
989   application.SendNotification();
990   application.Render();
991
992   DALI_TEST_EQUALS(observer.mLoaded, true, TEST_LOCATION);
993   DALI_TEST_EQUALS(observer.mObserverCalled, true, TEST_LOCATION);
994   DALI_TEST_EQUALS(observer.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
995
996   END_TEST;
997 }
998
999 int UtcTextureManagerUseInvalidMaskAndMaskLoadedLater(void)
1000 {
1001   ToolkitTestApplication application;
1002   tet_infoline("UtcTextureManagerUseInvalidMask when normal image loaded first, and mask image loaded later");
1003   tet_infoline("Try to check CheckForWaitingTexture called");
1004
1005   TextureManager textureManager; // Create new texture manager
1006
1007   TestObserver                       observer;
1008   std::string                        filename(TEST_IMAGE_FILE_NAME);
1009   std::string                        maskname("invalid.png");
1010   TextureManager::MaskingDataPointer maskInfo = nullptr;
1011   maskInfo.reset(new TextureManager::MaskingData());
1012   maskInfo->mAlphaMaskUrl       = maskname;
1013   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
1014   maskInfo->mCropToMask         = true;
1015   maskInfo->mContentScaleFactor = 1.0f;
1016
1017   auto                          textureId(TextureManager::INVALID_TEXTURE_ID);
1018   Vector4                       atlasRect(0.f, 0.f, 1.f, 1.f);
1019   Dali::ImageDimensions         atlasRectSize(0, 0);
1020   bool                          synchronousLoading(false);
1021   bool                          atlasingStatus(false);
1022   bool                          loadingStatus(false);
1023   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
1024   ImageAtlasManagerPtr          atlasManager        = nullptr;
1025   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
1026
1027   textureManager.LoadTexture(
1028     filename,
1029     ImageDimensions(),
1030     FittingMode::SCALE_TO_FILL,
1031     SamplingMode::BOX_THEN_LINEAR,
1032     maskInfo,
1033     synchronousLoading,
1034     textureId,
1035     atlasRect,
1036     atlasRectSize,
1037     atlasingStatus,
1038     loadingStatus,
1039     &observer,
1040     atlasUploadObserver,
1041     atlasManager,
1042     true,
1043     TextureManager::ReloadPolicy::CACHED,
1044     preMultiply);
1045
1046   DALI_TEST_EQUALS(observer.mLoaded, false, TEST_LOCATION);
1047   DALI_TEST_EQUALS(observer.mObserverCalled, false, TEST_LOCATION);
1048
1049   // CAPTION : HARD-CODING for coverage.
1050   {
1051     Dali::Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
1052       filename,
1053       ImageDimensions(),
1054       FittingMode::SCALE_TO_FILL,
1055       SamplingMode::BOX_THEN_LINEAR,
1056       true, ///< synchronousLoading
1057       nullptr,
1058       true, ///< orientationCorrection
1059       preMultiply);
1060
1061     std::vector<Devel::PixelBuffer> pixelBuffers;
1062     pixelBuffers.push_back(pixelBuffer);
1063     textureManager.AsyncLoadComplete(textureId, pixelBuffers);
1064     std::vector<Devel::PixelBuffer> maskBuffers;
1065     textureManager.AsyncLoadComplete(maskInfo->mAlphaMaskId, maskBuffers);
1066     textureManager.RequestRemove(maskInfo->mAlphaMaskId, nullptr);
1067     textureManager.RequestRemove(textureId, &observer);
1068   }
1069
1070   application.SendNotification();
1071   application.Render();
1072
1073   DALI_TEST_EQUALS(observer.mLoaded, true, TEST_LOCATION);
1074   DALI_TEST_EQUALS(observer.mObserverCalled, true, TEST_LOCATION);
1075   DALI_TEST_EQUALS(observer.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
1076
1077   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(2), true, TEST_LOCATION);
1078
1079   END_TEST;
1080 }
1081
1082 int UtcTextureManagerSynchronousLoadingFail(void)
1083 {
1084   ToolkitTestApplication application;
1085   tet_infoline("UtcTextureManagerSynchronousLoadingFail");
1086
1087   TextureManager textureManager; // Create new texture manager
1088
1089   std::string                        maskname("");
1090   TextureManager::MaskingDataPointer maskInfo = nullptr;
1091   maskInfo.reset(new TextureManager::MaskingData());
1092   maskInfo->mAlphaMaskUrl       = maskname;
1093   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
1094   maskInfo->mCropToMask         = true;
1095   maskInfo->mContentScaleFactor = 1.0f;
1096
1097   std::string                   filename("dummy");
1098   auto                          textureId(TextureManager::INVALID_TEXTURE_ID);
1099   Vector4                       atlasRect(0.f, 0.f, 0.f, 0.f);
1100   Dali::ImageDimensions         atlasRectSize(0, 0);
1101   bool                          atlasingStatus(false);
1102   bool                          loadingStatus(false);
1103   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
1104   ImageAtlasManagerPtr          atlasManager        = nullptr;
1105   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
1106
1107   // load image synchronously.
1108   TestObserver observer;
1109   TextureSet   textureSet = textureManager.LoadTexture(
1110     filename,
1111     ImageDimensions(),
1112     FittingMode::SCALE_TO_FILL,
1113     SamplingMode::BOX_THEN_LINEAR,
1114     maskInfo,
1115     true, // synchronous loading.
1116     textureId,
1117     atlasRect,
1118     atlasRectSize,
1119     atlasingStatus,
1120     loadingStatus,
1121     &observer,
1122     atlasUploadObserver,
1123     atlasManager,
1124     true,
1125     TextureManager::ReloadPolicy::CACHED,
1126     preMultiply);
1127
1128   DALI_TEST_EQUALS(loadingStatus, false, TEST_LOCATION);
1129   DALI_TEST_CHECK(!textureSet);                                     // texture loading fail.
1130   DALI_TEST_CHECK(textureId == TextureManager::INVALID_TEXTURE_ID); // invalid texture id is returned.
1131
1132   END_TEST;
1133 }
1134
1135 int UtcTextureManagerCachingSynchronousLoading(void)
1136 {
1137   ToolkitTestApplication application;
1138   tet_infoline("UtcTextureManagerCachingSynchronousLoading");
1139
1140   TextureManager textureManager; // Create new texture manager
1141
1142   std::string filename(TEST_IMAGE_FILE_NAME);
1143
1144   std::string                        maskname("");
1145   TextureManager::MaskingDataPointer maskInfo = nullptr;
1146   maskInfo.reset(new TextureManager::MaskingData());
1147   maskInfo->mAlphaMaskUrl       = maskname;
1148   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
1149   maskInfo->mCropToMask         = true;
1150   maskInfo->mContentScaleFactor = 1.0f;
1151
1152   Vector4                       atlasRect(0.f, 0.f, 0.f, 0.f);
1153   Dali::ImageDimensions         atlasRectSize(0, 0);
1154   bool                          atlasingStatus(false);
1155   bool                          loadingStatus(false);
1156   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
1157   ImageAtlasManagerPtr          atlasManager        = nullptr;
1158   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
1159
1160   // load image synchronously.
1161   TestObserver observer;
1162   auto         textureId(TextureManager::INVALID_TEXTURE_ID);
1163   TextureSet   textureSet = textureManager.LoadTexture(
1164     filename,
1165     ImageDimensions(),
1166     FittingMode::SCALE_TO_FILL,
1167     SamplingMode::BOX_THEN_LINEAR,
1168     maskInfo,
1169     true, // synchronous loading.
1170     textureId,
1171     atlasRect,
1172     atlasRectSize,
1173     atlasingStatus,
1174     loadingStatus,
1175     &observer,
1176     atlasUploadObserver,
1177     atlasManager,
1178     true,
1179     TextureManager::ReloadPolicy::CACHED,
1180     preMultiply);
1181
1182   DALI_TEST_EQUALS(loadingStatus, false, TEST_LOCATION);
1183   DALI_TEST_CHECK(textureSet); // texture is loaded.
1184
1185   // observer isn't called in synchronous loading.
1186   DALI_TEST_EQUALS(observer.mLoaded, false, TEST_LOCATION);
1187   DALI_TEST_EQUALS(observer.mObserverCalled, false, TEST_LOCATION);
1188
1189   // load same image asynchronously.
1190   TestObserver asyncObserver;
1191   auto         asyncTextureId(TextureManager::INVALID_TEXTURE_ID);
1192   loadingStatus              = false;
1193   TextureSet asyncTextureSet = textureManager.LoadTexture(
1194     filename,
1195     ImageDimensions(),
1196     FittingMode::SCALE_TO_FILL,
1197     SamplingMode::BOX_THEN_LINEAR,
1198     maskInfo,
1199     false, // asynchronous loading.
1200     asyncTextureId,
1201     atlasRect,
1202     atlasRectSize,
1203     atlasingStatus,
1204     loadingStatus,
1205     &asyncObserver,
1206     atlasUploadObserver,
1207     atlasManager,
1208     true,
1209     TextureManager::ReloadPolicy::CACHED,
1210     preMultiply);
1211
1212   DALI_TEST_EQUALS(asyncTextureId, textureId, TEST_LOCATION); // texture is loaded.
1213   DALI_TEST_EQUALS(loadingStatus, false, TEST_LOCATION);
1214   DALI_TEST_CHECK(asyncTextureSet); // Cached texture.
1215
1216   // observer is directly called because textureSet is retrieved by cache.
1217   DALI_TEST_EQUALS(asyncObserver.mLoaded, true, TEST_LOCATION);
1218   DALI_TEST_EQUALS(asyncObserver.mObserverCalled, true, TEST_LOCATION);
1219
1220   END_TEST;
1221 }
1222
1223 int UtcTextureManagerAsyncSyncAsync(void)
1224 {
1225   ToolkitTestApplication application;
1226   tet_infoline("UtcTextureManagerAsyncSyncAsync");
1227
1228   TextureManager textureManager; // Create new texture manager
1229
1230   std::string filename(TEST_IMAGE_FILE_NAME);
1231
1232   std::string                        maskname("");
1233   TextureManager::MaskingDataPointer maskInfo = nullptr;
1234   maskInfo.reset(new TextureManager::MaskingData());
1235   maskInfo->mAlphaMaskUrl       = maskname;
1236   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
1237   maskInfo->mCropToMask         = true;
1238   maskInfo->mContentScaleFactor = 1.0f;
1239
1240   Vector4                       atlasRect(0.f, 0.f, 0.f, 0.f);
1241   Dali::ImageDimensions         atlasRectSize(0, 0);
1242   bool                          atlasingStatus(false);
1243   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
1244   ImageAtlasManagerPtr          atlasManager        = nullptr;
1245   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
1246
1247   // load image asynchronously.
1248   TestObserver asyncObserver1;
1249   auto         asyncTextureId1(TextureManager::INVALID_TEXTURE_ID);
1250   bool         asyncLoadingStatus1 = false;
1251   TextureSet   asyncTextureSet1    = textureManager.LoadTexture(
1252     filename,
1253     ImageDimensions(),
1254     FittingMode::SCALE_TO_FILL,
1255     SamplingMode::BOX_THEN_LINEAR,
1256     maskInfo,
1257     false, // asynchronous loading.
1258     asyncTextureId1,
1259     atlasRect,
1260     atlasRectSize,
1261     atlasingStatus,
1262     asyncLoadingStatus1,
1263     &asyncObserver1,
1264     atlasUploadObserver,
1265     atlasManager,
1266     true,
1267     TextureManager::ReloadPolicy::CACHED,
1268     preMultiply);
1269
1270   DALI_TEST_EQUALS(asyncLoadingStatus1, true, TEST_LOCATION); // texture is loading now.
1271   DALI_TEST_CHECK(!asyncTextureSet1);                         // texture is not loaded yet.
1272
1273   // observer is still not called.
1274   DALI_TEST_EQUALS(asyncObserver1.mLoaded, false, TEST_LOCATION);
1275   DALI_TEST_EQUALS(asyncObserver1.mObserverCalled, false, TEST_LOCATION);
1276
1277   // load same image synchronously just after asynchronous loading.
1278   TestObserver syncObserver;
1279   auto         textureId(TextureManager::INVALID_TEXTURE_ID);
1280   bool         syncLoadingStatus = false;
1281   TextureSet   syncTextureSet    = textureManager.LoadTexture(
1282     filename,
1283     ImageDimensions(),
1284     FittingMode::SCALE_TO_FILL,
1285     SamplingMode::BOX_THEN_LINEAR,
1286     maskInfo,
1287     true, // synchronous loading.
1288     textureId,
1289     atlasRect,
1290     atlasRectSize,
1291     atlasingStatus,
1292     syncLoadingStatus,
1293     &syncObserver,
1294     atlasUploadObserver,
1295     atlasManager,
1296     true,
1297     TextureManager::ReloadPolicy::CACHED,
1298     preMultiply);
1299
1300   DALI_TEST_EQUALS(asyncTextureId1, textureId, TEST_LOCATION); // texture is loaded.
1301   DALI_TEST_EQUALS(syncLoadingStatus, false, TEST_LOCATION);   // texture is loaded.
1302   DALI_TEST_CHECK(syncTextureSet);                             // texture is loaded.
1303
1304   // syncObserver isn't called in synchronous loading.
1305   DALI_TEST_EQUALS(syncObserver.mLoaded, false, TEST_LOCATION);
1306   DALI_TEST_EQUALS(syncObserver.mObserverCalled, false, TEST_LOCATION);
1307
1308   // asyncObserver1 is still not called too.
1309   DALI_TEST_EQUALS(asyncObserver1.mLoaded, false, TEST_LOCATION);
1310   DALI_TEST_EQUALS(asyncObserver1.mObserverCalled, false, TEST_LOCATION);
1311
1312   // load image asynchronously.
1313   TestObserver asyncObserver2;
1314   auto         asyncTextureId2(TextureManager::INVALID_TEXTURE_ID);
1315   bool         asyncLoadingStatus2 = false;
1316   TextureSet   asyncTextureSet2    = textureManager.LoadTexture(
1317     filename,
1318     ImageDimensions(),
1319     FittingMode::SCALE_TO_FILL,
1320     SamplingMode::BOX_THEN_LINEAR,
1321     maskInfo,
1322     false, // asynchronous loading.
1323     asyncTextureId2,
1324     atlasRect,
1325     atlasRectSize,
1326     atlasingStatus,
1327     asyncLoadingStatus2,
1328     &asyncObserver2,
1329     atlasUploadObserver,
1330     atlasManager,
1331     true,
1332     TextureManager::ReloadPolicy::CACHED,
1333     preMultiply);
1334
1335   DALI_TEST_EQUALS(asyncLoadingStatus2, false, TEST_LOCATION); // texture is loaded by previous sync request
1336   DALI_TEST_CHECK(asyncTextureSet2);                           // texture is loaded
1337   Texture syncTexture   = syncTextureSet.GetTexture(0u);
1338   Texture asyncTexture2 = asyncTextureSet2.GetTexture(0u);
1339   DALI_TEST_CHECK(syncTexture);
1340   DALI_TEST_CHECK(asyncTexture2);
1341   DALI_TEST_CHECK(asyncTexture2 == syncTexture); // check loaded two texture is same.
1342
1343   // observer is called synchronously because the texture is cached.
1344   DALI_TEST_EQUALS(asyncObserver2.mLoaded, true, TEST_LOCATION);
1345   DALI_TEST_EQUALS(asyncObserver2.mObserverCalled, true, TEST_LOCATION);
1346
1347   asyncObserver2.mLoaded         = false;
1348   asyncObserver2.mObserverCalled = false;
1349
1350   application.SendNotification();
1351   application.Render();
1352
1353   // Requested asynchronous loading at first is finished now and async observer is called now.
1354   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1355   DALI_TEST_EQUALS(asyncObserver1.mLoaded, true, TEST_LOCATION);
1356   DALI_TEST_EQUALS(asyncObserver1.mObserverCalled, true, TEST_LOCATION);
1357   DALI_TEST_CHECK(asyncObserver1.mTextureSet);
1358
1359   Texture observerTexture = asyncObserver1.mTextureSet.GetTexture(0u);
1360   DALI_TEST_CHECK(observerTexture == asyncTexture2); // check loaded two texture is same.
1361
1362   // asyncObserver2 was already called so it isn't called here.
1363   DALI_TEST_EQUALS(asyncObserver2.mLoaded, false, TEST_LOCATION);
1364   DALI_TEST_EQUALS(asyncObserver2.mObserverCalled, false, TEST_LOCATION);
1365
1366   END_TEST;
1367 }
1368
1369 int UtcTextureManagerQueueRemoveDuringObserve(void)
1370 {
1371   ToolkitTestApplication application;
1372   tet_infoline("UtcTextureManagerQueueRemoveDuringObserve");
1373
1374   TextureManager textureManager; // Create new texture manager
1375
1376   TestObserverRemoveAndGenerateUrl observer(&textureManager); // special observer for this UTC.
1377
1378   std::string filename(TEST_IMAGE_FILE_NAME);
1379   auto        preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
1380
1381   TextureManager::TextureId textureId = textureManager.RequestLoad(
1382     filename,
1383     ImageDimensions(),
1384     FittingMode::SCALE_TO_FILL,
1385     SamplingMode::BOX_THEN_LINEAR,
1386     TextureManager::UseAtlas::NO_ATLAS,
1387     &observer,
1388     true,
1389     TextureManager::ReloadPolicy::CACHED,
1390     preMultiply);
1391
1392   DALI_TEST_EQUALS(observer.mLoaded, false, TEST_LOCATION);
1393   DALI_TEST_EQUALS(observer.mObserverCalled, false, TEST_LOCATION);
1394
1395   application.SendNotification();
1396   application.Render();
1397
1398   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1399
1400   application.SendNotification();
1401   application.Render();
1402
1403   DALI_TEST_EQUALS(observer.mLoaded, true, TEST_LOCATION);
1404   DALI_TEST_EQUALS(observer.mObserverCalled, true, TEST_LOCATION);
1405   DALI_TEST_EQUALS(observer.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
1406
1407   tet_printf("loaded textureId is %d, generated url is %s\n", static_cast<int>(textureId), observer.mGeneratedExternalUrl.c_str());
1408
1409   DALI_TEST_CHECK(static_cast<int>(textureId) != std::stoi(VisualUrl::GetLocation(observer.mGeneratedExternalUrl))); // Check we don't reuse textureId during observe
1410
1411   // Decrease external texture reference count who observer created
1412   textureManager.RemoveExternalTexture(observer.mGeneratedExternalUrl);
1413
1414   application.SendNotification();
1415   application.Render();
1416
1417   END_TEST;
1418 }
1419
1420 int UtcTextureManagerRemoveDuringApplyMasking(void)
1421 {
1422   ToolkitTestApplication application;
1423   tet_infoline("UtcTextureManagerRemoveDuringApplyMasking");
1424
1425   TextureManager textureManager; // Create new texture manager
1426
1427   TestObserver observer1;
1428   TestObserver observer2;
1429
1430   std::string                        filename(TEST_IMAGE_FILE_NAME);
1431   std::string                        maskname(TEST_MASK_FILE_NAME);
1432   TextureManager::MaskingDataPointer maskInfo = nullptr;
1433   maskInfo.reset(new TextureManager::MaskingData());
1434   maskInfo->mAlphaMaskUrl       = maskname;
1435   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
1436   maskInfo->mCropToMask         = true;
1437   maskInfo->mContentScaleFactor = 1.0f;
1438
1439   auto                          textureId1(TextureManager::INVALID_TEXTURE_ID);
1440   Vector4                       atlasRect(0.f, 0.f, 1.f, 1.f);
1441   Dali::ImageDimensions         atlasRectSize(0, 0);
1442   bool                          synchronousLoading(false);
1443   bool                          atlasingStatus(false);
1444   bool                          loadingStatus(false);
1445   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
1446   ImageAtlasManagerPtr          atlasManager        = nullptr;
1447   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
1448
1449   textureManager.LoadTexture(
1450     filename,
1451     ImageDimensions(),
1452     FittingMode::SCALE_TO_FILL,
1453     SamplingMode::BOX_THEN_LINEAR,
1454     maskInfo,
1455     synchronousLoading,
1456     textureId1,
1457     atlasRect,
1458     atlasRectSize,
1459     atlasingStatus,
1460     loadingStatus,
1461     &observer1,
1462     atlasUploadObserver,
1463     atlasManager,
1464     true,
1465     TextureManager::ReloadPolicy::CACHED,
1466     preMultiply);
1467
1468   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
1469   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
1470
1471   application.SendNotification();
1472   application.Render();
1473
1474   // Load image and mask image.
1475   // Now, LoadState become MASK_APPLYING
1476   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(2), true, TEST_LOCATION);
1477
1478   tet_printf("Current textureId1:%d's state become MASK_APPLYING\n", static_cast<int>(textureId1));
1479
1480   application.SendNotification();
1481   application.Render();
1482
1483   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
1484   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
1485
1486   // Remove current textureId1. and request new texture again.
1487   textureManager.RequestRemove(textureId1, &observer1);
1488   auto textureId2 = textureManager.RequestLoad(
1489     filename,
1490     ImageDimensions(),
1491     FittingMode::SCALE_TO_FILL,
1492     SamplingMode::BOX_THEN_LINEAR,
1493     TextureManager::UseAtlas::NO_ATLAS,
1494     &observer2,
1495     true, ///< orientationCorrection
1496     TextureManager::ReloadPolicy::CACHED,
1497     preMultiply,
1498     synchronousLoading);
1499
1500   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
1501   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
1502   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
1503   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
1504
1505   tet_printf("textureId1:%d removed and textureId2:%d requested\n", static_cast<int>(textureId1), static_cast<int>(textureId2));
1506
1507   // CAPTION : HARD-CODING.
1508   {
1509     std::vector<Devel::PixelBuffer> pixelBuffers;
1510     textureManager.AsyncLoadComplete(textureId2, pixelBuffers);
1511     textureManager.RequestRemove(textureId2, &observer2);
1512   }
1513
1514   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION); ///< Note that we call AsyncLoadComplete hardly with empty pixelbuffer.
1515   DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
1516   DALI_TEST_EQUALS(observer2.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
1517
1518   END_TEST;
1519 }
1520
1521 int UtcTextureManagerMaskCacheTest(void)
1522 {
1523   ToolkitTestApplication application;
1524   tet_infoline("UtcTextureManagerMaskCacheTest");
1525
1526   TextureManager textureManager; // Create new texture manager
1527
1528   TestObserver observer1;
1529   TestObserver observer2;
1530
1531   std::string                        filename(TEST_IMAGE_FILE_NAME);
1532   std::string                        filename2(TEST_IMAGE_2_FILE_NAME);
1533   std::string                        maskname(TEST_MASK_FILE_NAME);
1534   TextureManager::MaskingDataPointer maskInfo = nullptr;
1535   maskInfo.reset(new TextureManager::MaskingData());
1536   maskInfo->mAlphaMaskUrl       = maskname;
1537   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
1538   maskInfo->mCropToMask         = true;
1539   maskInfo->mContentScaleFactor = 1.0f;
1540
1541   TextureManager::MaskingDataPointer maskInfo2 = nullptr;
1542   maskInfo2.reset(new TextureManager::MaskingData());
1543   maskInfo2->mAlphaMaskUrl       = maskname;
1544   maskInfo2->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
1545   maskInfo2->mCropToMask         = true;
1546   maskInfo2->mContentScaleFactor = 1.0f;
1547
1548   auto                          textureId1(TextureManager::INVALID_TEXTURE_ID);
1549   auto                          textureId2(TextureManager::INVALID_TEXTURE_ID);
1550   Vector4                       atlasRect(0.f, 0.f, 1.f, 1.f);
1551   Dali::ImageDimensions         atlasRectSize(0, 0);
1552   bool                          synchronousLoading(false);
1553   bool                          atlasingStatus(false);
1554   bool                          loadingStatus(false);
1555   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
1556   ImageAtlasManagerPtr          atlasManager        = nullptr;
1557   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
1558
1559   textureManager.LoadTexture(
1560     filename,
1561     ImageDimensions(),
1562     FittingMode::SCALE_TO_FILL,
1563     SamplingMode::BOX_THEN_LINEAR,
1564     maskInfo,
1565     synchronousLoading,
1566     textureId1,
1567     atlasRect,
1568     atlasRectSize,
1569     atlasingStatus,
1570     loadingStatus,
1571     &observer1,
1572     atlasUploadObserver,
1573     atlasManager,
1574     true,
1575     TextureManager::ReloadPolicy::CACHED,
1576     preMultiply);
1577
1578   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
1579   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
1580
1581   application.SendNotification();
1582   application.Render();
1583
1584   // Load image and mask image.
1585   // Now, LoadState become MASK_APPLYING
1586   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(2), true, TEST_LOCATION);
1587
1588   tet_printf("Current textureId1:%d's state become MASK_APPLYING\n", static_cast<int>(textureId1));
1589
1590   textureManager.LoadTexture(
1591     filename2,
1592     ImageDimensions(),
1593     FittingMode::SCALE_TO_FILL,
1594     SamplingMode::BOX_THEN_LINEAR,
1595     maskInfo2,
1596     synchronousLoading,
1597     textureId2,
1598     atlasRect,
1599     atlasRectSize,
1600     atlasingStatus,
1601     loadingStatus,
1602     &observer2,
1603     atlasUploadObserver,
1604     atlasManager,
1605     true,
1606     TextureManager::ReloadPolicy::CACHED,
1607     preMultiply);
1608
1609   application.SendNotification();
1610   application.Render();
1611
1612   // Load image2 + image1 apply mask + image2 apply mask = total 3 event trigger required.
1613   // Note that we use cached mask image.
1614   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(3), true, TEST_LOCATION);
1615
1616   DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
1617   DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
1618   DALI_TEST_EQUALS(observer2.mLoaded, true, TEST_LOCATION);
1619   DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
1620
1621   try
1622   {
1623     // Remove textureId1 first, and then remove textureId2. Check whether segfault occured.
1624     textureManager.RequestRemove(textureId1, &observer1);
1625
1626     application.SendNotification();
1627     application.Render();
1628
1629     textureManager.RequestRemove(textureId2, &observer2);
1630
1631     application.SendNotification();
1632     application.Render();
1633
1634     TestObserver observer3;
1635     maskInfo.reset(new TextureManager::MaskingData());
1636     maskInfo->mAlphaMaskUrl       = maskname;
1637     maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
1638     maskInfo->mCropToMask         = true;
1639     maskInfo->mContentScaleFactor = 1.0f;
1640
1641     textureManager.LoadTexture(
1642       filename,
1643       ImageDimensions(),
1644       FittingMode::SCALE_TO_FILL,
1645       SamplingMode::BOX_THEN_LINEAR,
1646       maskInfo,
1647       synchronousLoading,
1648       textureId1,
1649       atlasRect,
1650       atlasRectSize,
1651       atlasingStatus,
1652       loadingStatus,
1653       &observer3,
1654       atlasUploadObserver,
1655       atlasManager,
1656       true,
1657       TextureManager::ReloadPolicy::CACHED,
1658       preMultiply);
1659
1660     DALI_TEST_EQUALS(observer3.mLoaded, false, TEST_LOCATION);
1661     DALI_TEST_EQUALS(observer3.mObserverCalled, false, TEST_LOCATION);
1662
1663     application.SendNotification();
1664     application.Render();
1665
1666     // Load image and mask image.
1667     DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1668     DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1669
1670     DALI_TEST_EQUALS(observer3.mLoaded, false, TEST_LOCATION);
1671     DALI_TEST_EQUALS(observer3.mObserverCalled, false, TEST_LOCATION);
1672
1673     // Apply mask.
1674     DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1675
1676     DALI_TEST_EQUALS(observer3.mLoaded, true, TEST_LOCATION);
1677     DALI_TEST_EQUALS(observer3.mObserverCalled, true, TEST_LOCATION);
1678   }
1679   catch(...)
1680   {
1681     DALI_TEST_CHECK(false);
1682   }
1683
1684   END_TEST;
1685 }
1686
1687 int UtcTextureManagerRemoveDuringGPUMasking(void)
1688 {
1689   ToolkitTestApplication application;
1690   tet_infoline("UtcTextureManagerRemoveDuringGPUMasking");
1691   tet_infoline("Request 3 different GPU masking image.");
1692   tet_infoline("Control to mask image load last. and then, check execute result.");
1693
1694   TextureManager textureManager; // Create new texture manager
1695
1696   TestObserverWithCustomFunction  observer1;
1697   TestObserverWithCustomFunction  observer2;
1698   TestObserverWithCustomFunction* observer3 = new TestObserverWithCustomFunction(); // Deleted in observer1 loaded signal
1699   TestObserver                    observer4;
1700
1701   std::string filename1(TEST_IMAGE_FILE_NAME);
1702   std::string filename2(TEST_IMAGE_2_FILE_NAME);
1703   std::string filename3(TEST_IMAGE_3_FILE_NAME);
1704   std::string filename4(TEST_IMAGE_4_FILE_NAME);
1705
1706   auto textureId1(TextureManager::INVALID_TEXTURE_ID);
1707   auto textureId2(TextureManager::INVALID_TEXTURE_ID);
1708   auto textureId3(TextureManager::INVALID_TEXTURE_ID);
1709   auto textureId4(TextureManager::INVALID_TEXTURE_ID);
1710
1711   std::string                        maskname(TEST_MASK_FILE_NAME);
1712   TextureManager::MaskingDataPointer maskInfo[3] = {nullptr, nullptr, nullptr};
1713   for(int i = 0; i < 3; i++)
1714   {
1715     maskInfo[i].reset(new TextureManager::MaskingData());
1716     maskInfo[i]->mAlphaMaskUrl       = maskname;
1717     maskInfo[i]->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
1718     maskInfo[i]->mCropToMask         = true;
1719     maskInfo[i]->mPreappliedMasking  = false; // To make GPU masking
1720     maskInfo[i]->mContentScaleFactor = 1.0f;
1721   }
1722   Vector4                       atlasRect(0.f, 0.f, 1.f, 1.f);
1723   Dali::ImageDimensions         atlasRectSize(0, 0);
1724   bool                          synchronousLoading(false);
1725   bool                          atlasingStatus(false);
1726   bool                          loadingStatus(false);
1727   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
1728   ImageAtlasManagerPtr          atlasManager        = nullptr;
1729   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
1730
1731   // Request image 1, 2, 3 with GPU masking
1732   textureManager.LoadTexture(
1733     filename1,
1734     ImageDimensions(),
1735     FittingMode::SCALE_TO_FILL,
1736     SamplingMode::BOX_THEN_LINEAR,
1737     maskInfo[0],
1738     synchronousLoading,
1739     textureId1,
1740     atlasRect,
1741     atlasRectSize,
1742     atlasingStatus,
1743     loadingStatus,
1744     &observer1,
1745     atlasUploadObserver,
1746     atlasManager,
1747     true,
1748     TextureManager::ReloadPolicy::CACHED,
1749     preMultiply);
1750
1751   textureManager.LoadTexture(
1752     filename2,
1753     ImageDimensions(),
1754     FittingMode::SCALE_TO_FILL,
1755     SamplingMode::BOX_THEN_LINEAR,
1756     maskInfo[1],
1757     synchronousLoading,
1758     textureId2,
1759     atlasRect,
1760     atlasRectSize,
1761     atlasingStatus,
1762     loadingStatus,
1763     &observer2,
1764     atlasUploadObserver,
1765     atlasManager,
1766     true,
1767     TextureManager::ReloadPolicy::CACHED,
1768     preMultiply);
1769
1770   textureManager.LoadTexture(
1771     filename3,
1772     ImageDimensions(),
1773     FittingMode::SCALE_TO_FILL,
1774     SamplingMode::BOX_THEN_LINEAR,
1775     maskInfo[2],
1776     synchronousLoading,
1777     textureId3,
1778     atlasRect,
1779     atlasRectSize,
1780     atlasingStatus,
1781     loadingStatus,
1782     observer3,
1783     atlasUploadObserver,
1784     atlasManager,
1785     true,
1786     TextureManager::ReloadPolicy::CACHED,
1787     preMultiply);
1788
1789   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
1790   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
1791   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
1792   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
1793   DALI_TEST_EQUALS(observer3->mLoaded, false, TEST_LOCATION);
1794   DALI_TEST_EQUALS(observer3->mObserverCalled, false, TEST_LOCATION);
1795   DALI_TEST_EQUALS(observer4.mLoaded, false, TEST_LOCATION);
1796   DALI_TEST_EQUALS(observer4.mObserverCalled, false, TEST_LOCATION);
1797
1798   // Check we use cached mask image
1799   DALI_TEST_CHECK(maskInfo[0]->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID);
1800   DALI_TEST_EQUALS(maskInfo[0]->mAlphaMaskId, maskInfo[1]->mAlphaMaskId, TEST_LOCATION);
1801   DALI_TEST_EQUALS(maskInfo[0]->mAlphaMaskId, maskInfo[2]->mAlphaMaskId, TEST_LOCATION);
1802
1803   // Connect observer1 custom function
1804   struct CustomData1
1805   {
1806     TextureManager*           textureManagerPtr{nullptr};
1807     TextureManager::TextureId removeTextureId{TextureManager::INVALID_TEXTURE_ID};
1808     TestObserver*             removeTextureObserver{nullptr};
1809   };
1810   CustomData1 data1;
1811   data1.textureManagerPtr     = &textureManager;
1812   data1.removeTextureId       = textureId3;
1813   data1.removeTextureObserver = observer3;
1814
1815   observer1.mData = &data1;
1816   observer1.ConnectFunction(
1817     [](void* data) {
1818       DALI_TEST_CHECK(data);
1819       CustomData1 data1 = *(CustomData1*)data;
1820
1821       DALI_TEST_CHECK(data1.textureManagerPtr);
1822       DALI_TEST_CHECK(data1.removeTextureId != TextureManager::INVALID_TEXTURE_ID);
1823       DALI_TEST_CHECK(data1.removeTextureObserver);
1824
1825       // Remove textureId3.
1826       data1.textureManagerPtr->RequestRemove(data1.removeTextureId, data1.removeTextureObserver);
1827
1828       // Destroy observer3
1829       delete data1.removeTextureObserver;
1830     });
1831
1832   // Connect observer2 custom function
1833   struct CustomData2
1834   {
1835     TextureManager*            textureManagerPtr{nullptr};
1836     std::string                addTextureUrl{};
1837     TextureManager::TextureId* addTextureIdPtr{nullptr};
1838     TestObserver*              addTextureObserver{nullptr};
1839   };
1840   CustomData2 data2;
1841   data2.textureManagerPtr  = &textureManager;
1842   data2.addTextureUrl      = filename4;
1843   data2.addTextureIdPtr    = &textureId4;
1844   data2.addTextureObserver = &observer4;
1845
1846   observer2.mData = &data2;
1847   observer2.ConnectFunction(
1848     [](void* data) {
1849       DALI_TEST_CHECK(data);
1850       CustomData2 data2 = *(CustomData2*)data;
1851
1852       DALI_TEST_CHECK(data2.textureManagerPtr);
1853       DALI_TEST_CHECK(!data2.addTextureUrl.empty());
1854       DALI_TEST_CHECK(data2.addTextureIdPtr);
1855       DALI_TEST_CHECK(data2.addTextureObserver);
1856
1857       auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
1858
1859       // Load textureId4
1860       (*data2.addTextureIdPtr) = data2.textureManagerPtr->RequestLoad(
1861         data2.addTextureUrl,
1862         ImageDimensions(),
1863         FittingMode::SCALE_TO_FILL,
1864         SamplingMode::BOX_THEN_LINEAR,
1865         TextureManager::UseAtlas::NO_ATLAS,
1866         data2.addTextureObserver,
1867         true,
1868         TextureManager::ReloadPolicy::CACHED,
1869         preMultiply);
1870     });
1871
1872   // Connect observer3 custom function
1873   struct CustomData3
1874   {
1875     TestObserver* self{nullptr};
1876     bool*         observerLoadedPtr{nullptr};
1877     bool*         observerCalleddPtr{nullptr};
1878   };
1879   CustomData3 data3;
1880   bool        observer3Loaded = false;
1881   bool        observer3Called = false;
1882   data3.self                  = observer3;
1883   data3.observerLoadedPtr     = &observer3Loaded;
1884   data3.observerCalleddPtr    = &observer3Called;
1885
1886   observer3->mData = &data3;
1887   observer3->ConnectFunction(
1888     [](void* data) {
1889       DALI_TEST_CHECK(data);
1890       CustomData3 data3 = *(CustomData3*)data;
1891
1892       DALI_TEST_CHECK(data3.self);
1893       DALI_TEST_CHECK(data3.observerLoadedPtr);
1894       DALI_TEST_CHECK(data3.observerCalleddPtr);
1895
1896       *data3.observerLoadedPtr  = data3.self->mLoaded;
1897       *data3.observerCalleddPtr = data3.self->mObserverCalled;
1898     });
1899
1900   tet_printf("Id info - mask : {%d}, 1 : {%d}, 2 : {%d}, 3 : {%d}, 4 : {%d}\n", static_cast<int>(maskInfo[0]->mAlphaMaskId), static_cast<int>(textureId1), static_cast<int>(textureId2), static_cast<int>(textureId3), static_cast<int>(textureId4));
1901
1902   // CAPTION : HARD-CODING.
1903   {
1904     // Complete async load 1, 2, 3.
1905     std::vector<Devel::PixelBuffer> pixelBuffers;
1906
1907     pixelBuffers.clear();
1908     pixelBuffers.push_back(Devel::PixelBuffer::New(1, 1, Pixel::Format::RGB888));
1909     textureManager.AsyncLoadComplete(textureId1, pixelBuffers);
1910     pixelBuffers.clear();
1911     pixelBuffers.push_back(Devel::PixelBuffer::New(1, 1, Pixel::Format::RGB888));
1912     textureManager.AsyncLoadComplete(textureId2, pixelBuffers);
1913     pixelBuffers.clear();
1914     pixelBuffers.push_back(Devel::PixelBuffer::New(1, 1, Pixel::Format::RGB888));
1915     textureManager.AsyncLoadComplete(textureId3, pixelBuffers);
1916
1917     // Ensure textureId3 remove request processed.
1918
1919     DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
1920     DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
1921     DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
1922     DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
1923     DALI_TEST_EQUALS(observer3Loaded, false, TEST_LOCATION);
1924     DALI_TEST_EQUALS(observer3Called, false, TEST_LOCATION);
1925     DALI_TEST_EQUALS(observer4.mLoaded, false, TEST_LOCATION);
1926     DALI_TEST_EQUALS(observer4.mObserverCalled, false, TEST_LOCATION);
1927
1928     // Complete mask load.
1929     pixelBuffers.clear();
1930     pixelBuffers.push_back(Devel::PixelBuffer::New(1, 1, Pixel::Format::L8));
1931     textureManager.AsyncLoadComplete(maskInfo[0]->mAlphaMaskId, pixelBuffers);
1932
1933     tet_printf("Id info after observer notify - mask : {%d}, 1 : {%d}, 2 : {%d}, 3 : {%d}, 4 : {%d}\n", static_cast<int>(maskInfo[0]->mAlphaMaskId), static_cast<int>(textureId1), static_cast<int>(textureId2), static_cast<int>(textureId3), static_cast<int>(textureId4));
1934
1935     // Check observer 1 and 2 called, but 3 and 4 not called.
1936     DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
1937     DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
1938     DALI_TEST_EQUALS(observer2.mLoaded, true, TEST_LOCATION);
1939     DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
1940     DALI_TEST_EQUALS(observer3Loaded, false, TEST_LOCATION);
1941     DALI_TEST_EQUALS(observer3Called, false, TEST_LOCATION);
1942     DALI_TEST_EQUALS(observer4.mLoaded, false, TEST_LOCATION);
1943     DALI_TEST_EQUALS(observer4.mObserverCalled, false, TEST_LOCATION);
1944
1945     // Check textureId4
1946     DALI_TEST_CHECK(textureId4 != TextureManager::INVALID_TEXTURE_ID);
1947
1948     // Complete 4.
1949     pixelBuffers.clear();
1950     pixelBuffers.push_back(Devel::PixelBuffer::New(1, 1, Pixel::Format::RGB888));
1951     textureManager.AsyncLoadComplete(textureId4, pixelBuffers);
1952
1953     DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
1954     DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
1955     DALI_TEST_EQUALS(observer2.mLoaded, true, TEST_LOCATION);
1956     DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
1957     DALI_TEST_EQUALS(observer3Loaded, false, TEST_LOCATION);
1958     DALI_TEST_EQUALS(observer3Called, false, TEST_LOCATION);
1959     DALI_TEST_EQUALS(observer4.mLoaded, true, TEST_LOCATION);
1960     DALI_TEST_EQUALS(observer4.mObserverCalled, true, TEST_LOCATION);
1961   }
1962
1963   END_TEST;
1964 }
1965
1966 int UtcTextureManagerDestroyObserverDuringObserve(void)
1967 {
1968   ToolkitTestApplication application;
1969   tet_infoline("UtcTextureManagerDestroyObserverDuringObserve");
1970   tet_infoline("Request 3 different image.");
1971   tet_infoline("Complete textureId1. After observer1 loaded done,");
1972   tet_infoline(" - Remove and destroy observer2");
1973   tet_infoline(" - Re-generate observer2 which has same address pointer with before.");
1974   tet_infoline(" - Remove and Reqeust third file by observer3");
1975   tet_infoline("Complete textureId2. and check old observer2 not emmited, and newly observer2 works.");
1976   tet_infoline("Complete textureId3. and check observer3 comes");
1977
1978   TextureManager textureManager; // Create new texture manager
1979
1980   TestObserverWithCustomFunction  observer1;
1981   TestObserverWithCustomFunction* observer2 = new TestObserverWithCustomFunction(); // Deleted in observer1 loaded signal.
1982   TestObserver                    observer3;
1983
1984   std::string filename1(TEST_IMAGE_FILE_NAME);
1985   std::string filename2(TEST_IMAGE_2_FILE_NAME);
1986   std::string filename3(TEST_IMAGE_3_FILE_NAME);
1987   std::string filename4(TEST_IMAGE_4_FILE_NAME);
1988
1989   auto textureId1(TextureManager::INVALID_TEXTURE_ID);
1990   auto textureId2(TextureManager::INVALID_TEXTURE_ID);
1991   auto textureId3(TextureManager::INVALID_TEXTURE_ID);
1992   auto textureId4(TextureManager::INVALID_TEXTURE_ID);
1993
1994   // Dummy reference value
1995   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
1996
1997   // Request image 1, 2, 3.
1998   textureId1 = textureManager.RequestLoad(
1999     filename1,
2000     ImageDimensions(),
2001     FittingMode::SCALE_TO_FILL,
2002     SamplingMode::BOX_THEN_LINEAR,
2003     TextureManager::UseAtlas::NO_ATLAS,
2004     &observer1,
2005     true,
2006     TextureManager::ReloadPolicy::CACHED,
2007     preMultiply);
2008
2009   textureId2 = textureManager.RequestLoad(
2010     filename2,
2011     ImageDimensions(),
2012     FittingMode::SCALE_TO_FILL,
2013     SamplingMode::BOX_THEN_LINEAR,
2014     TextureManager::UseAtlas::NO_ATLAS,
2015     observer2,
2016     true,
2017     TextureManager::ReloadPolicy::CACHED,
2018     preMultiply);
2019
2020   textureId3 = textureManager.RequestLoad(
2021     filename3,
2022     ImageDimensions(),
2023     FittingMode::SCALE_TO_FILL,
2024     SamplingMode::BOX_THEN_LINEAR,
2025     TextureManager::UseAtlas::NO_ATLAS,
2026     &observer3,
2027     true,
2028     TextureManager::ReloadPolicy::CACHED,
2029     preMultiply);
2030
2031   struct CustomData1
2032   {
2033     TextureManager*                  textureManagerPtr{nullptr};
2034     TextureManager::TextureId        removeTextureId{TextureManager::INVALID_TEXTURE_ID};
2035     TestObserverWithCustomFunction** removeTextureObserver{nullptr};
2036     std::string                      resendFilename{};
2037     TextureManager::TextureId        resendTextureId{TextureManager::INVALID_TEXTURE_ID};
2038     TestObserver*                    resendTextureObserver{nullptr};
2039     std::string                      newlyFilename{};
2040     TextureManager::TextureId*       newlyTextureIdPtr{nullptr};
2041   };
2042   struct CustomData2
2043   {
2044     TextureManager* textureManagerPtr{nullptr};
2045     TestObserver*   self{nullptr};
2046     bool*           observerLoadedPtr{nullptr};
2047     bool*           observerCalledPtr{nullptr};
2048   };
2049
2050   bool        observer2Loaded    = false;
2051   bool        observer2Called    = false;
2052   bool        newObserver2Loaded = false;
2053   bool        newObserver2Called = false;
2054   CustomData2 newData2; // Used on observer1 function
2055
2056   // Connect observer1 custom function
2057   CustomData1 data1;
2058   data1.textureManagerPtr     = &textureManager;
2059   data1.removeTextureId       = textureId2;
2060   data1.removeTextureObserver = &observer2;
2061   data1.resendFilename        = filename3;
2062   data1.resendTextureId       = textureId3;
2063   data1.resendTextureObserver = &observer3;
2064   data1.newlyFilename         = filename2; // Same as observer2 filename
2065   data1.newlyTextureIdPtr     = &textureId4;
2066
2067   observer1.mData = &data1;
2068   observer1.ConnectFunction(
2069     [&](void* data) {
2070       DALI_TEST_CHECK(data);
2071       CustomData1 data1 = *(CustomData1*)data;
2072
2073       DALI_TEST_CHECK(data1.textureManagerPtr);
2074       DALI_TEST_CHECK(data1.removeTextureId != TextureManager::INVALID_TEXTURE_ID);
2075       DALI_TEST_CHECK(data1.removeTextureObserver);
2076       DALI_TEST_CHECK(*data1.removeTextureObserver);
2077       DALI_TEST_CHECK(!data1.resendFilename.empty());
2078       DALI_TEST_CHECK(data1.resendTextureId != TextureManager::INVALID_TEXTURE_ID);
2079       DALI_TEST_CHECK(data1.resendTextureObserver);
2080       DALI_TEST_CHECK(!data1.newlyFilename.empty());
2081       DALI_TEST_CHECK(data1.newlyTextureIdPtr);
2082       DALI_TEST_CHECK(*data1.newlyTextureIdPtr == TextureManager::INVALID_TEXTURE_ID);
2083
2084       // Remove textureId2.
2085       data1.textureManagerPtr->RequestRemove(data1.removeTextureId, *data1.removeTextureObserver);
2086
2087       auto removedObserver = *data1.removeTextureObserver;
2088
2089       // Destroy observer2.
2090       delete removedObserver;
2091
2092       // Create new observer. Make we use same pointer if we can.
2093       uint32_t maxTryCount = 100u;
2094       uint32_t tryCount    = 0u;
2095
2096       while(tryCount < maxTryCount)
2097       {
2098         *data1.removeTextureObserver = new TestObserverWithCustomFunction();
2099         if(removedObserver == *data1.removeTextureObserver) break;
2100         ++tryCount;
2101         delete *data1.removeTextureObserver;
2102       }
2103
2104       tet_printf("TryCount[%u] / Old observer2 : %p, newly observer2 : %p\n", tryCount, removedObserver, *data1.removeTextureObserver);
2105
2106       // Connect new observer2 custom function
2107       newData2.textureManagerPtr = &textureManager;
2108       newData2.self              = (*data1.removeTextureObserver);
2109       newData2.observerLoadedPtr = &newObserver2Loaded;
2110       newData2.observerCalledPtr = &newObserver2Called;
2111
2112       (*data1.removeTextureObserver)->mData = &newData2;
2113       (*data1.removeTextureObserver)->ConnectFunction([](void* data) {
2114         DALI_TEST_CHECK(data);
2115         CustomData2 data2 = *(CustomData2*)data;
2116
2117         tet_printf("New created observer running\n");
2118
2119         DALI_TEST_CHECK(data2.self);
2120         DALI_TEST_CHECK(data2.observerLoadedPtr);
2121         DALI_TEST_CHECK(data2.observerCalledPtr);
2122
2123         *data2.observerLoadedPtr = data2.self->mLoaded;
2124         *data2.observerCalledPtr = data2.self->mObserverCalled;
2125       });
2126
2127       // Dummy reference value
2128       auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
2129
2130       // Resend textureId3
2131       data1.textureManagerPtr->RequestRemove(data1.resendTextureId, data1.resendTextureObserver);
2132
2133       TextureManager::TextureId tempId;
2134       tempId = data1.textureManagerPtr->RequestLoad(
2135         data1.resendFilename,
2136         ImageDimensions(),
2137         FittingMode::SCALE_TO_FILL,
2138         SamplingMode::BOX_THEN_LINEAR,
2139         TextureManager::UseAtlas::NO_ATLAS,
2140         data1.resendTextureObserver,
2141         true,
2142         TextureManager::ReloadPolicy::CACHED,
2143         preMultiply);
2144
2145       DALI_TEST_CHECK(tempId == data1.resendTextureId);
2146
2147       // Request new task
2148
2149       tempId = data1.textureManagerPtr->RequestLoad(
2150         data1.newlyFilename,
2151         ImageDimensions(),
2152         FittingMode::SCALE_TO_FILL,
2153         SamplingMode::BOX_THEN_LINEAR,
2154         TextureManager::UseAtlas::NO_ATLAS,
2155         *data1.removeTextureObserver,
2156         true,
2157         TextureManager::ReloadPolicy::CACHED,
2158         preMultiply);
2159
2160       DALI_TEST_CHECK(tempId != TextureManager::INVALID_TEXTURE_ID);
2161       *data1.newlyTextureIdPtr = tempId;
2162     });
2163
2164   // Connect observer2 custom function
2165   CustomData2 data2;
2166   data2.textureManagerPtr = &textureManager;
2167   data2.self              = observer2;
2168   data2.observerLoadedPtr = &observer2Loaded;
2169   data2.observerCalledPtr = &observer2Called;
2170
2171   observer2->mData = &data2;
2172   observer2->ConnectFunction(
2173     [](void* data) {
2174       DALI_TEST_CHECK(data);
2175       CustomData2 data2 = *(CustomData2*)data;
2176
2177       tet_printf("Old created observer running. Something error occured!\n");
2178
2179       DALI_TEST_CHECK(data2.self);
2180       DALI_TEST_CHECK(data2.observerLoadedPtr);
2181       DALI_TEST_CHECK(data2.observerCalledPtr);
2182
2183       *data2.observerLoadedPtr = data2.self->mLoaded;
2184       *data2.observerCalledPtr = data2.self->mObserverCalled;
2185     });
2186
2187   tet_printf("Id info - 1 : {%d}, 2 : {%d}, 3 : {%d}, 4 : {%d}\n", static_cast<int>(textureId1), static_cast<int>(textureId2), static_cast<int>(textureId3), static_cast<int>(textureId4));
2188
2189   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
2190   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
2191   DALI_TEST_EQUALS(observer2Loaded, false, TEST_LOCATION);
2192   DALI_TEST_EQUALS(observer2Called, false, TEST_LOCATION);
2193   DALI_TEST_EQUALS(newObserver2Loaded, false, TEST_LOCATION);
2194   DALI_TEST_EQUALS(newObserver2Called, false, TEST_LOCATION);
2195   DALI_TEST_EQUALS(observer3.mLoaded, false, TEST_LOCATION);
2196   DALI_TEST_EQUALS(observer3.mObserverCalled, false, TEST_LOCATION);
2197
2198   DALI_TEST_CHECK(textureId4 == TextureManager::INVALID_TEXTURE_ID);
2199
2200   // CAPTION : HARD-CODING.
2201   // Run codes without exception.
2202   try
2203   {
2204     tet_printf("Complete async load 1 first.\n");
2205     std::vector<Devel::PixelBuffer> pixelBuffers;
2206
2207     pixelBuffers.clear();
2208     pixelBuffers.push_back(Devel::PixelBuffer::New(1, 1, Pixel::Format::RGB888));
2209     textureManager.AsyncLoadComplete(textureId1, pixelBuffers);
2210
2211     tet_printf("Now observer2 deleted, observer3 resended, observer2 re-created.\n");
2212     DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
2213     DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
2214     DALI_TEST_EQUALS(observer2Loaded, false, TEST_LOCATION);
2215     DALI_TEST_EQUALS(observer2Called, false, TEST_LOCATION);
2216     DALI_TEST_EQUALS(newObserver2Loaded, false, TEST_LOCATION);
2217     DALI_TEST_EQUALS(newObserver2Called, false, TEST_LOCATION);
2218     DALI_TEST_EQUALS(observer3.mLoaded, false, TEST_LOCATION);
2219     DALI_TEST_EQUALS(observer3.mObserverCalled, false, TEST_LOCATION);
2220
2221     tet_printf("Id info - 1 : {%d}, 2 : {%d}, 3 : {%d}, 4 : {%d}\n", static_cast<int>(textureId1), static_cast<int>(textureId2), static_cast<int>(textureId3), static_cast<int>(textureId4));
2222
2223     DALI_TEST_CHECK(textureId4 == textureId2);
2224
2225     // Remove processor excute.
2226     application.SendNotification();
2227     application.Render();
2228
2229     tet_printf("Complete async load 2. Let we check old version observer2 ignored and newly observer2 loaded.\n");
2230     pixelBuffers.clear();
2231     pixelBuffers.push_back(Devel::PixelBuffer::New(1, 1, Pixel::Format::RGB888));
2232     textureManager.AsyncLoadComplete(textureId2, pixelBuffers);
2233
2234     DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
2235     DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
2236     DALI_TEST_EQUALS(observer2Loaded, false, TEST_LOCATION);
2237     DALI_TEST_EQUALS(observer2Called, false, TEST_LOCATION);
2238     DALI_TEST_EQUALS(newObserver2Loaded, true, TEST_LOCATION);
2239     DALI_TEST_EQUALS(newObserver2Called, true, TEST_LOCATION);
2240     // We don't check observer3 not loaded case because SendNotification can process AsyncTask.
2241     //DALI_TEST_EQUALS(observer3.mLoaded, false, TEST_LOCATION);
2242     //DALI_TEST_EQUALS(observer3.mObserverCalled, false, TEST_LOCATION);
2243
2244     tet_printf("Complete async load 3.\n");
2245     pixelBuffers.clear();
2246     pixelBuffers.push_back(Devel::PixelBuffer::New(1, 1, Pixel::Format::RGB888));
2247     textureManager.AsyncLoadComplete(textureId3, pixelBuffers);
2248
2249     DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
2250     DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
2251     DALI_TEST_EQUALS(observer2Loaded, false, TEST_LOCATION);
2252     DALI_TEST_EQUALS(observer2Called, false, TEST_LOCATION);
2253     DALI_TEST_EQUALS(newObserver2Loaded, true, TEST_LOCATION);
2254     DALI_TEST_EQUALS(newObserver2Called, true, TEST_LOCATION);
2255     DALI_TEST_EQUALS(observer3.mLoaded, true, TEST_LOCATION);
2256     DALI_TEST_EQUALS(observer3.mObserverCalled, true, TEST_LOCATION);
2257   }
2258   catch(...)
2259   {
2260     DALI_TEST_CHECK(false);
2261   }
2262
2263   END_TEST;
2264 }