1d5698fbd92f56845e21f926c30f8a3417e5af68
[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 BufferImage CreateBufferImage()
49 {
50   BufferImage image = BufferImage::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
71   actor.SetName("Test MeshActor");
72
73   return actor;
74 }
75
76
77 static ImageActor CreateImageActor()
78 {
79   BufferImage image = CreateBufferImage();
80   ImageActor actor = ImageActor::New( image );
81   actor.SetSize( 100.0f, 100.0f );
82   actor.SetName("Test ImageActor");
83   return actor;
84 }
85
86 } // anonymous namespace
87
88
89 // Positive test case for a method
90 int UtcDaliContextVertexAttribStartup(void)
91 {
92   tet_infoline("Testing vertex attrib initial state in context");
93
94   TestApplication application;
95
96   // start up
97   application.SendNotification();
98   application.Render();
99   application.Render();
100
101   // context class should initially set the vertex attrib locations to disable
102   // Make sure it has been modified
103   DALI_TEST_CHECK(application.GetGlAbstraction().GetVertexAttribArrayChanged());
104
105   // check the locations
106   for (unsigned int i = 0; i < TEST_MAX_ATTRIBUTE_CACHE_SIZE; i++)
107   {
108     DALI_TEST_CHECK( application.GetGlAbstraction().GetVertexAttribArrayState(i) == false);
109   }
110
111   tet_result(TET_PASS);
112   END_TEST;
113 }
114
115 // Tests to make the attribs only get set once when continually rendering an image actor
116 int UtcDaliContextVertexAttribImageRendering(void)
117 {
118   tet_infoline("Testing vertex attrib rendering state in context with images");
119
120   TestApplication application;
121
122   // start up
123   application.SendNotification();
124   application.Render();
125   application.Render();
126
127   // the vertex attribs get modified on startup to set them to disabled
128   // clear the flag to say they've changed
129   application.GetGlAbstraction().ClearVertexAttribArrayChanged();
130
131
132   // create a test image actor
133   ImageActor imageActor(CreateImageActor());
134   Stage::GetCurrent().Add(imageActor);
135
136
137   application.SendNotification();
138   application.Render();
139   application.Render();
140
141   // check to make sure the state has changed (the image renderer will enable some
142   // locations).
143   DALI_TEST_CHECK(application.GetGlAbstraction().GetVertexAttribArrayChanged());
144
145   // Now check to make sure the state is cached, and isn't being set each frame.
146   application.GetGlAbstraction().ClearVertexAttribArrayChanged();
147
148   application.Render();
149   application.Render();
150   application.Render();
151
152   // if it has changed then the caching has failed
153   DALI_TEST_CHECK(application.GetGlAbstraction().GetVertexAttribArrayChanged() == false);
154
155
156   tet_result(TET_PASS);
157   END_TEST;
158 }
159
160 // test to make sure the attribs change when rendering both image and mode actors
161 int UtcDaliContextVertexAttribImageAndModelRendering(void)
162 {
163   tet_infoline("Testing vertex attrib rendering state in context with images and models");
164
165   TestApplication application;
166
167   // start up
168   application.SendNotification();
169   application.Render();
170   application.Render();
171
172   // the vertex attribs get modified on startup to set them to disabled
173   // clear the flag to say they've changed
174   application.GetGlAbstraction().ClearVertexAttribArrayChanged();
175
176   // create a test image and mesh actor.
177
178   MeshActor meshActor(CreateMeshActor());
179   Stage::GetCurrent().Add(meshActor);
180
181   ImageActor imageActor(CreateImageActor());
182   Stage::GetCurrent().Add(imageActor);
183
184
185   application.SendNotification();
186   application.Render();
187   application.Render();
188
189   // check to make sure the state changes during the rendering of a frame
190   DALI_TEST_CHECK(application.GetGlAbstraction().GetVertexAttribArrayChanged());
191
192   // Now check to make sure the state is changing each frame.
193   application.GetGlAbstraction().ClearVertexAttribArrayChanged();
194
195   application.Render();
196   application.Render();
197   application.Render();
198
199   // make sure the state has changed
200   DALI_TEST_CHECK(application.GetGlAbstraction().GetVertexAttribArrayChanged());
201
202   // depending on the order of drawing, one of the attrib locations should be disabled
203   // Image uses locations 0 & 2  (position, texture)
204   // Model uses locations 0 & 1  (position, normals) -no textures
205   // so either location 1 or location 2 should be disabled after drawing.
206
207   // see if mesh was last to draw
208   if (application.GetGlAbstraction().GetVertexAttribArrayState(ATTRIB_NORMAL))
209   {
210     // texture should be disabled
211     DALI_TEST_CHECK( application.GetGlAbstraction().GetVertexAttribArrayState(ATTRIB_TEXCOORD) == false)
212   }
213   else
214   {
215     // image was to draw so, normals should be disabled
216     DALI_TEST_CHECK( application.GetGlAbstraction().GetVertexAttribArrayState(ATTRIB_NORMAL) == false)
217   }
218
219   tet_result(TET_PASS);
220
221   END_TEST;
222 }