Updates for const->constexpr
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-AsyncImageLoader.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 #include <stdlib.h>
18 #include <unistd.h>
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>
24
25 using namespace Dali;
26 using namespace Dali::Toolkit;
27
28 namespace
29 {
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";
36
37 // for testing the ImageLoadedSignal
38 class ImageLoadedSignalVerifier : public ConnectionTracker
39 {
40 public:
41
42   ImageLoadedSignalVerifier()
43   : mCount( 0 )
44   {}
45
46   virtual ~ImageLoadedSignalVerifier()
47   {}
48
49   void ImageLoaded( uint32_t id, PixelData pixelData )
50   {
51     mIDs.push_back( id );
52     mPixelDataList.push_back( pixelData );
53     mCount++;
54   }
55
56   int LoadedImageCount()
57   {
58     return mCount;
59   }
60
61   bool Verify( uint32_t id, uint32_t width, uint32_t height )
62   {
63     int size = mIDs.size();
64     for( int i = 0; i<size; i++  )
65     {
66       if( mIDs[i] == id )
67       {
68         return mPixelDataList[i].GetWidth() == width
69             && mPixelDataList[i].GetHeight() == height;
70       }
71     }
72
73     return false;
74   }
75
76 private:
77
78   int mCount;
79
80   std::vector<uint32_t> mIDs;
81   std::vector<PixelData> mPixelDataList;
82 };
83
84
85 } // anonymous namespace
86
87 void dali_async_image_loader_startup(void)
88 {
89   test_return_value = TET_UNDEF;
90 }
91
92 void dali_async_image_loader_cleanup(void)
93 {
94   test_return_value = TET_PASS;
95 }
96
97 int UtcDaliImageAtlasNew01(void)
98 {
99   ToolkitTestApplication application;
100
101   //invoke default handle constructor
102   AsyncImageLoader loader;
103
104   DALI_TEST_CHECK( !loader );
105
106   // initialise handle
107   loader = AsyncImageLoader::New();
108   DALI_TEST_CHECK( loader );
109
110   END_TEST;
111 }
112
113 int UtcDaliAsyncImageLoaderCopyConstructor(void)
114 {
115   ToolkitTestApplication application;
116
117   AsyncImageLoader loader = AsyncImageLoader::New( );
118   DALI_TEST_CHECK( loader );
119
120   AsyncImageLoader loaderCopy(loader);
121   DALI_TEST_CHECK( loaderCopy );
122
123   END_TEST;
124 }
125
126 int UtcDaliAsyncImageLoaderMoveConstructor(void)
127 {
128   ToolkitTestApplication application;
129
130   AsyncImageLoader loader = AsyncImageLoader::New( );
131   DALI_TEST_CHECK( loader );
132   DALI_TEST_EQUALS( 1, loader.GetBaseObject().ReferenceCount(), TEST_LOCATION );
133
134   AsyncImageLoader moved = std::move( loader );
135   DALI_TEST_CHECK( moved );
136   DALI_TEST_EQUALS( 1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION );
137   DALI_TEST_CHECK( !loader );
138
139   END_TEST;
140 }
141
142 int UtcDaliAsyncImageLoaderAssignmentOperator(void)
143 {
144   ToolkitTestApplication application;
145
146   AsyncImageLoader loader = AsyncImageLoader::New();
147   DALI_TEST_CHECK( loader );
148
149   AsyncImageLoader loader2;
150   DALI_TEST_CHECK( !loader2 );
151
152   loader2 = loader;
153   DALI_TEST_CHECK( loader2 );
154   DALI_TEST_CHECK( loader == loader2 ); // the two handles are pointing to the same object.
155
156   END_TEST;
157 }
158
159 int UtcDaliAsyncImageLoaderMoveAssignment(void)
160 {
161   ToolkitTestApplication application;
162
163   AsyncImageLoader loader = AsyncImageLoader::New( );
164   DALI_TEST_CHECK( loader );
165   DALI_TEST_EQUALS( 1, loader.GetBaseObject().ReferenceCount(), TEST_LOCATION );
166
167   AsyncImageLoader moved;
168   moved = std::move( loader );
169   DALI_TEST_CHECK( moved );
170   DALI_TEST_EQUALS( 1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION );
171   DALI_TEST_CHECK( !loader );
172
173   END_TEST;
174 }
175
176 int UtcDaliAsyncImageLoaderDownCastP(void)
177 {
178   ToolkitTestApplication application;
179
180   AsyncImageLoader asyncImageLoader = AsyncImageLoader::New();
181   BaseHandle object(asyncImageLoader);
182
183   AsyncImageLoader asyncImageLoader2 = AsyncImageLoader::DownCast( object );
184
185   DALI_TEST_CHECK( asyncImageLoader2 );
186
187   END_TEST;
188 }
189
190 int UtcDaliAsyncImageLoaderDownCastN(void)
191 {
192   ToolkitTestApplication application;
193
194   BaseHandle unInitializedObject;
195   AsyncImageLoader asyncImageLoader = AsyncImageLoader::DownCast( unInitializedObject );
196
197   DALI_TEST_CHECK( !asyncImageLoader );
198
199   END_TEST;
200 }
201
202 int UtcDaliAsyncImageLoaderLoadAndLoadedSignal(void)
203 {
204   ToolkitTestApplication application;
205
206   AsyncImageLoader loader = AsyncImageLoader::New();
207   ImageLoadedSignalVerifier loadedSignalVerifier;
208
209   loader.ImageLoadedSignal().Connect( &loadedSignalVerifier, &ImageLoadedSignalVerifier::ImageLoaded );
210
211   loader.Load( gImage_34_RGBA );
212   uint32_t id02 = loader.Load( gImage_50_RGBA, ImageDimensions( 25, 25 ) );
213   uint32_t id03 = loader.Load( gImage_128_RGB, ImageDimensions( 100, 100 ), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, true );
214
215   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
216
217   application.SendNotification();
218   application.Render();
219
220   DALI_TEST_CHECK( loadedSignalVerifier.LoadedImageCount() == 3 );
221   DALI_TEST_CHECK( loadedSignalVerifier.Verify( id02, 25, 25 ) );
222   DALI_TEST_CHECK( loadedSignalVerifier.Verify( id03, 100, 100 ) );
223
224   END_TEST;
225 }
226
227 // Note: This is not an ideal test, but we cannot guarantee we can call Cancel() before the image has finished loading.
228 int UtcDaliAsyncImageLoaderCancel(void)
229 {
230   ToolkitTestApplication application;
231
232   AsyncImageLoader loader = AsyncImageLoader::New();
233   ImageLoadedSignalVerifier loadedSignalVerifier;
234
235   loader.ImageLoadedSignal().Connect( &loadedSignalVerifier, &ImageLoadedSignalVerifier::ImageLoaded );
236
237   uint32_t id01 = loader.Load( gImage_34_RGBA, ImageDimensions( 34, 34 ) );
238   uint32_t id02 = loader.Load( gImage_50_RGBA, ImageDimensions( 25, 25 ) );
239   uint32_t id03 = loader.Load( gImage_128_RGB, ImageDimensions( 100, 100 ), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, true );
240
241   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
242
243   application.SendNotification();
244   application.Render();
245
246   DALI_TEST_CHECK( loadedSignalVerifier.LoadedImageCount() == 3 );
247
248   DALI_TEST_CHECK( !loader.Cancel( id03 ) ); // Cannot cancel a task that is already implemeted
249
250   DALI_TEST_CHECK( loadedSignalVerifier.Verify( id01, 34, 34 ) );   // first image is loaded
251   DALI_TEST_CHECK( loadedSignalVerifier.Verify( id02, 25, 25 ) );   // second image is loaded
252   DALI_TEST_CHECK( loadedSignalVerifier.Verify( id03, 100, 100 ) ); // third image is loaded
253
254   END_TEST;
255 }
256
257 int UtcDaliAsyncImageLoaderCancelAll(void)
258 {
259   ToolkitTestApplication application;
260
261   AsyncImageLoader loader = AsyncImageLoader::New();
262
263   // Test that it is safe to call CancelAll even there is no loading task requested.
264   try
265   {
266     loader.CancelAll();
267   }
268   catch(Dali::DaliException& e)
269   {
270     DALI_TEST_ASSERT(e, "AsyncImageLoader::LoadAll", TEST_LOCATION);
271   }
272
273   // Test that cancelling a non-existing loading task will return false
274   uint32_t id = 1;
275   DALI_TEST_CHECK( !(loader.Cancel( id )) );
276
277   END_TEST;
278 }