browser_tests: grant an NSApplicationActivationPolicy
To fix NativeAppWindowCocoaBrowserTest.FrameColor. This test needs
to activate windows. In order to do so an appropriate
NSApplicationActivationPolicy must be set.
This CL sets a process wide NSApplicationActivationPolicy of
NSApplicationActivationPolicyAccessory. Do this early on before any
tests are run. This will make seen any potential negative interactions
in a reproducible manner.
Bug: 1322741
Change-Id: Ie749ccd45b5da9af59905df14cd0276227280370
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3838824
Reviewed-by: Avi Drissman <avi@chromium.org>
Commit-Queue: Tom Burgin <bur@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1037258}
diff --git a/chrome/browser/notifications/notification_interactive_uitest_mac.mm b/chrome/browser/notifications/notification_interactive_uitest_mac.mm
index 4131d89..9b0aa058 100644
--- a/chrome/browser/notifications/notification_interactive_uitest_mac.mm
+++ b/chrome/browser/notifications/notification_interactive_uitest_mac.mm
@@ -27,12 +27,17 @@
{
base::scoped_nsobject<WindowedNSNotificationObserver> observer(
[[WindowedNSNotificationObserver alloc]
- initForNotification:NSApplicationDidResignActiveNotification
+ initForNotification:NSApplicationDidHideNotification
object:NSApp]);
[NSApp hide:nil];
[observer wait];
}
- EXPECT_FALSE([NSApp isActive]);
+ EXPECT_TRUE([NSApp isHidden]);
+
+ base::scoped_nsobject<WindowedNSNotificationObserver> observer(
+ [[WindowedNSNotificationObserver alloc]
+ initForNotification:NSApplicationDidUnhideNotification
+ object:NSApp]);
std::string result = CreateNotification(
browser(), true, "", "", "", "",
@@ -43,13 +48,8 @@
message_center::Notification* notification =
*message_center->GetVisibleNotifications().begin();
- {
- base::scoped_nsobject<WindowedNSNotificationObserver> observer(
- [[WindowedNSNotificationObserver alloc]
- initForNotification:NSApplicationDidBecomeActiveNotification
- object:NSApp]);
- message_center->ClickOnNotification(notification->id());
- [observer wait];
- }
- EXPECT_TRUE([NSApp isActive]);
+ message_center->ClickOnNotification(notification->id());
+ [observer wait];
+
+ EXPECT_FALSE([NSApp isHidden]);
}
diff --git a/chrome/browser/ui/cocoa/apps/native_app_window_cocoa_browsertest.mm b/chrome/browser/ui/cocoa/apps/native_app_window_cocoa_browsertest.mm
index dc92291..b1695e58 100644
--- a/chrome/browser/ui/cocoa/apps/native_app_window_cocoa_browsertest.mm
+++ b/chrome/browser/ui/cocoa/apps/native_app_window_cocoa_browsertest.mm
@@ -589,20 +589,28 @@
// Test that the colored frames have the correct color when active and inactive.
// Disabled; https://crbug.com/1322741.
-IN_PROC_BROWSER_TEST_F(NativeAppWindowCocoaBrowserTest, DISABLED_FrameColor) {
+IN_PROC_BROWSER_TEST_F(NativeAppWindowCocoaBrowserTest, FrameColor) {
+ EXPECT_EQ(NSApp.activationPolicy, NSApplicationActivationPolicyAccessory);
+
// The hex values indicate an RGB color. When we get the NSColor later, the
// components are CGFloats in the range [0, 1].
extensions::AppWindow* app_window = CreateTestAppWindow(
"{\"frame\": {\"color\": \"#FF0000\", \"inactiveColor\": \"#0000FF\"}}");
NSWindow* ns_window = app_window->GetNativeWindow().GetNativeNSWindow();
+
// No color correction in the default case.
[ns_window setColorSpace:[NSColorSpace sRGBColorSpace]];
- int half_width = NSWidth([ns_window frame]) / 2;
+ // Make sure the window is inactive before color sampling.
+ ui::test::ScopedFakeNSWindowFocus fake_focus;
+ [ns_window resignMainWindow];
+ [ns_window resignKeyWindow];
NSBitmapImageRep* bitmap = ScreenshotNSWindow(ns_window);
- // The window is currently inactive so it should be blue (#0000FF).
+ // The window is currently inactive so it should be blue (#0000FF). We are
+ // assuming the Light appearance is being used.
NSColor* expected_color = ColorInBitmapColorSpace(0xFF0000FF, bitmap);
+ int half_width = NSWidth([ns_window frame]) / 2;
NSColor* color = [bitmap colorAtX:half_width y:5];
CGFloat expected_components[4], color_components[4];
[expected_color getComponents:expected_components];
@@ -611,11 +619,12 @@
EXPECT_NEAR(expected_components[1], color_components[1], 0.01);
EXPECT_NEAR(expected_components[2], color_components[2], 0.01);
- ui::test::ScopedFakeNSWindowFocus fake_focus;
+ // Activate the window.
[ns_window makeMainWindow];
bitmap = ScreenshotNSWindow(ns_window);
- // The window is now active so it should be red (#FF0000).
+ // The window is now active so it should be red (#FF0000). Again, this is
+ // assuming the Light appearance is being used.
expected_color = ColorInBitmapColorSpace(0xFFFF0000, bitmap);
color = [bitmap colorAtX:half_width y:5];
[expected_color getComponents:expected_components];
diff --git a/content/public/test/browser_test_base.cc b/content/public/test/browser_test_base.cc
index 0521a8f..bb2781f 100644
--- a/content/public/test/browser_test_base.cc
+++ b/content/public/test/browser_test_base.cc
@@ -276,6 +276,7 @@
#elif BUILDFLAG(IS_MAC)
ui::test::EventGeneratorDelegate::SetFactoryFunction(
base::BindRepeating(&views::test::CreateEventGeneratorDelegateMac));
+ EnableNativeWindowActivation();
#endif
}
diff --git a/content/public/test/browser_test_utils.h b/content/public/test/browser_test_utils.h
index 5c6d2d3..5c7793a1 100644
--- a/content/public/test/browser_test_utils.h
+++ b/content/public/test/browser_test_utils.h
@@ -2293,6 +2293,13 @@
[[nodiscard]] bool HistoryGoBack(WebContents* wc);
[[nodiscard]] bool HistoryGoForward(WebContents* wc);
+#if BUILDFLAG(IS_MAC)
+// Grant native windows the ability to activate, allowing them to become key
+// and/or main. This can be useful to enable when the process hosting the window
+// is a standalone executable without an Info.plist.
+bool EnableNativeWindowActivation();
+#endif // BUILDFLAG(IS_MAC)
+
} // namespace content
#endif // CONTENT_PUBLIC_TEST_BROWSER_TEST_UTILS_H_
diff --git a/content/public/test/browser_test_utils_mac.mm b/content/public/test/browser_test_utils_mac.mm
new file mode 100644
index 0000000..fabd386
--- /dev/null
+++ b/content/public/test/browser_test_utils_mac.mm
@@ -0,0 +1,24 @@
+// Copyright 2022 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/public/test/browser_test_utils.h"
+
+#import <AppKit/AppKit.h>
+
+namespace content {
+
+bool EnableNativeWindowActivation() {
+ // Do not downgrade the activation policy.
+ if (NSApp.activationPolicy > NSApplicationActivationPolicyProhibited) {
+ return true;
+ }
+
+ // NSApplicationActivationPolicyAccessory is the least permissive policy that
+ // still allows for programmatic activation.
+ return [NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory]
+ ? true
+ : false;
+}
+
+} // namespace content
diff --git a/content/test/BUILD.gn b/content/test/BUILD.gn
index 5a97caf8..4330e25 100644
--- a/content/test/BUILD.gn
+++ b/content/test/BUILD.gn
@@ -695,6 +695,7 @@
sources += [
"../browser/renderer_host/test_render_widget_host_view_mac_factory.h",
"../browser/renderer_host/test_render_widget_host_view_mac_factory.mm",
+ "../public/test/browser_test_utils_mac.mm",
"../public/test/text_input_test_utils_mac.mm",
]
deps += [