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