2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <dali-toolkit-test-suite-utils.h>
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/image-loader/async-image-loader-devel.h>
20 #include <dali/dali.h>
21 #include <dali/devel-api/actors/actor-devel.h>
23 #include <toolkit-event-thread-callback.h>
27 using namespace Dali::Toolkit;
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";
38 static const char* gImage_gif = TEST_RESOURCE_DIR "/canvas-none.gif";
40 // for testing the ImageLoadedSignal
41 class ImageLoadedSignalVerifier : public ConnectionTracker
44 ImageLoadedSignalVerifier()
49 virtual ~ImageLoadedSignalVerifier()
53 void ImageLoaded(uint32_t id, PixelData pixelData)
58 mPixelDataList.push_back(pixelData);
63 int LoadedImageCount()
68 bool Verify(uint32_t id, uint32_t width, uint32_t height)
70 int size = mIDs.size();
71 for(int i = 0; i < size; i++)
75 return mPixelDataList[i].GetWidth() == width && mPixelDataList[i].GetHeight() == height;
85 std::vector<uint32_t> mIDs;
86 std::vector<PixelData> mPixelDataList;
89 } // anonymous namespace
91 void dali_async_image_loader_startup(void)
93 test_return_value = TET_UNDEF;
96 void dali_async_image_loader_cleanup(void)
98 test_return_value = TET_PASS;
101 int UtcDaliImageAtlasNew01(void)
103 ToolkitTestApplication application;
105 //invoke default handle constructor
106 AsyncImageLoader loader;
108 DALI_TEST_CHECK(!loader);
111 loader = AsyncImageLoader::New();
112 DALI_TEST_CHECK(loader);
117 int UtcDaliAsyncImageLoaderCopyConstructor(void)
119 ToolkitTestApplication application;
121 AsyncImageLoader loader = AsyncImageLoader::New();
122 DALI_TEST_CHECK(loader);
124 AsyncImageLoader loaderCopy(loader);
125 DALI_TEST_CHECK(loaderCopy);
130 int UtcDaliAsyncImageLoaderMoveConstructor(void)
132 ToolkitTestApplication application;
134 AsyncImageLoader loader = AsyncImageLoader::New();
135 DALI_TEST_CHECK(loader);
136 DALI_TEST_EQUALS(1, loader.GetBaseObject().ReferenceCount(), TEST_LOCATION);
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);
146 int UtcDaliAsyncImageLoaderAssignmentOperator(void)
148 ToolkitTestApplication application;
150 AsyncImageLoader loader = AsyncImageLoader::New();
151 DALI_TEST_CHECK(loader);
153 AsyncImageLoader loader2;
154 DALI_TEST_CHECK(!loader2);
157 DALI_TEST_CHECK(loader2);
158 DALI_TEST_CHECK(loader == loader2); // the two handles are pointing to the same object.
163 int UtcDaliAsyncImageLoaderMoveAssignment(void)
165 ToolkitTestApplication application;
167 AsyncImageLoader loader = AsyncImageLoader::New();
168 DALI_TEST_CHECK(loader);
169 DALI_TEST_EQUALS(1, loader.GetBaseObject().ReferenceCount(), TEST_LOCATION);
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);
180 int UtcDaliAsyncImageLoaderDownCastP(void)
182 ToolkitTestApplication application;
184 AsyncImageLoader asyncImageLoader = AsyncImageLoader::New();
185 BaseHandle object(asyncImageLoader);
187 AsyncImageLoader asyncImageLoader2 = AsyncImageLoader::DownCast(object);
189 DALI_TEST_CHECK(asyncImageLoader2);
194 int UtcDaliAsyncImageLoaderDownCastN(void)
196 ToolkitTestApplication application;
198 BaseHandle unInitializedObject;
199 AsyncImageLoader asyncImageLoader = AsyncImageLoader::DownCast(unInitializedObject);
201 DALI_TEST_CHECK(!asyncImageLoader);
206 int UtcDaliAsyncImageLoaderLoadAndLoadedSignal(void)
208 ToolkitTestApplication application;
210 AsyncImageLoader loader = AsyncImageLoader::New();
211 ImageLoadedSignalVerifier loadedSignalVerifier;
213 loader.ImageLoadedSignal().Connect(&loadedSignalVerifier, &ImageLoadedSignalVerifier::ImageLoaded);
215 loader.Load(gImage_34_RGBA); // Trigger 1
216 uint32_t id02 = loader.Load(gImage_50_RGBA, ImageDimensions(25, 25)); // Trigger 2
217 uint32_t id03 = loader.Load(gImage_128_RGB, ImageDimensions(100, 100), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, true); // Trigger 3
219 // Try load animted image
220 Dali::AnimatedImageLoading animatedImageLoading = Dali::AnimatedImageLoading::New(gImage_gif, true);
221 DevelAsyncImageLoader::LoadAnimatedImage(loader, animatedImageLoading, 0, DevelAsyncImageLoader::PreMultiplyOnLoad::OFF); // Trigger 4
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); // Trigger 5 - ApplyMask also uses AsyncTask
228 DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(5), true, TEST_LOCATION); // Wait for 5 triggers
230 application.SendNotification();
231 application.Render();
233 DALI_TEST_CHECK(loadedSignalVerifier.LoadedImageCount() == 5);
234 DALI_TEST_CHECK(loadedSignalVerifier.Verify(id02, 25, 25));
235 DALI_TEST_CHECK(loadedSignalVerifier.Verify(id03, 100, 100));
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)
243 ToolkitTestApplication application;
245 AsyncImageLoader loader = AsyncImageLoader::New();
246 ImageLoadedSignalVerifier loadedSignalVerifier;
248 loader.ImageLoadedSignal().Connect(&loadedSignalVerifier, &ImageLoadedSignalVerifier::ImageLoaded);
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);
254 DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(3), true, TEST_LOCATION);
256 application.SendNotification();
257 application.Render();
259 DALI_TEST_CHECK(loadedSignalVerifier.LoadedImageCount() == 3);
261 DALI_TEST_CHECK(!loader.Cancel(id03)); // Cannot cancel a task that is already implemeted
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
270 int UtcDaliAsyncImageLoaderCancel02(void)
272 ToolkitTestApplication application;
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
278 application.SendNotification();
279 application.Render();
283 int UtcDaliAsyncImageLoaderCancelAll(void)
285 ToolkitTestApplication application;
287 AsyncImageLoader loader = AsyncImageLoader::New();
289 // Test that it is safe to call CancelAll even there is no loading task requested.
294 catch(Dali::DaliException& e)
296 DALI_TEST_ASSERT(e, "AsyncImageLoader::LoadAll", TEST_LOCATION);
299 // Test that cancelling a non-existing loading task will return false
301 DALI_TEST_CHECK(!(loader.Cancel(id)));
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);
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)));
313 application.SendNotification();
314 application.Render();