Merge "Add APIs of webview back forward list" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-web-engine.cpp
index 132075a..0b8a473 100755 (executable)
@@ -23,6 +23,8 @@
 #include <dali/devel-api/adaptor-framework/web-engine-certificate.h>
 #include <dali/devel-api/adaptor-framework/web-engine-console-message.h>
 #include <dali/devel-api/adaptor-framework/web-engine-context.h>
+#include <dali/devel-api/adaptor-framework/web-engine-context-menu.h>
+#include <dali/devel-api/adaptor-framework/web-engine-context-menu-item.h>
 #include <dali/devel-api/adaptor-framework/web-engine-cookie-manager.h>
 #include <dali/devel-api/adaptor-framework/web-engine-form-repost-decision.h>
 #include <dali/devel-api/adaptor-framework/web-engine-frame.h>
@@ -30,6 +32,7 @@
 #include <dali/devel-api/adaptor-framework/web-engine-load-error.h>
 #include <dali/devel-api/adaptor-framework/web-engine-policy-decision.h>
 #include <dali/devel-api/adaptor-framework/web-engine-request-interceptor.h>
+#include <dali/devel-api/adaptor-framework/web-engine-security-origin.h>
 #include <dali/devel-api/adaptor-framework/web-engine-settings.h>
 #include <dali/public-api/adaptor-framework/native-image-source.h>
 #include <dali/public-api/images/pixel-data.h>
@@ -49,14 +52,16 @@ namespace Adaptor
 {
 
 class WebEngine;
+class MockWebEngineContext;
 
 namespace
 {
 
 // Generally only one WebEngine instance exists.
 // If > 1, a new web engine has been created by CreateWindowSignal.
-static WebEngine* gInstance = 0;
+static WebEngine* gInstance = nullptr;
 static int gInstanceCount = 0;
+static MockWebEngineContext* gWebEngineContextInstance = nullptr;
 
 bool OnGoBack();
 bool OnGoForward();
@@ -70,6 +75,11 @@ bool OnScreenshotCaptured();
 bool OnVideoPlaying();
 bool OnGeolocationPermission();
 bool OnClearHistory();
+bool OnSecurityOriginAcquired();
+bool OnStorageUsageAcquired();
+bool OnFormPasswordAcquired();
+bool OnDownloadStarted();
+bool OnMimeOverridden();
 
 static void ConnectToGlobalSignal( bool ( *func )() )
 {
@@ -114,12 +124,52 @@ public:
   {
   }
 
-  void DeleteWebDatabase() override
+  void DeleteAllWebDatabase() override
   {
   }
 
-  void DeleteWebStorage() override
+  bool GetWebDatabaseOrigins(Dali::WebEngineContext::WebEngineSecurityOriginAcquiredCallback callback)
   {
+    if (callback)
+    {
+      ConnectToGlobalSignal(&OnSecurityOriginAcquired);
+      mSecurityOriginAcquiredCallback = callback;
+    }
+    return true;
+  }
+
+  bool DeleteWebDatabase(Dali::WebEngineSecurityOrigin& origin)
+  {
+    return true;
+  }
+
+  bool GetWebStorageOrigins(Dali::WebEngineContext::WebEngineSecurityOriginAcquiredCallback callback)
+  {
+    if (callback)
+    {
+      ConnectToGlobalSignal(&OnSecurityOriginAcquired);
+      mSecurityOriginAcquiredCallback = callback;
+    }
+    return true;
+  }
+
+  bool GetWebStorageUsageForOrigin(Dali::WebEngineSecurityOrigin& origin, Dali::WebEngineContext::WebEngineStorageUsageAcquiredCallback callback) 
+  {
+    if (callback)
+    {
+      ConnectToGlobalSignal(&OnStorageUsageAcquired);
+      mStorageUsageAcquiredCallback = callback;
+    }
+    return true;
+  }
+
+  void DeleteAllWebStorage() override
+  {
+  }
+
+  bool DeleteWebStorageOrigin(Dali::WebEngineSecurityOrigin& origin)
+  {
+    return true;
   }
 
   void DeleteLocalFileSystem() override
@@ -134,10 +184,73 @@ public:
   {
   }
 
+  bool DeleteApplicationCache(Dali::WebEngineSecurityOrigin& origin)
+  {
+    return true;
+  }
+
+  void GetFormPasswordList(Dali::WebEngineContext::WebEngineFormPasswordAcquiredCallback callback)
+  {
+    if (callback)
+    {
+      ConnectToGlobalSignal(&OnFormPasswordAcquired);
+      mFormPasswordAcquiredCallback = callback;
+    }
+  }
+
+  void RegisterDownloadStartedCallback(Dali::WebEngineContext::WebEngineDownloadStartedCallback callback)
+  {
+    if (callback)
+    {
+      ConnectToGlobalSignal(&OnDownloadStarted);
+      mDownloadStartedCallback = callback;
+    }
+  }
+
+  void RegisterMimeOverriddenCallback(Dali::WebEngineContext::WebEngineMimeOverriddenCallback callback)
+  {
+    if (callback)
+    {
+      ConnectToGlobalSignal(&OnMimeOverridden);
+      mMimeOverriddenCallback = callback;
+    }
+  }
+
+public:
+  Dali::WebEngineContext::WebEngineSecurityOriginAcquiredCallback mSecurityOriginAcquiredCallback;
+  Dali::WebEngineContext::WebEngineStorageUsageAcquiredCallback mStorageUsageAcquiredCallback;
+  Dali::WebEngineContext::WebEngineFormPasswordAcquiredCallback mFormPasswordAcquiredCallback;
+  Dali::WebEngineContext::WebEngineDownloadStartedCallback mDownloadStartedCallback;
+  Dali::WebEngineContext::WebEngineMimeOverriddenCallback mMimeOverriddenCallback;
+
 private:
   Dali::WebEngineContext::CacheModel mockModel;
 };
 
+class MockWebEngineSecurityOrigin : public Dali::WebEngineSecurityOrigin
+{
+public:
+  MockWebEngineSecurityOrigin()
+    : mockUrl("https://test.html")
+    , mockPotocol("https")
+  {
+  }
+
+  std::string GetHost() const
+  {
+    return mockUrl;
+  }
+
+  std::string GetProtocol() const
+  {
+    return mockPotocol;
+  }
+
+private:
+  std::string mockUrl;
+  std::string mockPotocol;
+};
+
 class MockWebEngineCookieManager : public Dali::WebEngineCookieManager
 {
 public:
@@ -487,6 +600,107 @@ private:
   std::string mockUrl;
 };
 
+class MockWebEngineContextMenuItem : public Dali::WebEngineContextMenuItem
+{
+public:
+  MockWebEngineContextMenuItem()
+  {
+  }
+
+  ItemTag GetTag() const override
+  {
+    return ItemTag::NO_ACTION;
+  }
+
+  ItemType GetType() const override
+  {
+    return ItemType::ACTION;
+  }
+
+  bool IsEnabled() const override
+  {
+    return true;
+  }
+
+  std::string GetLinkUrl() const override
+  {
+    return "http://test.html";
+  }
+
+  std::string GetImageUrl() const override
+  {
+    return "http://test.jpg";
+  }
+
+  std::string GetTitle() const override
+  {
+    return "title";
+  }
+
+  std::unique_ptr<Dali::WebEngineContextMenu> GetParentMenu() const override
+  {
+    std::unique_ptr<Dali::WebEngineContextMenu> result;
+    return result;
+  }
+};
+
+class MockWebEngineContextMenu : public Dali::WebEngineContextMenu
+{
+public:
+  MockWebEngineContextMenu()
+  {
+  }
+
+  uint32_t GetItemCount() const override
+  {
+    return 1;
+  }
+
+  std::unique_ptr<Dali::WebEngineContextMenuItem> GetItemAt(uint32_t index) const override
+  {
+    std::unique_ptr<Dali::WebEngineContextMenuItem> webitem(new MockWebEngineContextMenuItem());
+    return webitem;
+  }
+
+  std::vector<std::unique_ptr<WebEngineContextMenuItem>> GetItemList() const override
+  {
+    std::vector<std::unique_ptr<WebEngineContextMenuItem>> result;
+    std::unique_ptr<Dali::WebEngineContextMenuItem> webitem(new MockWebEngineContextMenuItem());
+    result.push_back(std::move(webitem));
+    return result;
+  }
+
+  Dali::Vector2 GetPosition() const override
+  {
+    return Dali::Vector2(100, 100);
+  }
+
+  bool RemoveItem(WebEngineContextMenuItem& item) override
+  {
+    return true;
+  }
+
+  bool AppendItemAsAction(WebEngineContextMenuItem::ItemTag tag, const std::string& title, bool enabled) override
+  {
+    return true;
+  }
+
+  bool AppendItem(WebEngineContextMenuItem::ItemTag tag, const std::string& title, const std::string& iconFile, bool enabled) override
+  {
+    return true;
+  }
+
+  bool SelectItem(WebEngineContextMenuItem& item) override
+  {
+    return true;
+  }
+
+  bool Hide() override
+  {
+    return true;
+  }
+};
+
 class MockWebEngineSettings : public WebEngineSettings
 {
 public:
@@ -743,7 +957,12 @@ public:
     }
 
     mockWebEngineSettings = new MockWebEngineSettings();
-    mockWebEngineContext = new MockWebEngineContext();
+    MockWebEngineContext* engineContext = new MockWebEngineContext();
+    mockWebEngineContext = engineContext;
+    if ( gInstanceCount == 1 )
+    {
+      gWebEngineContextInstance = engineContext;
+    }
     mockWebEngineCookieManager = new MockWebEngineCookieManager();
     mockWebEngineBackForwardList = new MockWebEngineBackForwardList();
   }
@@ -754,6 +973,7 @@ public:
     if( !gInstanceCount )
     {
       gInstance = 0;
+      gWebEngineContextInstance = 0;
     }
 
     delete mockWebEngineSettings;
@@ -1082,25 +1302,37 @@ public:
     return mHttpAuthHandlerSignal;
   }
 
+  Dali::WebEnginePlugin::WebEngineContextMenuCustomizedSignalType& ContextMenuCustomizedSignal()
+  {
+    return mContextMenuCustomizedSignal;
+  }
+
+  Dali::WebEnginePlugin::WebEngineContextMenuItemSelectedSignalType& ContextMenuItemSelectedSignal()
+  {
+    return mContextMenuItemSelectedSignal;
+  }
+
   std::string              mUrl;
   std::vector<std::string> mHistory;
   size_t                   mCurrentPlusOnePos;
   std::string              mUserAgent;
 
-  Dali::WebEnginePlugin::WebEnginePageLoadSignalType           mPageLoadStartedSignal;
-  Dali::WebEnginePlugin::WebEnginePageLoadSignalType           mPageLoadInProgressSignal;
-  Dali::WebEnginePlugin::WebEnginePageLoadSignalType           mPageLoadFinishedSignal;
-  Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType      mPageLoadErrorSignal;
-  Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType  mScrollEdgeReachedSignal;
-  Dali::WebEnginePlugin::WebEngineUrlChangedSignalType         mUrlChangedSignal;
-  Dali::WebEnginePlugin::WebEngineFormRepostDecisionSignalType mFormRepostDecisionSignal;
-  Dali::WebEnginePlugin::WebEngineFrameRenderedSignalType      mFrameRenderedSignal;
-  Dali::WebEnginePlugin::WebEngineRequestInterceptorSignalType mRequestInterceptorSignal;
-  Dali::WebEnginePlugin::WebEngineConsoleMessageSignalType     mConsoleMessageSignal;
-  Dali::WebEnginePlugin::WebEnginePolicyDecisionSignalType     mPolicyDecisionSignal;
-  Dali::WebEnginePlugin::WebEngineCertificateSignalType        mCertificateConfirmSignal;
-  Dali::WebEnginePlugin::WebEngineCertificateSignalType        mSslCertificateChangedSignal;
-  Dali::WebEnginePlugin::WebEngineHttpAuthHandlerSignalType    mHttpAuthHandlerSignal;
+  Dali::WebEnginePlugin::WebEnginePageLoadSignalType                mPageLoadStartedSignal;
+  Dali::WebEnginePlugin::WebEnginePageLoadSignalType                mPageLoadInProgressSignal;
+  Dali::WebEnginePlugin::WebEnginePageLoadSignalType                mPageLoadFinishedSignal;
+  Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType           mPageLoadErrorSignal;
+  Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType       mScrollEdgeReachedSignal;
+  Dali::WebEnginePlugin::WebEngineUrlChangedSignalType              mUrlChangedSignal;
+  Dali::WebEnginePlugin::WebEngineFormRepostDecisionSignalType      mFormRepostDecisionSignal;
+  Dali::WebEnginePlugin::WebEngineFrameRenderedSignalType           mFrameRenderedSignal;
+  Dali::WebEnginePlugin::WebEngineRequestInterceptorSignalType      mRequestInterceptorSignal;
+  Dali::WebEnginePlugin::WebEngineConsoleMessageSignalType          mConsoleMessageSignal;
+  Dali::WebEnginePlugin::WebEnginePolicyDecisionSignalType          mPolicyDecisionSignal;
+  Dali::WebEnginePlugin::WebEngineCertificateSignalType             mCertificateConfirmSignal;
+  Dali::WebEnginePlugin::WebEngineCertificateSignalType             mSslCertificateChangedSignal;
+  Dali::WebEnginePlugin::WebEngineHttpAuthHandlerSignalType         mHttpAuthHandlerSignal;
+  Dali::WebEnginePlugin::WebEngineContextMenuCustomizedSignalType   mContextMenuCustomizedSignal;
+  Dali::WebEnginePlugin::WebEngineContextMenuItemSelectedSignalType mContextMenuItemSelectedSignal;
 
   bool  mEvaluating;
   float mPageZoomFactor;
@@ -1186,6 +1418,11 @@ bool OnLoadUrl()
     gInstance->mSslCertificateChangedSignal.Emit(std::move(sslCertificate));
     std::shared_ptr<Dali::WebEngineHttpAuthHandler> handler(new MockWebEngineHttpAuthHandler());
     gInstance->mHttpAuthHandlerSignal.Emit(std::move(handler));
+
+    std::shared_ptr<Dali::WebEngineContextMenu> menu(new MockWebEngineContextMenu());
+    gInstance->mContextMenuCustomizedSignal.Emit(std::move(menu));
+    std::shared_ptr<Dali::WebEngineContextMenuItem> item(new MockWebEngineContextMenuItem());
+    gInstance->mContextMenuItemSelectedSignal.Emit(std::move(item));
   }
   return false;
 }
@@ -1296,6 +1533,65 @@ bool OnClearHistory()
   return false;
 }
 
+bool OnSecurityOriginAcquired()
+{
+  DisconnectFromGlobalSignal(&OnSecurityOriginAcquired);
+  if (gWebEngineContextInstance)
+  {
+    std::vector<std::unique_ptr<Dali::WebEngineSecurityOrigin>> securityOriginList;
+    std::unique_ptr<Dali::WebEngineSecurityOrigin> origin(new MockWebEngineSecurityOrigin());
+    securityOriginList.push_back(std::move(origin));
+    gWebEngineContextInstance->mSecurityOriginAcquiredCallback(securityOriginList);
+  }
+  return false;
+}
+
+bool OnStorageUsageAcquired()
+{
+  DisconnectFromGlobalSignal(&OnStorageUsageAcquired);
+  if (gWebEngineContextInstance)
+  {
+    gWebEngineContextInstance->mStorageUsageAcquiredCallback(0);
+  }
+  return false;
+}
+
+bool OnFormPasswordAcquired()
+{
+  DisconnectFromGlobalSignal(&OnFormPasswordAcquired);
+  if (gWebEngineContextInstance)
+  {
+    std::vector<std::unique_ptr<Dali::WebEngineContext::PasswordData>> formPasswordList;
+    std::unique_ptr<Dali::WebEngineContext::PasswordData> data(new Dali::WebEngineContext::PasswordData());
+    data->url = "http://test.html";
+    data->useFingerprint = false;
+    formPasswordList.push_back(std::move(data));
+    gWebEngineContextInstance->mFormPasswordAcquiredCallback(formPasswordList);
+  }
+  return false;
+}
+
+bool OnDownloadStarted()
+{
+  DisconnectFromGlobalSignal(&OnDownloadStarted);
+  if (gWebEngineContextInstance)
+  {
+    gWebEngineContextInstance->mDownloadStartedCallback("http://test.html");
+  }
+  return false;
+}
+
+bool OnMimeOverridden()
+{
+  DisconnectFromGlobalSignal(&OnMimeOverridden);
+  if (gWebEngineContextInstance)
+  {
+    std::string newMime;
+    gWebEngineContextInstance->mMimeOverriddenCallback("http://test.html", "txt/xml", newMime);
+  }
+  return false;
+}
+
 } // namespace
 
 inline WebEngine& GetImplementation( Dali::WebEngine& webEngine )
@@ -1783,5 +2079,15 @@ Dali::WebEnginePlugin::WebEngineHttpAuthHandlerSignalType& WebEngine::HttpAuthHa
   return Internal::Adaptor::GetImplementation(*this).HttpAuthHandlerSignal();
 }
 
+Dali::WebEnginePlugin::WebEngineContextMenuCustomizedSignalType& WebEngine::ContextMenuCustomizedSignal()
+{
+  return Internal::Adaptor::GetImplementation( *this ).ContextMenuCustomizedSignal();
+}
+
+Dali::WebEnginePlugin::WebEngineContextMenuItemSelectedSignalType& WebEngine::ContextMenuItemSelectedSignal()
+{
+  return Internal::Adaptor::GetImplementation( *this ).ContextMenuItemSelectedSignal();
+}
+
 } // namespace Dali;