diff --git a/ohos_nweb/src/cef_delegate/nweb_delegate_unittest.cc b/ohos_nweb/src/cef_delegate/nweb_delegate_unittest.cc index f10cb4fe91dec1693dd3035caa454b88a162c0c4..077609577b9ae59e645e13f2adfb73a7d8720f88 100644 --- a/ohos_nweb/src/cef_delegate/nweb_delegate_unittest.cc +++ b/ohos_nweb/src/cef_delegate/nweb_delegate_unittest.cc @@ -436,4 +436,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 786ff42e719a185f325f93f01a1b9b6e6603a767..5ed021d7cc0cb298c47c062e029209add6f9a722 100644 --- a/ohos_nweb/src/nweb_impl_unittest.cc +++ b/ohos_nweb/src/nweb_impl_unittest.cc @@ -777,6 +777,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 { @@ -1616,4 +1621,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 3202ae50abef3a98ffb93c7f9ba42de692d948dc..4e4cb3ec8df98c07cca88e6c75f885d502cfa6f3 100644 --- a/ohos_nweb/src/nweb_input_handler_unittest.cc +++ b/ohos_nweb/src/nweb_input_handler_unittest.cc @@ -771,6 +771,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 }; 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; }