2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <dali/public-api/dali-core.h>
22 #include <dali/devel-api/actors/mesh-actor.h>
23 #include <dali/devel-api/geometry/mesh.h>
24 #include <dali/devel-api/geometry/animatable-mesh.h>
25 #include <dali-test-suite-utils.h>
30 void utc_dali_material_startup(void)
32 test_return_value = TET_UNDEF;
35 void utc_dali_material_cleanup(void)
37 test_return_value = TET_PASS;
43 static AnimatableMesh CreateMeshData(Material material)
45 AnimatableMesh::Faces faces;
46 for(int i=0; i<10-3; i++)
52 return AnimatableMesh::New(10, faces, material);
55 }// anonymous namespace
58 // Positive test case for a method
59 int UtcDaliMaterialNew01(void)
61 TestApplication application;
62 tet_infoline("Testing constructors, New and destructors");
65 DALI_TEST_CHECK( ! material );
67 material = Material::New("material");
68 DALI_TEST_CHECK( material );
70 Material* material2 = new Material();
71 DALI_TEST_CHECK( ! *material2 );
74 Material material3 = material;
77 Material material5 = material;
82 int UtcDaliMaterialDownCast(void)
84 TestApplication application;
86 Material material = Material::New("material");
87 BaseHandle handle(material);
89 Material mat2 = Material::DownCast( handle );
90 DALI_TEST_CHECK( mat2 );
94 int UtcDaliMaterialSettersAndGetters(void)
96 TestApplication application;
97 Material material = Material::New("material");
98 DALI_TEST_EQUALS( material.GetName(), "material", TEST_LOCATION );
99 material.SetName( "AnotherMaterial" );
100 DALI_TEST_EQUALS( material.GetName(), "AnotherMaterial", TEST_LOCATION );
102 DALI_TEST_EQUALS( material.GetOpacity(), Material::DEFAULT_OPACITY, 0.001, TEST_LOCATION);
103 material.SetOpacity(0.38f);
104 DALI_TEST_EQUALS( material.GetOpacity(), 0.38f, 0.001, TEST_LOCATION);
106 DALI_TEST_EQUALS( material.GetShininess(), Material::DEFAULT_SHININESS, 0.001, TEST_LOCATION);
107 material.SetShininess(0.47f);
108 DALI_TEST_EQUALS( material.GetShininess(), 0.47f, 0.001, TEST_LOCATION);
110 DALI_TEST_EQUALS( material.GetAmbientColor(), Material::DEFAULT_AMBIENT_COLOR, 0.001, TEST_LOCATION);
111 material.SetAmbientColor(Color::BLACK);
112 DALI_TEST_EQUALS( material.GetAmbientColor(), Color::BLACK, 0.001, TEST_LOCATION);
114 DALI_TEST_EQUALS( material.GetDiffuseColor(), Material::DEFAULT_DIFFUSE_COLOR, 0.001, TEST_LOCATION);
115 material.SetDiffuseColor(Color::BLUE);
116 DALI_TEST_EQUALS( material.GetDiffuseColor(), Color::BLUE, 0.001, TEST_LOCATION);
118 DALI_TEST_EQUALS( material.GetSpecularColor(), Material::DEFAULT_SPECULAR_COLOR, 0.001, TEST_LOCATION);
119 material.SetSpecularColor(Color::GREEN);
120 DALI_TEST_EQUALS( material.GetSpecularColor(), Color::GREEN, 0.001, TEST_LOCATION);
122 DALI_TEST_EQUALS( material.GetEmissiveColor(), Material::DEFAULT_EMISSIVE_COLOR, 0.001, TEST_LOCATION);
123 material.SetEmissiveColor(Color::MAGENTA);
124 DALI_TEST_EQUALS( material.GetEmissiveColor(), Color::MAGENTA, 0.001, TEST_LOCATION);
126 material.SetDiffuseTextureFileName("diffuse-texture.png");
127 DALI_TEST_EQUALS( material.GetDiffuseFileName(), "diffuse-texture.png", TEST_LOCATION);
129 material.SetOpacityTextureFileName("opacity-texture.png");
130 DALI_TEST_EQUALS( material.GetOpacityTextureFileName(), "opacity-texture.png", TEST_LOCATION);
132 material.SetNormalMapFileName("normal-map.png");
133 DALI_TEST_EQUALS( material.GetNormalMapFileName(), "normal-map.png", TEST_LOCATION);
135 Image diffuseTexture = ResourceImage::New("diffuse-texture.png");
136 material.SetDiffuseTexture(diffuseTexture);
137 DALI_TEST_EQUALS( material.GetDiffuseTexture(), diffuseTexture, TEST_LOCATION );
139 Image opacityTexture = ResourceImage::New("opacity-texture.png");
140 material.SetOpacityTexture(opacityTexture);
141 DALI_TEST_EQUALS( material.GetOpacityTexture(), opacityTexture, TEST_LOCATION);
143 Image normalMap = ResourceImage::New("normal-map.png");
144 material.SetNormalMap(normalMap);
145 DALI_TEST_EQUALS( material.GetNormalMap(), normalMap, TEST_LOCATION);
147 DALI_TEST_EQUALS( material.GetMapU(), (unsigned int)Material::DEFAULT_MAPPING_MODE, TEST_LOCATION );
148 DALI_TEST_EQUALS( material.GetMapV(), (unsigned int)Material::DEFAULT_MAPPING_MODE, TEST_LOCATION );
149 material.SetMapU( Material::MAPPING_MODE_WRAP );
150 material.SetMapV( Material::MAPPING_MODE_MIRROR );
151 DALI_TEST_EQUALS( material.GetMapU(), (unsigned int)Material::MAPPING_MODE_WRAP, TEST_LOCATION );
152 DALI_TEST_EQUALS( material.GetMapV(), (unsigned int)Material::MAPPING_MODE_MIRROR, TEST_LOCATION );
154 DALI_TEST_EQUALS( material.GetDiffuseUVIndex(), Material::DEFAULT_DIFFUSE_UV_INDEX, TEST_LOCATION );
155 material.SetDiffuseUVIndex( 1u );
156 DALI_TEST_EQUALS( material.GetDiffuseUVIndex(), 1u, TEST_LOCATION );
158 DALI_TEST_EQUALS( material.GetOpacityUVIndex(), Material::DEFAULT_OPACITY_UV_INDEX, TEST_LOCATION );
159 material.SetOpacityUVIndex( 1u );
160 DALI_TEST_EQUALS( material.GetOpacityUVIndex(), 1u, TEST_LOCATION );
162 DALI_TEST_EQUALS( material.GetNormalUVIndex(), Material::DEFAULT_NORMAL_UV_INDEX, TEST_LOCATION );
163 material.SetNormalUVIndex( 1u );
164 DALI_TEST_EQUALS( material.GetNormalUVIndex(), 1u, TEST_LOCATION );
166 DALI_TEST_EQUALS( material.GetHasHeightMap(), Material::DEFAULT_HAS_HEIGHT_MAP, TEST_LOCATION );
167 material.SetHasHeightMap(true);
168 DALI_TEST_EQUALS( material.GetHasHeightMap(), true, TEST_LOCATION );
172 int UtcDaliMaterialStage01(void)
174 TestApplication application;
175 TraceCallStack& textureTrace = application.GetGlAbstraction().GetTextureTrace();
176 textureTrace.Enable(true);
177 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
180 Material material = Material::New("material");
181 Image image = ResourceImage::New( "image.png", ResourceImage::IMMEDIATE, Image::NEVER );
182 DALI_TEST_CHECK(image);
183 application.SendNotification();
184 application.Render(16);
186 std::vector<GLuint> ids;
188 application.GetGlAbstraction().SetNextTextureIds( ids );
189 Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
190 Integration::ResourcePointer resource(bitmap);
191 bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
192 DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
193 Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
194 DALI_TEST_CHECK( request != NULL );
197 application.GetPlatform().SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
200 material.SetDiffuseTexture(image);
201 application.SendNotification();
202 application.Render();
204 AnimatableMesh mesh = CreateMeshData(material);
206 application.SendNotification();
207 application.Render();
209 MeshActor meshActor = MeshActor::New(mesh);
210 meshActor.SetSize(100, 100);
211 Stage::GetCurrent().Add(meshActor);
212 application.SendNotification();
213 application.Render();
215 material.SetOpacity(0.5f);
216 application.SendNotification();
217 application.Render();
218 DALI_TEST_EQUALS( material.GetOpacity(), 0.5f, 0.001f, TEST_LOCATION );
220 Stage::GetCurrent().Remove(meshActor);
221 application.SendNotification();
222 application.Render();
224 DALI_TEST_CHECK( glAbstraction.CheckNoTexturesDeleted() );
225 DALI_TEST_CHECK( ! textureTrace.FindMethod( "DeleteTextures" ) );
227 application.SendNotification();
228 application.Render();
230 // Mesh should be destroyed, reducing connection count on material to zero.
231 // This should also reduce connection count on image to zero
232 DALI_TEST_EQUALS( material.GetOpacity(), 0.5f, 0.001f, TEST_LOCATION );
234 // SceneGraph::Material & SceneGraph::RenderMaterial should be destroyed
235 // Image should be destroyed
236 application.SendNotification();
237 application.Render();
239 application.Render();
240 DALI_TEST_CHECK( textureTrace.FindMethod( "DeleteTextures" ) );
241 DALI_TEST_CHECK( glAbstraction.CheckTextureDeleted( 23 ) );
246 int UtcDaliMaterialStage01MemCheck(void)
248 TestApplication application;
249 tet_result(TET_PASS);
253 int UtcDaliMaterialStage02(void)
255 TestApplication application;
256 TraceCallStack& textureTrace = application.GetGlAbstraction().GetTextureTrace();
257 textureTrace.Enable(true);
258 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
261 Material material = Material::New("material");
263 Image image = ResourceImage::New( "image.png", ResourceImage::ON_DEMAND, Image::UNUSED );
264 DALI_TEST_CHECK(image);
265 application.SendNotification();
266 application.Render(16);
267 DALI_TEST_CHECK( !application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
268 DALI_TEST_CHECK( application.GetPlatform().GetRequest() == NULL );
270 std::vector<GLuint> ids;
272 application.GetGlAbstraction().SetNextTextureIds( ids );
274 material.SetDiffuseTexture(image);
275 application.SendNotification();
276 application.Render();
277 DALI_TEST_CHECK( !application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
278 DALI_TEST_CHECK( application.GetPlatform().GetRequest() == NULL );
280 AnimatableMesh mesh = CreateMeshData(material);
282 application.SendNotification();
283 application.Render();
284 DALI_TEST_CHECK( !application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
285 DALI_TEST_CHECK( application.GetPlatform().GetRequest() == NULL );
288 MeshActor meshActor = MeshActor::New(mesh);
289 meshActor.SetSize(100, 100);
290 Stage::GetCurrent().Add(meshActor);
291 application.SendNotification();
292 application.Render();
294 // Image connection count should go to one - image should get loaded
295 DALI_TEST_CHECK( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
296 Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
297 DALI_TEST_CHECK( request != NULL );
299 Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
300 Integration::ResourcePointer resource(bitmap);
301 bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
304 application.GetPlatform().SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
306 application.Render();
308 material.SetOpacity(0.5f);
309 application.SendNotification();
310 application.Render();
311 DALI_TEST_EQUALS( material.GetOpacity(), 0.5f, 0.001f, TEST_LOCATION );
313 Stage::GetCurrent().Remove(meshActor);
314 application.SendNotification();
315 application.Render();
317 // This should reduce connection count on material to zero, freeing the texture:
318 DALI_TEST_CHECK( textureTrace.FindMethod( "DeleteTextures" ) );
319 DALI_TEST_CHECK( glAbstraction.CheckTextureDeleted( 23 ) );
321 application.SendNotification();
322 application.Render();
324 // Mesh should be destroyed, reducing connection count on material to zero.
325 // This should also reduce connection count on image to zero, freeing it
326 DALI_TEST_EQUALS( material.GetOpacity(), 0.5f, 0.001f, TEST_LOCATION );
328 application.SendNotification();
329 application.Render();
331 // SceneGraph::Material & SceneGraph::RenderMaterial should be destroyed