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