Merge "Check loadPixelBuffer in the Caching Texture" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-TextureManager.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
18 #include <iostream>
19
20 #include <stdlib.h>
21
22 #include <dali-toolkit-test-suite-utils.h>
23 #include <toolkit-timer.h>
24 #include <toolkit-event-thread-callback.h>
25 #include <dali-toolkit/internal/visuals/texture-manager-impl.h>
26 #include <dali-toolkit/internal/visuals/texture-upload-observer.h>
27 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
28
29 using namespace Dali::Toolkit::Internal;
30
31
32 void utc_dali_toolkit_texture_manager_startup(void)
33 {
34   setenv( "LOG_TEXTURE_MANAGER", "3", 1 );
35   test_return_value = TET_UNDEF;
36 }
37
38 void utc_dali_toolkit_texture_manager_cleanup(void)
39 {
40   test_return_value = TET_PASS;
41 }
42
43 namespace
44 {
45
46 const char* TEST_IMAGE_FILE_NAME =  TEST_RESOURCE_DIR "/gallery-small-1.jpg";
47
48 }
49
50 class TestObserver : public Dali::Toolkit::TextureUploadObserver
51 {
52 public:
53   enum class CompleteType
54   {
55     NOT_COMPLETED = 0,
56     UPLOAD_COMPLETE,
57     LOAD_COMPLETE
58   };
59
60 public:
61   TestObserver()
62   : mCompleteType( CompleteType::NOT_COMPLETED ),
63     mLoaded(false),
64     mObserverCalled(false)
65   {
66   }
67
68   virtual void UploadComplete( bool loadSuccess, int32_t textureId, TextureSet textureSet,
69                                bool useAtlasing, const Vector4& atlasRect, bool preMultiplied ) override
70   {
71     mCompleteType = CompleteType::UPLOAD_COMPLETE;
72     mLoaded = loadSuccess;
73     mObserverCalled = true;
74   }
75
76   virtual void LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied ) override
77   {
78     mCompleteType = CompleteType::LOAD_COMPLETE;
79     mLoaded = loadSuccess;
80     mObserverCalled = true;
81   }
82
83   CompleteType mCompleteType;
84   bool mLoaded;
85   bool mObserverCalled;
86 };
87
88
89 int UtcTextureManagerRequestLoad(void)
90 {
91   ToolkitTestApplication application;
92
93   TextureManager textureManager; // Create new texture manager
94
95   TestObserver observer;
96   std::string filename("image.png");
97   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
98   TextureManager::TextureId textureId = textureManager.RequestLoad(
99     filename,
100     ImageDimensions(),
101     FittingMode::SCALE_TO_FILL,
102     SamplingMode::BOX_THEN_LINEAR,
103     TextureManager::NO_ATLAS,
104     &observer,
105     true,
106     TextureManager::ReloadPolicy::CACHED,
107     preMultiply);
108
109   VisualUrl url = textureManager.GetVisualUrl( textureId );
110
111   DALI_TEST_EQUALS( url.GetUrl().compare( filename ), 0, TEST_LOCATION );
112
113   END_TEST;
114 }
115
116 int UtcTextureManagerGenerateHash(void)
117 {
118   ToolkitTestApplication application;
119
120   TextureManager textureManager; // Create new texture manager
121
122   TestObserver observer;
123   std::string filename( "image.png" );
124   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
125   TextureManager::TextureId textureId = textureManager.RequestLoad(
126     filename,
127     ImageDimensions(),
128     FittingMode::SCALE_TO_FILL,
129     SamplingMode::BOX_THEN_LINEAR,
130     TextureManager::USE_ATLAS,
131     &observer,
132     true,
133     TextureManager::ReloadPolicy::CACHED,
134     preMultiply);
135
136   VisualUrl url = textureManager.GetVisualUrl( textureId );
137
138   DALI_TEST_EQUALS( url.GetUrl().compare( filename ), 0, TEST_LOCATION );
139
140   END_TEST;
141 }
142
143 int UtcTextureManagerCachingForDifferentLoadingType(void)
144 {
145   ToolkitTestApplication application;
146   tet_infoline( "UtcTextureManagerCachingForDifferentLoadingType" );
147
148   TextureManager textureManager; // Create new texture manager
149
150   TestObserver observer1;
151   std::string filename( TEST_IMAGE_FILE_NAME );
152   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
153   textureManager.RequestLoad(
154     filename,
155     ImageDimensions(),
156     FittingMode::SCALE_TO_FILL,
157     SamplingMode::BOX_THEN_LINEAR,
158     TextureManager::NO_ATLAS,
159     &observer1,
160     true,
161     TextureManager::ReloadPolicy::CACHED,
162     preMultiply);
163
164   DALI_TEST_EQUALS( observer1.mLoaded, false, TEST_LOCATION );
165   DALI_TEST_EQUALS( observer1.mObserverCalled, false, TEST_LOCATION );
166
167   application.SendNotification();
168   application.Render();
169
170   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
171
172   application.SendNotification();
173   application.Render();
174
175   DALI_TEST_EQUALS( observer1.mLoaded, true, TEST_LOCATION );
176   DALI_TEST_EQUALS( observer1.mObserverCalled, true, TEST_LOCATION );
177   DALI_TEST_EQUALS( observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION );
178
179   TestObserver observer2;
180   Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
181     filename,
182     ImageDimensions(),
183     FittingMode::SCALE_TO_FILL,
184     SamplingMode::BOX_THEN_LINEAR,
185     false,
186     &observer2,
187     true,
188     preMultiply);
189
190   DALI_TEST_EQUALS( observer2.mLoaded, false, TEST_LOCATION );
191   DALI_TEST_EQUALS( observer2.mObserverCalled, false, TEST_LOCATION );
192
193   application.SendNotification();
194   application.Render();
195
196   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
197
198   application.SendNotification();
199   application.Render();
200
201   DALI_TEST_EQUALS( observer2.mLoaded, true, TEST_LOCATION );
202   DALI_TEST_EQUALS( observer2.mObserverCalled, true, TEST_LOCATION );
203   DALI_TEST_EQUALS( observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION );
204
205   END_TEST;
206 }