2 * Copyright (c) 2022 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/dali.h>
20 #include <dali/devel-api/actors/actor-devel.h>
22 #include <toolkit-event-thread-callback.h>
26 using namespace Dali::Toolkit;
30 // resolution: 34*34, pixel format: RGBA8888
31 static const char* gImage_34_RGBA = TEST_RESOURCE_DIR "/icon-edit.png";
32 // resolution: 50*50, pixel format: RGBA8888
33 static const char* gImage_50_RGBA = TEST_RESOURCE_DIR "/icon-delete.png";
34 // resolution: 128*128, pixel format: RGB888
35 static const char* gImage_128_RGB = TEST_RESOURCE_DIR "/gallery-small-1.jpg";
37 // for testing the ImageLoadedSignal
38 class ImageLoadedSignalVerifier : public ConnectionTracker
41 ImageLoadedSignalVerifier()
46 virtual ~ImageLoadedSignalVerifier()
50 void ImageLoaded(uint32_t id, PixelData pixelData)
53 mPixelDataList.push_back(pixelData);
57 int LoadedImageCount()
62 bool Verify(uint32_t id, uint32_t width, uint32_t height)
64 int size = mIDs.size();
65 for(int i = 0; i < size; i++)
69 return mPixelDataList[i].GetWidth() == width && mPixelDataList[i].GetHeight() == height;
79 std::vector<uint32_t> mIDs;
80 std::vector<PixelData> mPixelDataList;
83 } // anonymous namespace
85 void dali_async_image_loader_startup(void)
87 test_return_value = TET_UNDEF;
90 void dali_async_image_loader_cleanup(void)
92 test_return_value = TET_PASS;
95 int UtcDaliImageAtlasNew01(void)
97 ToolkitTestApplication application;
99 //invoke default handle constructor
100 AsyncImageLoader loader;
102 DALI_TEST_CHECK(!loader);
105 loader = AsyncImageLoader::New();
106 DALI_TEST_CHECK(loader);
111 int UtcDaliAsyncImageLoaderCopyConstructor(void)
113 ToolkitTestApplication application;
115 AsyncImageLoader loader = AsyncImageLoader::New();
116 DALI_TEST_CHECK(loader);
118 AsyncImageLoader loaderCopy(loader);
119 DALI_TEST_CHECK(loaderCopy);
124 int UtcDaliAsyncImageLoaderMoveConstructor(void)
126 ToolkitTestApplication application;
128 AsyncImageLoader loader = AsyncImageLoader::New();
129 DALI_TEST_CHECK(loader);
130 DALI_TEST_EQUALS(1, loader.GetBaseObject().ReferenceCount(), TEST_LOCATION);
132 AsyncImageLoader moved = std::move(loader);
133 DALI_TEST_CHECK(moved);
134 DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
135 DALI_TEST_CHECK(!loader);
140 int UtcDaliAsyncImageLoaderAssignmentOperator(void)
142 ToolkitTestApplication application;
144 AsyncImageLoader loader = AsyncImageLoader::New();
145 DALI_TEST_CHECK(loader);
147 AsyncImageLoader loader2;
148 DALI_TEST_CHECK(!loader2);
151 DALI_TEST_CHECK(loader2);
152 DALI_TEST_CHECK(loader == loader2); // the two handles are pointing to the same object.
157 int UtcDaliAsyncImageLoaderMoveAssignment(void)
159 ToolkitTestApplication application;
161 AsyncImageLoader loader = AsyncImageLoader::New();
162 DALI_TEST_CHECK(loader);
163 DALI_TEST_EQUALS(1, loader.GetBaseObject().ReferenceCount(), TEST_LOCATION);
165 AsyncImageLoader moved;
166 moved = std::move(loader);
167 DALI_TEST_CHECK(moved);
168 DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
169 DALI_TEST_CHECK(!loader);
174 int UtcDaliAsyncImageLoaderDownCastP(void)
176 ToolkitTestApplication application;
178 AsyncImageLoader asyncImageLoader = AsyncImageLoader::New();
179 BaseHandle object(asyncImageLoader);
181 AsyncImageLoader asyncImageLoader2 = AsyncImageLoader::DownCast(object);
183 DALI_TEST_CHECK(asyncImageLoader2);
188 int UtcDaliAsyncImageLoaderDownCastN(void)
190 ToolkitTestApplication application;
192 BaseHandle unInitializedObject;
193 AsyncImageLoader asyncImageLoader = AsyncImageLoader::DownCast(unInitializedObject);
195 DALI_TEST_CHECK(!asyncImageLoader);
200 int UtcDaliAsyncImageLoaderLoadAndLoadedSignal(void)
202 ToolkitTestApplication application;
204 AsyncImageLoader loader = AsyncImageLoader::New();
205 ImageLoadedSignalVerifier loadedSignalVerifier;
207 loader.ImageLoadedSignal().Connect(&loadedSignalVerifier, &ImageLoadedSignalVerifier::ImageLoaded);
209 loader.Load(gImage_34_RGBA);
210 uint32_t id02 = loader.Load(gImage_50_RGBA, ImageDimensions(25, 25));
211 uint32_t id03 = loader.Load(gImage_128_RGB, ImageDimensions(100, 100), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, true);
213 DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(3), true, TEST_LOCATION);
215 application.SendNotification();
216 application.Render();
218 DALI_TEST_CHECK(loadedSignalVerifier.LoadedImageCount() == 3);
219 DALI_TEST_CHECK(loadedSignalVerifier.Verify(id02, 25, 25));
220 DALI_TEST_CHECK(loadedSignalVerifier.Verify(id03, 100, 100));
225 // Note: This is not an ideal test, but we cannot guarantee we can call Cancel() before the image has finished loading.
226 int UtcDaliAsyncImageLoaderCancel(void)
228 ToolkitTestApplication application;
230 AsyncImageLoader loader = AsyncImageLoader::New();
231 ImageLoadedSignalVerifier loadedSignalVerifier;
233 loader.ImageLoadedSignal().Connect(&loadedSignalVerifier, &ImageLoadedSignalVerifier::ImageLoaded);
235 uint32_t id01 = loader.Load(gImage_34_RGBA, ImageDimensions(34, 34));
236 uint32_t id02 = loader.Load(gImage_50_RGBA, ImageDimensions(25, 25));
237 uint32_t id03 = loader.Load(gImage_128_RGB, ImageDimensions(100, 100), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, true);
239 DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(3), true, TEST_LOCATION);
241 application.SendNotification();
242 application.Render();
244 DALI_TEST_CHECK(loadedSignalVerifier.LoadedImageCount() == 3);
246 DALI_TEST_CHECK(!loader.Cancel(id03)); // Cannot cancel a task that is already implemeted
248 DALI_TEST_CHECK(loadedSignalVerifier.Verify(id01, 34, 34)); // first image is loaded
249 DALI_TEST_CHECK(loadedSignalVerifier.Verify(id02, 25, 25)); // second image is loaded
250 DALI_TEST_CHECK(loadedSignalVerifier.Verify(id03, 100, 100)); // third image is loaded
255 int UtcDaliAsyncImageLoaderCancelAll(void)
257 ToolkitTestApplication application;
259 AsyncImageLoader loader = AsyncImageLoader::New();
261 // Test that it is safe to call CancelAll even there is no loading task requested.
266 catch(Dali::DaliException& e)
268 DALI_TEST_ASSERT(e, "AsyncImageLoader::LoadAll", TEST_LOCATION);
271 // Test that cancelling a non-existing loading task will return false
273 DALI_TEST_CHECK(!(loader.Cancel(id)));