db94ec06d35ba18c22189dd933c856449bbdcdf4
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-web-engine.cpp
1 /*
2  * Copyright (c) 2021 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-form-repost-decision.h>
26 #include <dali/devel-api/adaptor-framework/web-engine-request-interceptor.h>
27 #include <dali/devel-api/adaptor-framework/web-engine-settings.h>
28 #include <dali/public-api/adaptor-framework/native-image-source.h>
29 #include <dali/public-api/images/pixel-data.h>
30 #include <dali/public-api/object/any.h>
31 #include <dali/public-api/object/base-object.h>
32 #include <memory>
33 #include <string.h>
34 #include <toolkit-application.h>
35
36 namespace Dali
37 {
38
39 namespace Internal
40 {
41
42 namespace Adaptor
43 {
44
45 class WebEngine;
46
47 namespace
48 {
49
50 // Generally only one WebEngine instance exists.
51 // If > 1, a new web engine has been created by CreateWindowSignal.
52 static WebEngine* gInstance = 0;
53 static int gInstanceCount = 0;
54
55 bool OnGoBack();
56 bool OnGoForward();
57 bool OnLoadUrl();
58 bool OnEvaluteJavaScript();
59 bool OnJavaScriptAlert();
60 bool OnJavaScriptConfirm();
61 bool OnJavaScriptPrompt();
62 bool OnScrollEdge();
63 bool OnScreenshotCaptured();
64 bool OnVideoPlaying();
65 bool OnGeolocationPermission();
66 bool OnClearHistory();
67
68 static void ConnectToGlobalSignal( bool ( *func )() )
69 {
70   Dali::Timer timer = Dali::Timer::New( 0 );
71   timer.TickSignal().Connect( func );
72 }
73
74 static void DisconnectFromGlobalSignal( bool ( *func )() )
75 {
76   Dali::Timer timer = Dali::Timer::New( 0 );
77   timer.TickSignal().Disconnect( func );
78 }
79 } // namespace anonymous
80
81 class MockWebEngineContext : public Dali::WebEngineContext
82 {
83 public:
84   MockWebEngineContext()
85     : mockModel( Dali::WebEngineContext::CacheModel::DOCUMENT_VIEWER )
86   {
87   }
88
89   Dali::WebEngineContext::CacheModel GetCacheModel() const override
90   {
91     return mockModel;
92   }
93
94   void SetCacheModel( Dali::WebEngineContext::CacheModel cacheModel ) override
95   {
96     mockModel = cacheModel;
97   }
98
99   void SetProxyUri( const std::string& uri ) override
100   {
101   }
102
103   void SetDefaultProxyAuth( const std::string& username, const std::string& password ) override
104   {
105   }
106
107   void SetCertificateFilePath( const std::string& certificatePath ) override
108   {
109   }
110
111   void DeleteWebDatabase() override
112   {
113   }
114
115   void DeleteWebStorage() override
116   {
117   }
118
119   void DeleteLocalFileSystem() override
120   {
121   }
122
123   void DisableCache( bool cacheDisabled ) override
124   {
125   }
126
127   void ClearCache() override
128   {
129   }
130
131 private:
132   Dali::WebEngineContext::CacheModel mockModel;
133 };
134
135 class MockWebEngineCookieManager : public Dali::WebEngineCookieManager
136 {
137 public:
138   MockWebEngineCookieManager()
139     : mockCookieAcceptPolicy( Dali::WebEngineCookieManager::CookieAcceptPolicy::NO_THIRD_PARTY )
140   {
141   }
142
143   void SetCookieAcceptPolicy( Dali::WebEngineCookieManager::CookieAcceptPolicy policy ) override
144   {
145     mockCookieAcceptPolicy = policy;
146   }
147
148   Dali::WebEngineCookieManager::CookieAcceptPolicy GetCookieAcceptPolicy() const override
149   {
150     return mockCookieAcceptPolicy;
151   }
152
153   void ClearCookies() override
154   {
155   }
156
157   void SetPersistentStorage( const std::string& path, Dali::WebEngineCookieManager::CookiePersistentStorage storage ) override
158   {
159   }
160
161 private:
162   Dali::WebEngineCookieManager::CookieAcceptPolicy mockCookieAcceptPolicy;
163 };
164
165 class MockWebEngineBackForwardListItem : public Dali::WebEngineBackForwardListItem
166 {
167 public:
168   MockWebEngineBackForwardListItem()
169     : mockUrl( "http://url" ),
170       mockTitle( "title" ),
171       mockOriginalUrl( "http://originalurl" )
172   {
173   }
174
175   std::string GetUrl() const override
176   {
177     return mockUrl;
178   }
179
180   std::string GetTitle() const override
181   {
182     return mockTitle;
183   }
184
185   std::string GetOriginalUrl() const override
186   {
187     return mockOriginalUrl;
188   }
189
190 private:
191   std::string mockUrl;
192   std::string mockTitle;
193   std::string mockOriginalUrl;
194 };
195
196 class MockWebEngineBackForwardList : public Dali::WebEngineBackForwardList
197 {
198 public:
199   MockWebEngineBackForwardList()
200     : mockItem(),
201       pMockItem( &mockItem )
202   {
203   }
204
205   Dali::WebEngineBackForwardListItem& GetCurrentItem() const override
206   {
207     return *pMockItem;
208   }
209
210   Dali::WebEngineBackForwardListItem& GetItemAtIndex( uint32_t index ) const override
211   {
212     return *pMockItem;
213   }
214
215   uint32_t GetItemCount() const override
216   {
217     return 1;
218   }
219
220 private:
221   MockWebEngineBackForwardListItem mockItem;
222   WebEngineBackForwardListItem* pMockItem;
223 };
224
225 class MockWebEngineFormRepostDecision : public WebEngineFormRepostDecision
226 {
227 public:
228   MockWebEngineFormRepostDecision()
229   {
230   }
231
232   void Reply(bool allowed) override {}
233 };
234
235 class MockWebEngineRequestInterceptor : public WebEngineRequestInterceptor
236 {
237 public:
238   MockWebEngineRequestInterceptor()
239   {
240   }
241
242   std::string GetUrl() const override
243   {
244     return "http://test.html";
245   }
246
247   bool Ignore() override
248   {
249     return true;
250   }
251
252   bool SetResponseStatus(int statusCode, const std::string &customedStatusText) override
253   {
254     return true;
255   }
256
257   bool AddResponseHeader(const std::string &fieldName, const std::string &fieldValue) override
258   {
259     return true;
260   }
261
262   bool AddResponseBody(const std::string &body, uint32_t length) override
263   {
264     return true;
265   }
266 };
267
268 class MockWebEngineSettings : public WebEngineSettings
269 {
270 public:
271   MockWebEngineSettings()
272     : mockDefaultFontSize( 16 ),
273       mockJavaScriptEnabled( true ),
274       mockAutoFittingEnabled ( true ),
275       mockPluginsEnabled ( true ),
276       mockPrivateBrowsingEnabled( true ),
277       mockLinkMagnifierEnabled( true ),
278       mockKeypadWithoutUserActionUsed( true ),
279       mockAutofillPasswordFormEnabled( true ),
280       mockFormCandidateDataEnabled( true ),
281       mockTextSelectionEnabled( true ),
282       mockTextAutosizingEnable( true ),
283       mockArrowScrollEnable( true ),
284       mockClipboardEnabled( true ),
285       mockImePanelEnabled( true ),
286       mockImageLoadedAutomatically( true ),
287       mockDefaultTextEncodingName()
288   {
289   }
290
291   uint32_t GetDefaultFontSize() const override
292   {
293     return mockDefaultFontSize;
294   }
295
296   void SetDefaultFontSize( uint32_t size ) override
297   {
298     mockDefaultFontSize = size;
299   }
300
301   bool IsJavaScriptEnabled() const override
302   {
303     return mockJavaScriptEnabled;
304   }
305
306   void EnableJavaScript( bool enabled ) override
307   {
308     mockJavaScriptEnabled = enabled;
309   }
310
311   bool IsAutoFittingEnabled() const override
312   {
313     return mockAutoFittingEnabled;
314   }
315
316   void EnableAutoFitting( bool enabled ) override
317   {
318     mockAutoFittingEnabled = enabled;
319   }
320
321   bool ArePluginsEnabled() const override
322   {
323     return mockPluginsEnabled;
324   }
325
326   void EnablePlugins( bool enabled ) override
327   {
328     mockPluginsEnabled = enabled;
329   }
330
331   bool IsPrivateBrowsingEnabled() const override
332   {
333     return mockPrivateBrowsingEnabled;
334   }
335
336   void EnablePrivateBrowsing( bool enabled ) override
337   {
338     mockPrivateBrowsingEnabled = enabled;
339   }
340
341   bool IsLinkMagnifierEnabled() const override
342   {
343     return mockLinkMagnifierEnabled;
344   }
345
346   void EnableLinkMagnifier( bool enabled ) override
347   {
348     mockLinkMagnifierEnabled = enabled;
349   }
350
351   bool IsKeypadWithoutUserActionUsed() const override
352   {
353     return mockKeypadWithoutUserActionUsed;
354   }
355
356   void UseKeypadWithoutUserAction( bool used ) override
357   {
358     mockKeypadWithoutUserActionUsed = used;
359   }
360
361   bool IsAutofillPasswordFormEnabled() const override
362   {
363     return mockAutofillPasswordFormEnabled;
364   }
365
366   void EnableAutofillPasswordForm( bool enabled ) override
367   {
368     mockAutofillPasswordFormEnabled = enabled;
369   }
370
371   bool IsFormCandidateDataEnabled() const override
372   {
373     return mockFormCandidateDataEnabled;
374   }
375
376   void EnableFormCandidateData( bool enabled ) override
377   {
378     mockFormCandidateDataEnabled = enabled;
379   }
380
381   bool IsTextSelectionEnabled() const override
382   {
383     return mockTextSelectionEnabled;
384   }
385
386   void EnableTextSelection( bool enabled ) override
387   {
388     mockTextSelectionEnabled = enabled;
389   }
390
391   bool IsTextAutosizingEnabled() const override
392   {
393     return mockTextAutosizingEnable;
394   }
395
396   void EnableTextAutosizing( bool enabled ) override
397   {
398     mockTextAutosizingEnable = enabled;
399   }
400
401   bool IsArrowScrollEnabled() const override
402   {
403     return mockArrowScrollEnable;
404   }
405
406   void EnableArrowScroll( bool enabled ) override
407   {
408     mockArrowScrollEnable = enabled;
409   }
410
411   bool IsClipboardEnabled() const override
412   {
413     return mockClipboardEnabled;
414   }
415
416   void EnableClipboard( bool enabled ) override
417   {
418     mockClipboardEnabled = enabled;
419   }
420
421   bool IsImePanelEnabled() const override
422   {
423     return mockImePanelEnabled;
424   }
425
426   void EnableImePanel( bool enabled ) override
427   {
428     mockImePanelEnabled = enabled;
429   }
430
431   bool AreImagesLoadedAutomatically() const override
432   {
433     return mockImageLoadedAutomatically;
434   }
435
436   void AllowImagesLoadAutomatically( bool automatic ) override
437   {
438     mockImageLoadedAutomatically = automatic;
439   }
440
441   std::string GetDefaultTextEncodingName() const override
442   {
443     return mockDefaultTextEncodingName;
444   }
445
446   void SetDefaultTextEncodingName( const std::string& defaultTextEncodingName ) override
447   {
448     mockDefaultTextEncodingName = defaultTextEncodingName;
449   }
450
451   void AllowMixedContents( bool allowed ) override
452   {
453   }
454
455   void EnableSpatialNavigation( bool enabled ) override
456   {
457   }
458
459   void EnableWebSecurity( bool enabled ) override
460   {
461   }
462
463   void EnableCacheBuilder( bool enabled ) override
464   {
465   }
466
467   void UseScrollbarThumbFocusNotifications( bool used ) override
468   {
469   }
470
471   void EnableDoNotTrack( bool enabled ) override
472   {
473   }
474
475   void AllowFileAccessFromExternalUrl( bool allowed ) override
476   {
477   }
478
479   void AllowScriptsOpenWindows( bool allowed ) override
480   {
481   }
482
483 private:
484   int mockDefaultFontSize;
485   bool mockJavaScriptEnabled;
486   bool mockAutoFittingEnabled;
487   bool mockPluginsEnabled;
488   bool mockPrivateBrowsingEnabled;
489   bool mockLinkMagnifierEnabled;
490   bool mockKeypadWithoutUserActionUsed;
491   bool mockAutofillPasswordFormEnabled;
492   bool mockFormCandidateDataEnabled;
493   bool mockTextSelectionEnabled;
494   bool mockTextAutosizingEnable;
495   bool mockArrowScrollEnable;
496   bool mockClipboardEnabled;
497   bool mockImePanelEnabled;
498   bool mockImageLoadedAutomatically;
499   std::string mockDefaultTextEncodingName;
500 };
501
502 class WebEngine: public Dali::BaseObject
503 {
504 public:
505
506   using JavaScriptEvaluatedResultCallback = std::function<void(const std::string&)>;
507
508   WebEngine()
509     : mUrl()
510     , mCurrentPlusOnePos( 0 )
511     , mUserAgent()
512     , mEvaluating( false )
513     , mScrollPosition( 0, 0 )
514     , mScrollSize( 500, 500 )
515     , mContentSize( 500, 500 )
516   {
517     gInstanceCount++;
518     if ( gInstanceCount == 1 ) // only first web engine need be saved.
519     {
520       gInstance = this;
521     }
522
523     mockWebEngineSettings = new MockWebEngineSettings();
524     mockWebEngineContext = new MockWebEngineContext();
525     mockWebEngineCookieManager = new MockWebEngineCookieManager();
526     mockWebEngineBackForwardList = new MockWebEngineBackForwardList();
527   }
528
529   virtual ~WebEngine()
530   {
531     gInstanceCount--;
532     if( !gInstanceCount )
533     {
534       gInstance = 0;
535     }
536
537     delete mockWebEngineSettings;
538     delete mockWebEngineContext;
539     delete mockWebEngineCookieManager;
540     delete mockWebEngineBackForwardList;
541   }
542
543   Dali::WebEngineSettings& GetSettings() const
544   {
545     return *mockWebEngineSettings;
546   }
547
548   Dali::WebEngineContext& GetContext() const
549   {
550     return *mockWebEngineContext;
551   }
552
553   Dali::WebEngineCookieManager& GetCookieManager() const
554   {
555     return *mockWebEngineCookieManager;
556   }
557
558   Dali::WebEngineBackForwardList& GetBackForwardList() const
559   {
560     return *mockWebEngineBackForwardList;
561   }
562
563   void LoadUrl( const std::string& url )
564   {
565     mUrl = url;
566     ConnectToGlobalSignal( &OnLoadUrl );
567   }
568
569   const std::string& GetUrl() const
570   {
571     return mUrl;
572   }
573
574   std::string GetTitle() const
575   {
576     return std::string("title");
577   }
578
579   Dali::PixelData GetFavicon() const
580   {
581     uint8_t* faviconData = new uint8_t[ 16 ];
582
583     faviconData[ 0 ] = 0xff;
584     faviconData[ 1 ] = 0x00;
585     faviconData[ 2 ] = 0x00;
586     faviconData[ 3 ] = 0xff;
587     faviconData[ 4 ] = 0xff;
588     faviconData[ 5 ] = 0x00;
589     faviconData[ 6 ] = 0x00;
590     faviconData[ 7 ] = 0xff;
591     faviconData[ 8 ] = 0xff;
592     faviconData[ 9 ] = 0x00;
593     faviconData[ 10 ] = 0x00;
594     faviconData[ 11 ] = 0xff;
595     faviconData[ 12 ] = 0xff;
596     faviconData[ 13 ] = 0x00;
597     faviconData[ 14 ] = 0x00;
598     faviconData[ 15 ] = 0xff;
599
600     return Dali::PixelData::New( faviconData, 16, 2, 2,
601                                  Dali::Pixel::Format::RGBA8888,
602                                  Dali::PixelData::ReleaseFunction::DELETE_ARRAY );
603   }
604
605   bool CanGoForward() const
606   {
607     return mHistory.size() > mCurrentPlusOnePos;
608   }
609
610   void GoForward()
611   {
612     ConnectToGlobalSignal( &OnGoForward );
613   }
614
615   bool CanGoBack() const
616   {
617     return mCurrentPlusOnePos > 1;
618   }
619
620   void GoBack()
621   {
622     ConnectToGlobalSignal( &OnGoBack );
623   }
624
625   void EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
626   {
627     if( resultHandler )
628     {
629       if( !mEvaluating )
630       {
631         ConnectToGlobalSignal( &OnEvaluteJavaScript );
632       }
633       mResultCallbacks.push_back( resultHandler );
634     }
635   }
636
637   void RegisterJavaScriptAlertCallback( Dali::WebEnginePlugin::JavaScriptAlertCallback callback )
638   {
639     if ( callback )
640     {
641       ConnectToGlobalSignal( &OnJavaScriptAlert );
642       mJavaScriptAlertCallback = callback;
643     }
644   }
645
646   void RegisterJavaScriptConfirmCallback( Dali::WebEnginePlugin::JavaScriptConfirmCallback callback )
647   {
648     if ( callback )
649     {
650       ConnectToGlobalSignal( &OnJavaScriptConfirm );
651       mJavaScriptConfirmCallback = callback;
652     }
653   }
654
655   void RegisterJavaScriptPromptCallback( Dali::WebEnginePlugin::JavaScriptPromptCallback callback )
656   {
657     if ( callback )
658     {
659       ConnectToGlobalSignal( &OnJavaScriptPrompt );
660       mJavaScriptPromptCallback = callback;
661     }
662   }
663
664   void ClearHistory()
665   {
666     ConnectToGlobalSignal( &OnClearHistory );
667   }
668
669   const std::string& GetUserAgent() const
670   {
671     return mUserAgent;
672   }
673
674   void SetUserAgent( const std::string& userAgent )
675   {
676     mUserAgent = userAgent;
677   }
678
679   void ScrollBy( int dx, int dy )
680   {
681     mScrollPosition += Dali::Vector2( dx, dy );
682     if ( mScrollPosition.y + mScrollSize.height > mContentSize.height )
683     {
684       ConnectToGlobalSignal( &OnScrollEdge );
685     }
686   }
687
688   bool ScrollEdgeBy( int dx, int dy )
689   {
690     mScrollPosition += Dali::Vector2( dx, dy );
691     if ( mScrollPosition.y + mScrollSize.height > mContentSize.height )
692     {
693       ConnectToGlobalSignal( &OnScrollEdge );
694     }
695     return true;
696   }
697
698   void SetScrollPosition( int x, int y )
699   {
700     mScrollPosition.x = x;
701     mScrollPosition.y = y;
702   }
703
704   Dali::Vector2 GetScrollPosition() const
705   {
706     return mScrollPosition;
707   }
708
709   Dali::Vector2 GetScrollSize() const
710   {
711     return mScrollSize;
712   }
713
714   Dali::Vector2 GetContentSize() const
715   {
716     return mContentSize;
717   }
718
719   void SetPageZoomFactor(float zoomFactor)
720   {
721     mPageZoomFactor = zoomFactor;
722   }
723
724   float GetPageZoomFactor() const
725   {
726     return mPageZoomFactor;
727   }
728
729   void SetTextZoomFactor(float zoomFactor)
730   {
731     mTextZoomFactor = zoomFactor;
732   }
733
734   float GetTextZoomFactor() const
735   {
736     return mTextZoomFactor;
737   }
738
739   float GetLoadProgressPercentage() const
740   {
741     return 0.5f;
742   }
743
744   void SetScaleFactor(float scaleFactor, Dali::Vector2 point)
745   {
746     mScaleFactor = scaleFactor;
747   }
748
749   float GetScaleFactor() const
750   {
751     return mScaleFactor;
752   }
753
754   Dali::PixelData GetScreenshot(Dali::Rect<int> viewArea, float scaleFactor)
755   {
756     uint32_t bufferSize = viewArea.width * viewArea.height * 4 ;
757     uint8_t* pixel = new uint8_t[ bufferSize ];
758     memset(pixel, 0xff, bufferSize);
759     return Dali::PixelData::New( pixel, bufferSize, viewArea.width, viewArea.height,
760                                  Dali::Pixel::Format::RGBA8888,
761                                  Dali::PixelData::ReleaseFunction::DELETE_ARRAY );
762   }
763
764   bool GetScreenshotAsynchronously(Dali::Rect<int> viewArea, float scaleFactor, Dali::WebEnginePlugin::ScreenshotCapturedCallback callback)
765   {
766     if ( callback )
767     {
768       ConnectToGlobalSignal( &OnScreenshotCaptured );
769       mScreenshotCapturedCallback = callback;
770     }
771     return true;
772   }
773
774   bool CheckVideoPlayingAsynchronously(Dali::WebEnginePlugin::VideoPlayingCallback callback)
775   {
776     if ( callback )
777     {
778       ConnectToGlobalSignal( &OnVideoPlaying );
779       mVideoPlayingCallback = callback;
780     }
781     return true;
782   }
783
784   void RegisterGeolocationPermissionCallback(Dali::WebEnginePlugin::GeolocationPermissionCallback callback)
785   {
786     if ( callback )
787     {
788       ConnectToGlobalSignal( &OnGeolocationPermission );
789       mGeolocationPermissionCallback = callback;
790     }
791   }
792
793   Dali::WebEnginePlugin::WebEnginePageLoadSignalType& PageLoadStartedSignal()
794   {
795     return mPageLoadStartedSignal;
796   }
797
798   Dali::WebEnginePlugin::WebEnginePageLoadSignalType& PageLoadInProgressSignal()
799   {
800     return mPageLoadInProgressSignal;
801   }
802
803   Dali::WebEnginePlugin::WebEnginePageLoadSignalType& PageLoadFinishedSignal()
804   {
805     return mPageLoadFinishedSignal;
806   }
807
808   Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& PageLoadErrorSignal()
809   {
810     return mPageLoadErrorSignal;
811   }
812
813   Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& ScrollEdgeReachedSignal()
814   {
815     return mScrollEdgeReachedSignal;
816   }
817
818   Dali::WebEnginePlugin::WebEngineUrlChangedSignalType& UrlChangedSignal()
819   {
820     return mUrlChangedSignal;
821   }
822
823   Dali::WebEnginePlugin::WebEngineFormRepostDecisionSignalType& FormRepostDecisionSignal()
824   {
825     return mFormRepostDecisionSignal;
826   }
827
828   Dali::WebEnginePlugin::WebEngineFrameRenderedSignalType& FrameRenderedSignal()
829   {
830     return mFrameRenderedSignal;
831   }
832
833   Dali::WebEnginePlugin::WebEngineRequestInterceptorSignalType& RequestInterceptorSignal()
834   {
835     return mRequestInterceptorSignal;
836   }
837
838   std::string              mUrl;
839   std::vector<std::string> mHistory;
840   size_t                   mCurrentPlusOnePos;
841   std::string              mUserAgent;
842
843   Dali::WebEnginePlugin::WebEnginePageLoadSignalType           mPageLoadStartedSignal;
844   Dali::WebEnginePlugin::WebEnginePageLoadSignalType           mPageLoadInProgressSignal;
845   Dali::WebEnginePlugin::WebEnginePageLoadSignalType           mPageLoadFinishedSignal;
846   Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType      mPageLoadErrorSignal;
847   Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType  mScrollEdgeReachedSignal;
848   Dali::WebEnginePlugin::WebEngineUrlChangedSignalType         mUrlChangedSignal;
849   Dali::WebEnginePlugin::WebEngineFormRepostDecisionSignalType mFormRepostDecisionSignal;
850   Dali::WebEnginePlugin::WebEngineFrameRenderedSignalType      mFrameRenderedSignal;
851   Dali::WebEnginePlugin::WebEngineRequestInterceptorSignalType mRequestInterceptorSignal;
852
853   bool  mEvaluating;
854   float mPageZoomFactor;
855   float mTextZoomFactor;
856   float mScaleFactor;
857
858   Dali::Vector2             mScrollPosition;
859   Dali::Vector2             mScrollSize;
860   Dali::Vector2             mContentSize;
861   WebEngineBackForwardList* mockWebEngineBackForwardList;
862   WebEngineContext*         mockWebEngineContext;
863   WebEngineCookieManager*   mockWebEngineCookieManager;
864   WebEngineSettings*        mockWebEngineSettings;
865
866   std::vector<JavaScriptEvaluatedResultCallback>              mResultCallbacks;
867   Dali::WebEnginePlugin::JavaScriptAlertCallback              mJavaScriptAlertCallback;
868   Dali::WebEnginePlugin::JavaScriptConfirmCallback            mJavaScriptConfirmCallback;
869   Dali::WebEnginePlugin::JavaScriptPromptCallback             mJavaScriptPromptCallback;
870   Dali::WebEnginePlugin::ScreenshotCapturedCallback           mScreenshotCapturedCallback;
871   Dali::WebEnginePlugin::VideoPlayingCallback                 mVideoPlayingCallback;
872   Dali::WebEnginePlugin::GeolocationPermissionCallback        mGeolocationPermissionCallback;
873 };
874
875
876 namespace
877 {
878
879 bool OnGoBack()
880 {
881   DisconnectFromGlobalSignal( &OnGoBack );
882
883   if( gInstance && gInstance->CanGoBack() )
884   {
885     gInstance->mCurrentPlusOnePos--;
886   }
887   return false;
888 }
889
890 bool OnGoForward()
891 {
892   DisconnectFromGlobalSignal( &OnGoForward );
893
894   if( gInstance && gInstance->CanGoForward() )
895   {
896     gInstance->mCurrentPlusOnePos++;
897   }
898   return false;
899 }
900
901 bool OnLoadUrl()
902 {
903   DisconnectFromGlobalSignal( &OnLoadUrl );
904
905   if( gInstance )
906   {
907     if( gInstance->mHistory.size() > gInstance->mCurrentPlusOnePos )
908     {
909       gInstance->mHistory.erase( gInstance->mHistory.begin() + gInstance->mCurrentPlusOnePos, gInstance->mHistory.end() );
910     }
911     gInstance->mHistory.push_back( gInstance->mUrl );
912     gInstance->mCurrentPlusOnePos++;
913     gInstance->mPageLoadStartedSignal.Emit( gInstance->mUrl );
914     gInstance->mPageLoadInProgressSignal.Emit( gInstance->mUrl );
915     gInstance->mPageLoadFinishedSignal.Emit( gInstance->mUrl );
916     gInstance->mUrlChangedSignal.Emit( "http://new-test" );
917
918     std::shared_ptr<Dali::WebEngineFormRepostDecision> decision(new MockWebEngineFormRepostDecision());
919     gInstance->mFormRepostDecisionSignal.Emit(std::move(decision));
920     gInstance->mFrameRenderedSignal.Emit();
921     std::shared_ptr<Dali::WebEngineRequestInterceptor> interceptor(new MockWebEngineRequestInterceptor());
922     gInstance->mRequestInterceptorSignal.Emit(std::move(interceptor));
923   }
924   return false;
925 }
926
927 bool OnScrollEdge()
928 {
929   DisconnectFromGlobalSignal( &OnScrollEdge );
930
931   if( gInstance )
932   {
933     gInstance->mScrollEdgeReachedSignal.Emit( Dali::WebEnginePlugin::ScrollEdge::BOTTOM );
934   }
935
936   return false;
937 }
938
939 bool OnEvaluteJavaScript()
940 {
941   DisconnectFromGlobalSignal( &OnEvaluteJavaScript );
942
943   if( gInstance )
944   {
945     for( auto& func : gInstance->mResultCallbacks )
946     {
947       func("undefined");
948     }
949     gInstance->mResultCallbacks.clear();
950   }
951   return false;
952 }
953
954 bool OnJavaScriptAlert()
955 {
956   DisconnectFromGlobalSignal( &OnJavaScriptAlert );
957   if ( gInstance )
958   {
959     gInstance->mJavaScriptAlertCallback( "this is an alert popup." );
960   }
961   return false;
962 }
963
964 bool OnJavaScriptConfirm()
965 {
966   DisconnectFromGlobalSignal( &OnJavaScriptConfirm );
967   if ( gInstance )
968   {
969     gInstance->mJavaScriptConfirmCallback( "this is a confirm popup." );
970   }
971   return false;
972 }
973
974 bool OnJavaScriptPrompt()
975 {
976   DisconnectFromGlobalSignal( &OnJavaScriptPrompt );
977   if ( gInstance )
978   {
979     gInstance->mJavaScriptPromptCallback( "this is a prompt pompt.", "" );
980   }
981   return false;
982 }
983
984 bool OnScreenshotCaptured()
985 {
986   DisconnectFromGlobalSignal( &OnScreenshotCaptured );
987   if ( gInstance )
988   {
989     uint8_t* pixel = new uint8_t[ 2 * 2 * 4 ];
990     memset(pixel, 0xff, 2 * 2 * 4);
991     Dali::PixelData data = Dali::PixelData::New( pixel, 2 * 2 * 4, 2, 2,
992                                  Dali::Pixel::Format::RGBA8888,
993                                  Dali::PixelData::ReleaseFunction::DELETE_ARRAY );
994     gInstance->mScreenshotCapturedCallback( data );
995   }
996   return false;
997 }
998
999 bool OnVideoPlaying()
1000 {
1001   DisconnectFromGlobalSignal( &OnVideoPlaying );
1002   if ( gInstance )
1003   {
1004     gInstance->mVideoPlayingCallback( true );
1005   }
1006   return false;
1007 }
1008
1009 bool OnGeolocationPermission()
1010 {
1011   DisconnectFromGlobalSignal( &OnGeolocationPermission );
1012   if ( gInstance )
1013   {
1014     gInstance->mGeolocationPermissionCallback( "", "" );
1015   }
1016   return false;
1017 }
1018
1019 bool OnClearHistory()
1020 {
1021   DisconnectFromGlobalSignal( &OnClearHistory );
1022
1023   if( gInstance && gInstance->mCurrentPlusOnePos )
1024   {
1025     std::string url = gInstance->mHistory[ gInstance->mCurrentPlusOnePos - 1 ];
1026     std::vector< std::string >().swap( gInstance->mHistory );
1027     gInstance->mHistory.push_back( url );
1028     gInstance->mCurrentPlusOnePos = 1;
1029   }
1030   return false;
1031 }
1032
1033 } // namespace
1034
1035 inline WebEngine& GetImplementation( Dali::WebEngine& webEngine )
1036 {
1037   DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
1038   BaseObject& handle = webEngine.GetBaseObject();
1039   return static_cast< Internal::Adaptor::WebEngine& >( handle );
1040 }
1041
1042 inline const WebEngine& GetImplementation( const Dali::WebEngine& webEngine )
1043 {
1044   DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
1045   const BaseObject& handle = webEngine.GetBaseObject();
1046   return static_cast< const Internal::Adaptor::WebEngine& >( handle );
1047 }
1048
1049 } // namespace Adaptor
1050
1051 } // namespace Internal
1052
1053 // Dali::WebEngine Implementation
1054 WebEngine::WebEngine()
1055 {
1056 }
1057
1058 WebEngine::WebEngine( Internal::Adaptor::WebEngine* internal )
1059 : BaseHandle( internal )
1060 {
1061 }
1062
1063 WebEngine::~WebEngine()
1064 {
1065 }
1066
1067 WebEngine WebEngine::New()
1068 {
1069   Internal::Adaptor::WebEngine* baseObject = new Internal::Adaptor::WebEngine();
1070
1071   return WebEngine( baseObject );
1072 }
1073
1074 WebEngine::WebEngine( const WebEngine& WebEngine )
1075 : BaseHandle( WebEngine )
1076 {
1077 }
1078
1079 WebEngine& WebEngine::operator=( const WebEngine& webEngine )
1080 {
1081   BaseHandle::operator=( webEngine );
1082   return *this;
1083 }
1084
1085 WebEngine WebEngine::DownCast( BaseHandle handle )
1086 {
1087   return WebEngine( dynamic_cast< Internal::Adaptor::WebEngine* >( handle.GetObjectPtr() ) );
1088 }
1089
1090 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
1091 {
1092 }
1093
1094 void WebEngine::Create( int width, int height, int argc, char** argv )
1095 {
1096 }
1097
1098 void WebEngine::Destroy()
1099 {
1100 }
1101
1102 WebEngineSettings& WebEngine::GetSettings() const
1103 {
1104   return Internal::Adaptor::GetImplementation( *this ).GetSettings();
1105 }
1106
1107 WebEngineContext& WebEngine::GetContext() const
1108 {
1109   return Internal::Adaptor::GetImplementation( *this ).GetContext();
1110 }
1111
1112 WebEngineCookieManager& WebEngine::GetCookieManager() const
1113 {
1114   return Internal::Adaptor::GetImplementation( *this ).GetCookieManager();
1115 }
1116
1117 WebEngineBackForwardList& WebEngine::GetBackForwardList() const
1118 {
1119   return Internal::Adaptor::GetImplementation( *this ).GetBackForwardList();
1120 }
1121
1122 void WebEngine::LoadUrl( const std::string& url )
1123 {
1124   return Internal::Adaptor::GetImplementation( *this ).LoadUrl( url );
1125 }
1126
1127 std::string WebEngine::GetTitle() const
1128 {
1129   return Internal::Adaptor::GetImplementation( *this ).GetTitle();
1130 }
1131
1132 Dali::PixelData WebEngine::GetFavicon() const
1133 {
1134   return Internal::Adaptor::GetImplementation( *this ).GetFavicon();
1135 }
1136
1137 const std::string& WebEngine::GetUrl()
1138 {
1139   return Internal::Adaptor::GetImplementation( *this ).GetUrl();
1140 }
1141
1142 NativeImageInterfacePtr WebEngine::GetNativeImageSource()
1143 {
1144   Any source;
1145   Dali::NativeImageSourcePtr sourcePtr = Dali::NativeImageSource::New( source );
1146   return sourcePtr;
1147 }
1148
1149 void WebEngine::LoadHtmlString( const std::string& htmlString )
1150 {
1151 }
1152
1153 bool WebEngine::LoadHtmlStringOverrideCurrentEntry(const std::string& html, const std::string& basicUri, const std::string& unreachableUrl)
1154 {
1155   return true;
1156 }
1157
1158 bool WebEngine::LoadContents(const std::string& contents, uint32_t contentSize, const std::string& mimeType, const std::string& encoding, const std::string& baseUri)
1159 {
1160   return true;
1161 }
1162
1163 void WebEngine::Reload()
1164 {
1165 }
1166
1167 bool WebEngine::ReloadWithoutCache()
1168 {
1169   return true;
1170 }
1171
1172 void WebEngine::StopLoading()
1173 {
1174 }
1175
1176 void WebEngine::Suspend()
1177 {
1178 }
1179
1180 void WebEngine::Resume()
1181 {
1182 }
1183
1184 void WebEngine::SuspendNetworkLoading()
1185 {
1186 }
1187
1188 void WebEngine::ResumeNetworkLoading()
1189 {
1190 }
1191
1192 bool WebEngine::AddCustomHeader(const std::string& name, const std::string& value)
1193 {
1194   return true;
1195 }
1196
1197 bool WebEngine::RemoveCustomHeader(const std::string& name)
1198 {
1199   return true;
1200 }
1201
1202 uint32_t WebEngine::StartInspectorServer(uint32_t port)
1203 {
1204   return port;
1205 }
1206
1207 bool WebEngine::StopInspectorServer()
1208 {
1209   return true;
1210 }
1211
1212 bool WebEngine::CanGoForward()
1213 {
1214   return Internal::Adaptor::GetImplementation( *this ).CanGoForward();
1215 }
1216
1217 void WebEngine::GoForward()
1218 {
1219   Internal::Adaptor::GetImplementation( *this ).GoForward();
1220 }
1221
1222 bool WebEngine::CanGoBack()
1223 {
1224   return Internal::Adaptor::GetImplementation( *this ).CanGoBack();
1225 }
1226
1227 void WebEngine::GoBack()
1228 {
1229   Internal::Adaptor::GetImplementation( *this ).GoBack();
1230 }
1231
1232 void WebEngine::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
1233 {
1234   Internal::Adaptor::GetImplementation( *this ).EvaluateJavaScript( script, resultHandler );
1235 }
1236
1237 void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler )
1238 {
1239 }
1240
1241 void WebEngine::RegisterJavaScriptAlertCallback( Dali::WebEnginePlugin::JavaScriptAlertCallback callback )
1242 {
1243   Internal::Adaptor::GetImplementation( *this ).RegisterJavaScriptAlertCallback( callback );
1244 }
1245
1246 void WebEngine::JavaScriptAlertReply()
1247 {
1248 }
1249
1250 void WebEngine::RegisterJavaScriptConfirmCallback( Dali::WebEnginePlugin::JavaScriptConfirmCallback callback )
1251 {
1252   Internal::Adaptor::GetImplementation( *this ).RegisterJavaScriptConfirmCallback( callback );
1253 }
1254
1255 void WebEngine::JavaScriptConfirmReply( bool confirmed )
1256 {
1257 }
1258
1259 void WebEngine::RegisterJavaScriptPromptCallback( Dali::WebEnginePlugin::JavaScriptPromptCallback callback )
1260 {
1261   Internal::Adaptor::GetImplementation( *this ).RegisterJavaScriptPromptCallback( callback );
1262 }
1263
1264 void WebEngine::JavaScriptPromptReply( const std::string& result )
1265 {
1266 }
1267
1268 void WebEngine::ClearAllTilesResources()
1269 {
1270 }
1271
1272 void WebEngine::ClearHistory()
1273 {
1274   Internal::Adaptor::GetImplementation( *this ).ClearHistory();
1275 }
1276
1277 void WebEngine::SetScaleFactor(float scaleFactor, Dali::Vector2 point)
1278 {
1279   Internal::Adaptor::GetImplementation( *this ).SetScaleFactor(scaleFactor, point);
1280 }
1281
1282 float WebEngine::GetScaleFactor() const
1283 {
1284   return Internal::Adaptor::GetImplementation( *this ).GetScaleFactor();
1285 }
1286
1287 void WebEngine::ActivateAccessibility(bool activated)
1288 {
1289 }
1290
1291 bool WebEngine::HighlightText(const std::string& text, Dali::WebEnginePlugin::FindOption options, uint32_t maxMatchCount)
1292 {
1293   return true;
1294 }
1295
1296 void WebEngine::AddDynamicCertificatePath(const std::string& host, const std::string& certPath)
1297 {
1298 }
1299
1300 Dali::PixelData WebEngine::GetScreenshot(Dali::Rect<int> viewArea, float scaleFactor)
1301 {
1302   return Internal::Adaptor::GetImplementation( *this ).GetScreenshot(viewArea, scaleFactor);
1303 }
1304
1305 bool WebEngine::GetScreenshotAsynchronously(Dali::Rect<int> viewArea, float scaleFactor, Dali::WebEnginePlugin::ScreenshotCapturedCallback callback)
1306 {
1307   return Internal::Adaptor::GetImplementation( *this ).GetScreenshotAsynchronously(viewArea, scaleFactor, callback);
1308 }
1309
1310 bool WebEngine::CheckVideoPlayingAsynchronously(Dali::WebEnginePlugin::VideoPlayingCallback callback)
1311 {
1312   return Internal::Adaptor::GetImplementation( *this ).CheckVideoPlayingAsynchronously(callback);
1313 }
1314
1315 void WebEngine::RegisterGeolocationPermissionCallback(Dali::WebEnginePlugin::GeolocationPermissionCallback callback)
1316 {
1317   Internal::Adaptor::GetImplementation( *this ).RegisterGeolocationPermissionCallback(callback);
1318 }
1319
1320 const std::string& WebEngine::GetUserAgent() const
1321 {
1322   return Internal::Adaptor::GetImplementation( *this ).GetUserAgent();
1323 }
1324
1325 void WebEngine::SetUserAgent( const std::string& userAgent )
1326 {
1327   Internal::Adaptor::GetImplementation( *this ).SetUserAgent( userAgent );
1328 }
1329
1330 void WebEngine::ScrollBy( int dx, int dy )
1331 {
1332   Internal::Adaptor::GetImplementation( *this ).ScrollBy( dx, dy );
1333 }
1334
1335 bool WebEngine::ScrollEdgeBy( int dx, int dy )
1336 {
1337   return Internal::Adaptor::GetImplementation( *this ).ScrollEdgeBy( dx, dy );
1338 }
1339
1340 void WebEngine::SetScrollPosition( int x, int y )
1341 {
1342   Internal::Adaptor::GetImplementation( *this ).SetScrollPosition( x, y );
1343 }
1344
1345 Dali::Vector2 WebEngine::GetScrollPosition() const
1346 {
1347   return Internal::Adaptor::GetImplementation( *this ).GetScrollPosition();
1348 }
1349
1350 Dali::Vector2 WebEngine::GetScrollSize() const
1351 {
1352   return Internal::Adaptor::GetImplementation( *this ).GetScrollSize();
1353 }
1354
1355 Dali::Vector2 WebEngine::GetContentSize() const
1356 {
1357   return Internal::Adaptor::GetImplementation( *this ).GetContentSize();
1358 }
1359
1360 void WebEngine::SetSize( int width, int height )
1361 {
1362 }
1363
1364 void WebEngine::SetDocumentBackgroundColor(Dali::Vector4 color)
1365 {
1366 }
1367
1368 void WebEngine::ClearTilesWhenHidden(bool cleared)
1369 {
1370 }
1371
1372 void WebEngine::SetTileCoverAreaMultiplier(float multiplier)
1373 {
1374 }
1375
1376 void WebEngine::EnableCursorByClient(bool enabled)
1377 {
1378 }
1379
1380 std::string WebEngine::GetSelectedText() const
1381 {
1382   return "test";
1383 }
1384
1385 bool WebEngine::SendTouchEvent( const TouchEvent& touch )
1386 {
1387   return true;
1388 }
1389
1390 bool WebEngine::SendKeyEvent( const KeyEvent& event )
1391 {
1392   return true;
1393 }
1394
1395 bool WebEngine::SendHoverEvent( const HoverEvent& event )
1396 {
1397   return true;
1398 }
1399
1400 bool WebEngine::SendWheelEvent( const WheelEvent& event )
1401 {
1402   return true;
1403 }
1404
1405 void WebEngine::SetFocus( bool focused )
1406 {
1407 }
1408
1409 void WebEngine::SetPageZoomFactor(float zoomFactor)
1410 {
1411   Internal::Adaptor::GetImplementation( *this ).SetPageZoomFactor(zoomFactor);
1412 }
1413
1414 float WebEngine::GetPageZoomFactor() const
1415 {
1416   return Internal::Adaptor::GetImplementation( *this ).GetPageZoomFactor();
1417 }
1418
1419 void WebEngine::SetTextZoomFactor(float zoomFactor)
1420 {
1421   Internal::Adaptor::GetImplementation( *this ).SetTextZoomFactor(zoomFactor);
1422 }
1423
1424 float WebEngine::GetTextZoomFactor() const
1425 {
1426   return Internal::Adaptor::GetImplementation( *this ).GetTextZoomFactor();
1427 }
1428
1429 float WebEngine::GetLoadProgressPercentage() const
1430 {
1431   return Internal::Adaptor::GetImplementation( *this ).GetLoadProgressPercentage();
1432 }
1433
1434 void WebEngine::UpdateDisplayArea( Dali::Rect< int > displayArea )
1435 {
1436 }
1437
1438 void WebEngine::EnableVideoHole( bool enabled )
1439 {
1440 }
1441
1442 void WebEngine::EnableMouseEvents( bool enabled )
1443 {
1444 }
1445
1446 void WebEngine::EnableKeyEvents( bool enabled )
1447 {
1448 }
1449
1450 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
1451 {
1452   return Internal::Adaptor::GetImplementation( *this ).PageLoadStartedSignal();
1453 }
1454
1455 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadInProgressSignal()
1456 {
1457   return Internal::Adaptor::GetImplementation( *this ).PageLoadInProgressSignal();
1458 }
1459
1460 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
1461 {
1462   return Internal::Adaptor::GetImplementation( *this ).PageLoadFinishedSignal();
1463 }
1464
1465 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
1466 {
1467   return Internal::Adaptor::GetImplementation( *this ).PageLoadErrorSignal();
1468 }
1469
1470 Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& WebEngine::ScrollEdgeReachedSignal()
1471 {
1472   return Internal::Adaptor::GetImplementation( *this ).ScrollEdgeReachedSignal();
1473 }
1474
1475 Dali::WebEnginePlugin::WebEngineUrlChangedSignalType& WebEngine::UrlChangedSignal()
1476 {
1477   return Internal::Adaptor::GetImplementation( *this ).UrlChangedSignal();
1478 }
1479
1480 Dali::WebEnginePlugin::WebEngineFormRepostDecisionSignalType& WebEngine::FormRepostDecisionSignal()
1481 {
1482   return Internal::Adaptor::GetImplementation( *this ).FormRepostDecisionSignal();
1483 }
1484
1485 Dali::WebEnginePlugin::WebEngineFrameRenderedSignalType& WebEngine::FrameRenderedSignal()
1486 {
1487   return Internal::Adaptor::GetImplementation(*this).FrameRenderedSignal();
1488 }
1489
1490 Dali::WebEnginePlugin::WebEngineRequestInterceptorSignalType& WebEngine::RequestInterceptorSignal()
1491 {
1492   return Internal::Adaptor::GetImplementation(*this).RequestInterceptorSignal();
1493 }
1494
1495 } // namespace Dali;
1496