(AutomatedTests) Merged managed & unmanaged tests
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Context.cpp
1 /*
2  * Copyright (c) 2014 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 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23
24 using namespace Dali;
25
26 #include "mesh-builder.h"
27
28
29 namespace
30 {
31 // Size of the VertexAttributeArray enables
32 // GLES specification states that there's minimum of
33 const unsigned int TEST_MAX_ATTRIBUTE_CACHE_SIZE = 8;
34
35 enum TestAttribType
36 {
37   ATTRIB_UNKNOWN = -1,
38   ATTRIB_POSITION,
39   ATTRIB_NORMAL,
40   ATTRIB_TEXCOORD,
41   ATTRIB_COLOR,
42   ATTRIB_BONE_WEIGHTS,
43   ATTRIB_BONE_INDICES,
44   ATTRIB_TYPE_LAST
45 };
46
47 // Create bitmap image
48 static BitmapImage CreateBitmapImage()
49 {
50   BitmapImage image = BitmapImage::New(4,4,Pixel::RGBA8888);
51
52   return image;
53 }
54
55 static MeshActor CreateMeshActor()
56 {
57   MeshData meshData;
58   MeshData::VertexContainer    vertices;
59   MeshData::FaceIndices        faces;
60   BoneContainer                bones;
61   ConstructVertices(vertices, 60);
62   ConstructFaces(vertices, faces);
63   Material customMaterial = ConstructMaterial();
64   meshData.SetData(vertices, faces, bones, customMaterial);
65   meshData.SetHasNormals(true);
66   meshData.SetHasTextureCoords(true);
67
68   Mesh mesh = Mesh::New(meshData);
69   MeshActor actor = MeshActor::New(mesh);
70   actor.SetName("Test MeshActor");
71
72   return actor;
73 }
74
75 static LightActor CreateLight()
76 {
77   Light light = Light::New("Light");
78   light.SetType( POINT );
79   light.SetAmbientColor( Vector3( 0.22f, 0.33f, 0.44f ) );
80   light.SetDiffuseColor( Vector3( 0.55f, 0.66f, 0.77f) );
81   light.SetSpecularColor( Vector3( 0.88f, 0.99f, 0.11f) );
82   LightActor lightActor = LightActor::New();
83   lightActor.SetParentOrigin( ParentOrigin::CENTER );
84   lightActor.SetPosition( 0.f, 0.f, 100.0f );
85   lightActor.SetLight( light );
86   lightActor.SetName( light.GetName() );
87
88   return lightActor;
89 }
90
91 static ImageActor CreateImageActor()
92 {
93   BitmapImage image = CreateBitmapImage();
94   ImageActor actor = ImageActor::New( image );
95   actor.SetSize( 100.0f, 100.0f );
96   actor.SetName("Test ImageActor");
97   return actor;
98 }
99
100 } // anonymous namespace
101
102
103 // Positive test case for a method
104 int UtcDaliContextVertexAttribStartup(void)
105 {
106   tet_infoline("Testing vertex attrib initial state in context");
107
108   TestApplication application;
109
110   // start up
111   application.SendNotification();
112   application.Render();
113   application.Render();
114
115   // context class should initially set the vertex attrib locations to disable
116   // Make sure it has been modified
117   DALI_TEST_CHECK(application.GetGlAbstraction().GetVertexAttribArrayChanged());
118
119   // check the locations
120   for (unsigned int i = 0; i < TEST_MAX_ATTRIBUTE_CACHE_SIZE; i++)
121   {
122     DALI_TEST_CHECK( application.GetGlAbstraction().GetVertexAttribArrayState(i) == false);
123   }
124
125   tet_result(TET_PASS);
126   END_TEST;
127 }
128
129 // Tests to make the attribs only get set once when continually rendering an image actor
130 int UtcDaliContextVertexAttribImageRendering(void)
131 {
132   tet_infoline("Testing vertex attrib rendering state in context with images");
133
134   TestApplication application;
135
136   // start up
137   application.SendNotification();
138   application.Render();
139   application.Render();
140
141   // the vertex attribs get modified on startup to set them to disabled
142   // clear the flag to say they've changed
143   application.GetGlAbstraction().ClearVertexAttribArrayChanged();
144
145
146   // create a test image actor
147   ImageActor imageActor(CreateImageActor());
148   Stage::GetCurrent().Add(imageActor);
149
150
151   application.SendNotification();
152   application.Render();
153   application.Render();
154
155   // check to make sure the state has changed (the image renderer will enable some
156   // locations).
157   DALI_TEST_CHECK(application.GetGlAbstraction().GetVertexAttribArrayChanged());
158
159   // Now check to make sure the state is cached, and isn't being set each frame.
160   application.GetGlAbstraction().ClearVertexAttribArrayChanged();
161
162   application.Render();
163   application.Render();
164   application.Render();
165
166   // if it has changed then the caching has failed
167   DALI_TEST_CHECK(application.GetGlAbstraction().GetVertexAttribArrayChanged() == false);
168
169
170   tet_result(TET_PASS);
171   END_TEST;
172 }
173
174 // test to make sure the attribs change when rendering both image and mode actors
175 int UtcDaliContextVertexAttribImageAndModelRendering(void)
176 {
177   tet_infoline("Testing vertex attrib rendering state in context with images and models");
178
179   TestApplication application;
180
181   // start up
182   application.SendNotification();
183   application.Render();
184   application.Render();
185
186   // the vertex attribs get modified on startup to set them to disabled
187   // clear the flag to say they've changed
188   application.GetGlAbstraction().ClearVertexAttribArrayChanged();
189
190   // create a test light, image and mesh actor. (meshes won't render without light)
191
192   LightActor lightActor(CreateLight());
193   Stage::GetCurrent().Add(lightActor);
194   lightActor.SetActive(true);
195
196   MeshActor meshActor(CreateMeshActor());
197   Stage::GetCurrent().Add(meshActor);
198
199   ImageActor imageActor(CreateImageActor());
200   Stage::GetCurrent().Add(imageActor);
201
202
203   application.SendNotification();
204   application.Render();
205   application.Render();
206
207   // check to make sure the state changes during the rendering of a frame
208   DALI_TEST_CHECK(application.GetGlAbstraction().GetVertexAttribArrayChanged());
209
210   // Now check to make sure the state is changing each frame.
211   application.GetGlAbstraction().ClearVertexAttribArrayChanged();
212
213   application.Render();
214   application.Render();
215   application.Render();
216
217   // make sure the state has changed
218   DALI_TEST_CHECK(application.GetGlAbstraction().GetVertexAttribArrayChanged());
219
220   // depending on the order of drawing, one of the attrib locations should be disabled
221   // Image uses locations 0 & 2  (position, texture)
222   // Model uses locations 0 & 1  (position, normals) -no textures
223   // so either location 1 or location 2 should be disabled after drawing.
224
225   // see if mesh was last to draw
226   if (application.GetGlAbstraction().GetVertexAttribArrayState(ATTRIB_NORMAL))
227   {
228     // texture should be disabled
229     DALI_TEST_CHECK( application.GetGlAbstraction().GetVertexAttribArrayState(ATTRIB_TEXCOORD) == false)
230   }
231   else
232   {
233     // image was to draw so, normals should be disabled
234     DALI_TEST_CHECK( application.GetGlAbstraction().GetVertexAttribArrayState(ATTRIB_NORMAL) == false)
235   }
236
237   tet_result(TET_PASS);
238
239   END_TEST;
240 }