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