Merge "Add KeyboardFocus History Stack" into devel/master
[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                                   ResourcePolicy::DataRetention policy)
29 : mCore( NULL ),
30   mSurfaceWidth( surfaceWidth ),
31   mSurfaceHeight( surfaceHeight ),
32   mFrame( 0u ),
33   mDpi( horizontalDpi, verticalDpi ),
34   mLastVSyncTime(0u),
35   mDataRetentionPolicy( policy )
36 {
37   Initialize();
38 }
39
40 TestApplication::TestApplication( bool   initialize,
41                                   size_t surfaceWidth,
42                                   size_t surfaceHeight,
43                                   float  horizontalDpi,
44                                   float  verticalDpi,
45                                   ResourcePolicy::DataRetention policy)
46 : mCore( NULL ),
47   mSurfaceWidth( surfaceWidth ),
48   mSurfaceHeight( surfaceHeight ),
49   mFrame( 0u ),
50   mDpi( horizontalDpi, verticalDpi ),
51   mLastVSyncTime(0u),
52   mDataRetentionPolicy( policy )
53 {
54   if ( initialize )
55   {
56     Initialize();
57   }
58 }
59
60 void TestApplication::Initialize()
61 {
62   // We always need the first update!
63   mStatus.keepUpdating = Integration::KeepUpdating::STAGE_KEEP_RENDERING;
64
65   mCore = Dali::Integration::Core::New(
66     mRenderController,
67     mPlatformAbstraction,
68     mGlAbstraction,
69     mGlSyncAbstraction,
70     mGestureManager,
71     mDataRetentionPolicy);
72
73   mCore->ContextCreated();
74   mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );
75   mCore->SetDpi( mDpi.x, mDpi.y );
76
77   Dali::Integration::Log::LogFunction logFunction(&TestApplication::LogMessage);
78   Dali::Integration::Log::InstallLogFunction(logFunction);
79
80   mCore->SceneCreated();
81 }
82
83 TestApplication::~TestApplication()
84 {
85   Dali::Integration::Log::UninstallLogFunction();
86   delete mCore;
87 }
88
89 void TestApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
90 {
91   switch(level)
92   {
93     case Dali::Integration::Log::DebugInfo:
94       fprintf(stderr, "INFO: %s", message.c_str());
95       break;
96     case Dali::Integration::Log::DebugWarning:
97       fprintf(stderr, "WARN: %s", message.c_str());
98       break;
99     case Dali::Integration::Log::DebugError:
100       fprintf(stderr, "ERROR: %s", message.c_str());
101       break;
102     default:
103       fprintf(stderr, "DEFAULT: %s", message.c_str());
104       break;
105   }
106 }
107
108 Dali::Integration::Core& TestApplication::GetCore()
109 {
110   return *mCore;
111 }
112
113 TestPlatformAbstraction& TestApplication::GetPlatform()
114 {
115   return mPlatformAbstraction;
116 }
117
118 TestRenderController& TestApplication::GetRenderController()
119 {
120   return mRenderController;
121 }
122
123 TestGlAbstraction& TestApplication::GetGlAbstraction()
124 {
125   return mGlAbstraction;
126 }
127
128 TestGlSyncAbstraction& TestApplication::GetGlSyncAbstraction()
129 {
130   return mGlSyncAbstraction;
131 }
132
133 TestGestureManager& TestApplication::GetGestureManager()
134 {
135   return mGestureManager;
136 }
137
138 void TestApplication::ProcessEvent(const Integration::Event& event)
139 {
140   mCore->QueueEvent(event);
141   mCore->ProcessEvents();
142 }
143
144 void TestApplication::SendNotification()
145 {
146   mCore->ProcessEvents();
147 }
148
149 void TestApplication::SetSurfaceWidth( unsigned int width, unsigned height )
150 {
151   mSurfaceWidth = width;
152   mSurfaceHeight = height;
153
154   mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );
155 }
156
157 void TestApplication::SetTopMargin( unsigned int margin )
158 {
159   mCore->SetTopMargin( margin );
160 }
161
162 void TestApplication::DoUpdate( unsigned int intervalMilliseconds, const char* location )
163 {
164   if( GetUpdateStatus() == 0 &&
165       mRenderStatus.NeedsUpdate() == false &&
166       ! GetRenderController().WasCalled(TestRenderController::RequestUpdateFunc) )
167   {
168     fprintf(stderr, "WARNING - Update not required :%s\n", location==NULL?"NULL":location);
169   }
170
171   unsigned int nextVSyncTime = mLastVSyncTime + intervalMilliseconds;
172   float elapsedSeconds = intervalMilliseconds / 1e3f;
173
174   mCore->Update( elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus );
175
176   GetRenderController().Initialize();
177
178   mLastVSyncTime = nextVSyncTime;
179 }
180
181 bool TestApplication::Render( unsigned int intervalMilliseconds, const char* location )
182 {
183   DoUpdate( intervalMilliseconds, location );
184   mCore->Render( mRenderStatus );
185
186   mFrame++;
187
188   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
189 }
190
191 unsigned int TestApplication::GetUpdateStatus()
192 {
193   return mStatus.KeepUpdating();
194 }
195
196 bool TestApplication::UpdateOnly( unsigned int intervalMilliseconds  )
197 {
198   DoUpdate( intervalMilliseconds );
199   return mStatus.KeepUpdating();
200 }
201
202 bool TestApplication::GetRenderNeedsUpdate()
203 {
204   return mRenderStatus.NeedsUpdate();
205 }
206
207 bool TestApplication::RenderOnly( )
208 {
209   // Update Time values
210   mCore->Render( mRenderStatus );
211
212   mFrame++;
213
214   return mRenderStatus.NeedsUpdate();
215 }
216
217 void TestApplication::ResetContext()
218 {
219   mCore->ContextDestroyed();
220   mGlAbstraction.Initialize();
221   mCore->ContextCreated();
222 }
223
224 unsigned int TestApplication::Wait( unsigned int durationToWait )
225 {
226   int time = 0;
227
228   for(unsigned int i = 0; i <= ( durationToWait / RENDER_FRAME_INTERVAL); i++)
229   {
230     SendNotification();
231     Render(RENDER_FRAME_INTERVAL);
232     time += RENDER_FRAME_INTERVAL;
233   }
234   return time;
235 }
236
237 } // Namespace dali