From bfb444b3342ea30f045af1c624d94c4bb77dbbf1 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Sat, 3 Oct 2026 21:44:05 -0400 Subject: [PATCH] tests: Fix flaky OwnedWidgetShutdownTest.SyntheticOwnedWindow The owned menu window could be created before the main window was activated. When the main window became active later, the menu was dismissed (e.g. Wayland popups), so owned_window_ was null by the time CloseMainWindow ran. This happened reliably on repeat iterations, where cached loads make OnLoadEnd fire sooner. Forward CefWindowDelegate::OnWindowActivationChanged from TestWindowDelegate to a new TestHandler::OnWindowActivationChanged hook, and wait until the load has ended and the main window is active before creating the owned window. --- .../owned_widget_shutdown_unittest.cc | 19 +++++++++++++++++++ tests/ceftests/test_handler.cc | 5 +++++ tests/ceftests/test_handler.h | 1 + 3 files changed, 25 insertions(+) diff --git a/tests/ceftests/owned_widget_shutdown_unittest.cc b/tests/ceftests/owned_widget_shutdown_unittest.cc index 028167e68..118480844 100644 --- a/tests/ceftests/owned_widget_shutdown_unittest.cc +++ b/tests/ceftests/owned_widget_shutdown_unittest.cc @@ -312,6 +312,23 @@ class SyntheticOwnedWindowTestHandler : public TestHandler, CefGetChromeBrowserOwnedWidgetCountForTests(browser); ASSERT_GE(initial_owned_widget_count_, 0); + load_ended_ = true; + MaybeCreateOwnedWindow(); + } + + void OnWindowActivationChanged(int browser_id, bool active) override { + main_window_active_ = active; + MaybeCreateOwnedWindow(); + } + + // Wait for the main window to become active before creating the owned menu + // window. Otherwise, later activation of the main window may dismiss the + // menu (e.g. Wayland popups) before the test closes the main window. + void MaybeCreateOwnedWindow() { + if (!load_ended_ || !main_window_active_ || owned_window_delegate_) { + return; + } + owned_window_delegate_ = new OwnedWindowDelegate(main_window_, this); CefWindow::CreateTopLevelWindow(owned_window_delegate_); } @@ -380,6 +397,8 @@ class SyntheticOwnedWindowTestHandler : public TestHandler, CefRefPtr owned_window_delegate_; int initial_owned_widget_count_ = -1; int pending_browser_id_ = 0; + bool load_ended_ = false; + bool main_window_active_ = false; IMPLEMENT_REFCOUNTING(SyntheticOwnedWindowTestHandler); }; diff --git a/tests/ceftests/test_handler.cc b/tests/ceftests/test_handler.cc index d6d4db651..937d1742c 100644 --- a/tests/ceftests/test_handler.cc +++ b/tests/ceftests/test_handler.cc @@ -72,6 +72,11 @@ class TestWindowDelegate : public CefWindowDelegate { handler_->OnWindowDestroyed(browser_id_); } + void OnWindowActivationChanged(CefRefPtr window, + bool active) override { + handler_->OnWindowActivationChanged(browser_id_, active); + } + bool CanClose(CefRefPtr window) override { // Allow the window to close if the browser says it's OK. CefRefPtr browser = browser_view_->GetBrowser(); diff --git a/tests/ceftests/test_handler.h b/tests/ceftests/test_handler.h index 101d9332d..cba3bf960 100644 --- a/tests/ceftests/test_handler.h +++ b/tests/ceftests/test_handler.h @@ -215,6 +215,7 @@ class TestHandler : public CefClient, // Called from TestWindowDelegate when Views is enabled. void OnWindowCreated(int browser_id); virtual void OnWindowDestroyed(int browser_id); + virtual void OnWindowActivationChanged(int browser_id, bool active) {} // Returns the count of TestHandlers the currently exist. static size_t GetTestHandlerCount() {