[dali_1.1.10] 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) 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   mCore = Dali::Integration::Core::New(
63     mRenderController,
64     mPlatformAbstraction,
65     mGlAbstraction,
66     mGlSyncAbstraction,
67     mGestureManager,
68     mDataRetentionPolicy);
69
70   mCore->ContextCreated();
71   mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );
72   mCore->SetDpi( mDpi.x, mDpi.y );
73
74   Dali::Integration::Log::LogFunction logFunction(&TestApplication::LogMessage);
75   Dali::Integration::Log::InstallLogFunction(logFunction);
76
77   mCore->SceneCreated();
78 }
79
80 TestApplication::~TestApplication()
81 {
82   Dali::Integration::Log::UninstallLogFunction();
83   delete mCore;
84 }
85
86 void TestApplication::LogMessage(Dali::Integration::Log::DebugPriority level, std::string& message)
87 {
88   switch(level)
89   {
90     case Dali::Integration::Log::DebugInfo:
91       fprintf(stderr, "INFO: %s", message.c_str());
92       break;
93     case Dali::Integration::Log::DebugWarning:
94       fprintf(stderr, "WARN: %s", message.c_str());
95       break;
96     case Dali::Integration::Log::DebugError:
97       fprintf(stderr, "ERROR: %s", message.c_str());
98       break;
99     default:
100       fprintf(stderr, "DEFAULT: %s", message.c_str());
101       break;
102   }
103 }
104
105 Dali::Integration::Core& TestApplication::GetCore()
106 {
107   return *mCore;
108 }
109
110 TestPlatformAbstraction& TestApplication::GetPlatform()
111 {
112   return mPlatformAbstraction;
113 }
114
115 TestRenderController& TestApplication::GetRenderController()
116 {
117   return mRenderController;
118 }
119
120 TestGlAbstraction& TestApplication::GetGlAbstraction()
121 {
122   return mGlAbstraction;
123 }
124
125 TestGlSyncAbstraction& TestApplication::GetGlSyncAbstraction()
126 {
127   return mGlSyncAbstraction;
128 }
129
130 TestGestureManager& TestApplication::GetGestureManager()
131 {
132   return mGestureManager;
133 }
134
135 void TestApplication::ProcessEvent(const Integration::Event& event)
136 {
137   mCore->QueueEvent(event);
138   mCore->ProcessEvents();
139 }
140
141 void TestApplication::SendNotification()
142 {
143   mCore->ProcessEvents();
144 }
145
146 void TestApplication::SetSurfaceWidth( unsigned int width, unsigned height )
147 {
148   mSurfaceWidth = width;
149   mSurfaceHeight = height;
150
151   mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );
152 }
153
154 void TestApplication::DoUpdate( unsigned int intervalMilliseconds )
155 {
156   unsigned int nextVSyncTime = mLastVSyncTime + intervalMilliseconds;
157   float elapsedSeconds = intervalMilliseconds / 1e3f;
158
159   mCore->Update( elapsedSeconds, mLastVSyncTime, nextVSyncTime, mStatus );
160
161   mLastVSyncTime = nextVSyncTime;
162 }
163
164 bool TestApplication::Render( unsigned int intervalMilliseconds  )
165 {
166   DoUpdate( intervalMilliseconds );
167   mCore->Render( mRenderStatus );
168
169   mFrame++;
170
171   return mStatus.KeepUpdating() || mRenderStatus.NeedsUpdate();
172 }
173
174 unsigned int TestApplication::GetUpdateStatus()
175 {
176   return mStatus.KeepUpdating();
177 }
178
179 bool TestApplication::UpdateOnly( unsigned int intervalMilliseconds  )
180 {
181   DoUpdate( intervalMilliseconds );
182   return mStatus.KeepUpdating();
183 }
184
185 bool TestApplication::RenderOnly( )
186 {
187   // Update Time values
188   mCore->Render( mRenderStatus );
189
190   mFrame++;
191
192   return mRenderStatus.NeedsUpdate();
193 }
194
195 void TestApplication::ResetContext()
196 {
197   mCore->ContextDestroyed();
198   mCore->ContextCreated();
199 }
200
201
202 } // Namespace dali