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