c1ea9ad5f9f909d89c08b1fa06c0a0fd65edb78f
[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
30 #include <automated-tests/src/dali-toolkit-internal/dali-toolkit-test-utils/dbus-wrapper.h>
31
32 using namespace Dali::Toolkit::Internal;
33
34 void utc_dali_toolkit_texture_manager_startup(void)
35 {
36   setenv( "LOG_TEXTURE_MANAGER", "3", 1 );
37   test_return_value = TET_UNDEF;
38   DBusWrapper::Install(std::unique_ptr<DBusWrapper>(new TestDBusWrapper));
39 }
40
41 void utc_dali_toolkit_texture_manager_cleanup(void)
42 {
43   test_return_value = TET_PASS;
44 }
45
46 namespace
47 {
48
49 const char* TEST_IMAGE_FILE_NAME =  TEST_RESOURCE_DIR "/gallery-small-1.jpg";
50
51 }
52
53 class TestObserver : public Dali::Toolkit::TextureUploadObserver
54 {
55 public:
56   enum class CompleteType
57   {
58     NOT_COMPLETED = 0,
59     UPLOAD_COMPLETE,
60     LOAD_COMPLETE
61   };
62
63 public:
64   TestObserver()
65   : mCompleteType( CompleteType::NOT_COMPLETED ),
66     mLoaded(false),
67     mObserverCalled(false)
68   {
69   }
70
71   virtual void UploadComplete( bool loadSuccess, int32_t textureId, TextureSet textureSet,
72                                bool useAtlasing, const Vector4& atlasRect, bool preMultiplied ) override
73   {
74     mCompleteType = CompleteType::UPLOAD_COMPLETE;
75     mLoaded = loadSuccess;
76     mObserverCalled = true;
77   }
78
79   virtual void LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied ) override
80   {
81     mCompleteType = CompleteType::LOAD_COMPLETE;
82     mLoaded = loadSuccess;
83     mObserverCalled = true;
84   }
85
86   CompleteType mCompleteType;
87   bool mLoaded;
88   bool mObserverCalled;
89 };
90
91
92 int UtcTextureManagerRequestLoad(void)
93 {
94   ToolkitTestApplication application;
95
96   TextureManager textureManager; // Create new texture manager
97
98   TestObserver observer;
99   std::string filename("image.png");
100   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
101   TextureManager::TextureId textureId = textureManager.RequestLoad(
102     filename,
103     ImageDimensions(),
104     FittingMode::SCALE_TO_FILL,
105     SamplingMode::BOX_THEN_LINEAR,
106     TextureManager::NO_ATLAS,
107     &observer,
108     true,
109     TextureManager::ReloadPolicy::CACHED,
110     preMultiply);
111
112   VisualUrl url = textureManager.GetVisualUrl( textureId );
113
114   DALI_TEST_EQUALS( url.GetUrl().compare( filename ), 0, TEST_LOCATION );
115
116   END_TEST;
117 }
118
119 int UtcTextureManagerGenerateHash(void)
120 {
121   ToolkitTestApplication application;
122
123   TextureManager textureManager; // Create new texture manager
124
125   TestObserver observer;
126   std::string filename( "image.png" );
127   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
128   TextureManager::TextureId textureId = textureManager.RequestLoad(
129     filename,
130     ImageDimensions(),
131     FittingMode::SCALE_TO_FILL,
132     SamplingMode::BOX_THEN_LINEAR,
133     TextureManager::USE_ATLAS,
134     &observer,
135     true,
136     TextureManager::ReloadPolicy::CACHED,
137     preMultiply);
138
139   VisualUrl url = textureManager.GetVisualUrl( textureId );
140
141   DALI_TEST_EQUALS( url.GetUrl().compare( filename ), 0, TEST_LOCATION );
142
143   END_TEST;
144 }
145
146 int UtcTextureManagerCachingForDifferentLoadingType(void)
147 {
148   ToolkitTestApplication application;
149   tet_infoline( "UtcTextureManagerCachingForDifferentLoadingType" );
150
151   TextureManager textureManager; // Create new texture manager
152
153   TestObserver observer1;
154   std::string filename( TEST_IMAGE_FILE_NAME );
155   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
156   textureManager.RequestLoad(
157     filename,
158     ImageDimensions(),
159     FittingMode::SCALE_TO_FILL,
160     SamplingMode::BOX_THEN_LINEAR,
161     TextureManager::NO_ATLAS,
162     &observer1,
163     true,
164     TextureManager::ReloadPolicy::CACHED,
165     preMultiply);
166
167   DALI_TEST_EQUALS( observer1.mLoaded, false, TEST_LOCATION );
168   DALI_TEST_EQUALS( observer1.mObserverCalled, false, TEST_LOCATION );
169
170   application.SendNotification();
171   application.Render();
172
173   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
174
175   application.SendNotification();
176   application.Render();
177
178   DALI_TEST_EQUALS( observer1.mLoaded, true, TEST_LOCATION );
179   DALI_TEST_EQUALS( observer1.mObserverCalled, true, TEST_LOCATION );
180   DALI_TEST_EQUALS( observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );
181
182   TestObserver observer2;
183   Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
184     filename,
185     ImageDimensions(),
186     FittingMode::SCALE_TO_FILL,
187     SamplingMode::BOX_THEN_LINEAR,
188     false,
189     &observer2,
190     true,
191     preMultiply);
192
193   DALI_TEST_EQUALS( observer2.mLoaded, false, TEST_LOCATION );
194   DALI_TEST_EQUALS( observer2.mObserverCalled, false, TEST_LOCATION );
195
196   application.SendNotification();
197   application.Render();
198
199   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
200
201   application.SendNotification();
202   application.Render();
203
204   DALI_TEST_EQUALS( observer2.mLoaded, true, TEST_LOCATION );
205   DALI_TEST_EQUALS( observer2.mObserverCalled, true, TEST_LOCATION );
206   DALI_TEST_EQUALS( observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION );
207
208   END_TEST;
209 }
210
211 int UtcTextureManagerUseInvalidMask(void)
212 {
213   ToolkitTestApplication application;
214   tet_infoline( "UtcTextureManagerUseInvalidMask" );
215
216   TextureManager textureManager; // Create new texture manager
217
218   TestObserver observer;
219   std::string filename( TEST_IMAGE_FILE_NAME );
220   std::string maskname("");
221   TextureManager::MaskingDataPointer maskInfo = nullptr;
222   maskInfo.reset(new TextureManager::MaskingData());
223   maskInfo->mAlphaMaskUrl = maskname;
224   maskInfo->mAlphaMaskId = TextureManager::INVALID_TEXTURE_ID;
225   maskInfo->mCropToMask = true;
226   maskInfo->mContentScaleFactor = 1.0f;
227
228   auto textureId( TextureManager::INVALID_TEXTURE_ID );
229   Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
230   Dali::ImageDimensions atlasRectSize( 0,0 );
231   bool synchronousLoading(false);
232   bool atlasingStatus(false);
233   bool loadingStatus(false);
234   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
235   ImageAtlasManagerPtr atlasManager = nullptr;
236   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
237
238   textureManager.LoadTexture(
239     filename,
240     ImageDimensions(),
241     FittingMode::SCALE_TO_FILL,
242     SamplingMode::BOX_THEN_LINEAR,
243     maskInfo,
244     synchronousLoading,
245     textureId,
246     atlasRect,
247     atlasRectSize,
248     atlasingStatus,
249     loadingStatus,
250     WrapMode::DEFAULT,
251     WrapMode::DEFAULT,
252     &observer,
253     atlasUploadObserver,
254     atlasManager,
255     true,
256     TextureManager::ReloadPolicy::CACHED,
257     preMultiply
258   );
259
260   DALI_TEST_EQUALS( observer.mLoaded, false, TEST_LOCATION );
261   DALI_TEST_EQUALS( observer.mObserverCalled, false, TEST_LOCATION );
262
263   application.SendNotification();
264   application.Render();
265
266   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
267
268   application.SendNotification();
269   application.Render();
270
271   DALI_TEST_EQUALS( observer.mLoaded, true, TEST_LOCATION );
272   DALI_TEST_EQUALS( observer.mObserverCalled, true, TEST_LOCATION );
273   DALI_TEST_EQUALS( observer.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );
274
275   END_TEST;
276 }