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