[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-AsyncImageLoader.cpp
1 /*
2  * Copyright (c) 2022 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 #include <dali-toolkit-test-suite-utils.h>
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali/dali.h>
20 #include <dali/devel-api/actors/actor-devel.h>
21 #include <stdlib.h>
22 #include <toolkit-event-thread-callback.h>
23 #include <unistd.h>
24 #include <dali-toolkit/devel-api/image-loader/async-image-loader-devel.h>
25
26 using namespace Dali;
27 using namespace Dali::Toolkit;
28
29 namespace
30 {
31 // resolution: 34*34, pixel format: RGBA8888
32 static const char* gImage_34_RGBA = TEST_RESOURCE_DIR "/icon-edit.png";
33 // resolution: 50*50, pixel format: RGBA8888
34 static const char* gImage_50_RGBA = TEST_RESOURCE_DIR "/icon-delete.png";
35 // resolution: 128*128, pixel format: RGB888
36 static const char* gImage_128_RGB = TEST_RESOURCE_DIR "/gallery-small-1.jpg";
37 // animated image
38 static const char* gImage_gif = TEST_RESOURCE_DIR "/canvas-none.gif";
39
40 // for testing the ImageLoadedSignal
41 class ImageLoadedSignalVerifier : public ConnectionTracker
42 {
43 public:
44   ImageLoadedSignalVerifier()
45   : mCount(0)
46   {
47   }
48
49   virtual ~ImageLoadedSignalVerifier()
50   {
51   }
52
53   void ImageLoaded(uint32_t id, PixelData pixelData)
54   {
55     if(pixelData)
56     {
57       mIDs.push_back(id);
58       mPixelDataList.push_back(pixelData);
59     }
60     mCount++;
61   }
62
63   int LoadedImageCount()
64   {
65     return mCount;
66   }
67
68   bool Verify(uint32_t id, uint32_t width, uint32_t height)
69   {
70     int size = mIDs.size();
71     for(int i = 0; i < size; i++)
72     {
73       if(mIDs[i] == id)
74       {
75         return mPixelDataList[i].GetWidth() == width && mPixelDataList[i].GetHeight() == height;
76       }
77     }
78
79     return false;
80   }
81
82 private:
83   int mCount;
84
85   std::vector<uint32_t>  mIDs;
86   std::vector<PixelData> mPixelDataList;
87 };
88
89 } // anonymous namespace
90
91 void dali_async_image_loader_startup(void)
92 {
93   test_return_value = TET_UNDEF;
94 }
95
96 void dali_async_image_loader_cleanup(void)
97 {
98   test_return_value = TET_PASS;
99 }
100
101 int UtcDaliImageAtlasNew01(void)
102 {
103   ToolkitTestApplication application;
104
105   //invoke default handle constructor
106   AsyncImageLoader loader;
107
108   DALI_TEST_CHECK(!loader);
109
110   // initialise handle
111   loader = AsyncImageLoader::New();
112   DALI_TEST_CHECK(loader);
113
114   END_TEST;
115 }
116
117 int UtcDaliAsyncImageLoaderCopyConstructor(void)
118 {
119   ToolkitTestApplication application;
120
121   AsyncImageLoader loader = AsyncImageLoader::New();
122   DALI_TEST_CHECK(loader);
123
124   AsyncImageLoader loaderCopy(loader);
125   DALI_TEST_CHECK(loaderCopy);
126
127   END_TEST;
128 }
129
130 int UtcDaliAsyncImageLoaderMoveConstructor(void)
131 {
132   ToolkitTestApplication application;
133
134   AsyncImageLoader loader = AsyncImageLoader::New();
135   DALI_TEST_CHECK(loader);
136   DALI_TEST_EQUALS(1, loader.GetBaseObject().ReferenceCount(), TEST_LOCATION);
137
138   AsyncImageLoader moved = std::move(loader);
139   DALI_TEST_CHECK(moved);
140   DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
141   DALI_TEST_CHECK(!loader);
142
143   END_TEST;
144 }
145
146 int UtcDaliAsyncImageLoaderAssignmentOperator(void)
147 {
148   ToolkitTestApplication application;
149
150   AsyncImageLoader loader = AsyncImageLoader::New();
151   DALI_TEST_CHECK(loader);
152
153   AsyncImageLoader loader2;
154   DALI_TEST_CHECK(!loader2);
155
156   loader2 = loader;
157   DALI_TEST_CHECK(loader2);
158   DALI_TEST_CHECK(loader == loader2); // the two handles are pointing to the same object.
159
160   END_TEST;
161 }
162
163 int UtcDaliAsyncImageLoaderMoveAssignment(void)
164 {
165   ToolkitTestApplication application;
166
167   AsyncImageLoader loader = AsyncImageLoader::New();
168   DALI_TEST_CHECK(loader);
169   DALI_TEST_EQUALS(1, loader.GetBaseObject().ReferenceCount(), TEST_LOCATION);
170
171   AsyncImageLoader moved;
172   moved = std::move(loader);
173   DALI_TEST_CHECK(moved);
174   DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
175   DALI_TEST_CHECK(!loader);
176
177   END_TEST;
178 }
179
180 int UtcDaliAsyncImageLoaderDownCastP(void)
181 {
182   ToolkitTestApplication application;
183
184   AsyncImageLoader asyncImageLoader = AsyncImageLoader::New();
185   BaseHandle       object(asyncImageLoader);
186
187   AsyncImageLoader asyncImageLoader2 = AsyncImageLoader::DownCast(object);
188
189   DALI_TEST_CHECK(asyncImageLoader2);
190
191   END_TEST;
192 }
193
194 int UtcDaliAsyncImageLoaderDownCastN(void)
195 {
196   ToolkitTestApplication application;
197
198   BaseHandle       unInitializedObject;
199   AsyncImageLoader asyncImageLoader = AsyncImageLoader::DownCast(unInitializedObject);
200
201   DALI_TEST_CHECK(!asyncImageLoader);
202
203   END_TEST;
204 }
205
206 int UtcDaliAsyncImageLoaderLoadAndLoadedSignal(void)
207 {
208   ToolkitTestApplication application;
209
210   AsyncImageLoader          loader = AsyncImageLoader::New();
211   ImageLoadedSignalVerifier loadedSignalVerifier;
212
213   loader.ImageLoadedSignal().Connect(&loadedSignalVerifier, &ImageLoadedSignalVerifier::ImageLoaded);
214
215   loader.Load(gImage_34_RGBA);
216   uint32_t id02 = loader.Load(gImage_50_RGBA, ImageDimensions(25, 25));
217   uint32_t id03 = loader.Load(gImage_128_RGB, ImageDimensions(100, 100), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, true);
218
219   // Try load animted image
220   Dali::AnimatedImageLoading animatedImageLoading = Dali::AnimatedImageLoading::New(gImage_gif, true);
221   DevelAsyncImageLoader::LoadAnimatedImage(loader, animatedImageLoading, 0, DevelAsyncImageLoader::PreMultiplyOnLoad::OFF);
222
223   // Try apply mask image
224   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(50, 50, Dali::Pixel::RGBA8888);
225   Devel::PixelBuffer maskData = Devel::PixelBuffer::New(50, 50, Dali::Pixel::RGBA8888);
226   DevelAsyncImageLoader::ApplyMask(loader, imageData, maskData, 0.0f, false, DevelAsyncImageLoader::PreMultiplyOnLoad::OFF);
227
228   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(4), true, TEST_LOCATION);
229
230   application.SendNotification();
231   application.Render();
232
233   DALI_TEST_CHECK(loadedSignalVerifier.LoadedImageCount() == 4);
234   DALI_TEST_CHECK(loadedSignalVerifier.Verify(id02, 25, 25));
235   DALI_TEST_CHECK(loadedSignalVerifier.Verify(id03, 100, 100));
236
237   END_TEST;
238 }
239
240 // Note: This is not an ideal test, but we cannot guarantee we can call Cancel() before the image has finished loading.
241 int UtcDaliAsyncImageLoaderCancel01(void)
242 {
243   ToolkitTestApplication application;
244
245   AsyncImageLoader          loader = AsyncImageLoader::New();
246   ImageLoadedSignalVerifier loadedSignalVerifier;
247
248   loader.ImageLoadedSignal().Connect(&loadedSignalVerifier, &ImageLoadedSignalVerifier::ImageLoaded);
249
250   uint32_t id01 = loader.Load(gImage_34_RGBA, ImageDimensions(34, 34));
251   uint32_t id02 = loader.Load(gImage_50_RGBA, ImageDimensions(25, 25));
252   uint32_t id03 = loader.Load(gImage_128_RGB, ImageDimensions(100, 100), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, true);
253
254   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(3), true, TEST_LOCATION);
255
256   application.SendNotification();
257   application.Render();
258
259   DALI_TEST_CHECK(loadedSignalVerifier.LoadedImageCount() == 3);
260
261   DALI_TEST_CHECK(!loader.Cancel(id03)); // Cannot cancel a task that is already implemeted
262
263   DALI_TEST_CHECK(loadedSignalVerifier.Verify(id01, 34, 34));   // first image is loaded
264   DALI_TEST_CHECK(loadedSignalVerifier.Verify(id02, 25, 25));   // second image is loaded
265   DALI_TEST_CHECK(loadedSignalVerifier.Verify(id03, 100, 100)); // third image is loaded
266
267   END_TEST;
268 }
269
270 int UtcDaliAsyncImageLoaderCancel02(void)
271 {
272   ToolkitTestApplication application;
273
274   AsyncImageLoader loader = AsyncImageLoader::New();
275   uint32_t id01 = loader.Load(gImage_34_RGBA, ImageDimensions(34, 34));
276   DALI_TEST_CHECK(loader.Cancel(id01)); // Cancle a task
277
278   application.SendNotification();
279   application.Render();
280   END_TEST;
281 }
282
283 int UtcDaliAsyncImageLoaderCancelAll(void)
284 {
285   ToolkitTestApplication application;
286
287   AsyncImageLoader loader = AsyncImageLoader::New();
288
289   // Test that it is safe to call CancelAll even there is no loading task requested.
290   try
291   {
292     loader.CancelAll();
293   }
294   catch(Dali::DaliException& e)
295   {
296     DALI_TEST_ASSERT(e, "AsyncImageLoader::LoadAll", TEST_LOCATION);
297   }
298
299   // Test that cancelling a non-existing loading task will return false
300   uint32_t id = 1;
301   DALI_TEST_CHECK(!(loader.Cancel(id)));
302
303   uint32_t id01 = loader.Load(gImage_34_RGBA, ImageDimensions(34, 34));
304   uint32_t id02 = loader.Load(gImage_50_RGBA, ImageDimensions(25, 25));
305   uint32_t id03 = loader.Load(gImage_128_RGB, ImageDimensions(100, 100), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, true);
306   loader.CancelAll();
307
308   // Test that cancelling a non-existing loading task will return false
309   DALI_TEST_CHECK(!(loader.Cancel(id01)));
310   DALI_TEST_CHECK(!(loader.Cancel(id02)));
311   DALI_TEST_CHECK(!(loader.Cancel(id03)));
312
313   application.SendNotification();
314   application.Render();
315
316   END_TEST;
317 }