From feff63c184817296f531da9ce4ea4ad1cfbba7fd Mon Sep 17 00:00:00 2001 From: tengfan Date: Wed, 18 Dec 2024 10:14:43 +0800 Subject: [PATCH] =?UTF-8?q?beforeunload=E8=A1=A5=E5=85=85UT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tengfan --- .../cef_delegate/nweb_delegate_unittest.cc | 14 +++++++ .../nweb_event_handler_unittest.cc | 2 + .../nweb_handler_delegate_unittest.cc | 2 + .../nweb_preference_delegate_unittest.cc | 2 + ohos_nweb/src/nweb_impl_unittest.cc | 41 ++++++++++++++++++- ohos_nweb/src/nweb_input_handler_unittest.cc | 4 ++ .../src/nweb_inputmethod_handler_unittest.cc | 2 + 7 files changed, 66 insertions(+), 1 deletion(-) diff --git a/ohos_nweb/src/cef_delegate/nweb_delegate_unittest.cc b/ohos_nweb/src/cef_delegate/nweb_delegate_unittest.cc index 24753cace3..cd6f9c8899 100644 --- a/ohos_nweb/src/cef_delegate/nweb_delegate_unittest.cc +++ b/ohos_nweb/src/cef_delegate/nweb_delegate_unittest.cc @@ -13,7 +13,9 @@ * limitations under the License. */ +#define private public #include "nweb_delegate.h" +#undef private #include "nweb.h" #include "nweb_delegate_adapter.h" #include "nweb_delegate_interface.h" @@ -436,4 +438,16 @@ TEST_F(NWebDelegateTest, ExecuteCreatePDFExt) { nweb_delegate_->ExecuteCreatePDFExt(pdfConfig, callback); } +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) +TEST_F(NWebDelegateTest, NeedToFireBeforeUnloadOrUnloadEvents) { + auto result = nweb_delegate_->NeedToFireBeforeUnloadOrUnloadEvents(); + EXPECT_FALSE(result); +} +#endif + +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) +TEST_F(NWebDelegateTest, DispatchBeforeUnload) { + nweb_delegate_->DispatchBeforeUnload(); +} +#endif } // namespace OHOS::NWeb diff --git a/ohos_nweb/src/cef_delegate/nweb_event_handler_unittest.cc b/ohos_nweb/src/cef_delegate/nweb_event_handler_unittest.cc index 6cdd1ea088..2c57ff2a56 100644 --- a/ohos_nweb/src/cef_delegate/nweb_event_handler_unittest.cc +++ b/ohos_nweb/src/cef_delegate/nweb_event_handler_unittest.cc @@ -640,6 +640,8 @@ class MockCefBrowser : public CefBrowser { return 0; } void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) override {} + bool NeedToFireBeforeUnloadOrUnloadEvents() override { return false; } + void DispatchBeforeUnload() override {} #endif // BUILDFLAG(IS_OHOS) private: CefRefPtr host_; diff --git a/ohos_nweb/src/cef_delegate/nweb_handler_delegate_unittest.cc b/ohos_nweb/src/cef_delegate/nweb_handler_delegate_unittest.cc index d2693247f8..9a177f2a66 100644 --- a/ohos_nweb/src/cef_delegate/nweb_handler_delegate_unittest.cc +++ b/ohos_nweb/src/cef_delegate/nweb_handler_delegate_unittest.cc @@ -214,6 +214,8 @@ class MockCefBrowser : public CefBrowser { return 0; } void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive) override {} + bool NeedToFireBeforeUnloadOrUnloadEvents() override { return false; } + void DispatchBeforeUnload() override {} #endif // BUILDFLAG(IS_OHOS) }; diff --git a/ohos_nweb/src/cef_delegate/nweb_preference_delegate_unittest.cc b/ohos_nweb/src/cef_delegate/nweb_preference_delegate_unittest.cc index ddb8b72fea..661eb51947 100644 --- a/ohos_nweb/src/cef_delegate/nweb_preference_delegate_unittest.cc +++ b/ohos_nweb/src/cef_delegate/nweb_preference_delegate_unittest.cc @@ -56,6 +56,8 @@ class MockCefBrowser : public CefBrowser, public CefBrowserHost { bool IsSame(CefRefPtr that) override { return false; } bool IsPopup() override { return false; } bool HasDocument() override { return false; } + bool NeedToFireBeforeUnloadOrUnloadEvents() override { return false; } + void DispatchBeforeUnload() override {} CefRefPtr GetMainFrame() override { return nullptr; } CefRefPtr GetFocusedFrame() override { return nullptr; } CefRefPtr GetFrame(int64 identifier) override { return nullptr; } diff --git a/ohos_nweb/src/nweb_impl_unittest.cc b/ohos_nweb/src/nweb_impl_unittest.cc index bfb79b3f51..5c6e940780 100644 --- a/ohos_nweb/src/nweb_impl_unittest.cc +++ b/ohos_nweb/src/nweb_impl_unittest.cc @@ -781,6 +781,11 @@ class MockNWebDelegate : public NWebDelegateInterface { MOCK_METHOD(void, WebExtensionContextMenuIsIframe, (), (override)); MOCK_METHOD(bool, WebExtensionContextMenuReloadFocusedFrame, (), (override)); #endif + +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) + MOCK_METHOD(bool, NeedToFireBeforeUnloadOrUnloadEvents, (), (override)); + MOCK_METHOD(void, DispatchBeforeUnload, (), (override)); +#endif // OHOS_DISPATCH_BEFORE_UNLOAD }; class MockNWebDragEvent : public NWebDragEvent { @@ -1620,4 +1625,38 @@ TEST_F(NWebImplTest, NWebImplTest_ExecuteCreatePDFExt_001) { nweb_impl_->ExecuteCreatePDFExt(pdfConfig, callback); } -} // namespace OHOS::NWeb \ No newline at end of file +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) +TEST_F(NWebImplTest, NeedToFireBeforeUnloadOrUnloadEvents_001) { + nweb_impl_->nweb_delegate_ = nullptr; + auto result = nweb_impl_->NeedToFireBeforeUnloadOrUnloadEvents(); + EXPECT_FALSE(result); +} +#endif // OHOS_DISPATCH_BEFORE_UNLOAD + +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) +TEST_F(NWebImplTest, NeedToFireBeforeUnloadOrUnloadEvents_002) { + nweb_impl_->nweb_delegate_ = mock_delegate_; + EXPECT_CALL(*mock_delegate_, NeedToFireBeforeUnloadOrUnloadEvents()) + .WillOnce(Return(true)); + auto result = nweb_impl_->NeedToFireBeforeUnloadOrUnloadEvents(); + EXPECT_TRUE(result); +} +#endif // OHOS_DISPATCH_BEFORE_UNLOAD + +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) +TEST_F(NWebImplTest, DispatchBeforeUnload_001) { + nweb_impl_->nweb_delegate_ = nullptr; + nweb_impl_->DispatchBeforeUnload(); + EXPECT_FALSE(nweb_impl_->nweb_delegate_); +} +#endif // OHOS_DISPATCH_BEFORE_UNLOAD + +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) +TEST_F(NWebImplTest, DispatchBeforeUnload_002) { + nweb_impl_->nweb_delegate_ = mock_delegate_; + EXPECT_CALL(*mock_delegate_, DispatchBeforeUnload()).Times(1); + nweb_impl_->DispatchBeforeUnload(); + EXPECT_TRUE(nweb_impl_->nweb_delegate_); +} +#endif // OHOS_DISPATCH_BEFORE_UNLOAD +} // namespace OHOS::NWeb diff --git a/ohos_nweb/src/nweb_input_handler_unittest.cc b/ohos_nweb/src/nweb_input_handler_unittest.cc index 021d4bc02b..1836196d70 100644 --- a/ohos_nweb/src/nweb_input_handler_unittest.cc +++ b/ohos_nweb/src/nweb_input_handler_unittest.cc @@ -775,6 +775,10 @@ class MockNWebDelegate : public NWebDelegateInterface { MOCK_METHOD(void, WebExtensionContextMenuIsIframe, (), (override)); MOCK_METHOD(bool, WebExtensionContextMenuReloadFocusedFrame, (), (override)); #endif +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) + MOCK_METHOD(bool, NeedToFireBeforeUnloadOrUnloadEvents, (), (override)); + MOCK_METHOD(void, DispatchBeforeUnload, (), (override)); +#endif }; class MockNWebTouchPointInfo : public NWebTouchPointInfo { diff --git a/ohos_nweb/src/nweb_inputmethod_handler_unittest.cc b/ohos_nweb/src/nweb_inputmethod_handler_unittest.cc index 4b4f1b9125..d3c4b0e605 100644 --- a/ohos_nweb/src/nweb_inputmethod_handler_unittest.cc +++ b/ohos_nweb/src/nweb_inputmethod_handler_unittest.cc @@ -51,6 +51,8 @@ class MockCefBrowser : public CefBrowser { bool IsSame(CefRefPtr that) override { return false; } bool IsPopup() override { return false; } bool HasDocument() override { return false; } + bool NeedToFireBeforeUnloadOrUnloadEvents() override { return false; } + void DispatchBeforeUnload() override {} CefRefPtr GetMainFrame() override { return nullptr; } CefRefPtr GetFocusedFrame() override { return nullptr; } CefRefPtr GetFrame(int64 identifier) override { return nullptr; } -- Gitee