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