[dali_2.1.3] Merge branch 'devel/master'
[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 UploadComplete( bool loadSuccess, int32_t textureId, TextureSet textureSet,
80                                bool useAtlasing, const Vector4& atlasRect, bool preMultiplied ) override
81   {
82     mCompleteType = CompleteType::UPLOAD_COMPLETE;
83     mLoaded = loadSuccess;
84     mObserverCalled = true;
85     mTextureSet = textureSet;
86   }
87
88   virtual void LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied ) override
89   {
90     mCompleteType = CompleteType::LOAD_COMPLETE;
91     mLoaded = loadSuccess;
92     mObserverCalled = true;
93   }
94
95   CompleteType mCompleteType;
96   bool mLoaded;
97   bool mObserverCalled;
98   TextureSet mTextureSet;
99 };
100
101
102 int UtcTextureManagerRequestLoad(void)
103 {
104   ToolkitTestApplication application;
105
106   TextureManager textureManager; // Create new texture manager
107
108   TestObserver observer;
109   std::string filename("image.png");
110   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
111   TextureManager::TextureId textureId = textureManager.RequestLoad(
112     filename,
113     ImageDimensions(),
114     FittingMode::SCALE_TO_FILL,
115     SamplingMode::BOX_THEN_LINEAR,
116     TextureManager::NO_ATLAS,
117     &observer,
118     true,
119     TextureManager::ReloadPolicy::CACHED,
120     preMultiply);
121
122   VisualUrl url = textureManager.GetVisualUrl( textureId );
123
124   DALI_TEST_EQUALS( url.GetUrl().compare( filename ), 0, TEST_LOCATION );
125
126   END_TEST;
127 }
128
129 int UtcTextureManagerGenerateHash(void)
130 {
131   ToolkitTestApplication application;
132
133   TextureManager textureManager; // Create new texture manager
134
135   TestObserver observer;
136   std::string filename( "image.png" );
137   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
138   TextureManager::TextureId textureId = textureManager.RequestLoad(
139     filename,
140     ImageDimensions(),
141     FittingMode::SCALE_TO_FILL,
142     SamplingMode::BOX_THEN_LINEAR,
143     TextureManager::USE_ATLAS,
144     &observer,
145     true,
146     TextureManager::ReloadPolicy::CACHED,
147     preMultiply);
148
149   VisualUrl url = textureManager.GetVisualUrl( textureId );
150
151   DALI_TEST_EQUALS( url.GetUrl().compare( filename ), 0, TEST_LOCATION );
152
153   END_TEST;
154 }
155
156 int UtcTextureManagerEncodedImageBuffer(void)
157 {
158   ToolkitTestApplication application;
159   tet_infoline( "UtcTextureManagerEncodedImageBuffer" );
160
161   auto  visualFactory  = Toolkit::VisualFactory::Get();
162   auto& textureManager = GetImplementation(visualFactory).GetTextureManager(); // Use VisualFactory's texture manager
163
164   // Get encoded raw-buffer image and generate url
165   EncodedImageBuffer buffer1 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
166   EncodedImageBuffer buffer2 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
167
168   std::string  url1 = textureManager.AddExternalEncodedImageBuffer(buffer1);
169   std::string  url2 = textureManager.AddExternalEncodedImageBuffer(buffer1);
170   std::string  url3 = VisualUrl::CreateBufferUrl(""); ///< Impossible Buffer URL. for coverage
171
172   // Check if same EncodedImageBuffer get same url
173   DALI_TEST_CHECK(url1 == url2);
174   // Reduce reference count
175   textureManager.RemoveExternalEncodedImageBuffer(url1);
176   // Check whethere url1 still valid
177   DALI_TEST_CHECK(textureManager.GetEncodedImageBuffer(url1));
178
179   url2 = textureManager.AddExternalEncodedImageBuffer(buffer2);
180   // Check if difference EncodedImageBuffer get difference url
181   DALI_TEST_CHECK(url1 != url2);
182
183   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
184
185   TestObserver observer1;
186   textureManager.RequestLoad(
187     url1,
188     ImageDimensions(),
189     FittingMode::SCALE_TO_FILL,
190     SamplingMode::BOX_THEN_LINEAR,
191     TextureManager::NO_ATLAS,
192     &observer1,
193     true, ///< orientationCorrection
194     TextureManager::ReloadPolicy::CACHED,
195     preMultiply);
196
197   DALI_TEST_EQUALS( observer1.mLoaded, false, TEST_LOCATION );
198   DALI_TEST_EQUALS( observer1.mObserverCalled, false, TEST_LOCATION );
199
200   application.SendNotification();
201   application.Render();
202
203   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
204
205   application.SendNotification();
206   application.Render();
207
208   DALI_TEST_EQUALS( observer1.mLoaded, true, TEST_LOCATION );
209   DALI_TEST_EQUALS( observer1.mObserverCalled, true, TEST_LOCATION );
210   DALI_TEST_EQUALS( observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );
211
212   TestObserver observer2;
213   // Syncload
214   Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
215     url2,
216     ImageDimensions(),
217     FittingMode::SCALE_TO_FILL,
218     SamplingMode::BOX_THEN_LINEAR,
219     true, ///< synchronousLoading
220     &observer2,
221     true, ///< orientationCorrection
222     preMultiply);
223
224   DALI_TEST_CHECK( pixelBuffer );
225   DALI_TEST_EQUALS( observer2.mLoaded, false, TEST_LOCATION );
226   DALI_TEST_EQUALS( observer2.mObserverCalled, false, TEST_LOCATION );
227
228   // Asyncload
229   pixelBuffer = textureManager.LoadPixelBuffer(
230     url2,
231     ImageDimensions(),
232     FittingMode::SCALE_TO_FILL,
233     SamplingMode::BOX_THEN_LINEAR,
234     false, ///< synchronousLoading
235     &observer2,
236     true, ///< orientationCorrection
237     preMultiply);
238
239   DALI_TEST_EQUALS( observer2.mLoaded, false, TEST_LOCATION );
240   DALI_TEST_EQUALS( observer2.mObserverCalled, false, TEST_LOCATION );
241
242   application.SendNotification();
243   application.Render();
244
245   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
246
247   application.SendNotification();
248   application.Render();
249
250   DALI_TEST_EQUALS( observer2.mLoaded, true, TEST_LOCATION );
251   DALI_TEST_EQUALS( observer2.mObserverCalled, true, TEST_LOCATION );
252   DALI_TEST_EQUALS( observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION );
253
254   textureManager.RemoveExternalEncodedImageBuffer(url1);
255   textureManager.RemoveExternalEncodedImageBuffer(url2);
256
257   // Now url1 and url2 is invalid type. mLoaded will return false
258
259   TestObserver observer3;
260   textureManager.RequestLoad(
261     url1,
262     ImageDimensions(),
263     FittingMode::SCALE_TO_FILL,
264     SamplingMode::BOX_THEN_LINEAR,
265     TextureManager::NO_ATLAS,
266     &observer3,
267     true, ///< orientationCorrection
268     TextureManager::ReloadPolicy::CACHED,
269     preMultiply);
270
271   // Load will be success because url1 is cached
272   DALI_TEST_EQUALS( observer3.mLoaded, true, TEST_LOCATION );
273   DALI_TEST_EQUALS( observer3.mObserverCalled, true, TEST_LOCATION );
274   DALI_TEST_EQUALS( observer3.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );
275
276   TestObserver observer4;
277   textureManager.RequestLoad(
278     url2,
279     ImageDimensions(),
280     FittingMode::SCALE_TO_FILL,
281     SamplingMode::BOX_THEN_LINEAR,
282     TextureManager::NO_ATLAS,
283     &observer4,
284     true, ///< orientationCorrection
285     TextureManager::ReloadPolicy::FORCED,
286     preMultiply);
287
288   DALI_TEST_EQUALS( observer4.mLoaded, false, TEST_LOCATION );
289   DALI_TEST_EQUALS( observer4.mObserverCalled, false, TEST_LOCATION );
290   application.SendNotification();
291   application.Render();
292
293   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
294
295   application.SendNotification();
296   application.Render();
297
298   // Load will be failed becuase reloadpolicy is forced
299   DALI_TEST_EQUALS( observer4.mLoaded, false, TEST_LOCATION );
300   DALI_TEST_EQUALS( observer4.mObserverCalled, true, TEST_LOCATION );
301   DALI_TEST_EQUALS( observer4.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );
302
303   TestObserver observer5;
304   pixelBuffer = textureManager.LoadPixelBuffer(
305     url2,
306     ImageDimensions(),
307     FittingMode::SCALE_TO_FILL,
308     SamplingMode::BOX_THEN_LINEAR,
309     true, ///< synchronousLoading
310     &observer5,
311     true, ///< orientationCorrection
312     preMultiply);
313
314   // Load will be faild because synchronousLoading doesn't use cached texture
315   DALI_TEST_CHECK( !pixelBuffer );
316   DALI_TEST_EQUALS( observer5.mLoaded, false, TEST_LOCATION );
317   DALI_TEST_EQUALS( observer5.mObserverCalled, false, TEST_LOCATION );
318
319   TestObserver observer6;
320   pixelBuffer = textureManager.LoadPixelBuffer(
321     url3,
322     ImageDimensions(),
323     FittingMode::SCALE_TO_FILL,
324     SamplingMode::BOX_THEN_LINEAR,
325     false, ///< synchronousLoading
326     &observer6,
327     true, ///< orientationCorrection
328     preMultiply);
329
330   DALI_TEST_EQUALS( observer6.mLoaded, false, TEST_LOCATION );
331   DALI_TEST_EQUALS( observer6.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   // Load will be failed because url3 is invalid URL
342   DALI_TEST_EQUALS( observer6.mLoaded, false, TEST_LOCATION );
343   DALI_TEST_EQUALS( observer6.mObserverCalled, true, TEST_LOCATION );
344   DALI_TEST_EQUALS( observer6.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION );
345
346   END_TEST;
347 }
348
349 int UtcTextureManagerEncodedImageBufferReferenceCount(void)
350 {
351   ToolkitTestApplication application;
352   tet_infoline( "UtcTextureManagerEncodedImageBuffer check reference count works well" );
353
354   auto  visualFactory  = Toolkit::VisualFactory::Get();
355   auto& textureManager = GetImplementation(visualFactory).GetTextureManager(); // Use VisualFactory's texture manager
356
357   // Get encoded raw-buffer image and generate url
358   EncodedImageBuffer buffer1 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
359   EncodedImageBuffer buffer2 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
360
361   std::string  url1    = textureManager.AddExternalEncodedImageBuffer(buffer1);
362   std::string  url2    = textureManager.AddExternalEncodedImageBuffer(buffer1);
363
364   // Check if same EncodedImageBuffer get same url
365   DALI_TEST_CHECK(url1 == url2);
366
367   // Reduce reference count
368   textureManager.RemoveExternalEncodedImageBuffer(url1);
369   // Check whethere url1 still valid
370   DALI_TEST_CHECK(textureManager.GetEncodedImageBuffer(url1));
371
372   // Reduce reference count
373   textureManager.RemoveExternalEncodedImageBuffer(url1);
374   // Check whethere url1 is not valid anymore
375   DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url1));
376
377   // UseExternalTexture doesn't create new buffer.
378   // So, reference count is still zero.
379   textureManager.UseExternalResource(url1);
380   DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url1));
381
382   url1 = textureManager.AddExternalEncodedImageBuffer(buffer1);
383   // Check if difference EncodedImageBuffer get difference url
384   // Previous EncodedImageBuffer was deleted, so we get new url even same buffer.
385   DALI_TEST_CHECK(url1 != url2);
386
387   url2 = textureManager.AddExternalEncodedImageBuffer(buffer2);
388   // Check if difference EncodedImageBuffer get difference url
389   DALI_TEST_CHECK(url1 != url2);
390
391   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
392
393   // url1 load image by cache
394   TestObserver observer1;
395   textureManager.RequestLoad(
396     url1,
397     ImageDimensions(),
398     FittingMode::SCALE_TO_FILL,
399     SamplingMode::BOX_THEN_LINEAR,
400     TextureManager::NO_ATLAS,
401     &observer1,
402     true, ///< orientationCorrection
403     TextureManager::ReloadPolicy::CACHED,
404     preMultiply);
405
406   DALI_TEST_EQUALS( observer1.mLoaded, false, TEST_LOCATION );
407   DALI_TEST_EQUALS( observer1.mObserverCalled, false, TEST_LOCATION );
408
409   application.SendNotification();
410   application.Render();
411
412   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
413
414   application.SendNotification();
415   application.Render();
416
417   DALI_TEST_EQUALS( observer1.mLoaded, true, TEST_LOCATION );
418   DALI_TEST_EQUALS( observer1.mObserverCalled, true, TEST_LOCATION );
419   DALI_TEST_EQUALS( observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );
420
421   // LoadPixelBuffer doen't use cache. url2 will not be cached
422   TestObserver observer2;
423   Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
424     url2,
425     ImageDimensions(),
426     FittingMode::SCALE_TO_FILL,
427     SamplingMode::BOX_THEN_LINEAR,
428     false, ///< synchronousLoading
429     &observer2,
430     true, ///< orientationCorrection
431     preMultiply);
432
433   DALI_TEST_EQUALS( observer2.mLoaded, false, TEST_LOCATION );
434   DALI_TEST_EQUALS( observer2.mObserverCalled, false, TEST_LOCATION );
435
436   application.SendNotification();
437   application.Render();
438
439   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
440
441   application.SendNotification();
442   application.Render();
443
444   DALI_TEST_EQUALS( observer2.mLoaded, true, TEST_LOCATION );
445   DALI_TEST_EQUALS( observer2.mObserverCalled, true, TEST_LOCATION );
446   DALI_TEST_EQUALS( observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION );
447
448   // Decrease each url's reference count.
449   textureManager.RemoveExternalEncodedImageBuffer(url1);
450   textureManager.RemoveExternalEncodedImageBuffer(url2);
451
452   // url1 buffer is still have 1 reference count because it is cached.
453   // But url2 not valid because it is not cached.
454   DALI_TEST_CHECK(textureManager.GetEncodedImageBuffer(url1));
455   DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url2));
456
457   // Check url1 buffer have 1 reference count because it is cached.
458   textureManager.RemoveExternalEncodedImageBuffer(url1);
459   DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url1));
460
461   END_TEST;
462 }
463
464
465 int UtcTextureManagerCachingForDifferentLoadingType(void)
466 {
467   ToolkitTestApplication application;
468   tet_infoline( "UtcTextureManagerCachingForDifferentLoadingType" );
469
470   TextureManager textureManager; // Create new texture manager
471
472   TestObserver observer1;
473   std::string filename( TEST_IMAGE_FILE_NAME );
474   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
475   textureManager.RequestLoad(
476     filename,
477     ImageDimensions(),
478     FittingMode::SCALE_TO_FILL,
479     SamplingMode::BOX_THEN_LINEAR,
480     TextureManager::NO_ATLAS,
481     &observer1,
482     true,
483     TextureManager::ReloadPolicy::CACHED,
484     preMultiply);
485
486   DALI_TEST_EQUALS( observer1.mLoaded, false, TEST_LOCATION );
487   DALI_TEST_EQUALS( observer1.mObserverCalled, false, TEST_LOCATION );
488
489   application.SendNotification();
490   application.Render();
491
492   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
493
494   application.SendNotification();
495   application.Render();
496
497   DALI_TEST_EQUALS( observer1.mLoaded, true, TEST_LOCATION );
498   DALI_TEST_EQUALS( observer1.mObserverCalled, true, TEST_LOCATION );
499   DALI_TEST_EQUALS( observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );
500
501   TestObserver observer2;
502   Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
503     filename,
504     ImageDimensions(),
505     FittingMode::SCALE_TO_FILL,
506     SamplingMode::BOX_THEN_LINEAR,
507     false,
508     &observer2,
509     true,
510     preMultiply);
511
512   DALI_TEST_EQUALS( observer2.mLoaded, false, TEST_LOCATION );
513   DALI_TEST_EQUALS( observer2.mObserverCalled, false, TEST_LOCATION );
514
515   application.SendNotification();
516   application.Render();
517
518   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
519
520   application.SendNotification();
521   application.Render();
522
523   DALI_TEST_EQUALS( observer2.mLoaded, true, TEST_LOCATION );
524   DALI_TEST_EQUALS( observer2.mObserverCalled, true, TEST_LOCATION );
525   DALI_TEST_EQUALS( observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION );
526
527   END_TEST;
528 }
529
530 int UtcTextureManagerUseInvalidMask(void)
531 {
532   ToolkitTestApplication application;
533   tet_infoline( "UtcTextureManagerUseInvalidMask" );
534
535   TextureManager textureManager; // Create new texture manager
536
537   TestObserver observer;
538   std::string filename( TEST_IMAGE_FILE_NAME );
539   std::string maskname("");
540   TextureManager::MaskingDataPointer maskInfo = nullptr;
541   maskInfo.reset(new TextureManager::MaskingData());
542   maskInfo->mAlphaMaskUrl = maskname;
543   maskInfo->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
544   maskInfo->mCropToMask = true;
545   maskInfo->mContentScaleFactor = 1.0f;
546
547   auto textureId( TextureManager::INVALID_TEXTURE_ID );
548   Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
549   Dali::ImageDimensions atlasRectSize( 0,0 );
550   bool synchronousLoading(false);
551   bool atlasingStatus(false);
552   bool loadingStatus(false);
553   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
554   ImageAtlasManagerPtr atlasManager = nullptr;
555   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
556
557   textureManager.LoadTexture(
558     filename,
559     ImageDimensions(),
560     FittingMode::SCALE_TO_FILL,
561     SamplingMode::BOX_THEN_LINEAR,
562     maskInfo,
563     synchronousLoading,
564     textureId,
565     atlasRect,
566     atlasRectSize,
567     atlasingStatus,
568     loadingStatus,
569     WrapMode::DEFAULT,
570     WrapMode::DEFAULT,
571     &observer,
572     atlasUploadObserver,
573     atlasManager,
574     true,
575     TextureManager::ReloadPolicy::CACHED,
576     preMultiply
577   );
578
579   DALI_TEST_EQUALS( observer.mLoaded, false, TEST_LOCATION );
580   DALI_TEST_EQUALS( observer.mObserverCalled, false, TEST_LOCATION );
581
582   application.SendNotification();
583   application.Render();
584
585   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
586
587   application.SendNotification();
588   application.Render();
589
590   DALI_TEST_EQUALS( observer.mLoaded, true, TEST_LOCATION );
591   DALI_TEST_EQUALS( observer.mObserverCalled, true, TEST_LOCATION );
592   DALI_TEST_EQUALS( observer.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );
593
594   END_TEST;
595 }
596
597 int UtcTextureManagerSynchronousLoadingFail(void)
598 {
599   ToolkitTestApplication application;
600   tet_infoline( "UtcTextureManagerSynchronousLoadingFail" );
601
602   TextureManager textureManager; // Create new texture manager
603
604   std::string maskname("");
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   std::string filename("dummy");
613   auto textureId( TextureManager::INVALID_TEXTURE_ID );
614   Vector4 atlasRect( 0.f, 0.f, 0.f, 0.f );
615   Dali::ImageDimensions atlasRectSize( 0,0 );
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   // load image synchronously.
623   TestObserver observer;
624   TextureSet textureSet = textureManager.LoadTexture(
625     filename,
626     ImageDimensions(),
627     FittingMode::SCALE_TO_FILL,
628     SamplingMode::BOX_THEN_LINEAR,
629     maskInfo,
630     true, // synchronous loading.
631     textureId,
632     atlasRect,
633     atlasRectSize,
634     atlasingStatus,
635     loadingStatus,
636     WrapMode::DEFAULT,
637     WrapMode::DEFAULT,
638     &observer,
639     atlasUploadObserver,
640     atlasManager,
641     true,
642     TextureManager::ReloadPolicy::CACHED,
643     preMultiply
644   );
645
646   DALI_TEST_EQUALS(loadingStatus, false, TEST_LOCATION);
647   DALI_TEST_CHECK(!textureSet);  // texture loading fail.
648   DALI_TEST_CHECK(textureId == TextureManager::INVALID_TEXTURE_ID); // invalid texture id is returned.
649
650   END_TEST;
651 }
652
653 int UtcTextureManagerCachingSynchronousLoading(void)
654 {
655   ToolkitTestApplication application;
656   tet_infoline( "UtcTextureManagerCachingSynchronousLoading" );
657
658   TextureManager textureManager; // Create new texture manager
659
660   std::string filename( TEST_IMAGE_FILE_NAME );
661
662   std::string maskname("");
663   TextureManager::MaskingDataPointer maskInfo = nullptr;
664   maskInfo.reset(new TextureManager::MaskingData());
665   maskInfo->mAlphaMaskUrl = maskname;
666   maskInfo->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
667   maskInfo->mCropToMask = true;
668   maskInfo->mContentScaleFactor = 1.0f;
669
670   Vector4 atlasRect( 0.f, 0.f, 0.f, 0.f );
671   Dali::ImageDimensions atlasRectSize( 0,0 );
672   bool atlasingStatus(false);
673   bool loadingStatus(false);
674   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
675   ImageAtlasManagerPtr atlasManager = nullptr;
676   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
677
678   // load image synchronously.
679   TestObserver observer;
680   auto textureId( TextureManager::INVALID_TEXTURE_ID );
681   TextureSet textureSet = textureManager.LoadTexture(
682     filename,
683     ImageDimensions(),
684     FittingMode::SCALE_TO_FILL,
685     SamplingMode::BOX_THEN_LINEAR,
686     maskInfo,
687     true, // synchronous loading.
688     textureId,
689     atlasRect,
690     atlasRectSize,
691     atlasingStatus,
692     loadingStatus,
693     WrapMode::DEFAULT,
694     WrapMode::DEFAULT,
695     &observer,
696     atlasUploadObserver,
697     atlasManager,
698     true,
699     TextureManager::ReloadPolicy::CACHED,
700     preMultiply
701   );
702
703   DALI_TEST_EQUALS(loadingStatus, false, TEST_LOCATION);
704   DALI_TEST_CHECK(textureSet);  // texture is loaded.
705
706   // observer isn't called in synchronous loading.
707   DALI_TEST_EQUALS(observer.mLoaded, false, TEST_LOCATION);
708   DALI_TEST_EQUALS(observer.mObserverCalled, false, TEST_LOCATION);
709
710
711   // load same image asynchronously.
712   TestObserver asyncObserver;
713   auto asyncTextureId( TextureManager::INVALID_TEXTURE_ID );
714   loadingStatus = false;
715   TextureSet asyncTextureSet = textureManager.LoadTexture(
716     filename,
717     ImageDimensions(),
718     FittingMode::SCALE_TO_FILL,
719     SamplingMode::BOX_THEN_LINEAR,
720     maskInfo,
721     false, // asynchronous loading.
722     asyncTextureId,
723     atlasRect,
724     atlasRectSize,
725     atlasingStatus,
726     loadingStatus,
727     WrapMode::DEFAULT,
728     WrapMode::DEFAULT,
729     &asyncObserver,
730     atlasUploadObserver,
731     atlasManager,
732     true,
733     TextureManager::ReloadPolicy::CACHED,
734     preMultiply
735   );
736
737   DALI_TEST_EQUALS(asyncTextureId, textureId, TEST_LOCATION); // texture is loaded.
738   DALI_TEST_EQUALS(loadingStatus, false, TEST_LOCATION);
739   DALI_TEST_CHECK(asyncTextureSet);  // Cached texture.
740
741   // observer is directly called because textureSet is retrieved by cache.
742   DALI_TEST_EQUALS(asyncObserver.mLoaded, true, TEST_LOCATION);
743   DALI_TEST_EQUALS(asyncObserver.mObserverCalled, true, TEST_LOCATION);
744
745   END_TEST;
746 }
747
748 int UtcTextureManagerAsyncSyncAsync(void)
749 {
750   ToolkitTestApplication application;
751   tet_infoline( "UtcTextureManagerAsyncSyncAsync" );
752
753   TextureManager textureManager; // Create new texture manager
754
755   std::string filename( TEST_IMAGE_FILE_NAME );
756
757   std::string maskname("");
758   TextureManager::MaskingDataPointer maskInfo = nullptr;
759   maskInfo.reset(new TextureManager::MaskingData());
760   maskInfo->mAlphaMaskUrl = maskname;
761   maskInfo->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
762   maskInfo->mCropToMask = true;
763   maskInfo->mContentScaleFactor = 1.0f;
764
765   Vector4 atlasRect( 0.f, 0.f, 0.f, 0.f );
766   Dali::ImageDimensions atlasRectSize( 0,0 );
767   bool atlasingStatus(false);
768   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
769   ImageAtlasManagerPtr atlasManager = nullptr;
770   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
771
772   // load image asynchronously.
773   TestObserver asyncObserver1;
774   auto asyncTextureId1( TextureManager::INVALID_TEXTURE_ID );
775   bool asyncLoadingStatus1 = false;
776   TextureSet asyncTextureSet1 = textureManager.LoadTexture(
777     filename,
778     ImageDimensions(),
779     FittingMode::SCALE_TO_FILL,
780     SamplingMode::BOX_THEN_LINEAR,
781     maskInfo,
782     false, // asynchronous loading.
783     asyncTextureId1,
784     atlasRect,
785     atlasRectSize,
786     atlasingStatus,
787     asyncLoadingStatus1,
788     WrapMode::DEFAULT,
789     WrapMode::DEFAULT,
790     &asyncObserver1,
791     atlasUploadObserver,
792     atlasManager,
793     true,
794     TextureManager::ReloadPolicy::CACHED,
795     preMultiply
796   );
797
798   DALI_TEST_EQUALS(asyncLoadingStatus1, true, TEST_LOCATION); // texture is loading now.
799   DALI_TEST_CHECK(!asyncTextureSet1);  // texture is not loaded yet.
800
801   // observer is still not called.
802   DALI_TEST_EQUALS(asyncObserver1.mLoaded, false, TEST_LOCATION);
803   DALI_TEST_EQUALS(asyncObserver1.mObserverCalled, false, TEST_LOCATION);
804
805
806   // load same image synchronously just after asynchronous loading.
807   TestObserver syncObserver;
808   auto textureId( TextureManager::INVALID_TEXTURE_ID );
809   bool syncLoadingStatus = false;
810   TextureSet syncTextureSet = textureManager.LoadTexture(
811     filename,
812     ImageDimensions(),
813     FittingMode::SCALE_TO_FILL,
814     SamplingMode::BOX_THEN_LINEAR,
815     maskInfo,
816     true, // synchronous loading.
817     textureId,
818     atlasRect,
819     atlasRectSize,
820     atlasingStatus,
821     syncLoadingStatus,
822     WrapMode::DEFAULT,
823     WrapMode::DEFAULT,
824     &syncObserver,
825     atlasUploadObserver,
826     atlasManager,
827     true,
828     TextureManager::ReloadPolicy::CACHED,
829     preMultiply
830   );
831
832   DALI_TEST_EQUALS(asyncTextureId1, textureId, TEST_LOCATION); // texture is loaded.
833   DALI_TEST_EQUALS(syncLoadingStatus, false, TEST_LOCATION); // texture is loaded.
834   DALI_TEST_CHECK(syncTextureSet);  // texture is loaded.
835
836   // syncObserver isn't called in synchronous loading.
837   DALI_TEST_EQUALS(syncObserver.mLoaded, false, TEST_LOCATION);
838   DALI_TEST_EQUALS(syncObserver.mObserverCalled, false, TEST_LOCATION);
839
840   // asyncObserver1 is still not called too.
841   DALI_TEST_EQUALS(asyncObserver1.mLoaded, false, TEST_LOCATION);
842   DALI_TEST_EQUALS(asyncObserver1.mObserverCalled, false, TEST_LOCATION);
843
844
845
846   // load image asynchronously.
847   TestObserver asyncObserver2;
848   auto asyncTextureId2( TextureManager::INVALID_TEXTURE_ID );
849   bool asyncLoadingStatus2 = false;
850   TextureSet asyncTextureSet2 = textureManager.LoadTexture(
851     filename,
852     ImageDimensions(),
853     FittingMode::SCALE_TO_FILL,
854     SamplingMode::BOX_THEN_LINEAR,
855     maskInfo,
856     false, // asynchronous loading.
857     asyncTextureId2,
858     atlasRect,
859     atlasRectSize,
860     atlasingStatus,
861     asyncLoadingStatus2,
862     WrapMode::DEFAULT,
863     WrapMode::DEFAULT,
864     &asyncObserver2,
865     atlasUploadObserver,
866     atlasManager,
867     true,
868     TextureManager::ReloadPolicy::CACHED,
869     preMultiply
870   );
871
872   DALI_TEST_EQUALS(asyncLoadingStatus2, false, TEST_LOCATION); // texture is loaded by previous sync request
873   DALI_TEST_CHECK(asyncTextureSet2); // texture is loaded
874   DALI_TEST_CHECK(asyncTextureSet2 == syncTextureSet);  // check loaded two texture is same.
875
876   // observer is called synchronously because the texture is cached.
877   DALI_TEST_EQUALS(asyncObserver2.mLoaded, true, TEST_LOCATION);
878   DALI_TEST_EQUALS(asyncObserver2.mObserverCalled, true, TEST_LOCATION);
879
880   asyncObserver2.mLoaded = false;
881   asyncObserver2.mObserverCalled = false;
882
883   application.SendNotification();
884   application.Render();
885
886   // Requested asynchronous loading at first is finished now and async observer is called now.
887   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
888   DALI_TEST_EQUALS(asyncObserver1.mLoaded, true, TEST_LOCATION);
889   DALI_TEST_EQUALS(asyncObserver1.mObserverCalled, true, TEST_LOCATION);
890   DALI_TEST_CHECK(asyncObserver1.mTextureSet == asyncTextureSet2);  // check loaded two texture is same.
891
892   // asyncObserver2 was already called so it isn't called here.
893   DALI_TEST_EQUALS(asyncObserver2.mLoaded, false, TEST_LOCATION);
894   DALI_TEST_EQUALS(asyncObserver2.mObserverCalled, false, TEST_LOCATION);
895
896   END_TEST;
897 }