Truly independent web browser
test-web: Reset a view’s notification state between tests
sideshowbarker6cf96f2fde5c
diff --git a/Libraries/LibWebView/RecordingNotificationBackend.cpp b/Libraries/LibWebView/RecordingNotificationBackend.cppindex 8a947bb2db4..75fa7b93ee2 100644--- a/Libraries/LibWebView/RecordingNotificationBackend.cpp+++ b/Libraries/LibWebView/RecordingNotificationBackend.cpp@@ -79,6 +79,14 @@ void RecordingNotificationBackend::record_open(URL::URL const& url, Web::PageIdm_opened.append({ url, originating_page });}+void RecordingNotificationBackend::forget_page(Web::PageId page_id)+{+ m_shown.remove_all_matching([&](auto const& notification) { return notification.page_id == page_id; });+ m_closed.remove_all_matching([&](auto const& entry) { return entry.page_id == page_id; });+ m_focused.remove_all_matching([&](auto const& focused) { return focused == page_id; });+ m_opened.remove_all_matching([&](auto const& entry) { return entry.page_id == page_id; });+}+String RecordingNotificationBackend::state_json(Web::PageId page_id) const{JsonArray shown;diff --git a/Libraries/LibWebView/RecordingNotificationBackend.h b/Libraries/LibWebView/RecordingNotificationBackend.hindex 21e9c53e96f..e4a03f20a66 100644--- a/Libraries/LibWebView/RecordingNotificationBackend.h+++ b/Libraries/LibWebView/RecordingNotificationBackend.h@@ -35,6 +35,8 @@ public:// Everything above, for the notifications of one page, as JSON for a test page to read. Tests run side by side in// one UI process, so each sees only its own.String state_json(Web::PageId) const;+ // Forgets a page's history, since test-web hands the page id to the next test it runs in the same view.+ void forget_page(Web::PageId);// When set, the next show() fails instead, as a platform that refuses the notification would.bool fail_next_show { false };diff --git a/Tests/LibWeb/test-web/TestWebView.cpp b/Tests/LibWeb/test-web/TestWebView.cppindex a406f0afc94..3a5d404d860 100644--- a/Tests/LibWeb/test-web/TestWebView.cpp+++ b/Tests/LibWeb/test-web/TestWebView.cpp@@ -11,7 +11,10 @@#include <LibCore/AnonymousBuffer.h>#include <LibGfx/Bitmap.h>#include <LibGfx/ShareableBitmap.h>+#include <LibWebView/NotificationManager.h>#include <LibWebView/PermissionManager.h>+#include <LibWebView/RecordingNotificationBackend.h>+#include <LibWebView/WebContentPage.h>namespace TestWeb {@@ -66,6 +69,15 @@ void TestWebView::reset_permissions()reset_permission_prompts_for_testing();}+// A test's notifications live in the UI process, keyed by a page id the next test in this view gets too, so they may+// not reach the next test.+void TestWebView::reset_notifications()+{+ WebView::NotificationManager::the().reset_page_for_testing(page());+ if (auto* recording_backend = WebView::NotificationManager::the().backend().as_recording_backend())+ recording_backend->forget_page(page().id());+}+NonnullRefPtr<Core::Promise<Empty>> TestWebView::reset_state_for_next_test(){return WebView::ViewImplementation::reset_state_for_testing();diff --git a/Tests/LibWeb/test-web/TestWebView.h b/Tests/LibWeb/test-web/TestWebView.hindex a82b8de6792..6172b84cbc9 100644--- a/Tests/LibWeb/test-web/TestWebView.h+++ b/Tests/LibWeb/test-web/TestWebView.h@@ -28,6 +28,7 @@ public:void reset_user_style();void reset_geolocation_emulated_position();void reset_permissions();+ void reset_notifications();NonnullRefPtr<Core::Promise<Empty>> reset_state_for_next_test();NonnullRefPtr<Core::Promise<RefPtr<Gfx::Bitmap const>>> take_screenshot();diff --git a/Tests/LibWeb/test-web/main.cpp b/Tests/LibWeb/test-web/main.cppindex 34ecbe5ef8d..7c8d68e9da7 100644--- a/Tests/LibWeb/test-web/main.cpp+++ b/Tests/LibWeb/test-web/main.cpp@@ -1331,6 +1331,7 @@ static ErrorOr<int> run_tests(Core::AnonymousBuffer const& theme, Web::DevicePixview->reset_user_style();view->reset_geolocation_emulated_position();view->reset_permissions();+ view->reset_notifications();view->reset_viewport_size(window_size);}