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