diff --git a/ohos_nweb/src/cef_delegate/nweb_delegate_unittest.cc b/ohos_nweb/src/cef_delegate/nweb_delegate_unittest.cc index 24753cace3e5945453286995d6af3a35dc8ac131..cd6f9c88996fcfda74fee8e7a150a203d277b7de 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 6cdd1ea088491e6223f0e54f4f9ae6061bd0b672..2c57ff2a56df7ed980b5fb33dad4280aa165f110 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 d2693247f8f89a3bbe4a111b0944d0232ee69511..9a177f2a66c64568e1024ca292b123dbe4db8fb7 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 ddb8b72fea6244bc8a6530919ca13817da25c381..661eb51947224b098113d3a25b69e0a87d3f8034 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 bfb79b3f519e83d17669b2220e8b59660076ee07..5c6e9407808b671b3569421f9dd7d352f63ace2d 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 021d4bc02b14cd4f0fbe2c1e27029ae539afa495..1836196d700c514a35850a49f0cdfa265a714036 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 4b4f1b9125d0a1fe5b506e09d5fc0844c9ec1f6f..d3c4b0e605f486d316794a28180c442f1867f2ec 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; }