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