Fixed bug in TextureSet::GetTextureCount()
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-TextureSet.cpp
1 /*
2  * Copyright (c) 2016 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 // EXTERNAL INCLUDES
19 #include <dali/public-api/dali-core.h>
20 #include <dali/devel-api/images/texture-set-image.h>
21
22 // INTERNAL INCLUDES
23 #include <dali-test-suite-utils.h>
24 #include <mesh-builder.h>
25
26 using namespace Dali;
27 void texture_set_test_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void texture_set_test_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37 int UtcDaliTextureSetNew01(void)
38 {
39   TestApplication application;
40
41   TextureSet textureSet = TextureSet::New();
42
43   DALI_TEST_CHECK( textureSet );
44   END_TEST;
45 }
46
47 int UtcDaliTextureSetNew02(void)
48 {
49   TestApplication application;
50   TextureSet textureSet;
51   DALI_TEST_CHECK( !textureSet );
52   END_TEST;
53 }
54
55 int UtcDaliTextureSetCopyConstructor(void)
56 {
57   TestApplication application;
58
59   Image image = BufferImage::New(32, 32, Pixel::RGBA8888);
60   TextureSet textureSet = TextureSet::New();
61   TextureSetImage( textureSet, 0u, image );
62
63   TextureSet textureSetCopy(textureSet);
64
65   DALI_TEST_CHECK( textureSetCopy );
66
67   END_TEST;
68 }
69
70 int UtcDaliTextureSetAssignmentOperator(void)
71 {
72   TestApplication application;
73   TextureSet textureSet = TextureSet::New();
74
75   TextureSet textureSet2;
76   DALI_TEST_CHECK( !textureSet2 );
77
78   textureSet2 = textureSet;
79   DALI_TEST_CHECK( textureSet2 );
80
81   END_TEST;
82 }
83
84 int UtcDaliTextureSetDownCast01(void)
85 {
86   TestApplication application;
87   TextureSet textureSet = TextureSet::New();
88
89   BaseHandle handle(textureSet);
90   TextureSet textureSet2 = TextureSet::DownCast(handle);
91   DALI_TEST_CHECK( textureSet2 );
92
93   END_TEST;
94 }
95
96 int UtcDaliTextureSetDownCast02(void)
97 {
98   TestApplication application;
99
100   Handle handle = Handle::New(); // Create a custom object
101   TextureSet textureSet = TextureSet::DownCast(handle);
102   DALI_TEST_CHECK( !textureSet );
103   END_TEST;
104 }
105
106
107 int UtcDaliTextureSetTexture01(void)
108 {
109   TestApplication application;
110
111   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64 );
112
113   Shader shader = CreateShader();
114   TextureSet textureSet = CreateTextureSet();
115   textureSet.SetTexture( 0u, texture );
116
117   Geometry geometry = CreateQuadGeometry();
118   Renderer renderer = Renderer::New( geometry, shader );
119   renderer.SetTextures( textureSet );
120
121   Actor actor = Actor::New();
122   actor.AddRenderer(renderer);
123   actor.SetParentOrigin( ParentOrigin::CENTER );
124   actor.SetSize(400, 400);
125
126   Stage::GetCurrent().Add( actor );
127
128   TestGlAbstraction& gl = application.GetGlAbstraction();
129
130   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
131   texParameterTrace.Reset();
132   texParameterTrace.Enable( true );
133   application.SendNotification();
134   application.Render();
135
136   int textureUnit=-1;
137   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture", textureUnit ) );
138   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
139
140   texParameterTrace.Enable( false );
141
142   // Verify gl state
143   // There are four calls to TexParameteri when the texture is first created
144   // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
145   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 4, TEST_LOCATION);
146
147   END_TEST;
148 }
149
150 int UtcDaliTextureSetTexture02(void)
151 {
152   TestApplication application;
153
154   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64 );
155
156   Shader shader = CreateShader();
157   TextureSet textureSet = CreateTextureSet();
158
159   Sampler sampler = Sampler::New();
160   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
161   textureSet.SetTexture( 0u, texture );
162   textureSet.SetSampler( 0u, sampler );
163
164   Geometry geometry = CreateQuadGeometry();
165   Renderer renderer = Renderer::New( geometry, shader );
166   renderer.SetTextures( textureSet );
167
168   Actor actor = Actor::New();
169   actor.AddRenderer(renderer);
170   actor.SetParentOrigin( ParentOrigin::CENTER );
171   actor.SetSize(400, 400);
172
173   Stage::GetCurrent().Add( actor );
174
175   TestGlAbstraction& gl = application.GetGlAbstraction();
176
177   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
178   texParameterTrace.Reset();
179   texParameterTrace.Enable( true );
180   application.SendNotification();
181   application.Render();
182
183   int textureUnit=-1;
184   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture", textureUnit ) );
185   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
186
187   texParameterTrace.Enable( false );
188
189   // Verify gl state
190   // There are four calls to TexParameteri when the texture is first created
191   // Texture minification and magnification filters are now different than default so
192   //there should have been two extra TexParameteri calls to set the new filter mode
193   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 6, TEST_LOCATION);
194
195   END_TEST;
196 }
197 int UtcDaliTextureSetSetSampler(void)
198 {
199   TestApplication application;
200
201   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
202
203   Shader shader = CreateShader();
204   TextureSet textureSet = CreateTextureSet( image );
205
206   Geometry geometry = CreateQuadGeometry();
207   Renderer renderer = Renderer::New( geometry, shader );
208   renderer.SetTextures( textureSet );
209
210   Actor actor = Actor::New();
211   actor.AddRenderer(renderer);
212   actor.SetParentOrigin( ParentOrigin::CENTER );
213   actor.SetSize(400, 400);
214
215   Stage::GetCurrent().Add( actor );
216
217   TestGlAbstraction& gl = application.GetGlAbstraction();
218
219   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
220   texParameterTrace.Reset();
221   texParameterTrace.Enable( true );
222   application.SendNotification();
223   application.Render();
224
225   int textureUnit=-1;
226   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture", textureUnit ) );
227   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
228
229   texParameterTrace.Enable( false );
230
231   // Verify gl state
232   // There are 4 calls to TexParameteri when the texture is first created
233   // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
234   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3, TEST_LOCATION);
235
236   texParameterTrace.Reset();
237   texParameterTrace.Enable( true );
238
239   Sampler sampler = Sampler::New();
240   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
241   textureSet.SetSampler( 0u, sampler );
242
243
244   application.SendNotification();
245   application.Render();
246
247   texParameterTrace.Enable( false );
248
249   // Verify gl state
250   //There should have been two calls to TexParameteri to set the new filtering mode
251   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 2, TEST_LOCATION);
252
253
254   END_TEST;
255 }
256
257 int UtcDaliTextureSetGetTexture(void)
258 {
259   TestApplication application;
260
261   TextureSet textureSet = CreateTextureSet();
262   DALI_TEST_EQUALS( textureSet.GetTexture(0), Texture(), TEST_LOCATION );
263   DALI_TEST_EQUALS( textureSet.GetTexture(1), Texture(), TEST_LOCATION );
264   DALI_TEST_EQUALS( textureSet.GetTexture(2), Texture(), TEST_LOCATION );
265
266   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64 );
267   textureSet.SetTexture( 0u, texture );
268
269   DALI_TEST_EQUALS( textureSet.GetTexture(0), texture, TEST_LOCATION );
270   DALI_TEST_EQUALS( textureSet.GetTexture(1), Texture(), TEST_LOCATION );
271   DALI_TEST_EQUALS( textureSet.GetTexture(2), Texture(), TEST_LOCATION );
272
273   textureSet.SetTexture( 2u, texture );
274   DALI_TEST_EQUALS( textureSet.GetTexture(0), texture, TEST_LOCATION );
275   DALI_TEST_EQUALS( textureSet.GetTexture(1), Texture(), TEST_LOCATION );
276   DALI_TEST_EQUALS( textureSet.GetTexture(2), texture, TEST_LOCATION );
277
278   textureSet.SetTexture( 2u, Texture() );
279   DALI_TEST_EQUALS( textureSet.GetTexture(0), texture, TEST_LOCATION );
280   DALI_TEST_EQUALS( textureSet.GetTexture(1), Texture(), TEST_LOCATION );
281   DALI_TEST_EQUALS( textureSet.GetTexture(2), Texture(), TEST_LOCATION );
282
283   END_TEST;
284 }
285
286 int UtcDaliTextureSetGetSampler(void)
287 {
288   TestApplication application;
289
290   TextureSet textureSet = CreateTextureSet();
291   DALI_TEST_EQUALS( textureSet.GetSampler(0), Sampler(), TEST_LOCATION );
292   DALI_TEST_EQUALS( textureSet.GetSampler(1), Sampler(), TEST_LOCATION );
293   DALI_TEST_EQUALS( textureSet.GetSampler(2), Sampler(), TEST_LOCATION );
294
295   Sampler sampler = Sampler::New();
296   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
297   textureSet.SetSampler( 0u, sampler );
298
299   DALI_TEST_EQUALS( textureSet.GetSampler(0), sampler, TEST_LOCATION );
300   DALI_TEST_EQUALS( textureSet.GetSampler(1), Sampler(), TEST_LOCATION );
301   DALI_TEST_EQUALS( textureSet.GetSampler(2), Sampler(), TEST_LOCATION );
302
303   textureSet.SetSampler( 2u, sampler );
304   DALI_TEST_EQUALS( textureSet.GetSampler(0), sampler, TEST_LOCATION );
305   DALI_TEST_EQUALS( textureSet.GetSampler(1), Sampler(), TEST_LOCATION );
306   DALI_TEST_EQUALS( textureSet.GetSampler(2), sampler, TEST_LOCATION );
307
308   textureSet.SetSampler( 2u, Sampler() );
309   DALI_TEST_EQUALS( textureSet.GetSampler(0), sampler, TEST_LOCATION );
310   DALI_TEST_EQUALS( textureSet.GetSampler(1), Sampler(), TEST_LOCATION );
311   DALI_TEST_EQUALS( textureSet.GetSampler(2), Sampler(), TEST_LOCATION );
312
313   END_TEST;
314 }
315
316 int UtcDaliTextureGetTextureCount0(void)
317 {
318   TestApplication application;
319
320   TextureSet textureSet = CreateTextureSet();
321   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 0u, TEST_LOCATION );
322
323   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
324   TextureSetImage( textureSet, 0u, image );
325   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 1u, TEST_LOCATION );
326
327   TextureSetImage( textureSet, 1u, image );
328   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 2u, TEST_LOCATION );
329
330   textureSet.SetSampler( 2u, Sampler::New() );
331   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 3u, TEST_LOCATION );
332
333   END_TEST;
334 }
335
336 int UtcDaliTextureGetTextureCount1(void)
337 {
338   TestApplication application;
339
340   TextureSet textureSet = CreateTextureSet();
341   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 0u, TEST_LOCATION );
342
343   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64 );
344   textureSet.SetTexture( 0u, texture );
345   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 1u, TEST_LOCATION );
346
347   textureSet.SetTexture( 1u, texture );
348   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 2u, TEST_LOCATION );
349
350   textureSet.SetSampler( 2u, Sampler::New() );
351   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 3u, TEST_LOCATION );
352
353   END_TEST;
354 }