Merge "Ensuring test files match dali-core/adaptor" 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) 2020 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 <toolkit-application.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31
32 namespace Adaptor
33 {
34
35 class WebEngine;
36
37 namespace
38 {
39 static WebEngine* gInstance = NULL;
40 static int gInstanceCount = 0;
41
42 bool OnGoBack();
43 bool OnGoForward();
44 bool OnLoadUrl();
45 bool OnEvaluteJavaScript();
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     , mCacheModel( Dali::WebEnginePlugin::CacheModel::DOCUMENT_VIEWER )
69     , mCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy::NO_THIRD_PARTY )
70     , mUserAgent()
71     , mEnableJavaScript( true )
72     , mLoadImagesAutomatically( true )
73     , mDefaultTextEncodingName()
74     , mDefaultFontSize( 16 )
75     , mEvaluating( false )
76     , mScrollPosition( 0, 0 )
77     , mScrollSize( 500, 500 )
78     , mContentSize( 500, 500 )
79   {
80     gInstanceCount++;
81     gInstance = this;
82   }
83
84   virtual ~WebEngine()
85   {
86     gInstanceCount--;
87     if( !gInstanceCount )
88     {
89       gInstance = NULL;
90     }
91   }
92
93   void LoadUrl( const std::string& url )
94   {
95     mUrl = url;
96     ConnectToGlobalSignal( &OnLoadUrl );
97   }
98
99   const std::string& GetUrl() const
100   {
101     return mUrl;
102   }
103
104   bool CanGoForward() const
105   {
106     return mHistory.size() > mCurrentPlusOnePos;
107   }
108
109   void GoForward()
110   {
111     ConnectToGlobalSignal( &OnGoForward );
112   }
113
114   bool CanGoBack() const
115   {
116     return mCurrentPlusOnePos > 1;
117   }
118
119   void GoBack()
120   {
121     ConnectToGlobalSignal( &OnGoBack );
122   }
123
124   void EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
125   {
126     if( resultHandler )
127     {
128       if( !mEvaluating )
129       {
130         ConnectToGlobalSignal( &OnEvaluteJavaScript );
131       }
132       mResultCallbacks.push_back( resultHandler );
133     }
134   }
135
136   void ClearHistory()
137   {
138     ConnectToGlobalSignal( &OnClearHistory );
139   }
140
141   Dali::WebEnginePlugin::CacheModel GetCacheModel() const
142   {
143     return mCacheModel;
144   }
145
146   void SetCacheModel( Dali::WebEnginePlugin::CacheModel cacheModel )
147   {
148     mCacheModel = cacheModel;
149   }
150
151   Dali::WebEnginePlugin::CookieAcceptPolicy GetCookieAcceptPolicy() const
152   {
153     return mCookieAcceptPolicy;
154   }
155
156   void SetCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy policy )
157   {
158     mCookieAcceptPolicy = policy;
159   }
160
161   const std::string& GetUserAgent() const
162   {
163     return mUserAgent;
164   }
165
166   void SetUserAgent( const std::string& userAgent )
167   {
168     mUserAgent = userAgent;
169   }
170
171   bool IsJavaScriptEnabled() const
172   {
173     return mEnableJavaScript;
174   }
175
176   void EnableJavaScript( bool enabled )
177   {
178     mEnableJavaScript = enabled;
179   }
180
181   bool AreImagesAutomaticallyLoaded() const
182   {
183     return mLoadImagesAutomatically;
184   }
185
186   void LoadImagesAutomatically( bool automatic )
187   {
188     mLoadImagesAutomatically = automatic;
189   }
190
191   const std::string& GetDefaultTextEncodingName() const
192   {
193     return mDefaultTextEncodingName;
194   }
195
196   void SetDefaultTextEncodingName( const std::string& defaultTextEncodingName )
197   {
198     mDefaultTextEncodingName = defaultTextEncodingName;
199   }
200
201   int GetDefaultFontSize() const
202   {
203     return mDefaultFontSize;
204   }
205
206   void SetDefaultFontSize( int defaultFontSize )
207   {
208     mDefaultFontSize = defaultFontSize;
209   }
210
211   void ScrollBy( int dx, int dy )
212   {
213     mScrollPosition += Dali::Vector2( dx, dy );
214     if ( mScrollPosition.y + mScrollSize.height > mContentSize.height )
215     {
216       gInstance->mScrollEdgeReachedSignal.Emit( Dali::WebEnginePlugin::ScrollEdge::BOTTOM );
217     }
218   }
219
220   void SetScrollPosition( int x, int y )
221   {
222     mScrollPosition.x = x;
223     mScrollPosition.y = y;
224   }
225
226   void GetScrollPosition( int& x, int& y ) const
227   {
228     x = mScrollPosition.x;
229     y = mScrollPosition.y;
230   }
231
232   void GetScrollSize( int& w, int& h ) const
233   {
234     w = mScrollSize.width;
235     h = mScrollSize.height;
236   }
237
238   void GetContentSize( int& w, int& h ) const
239   {
240     w = mContentSize.width;
241     h = mContentSize.height;
242   }
243
244   Dali::WebEnginePlugin::WebEnginePageLoadSignalType& PageLoadStartedSignal()
245   {
246     return mPageLoadStartedSignal;
247   }
248
249   Dali::WebEnginePlugin::WebEnginePageLoadSignalType& PageLoadFinishedSignal()
250   {
251     return mPageLoadFinishedSignal;
252   }
253
254   Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& PageLoadErrorSignal()
255   {
256     return mPageLoadErrorSignal;
257   }
258
259   Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& ScrollEdgeReachedSignal()
260   {
261     return mScrollEdgeReachedSignal;
262   }
263
264   std::string                                                mUrl;
265   std::vector< std::string >                                 mHistory;
266   size_t                                                     mCurrentPlusOnePos;
267   Dali::WebEnginePlugin::CacheModel                          mCacheModel;
268   Dali::WebEnginePlugin::CookieAcceptPolicy                  mCookieAcceptPolicy;
269   std::string                                                mUserAgent;
270   bool                                                       mEnableJavaScript;
271   bool                                                       mLoadImagesAutomatically;
272   std::string                                                mDefaultTextEncodingName;
273   int                                                        mDefaultFontSize;
274   Dali::WebEnginePlugin::WebEnginePageLoadSignalType         mPageLoadStartedSignal;
275   Dali::WebEnginePlugin::WebEnginePageLoadSignalType         mPageLoadFinishedSignal;
276   Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType    mPageLoadErrorSignal;
277   std::vector< std::function< void( const std::string& ) > > mResultCallbacks;
278   bool                                                       mEvaluating;
279
280   Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType mScrollEdgeReachedSignal;
281   Dali::Vector2                                               mScrollPosition;
282   Dali::Vector2                                               mScrollSize;
283   Dali::Vector2                                               mContentSize;
284 };
285
286 inline WebEngine& GetImplementation( Dali::WebEngine& webEngine )
287 {
288   DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
289   BaseObject& handle = webEngine.GetBaseObject();
290   return static_cast< Internal::Adaptor::WebEngine& >( handle );
291 }
292
293 inline const WebEngine& GetImplementation( const Dali::WebEngine& webEngine )
294 {
295   DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
296   const BaseObject& handle = webEngine.GetBaseObject();
297   return static_cast< const Internal::Adaptor::WebEngine& >( handle );
298 }
299
300 namespace
301 {
302
303 bool OnGoBack()
304 {
305   DisconnectFromGlobalSignal( &OnGoBack );
306
307   if( gInstance && gInstance->CanGoBack() )
308   {
309     gInstance->mCurrentPlusOnePos--;
310   }
311   return false;
312 }
313
314 bool OnGoForward()
315 {
316   DisconnectFromGlobalSignal( &OnGoForward );
317
318   if( gInstance && gInstance->CanGoForward() )
319   {
320     gInstance->mCurrentPlusOnePos++;
321   }
322   return false;
323 }
324
325 bool OnLoadUrl()
326 {
327   DisconnectFromGlobalSignal( &OnLoadUrl );
328
329   if( gInstance )
330   {
331     if( gInstance->mHistory.size() > gInstance->mCurrentPlusOnePos )
332     {
333       gInstance->mHistory.erase( gInstance->mHistory.begin() + gInstance->mCurrentPlusOnePos, gInstance->mHistory.end() );
334     }
335     gInstance->mHistory.push_back( gInstance->mUrl );
336     gInstance->mCurrentPlusOnePos++;
337     gInstance->mPageLoadStartedSignal.Emit( gInstance->mUrl );
338     gInstance->mPageLoadFinishedSignal.Emit( gInstance->mUrl );
339   }
340   return false;
341 }
342
343 bool OnEvaluteJavaScript()
344 {
345   DisconnectFromGlobalSignal( &OnEvaluteJavaScript );
346
347   if( gInstance )
348   {
349     for( auto& func : gInstance->mResultCallbacks )
350     {
351       func("undefined");
352     }
353     gInstance->mResultCallbacks.clear();
354   }
355   return false;
356 }
357
358 bool OnClearHistory()
359 {
360   DisconnectFromGlobalSignal( &OnClearHistory );
361
362   if( gInstance && gInstance->mCurrentPlusOnePos ) {
363     std::string url = gInstance->mHistory[ gInstance->mCurrentPlusOnePos - 1 ];
364     std::vector< std::string >().swap( gInstance->mHistory );
365     gInstance->mHistory.push_back( url );
366     gInstance->mCurrentPlusOnePos = 1;
367   }
368   return false;
369 }
370 } // namespace
371
372 } // namespace Adaptor
373
374 } // namespace Internal
375
376
377 // Dali::WebEngine Implementation
378 WebEngine::WebEngine()
379 {
380 }
381
382 WebEngine::WebEngine( Internal::Adaptor::WebEngine* internal )
383 : BaseHandle( internal )
384 {
385 }
386
387 WebEngine::~WebEngine()
388 {
389 }
390
391 WebEngine WebEngine::New()
392 {
393   Internal::Adaptor::WebEngine* baseObject = new Internal::Adaptor::WebEngine();
394
395   return WebEngine( baseObject );
396 }
397
398 WebEngine::WebEngine( const WebEngine& WebEngine )
399 : BaseHandle( WebEngine )
400 {
401 }
402
403 WebEngine& WebEngine::operator=( const WebEngine& webEngine )
404 {
405   BaseHandle::operator=( webEngine );
406   return *this;
407 }
408
409 WebEngine WebEngine::DownCast( BaseHandle handle )
410 {
411   return WebEngine( dynamic_cast< Internal::Adaptor::WebEngine* >( handle.GetObjectPtr() ) );
412 }
413
414 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
415 {
416 }
417
418 void WebEngine::Destroy()
419 {
420 }
421
422 void WebEngine::LoadUrl( const std::string& url )
423 {
424   return Internal::Adaptor::GetImplementation( *this ).LoadUrl( url );
425 }
426
427 const std::string& WebEngine::GetUrl()
428 {
429   return Internal::Adaptor::GetImplementation( *this ).GetUrl();
430 }
431
432 NativeImageInterfacePtr WebEngine::GetNativeImageSource()
433 {
434   Any source;
435   Dali::NativeImageSourcePtr sourcePtr = Dali::NativeImageSource::New( source );
436   return sourcePtr;
437 }
438
439 void WebEngine::LoadHTMLString( const std::string& htmlString )
440 {
441 }
442
443 void WebEngine::Reload()
444 {
445 }
446
447 void WebEngine::StopLoading()
448 {
449 }
450
451 void WebEngine::Suspend()
452 {
453 }
454
455 void WebEngine::Resume()
456 {
457 }
458
459 bool WebEngine::CanGoForward()
460 {
461   return Internal::Adaptor::GetImplementation( *this ).CanGoForward();
462 }
463
464 void WebEngine::GoForward()
465 {
466   Internal::Adaptor::GetImplementation( *this ).GoForward();
467 }
468
469 bool WebEngine::CanGoBack()
470 {
471   return Internal::Adaptor::GetImplementation( *this ).CanGoBack();
472 }
473
474 void WebEngine::GoBack()
475 {
476   Internal::Adaptor::GetImplementation( *this ).GoBack();
477 }
478
479 void WebEngine::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
480 {
481   Internal::Adaptor::GetImplementation( *this ).EvaluateJavaScript( script, resultHandler );
482 }
483
484 void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler )
485 {
486 }
487
488 void WebEngine::ClearHistory()
489 {
490   Internal::Adaptor::GetImplementation( *this ).ClearHistory();
491 }
492
493 void WebEngine::ClearCache()
494 {
495 }
496
497 void WebEngine::ClearCookies()
498 {
499 }
500
501 Dali::WebEnginePlugin::CacheModel WebEngine::GetCacheModel() const
502 {
503   return Internal::Adaptor::GetImplementation( *this ).GetCacheModel();
504 }
505
506 void WebEngine::SetCacheModel( Dali::WebEnginePlugin::CacheModel cacheModel )
507 {
508   Internal::Adaptor::GetImplementation( *this ).SetCacheModel( cacheModel );
509 }
510
511 Dali::WebEnginePlugin::CookieAcceptPolicy WebEngine::GetCookieAcceptPolicy() const
512 {
513   return Internal::Adaptor::GetImplementation( *this ).GetCookieAcceptPolicy();
514 }
515
516 void WebEngine::SetCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy policy )
517 {
518   Internal::Adaptor::GetImplementation( *this ).SetCookieAcceptPolicy( policy );
519 }
520
521 const std::string& WebEngine::GetUserAgent() const
522 {
523   return Internal::Adaptor::GetImplementation( *this ).GetUserAgent();
524 }
525
526 void WebEngine::SetUserAgent( const std::string& userAgent )
527 {
528   Internal::Adaptor::GetImplementation( *this ).SetUserAgent( userAgent );
529 }
530
531 bool WebEngine::IsJavaScriptEnabled() const
532 {
533   return Internal::Adaptor::GetImplementation( *this ).IsJavaScriptEnabled();
534 }
535
536 void WebEngine::EnableJavaScript( bool enabled )
537 {
538   Internal::Adaptor::GetImplementation( *this ).EnableJavaScript( enabled );
539 }
540
541 bool WebEngine::AreImagesAutomaticallyLoaded() const
542 {
543   return Internal::Adaptor::GetImplementation( *this ).AreImagesAutomaticallyLoaded();
544 }
545
546 void WebEngine::LoadImagesAutomatically( bool automatic )
547 {
548   Internal::Adaptor::GetImplementation( *this ).LoadImagesAutomatically( automatic );
549 }
550
551 const std::string& WebEngine::GetDefaultTextEncodingName() const
552 {
553   return Internal::Adaptor::GetImplementation( *this ).GetDefaultTextEncodingName();
554 }
555
556 void WebEngine::SetDefaultTextEncodingName( const std::string& defaultTextEncodingName )
557 {
558   Internal::Adaptor::GetImplementation( *this ).SetDefaultTextEncodingName( defaultTextEncodingName );
559 }
560
561 int WebEngine::GetDefaultFontSize() const
562 {
563   return Internal::Adaptor::GetImplementation( *this ).GetDefaultFontSize();
564 }
565
566 void WebEngine::SetDefaultFontSize( int defaultFontSize )
567 {
568   Internal::Adaptor::GetImplementation( *this ).SetDefaultFontSize( defaultFontSize );
569 }
570
571 void WebEngine::ScrollBy( int dx, int dy )
572 {
573   Internal::Adaptor::GetImplementation( *this ).ScrollBy( dx, dy );
574 }
575
576 void WebEngine::SetScrollPosition( int x, int y )
577 {
578   Internal::Adaptor::GetImplementation( *this ).SetScrollPosition( x, y );
579 }
580
581 void WebEngine::GetScrollPosition( int& x, int& y ) const
582 {
583   Internal::Adaptor::GetImplementation( *this ).GetScrollPosition( x, y );
584 }
585
586 void WebEngine::GetScrollSize( int& w, int& h ) const
587 {
588   Internal::Adaptor::GetImplementation( *this ).GetScrollSize( w, h );
589 }
590
591 void WebEngine::GetContentSize( int& w, int& h ) const
592 {
593   Internal::Adaptor::GetImplementation( *this ).GetContentSize( w, h );
594 }
595
596 void WebEngine::SetSize( int width, int height )
597 {
598 }
599
600 bool WebEngine::SendTouchEvent( const TouchEvent& touch )
601 {
602   return true;
603 }
604
605 bool WebEngine::SendKeyEvent( const KeyEvent& event )
606 {
607   return true;
608 }
609
610 void WebEngine::SetFocus( bool focused )
611 {
612 }
613
614 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
615 {
616   return Internal::Adaptor::GetImplementation( *this ).PageLoadStartedSignal();
617 }
618
619 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
620 {
621   return Internal::Adaptor::GetImplementation( *this ).PageLoadFinishedSignal();
622 }
623
624 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
625 {
626   return Internal::Adaptor::GetImplementation( *this ).PageLoadErrorSignal();
627 }
628
629 Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& WebEngine::ScrollEdgeReachedSignal()
630 {
631   return Internal::Adaptor::GetImplementation( *this ).ScrollEdgeReachedSignal();
632 }
633
634 } // namespace Dali;
635