d27948240e368a06924b50cf5ab8c8f42b3caf92
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-TextureManager.cpp
1 /*
2  * Copyright (c) 2020 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-timer.h>
24 #include <toolkit-event-thread-callback.h>
25 #include <dali-toolkit/internal/visuals/texture-manager-impl.h>
26 #include <dali-toolkit/internal/visuals/texture-upload-observer.h>
27 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
28 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
29 #include <dali-toolkit/internal/visuals/visual-factory-impl.h> ///< For VisualFactory's member TextureManager.
30
31 #include <test-encoded-image-buffer.h>
32
33 #if defined(ELDBUS_ENABLED)
34 #include <automated-tests/src/dali-toolkit-internal/dali-toolkit-test-utils/dbus-wrapper.h>
35 #endif
36
37 using namespace Dali::Toolkit::Internal;
38
39 void utc_dali_toolkit_texture_manager_startup(void)
40 {
41   setenv( "LOG_TEXTURE_MANAGER", "3", 1 );
42   test_return_value = TET_UNDEF;
43 #if defined(ELDBUS_ENABLED)
44   DBusWrapper::Install(std::unique_ptr<DBusWrapper>(new TestDBusWrapper));
45 #endif
46 }
47
48 void utc_dali_toolkit_texture_manager_cleanup(void)
49 {
50   test_return_value = TET_PASS;
51 }
52
53 namespace
54 {
55
56 const char* TEST_IMAGE_FILE_NAME =  TEST_RESOURCE_DIR "/gallery-small-1.jpg";
57
58 }
59
60 class TestObserver : public Dali::Toolkit::TextureUploadObserver
61 {
62 public:
63   enum class CompleteType
64   {
65     NOT_COMPLETED = 0,
66     UPLOAD_COMPLETE,
67     LOAD_COMPLETE
68   };
69
70 public:
71   TestObserver()
72   : mCompleteType( CompleteType::NOT_COMPLETED ),
73     mLoaded(false),
74     mObserverCalled(false),
75     mTextureSet()
76   {
77   }
78
79   virtual void LoadComplete( bool loadSuccess, TextureInformation textureInformation ) override
80   {
81     if(textureInformation.returnType == TextureUploadObserver::ReturnType::TEXTURE)
82     {
83       mCompleteType = CompleteType::UPLOAD_COMPLETE;
84     }
85     else
86     {
87       mCompleteType = CompleteType::LOAD_COMPLETE;
88     }
89     mLoaded = loadSuccess;
90     mObserverCalled = true;
91     mTextureSet = textureInformation.textureSet;
92   }
93
94   CompleteType mCompleteType;
95   bool mLoaded;
96   bool mObserverCalled;
97   TextureSet mTextureSet;
98 };
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::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::USE_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::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::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::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::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
464 int UtcTextureManagerCachingForDifferentLoadingType(void)
465 {
466   ToolkitTestApplication application;
467   tet_infoline( "UtcTextureManagerCachingForDifferentLoadingType" );
468
469   TextureManager textureManager; // Create new texture manager
470
471   TestObserver observer1;
472   std::string filename( TEST_IMAGE_FILE_NAME );
473   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
474   textureManager.RequestLoad(
475     filename,
476     ImageDimensions(),
477     FittingMode::SCALE_TO_FILL,
478     SamplingMode::BOX_THEN_LINEAR,
479     TextureManager::NO_ATLAS,
480     &observer1,
481     true,
482     TextureManager::ReloadPolicy::CACHED,
483     preMultiply);
484
485   DALI_TEST_EQUALS( observer1.mLoaded, false, TEST_LOCATION );
486   DALI_TEST_EQUALS( observer1.mObserverCalled, false, TEST_LOCATION );
487
488   application.SendNotification();
489   application.Render();
490
491   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
492
493   application.SendNotification();
494   application.Render();
495
496   DALI_TEST_EQUALS( observer1.mLoaded, true, TEST_LOCATION );
497   DALI_TEST_EQUALS( observer1.mObserverCalled, true, TEST_LOCATION );
498   DALI_TEST_EQUALS( observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );
499
500   TestObserver observer2;
501   Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
502     filename,
503     ImageDimensions(),
504     FittingMode::SCALE_TO_FILL,
505     SamplingMode::BOX_THEN_LINEAR,
506     false,
507     &observer2,
508     true,
509     preMultiply);
510
511   DALI_TEST_EQUALS( observer2.mLoaded, false, TEST_LOCATION );
512   DALI_TEST_EQUALS( observer2.mObserverCalled, false, TEST_LOCATION );
513
514   application.SendNotification();
515   application.Render();
516
517   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
518
519   application.SendNotification();
520   application.Render();
521
522   DALI_TEST_EQUALS( observer2.mLoaded, true, TEST_LOCATION );
523   DALI_TEST_EQUALS( observer2.mObserverCalled, true, TEST_LOCATION );
524   DALI_TEST_EQUALS( observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION );
525
526   END_TEST;
527 }
528
529 int UtcTextureManagerUseInvalidMask(void)
530 {
531   ToolkitTestApplication application;
532   tet_infoline( "UtcTextureManagerUseInvalidMask" );
533
534   TextureManager textureManager; // Create new texture manager
535
536   TestObserver observer;
537   std::string filename( TEST_IMAGE_FILE_NAME );
538   std::string maskname("");
539   TextureManager::MaskingDataPointer maskInfo = nullptr;
540   maskInfo.reset(new TextureManager::MaskingData());
541   maskInfo->mAlphaMaskUrl = maskname;
542   maskInfo->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
543   maskInfo->mCropToMask = true;
544   maskInfo->mContentScaleFactor = 1.0f;
545
546   auto textureId( TextureManager::INVALID_TEXTURE_ID );
547   Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
548   Dali::ImageDimensions atlasRectSize( 0,0 );
549   bool synchronousLoading(false);
550   bool atlasingStatus(false);
551   bool loadingStatus(false);
552   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
553   ImageAtlasManagerPtr atlasManager = nullptr;
554   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
555
556   textureManager.LoadTexture(
557     filename,
558     ImageDimensions(),
559     FittingMode::SCALE_TO_FILL,
560     SamplingMode::BOX_THEN_LINEAR,
561     maskInfo,
562     synchronousLoading,
563     textureId,
564     atlasRect,
565     atlasRectSize,
566     atlasingStatus,
567     loadingStatus,
568     WrapMode::DEFAULT,
569     WrapMode::DEFAULT,
570     &observer,
571     atlasUploadObserver,
572     atlasManager,
573     true,
574     TextureManager::ReloadPolicy::CACHED,
575     preMultiply
576   );
577
578   DALI_TEST_EQUALS( observer.mLoaded, false, TEST_LOCATION );
579   DALI_TEST_EQUALS( observer.mObserverCalled, false, TEST_LOCATION );
580
581   application.SendNotification();
582   application.Render();
583
584   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
585
586   application.SendNotification();
587   application.Render();
588
589   DALI_TEST_EQUALS( observer.mLoaded, true, TEST_LOCATION );
590   DALI_TEST_EQUALS( observer.mObserverCalled, true, TEST_LOCATION );
591   DALI_TEST_EQUALS( observer.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );
592
593   END_TEST;
594 }
595
596 int UtcTextureManagerSynchronousLoadingFail(void)
597 {
598   ToolkitTestApplication application;
599   tet_infoline( "UtcTextureManagerSynchronousLoadingFail" );
600
601   TextureManager textureManager; // Create new texture manager
602
603   std::string maskname("");
604   TextureManager::MaskingDataPointer maskInfo = nullptr;
605   maskInfo.reset(new TextureManager::MaskingData());
606   maskInfo->mAlphaMaskUrl = maskname;
607   maskInfo->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
608   maskInfo->mCropToMask = true;
609   maskInfo->mContentScaleFactor = 1.0f;
610
611   std::string filename("dummy");
612   auto textureId( TextureManager::INVALID_TEXTURE_ID );
613   Vector4 atlasRect( 0.f, 0.f, 0.f, 0.f );
614   Dali::ImageDimensions atlasRectSize( 0,0 );
615   bool atlasingStatus(false);
616   bool loadingStatus(false);
617   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
618   ImageAtlasManagerPtr atlasManager = nullptr;
619   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
620
621   // load image synchronously.
622   TestObserver observer;
623   TextureSet textureSet = textureManager.LoadTexture(
624     filename,
625     ImageDimensions(),
626     FittingMode::SCALE_TO_FILL,
627     SamplingMode::BOX_THEN_LINEAR,
628     maskInfo,
629     true, // synchronous loading.
630     textureId,
631     atlasRect,
632     atlasRectSize,
633     atlasingStatus,
634     loadingStatus,
635     WrapMode::DEFAULT,
636     WrapMode::DEFAULT,
637     &observer,
638     atlasUploadObserver,
639     atlasManager,
640     true,
641     TextureManager::ReloadPolicy::CACHED,
642     preMultiply
643   );
644
645   DALI_TEST_EQUALS(loadingStatus, false, TEST_LOCATION);
646   DALI_TEST_CHECK(!textureSet);  // texture loading fail.
647   DALI_TEST_CHECK(textureId == TextureManager::INVALID_TEXTURE_ID); // invalid texture id is returned.
648
649   END_TEST;
650 }
651
652 int UtcTextureManagerCachingSynchronousLoading(void)
653 {
654   ToolkitTestApplication application;
655   tet_infoline( "UtcTextureManagerCachingSynchronousLoading" );
656
657   TextureManager textureManager; // Create new texture manager
658
659   std::string filename( TEST_IMAGE_FILE_NAME );
660
661   std::string maskname("");
662   TextureManager::MaskingDataPointer maskInfo = nullptr;
663   maskInfo.reset(new TextureManager::MaskingData());
664   maskInfo->mAlphaMaskUrl = maskname;
665   maskInfo->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
666   maskInfo->mCropToMask = true;
667   maskInfo->mContentScaleFactor = 1.0f;
668
669   Vector4 atlasRect( 0.f, 0.f, 0.f, 0.f );
670   Dali::ImageDimensions atlasRectSize( 0,0 );
671   bool atlasingStatus(false);
672   bool loadingStatus(false);
673   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
674   ImageAtlasManagerPtr atlasManager = nullptr;
675   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
676
677   // load image synchronously.
678   TestObserver observer;
679   auto textureId( TextureManager::INVALID_TEXTURE_ID );
680   TextureSet textureSet = textureManager.LoadTexture(
681     filename,
682     ImageDimensions(),
683     FittingMode::SCALE_TO_FILL,
684     SamplingMode::BOX_THEN_LINEAR,
685     maskInfo,
686     true, // synchronous loading.
687     textureId,
688     atlasRect,
689     atlasRectSize,
690     atlasingStatus,
691     loadingStatus,
692     WrapMode::DEFAULT,
693     WrapMode::DEFAULT,
694     &observer,
695     atlasUploadObserver,
696     atlasManager,
697     true,
698     TextureManager::ReloadPolicy::CACHED,
699     preMultiply
700   );
701
702   DALI_TEST_EQUALS(loadingStatus, false, TEST_LOCATION);
703   DALI_TEST_CHECK(textureSet);  // texture is loaded.
704
705   // observer isn't called in synchronous loading.
706   DALI_TEST_EQUALS(observer.mLoaded, false, TEST_LOCATION);
707   DALI_TEST_EQUALS(observer.mObserverCalled, false, TEST_LOCATION);
708
709
710   // load same image asynchronously.
711   TestObserver asyncObserver;
712   auto asyncTextureId( TextureManager::INVALID_TEXTURE_ID );
713   loadingStatus = false;
714   TextureSet asyncTextureSet = textureManager.LoadTexture(
715     filename,
716     ImageDimensions(),
717     FittingMode::SCALE_TO_FILL,
718     SamplingMode::BOX_THEN_LINEAR,
719     maskInfo,
720     false, // asynchronous loading.
721     asyncTextureId,
722     atlasRect,
723     atlasRectSize,
724     atlasingStatus,
725     loadingStatus,
726     WrapMode::DEFAULT,
727     WrapMode::DEFAULT,
728     &asyncObserver,
729     atlasUploadObserver,
730     atlasManager,
731     true,
732     TextureManager::ReloadPolicy::CACHED,
733     preMultiply
734   );
735
736   DALI_TEST_EQUALS(asyncTextureId, textureId, TEST_LOCATION); // texture is loaded.
737   DALI_TEST_EQUALS(loadingStatus, false, TEST_LOCATION);
738   DALI_TEST_CHECK(asyncTextureSet);  // Cached texture.
739
740   // observer is directly called because textureSet is retrieved by cache.
741   DALI_TEST_EQUALS(asyncObserver.mLoaded, true, TEST_LOCATION);
742   DALI_TEST_EQUALS(asyncObserver.mObserverCalled, true, TEST_LOCATION);
743
744   END_TEST;
745 }
746
747 int UtcTextureManagerAsyncSyncAsync(void)
748 {
749   ToolkitTestApplication application;
750   tet_infoline( "UtcTextureManagerAsyncSyncAsync" );
751
752   TextureManager textureManager; // Create new texture manager
753
754   std::string filename( TEST_IMAGE_FILE_NAME );
755
756   std::string maskname("");
757   TextureManager::MaskingDataPointer maskInfo = nullptr;
758   maskInfo.reset(new TextureManager::MaskingData());
759   maskInfo->mAlphaMaskUrl = maskname;
760   maskInfo->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
761   maskInfo->mCropToMask = true;
762   maskInfo->mContentScaleFactor = 1.0f;
763
764   Vector4 atlasRect( 0.f, 0.f, 0.f, 0.f );
765   Dali::ImageDimensions atlasRectSize( 0,0 );
766   bool atlasingStatus(false);
767   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
768   ImageAtlasManagerPtr atlasManager = nullptr;
769   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
770
771   // load image asynchronously.
772   TestObserver asyncObserver1;
773   auto asyncTextureId1( TextureManager::INVALID_TEXTURE_ID );
774   bool asyncLoadingStatus1 = false;
775   TextureSet asyncTextureSet1 = textureManager.LoadTexture(
776     filename,
777     ImageDimensions(),
778     FittingMode::SCALE_TO_FILL,
779     SamplingMode::BOX_THEN_LINEAR,
780     maskInfo,
781     false, // asynchronous loading.
782     asyncTextureId1,
783     atlasRect,
784     atlasRectSize,
785     atlasingStatus,
786     asyncLoadingStatus1,
787     WrapMode::DEFAULT,
788     WrapMode::DEFAULT,
789     &asyncObserver1,
790     atlasUploadObserver,
791     atlasManager,
792     true,
793     TextureManager::ReloadPolicy::CACHED,
794     preMultiply
795   );
796
797   DALI_TEST_EQUALS(asyncLoadingStatus1, true, TEST_LOCATION); // texture is loading now.
798   DALI_TEST_CHECK(!asyncTextureSet1);  // texture is not loaded yet.
799
800   // observer is still not called.
801   DALI_TEST_EQUALS(asyncObserver1.mLoaded, false, TEST_LOCATION);
802   DALI_TEST_EQUALS(asyncObserver1.mObserverCalled, false, TEST_LOCATION);
803
804
805   // load same image synchronously just after asynchronous loading.
806   TestObserver syncObserver;
807   auto textureId( TextureManager::INVALID_TEXTURE_ID );
808   bool syncLoadingStatus = false;
809   TextureSet syncTextureSet = textureManager.LoadTexture(
810     filename,
811     ImageDimensions(),
812     FittingMode::SCALE_TO_FILL,
813     SamplingMode::BOX_THEN_LINEAR,
814     maskInfo,
815     true, // synchronous loading.
816     textureId,
817     atlasRect,
818     atlasRectSize,
819     atlasingStatus,
820     syncLoadingStatus,
821     WrapMode::DEFAULT,
822     WrapMode::DEFAULT,
823     &syncObserver,
824     atlasUploadObserver,
825     atlasManager,
826     true,
827     TextureManager::ReloadPolicy::CACHED,
828     preMultiply
829   );
830
831   DALI_TEST_EQUALS(asyncTextureId1, textureId, TEST_LOCATION); // texture is loaded.
832   DALI_TEST_EQUALS(syncLoadingStatus, false, TEST_LOCATION); // texture is loaded.
833   DALI_TEST_CHECK(syncTextureSet);  // texture is loaded.
834
835   // syncObserver isn't called in synchronous loading.
836   DALI_TEST_EQUALS(syncObserver.mLoaded, false, TEST_LOCATION);
837   DALI_TEST_EQUALS(syncObserver.mObserverCalled, false, TEST_LOCATION);
838
839   // asyncObserver1 is still not called too.
840   DALI_TEST_EQUALS(asyncObserver1.mLoaded, false, TEST_LOCATION);
841   DALI_TEST_EQUALS(asyncObserver1.mObserverCalled, false, TEST_LOCATION);
842
843
844
845   // load image asynchronously.
846   TestObserver asyncObserver2;
847   auto asyncTextureId2( TextureManager::INVALID_TEXTURE_ID );
848   bool asyncLoadingStatus2 = false;
849   TextureSet asyncTextureSet2 = textureManager.LoadTexture(
850     filename,
851     ImageDimensions(),
852     FittingMode::SCALE_TO_FILL,
853     SamplingMode::BOX_THEN_LINEAR,
854     maskInfo,
855     false, // asynchronous loading.
856     asyncTextureId2,
857     atlasRect,
858     atlasRectSize,
859     atlasingStatus,
860     asyncLoadingStatus2,
861     WrapMode::DEFAULT,
862     WrapMode::DEFAULT,
863     &asyncObserver2,
864     atlasUploadObserver,
865     atlasManager,
866     true,
867     TextureManager::ReloadPolicy::CACHED,
868     preMultiply
869   );
870
871   DALI_TEST_EQUALS(asyncLoadingStatus2, false, TEST_LOCATION); // texture is loaded by previous sync request
872   DALI_TEST_CHECK(asyncTextureSet2); // texture is loaded
873   DALI_TEST_CHECK(asyncTextureSet2 == syncTextureSet);  // check loaded two texture is same.
874
875   // observer is called synchronously because the texture is cached.
876   DALI_TEST_EQUALS(asyncObserver2.mLoaded, true, TEST_LOCATION);
877   DALI_TEST_EQUALS(asyncObserver2.mObserverCalled, true, TEST_LOCATION);
878
879   asyncObserver2.mLoaded = false;
880   asyncObserver2.mObserverCalled = false;
881
882   application.SendNotification();
883   application.Render();
884
885   // Requested asynchronous loading at first is finished now and async observer is called now.
886   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
887   DALI_TEST_EQUALS(asyncObserver1.mLoaded, true, TEST_LOCATION);
888   DALI_TEST_EQUALS(asyncObserver1.mObserverCalled, true, TEST_LOCATION);
889   DALI_TEST_CHECK(asyncObserver1.mTextureSet == asyncTextureSet2);  // check loaded two texture is same.
890
891   // asyncObserver2 was already called so it isn't called here.
892   DALI_TEST_EQUALS(asyncObserver2.mLoaded, false, TEST_LOCATION);
893   DALI_TEST_EQUALS(asyncObserver2.mObserverCalled, false, TEST_LOCATION);
894
895   END_TEST;
896 }