Merge "Implement some new ewk apis in web view." 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/devel-api/adaptor-framework/web-engine-back-forward-list.h>
22 #include <dali/devel-api/adaptor-framework/web-engine-back-forward-list-item.h>
23 #include <dali/devel-api/adaptor-framework/web-engine-context.h>
24 #include <dali/devel-api/adaptor-framework/web-engine-cookie-manager.h>
25 #include <dali/devel-api/adaptor-framework/web-engine-settings.h>
26 #include <dali/public-api/object/any.h>
27 #include <dali/public-api/object/base-object.h>
28 #include <dali/public-api/adaptor-framework/native-image-source.h>
29 #include <toolkit-application.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Adaptor
38 {
39
40 class WebEngine;
41
42 namespace
43 {
44 static WebEngine* gInstance = NULL;
45 static int gInstanceCount = 0;
46
47 bool OnGoBack();
48 bool OnGoForward();
49 bool OnLoadUrl();
50 bool OnEvaluteJavaScript();
51 bool OnClearHistory();
52
53 static void ConnectToGlobalSignal( bool (*func)() )
54 {
55   Dali::Timer timer = Dali::Timer::New( 0 );
56   timer.TickSignal().Connect( func );
57 }
58
59 static void DisconnectFromGlobalSignal( bool (*func)() )
60 {
61   Dali::Timer timer = Dali::Timer::New( 0 );
62   timer.TickSignal().Disconnect( func );
63 }
64 }
65
66 class MockWebEngineContext : public Dali::WebEngineContext
67 {
68 public:
69   MockWebEngineContext()
70     : mockModel( Dali::WebEngineContext::CacheModel::DOCUMENT_VIEWER )
71   {
72   }
73
74   Dali::WebEngineContext::CacheModel GetCacheModel() const override
75   {
76     return mockModel;
77   }
78
79   void SetCacheModel( Dali::WebEngineContext::CacheModel cacheModel ) override
80   {
81     mockModel = cacheModel;
82   }
83
84   void SetProxyUri( const std::string& uri ) override
85   {
86   }
87
88   void SetDefaultProxyAuth( const std::string& username, const std::string& password ) override
89   {
90   }
91
92   void SetCertificateFilePath( const std::string& certificatePath ) override
93   {
94   }
95
96   void DeleteWebDatabase() override
97   {
98   }
99
100   void DeleteWebStorage() override
101   {
102   }
103
104   void DeleteLocalFileSystem() override
105   {
106   }
107
108   void DisableCache( bool cacheDisabled ) override
109   {
110   }
111
112   void ClearCache() override
113   {
114   }
115
116 private:
117   Dali::WebEngineContext::CacheModel mockModel;
118 };
119
120 class MockWebEngineCookieManager : public Dali::WebEngineCookieManager
121 {
122 public:
123   MockWebEngineCookieManager()
124     : mockCookieAcceptPolicy( Dali::WebEngineCookieManager::CookieAcceptPolicy::NO_THIRD_PARTY )
125   {
126   }
127
128   void SetCookieAcceptPolicy( Dali::WebEngineCookieManager::CookieAcceptPolicy policy ) override
129   {
130     mockCookieAcceptPolicy = policy;
131   }
132
133   Dali::WebEngineCookieManager::CookieAcceptPolicy GetCookieAcceptPolicy() const override
134   {
135     return mockCookieAcceptPolicy;
136   }
137
138   void ClearCookies() override
139   {
140   }
141
142   void SetPersistentStorage( const std::string& path, Dali::WebEngineCookieManager::CookiePersistentStorage storage ) override
143   {
144   }
145
146 private:
147   Dali::WebEngineCookieManager::CookieAcceptPolicy mockCookieAcceptPolicy;
148 };
149
150 class MockWebEngineBackForwardListItem : public Dali::WebEngineBackForwardListItem
151 {
152 public:
153   MockWebEngineBackForwardListItem()
154     : mockUrl( "http://url" ),
155       mockTitle( "title" ),
156       mockOriginalUrl( "http://originalurl" )
157   {
158   }
159
160   std::string GetUrl() const override
161   {
162     return mockUrl;
163   }
164
165   std::string GetTitle() const override
166   {
167     return mockTitle;
168   }
169
170   std::string GetOriginalUrl() const override
171   {
172     return mockOriginalUrl;
173   }
174
175 private:
176   std::string mockUrl;
177   std::string mockTitle;
178   std::string mockOriginalUrl;
179 };
180
181 class MockWebEngineBackForwardList : public Dali::WebEngineBackForwardList
182 {
183 public:
184   MockWebEngineBackForwardList( )
185     : mockItem(),
186       pMockItem( &mockItem )
187   {
188   }
189
190   Dali::WebEngineBackForwardListItem& GetCurrentItem() const override
191   {
192     return *pMockItem;
193   }
194
195   Dali::WebEngineBackForwardListItem& GetItemAtIndex( uint32_t index ) const override
196   {
197     return *pMockItem;
198   }
199
200   uint32_t GetItemCount() const override
201   {
202     return 1;
203   }
204
205 private:
206   MockWebEngineBackForwardListItem mockItem;
207   WebEngineBackForwardListItem* pMockItem;
208 };
209
210 class MockWebEngineSettings : public WebEngineSettings
211 {
212 public:
213   MockWebEngineSettings()
214     : mockDefaultFontSize( 16 ),
215       mockJavaScriptEnabled( true ),
216       mockImageLoadedAutomatically( true ),
217       mockDefaultTextEncodingName()
218   {
219   }
220
221   uint32_t GetDefaultFontSize() const override
222   {
223     return mockDefaultFontSize;
224   }
225
226   void SetDefaultFontSize( uint32_t size ) override
227   {
228     mockDefaultFontSize = size;
229   }
230
231   bool IsJavaScriptEnabled() const override
232   {
233     return mockJavaScriptEnabled;
234   }
235
236   void EnableJavaScript( bool enabled ) override
237   {
238     mockJavaScriptEnabled = enabled;
239   }
240
241   bool AreImagesLoadedAutomatically() const override
242   {
243     return mockImageLoadedAutomatically;
244   }
245
246   void AllowImagesLoadAutomatically( bool automatic ) override
247   {
248     mockImageLoadedAutomatically = automatic;
249   }
250
251   std::string GetDefaultTextEncodingName() const override
252   {
253     return mockDefaultTextEncodingName;
254   }
255
256   void SetDefaultTextEncodingName( const std::string& defaultTextEncodingName ) override
257   {
258     mockDefaultTextEncodingName = defaultTextEncodingName;
259   }
260
261   void AllowMixedContents( bool allowed ) override
262   {
263   }
264
265   void EnableSpatialNavigation( bool enabled ) override
266   {
267   }
268
269   void EnableWebSecurity( bool enabled ) override
270   {
271   }
272
273   void AllowFileAccessFromExternalUrl( bool allowed ) override
274   {
275   }
276
277   void AllowScriptsOpenWindows( bool allowed ) override
278   {
279   }
280
281 private:
282   int mockDefaultFontSize;
283   bool mockJavaScriptEnabled;
284   bool mockImageLoadedAutomatically;
285   std::string mockDefaultTextEncodingName;
286 };
287
288 class WebEngine: public Dali::BaseObject
289 {
290 public:
291
292   WebEngine()
293     : mUrl()
294     , mCurrentPlusOnePos( 0 )
295     , mUserAgent()
296     , mEvaluating( false )
297     , mScrollPosition( 0, 0 )
298     , mScrollSize( 500, 500 )
299     , mContentSize( 500, 500 )
300   {
301     gInstanceCount++;
302     gInstance = this;
303
304     mockWebEngineSettings = new MockWebEngineSettings();
305     mockWebEngineContext = new MockWebEngineContext();
306     mockWebEngineCookieManager = new MockWebEngineCookieManager();
307     mockWebEngineBackForwardList = new MockWebEngineBackForwardList();
308   }
309
310   virtual ~WebEngine()
311   {
312     gInstanceCount--;
313     if( !gInstanceCount )
314     {
315       gInstance = NULL;
316     }
317
318     delete mockWebEngineSettings;
319     delete mockWebEngineContext;
320     delete mockWebEngineCookieManager;
321     delete mockWebEngineBackForwardList;
322   }
323
324   Dali::WebEngineSettings& GetSettings() const
325   {
326     return *mockWebEngineSettings;
327   }
328
329   Dali::WebEngineContext& GetContext() const
330   {
331     return *mockWebEngineContext;
332   }
333
334   Dali::WebEngineCookieManager& GetCookieManager() const
335   {
336     return *mockWebEngineCookieManager;
337   }
338
339   Dali::WebEngineBackForwardList& GetBackForwardList() const
340   {
341     return *mockWebEngineBackForwardList;
342   }
343
344   void LoadUrl( const std::string& url )
345   {
346     mUrl = url;
347     ConnectToGlobalSignal( &OnLoadUrl );
348   }
349
350   const std::string& GetUrl() const
351   {
352     return mUrl;
353   }
354
355   bool CanGoForward() const
356   {
357     return mHistory.size() > mCurrentPlusOnePos;
358   }
359
360   void GoForward()
361   {
362     ConnectToGlobalSignal( &OnGoForward );
363   }
364
365   bool CanGoBack() const
366   {
367     return mCurrentPlusOnePos > 1;
368   }
369
370   void GoBack()
371   {
372     ConnectToGlobalSignal( &OnGoBack );
373   }
374
375   void EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
376   {
377     if( resultHandler )
378     {
379       if( !mEvaluating )
380       {
381         ConnectToGlobalSignal( &OnEvaluteJavaScript );
382       }
383       mResultCallbacks.push_back( resultHandler );
384     }
385   }
386
387   void ClearHistory()
388   {
389     ConnectToGlobalSignal( &OnClearHistory );
390   }
391
392   const std::string& GetUserAgent() const
393   {
394     return mUserAgent;
395   }
396
397   void SetUserAgent( const std::string& userAgent )
398   {
399     mUserAgent = userAgent;
400   }
401
402   void ScrollBy( int dx, int dy )
403   {
404     mScrollPosition += Dali::Vector2( dx, dy );
405     if ( mScrollPosition.y + mScrollSize.height > mContentSize.height )
406     {
407       gInstance->mScrollEdgeReachedSignal.Emit( Dali::WebEnginePlugin::ScrollEdge::BOTTOM );
408     }
409   }
410
411   void SetScrollPosition( int x, int y )
412   {
413     mScrollPosition.x = x;
414     mScrollPosition.y = y;
415   }
416
417   void GetScrollPosition( int& x, int& y ) const
418   {
419     x = mScrollPosition.x;
420     y = mScrollPosition.y;
421   }
422
423   void GetScrollSize( int& w, int& h ) const
424   {
425     w = mScrollSize.width;
426     h = mScrollSize.height;
427   }
428
429   void GetContentSize( int& w, int& h ) const
430   {
431     w = mContentSize.width;
432     h = mContentSize.height;
433   }
434
435   Dali::WebEnginePlugin::WebEnginePageLoadSignalType& PageLoadStartedSignal()
436   {
437     return mPageLoadStartedSignal;
438   }
439
440   Dali::WebEnginePlugin::WebEnginePageLoadSignalType& PageLoadFinishedSignal()
441   {
442     return mPageLoadFinishedSignal;
443   }
444
445   Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& PageLoadErrorSignal()
446   {
447     return mPageLoadErrorSignal;
448   }
449
450   Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& ScrollEdgeReachedSignal()
451   {
452     return mScrollEdgeReachedSignal;
453   }
454
455   std::string                                                mUrl;
456   std::vector< std::string >                                 mHistory;
457   size_t                                                     mCurrentPlusOnePos;
458   std::string                                                mUserAgent;
459   Dali::WebEnginePlugin::WebEnginePageLoadSignalType         mPageLoadStartedSignal;
460   Dali::WebEnginePlugin::WebEnginePageLoadSignalType         mPageLoadFinishedSignal;
461   Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType    mPageLoadErrorSignal;
462   std::vector< std::function< void( const std::string& ) > > mResultCallbacks;
463   bool                                                       mEvaluating;
464
465   Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType mScrollEdgeReachedSignal;
466   Dali::Vector2                                               mScrollPosition;
467   Dali::Vector2                                               mScrollSize;
468   Dali::Vector2                                               mContentSize;
469   WebEngineBackForwardList*                                   mockWebEngineBackForwardList;
470   WebEngineContext*                                           mockWebEngineContext;
471   WebEngineCookieManager*                                     mockWebEngineCookieManager;
472   WebEngineSettings*                                          mockWebEngineSettings;
473 };
474
475 inline WebEngine& GetImplementation( Dali::WebEngine& webEngine )
476 {
477   DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
478   BaseObject& handle = webEngine.GetBaseObject();
479   return static_cast< Internal::Adaptor::WebEngine& >( handle );
480 }
481
482 inline const WebEngine& GetImplementation( const Dali::WebEngine& webEngine )
483 {
484   DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
485   const BaseObject& handle = webEngine.GetBaseObject();
486   return static_cast< const Internal::Adaptor::WebEngine& >( handle );
487 }
488
489 namespace
490 {
491
492 bool OnGoBack()
493 {
494   DisconnectFromGlobalSignal( &OnGoBack );
495
496   if( gInstance && gInstance->CanGoBack() )
497   {
498     gInstance->mCurrentPlusOnePos--;
499   }
500   return false;
501 }
502
503 bool OnGoForward()
504 {
505   DisconnectFromGlobalSignal( &OnGoForward );
506
507   if( gInstance && gInstance->CanGoForward() )
508   {
509     gInstance->mCurrentPlusOnePos++;
510   }
511   return false;
512 }
513
514 bool OnLoadUrl()
515 {
516   DisconnectFromGlobalSignal( &OnLoadUrl );
517
518   if( gInstance )
519   {
520     if( gInstance->mHistory.size() > gInstance->mCurrentPlusOnePos )
521     {
522       gInstance->mHistory.erase( gInstance->mHistory.begin() + gInstance->mCurrentPlusOnePos, gInstance->mHistory.end() );
523     }
524     gInstance->mHistory.push_back( gInstance->mUrl );
525     gInstance->mCurrentPlusOnePos++;
526     gInstance->mPageLoadStartedSignal.Emit( gInstance->mUrl );
527     gInstance->mPageLoadFinishedSignal.Emit( gInstance->mUrl );
528   }
529   return false;
530 }
531
532 bool OnEvaluteJavaScript()
533 {
534   DisconnectFromGlobalSignal( &OnEvaluteJavaScript );
535
536   if( gInstance )
537   {
538     for( auto& func : gInstance->mResultCallbacks )
539     {
540       func("undefined");
541     }
542     gInstance->mResultCallbacks.clear();
543   }
544   return false;
545 }
546
547 bool OnClearHistory()
548 {
549   DisconnectFromGlobalSignal( &OnClearHistory );
550
551   if( gInstance && gInstance->mCurrentPlusOnePos ) {
552     std::string url = gInstance->mHistory[ gInstance->mCurrentPlusOnePos - 1 ];
553     std::vector< std::string >().swap( gInstance->mHistory );
554     gInstance->mHistory.push_back( url );
555     gInstance->mCurrentPlusOnePos = 1;
556   }
557   return false;
558 }
559 } // namespace
560
561 } // namespace Adaptor
562
563 } // namespace Internal
564
565
566 // Dali::WebEngine Implementation
567 WebEngine::WebEngine()
568 {
569 }
570
571 WebEngine::WebEngine( Internal::Adaptor::WebEngine* internal )
572 : BaseHandle( internal )
573 {
574 }
575
576 WebEngine::~WebEngine()
577 {
578 }
579
580 WebEngine WebEngine::New()
581 {
582   Internal::Adaptor::WebEngine* baseObject = new Internal::Adaptor::WebEngine();
583
584   return WebEngine( baseObject );
585 }
586
587 WebEngine::WebEngine( const WebEngine& WebEngine )
588 : BaseHandle( WebEngine )
589 {
590 }
591
592 WebEngine& WebEngine::operator=( const WebEngine& webEngine )
593 {
594   BaseHandle::operator=( webEngine );
595   return *this;
596 }
597
598 WebEngine WebEngine::DownCast( BaseHandle handle )
599 {
600   return WebEngine( dynamic_cast< Internal::Adaptor::WebEngine* >( handle.GetObjectPtr() ) );
601 }
602
603 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
604 {
605 }
606
607 void WebEngine::Destroy()
608 {
609 }
610
611 WebEngineSettings& WebEngine::GetSettings() const
612 {
613   return Internal::Adaptor::GetImplementation( *this ).GetSettings();
614 }
615
616 WebEngineContext& WebEngine::GetContext() const
617 {
618   return Internal::Adaptor::GetImplementation( *this ).GetContext();
619 }
620
621 WebEngineCookieManager& WebEngine::GetCookieManager() const
622 {
623   return Internal::Adaptor::GetImplementation( *this ).GetCookieManager();
624 }
625
626 WebEngineBackForwardList& WebEngine::GetBackForwardList() const
627 {
628   return Internal::Adaptor::GetImplementation( *this ).GetBackForwardList();
629 }
630
631 void WebEngine::LoadUrl( const std::string& url )
632 {
633   return Internal::Adaptor::GetImplementation( *this ).LoadUrl( url );
634 }
635
636 const std::string& WebEngine::GetUrl()
637 {
638   return Internal::Adaptor::GetImplementation( *this ).GetUrl();
639 }
640
641 NativeImageInterfacePtr WebEngine::GetNativeImageSource()
642 {
643   Any source;
644   Dali::NativeImageSourcePtr sourcePtr = Dali::NativeImageSource::New( source );
645   return sourcePtr;
646 }
647
648 void WebEngine::LoadHtmlString( const std::string& htmlString )
649 {
650 }
651
652 void WebEngine::Reload()
653 {
654 }
655
656 void WebEngine::StopLoading()
657 {
658 }
659
660 void WebEngine::Suspend()
661 {
662 }
663
664 void WebEngine::Resume()
665 {
666 }
667
668 bool WebEngine::CanGoForward()
669 {
670   return Internal::Adaptor::GetImplementation( *this ).CanGoForward();
671 }
672
673 void WebEngine::GoForward()
674 {
675   Internal::Adaptor::GetImplementation( *this ).GoForward();
676 }
677
678 bool WebEngine::CanGoBack()
679 {
680   return Internal::Adaptor::GetImplementation( *this ).CanGoBack();
681 }
682
683 void WebEngine::GoBack()
684 {
685   Internal::Adaptor::GetImplementation( *this ).GoBack();
686 }
687
688 void WebEngine::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
689 {
690   Internal::Adaptor::GetImplementation( *this ).EvaluateJavaScript( script, resultHandler );
691 }
692
693 void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler )
694 {
695 }
696
697 void WebEngine::ClearHistory()
698 {
699   Internal::Adaptor::GetImplementation( *this ).ClearHistory();
700 }
701
702 const std::string& WebEngine::GetUserAgent() const
703 {
704   return Internal::Adaptor::GetImplementation( *this ).GetUserAgent();
705 }
706
707 void WebEngine::SetUserAgent( const std::string& userAgent )
708 {
709   Internal::Adaptor::GetImplementation( *this ).SetUserAgent( userAgent );
710 }
711
712 void WebEngine::ScrollBy( int dx, int dy )
713 {
714   Internal::Adaptor::GetImplementation( *this ).ScrollBy( dx, dy );
715 }
716
717 void WebEngine::SetScrollPosition( int x, int y )
718 {
719   Internal::Adaptor::GetImplementation( *this ).SetScrollPosition( x, y );
720 }
721
722 void WebEngine::GetScrollPosition( int& x, int& y ) const
723 {
724   Internal::Adaptor::GetImplementation( *this ).GetScrollPosition( x, y );
725 }
726
727 void WebEngine::GetScrollSize( int& w, int& h ) const
728 {
729   Internal::Adaptor::GetImplementation( *this ).GetScrollSize( w, h );
730 }
731
732 void WebEngine::GetContentSize( int& w, int& h ) const
733 {
734   Internal::Adaptor::GetImplementation( *this ).GetContentSize( w, h );
735 }
736
737 void WebEngine::SetSize( int width, int height )
738 {
739 }
740
741 bool WebEngine::SendTouchEvent( const TouchEvent& touch )
742 {
743   return true;
744 }
745
746 bool WebEngine::SendKeyEvent( const KeyEvent& event )
747 {
748   return true;
749 }
750
751 void WebEngine::SetFocus( bool focused )
752 {
753 }
754
755 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
756 {
757   return Internal::Adaptor::GetImplementation( *this ).PageLoadStartedSignal();
758 }
759
760 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
761 {
762   return Internal::Adaptor::GetImplementation( *this ).PageLoadFinishedSignal();
763 }
764
765 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
766 {
767   return Internal::Adaptor::GetImplementation( *this ).PageLoadErrorSignal();
768 }
769
770 Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& WebEngine::ScrollEdgeReachedSignal()
771 {
772   return Internal::Adaptor::GetImplementation( *this ).ScrollEdgeReachedSignal();
773 }
774
775 } // namespace Dali;
776