2 * Copyright (c) 2019 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.
19 #include <dali/dali.h>
20 #include <dali/devel-api/actors/actor-devel.h>
21 #include <dali-toolkit-test-suite-utils.h>
22 #include <toolkit-event-thread-callback.h>
23 #include <dali-toolkit/dali-toolkit.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
42 ImageLoadedSignalVerifier()
46 virtual ~ImageLoadedSignalVerifier()
49 void ImageLoaded( uint32_t id, PixelData pixelData )
52 mPixelDataList.push_back( pixelData );
56 int LoadedImageCount()
61 bool Verify( uint32_t id, uint32_t width, uint32_t height )
63 int size = mIDs.size();
64 for( int i = 0; i<size; i++ )
68 return mPixelDataList[i].GetWidth() == width
69 && mPixelDataList[i].GetHeight() == height;
80 std::vector<uint32_t> mIDs;
81 std::vector<PixelData> mPixelDataList;
85 } // anonymous namespace
87 void dali_async_image_loader_startup(void)
89 test_return_value = TET_UNDEF;
92 void dali_async_image_loader_cleanup(void)
94 test_return_value = TET_PASS;
97 int UtcDaliImageAtlasNew01(void)
99 ToolkitTestApplication application;
101 //invoke default handle constructor
102 AsyncImageLoader loader;
104 DALI_TEST_CHECK( !loader );
107 loader = AsyncImageLoader::New();
108 DALI_TEST_CHECK( loader );
113 int UtcDaliAsyncImageLoaderCopyConstructor(void)
115 ToolkitTestApplication application;
117 AsyncImageLoader loader = AsyncImageLoader::New( );
118 DALI_TEST_CHECK( loader );
120 AsyncImageLoader loaderCopy(loader);
121 DALI_TEST_CHECK( loaderCopy );
126 int UtcDaliAsyncImageLoaderAssignmentOperator(void)
128 ToolkitTestApplication application;
130 AsyncImageLoader loader = AsyncImageLoader::New();
131 DALI_TEST_CHECK( loader );
133 AsyncImageLoader loader2;
134 DALI_TEST_CHECK( !loader2 );
137 DALI_TEST_CHECK( loader2 );
138 DALI_TEST_CHECK( loader == loader2 ); // the two handles are pointing to the same object.
143 int UtcDaliAsyncImageLoaderDownCastP(void)
145 ToolkitTestApplication application;
147 AsyncImageLoader asyncImageLoader = AsyncImageLoader::New();
148 BaseHandle object(asyncImageLoader);
150 AsyncImageLoader asyncImageLoader2 = AsyncImageLoader::DownCast( object );
152 DALI_TEST_CHECK( asyncImageLoader2 );
157 int UtcDaliAsyncImageLoaderDownCastN(void)
159 ToolkitTestApplication application;
161 BaseHandle unInitializedObject;
162 AsyncImageLoader asyncImageLoader = AsyncImageLoader::DownCast( unInitializedObject );
164 DALI_TEST_CHECK( !asyncImageLoader );
169 int UtcDaliAsyncImageLoaderLoadAndLoadedSignal(void)
171 ToolkitTestApplication application;
173 AsyncImageLoader loader = AsyncImageLoader::New();
174 ImageLoadedSignalVerifier loadedSignalVerifier;
176 loader.ImageLoadedSignal().Connect( &loadedSignalVerifier, &ImageLoadedSignalVerifier::ImageLoaded );
178 loader.Load( gImage_34_RGBA );
179 uint32_t id02 = loader.Load( gImage_50_RGBA, ImageDimensions( 25, 25 ) );
180 uint32_t id03 = loader.Load( gImage_128_RGB, ImageDimensions( 100, 100 ), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, true );
182 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
184 application.SendNotification();
185 application.Render();
187 DALI_TEST_CHECK( loadedSignalVerifier.LoadedImageCount() == 3 );
188 DALI_TEST_CHECK( loadedSignalVerifier.Verify( id02, 25, 25 ) );
189 DALI_TEST_CHECK( loadedSignalVerifier.Verify( id03, 100, 100 ) );
194 // Note: This is not an ideal test, but we cannot guarantee we can call Cancel() before the image has finished loading.
195 int UtcDaliAsyncImageLoaderCancel(void)
197 ToolkitTestApplication application;
199 AsyncImageLoader loader = AsyncImageLoader::New();
200 ImageLoadedSignalVerifier loadedSignalVerifier;
202 loader.ImageLoadedSignal().Connect( &loadedSignalVerifier, &ImageLoadedSignalVerifier::ImageLoaded );
204 uint32_t id01 = loader.Load( gImage_34_RGBA, ImageDimensions( 34, 34 ) );
205 uint32_t id02 = loader.Load( gImage_50_RGBA, ImageDimensions( 25, 25 ) );
206 uint32_t id03 = loader.Load( gImage_128_RGB, ImageDimensions( 100, 100 ), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, true );
208 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
210 application.SendNotification();
211 application.Render();
213 DALI_TEST_CHECK( loadedSignalVerifier.LoadedImageCount() == 3 );
215 DALI_TEST_CHECK( !loader.Cancel( id03 ) ); // Cannot cancel a task that is already implemeted
217 DALI_TEST_CHECK( loadedSignalVerifier.Verify( id01, 34, 34 ) ); // first image is loaded
218 DALI_TEST_CHECK( loadedSignalVerifier.Verify( id02, 25, 25 ) ); // second image is loaded
219 DALI_TEST_CHECK( loadedSignalVerifier.Verify( id03, 100, 100 ) ); // third image is loaded
224 int UtcDaliAsyncImageLoaderCancelAll(void)
226 ToolkitTestApplication application;
228 AsyncImageLoader loader = AsyncImageLoader::New();
230 // Test that it is safe to call CancelAll even there is no loading task requested.
235 catch(Dali::DaliException& e)
237 DALI_TEST_ASSERT(e, "AsyncImageLoader::LoadAll", TEST_LOCATION);
240 // Test that cancelling a non-existing loading task will return false
242 DALI_TEST_CHECK( !(loader.Cancel( id )) );