c2680b229b4a5ba368f247b761de8bfc3dbe47ee
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-application.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 "test-application.h"
19
20 namespace Dali
21 {
22
23
24 TestApplication::TestApplication( size_t surfaceWidth,
25                                   size_t surfaceHeight,
26                                   float  horizontalDpi,
27                                   float  verticalDpi)
28 : mCore( NULL ),
29   mSurfaceWidth( surfaceWidth ),
30   mSurfaceHeight( surfaceHeight ),
31   mFrame( 0u ),
32   mDpi( horizontalDpi, verticalDpi ),
33   mLastVSyncTime(0u)
34 {
35   Initialize();
36 }
37
38 TestApplication::TestApplication( bool   initialize,
39                                   size_t surfaceWidth,
40                                   size_t surfaceHeight,
41                                   float  horizontalDpi,
42                                   float  verticalDpi )
43 : mCore( NULL ),
44   mSurfaceWidth( surfaceWidth ),
45   mSurfaceHeight( surfaceHeight ),
46   mFrame( 0u ),
47   mDpi( horizontalDpi, verticalDpi )
48 {
49   if ( initialize )
50   {
51     Initialize();
52   }
53 }
54
55 void TestApplication::Initialize()
56 {
57   mCore = Dali::Integration::Core::New(
58     mRenderController,
59     mPlatformAbstraction,
60     mGlAbstraction,
61     mGlSyncAbstraction,
62     mGestureManager );
63
64   mCore->ContextCreated();
65   mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );
66   mCore->SetDpi( mDpi.x, mDpi.y );
67
68   Dali::Integration::Log::LogFunction logFunction(&TestApplication::LogMessage);
69   Dali::Integration::Log::InstallLogFunction(logFunction);
70 }
71
72 TestApplication::~TestApplication()
73 {
74   Dali::Integration::Log::UninstallLogFunction();
75   delete mCore;
76 }
77
78 void TestApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
79 {
80   switch(level)
81   {
82     case Dali::Integration::Log::DebugInfo:
83       fprintf(stderr, "INFO: %s", message.c_str());
84       break;
85     case Dali::Integration::Log::DebugWarning:
86       fprintf(stderr, "WARN: %s", message.c_str());
87       break;
88     case Dali::Integration::Log::DebugError:
89       fprintf(stderr, "ERROR: %s", message.c_str());
90       break;
91     default:
92       fprintf(stderr, "DEFAULT: %s", message.c_str());
93       break;
94   }
95 }
96
97 Dali::Integration::Core& TestApplication::GetCore()
98 {
99   return *mCore;
100 }
101
102 TestPlatformAbstraction& TestApplication::GetPlatform()
103 {
104   return mPlatformAbstraction;
105 }
106
107 TestRenderController& TestApplication::GetRenderController()
108 {
109   return mRenderController;
110 }
111
112 TestGlAbstraction& TestApplication::GetGlAbstraction()
113 {
114   return mGlAbstraction;
115 }
116
117 TestGlSyncAbstraction& TestApplication::GetGlSyncAbstraction()
118 {
119   return mGlSyncAbstraction;
120 }
121
122 TestGestureManager& TestApplication::GetGestureManager()
123 {
124   return mGestureManager;
125 }
126
127 void TestApplication::ProcessEvent(const Integration::Event& event)
128 {
129   mCore->QueueEvent(event);
130   mCore->ProcessEvents();
131 }
132
133 void TestApplication::SendNotification()
134 {
135   mCore->ProcessEvents();
136 }
137
138 void TestApplication::SetSurfaceWidth( unsigned int width, unsigned height )
139 {
140   mSurfaceWidth = width;
141   mSurfaceHeight = height;
142
143   mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );
144 }
145
146 void TestApplication::DoUpdate( unsigned int intervalMilliseconds )
147 {
148   unsigned int seconds(0u), microseconds(0u);
149   mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
150   mLastVSyncTime = ( seconds * 1e3 ) + ( microseconds / 1e3 );
151   unsigned int nextVSyncTime = mLastVSyncTime + 16;
152
153   // Update Time values
154   mPlatformAbstraction.IncrementGetTimeResult( intervalMilliseconds );
155
156   float elapsedSeconds = intervalMilliseconds / 1e3f;
157   mCore->Update( elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus );
158 }
159
160 bool TestApplication::Render( unsigned int intervalMilliseconds  )
161 {
162   DoUpdate( intervalMilliseconds );
163   mCore->Render( mRenderStatus );
164
165   mFrame++;
166
167   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
168 }
169
170 unsigned int TestApplication::GetUpdateStatus()
171 {
172   return mStatus.KeepUpdating();
173 }
174
175 bool TestApplication::UpdateOnly( unsigned int intervalMilliseconds  )
176 {
177   DoUpdate( intervalMilliseconds );
178   return mStatus.KeepUpdating();
179 }
180
181 bool TestApplication::RenderOnly( )
182 {
183   // Update Time values
184   mCore->Render( mRenderStatus );
185
186   mFrame++;
187
188   return mRenderStatus.NeedsUpdate();
189 }
190
191 void TestApplication::ResetContext()
192 {
193   mCore->ContextToBeDestroyed();
194   mCore->ContextCreated();
195 }
196
197
198 } // Namespace dali