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