Truly independent web browser
Tests: Wait for the notification state a test checks, not a timer
sideshowbarkerff0a96fb2bc7
diff --git a/Libraries/LibWebView/NotificationManager.cpp b/Libraries/LibWebView/NotificationManager.cppindex 15dcdcc912c..7f9cecac958 100644--- a/Libraries/LibWebView/NotificationManager.cpp+++ b/Libraries/LibWebView/NotificationManager.cpp@@ -122,6 +122,16 @@ void NotificationManager::close(WebContentPage& page, u64 page_notification_id)backend().close(m_records[*index].id);}+Vector<u64> NotificationManager::ids_without_a_page_for_testing(Web::PageId page_id) const+{+ Vector<u64> ids;+ for (auto const& record : m_records) {+ if (record.page_id == page_id && !record.page)+ ids.append(record.id);+ }+ return ids;+}+void NotificationManager::abandon(WebContentPage& page, u64 page_notification_id){if (auto index = index_of(page, page_notification_id); index.has_value())diff --git a/Libraries/LibWebView/NotificationManager.h b/Libraries/LibWebView/NotificationManager.hindex c55d180e090..6a51aa76f4f 100644--- a/Libraries/LibWebView/NotificationManager.h+++ b/Libraries/LibWebView/NotificationManager.h@@ -53,6 +53,8 @@ public:// The manager's id of a page's notification, for tests that act on it through the backend.Optional<u64> id_for(WebContentPage const&, u64 page_notification_id) const;+ // The ids of a page's notifications whose document has gone, so a test can wait for the page to have said so.+ Vector<u64> ids_without_a_page_for_testing(Web::PageId) const;private:struct Record {diff --git a/Libraries/LibWebView/RecordingNotificationBackend.cpp b/Libraries/LibWebView/RecordingNotificationBackend.cppindex 75fa7b93ee2..f9feacb3ff4 100644--- a/Libraries/LibWebView/RecordingNotificationBackend.cpp+++ b/Libraries/LibWebView/RecordingNotificationBackend.cpp@@ -87,7 +87,7 @@ void RecordingNotificationBackend::forget_page(Web::PageId 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+String RecordingNotificationBackend::state_json(Web::PageId page_id, Vector<u64> const& abandoned) const{JsonArray shown;for (auto const& notification : m_shown) {@@ -129,11 +129,16 @@ String RecordingNotificationBackend::state_json(Web::PageId page_id) constopened.must_append(entry.url.serialize());}+ JsonArray abandoned_ids;+ for (auto id : abandoned)+ abandoned_ids.must_append(id);+JsonObject state;state.set("shown"sv, move(shown));state.set("closed"sv, move(closed));state.set("focused"sv, move(focused));state.set("opened"sv, move(opened));+ state.set("abandoned"sv, move(abandoned_ids));return state.serialized();}diff --git a/Libraries/LibWebView/RecordingNotificationBackend.h b/Libraries/LibWebView/RecordingNotificationBackend.hindex e4a03f20a66..39cb85b7c10 100644--- a/Libraries/LibWebView/RecordingNotificationBackend.h+++ b/Libraries/LibWebView/RecordingNotificationBackend.h@@ -34,7 +34,7 @@ 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;+ String state_json(Web::PageId, Vector<u64> const& abandoned) 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);diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cppindex 5e02ce88bae..95c8fc85b2b 100644--- a/Libraries/LibWebView/WebContentClient.cpp+++ b/Libraries/LibWebView/WebContentClient.cpp@@ -652,7 +652,7 @@ Messages::WebContentClient::DidRequestAllCookiesCookiestoreResponse WebContentClMessages::WebContentClient::DidRequestNotificationsStateForTestingResponse WebContentClient::did_request_notifications_state_for_testing(Web::PageId page_id){if (auto* recording_backend = NotificationManager::the().backend().as_recording_backend())- return recording_backend->state_json(page_id);+ return recording_backend->state_json(page_id, NotificationManager::the().ids_without_a_page_for_testing(page_id));return "{}"_string;}diff --git a/Tests/LibWeb/Text/input/notifications/cap-replaces-oldest.html b/Tests/LibWeb/Text/input/notifications/cap-replaces-oldest.htmlindex 8bf956e619b..080cf66816b 100644--- a/Tests/LibWeb/Text/input/notifications/cap-replaces-oldest.html+++ b/Tests/LibWeb/Text/input/notifications/cap-replaces-oldest.html@@ -9,16 +9,17 @@let closes = 0;let firstClosed = null;const notifications = [];- const allShown = new Promise(resolve => {- for (let i = 0; i < total; ++i) {- const notification = new Notification(`n${i}`, { tag: `t${i}` });- notification.onshow = () => { if (++shows === total) resolve(); };- notification.onclose = () => { firstClosed ??= i; ++closes; };- notifications.push(notification);- }- });- await allShown;- await new Promise(resolve => setTimeout(resolve, 100));+ let allShown, allEvicted;+ const shown = new Promise(resolve => (allShown = resolve));+ const evicted = new Promise(resolve => (allEvicted = resolve));+ for (let i = 0; i < total; ++i) {+ const notification = new Notification(`n${i}`, { tag: `t${i}` });+ notification.onshow = () => { if (++shows === total) allShown(); };+ notification.onclose = () => { firstClosed ??= i; if (++closes === total - 8) allEvicted(); };+ notifications.push(notification);+ }+ await shown;+ await evicted;const state = JSON.parse(internals.notificationsStateForTesting());println(`shown: ${state.shown.length}`);println(`show events: ${shows}`);diff --git a/Tests/LibWeb/Text/input/notifications/dead-page-click-opens-origin.html b/Tests/LibWeb/Text/input/notifications/dead-page-click-opens-origin.htmlindex 574bfbed640..61a3fdafc63 100644--- a/Tests/LibWeb/Text/input/notifications/dead-page-click-opens-origin.html+++ b/Tests/LibWeb/Text/input/notifications/dead-page-click-opens-origin.html@@ -26,13 +26,21 @@return;}+ const stateWhen = async predicate => {+ for (;;) {+ const state = JSON.parse(internals.notificationsStateForTesting());+ if (predicate(state))+ return state;+ await new Promise(resolve => setTimeout(resolve, 10));+ }+ };+iframe.remove();- // Let the frame's document go away, and the UI process hear of it, before the click.- await new Promise(resolve => setTimeout(resolve, 100));+ // The frame's document goes away in a later task, and the UI process hears of it after that: Click only once+ // it has, or the click would still be delivered to the page.+ await stateWhen(state => state.abandoned.includes(id));internals.clickNotificationByIdForTesting(id);- await new Promise(resolve => setTimeout(resolve, 100));-- const state = JSON.parse(internals.notificationsStateForTesting());+ const state = await stateWhen(state => state.opened.length > 0 || state.focused.length > 0);println(`opened this page's site: ${state.opened.length === 1 && state.opened[0] === location.origin + "/"}`);println(`focused: ${state.focused.length}`);println(`still shown: ${state.shown.some(n => n.id === id)}`);diff --git a/Tests/LibWeb/Text/input/notifications/tag-replace-then-close-old.html b/Tests/LibWeb/Text/input/notifications/tag-replace-then-close-old.htmlindex d325251b3ea..bf156591559 100644--- a/Tests/LibWeb/Text/input/notifications/tag-replace-then-close-old.html+++ b/Tests/LibWeb/Text/input/notifications/tag-replace-then-close-old.html@@ -7,11 +7,13 @@const a = new Notification("A", { tag: "x" });await new Promise(resolve => (a.onshow = resolve));let closes = 0;- a.onclose = () => closes++;+ const aClosed = new Promise(resolve => (a.onclose = () => { ++closes; resolve(); }));const b = new Notification("B", { tag: "x" });a.close();await new Promise(resolve => (b.onshow = resolve));- await new Promise(resolve => setTimeout(resolve, 100));+ await aClosed;+ // A second close, were one to come, would be queued by now; let it run before counting.+ await new Promise(resolve => setTimeout(resolve, 0));println(`close events on a: ${closes}`);b.close();done();