Merge "Fix InputMethodContext to work well in multi-window env" into devel/master
[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 OnEvaluteJavaScript();
47 bool OnClearHistory();
48
49 static void ConnectToGlobalSignal( bool (*func)() )
50 {
51   Dali::Timer timer = Dali::Timer::New( 0 );
52   timer.TickSignal().Connect( func );
53 }
54
55 static void DisconnectFromGlobalSignal( bool (*func)() )
56 {
57   Dali::Timer timer = Dali::Timer::New( 0 );
58   timer.TickSignal().Disconnect( func );
59 }
60 }
61
62 class WebEngine: public Dali::BaseObject
63 {
64 public:
65
66   WebEngine()
67     : mUrl()
68     , mCurrentPlusOnePos( 0 )
69     , mCacheModel( Dali::WebEnginePlugin::CacheModel::DOCUMENT_VIEWER )
70     , mCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy::NO_THIRD_PARTY )
71     , mUserAgent()
72     , mEnableJavaScript( true )
73     , mLoadImagesAutomatically( true )
74     , mDefaultTextEncodingName()
75     , mDefaultFontSize( 16 )
76     , mEvaluating( false )
77   {
78     gInstanceCount++;
79     gInstance = this;
80   }
81
82   virtual ~WebEngine()
83   {
84     gInstanceCount--;
85     if( !gInstanceCount )
86     {
87       gInstance = NULL;
88     }
89   }
90
91   void LoadUrl( const std::string& url )
92   {
93     mUrl = url;
94     ConnectToGlobalSignal( &OnLoadUrl );
95   }
96
97   const std::string& GetUrl() const
98   {
99     return mUrl;
100   }
101
102   bool CanGoForward() const
103   {
104     return mHistory.size() > mCurrentPlusOnePos;
105   }
106
107   void GoForward()
108   {
109     ConnectToGlobalSignal( &OnGoForward );
110   }
111
112   bool CanGoBack() const
113   {
114     return mCurrentPlusOnePos > 1;
115   }
116
117   void GoBack()
118   {
119     ConnectToGlobalSignal( &OnGoBack );
120   }
121
122   void EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
123   {
124     if( resultHandler )
125     {
126       if( !mEvaluating )
127       {
128         ConnectToGlobalSignal( &OnEvaluteJavaScript );
129       }
130       mResultCallbacks.push_back( resultHandler );
131     }
132   }
133
134   void ClearHistory()
135   {
136     ConnectToGlobalSignal( &OnClearHistory );
137   }
138
139   Dali::WebEnginePlugin::CacheModel GetCacheModel() const
140   {
141     return mCacheModel;
142   }
143
144   void SetCacheModel( Dali::WebEnginePlugin::CacheModel cacheModel )
145   {
146     mCacheModel = cacheModel;
147   }
148
149   Dali::WebEnginePlugin::CookieAcceptPolicy GetCookieAcceptPolicy() const
150   {
151     return mCookieAcceptPolicy;
152   }
153
154   void SetCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy policy )
155   {
156     mCookieAcceptPolicy = policy;
157   }
158
159   const std::string& GetUserAgent() const
160   {
161     return mUserAgent;
162   }
163
164   void SetUserAgent( const std::string& userAgent )
165   {
166     mUserAgent = userAgent;
167   }
168
169   bool IsJavaScriptEnabled() const
170   {
171     return mEnableJavaScript;
172   }
173
174   void EnableJavaScript( bool enabled )
175   {
176     mEnableJavaScript = enabled;
177   }
178
179   bool AreImagesAutomaticallyLoaded() const
180   {
181     return mLoadImagesAutomatically;
182   }
183
184   void LoadImagesAutomatically( bool automatic )
185   {
186     mLoadImagesAutomatically = automatic;
187   }
188
189   const std::string& GetDefaultTextEncodingName() const
190   {
191     return mDefaultTextEncodingName;
192   }
193
194   void SetDefaultTextEncodingName( const std::string& defaultTextEncodingName )
195   {
196     mDefaultTextEncodingName = defaultTextEncodingName;
197   }
198
199   int GetDefaultFontSize() const
200   {
201     return mDefaultFontSize;
202   }
203
204   void SetDefaultFontSize( int defaultFontSize )
205   {
206     mDefaultFontSize = defaultFontSize;
207   }
208
209   Dali::WebEnginePlugin::WebEnginePageLoadSignalType& PageLoadStartedSignal()
210   {
211     return mPageLoadStartedSignal;
212   }
213
214   Dali::WebEnginePlugin::WebEnginePageLoadSignalType& PageLoadFinishedSignal()
215   {
216     return mPageLoadFinishedSignal;
217   }
218
219   Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& PageLoadErrorSignal()
220   {
221     return mPageLoadErrorSignal;
222   }
223
224   std::string                                                mUrl;
225   std::vector< std::string >                                 mHistory;
226   size_t                                                     mCurrentPlusOnePos;
227   Dali::WebEnginePlugin::CacheModel                          mCacheModel;
228   Dali::WebEnginePlugin::CookieAcceptPolicy                  mCookieAcceptPolicy;
229   std::string                                                mUserAgent;
230   bool                                                       mEnableJavaScript;
231   bool                                                       mLoadImagesAutomatically;
232   std::string                                                mDefaultTextEncodingName;
233   int                                                        mDefaultFontSize;
234   Dali::WebEnginePlugin::WebEnginePageLoadSignalType         mPageLoadStartedSignal;
235   Dali::WebEnginePlugin::WebEnginePageLoadSignalType         mPageLoadFinishedSignal;
236   Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType    mPageLoadErrorSignal;
237   std::vector< std::function< void( const std::string& ) > > mResultCallbacks;
238   bool                                                       mEvaluating;
239 };
240
241 inline WebEngine& GetImplementation( Dali::WebEngine& webEngine )
242 {
243   DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
244   BaseObject& handle = webEngine.GetBaseObject();
245   return static_cast< Internal::Adaptor::WebEngine& >( handle );
246 }
247
248 inline const WebEngine& GetImplementation( const Dali::WebEngine& webEngine )
249 {
250   DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
251   const BaseObject& handle = webEngine.GetBaseObject();
252   return static_cast< const Internal::Adaptor::WebEngine& >( handle );
253 }
254
255 namespace
256 {
257
258 bool OnGoBack()
259 {
260   DisconnectFromGlobalSignal( &OnGoBack );
261
262   if( gInstance && gInstance->CanGoBack() )
263   {
264     gInstance->mCurrentPlusOnePos--;
265   }
266   return false;
267 }
268
269 bool OnGoForward()
270 {
271   DisconnectFromGlobalSignal( &OnGoForward );
272
273   if( gInstance && gInstance->CanGoForward() )
274   {
275     gInstance->mCurrentPlusOnePos++;
276   }
277   return false;
278 }
279
280 bool OnLoadUrl()
281 {
282   DisconnectFromGlobalSignal( &OnLoadUrl );
283
284   if( gInstance )
285   {
286     if( gInstance->mHistory.size() > gInstance->mCurrentPlusOnePos )
287     {
288       gInstance->mHistory.erase( gInstance->mHistory.begin() + gInstance->mCurrentPlusOnePos, gInstance->mHistory.end() );
289     }
290     gInstance->mHistory.push_back( gInstance->mUrl );
291     gInstance->mCurrentPlusOnePos++;
292     gInstance->mPageLoadStartedSignal.Emit( gInstance->mUrl );
293     gInstance->mPageLoadFinishedSignal.Emit( gInstance->mUrl );
294   }
295   return false;
296 }
297
298 bool OnEvaluteJavaScript()
299 {
300   DisconnectFromGlobalSignal( &OnEvaluteJavaScript );
301
302   if( gInstance )
303   {
304     for( auto& func : gInstance->mResultCallbacks )
305     {
306       func("undefined");
307     }
308     gInstance->mResultCallbacks.clear();
309   }
310   return false;
311 }
312
313 bool OnClearHistory()
314 {
315   DisconnectFromGlobalSignal( &OnClearHistory );
316
317   if( gInstance && gInstance->mCurrentPlusOnePos ) {
318     std::string url = gInstance->mHistory[ gInstance->mCurrentPlusOnePos - 1 ];
319     std::vector< std::string >().swap( gInstance->mHistory );
320     gInstance->mHistory.push_back( url );
321     gInstance->mCurrentPlusOnePos = 1;
322   }
323   return false;
324 }
325 } // namespace
326
327 } // namespace Adaptor
328
329 } // namespace Internal
330
331
332 // Dali::WebEngine Implementation
333 WebEngine::WebEngine()
334 {
335 }
336
337 WebEngine::WebEngine( Internal::Adaptor::WebEngine* internal )
338 : BaseHandle( internal )
339 {
340 }
341
342 WebEngine::~WebEngine()
343 {
344 }
345
346 WebEngine WebEngine::New()
347 {
348   Internal::Adaptor::WebEngine* baseObject = new Internal::Adaptor::WebEngine();
349
350   return WebEngine( baseObject );
351 }
352
353 WebEngine::WebEngine( const WebEngine& WebEngine )
354 : BaseHandle( WebEngine )
355 {
356 }
357
358 WebEngine& WebEngine::operator=( const WebEngine& webEngine )
359 {
360   BaseHandle::operator=( webEngine );
361   return *this;
362 }
363
364 WebEngine WebEngine::DownCast( BaseHandle handle )
365 {
366   return WebEngine( dynamic_cast< Internal::Adaptor::WebEngine* >( handle.GetObjectPtr() ) );
367 }
368
369 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
370 {
371 }
372
373 void WebEngine::Destroy()
374 {
375 }
376
377 void WebEngine::LoadUrl( const std::string& url )
378 {
379   return Internal::Adaptor::GetImplementation( *this ).LoadUrl( url );
380 }
381
382 const std::string& WebEngine::GetUrl()
383 {
384   return Internal::Adaptor::GetImplementation( *this ).GetUrl();
385 }
386
387 NativeImageInterfacePtr WebEngine::GetNativeImageSource()
388 {
389   Any source;
390   Dali::NativeImageSourcePtr sourcePtr = Dali::NativeImageSource::New( source );
391   return sourcePtr;
392 }
393
394 void WebEngine::LoadHTMLString( const std::string& htmlString )
395 {
396 }
397
398 void WebEngine::Reload()
399 {
400 }
401
402 void WebEngine::StopLoading()
403 {
404 }
405
406 void WebEngine::Suspend()
407 {
408 }
409
410 void WebEngine::Resume()
411 {
412 }
413
414 bool WebEngine::CanGoForward()
415 {
416   return Internal::Adaptor::GetImplementation( *this ).CanGoForward();
417 }
418
419 void WebEngine::GoForward()
420 {
421   Internal::Adaptor::GetImplementation( *this ).GoForward();
422 }
423
424 bool WebEngine::CanGoBack()
425 {
426   return Internal::Adaptor::GetImplementation( *this ).CanGoBack();
427 }
428
429 void WebEngine::GoBack()
430 {
431   Internal::Adaptor::GetImplementation( *this ).GoBack();
432 }
433
434 void WebEngine::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
435 {
436   Internal::Adaptor::GetImplementation( *this ).EvaluateJavaScript( script, resultHandler );
437 }
438
439 void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler )
440 {
441 }
442
443 void WebEngine::ClearHistory()
444 {
445   Internal::Adaptor::GetImplementation( *this ).ClearHistory();
446 }
447
448 void WebEngine::ClearCache()
449 {
450 }
451
452 void WebEngine::ClearCookies()
453 {
454 }
455
456 Dali::WebEnginePlugin::CacheModel WebEngine::GetCacheModel() const
457 {
458   return Internal::Adaptor::GetImplementation( *this ).GetCacheModel();
459 }
460
461 void WebEngine::SetCacheModel( Dali::WebEnginePlugin::CacheModel cacheModel )
462 {
463   Internal::Adaptor::GetImplementation( *this ).SetCacheModel( cacheModel );
464 }
465
466 Dali::WebEnginePlugin::CookieAcceptPolicy WebEngine::GetCookieAcceptPolicy() const
467 {
468   return Internal::Adaptor::GetImplementation( *this ).GetCookieAcceptPolicy();
469 }
470
471 void WebEngine::SetCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy policy )
472 {
473   Internal::Adaptor::GetImplementation( *this ).SetCookieAcceptPolicy( policy );
474 }
475
476 const std::string& WebEngine::GetUserAgent() const
477 {
478   return Internal::Adaptor::GetImplementation( *this ).GetUserAgent();
479 }
480
481 void WebEngine::SetUserAgent( const std::string& userAgent )
482 {
483   Internal::Adaptor::GetImplementation( *this ).SetUserAgent( userAgent );
484 }
485
486 bool WebEngine::IsJavaScriptEnabled() const
487 {
488   return Internal::Adaptor::GetImplementation( *this ).IsJavaScriptEnabled();
489 }
490
491 void WebEngine::EnableJavaScript( bool enabled )
492 {
493   Internal::Adaptor::GetImplementation( *this ).EnableJavaScript( enabled );
494 }
495
496 bool WebEngine::AreImagesAutomaticallyLoaded() const
497 {
498   return Internal::Adaptor::GetImplementation( *this ).AreImagesAutomaticallyLoaded();
499 }
500
501 void WebEngine::LoadImagesAutomatically( bool automatic )
502 {
503   Internal::Adaptor::GetImplementation( *this ).LoadImagesAutomatically( automatic );
504 }
505
506 const std::string& WebEngine::GetDefaultTextEncodingName() const
507 {
508   return Internal::Adaptor::GetImplementation( *this ).GetDefaultTextEncodingName();
509 }
510
511 void WebEngine::SetDefaultTextEncodingName( const std::string& defaultTextEncodingName )
512 {
513   Internal::Adaptor::GetImplementation( *this ).SetDefaultTextEncodingName( defaultTextEncodingName );
514 }
515
516 int WebEngine::GetDefaultFontSize() const
517 {
518   return Internal::Adaptor::GetImplementation( *this ).GetDefaultFontSize();
519 }
520
521 void WebEngine::SetDefaultFontSize( int defaultFontSize )
522 {
523   Internal::Adaptor::GetImplementation( *this ).SetDefaultFontSize( defaultFontSize );
524 }
525
526 void WebEngine::SetSize( int width, int height )
527 {
528 }
529
530 bool WebEngine::SendTouchEvent( const TouchData& touch )
531 {
532   return true;
533 }
534
535 bool WebEngine::SendKeyEvent( const KeyEvent& event )
536 {
537   return true;
538 }
539
540 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
541 {
542   return Internal::Adaptor::GetImplementation( *this ).PageLoadStartedSignal();
543 }
544
545 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
546 {
547   return Internal::Adaptor::GetImplementation( *this ).PageLoadFinishedSignal();
548 }
549
550 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
551 {
552   return Internal::Adaptor::GetImplementation( *this ).PageLoadErrorSignal();
553 }
554
555 } // namespace Dali;
556