[Tizen] Add Integration API to Create public event type
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-TextureSet.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 // EXTERNAL INCLUDES
19 #include <dali/public-api/dali-core.h>
20
21 // INTERNAL INCLUDES
22 #include <dali-test-suite-utils.h>
23 #include <mesh-builder.h>
24
25 using namespace Dali;
26
27 namespace
28 {
29 enum SetSampler
30 {
31   SET_SAMPLER,
32   DONT_SET_SAMPLER
33 };
34
35 Actor CreateActor(SetSampler setSamplerOption)
36 {
37   Texture texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
38
39   Shader     shader     = CreateShader();
40   TextureSet textureSet = CreateTextureSet();
41
42   Sampler sampler = Sampler::New();
43   sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
44   textureSet.SetTexture(0u, texture);
45   if(setSamplerOption == SET_SAMPLER)
46   {
47     textureSet.SetSampler(0u, sampler);
48   }
49
50   Geometry geometry = CreateQuadGeometry();
51   Renderer renderer = Renderer::New(geometry, shader);
52   renderer.SetTextures(textureSet);
53
54   Actor actor = Actor::New();
55   actor.AddRenderer(renderer);
56   actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
57   actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
58
59   return actor;
60 }
61
62 } // namespace
63
64 void texture_set_test_startup(void)
65 {
66   test_return_value = TET_UNDEF;
67 }
68
69 void texture_set_test_cleanup(void)
70 {
71   test_return_value = TET_PASS;
72 }
73
74 int UtcDaliTextureSetNew01(void)
75 {
76   TestApplication application;
77
78   TextureSet textureSet = TextureSet::New();
79
80   DALI_TEST_CHECK(textureSet);
81   END_TEST;
82 }
83
84 int UtcDaliTextureSetNew02(void)
85 {
86   TestApplication application;
87   TextureSet      textureSet;
88   DALI_TEST_CHECK(!textureSet);
89   END_TEST;
90 }
91
92 int UtcDaliTextureSetCopyConstructor(void)
93 {
94   TestApplication application;
95
96   Texture    image      = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 32, 32);
97   TextureSet textureSet = TextureSet::New();
98   textureSet.SetTexture(0u, image);
99
100   TextureSet textureSetCopy(textureSet);
101
102   DALI_TEST_CHECK(textureSetCopy);
103
104   END_TEST;
105 }
106
107 int UtcDaliTextureSetAssignmentOperator(void)
108 {
109   TestApplication application;
110   TextureSet      textureSet = TextureSet::New();
111
112   TextureSet textureSet2;
113   DALI_TEST_CHECK(!textureSet2);
114
115   textureSet2 = textureSet;
116   DALI_TEST_CHECK(textureSet2);
117
118   END_TEST;
119 }
120
121 int UtcDaliTextureSetMoveConstructor(void)
122 {
123   TestApplication application;
124
125   TextureSet textureSet = TextureSet::New();
126   DALI_TEST_CHECK(textureSet);
127   DALI_TEST_EQUALS(1, textureSet.GetBaseObject().ReferenceCount(), TEST_LOCATION);
128
129   Texture texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 32, 32);
130   textureSet.SetTexture(0u, texture);
131   DALI_TEST_EQUALS(textureSet.GetTexture(0u), texture, TEST_LOCATION);
132
133   TextureSet move = std::move(textureSet);
134   DALI_TEST_CHECK(move);
135   DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
136   DALI_TEST_EQUALS(move.GetTexture(0u), texture, TEST_LOCATION);
137   DALI_TEST_CHECK(!textureSet);
138
139   END_TEST;
140 }
141
142 int UtcDaliTextureSetMoveAssignment(void)
143 {
144   TestApplication application;
145
146   TextureSet textureSet = TextureSet::New();
147   DALI_TEST_CHECK(textureSet);
148   DALI_TEST_EQUALS(1, textureSet.GetBaseObject().ReferenceCount(), TEST_LOCATION);
149
150   Texture texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 32, 32);
151   textureSet.SetTexture(0u, texture);
152   DALI_TEST_EQUALS(textureSet.GetTexture(0u), texture, TEST_LOCATION);
153
154   TextureSet move;
155   move = std::move(textureSet);
156   DALI_TEST_CHECK(move);
157   DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
158   DALI_TEST_EQUALS(move.GetTexture(0u), texture, TEST_LOCATION);
159   DALI_TEST_CHECK(!textureSet);
160
161   END_TEST;
162 }
163
164 int UtcDaliTextureSetDownCast01(void)
165 {
166   TestApplication application;
167   TextureSet      textureSet = TextureSet::New();
168
169   BaseHandle handle(textureSet);
170   TextureSet textureSet2 = TextureSet::DownCast(handle);
171   DALI_TEST_CHECK(textureSet2);
172
173   END_TEST;
174 }
175
176 int UtcDaliTextureSetDownCast02(void)
177 {
178   TestApplication application;
179
180   Handle     handle     = Handle::New(); // Create a custom object
181   TextureSet textureSet = TextureSet::DownCast(handle);
182   DALI_TEST_CHECK(!textureSet);
183   END_TEST;
184 }
185
186 int UtcDaliTextureSetTexture01(void)
187 {
188   TestApplication application;
189
190   Actor actor = CreateActor(DONT_SET_SAMPLER);
191
192   application.GetScene().Add(actor);
193
194   TestGlAbstraction& gl = application.GetGlAbstraction();
195
196   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
197   texParameterTrace.Reset();
198   texParameterTrace.Enable(true);
199   application.SendNotification();
200   application.Render();
201
202   int textureUnit = -1;
203   DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
204   DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
205
206   texParameterTrace.Enable(false);
207
208   // Verify gl state
209   // There are four calls to TexParameteri when the texture is first created
210   // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
211   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
212
213   END_TEST;
214 }
215
216 int UtcDaliTextureSetTexture02(void)
217 {
218   TestApplication application;
219
220   Actor actor = CreateActor(SET_SAMPLER);
221
222   application.GetScene().Add(actor);
223
224   TestGlAbstraction& gl = application.GetGlAbstraction();
225
226   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
227   texParameterTrace.Reset();
228   texParameterTrace.Enable(true);
229   application.SendNotification();
230   application.Render();
231
232   int textureUnit = -1;
233   DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
234   DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
235
236   texParameterTrace.Enable(false);
237
238   // Verify gl state
239   // There are four calls to TexParameteri when the texture is first created
240   // Texture minification and magnification filters are now different than default so
241   //there should have been two extra TexParameteri calls to set the new filter mode
242   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 6, TEST_LOCATION);
243
244   END_TEST;
245 }
246
247 int UtcDaliTextureSetMultiple(void)
248 {
249   TestApplication application;
250
251   Actor actor1 = CreateActor(SET_SAMPLER);
252   Actor actor2 = CreateActor(SET_SAMPLER);
253
254   application.GetScene().Add(actor1);
255   application.GetScene().Add(actor2);
256
257   TestGlAbstraction& gl = application.GetGlAbstraction();
258
259   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
260   texParameterTrace.Reset();
261   texParameterTrace.Enable(true);
262   application.SendNotification();
263   application.Render();
264
265   int textureUnit = -1;
266   DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
267   DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
268
269   texParameterTrace.Enable(false);
270
271   // Verify gl state
272   // For each actor there are four calls to TexParameteri when the texture is first created
273   // Texture minification and magnification filters are now different than default so
274   //there should have been two extra TexParameteri calls to set the new filter mode
275   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 2 * 6, TEST_LOCATION);
276
277   END_TEST;
278 }
279
280 int UtcDaliTextureSetSetSampler(void)
281 {
282   TestApplication application;
283
284   Texture image = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
285
286   Shader     shader     = CreateShader();
287   TextureSet textureSet = CreateTextureSet(image);
288
289   Geometry geometry = CreateQuadGeometry();
290   Renderer renderer = Renderer::New(geometry, shader);
291   renderer.SetTextures(textureSet);
292
293   Actor actor = Actor::New();
294   actor.AddRenderer(renderer);
295   actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
296   actor.SetProperty(Actor::Property::SIZE, Vector2(400.0f, 400.0f));
297
298   application.GetScene().Add(actor);
299
300   TestGlAbstraction& gl = application.GetGlAbstraction();
301
302   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
303   texParameterTrace.Reset();
304   texParameterTrace.Enable(true);
305   application.SendNotification();
306   application.Render();
307
308   int textureUnit = -1;
309   DALI_TEST_CHECK(gl.GetUniformValue<int>("sTexture", textureUnit));
310   DALI_TEST_EQUALS(textureUnit, 0, TEST_LOCATION);
311
312   texParameterTrace.Enable(false);
313
314   // Verify gl state
315   // There are 4 calls to TexParameteri when the texture is first created
316   // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
317   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 4, TEST_LOCATION);
318
319   texParameterTrace.Reset();
320   texParameterTrace.Enable(true);
321
322   Sampler sampler = Sampler::New();
323   sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
324   textureSet.SetSampler(0u, sampler);
325
326   application.SendNotification();
327   application.Render();
328
329   texParameterTrace.Enable(false);
330
331   // Verify gl state
332   //There should have been two calls to TexParameteri to set the new filtering mode
333   DALI_TEST_EQUALS(texParameterTrace.CountMethod("TexParameteri"), 2, TEST_LOCATION);
334
335   END_TEST;
336 }
337
338 int UtcDaliTextureSetGetTexture(void)
339 {
340   TestApplication application;
341
342   TextureSet textureSet = CreateTextureSet();
343   DALI_TEST_EQUALS(textureSet.GetTexture(0), Texture(), TEST_LOCATION);
344   DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
345   DALI_TEST_EQUALS(textureSet.GetTexture(2), Texture(), TEST_LOCATION);
346
347   Texture texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
348   textureSet.SetTexture(0u, texture);
349
350   DALI_TEST_EQUALS(textureSet.GetTexture(0), texture, TEST_LOCATION);
351   DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
352   DALI_TEST_EQUALS(textureSet.GetTexture(2), Texture(), TEST_LOCATION);
353
354   textureSet.SetTexture(2u, texture);
355   DALI_TEST_EQUALS(textureSet.GetTexture(0), texture, TEST_LOCATION);
356   DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
357   DALI_TEST_EQUALS(textureSet.GetTexture(2), texture, TEST_LOCATION);
358
359   textureSet.SetTexture(2u, Texture());
360   DALI_TEST_EQUALS(textureSet.GetTexture(0), texture, TEST_LOCATION);
361   DALI_TEST_EQUALS(textureSet.GetTexture(1), Texture(), TEST_LOCATION);
362   DALI_TEST_EQUALS(textureSet.GetTexture(2), Texture(), TEST_LOCATION);
363
364   END_TEST;
365 }
366
367 int UtcDaliTextureSetGetSampler(void)
368 {
369   TestApplication application;
370
371   TextureSet textureSet = CreateTextureSet();
372   DALI_TEST_EQUALS(textureSet.GetSampler(0), Sampler(), TEST_LOCATION);
373   DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
374   DALI_TEST_EQUALS(textureSet.GetSampler(2), Sampler(), TEST_LOCATION);
375
376   Sampler sampler = Sampler::New();
377   sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
378   textureSet.SetSampler(0u, sampler);
379
380   DALI_TEST_EQUALS(textureSet.GetSampler(0), sampler, TEST_LOCATION);
381   DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
382   DALI_TEST_EQUALS(textureSet.GetSampler(2), Sampler(), TEST_LOCATION);
383
384   textureSet.SetSampler(2u, sampler);
385   DALI_TEST_EQUALS(textureSet.GetSampler(0), sampler, TEST_LOCATION);
386   DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
387   DALI_TEST_EQUALS(textureSet.GetSampler(2), sampler, TEST_LOCATION);
388
389   textureSet.SetSampler(2u, Sampler());
390   DALI_TEST_EQUALS(textureSet.GetSampler(0), sampler, TEST_LOCATION);
391   DALI_TEST_EQUALS(textureSet.GetSampler(1), Sampler(), TEST_LOCATION);
392   DALI_TEST_EQUALS(textureSet.GetSampler(2), Sampler(), TEST_LOCATION);
393
394   END_TEST;
395 }
396
397 int UtcDaliTextureSetGetTextureCount0(void)
398 {
399   TestApplication application;
400
401   TextureSet textureSet = CreateTextureSet();
402   DALI_TEST_EQUALS(textureSet.GetTextureCount(), 0u, TEST_LOCATION);
403
404   Texture image = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
405   textureSet.SetTexture(0u, image);
406   DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
407
408   textureSet.SetTexture(1u, image);
409   DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
410
411   Sampler sampler = Sampler::New();
412   sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
413   textureSet.SetSampler(2u, sampler);
414   DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
415
416   textureSet.SetTexture(2u, image);
417   DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
418   DALI_TEST_EQUALS(textureSet.GetSampler(2u), sampler, TEST_LOCATION);
419
420   END_TEST;
421 }
422
423 int UtcDaliTextureSetGetTextureCount1(void)
424 {
425   TestApplication application;
426
427   TextureSet textureSet = CreateTextureSet();
428   DALI_TEST_EQUALS(textureSet.GetTextureCount(), 0u, TEST_LOCATION);
429
430   Texture texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
431   textureSet.SetTexture(0u, texture);
432   DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
433
434   textureSet.SetTexture(1u, texture);
435   DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
436
437   Sampler sampler = Sampler::New();
438   sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST);
439   textureSet.SetSampler(2u, sampler);
440   DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
441
442   textureSet.SetTexture(2u, texture);
443   DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
444   DALI_TEST_EQUALS(textureSet.GetSampler(2u), sampler, TEST_LOCATION);
445
446   END_TEST;
447 }
448
449 int UtcDaliTextureSetSetSamplerNegative(void)
450 {
451   TestApplication  application;
452   Dali::TextureSet instance;
453   try
454   {
455     unsigned long arg1(0u);
456     Dali::Sampler arg2;
457     instance.SetSampler(arg1, arg2);
458     DALI_TEST_CHECK(false); // Should not get here
459   }
460   catch(...)
461   {
462     DALI_TEST_CHECK(true); // We expect an assert
463   }
464   END_TEST;
465 }
466
467 int UtcDaliTextureSetSetTextureNegative(void)
468 {
469   TestApplication  application;
470   Dali::TextureSet instance;
471   try
472   {
473     unsigned long arg1(0u);
474     Dali::Texture arg2;
475     instance.SetTexture(arg1, arg2);
476     DALI_TEST_CHECK(false); // Should not get here
477   }
478   catch(...)
479   {
480     DALI_TEST_CHECK(true); // We expect an assert
481   }
482   END_TEST;
483 }
484
485 int UtcDaliTextureSetGetSamplerNegative(void)
486 {
487   TestApplication  application;
488   Dali::TextureSet instance;
489   try
490   {
491     unsigned long arg1(0u);
492     instance.GetSampler(arg1);
493     DALI_TEST_CHECK(false); // Should not get here
494   }
495   catch(...)
496   {
497     DALI_TEST_CHECK(true); // We expect an assert
498   }
499   END_TEST;
500 }
501
502 int UtcDaliTextureSetGetTextureNegative(void)
503 {
504   TestApplication  application;
505   Dali::TextureSet instance;
506   try
507   {
508     unsigned long arg1(0u);
509     instance.GetTexture(arg1);
510     DALI_TEST_CHECK(false); // Should not get here
511   }
512   catch(...)
513   {
514     DALI_TEST_CHECK(true); // We expect an assert
515   }
516   END_TEST;
517 }
518
519 int UtcDaliTextureSetGetTextureCountNegative(void)
520 {
521   TestApplication  application;
522   Dali::TextureSet instance;
523   try
524   {
525     instance.GetTextureCount();
526     DALI_TEST_CHECK(false); // Should not get here
527   }
528   catch(...)
529   {
530     DALI_TEST_CHECK(true); // We expect an assert
531   }
532   END_TEST;
533 }