[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-scene3d / utc-Dali-Material.cpp
1 /*
2  * Copyright (c) 2023 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 <dali-toolkit-test-suite-utils.h>
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <stdlib.h>
21 #include <iostream>
22
23 #include <dali-scene3d/public-api/model-components/material.h>
24
25 using namespace Dali;
26 using namespace Dali::Toolkit;
27
28 void model_components_material_startup(void)
29 {
30   test_return_value = TET_UNDEF;
31 }
32
33 void model_components_material_cleanup(void)
34 {
35   test_return_value = TET_PASS;
36 }
37
38 namespace
39 {
40 } // namespace
41
42 // Positive test case for a method
43 int UtcDaliMaterialNew(void)
44 {
45   ToolkitTestApplication application;
46   tet_infoline(" UtcDaliMaterialNew");
47
48   Scene3D::Material material = Scene3D::Material::New();
49   DALI_TEST_CHECK(material);
50   END_TEST;
51 }
52
53 // Positive test case for a method
54 int UtcDaliMaterialDownCast(void)
55 {
56   ToolkitTestApplication application;
57   tet_infoline(" UtcDaliMaterialDownCast");
58
59   Scene3D::Material material = Scene3D::Material::New();
60   BaseHandle        handle(material);
61
62   Scene3D::Material material2 = Scene3D::Material::DownCast(handle);
63   DALI_TEST_CHECK(material);
64   DALI_TEST_CHECK(material2);
65   DALI_TEST_CHECK(material2 == material);
66   END_TEST;
67 }
68
69 int UtcDaliMaterialTypeRegistry(void)
70 {
71   ToolkitTestApplication application;
72
73   TypeRegistry typeRegistry = TypeRegistry::Get();
74   DALI_TEST_CHECK(typeRegistry);
75
76   TypeInfo typeInfo = typeRegistry.GetTypeInfo("Material");
77   DALI_TEST_CHECK(typeInfo);
78
79   BaseHandle handle = typeInfo.CreateInstance();
80   DALI_TEST_CHECK(handle);
81
82   Scene3D::Material mateiral = Scene3D::Material::DownCast(handle);
83   DALI_TEST_CHECK(mateiral);
84
85   END_TEST;
86 }
87
88 int UtcDaliMaterialCopyAndAssignment(void)
89 {
90   ToolkitTestApplication application;
91
92   Scene3D::Material mateiral = Scene3D::Material::New();
93   DALI_TEST_CHECK(mateiral);
94
95   Scene3D::Material copy(mateiral);
96   DALI_TEST_CHECK(mateiral == copy);
97
98   Scene3D::Material assign;
99   DALI_TEST_CHECK(!assign);
100
101   assign = copy;
102   DALI_TEST_CHECK(assign == mateiral);
103
104   END_TEST;
105 }
106
107 int UtcDaliMaterialMoveConstructor(void)
108 {
109   ToolkitTestApplication application;
110
111   Scene3D::Material mateiral = Scene3D::Material::New();
112   DALI_TEST_EQUALS(1, mateiral.GetBaseObject().ReferenceCount(), TEST_LOCATION);
113
114   Scene3D::Material moved = std::move(mateiral);
115   DALI_TEST_CHECK(moved);
116   DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
117   DALI_TEST_CHECK(!mateiral);
118
119   END_TEST;
120 }
121
122 int UtcDaliMaterialMoveAssignment(void)
123 {
124   ToolkitTestApplication application;
125
126   Scene3D::Material mateiral = Scene3D::Material::New();
127   DALI_TEST_EQUALS(1, mateiral.GetBaseObject().ReferenceCount(), TEST_LOCATION);
128
129   Scene3D::Material moved;
130   moved = std::move(mateiral);
131   DALI_TEST_CHECK(moved);
132   DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
133   DALI_TEST_CHECK(!mateiral);
134
135   END_TEST;
136 }
137
138 // Method test
139 int UtcDaliMaterialSetGetProperty(void)
140 {
141   tet_infoline("UtcDaliMaterialSetGetProperty.");
142
143   ToolkitTestApplication application;
144
145   Scene3D::Material material = Scene3D::Material::New();
146
147   std::string name = "name";
148   material.SetProperty(Scene3D::Material::Property::NAME, name);
149   DALI_TEST_EQUALS(name, material.GetProperty<std::string>(Scene3D::Material::Property::NAME), TEST_LOCATION);
150
151   std::string baseColorUrl = "baseColorUrl";
152   material.SetProperty(Scene3D::Material::Property::BASE_COLOR_URL, baseColorUrl);
153   DALI_TEST_EQUALS(baseColorUrl, material.GetProperty<std::string>(Scene3D::Material::Property::BASE_COLOR_URL), TEST_LOCATION);
154
155   Vector4 baseColorFactor(0.1f, 0.2f, 0.3f, 0.4f);
156   material.SetProperty(Scene3D::Material::Property::BASE_COLOR_FACTOR, baseColorFactor);
157   DALI_TEST_EQUALS(baseColorFactor, material.GetProperty<Vector4>(Scene3D::Material::Property::BASE_COLOR_FACTOR), TEST_LOCATION);
158
159   std::string metallicRoughnessUrl = "metallicRoughnessUrl";
160   material.SetProperty(Scene3D::Material::Property::METALLIC_ROUGHNESS_URL, metallicRoughnessUrl);
161   DALI_TEST_EQUALS(metallicRoughnessUrl, material.GetProperty<std::string>(Scene3D::Material::Property::METALLIC_ROUGHNESS_URL), TEST_LOCATION);
162
163   float metallicFactor = 0.5f;
164   material.SetProperty(Scene3D::Material::Property::METALLIC_FACTOR, metallicFactor);
165   DALI_TEST_EQUALS(metallicFactor, material.GetProperty<float>(Scene3D::Material::Property::METALLIC_FACTOR), TEST_LOCATION);
166
167   float roughnessFactor = 0.6f;
168   material.SetProperty(Scene3D::Material::Property::ROUGHNESS_FACTOR, roughnessFactor);
169   DALI_TEST_EQUALS(roughnessFactor, material.GetProperty<float>(Scene3D::Material::Property::ROUGHNESS_FACTOR), TEST_LOCATION);
170
171   std::string normalUrl = "normalUrl";
172   material.SetProperty(Scene3D::Material::Property::NORMAL_URL, normalUrl);
173   DALI_TEST_EQUALS(normalUrl, material.GetProperty<std::string>(Scene3D::Material::Property::NORMAL_URL), TEST_LOCATION);
174
175   float normalScale = 0.7f;
176   material.SetProperty(Scene3D::Material::Property::NORMAL_SCALE, normalScale);
177   DALI_TEST_EQUALS(normalScale, material.GetProperty<float>(Scene3D::Material::Property::NORMAL_SCALE), TEST_LOCATION);
178
179   std::string occlusionUrl = "occlusionUrl";
180   material.SetProperty(Scene3D::Material::Property::OCCLUSION_URL, occlusionUrl);
181   DALI_TEST_EQUALS(occlusionUrl, material.GetProperty<std::string>(Scene3D::Material::Property::OCCLUSION_URL), TEST_LOCATION);
182
183   float occlusionStrength = 0.8f;
184   material.SetProperty(Scene3D::Material::Property::OCCLUSION_STRENGTH, occlusionStrength);
185   DALI_TEST_EQUALS(occlusionStrength, material.GetProperty<float>(Scene3D::Material::Property::OCCLUSION_STRENGTH), TEST_LOCATION);
186
187   std::string emissiveUrl = "emissiveUrl";
188   material.SetProperty(Scene3D::Material::Property::EMISSIVE_URL, emissiveUrl);
189   DALI_TEST_EQUALS(emissiveUrl, material.GetProperty<std::string>(Scene3D::Material::Property::EMISSIVE_URL), TEST_LOCATION);
190
191   Vector3 emissiveFactor(0.9f, 0.1f, 0.2f);
192   material.SetProperty(Scene3D::Material::Property::EMISSIVE_FACTOR, emissiveFactor);
193   DALI_TEST_EQUALS(emissiveFactor, material.GetProperty<Vector3>(Scene3D::Material::Property::EMISSIVE_FACTOR), TEST_LOCATION);
194
195   int alphaMode = 1;
196   material.SetProperty(Scene3D::Material::Property::ALPHA_MODE, alphaMode);
197   DALI_TEST_EQUALS(alphaMode, material.GetProperty<int>(Scene3D::Material ::Property ::ALPHA_MODE), TEST_LOCATION);
198
199   float alphaCutoff = 0.9f;
200   material.SetProperty(Scene3D ::Material ::Property ::ALPHA_CUTOFF, alphaCutoff);
201   DALI_TEST_EQUALS(alphaCutoff, material.GetProperty<float>(Scene3D ::Material ::Property ::ALPHA_CUTOFF), TEST_LOCATION);
202
203   bool doubleSided = true;
204   material.SetProperty(Scene3D ::Material ::Property ::DOUBLE_SIDED, doubleSided);
205   DALI_TEST_EQUALS(doubleSided, material.GetProperty<bool>(Scene3D ::Material ::Property ::DOUBLE_SIDED), TEST_LOCATION);
206
207   float ior = 1.0f;
208   material.SetProperty(Scene3D::Material::Property::IOR, ior);
209   DALI_TEST_EQUALS(ior, material.GetProperty<float>(Scene3D::Material::Property::IOR), TEST_LOCATION);
210
211   std::string specularUrl = "specularUrl";
212   material.SetProperty(Scene3D::Material::Property::SPECULAR_URL, specularUrl);
213   DALI_TEST_EQUALS(specularUrl, material.GetProperty<std::string>(Scene3D::Material::Property::SPECULAR_URL), TEST_LOCATION);
214
215   float specularFactor = 1.0f;
216   material.SetProperty(Scene3D::Material::Property::SPECULAR_FACTOR, specularFactor);
217   DALI_TEST_EQUALS(specularFactor, material.GetProperty<float>(Scene3D::Material::Property::SPECULAR_FACTOR), TEST_LOCATION);
218
219   std::string specularColorUrl = "specularColorUrl";
220   material.SetProperty(Scene3D::Material::Property::SPECULAR_COLOR_URL, specularColorUrl);
221   DALI_TEST_EQUALS(specularColorUrl, material.GetProperty<std::string>(Scene3D::Material::Property::SPECULAR_COLOR_URL), TEST_LOCATION);
222
223   Vector3 specularColorFactor(1.0f, 1.0f, 1.0f);
224   material.SetProperty(Scene3D::Material::Property::SPECULAR_COLOR_FACTOR, specularColorFactor);
225   DALI_TEST_EQUALS(specularColorFactor, material.GetProperty<Vector3>(Scene3D::Material::Property::SPECULAR_COLOR_FACTOR), TEST_LOCATION);
226
227   int32_t depthIndex = 50;
228   material.SetProperty(Scene3D::Material::Property::DEPTH_INDEX, depthIndex);
229   DALI_TEST_EQUALS(depthIndex, material.GetProperty<int32_t>(Scene3D::Material::Property::DEPTH_INDEX), TEST_LOCATION);
230
231   END_TEST;
232 }
233
234 int UtcDaliMaterialSetGetTexture(void)
235 {
236   tet_infoline("UtcDaliMaterialSetGetTexture.");
237
238   ToolkitTestApplication application;
239
240   Scene3D::Material material = Scene3D::Material::New();
241
242   Dali::Texture baseColor = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 100, 100);
243   material.SetTexture(Scene3D::Material::TextureType::BASE_COLOR, baseColor);
244   DALI_TEST_EQUALS(baseColor, material.GetTexture(Scene3D::Material::TextureType::BASE_COLOR), TEST_LOCATION);
245
246   Dali::Texture metallicRoughness = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 100, 100);
247   material.SetTexture(Scene3D::Material::TextureType::METALLIC_ROUGHNESS, metallicRoughness);
248   DALI_TEST_EQUALS(metallicRoughness, material.GetTexture(Scene3D::Material::TextureType::METALLIC_ROUGHNESS), TEST_LOCATION);
249
250   Dali::Texture normal = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 100, 100);
251   material.SetTexture(Scene3D::Material::TextureType::NORMAL, normal);
252   DALI_TEST_EQUALS(normal, material.GetTexture(Scene3D::Material::TextureType::NORMAL), TEST_LOCATION);
253
254   Dali::Texture occlusion = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 100, 100);
255   material.SetTexture(Scene3D::Material::TextureType::OCCLUSION, occlusion);
256   DALI_TEST_EQUALS(occlusion, material.GetTexture(Scene3D::Material::TextureType::OCCLUSION), TEST_LOCATION);
257
258   Dali::Texture emissive = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 100, 100);
259   material.SetTexture(Scene3D::Material::TextureType::EMISSIVE, emissive);
260   DALI_TEST_EQUALS(emissive, material.GetTexture(Scene3D::Material::TextureType::EMISSIVE), TEST_LOCATION);
261
262   Dali::Texture specular = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 100, 100);
263   material.SetTexture(Scene3D::Material::TextureType::SPECULAR, specular);
264   DALI_TEST_EQUALS(specular, material.GetTexture(Scene3D::Material::TextureType::SPECULAR), TEST_LOCATION);
265
266   Dali::Texture specularColor = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 100, 100);
267   material.SetTexture(Scene3D::Material::TextureType::SPECULAR_COLOR, specularColor);
268   DALI_TEST_EQUALS(specularColor, material.GetTexture(Scene3D::Material::TextureType::SPECULAR_COLOR), TEST_LOCATION);
269
270   DALI_TEST_CHECK(!material.GetTexture((Scene3D::Material::TextureType)100));
271
272   END_TEST;
273 }
274
275 int UtcDaliMaterialSetGetSampler(void)
276 {
277   tet_infoline("UtcDaliMaterialSetGetSampler.");
278
279   ToolkitTestApplication application;
280
281   Scene3D::Material material = Scene3D::Material::New();
282
283   Dali::Sampler baseColor = Dali::Sampler::New();
284   material.SetSampler(Scene3D::Material::TextureType::BASE_COLOR, baseColor);
285   DALI_TEST_EQUALS(baseColor, material.GetSampler(Scene3D::Material::TextureType::BASE_COLOR), TEST_LOCATION);
286
287   Dali::Sampler metallicRoughness = Dali::Sampler::New();
288   material.SetSampler(Scene3D::Material::TextureType::METALLIC_ROUGHNESS, metallicRoughness);
289   DALI_TEST_EQUALS(metallicRoughness, material.GetSampler(Scene3D::Material::TextureType::METALLIC_ROUGHNESS), TEST_LOCATION);
290
291   Dali::Sampler normal = Dali::Sampler::New();
292   material.SetSampler(Scene3D::Material::TextureType::NORMAL, normal);
293   DALI_TEST_EQUALS(normal, material.GetSampler(Scene3D::Material::TextureType::NORMAL), TEST_LOCATION);
294
295   Dali::Sampler occlusion = Dali::Sampler::New();
296   material.SetSampler(Scene3D::Material::TextureType::OCCLUSION, occlusion);
297   DALI_TEST_EQUALS(occlusion, material.GetSampler(Scene3D::Material::TextureType::OCCLUSION), TEST_LOCATION);
298
299   Dali::Sampler emissive = Dali::Sampler::New();
300   material.SetSampler(Scene3D::Material::TextureType::EMISSIVE, emissive);
301   DALI_TEST_EQUALS(emissive, material.GetSampler(Scene3D::Material::TextureType::EMISSIVE), TEST_LOCATION);
302
303   Dali::Sampler specular = Dali::Sampler::New();
304   material.SetSampler(Scene3D::Material::TextureType::SPECULAR, specular);
305   DALI_TEST_EQUALS(specular, material.GetSampler(Scene3D::Material::TextureType::SPECULAR), TEST_LOCATION);
306
307   Dali::Sampler specularColor = Dali::Sampler::New();
308   material.SetSampler(Scene3D::Material::TextureType::SPECULAR_COLOR, specularColor);
309   DALI_TEST_EQUALS(specularColor, material.GetSampler(Scene3D::Material::TextureType::SPECULAR_COLOR), TEST_LOCATION);
310
311   DALI_TEST_CHECK(!material.GetSampler((Scene3D::Material::TextureType)100));
312
313   END_TEST;
314 }