Merge "Make broken alpha mask image show image" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-TextureManager.cpp
1 /*
2  * Copyright (c) 2022 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/devel-api/adaptor-framework/pixel-buffer.h>
32
33 #include <test-encoded-image-buffer.h>
34
35 #if defined(ELDBUS_ENABLED)
36 #include <automated-tests/src/dali-toolkit-internal/dali-toolkit-test-utils/dbus-wrapper.h>
37 #endif
38
39 using namespace Dali::Toolkit::Internal;
40
41 void utc_dali_toolkit_texture_manager_startup(void)
42 {
43   setenv("LOG_TEXTURE_MANAGER", "3", 1);
44   test_return_value = TET_UNDEF;
45 #if defined(ELDBUS_ENABLED)
46   DBusWrapper::Install(std::unique_ptr<DBusWrapper>(new TestDBusWrapper));
47 #endif
48 }
49
50 void utc_dali_toolkit_texture_manager_cleanup(void)
51 {
52   test_return_value = TET_PASS;
53 }
54
55 namespace
56 {
57 const char* TEST_IMAGE_FILE_NAME = TEST_RESOURCE_DIR "/gallery-small-1.jpg";
58
59 }
60
61 class TestObserver : public Dali::Toolkit::TextureUploadObserver
62 {
63 public:
64   enum class CompleteType
65   {
66     NOT_COMPLETED = 0,
67     UPLOAD_COMPLETE,
68     LOAD_COMPLETE
69   };
70
71 public:
72   TestObserver()
73   : mCompleteType(CompleteType::NOT_COMPLETED),
74     mLoaded(false),
75     mObserverCalled(false),
76     mTextureSet()
77   {
78   }
79
80   virtual void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override
81   {
82     if(textureInformation.returnType == TextureUploadObserver::ReturnType::TEXTURE)
83     {
84       mCompleteType = CompleteType::UPLOAD_COMPLETE;
85     }
86     else
87     {
88       mCompleteType = CompleteType::LOAD_COMPLETE;
89     }
90     mLoaded         = loadSuccess;
91     mObserverCalled = true;
92     mTextureSet     = textureInformation.textureSet;
93   }
94
95   CompleteType mCompleteType;
96   bool         mLoaded;
97   bool         mObserverCalled;
98   TextureSet   mTextureSet;
99 };
100
101 int UtcTextureManagerRequestLoad(void)
102 {
103   ToolkitTestApplication application;
104
105   TextureManager textureManager; // Create new texture manager
106
107   TestObserver              observer;
108   std::string               filename("image.png");
109   auto                      preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
110   TextureManager::TextureId textureId   = textureManager.RequestLoad(
111     filename,
112     ImageDimensions(),
113     FittingMode::SCALE_TO_FILL,
114     SamplingMode::BOX_THEN_LINEAR,
115     TextureManager::UseAtlas::NO_ATLAS,
116     &observer,
117     true,
118     TextureManager::ReloadPolicy::CACHED,
119     preMultiply);
120
121   VisualUrl url = textureManager.GetVisualUrl(textureId);
122
123   DALI_TEST_EQUALS(url.GetUrl().compare(filename), 0, TEST_LOCATION);
124
125   END_TEST;
126 }
127
128 int UtcTextureManagerGenerateHash(void)
129 {
130   ToolkitTestApplication application;
131
132   TextureManager textureManager; // Create new texture manager
133
134   TestObserver              observer;
135   std::string               filename("image.png");
136   auto                      preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
137   TextureManager::TextureId textureId   = textureManager.RequestLoad(
138     filename,
139     ImageDimensions(),
140     FittingMode::SCALE_TO_FILL,
141     SamplingMode::BOX_THEN_LINEAR,
142     TextureManager::UseAtlas::NO_ATLAS,
143     &observer,
144     true,
145     TextureManager::ReloadPolicy::CACHED,
146     preMultiply);
147
148   VisualUrl url = textureManager.GetVisualUrl(textureId);
149
150   DALI_TEST_EQUALS(url.GetUrl().compare(filename), 0, TEST_LOCATION);
151
152   END_TEST;
153 }
154
155 int UtcTextureManagerEncodedImageBuffer(void)
156 {
157   ToolkitTestApplication application;
158   tet_infoline("UtcTextureManagerEncodedImageBuffer");
159
160   auto  visualFactory  = Toolkit::VisualFactory::Get();
161   auto& textureManager = GetImplementation(visualFactory).GetTextureManager(); // Use VisualFactory's texture manager
162
163   // Get encoded raw-buffer image and generate url
164   EncodedImageBuffer buffer1 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
165   EncodedImageBuffer buffer2 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
166
167   std::string url1 = textureManager.AddExternalEncodedImageBuffer(buffer1);
168   std::string url2 = textureManager.AddExternalEncodedImageBuffer(buffer1);
169   std::string url3 = VisualUrl::CreateBufferUrl(""); ///< Impossible Buffer URL. for coverage
170
171   // Check if same EncodedImageBuffer get same url
172   DALI_TEST_CHECK(url1 == url2);
173   // Reduce reference count
174   textureManager.RemoveExternalEncodedImageBuffer(url1);
175   // Check whethere url1 still valid
176   DALI_TEST_CHECK(textureManager.GetEncodedImageBuffer(url1));
177
178   url2 = textureManager.AddExternalEncodedImageBuffer(buffer2);
179   // Check if difference EncodedImageBuffer get difference url
180   DALI_TEST_CHECK(url1 != url2);
181
182   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
183
184   TestObserver observer1;
185   textureManager.RequestLoad(
186     url1,
187     ImageDimensions(),
188     FittingMode::SCALE_TO_FILL,
189     SamplingMode::BOX_THEN_LINEAR,
190     TextureManager::UseAtlas::NO_ATLAS,
191     &observer1,
192     true, ///< orientationCorrection
193     TextureManager::ReloadPolicy::CACHED,
194     preMultiply);
195
196   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
197   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
198
199   application.SendNotification();
200   application.Render();
201
202   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
203
204   application.SendNotification();
205   application.Render();
206
207   DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
208   DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
209   DALI_TEST_EQUALS(observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
210
211   TestObserver observer2;
212   // Syncload
213   Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
214     url2,
215     ImageDimensions(),
216     FittingMode::SCALE_TO_FILL,
217     SamplingMode::BOX_THEN_LINEAR,
218     true, ///< synchronousLoading
219     &observer2,
220     true, ///< orientationCorrection
221     preMultiply);
222
223   DALI_TEST_CHECK(pixelBuffer);
224   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
225   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
226
227   // Asyncload
228   pixelBuffer = textureManager.LoadPixelBuffer(
229     url2,
230     ImageDimensions(),
231     FittingMode::SCALE_TO_FILL,
232     SamplingMode::BOX_THEN_LINEAR,
233     false, ///< synchronousLoading
234     &observer2,
235     true, ///< orientationCorrection
236     preMultiply);
237
238   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
239   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
240
241   application.SendNotification();
242   application.Render();
243
244   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
245
246   application.SendNotification();
247   application.Render();
248
249   DALI_TEST_EQUALS(observer2.mLoaded, true, TEST_LOCATION);
250   DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
251   DALI_TEST_EQUALS(observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION);
252
253   textureManager.RemoveExternalEncodedImageBuffer(url1);
254   textureManager.RemoveExternalEncodedImageBuffer(url2);
255
256   // Now url1 and url2 is invalid type. mLoaded will return false
257
258   TestObserver observer3;
259   textureManager.RequestLoad(
260     url1,
261     ImageDimensions(),
262     FittingMode::SCALE_TO_FILL,
263     SamplingMode::BOX_THEN_LINEAR,
264     TextureManager::UseAtlas::NO_ATLAS,
265     &observer3,
266     true, ///< orientationCorrection
267     TextureManager::ReloadPolicy::CACHED,
268     preMultiply);
269
270   // Load will be success because url1 is cached
271   DALI_TEST_EQUALS(observer3.mLoaded, true, TEST_LOCATION);
272   DALI_TEST_EQUALS(observer3.mObserverCalled, true, TEST_LOCATION);
273   DALI_TEST_EQUALS(observer3.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
274
275   TestObserver observer4;
276   textureManager.RequestLoad(
277     url2,
278     ImageDimensions(),
279     FittingMode::SCALE_TO_FILL,
280     SamplingMode::BOX_THEN_LINEAR,
281     TextureManager::UseAtlas::NO_ATLAS,
282     &observer4,
283     true, ///< orientationCorrection
284     TextureManager::ReloadPolicy::FORCED,
285     preMultiply);
286
287   DALI_TEST_EQUALS(observer4.mLoaded, false, TEST_LOCATION);
288   DALI_TEST_EQUALS(observer4.mObserverCalled, false, TEST_LOCATION);
289   application.SendNotification();
290   application.Render();
291
292   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
293
294   application.SendNotification();
295   application.Render();
296
297   // Load will be failed becuase reloadpolicy is forced
298   DALI_TEST_EQUALS(observer4.mLoaded, false, TEST_LOCATION);
299   DALI_TEST_EQUALS(observer4.mObserverCalled, true, TEST_LOCATION);
300   DALI_TEST_EQUALS(observer4.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
301
302   TestObserver observer5;
303   pixelBuffer = textureManager.LoadPixelBuffer(
304     url2,
305     ImageDimensions(),
306     FittingMode::SCALE_TO_FILL,
307     SamplingMode::BOX_THEN_LINEAR,
308     true, ///< synchronousLoading
309     &observer5,
310     true, ///< orientationCorrection
311     preMultiply);
312
313   // Load will be faild because synchronousLoading doesn't use cached texture
314   DALI_TEST_CHECK(!pixelBuffer);
315   DALI_TEST_EQUALS(observer5.mLoaded, false, TEST_LOCATION);
316   DALI_TEST_EQUALS(observer5.mObserverCalled, false, TEST_LOCATION);
317
318   TestObserver observer6;
319   pixelBuffer = textureManager.LoadPixelBuffer(
320     url3,
321     ImageDimensions(),
322     FittingMode::SCALE_TO_FILL,
323     SamplingMode::BOX_THEN_LINEAR,
324     false, ///< synchronousLoading
325     &observer6,
326     true, ///< orientationCorrection
327     preMultiply);
328
329   DALI_TEST_EQUALS(observer6.mLoaded, false, TEST_LOCATION);
330   DALI_TEST_EQUALS(observer6.mObserverCalled, false, TEST_LOCATION);
331
332   application.SendNotification();
333   application.Render();
334
335   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
336
337   application.SendNotification();
338   application.Render();
339
340   // Load will be failed because url3 is invalid URL
341   DALI_TEST_EQUALS(observer6.mLoaded, false, TEST_LOCATION);
342   DALI_TEST_EQUALS(observer6.mObserverCalled, true, TEST_LOCATION);
343   DALI_TEST_EQUALS(observer6.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION);
344
345   END_TEST;
346 }
347
348 int UtcTextureManagerEncodedImageBufferReferenceCount(void)
349 {
350   ToolkitTestApplication application;
351   tet_infoline("UtcTextureManagerEncodedImageBuffer check reference count works well");
352
353   auto  visualFactory  = Toolkit::VisualFactory::Get();
354   auto& textureManager = GetImplementation(visualFactory).GetTextureManager(); // Use VisualFactory's texture manager
355
356   // Get encoded raw-buffer image and generate url
357   EncodedImageBuffer buffer1 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
358   EncodedImageBuffer buffer2 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
359
360   std::string url1 = textureManager.AddExternalEncodedImageBuffer(buffer1);
361   std::string url2 = textureManager.AddExternalEncodedImageBuffer(buffer1);
362
363   // Check if same EncodedImageBuffer get same url
364   DALI_TEST_CHECK(url1 == url2);
365
366   // Reduce reference count
367   textureManager.RemoveExternalEncodedImageBuffer(url1);
368   // Check whethere url1 still valid
369   DALI_TEST_CHECK(textureManager.GetEncodedImageBuffer(url1));
370
371   // Reduce reference count
372   textureManager.RemoveExternalEncodedImageBuffer(url1);
373   // Check whethere url1 is not valid anymore
374   DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url1));
375
376   // UseExternalTexture doesn't create new buffer.
377   // So, reference count is still zero.
378   textureManager.UseExternalResource(url1);
379   DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url1));
380
381   url1 = textureManager.AddExternalEncodedImageBuffer(buffer1);
382   // Check if difference EncodedImageBuffer get difference url
383   // Previous EncodedImageBuffer was deleted, so we get new url even same buffer.
384   DALI_TEST_CHECK(url1 != url2);
385
386   url2 = textureManager.AddExternalEncodedImageBuffer(buffer2);
387   // Check if difference EncodedImageBuffer get difference url
388   DALI_TEST_CHECK(url1 != url2);
389
390   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
391
392   // url1 load image by cache
393   TestObserver observer1;
394   textureManager.RequestLoad(
395     url1,
396     ImageDimensions(),
397     FittingMode::SCALE_TO_FILL,
398     SamplingMode::BOX_THEN_LINEAR,
399     TextureManager::UseAtlas::NO_ATLAS,
400     &observer1,
401     true, ///< orientationCorrection
402     TextureManager::ReloadPolicy::CACHED,
403     preMultiply);
404
405   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
406   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
407
408   application.SendNotification();
409   application.Render();
410
411   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
412
413   application.SendNotification();
414   application.Render();
415
416   DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
417   DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
418   DALI_TEST_EQUALS(observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
419
420   // LoadPixelBuffer doen't use cache. url2 will not be cached
421   TestObserver       observer2;
422   Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
423     url2,
424     ImageDimensions(),
425     FittingMode::SCALE_TO_FILL,
426     SamplingMode::BOX_THEN_LINEAR,
427     false, ///< synchronousLoading
428     &observer2,
429     true, ///< orientationCorrection
430     preMultiply);
431
432   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
433   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
434
435   application.SendNotification();
436   application.Render();
437
438   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
439
440   application.SendNotification();
441   application.Render();
442
443   DALI_TEST_EQUALS(observer2.mLoaded, true, TEST_LOCATION);
444   DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
445   DALI_TEST_EQUALS(observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION);
446
447   // Decrease each url's reference count.
448   textureManager.RemoveExternalEncodedImageBuffer(url1);
449   textureManager.RemoveExternalEncodedImageBuffer(url2);
450
451   // url1 buffer is still have 1 reference count because it is cached.
452   // But url2 not valid because it is not cached.
453   DALI_TEST_CHECK(textureManager.GetEncodedImageBuffer(url1));
454   DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url2));
455
456   // Check url1 buffer have 1 reference count because it is cached.
457   textureManager.RemoveExternalEncodedImageBuffer(url1);
458   DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url1));
459
460   END_TEST;
461 }
462
463 int UtcTextureManagerCachingForDifferentLoadingType(void)
464 {
465   ToolkitTestApplication application;
466   tet_infoline("UtcTextureManagerCachingForDifferentLoadingType");
467
468   TextureManager textureManager; // Create new texture manager
469
470   TestObserver observer1;
471   std::string  filename(TEST_IMAGE_FILE_NAME);
472   auto         preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
473   textureManager.RequestLoad(
474     filename,
475     ImageDimensions(),
476     FittingMode::SCALE_TO_FILL,
477     SamplingMode::BOX_THEN_LINEAR,
478     TextureManager::UseAtlas::NO_ATLAS,
479     &observer1,
480     true,
481     TextureManager::ReloadPolicy::CACHED,
482     preMultiply);
483
484   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
485   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
486
487   application.SendNotification();
488   application.Render();
489
490   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
491
492   application.SendNotification();
493   application.Render();
494
495   DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
496   DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
497   DALI_TEST_EQUALS(observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
498
499   TestObserver       observer2;
500   Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
501     filename,
502     ImageDimensions(),
503     FittingMode::SCALE_TO_FILL,
504     SamplingMode::BOX_THEN_LINEAR,
505     false,
506     &observer2,
507     true,
508     preMultiply);
509
510   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
511   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
512
513   application.SendNotification();
514   application.Render();
515
516   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
517
518   application.SendNotification();
519   application.Render();
520
521   DALI_TEST_EQUALS(observer2.mLoaded, true, TEST_LOCATION);
522   DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
523   DALI_TEST_EQUALS(observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION);
524
525   END_TEST;
526 }
527
528 int UtcTextureManagerUseInvalidMask(void)
529 {
530   ToolkitTestApplication application;
531   tet_infoline("UtcTextureManagerUseInvalidMask");
532
533   TextureManager textureManager; // Create new texture manager
534
535   TestObserver                       observer;
536   std::string                        filename(TEST_IMAGE_FILE_NAME);
537   std::string                        maskname("");
538   TextureManager::MaskingDataPointer maskInfo = nullptr;
539   maskInfo.reset(new TextureManager::MaskingData());
540   maskInfo->mAlphaMaskUrl       = maskname;
541   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
542   maskInfo->mCropToMask         = true;
543   maskInfo->mContentScaleFactor = 1.0f;
544
545   auto                          textureId(TextureManager::INVALID_TEXTURE_ID);
546   Vector4                       atlasRect(0.f, 0.f, 1.f, 1.f);
547   Dali::ImageDimensions         atlasRectSize(0, 0);
548   bool                          synchronousLoading(false);
549   bool                          atlasingStatus(false);
550   bool                          loadingStatus(false);
551   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
552   ImageAtlasManagerPtr          atlasManager        = nullptr;
553   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
554
555   textureManager.LoadTexture(
556     filename,
557     ImageDimensions(),
558     FittingMode::SCALE_TO_FILL,
559     SamplingMode::BOX_THEN_LINEAR,
560     maskInfo,
561     synchronousLoading,
562     textureId,
563     atlasRect,
564     atlasRectSize,
565     atlasingStatus,
566     loadingStatus,
567     WrapMode::DEFAULT,
568     WrapMode::DEFAULT,
569     &observer,
570     atlasUploadObserver,
571     atlasManager,
572     true,
573     TextureManager::ReloadPolicy::CACHED,
574     preMultiply);
575
576   DALI_TEST_EQUALS(observer.mLoaded, false, TEST_LOCATION);
577   DALI_TEST_EQUALS(observer.mObserverCalled, false, TEST_LOCATION);
578
579   application.SendNotification();
580   application.Render();
581
582   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
583
584   application.SendNotification();
585   application.Render();
586
587   DALI_TEST_EQUALS(observer.mLoaded, true, TEST_LOCATION);
588   DALI_TEST_EQUALS(observer.mObserverCalled, true, TEST_LOCATION);
589   DALI_TEST_EQUALS(observer.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
590
591   END_TEST;
592 }
593
594 int UtcTextureManagerUseInvalidMaskAndMaskLoadedFirst(void)
595 {
596   ToolkitTestApplication application;
597   tet_infoline("UtcTextureManagerUseInvalidMask when normal image loaded first, and mask image loaded first");
598   tet_infoline("Try to check PostLoad works well");
599
600   TextureManager textureManager; // Create new texture manager
601
602   TestObserver                       observer;
603   std::string                        filename(TEST_IMAGE_FILE_NAME);
604   std::string                        maskname("invalid.png");
605   TextureManager::MaskingDataPointer maskInfo = nullptr;
606   maskInfo.reset(new TextureManager::MaskingData());
607   maskInfo->mAlphaMaskUrl       = maskname;
608   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
609   maskInfo->mCropToMask         = true;
610   maskInfo->mContentScaleFactor = 1.0f;
611
612   auto                          textureId(TextureManager::INVALID_TEXTURE_ID);
613   Vector4                       atlasRect(0.f, 0.f, 1.f, 1.f);
614   Dali::ImageDimensions         atlasRectSize(0, 0);
615   bool                          synchronousLoading(false);
616   bool                          atlasingStatus(false);
617   bool                          loadingStatus(false);
618   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
619   ImageAtlasManagerPtr          atlasManager        = nullptr;
620   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
621
622   textureManager.LoadTexture(
623     filename,
624     ImageDimensions(),
625     FittingMode::SCALE_TO_FILL,
626     SamplingMode::BOX_THEN_LINEAR,
627     maskInfo,
628     synchronousLoading,
629     textureId,
630     atlasRect,
631     atlasRectSize,
632     atlasingStatus,
633     loadingStatus,
634     WrapMode::DEFAULT,
635     WrapMode::DEFAULT,
636     &observer,
637     atlasUploadObserver,
638     atlasManager,
639     true,
640     TextureManager::ReloadPolicy::CACHED,
641     preMultiply);
642
643   DALI_TEST_EQUALS(observer.mLoaded, false, TEST_LOCATION);
644   DALI_TEST_EQUALS(observer.mObserverCalled, false, TEST_LOCATION);
645
646   application.SendNotification();
647   application.Render();
648
649   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(2), true, TEST_LOCATION);
650
651   application.SendNotification();
652   application.Render();
653
654   DALI_TEST_EQUALS(observer.mLoaded, true, TEST_LOCATION);
655   DALI_TEST_EQUALS(observer.mObserverCalled, true, TEST_LOCATION);
656   DALI_TEST_EQUALS(observer.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
657
658   END_TEST;
659 }
660
661 int UtcTextureManagerUseInvalidMaskAndMaskLoadedLater(void)
662 {
663   ToolkitTestApplication application;
664   tet_infoline("UtcTextureManagerUseInvalidMask when normal image loaded first, and mask image loaded later");
665   tet_infoline("Try to check CheckForWaitingTexture called");
666
667   TextureManager textureManager; // Create new texture manager
668
669   TestObserver                       observer;
670   std::string                        filename(TEST_IMAGE_FILE_NAME);
671   std::string                        maskname("invalid.png");
672   TextureManager::MaskingDataPointer maskInfo = nullptr;
673   maskInfo.reset(new TextureManager::MaskingData());
674   maskInfo->mAlphaMaskUrl       = maskname;
675   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
676   maskInfo->mCropToMask         = true;
677   maskInfo->mContentScaleFactor = 1.0f;
678
679   auto                          textureId(TextureManager::INVALID_TEXTURE_ID);
680   Vector4                       atlasRect(0.f, 0.f, 1.f, 1.f);
681   Dali::ImageDimensions         atlasRectSize(0, 0);
682   bool                          synchronousLoading(false);
683   bool                          atlasingStatus(false);
684   bool                          loadingStatus(false);
685   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
686   ImageAtlasManagerPtr          atlasManager        = nullptr;
687   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
688
689   textureManager.LoadTexture(
690     filename,
691     ImageDimensions(),
692     FittingMode::SCALE_TO_FILL,
693     SamplingMode::BOX_THEN_LINEAR,
694     maskInfo,
695     synchronousLoading,
696     textureId,
697     atlasRect,
698     atlasRectSize,
699     atlasingStatus,
700     loadingStatus,
701     WrapMode::DEFAULT,
702     WrapMode::DEFAULT,
703     &observer,
704     atlasUploadObserver,
705     atlasManager,
706     true,
707     TextureManager::ReloadPolicy::CACHED,
708     preMultiply);
709
710   DALI_TEST_EQUALS(observer.mLoaded, false, TEST_LOCATION);
711   DALI_TEST_EQUALS(observer.mObserverCalled, false, TEST_LOCATION);
712
713   application.SendNotification();
714   application.Render();
715
716   // CAPTION : HARD-CODING for coverage. If you are a good boy, Do not follow this code.
717   {
718     Dali::Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
719       filename,
720       ImageDimensions(),
721       FittingMode::SCALE_TO_FILL,
722       SamplingMode::BOX_THEN_LINEAR,
723       true, ///< synchronousLoading
724       nullptr,
725       true, ///< orientationCorrection
726       preMultiply);
727
728     textureManager.AsyncLoadComplete(textureId, pixelBuffer);
729     textureManager.AsyncLoadComplete(maskInfo->mAlphaMaskId, Dali::Devel::PixelBuffer());
730     textureManager.Remove(maskInfo->mAlphaMaskId, nullptr);
731     textureManager.Remove(textureId, &observer);
732   }
733
734   application.SendNotification();
735   application.Render();
736
737   DALI_TEST_EQUALS(observer.mLoaded, true, TEST_LOCATION);
738   DALI_TEST_EQUALS(observer.mObserverCalled, true, TEST_LOCATION);
739   DALI_TEST_EQUALS(observer.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
740
741   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(2), true, TEST_LOCATION);
742
743   END_TEST;
744 }
745
746 int UtcTextureManagerSynchronousLoadingFail(void)
747 {
748   ToolkitTestApplication application;
749   tet_infoline("UtcTextureManagerSynchronousLoadingFail");
750
751   TextureManager textureManager; // Create new texture manager
752
753   std::string                        maskname("");
754   TextureManager::MaskingDataPointer maskInfo = nullptr;
755   maskInfo.reset(new TextureManager::MaskingData());
756   maskInfo->mAlphaMaskUrl       = maskname;
757   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
758   maskInfo->mCropToMask         = true;
759   maskInfo->mContentScaleFactor = 1.0f;
760
761   std::string                   filename("dummy");
762   auto                          textureId(TextureManager::INVALID_TEXTURE_ID);
763   Vector4                       atlasRect(0.f, 0.f, 0.f, 0.f);
764   Dali::ImageDimensions         atlasRectSize(0, 0);
765   bool                          atlasingStatus(false);
766   bool                          loadingStatus(false);
767   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
768   ImageAtlasManagerPtr          atlasManager        = nullptr;
769   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
770
771   // load image synchronously.
772   TestObserver observer;
773   TextureSet   textureSet = textureManager.LoadTexture(
774     filename,
775     ImageDimensions(),
776     FittingMode::SCALE_TO_FILL,
777     SamplingMode::BOX_THEN_LINEAR,
778     maskInfo,
779     true, // synchronous loading.
780     textureId,
781     atlasRect,
782     atlasRectSize,
783     atlasingStatus,
784     loadingStatus,
785     WrapMode::DEFAULT,
786     WrapMode::DEFAULT,
787     &observer,
788     atlasUploadObserver,
789     atlasManager,
790     true,
791     TextureManager::ReloadPolicy::CACHED,
792     preMultiply);
793
794   DALI_TEST_EQUALS(loadingStatus, false, TEST_LOCATION);
795   DALI_TEST_CHECK(!textureSet);                                     // texture loading fail.
796   DALI_TEST_CHECK(textureId == TextureManager::INVALID_TEXTURE_ID); // invalid texture id is returned.
797
798   END_TEST;
799 }
800
801 int UtcTextureManagerCachingSynchronousLoading(void)
802 {
803   ToolkitTestApplication application;
804   tet_infoline("UtcTextureManagerCachingSynchronousLoading");
805
806   TextureManager textureManager; // Create new texture manager
807
808   std::string filename(TEST_IMAGE_FILE_NAME);
809
810   std::string                        maskname("");
811   TextureManager::MaskingDataPointer maskInfo = nullptr;
812   maskInfo.reset(new TextureManager::MaskingData());
813   maskInfo->mAlphaMaskUrl       = maskname;
814   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
815   maskInfo->mCropToMask         = true;
816   maskInfo->mContentScaleFactor = 1.0f;
817
818   Vector4                       atlasRect(0.f, 0.f, 0.f, 0.f);
819   Dali::ImageDimensions         atlasRectSize(0, 0);
820   bool                          atlasingStatus(false);
821   bool                          loadingStatus(false);
822   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
823   ImageAtlasManagerPtr          atlasManager        = nullptr;
824   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
825
826   // load image synchronously.
827   TestObserver observer;
828   auto         textureId(TextureManager::INVALID_TEXTURE_ID);
829   TextureSet   textureSet = textureManager.LoadTexture(
830     filename,
831     ImageDimensions(),
832     FittingMode::SCALE_TO_FILL,
833     SamplingMode::BOX_THEN_LINEAR,
834     maskInfo,
835     true, // synchronous loading.
836     textureId,
837     atlasRect,
838     atlasRectSize,
839     atlasingStatus,
840     loadingStatus,
841     WrapMode::DEFAULT,
842     WrapMode::DEFAULT,
843     &observer,
844     atlasUploadObserver,
845     atlasManager,
846     true,
847     TextureManager::ReloadPolicy::CACHED,
848     preMultiply);
849
850   DALI_TEST_EQUALS(loadingStatus, false, TEST_LOCATION);
851   DALI_TEST_CHECK(textureSet); // texture is loaded.
852
853   // observer isn't called in synchronous loading.
854   DALI_TEST_EQUALS(observer.mLoaded, false, TEST_LOCATION);
855   DALI_TEST_EQUALS(observer.mObserverCalled, false, TEST_LOCATION);
856
857   // load same image asynchronously.
858   TestObserver asyncObserver;
859   auto         asyncTextureId(TextureManager::INVALID_TEXTURE_ID);
860   loadingStatus              = false;
861   TextureSet asyncTextureSet = textureManager.LoadTexture(
862     filename,
863     ImageDimensions(),
864     FittingMode::SCALE_TO_FILL,
865     SamplingMode::BOX_THEN_LINEAR,
866     maskInfo,
867     false, // asynchronous loading.
868     asyncTextureId,
869     atlasRect,
870     atlasRectSize,
871     atlasingStatus,
872     loadingStatus,
873     WrapMode::DEFAULT,
874     WrapMode::DEFAULT,
875     &asyncObserver,
876     atlasUploadObserver,
877     atlasManager,
878     true,
879     TextureManager::ReloadPolicy::CACHED,
880     preMultiply);
881
882   DALI_TEST_EQUALS(asyncTextureId, textureId, TEST_LOCATION); // texture is loaded.
883   DALI_TEST_EQUALS(loadingStatus, false, TEST_LOCATION);
884   DALI_TEST_CHECK(asyncTextureSet); // Cached texture.
885
886   // observer is directly called because textureSet is retrieved by cache.
887   DALI_TEST_EQUALS(asyncObserver.mLoaded, true, TEST_LOCATION);
888   DALI_TEST_EQUALS(asyncObserver.mObserverCalled, true, TEST_LOCATION);
889
890   END_TEST;
891 }
892
893 int UtcTextureManagerAsyncSyncAsync(void)
894 {
895   ToolkitTestApplication application;
896   tet_infoline("UtcTextureManagerAsyncSyncAsync");
897
898   TextureManager textureManager; // Create new texture manager
899
900   std::string filename(TEST_IMAGE_FILE_NAME);
901
902   std::string                        maskname("");
903   TextureManager::MaskingDataPointer maskInfo = nullptr;
904   maskInfo.reset(new TextureManager::MaskingData());
905   maskInfo->mAlphaMaskUrl       = maskname;
906   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
907   maskInfo->mCropToMask         = true;
908   maskInfo->mContentScaleFactor = 1.0f;
909
910   Vector4                       atlasRect(0.f, 0.f, 0.f, 0.f);
911   Dali::ImageDimensions         atlasRectSize(0, 0);
912   bool                          atlasingStatus(false);
913   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
914   ImageAtlasManagerPtr          atlasManager        = nullptr;
915   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
916
917   // load image asynchronously.
918   TestObserver asyncObserver1;
919   auto         asyncTextureId1(TextureManager::INVALID_TEXTURE_ID);
920   bool         asyncLoadingStatus1 = false;
921   TextureSet   asyncTextureSet1    = textureManager.LoadTexture(
922     filename,
923     ImageDimensions(),
924     FittingMode::SCALE_TO_FILL,
925     SamplingMode::BOX_THEN_LINEAR,
926     maskInfo,
927     false, // asynchronous loading.
928     asyncTextureId1,
929     atlasRect,
930     atlasRectSize,
931     atlasingStatus,
932     asyncLoadingStatus1,
933     WrapMode::DEFAULT,
934     WrapMode::DEFAULT,
935     &asyncObserver1,
936     atlasUploadObserver,
937     atlasManager,
938     true,
939     TextureManager::ReloadPolicy::CACHED,
940     preMultiply);
941
942   DALI_TEST_EQUALS(asyncLoadingStatus1, true, TEST_LOCATION); // texture is loading now.
943   DALI_TEST_CHECK(!asyncTextureSet1);                         // texture is not loaded yet.
944
945   // observer is still not called.
946   DALI_TEST_EQUALS(asyncObserver1.mLoaded, false, TEST_LOCATION);
947   DALI_TEST_EQUALS(asyncObserver1.mObserverCalled, false, TEST_LOCATION);
948
949   // load same image synchronously just after asynchronous loading.
950   TestObserver syncObserver;
951   auto         textureId(TextureManager::INVALID_TEXTURE_ID);
952   bool         syncLoadingStatus = false;
953   TextureSet   syncTextureSet    = textureManager.LoadTexture(
954     filename,
955     ImageDimensions(),
956     FittingMode::SCALE_TO_FILL,
957     SamplingMode::BOX_THEN_LINEAR,
958     maskInfo,
959     true, // synchronous loading.
960     textureId,
961     atlasRect,
962     atlasRectSize,
963     atlasingStatus,
964     syncLoadingStatus,
965     WrapMode::DEFAULT,
966     WrapMode::DEFAULT,
967     &syncObserver,
968     atlasUploadObserver,
969     atlasManager,
970     true,
971     TextureManager::ReloadPolicy::CACHED,
972     preMultiply);
973
974   DALI_TEST_EQUALS(asyncTextureId1, textureId, TEST_LOCATION); // texture is loaded.
975   DALI_TEST_EQUALS(syncLoadingStatus, false, TEST_LOCATION);   // texture is loaded.
976   DALI_TEST_CHECK(syncTextureSet);                             // texture is loaded.
977
978   // syncObserver isn't called in synchronous loading.
979   DALI_TEST_EQUALS(syncObserver.mLoaded, false, TEST_LOCATION);
980   DALI_TEST_EQUALS(syncObserver.mObserverCalled, false, TEST_LOCATION);
981
982   // asyncObserver1 is still not called too.
983   DALI_TEST_EQUALS(asyncObserver1.mLoaded, false, TEST_LOCATION);
984   DALI_TEST_EQUALS(asyncObserver1.mObserverCalled, false, TEST_LOCATION);
985
986   // load image asynchronously.
987   TestObserver asyncObserver2;
988   auto         asyncTextureId2(TextureManager::INVALID_TEXTURE_ID);
989   bool         asyncLoadingStatus2 = false;
990   TextureSet   asyncTextureSet2    = textureManager.LoadTexture(
991     filename,
992     ImageDimensions(),
993     FittingMode::SCALE_TO_FILL,
994     SamplingMode::BOX_THEN_LINEAR,
995     maskInfo,
996     false, // asynchronous loading.
997     asyncTextureId2,
998     atlasRect,
999     atlasRectSize,
1000     atlasingStatus,
1001     asyncLoadingStatus2,
1002     WrapMode::DEFAULT,
1003     WrapMode::DEFAULT,
1004     &asyncObserver2,
1005     atlasUploadObserver,
1006     atlasManager,
1007     true,
1008     TextureManager::ReloadPolicy::CACHED,
1009     preMultiply);
1010
1011   DALI_TEST_EQUALS(asyncLoadingStatus2, false, TEST_LOCATION); // texture is loaded by previous sync request
1012   DALI_TEST_CHECK(asyncTextureSet2);                           // texture is loaded
1013   DALI_TEST_CHECK(asyncTextureSet2 == syncTextureSet);         // check loaded two texture is same.
1014
1015   // observer is called synchronously because the texture is cached.
1016   DALI_TEST_EQUALS(asyncObserver2.mLoaded, true, TEST_LOCATION);
1017   DALI_TEST_EQUALS(asyncObserver2.mObserverCalled, true, TEST_LOCATION);
1018
1019   asyncObserver2.mLoaded         = false;
1020   asyncObserver2.mObserverCalled = false;
1021
1022   application.SendNotification();
1023   application.Render();
1024
1025   // Requested asynchronous loading at first is finished now and async observer is called now.
1026   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1027   DALI_TEST_EQUALS(asyncObserver1.mLoaded, true, TEST_LOCATION);
1028   DALI_TEST_EQUALS(asyncObserver1.mObserverCalled, true, TEST_LOCATION);
1029   DALI_TEST_CHECK(asyncObserver1.mTextureSet == asyncTextureSet2); // check loaded two texture is same.
1030
1031   // asyncObserver2 was already called so it isn't called here.
1032   DALI_TEST_EQUALS(asyncObserver2.mLoaded, false, TEST_LOCATION);
1033   DALI_TEST_EQUALS(asyncObserver2.mObserverCalled, false, TEST_LOCATION);
1034
1035   END_TEST;
1036 }