RenderSurface interface change in automated test utils
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-web-engine.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 "toolkit-timer.h"
19
20 #include <dali/devel-api/adaptor-framework/web-engine.h>
21 #include <dali/public-api/object/any.h>
22 #include <dali/public-api/object/base-object.h>
23 #include <dali/public-api/adaptor-framework/native-image-source.h>
24 #include <dali/public-api/images/native-image.h>
25 #include <toolkit-application.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace Adaptor
34 {
35
36 class WebEngine;
37
38 namespace
39 {
40 static WebEngine* gInstance = NULL;
41 static int gInstanceCount = 0;
42
43 bool OnGoBack();
44 bool OnGoForward();
45 bool OnLoadUrl();
46 bool OnClearHistory();
47
48 static void ConnectToGlobalSignal( bool (*func)() )
49 {
50   Dali::Timer timer = Dali::Timer::New( 0 );
51   timer.TickSignal().Connect( func );
52 }
53
54 static void DisconnectFromGlobalSignal( bool (*func)() )
55 {
56   Dali::Timer timer = Dali::Timer::New( 0 );
57   timer.TickSignal().Disconnect( func );
58 }
59 }
60
61 class WebEngine: public Dali::BaseObject
62 {
63 public:
64
65   WebEngine()
66     : mUrl()
67     , mCurrentPlusOnePos( 0 )
68   {
69     gInstanceCount++;
70     gInstance = this;
71   }
72
73   virtual ~WebEngine()
74   {
75     gInstanceCount--;
76     if ( !gInstanceCount )
77     {
78       gInstance = NULL;
79     }
80   }
81
82   void LoadUrl( const std::string& url )
83   {
84     mUrl = url;
85     ConnectToGlobalSignal( &OnLoadUrl );
86   }
87
88   const std::string& GetUrl() const
89   {
90     return mUrl;
91   }
92
93   bool CanGoForward() const
94   {
95     return mHistory.size() > mCurrentPlusOnePos;
96   }
97
98   void GoForward()
99   {
100     ConnectToGlobalSignal( &OnGoForward );
101   }
102
103   bool CanGoBack() const
104   {
105     return mCurrentPlusOnePos > 1;
106   }
107
108   void GoBack()
109   {
110     ConnectToGlobalSignal( &OnGoBack );
111   }
112
113   void ClearHistory()
114   {
115     ConnectToGlobalSignal( &OnClearHistory );
116   }
117
118   Dali::WebEnginePlugin::WebEngineSignalType& PageLoadStartedSignal()
119   {
120     return mPageLoadStartedSignal;
121   }
122
123   Dali::WebEnginePlugin::WebEngineSignalType& PageLoadFinishedSignal()
124   {
125     return mPageLoadFinishedSignal;
126   }
127
128   std::string mUrl;
129   std::vector< std::string > mHistory;
130   size_t mCurrentPlusOnePos;
131   Dali::WebEnginePlugin::WebEngineSignalType mPageLoadStartedSignal;
132   Dali::WebEnginePlugin::WebEngineSignalType mPageLoadFinishedSignal;
133 };
134
135 inline WebEngine& GetImplementation( Dali::WebEngine& webEngine )
136 {
137   DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
138   BaseObject& handle = webEngine.GetBaseObject();
139   return static_cast< Internal::Adaptor::WebEngine& >( handle );
140 }
141
142 inline const WebEngine& GetImplementation( const Dali::WebEngine& webEngine )
143 {
144   DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
145   const BaseObject& handle = webEngine.GetBaseObject();
146   return static_cast< const Internal::Adaptor::WebEngine& >( handle );
147 }
148
149 namespace
150 {
151
152 bool OnGoBack()
153 {
154   DisconnectFromGlobalSignal( &OnGoBack );
155
156   if ( gInstance && gInstance->CanGoBack() )
157   {
158     gInstance->mCurrentPlusOnePos--;
159   }
160   return false;
161 }
162
163 bool OnGoForward()
164 {
165   DisconnectFromGlobalSignal( &OnGoForward );
166
167   if ( gInstance && gInstance->CanGoForward() )
168   {
169     gInstance->mCurrentPlusOnePos++;
170   }
171   return false;
172 }
173
174 bool OnLoadUrl()
175 {
176   DisconnectFromGlobalSignal( &OnLoadUrl );
177
178   if ( gInstance )
179   {
180     if ( gInstance->mHistory.size() > gInstance->mCurrentPlusOnePos )
181     {
182       gInstance->mHistory.erase( gInstance->mHistory.begin() + gInstance->mCurrentPlusOnePos, gInstance->mHistory.end() );
183     }
184     gInstance->mHistory.push_back( gInstance->mUrl );
185     gInstance->mCurrentPlusOnePos++;
186     gInstance->mPageLoadStartedSignal.Emit( gInstance->mUrl );
187     gInstance->mPageLoadFinishedSignal.Emit( gInstance->mUrl );
188   }
189
190   return false;
191 }
192
193 bool OnClearHistory()
194 {
195   DisconnectFromGlobalSignal( &OnClearHistory );
196
197   if ( gInstance && gInstance->mCurrentPlusOnePos ) {
198     std::string url = gInstance->mHistory[ gInstance->mCurrentPlusOnePos - 1 ];
199     std::vector< std::string >().swap( gInstance->mHistory );
200     gInstance->mHistory.push_back( url );
201     gInstance->mCurrentPlusOnePos = 1;
202   }
203   return false;
204 }
205 } // namespace
206
207 } // namespace Adaptor
208
209 } // namespace Internal
210
211
212 // Dali::WebEngine Implementation
213 WebEngine::WebEngine()
214 {
215 }
216
217 WebEngine::WebEngine( Internal::Adaptor::WebEngine* internal )
218 : BaseHandle( internal )
219 {
220 }
221
222 WebEngine::~WebEngine()
223 {
224 }
225
226 WebEngine WebEngine::New()
227 {
228   Internal::Adaptor::WebEngine* baseObject = new Internal::Adaptor::WebEngine();
229
230   return WebEngine( baseObject );
231 }
232
233 WebEngine::WebEngine( const WebEngine& WebEngine )
234 : BaseHandle( WebEngine )
235 {
236 }
237
238 WebEngine& WebEngine::operator=( const WebEngine& webEngine )
239 {
240   BaseHandle::operator=( webEngine );
241   return *this;
242 }
243
244 WebEngine WebEngine::DownCast( BaseHandle handle )
245 {
246   return WebEngine( dynamic_cast< Internal::Adaptor::WebEngine* >( handle.GetObjectPtr() ) );
247 }
248
249 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
250 {
251 }
252
253 void WebEngine::Destroy()
254 {
255 }
256
257 void WebEngine::LoadUrl( const std::string& url )
258 {
259   return Internal::Adaptor::GetImplementation( *this ).LoadUrl( url );
260 }
261
262 const std::string& WebEngine::GetUrl()
263 {
264   return Internal::Adaptor::GetImplementation( *this ).GetUrl();
265 }
266
267 NativeImageInterfacePtr WebEngine::GetNativeImageSource()
268 {
269   Any source;
270   Dali::NativeImageSourcePtr sourcePtr = Dali::NativeImageSource::New( source );
271   return sourcePtr;
272 }
273
274 void WebEngine::LoadHTMLString( const std::string& htmlString )
275 {
276 }
277
278 void WebEngine::Reload()
279 {
280 }
281
282 void WebEngine::StopLoading()
283 {
284 }
285
286 bool WebEngine::CanGoForward()
287 {
288   return Internal::Adaptor::GetImplementation( *this ).CanGoForward();
289 }
290
291 void WebEngine::GoForward()
292 {
293   Internal::Adaptor::GetImplementation( *this ).GoForward();
294 }
295
296 bool WebEngine::CanGoBack()
297 {
298   return Internal::Adaptor::GetImplementation( *this ).CanGoBack();
299 }
300
301 void WebEngine::GoBack()
302 {
303   Internal::Adaptor::GetImplementation( *this ).GoBack();
304 }
305
306 void WebEngine::EvaluateJavaScript( const std::string& script )
307 {
308 }
309
310 void WebEngine::AddJavaScriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName, std::function< std::string(const std::string&) > cb )
311 {
312 }
313
314 void WebEngine::RemoveJavascriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName )
315 {
316 }
317
318 void WebEngine::ClearHistory()
319 {
320   Internal::Adaptor::GetImplementation( *this ).ClearHistory();
321 }
322
323 void WebEngine::ClearCache()
324 {
325 }
326
327 void WebEngine::SetSize( int width, int height )
328 {
329 }
330
331 bool WebEngine::SendTouchEvent( const TouchData& touch )
332 {
333   return true;
334 }
335
336 bool WebEngine::SendKeyEvent( const KeyEvent& event )
337 {
338   return true;
339 }
340
341 Dali::WebEnginePlugin::WebEngineSignalType& WebEngine::PageLoadStartedSignal()
342 {
343   return Internal::Adaptor::GetImplementation( *this ).PageLoadStartedSignal();
344 }
345
346 Dali::WebEnginePlugin::WebEngineSignalType& WebEngine::PageLoadFinishedSignal()
347 {
348   return Internal::Adaptor::GetImplementation( *this ).PageLoadFinishedSignal();
349 }
350
351 } // namespace Dali;
352