[Mac] Do not initialize the MockCrApplication in base::TestSuite.
This is not the right layer to do this, since it forces all test suites to have
an initialized Cocoa NSApp. Instead, only do it for test suites that require it.
BUG=71686
Review URL: https://codereview.chromium.org/656293003
Cr-Commit-Position: refs/heads/master@{#300275}
diff --git a/base/mac/scoped_sending_event_unittest.mm b/base/mac/scoped_sending_event_unittest.mm
index 9ae9985..95f6eba 100644
--- a/base/mac/scoped_sending_event_unittest.mm
+++ b/base/mac/scoped_sending_event_unittest.mm
@@ -4,12 +4,39 @@
#import "base/mac/scoped_sending_event.h"
+#import <Foundation/Foundation.h>
+
+#include "base/mac/scoped_nsobject.h"
#include "testing/gtest/include/gtest/gtest.h"
+@interface ScopedSendingEventTestCrApp : NSObject <CrAppControlProtocol> {
+ @private
+ BOOL handlingSendEvent_;
+}
+@property(nonatomic, assign, getter=isHandlingSendEvent) BOOL handlingSendEvent;
+@end
+
+@implementation ScopedSendingEventTestCrApp
+@synthesize handlingSendEvent = handlingSendEvent_;
+@end
+
namespace {
+class ScopedSendingEventTest : public testing::Test {
+ public:
+ ScopedSendingEventTest() : app_([[ScopedSendingEventTestCrApp alloc] init]) {
+ NSApp = app_.get();
+ }
+ virtual ~ScopedSendingEventTest() {
+ NSApp = nil;
+ }
+
+ private:
+ base::scoped_nsobject<ScopedSendingEventTestCrApp> app_;
+};
+
// Sets the flag within scope, resets when leaving scope.
-TEST(ScopedSendingEventTest, SetHandlingSendEvent) {
+TEST_F(ScopedSendingEventTest, SetHandlingSendEvent) {
id<CrAppProtocol> app = NSApp;
EXPECT_FALSE([app isHandlingSendEvent]);
{
@@ -20,7 +47,7 @@
}
// Nested call restores previous value rather than resetting flag.
-TEST(ScopedSendingEventTest, NestedSetHandlingSendEvent) {
+TEST_F(ScopedSendingEventTest, NestedSetHandlingSendEvent) {
id<CrAppProtocol> app = NSApp;
EXPECT_FALSE([app isHandlingSendEvent]);
{
diff --git a/base/test/test_suite.cc b/base/test/test_suite.cc
index 23ffa1e..206507bb 100644
--- a/base/test/test_suite.cc
+++ b/base/test/test_suite.cc
@@ -32,8 +32,6 @@
#include "base/mac/scoped_nsautorelease_pool.h"
#if defined(OS_IOS)
#include "base/test/test_listener_ios.h"
-#else
-#include "base/test/mock_chrome_application_mac.h"
#endif // OS_IOS
#endif // OS_MACOSX
@@ -287,11 +285,6 @@
}
#endif
-#if defined(OS_MACOSX) && !defined(OS_IOS)
- // Some of the app unit tests spin runloops.
- mock_cr_app::RegisterMockCrApp();
-#endif
-
#if defined(OS_IOS)
InitIOSTestMessageLoop();
#endif // OS_IOS
diff --git a/content/test/content_test_suite.cc b/content/test/content_test_suite.cc
index 4b7083a..24bad08 100644
--- a/content/test/content_test_suite.cc
+++ b/content/test/content_test_suite.cc
@@ -18,6 +18,9 @@
#if defined(OS_MACOSX)
#include "base/mac/scoped_nsautorelease_pool.h"
+#if !defined(OS_IOS)
+#include "base/test/mock_chrome_application_mac.h"
+#endif
#endif
#if !defined(OS_IOS)
@@ -63,6 +66,9 @@
void ContentTestSuite::Initialize() {
#if defined(OS_MACOSX)
base::mac::ScopedNSAutoreleasePool autorelease_pool;
+#if !defined(OS_IOS)
+ mock_cr_app::RegisterMockCrApp();
+#endif
#endif
#if defined(OS_WIN)
diff --git a/ui/app_list/test/run_all_unittests.cc b/ui/app_list/test/run_all_unittests.cc
index 19f0a0c8..bd03d4fc 100644
--- a/ui/app_list/test/run_all_unittests.cc
+++ b/ui/app_list/test/run_all_unittests.cc
@@ -12,7 +12,9 @@
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_paths.h"
-#if !defined(OS_MACOSX)
+#if defined(OS_MACOSX)
+#include "base/test/mock_chrome_application_mac.h"
+#else
#include "ui/gl/gl_surface.h"
#endif
@@ -24,7 +26,9 @@
protected:
virtual void Initialize() override {
-#if !defined(OS_MACOSX)
+#if defined(OS_MACOSX)
+ mock_cr_app::RegisterMockCrApp();
+#else
gfx::GLSurface::InitializeOneOffForTests();
#endif
base::TestSuite::Initialize();
diff --git a/ui/base/test/run_all_unittests.cc b/ui/base/test/run_all_unittests.cc
index 1254d97d..7e14b52 100644
--- a/ui/base/test/run_all_unittests.cc
+++ b/ui/base/test/run_all_unittests.cc
@@ -19,6 +19,7 @@
#if defined(OS_MACOSX) && !defined(OS_IOS)
#include "base/mac/bundle_locations.h"
+#include "base/test/mock_chrome_application_mac.h"
#endif
#if defined(OS_WIN)
@@ -62,6 +63,8 @@
PathService::Get(base::DIR_EXE, &exe_path);
#if defined(OS_MACOSX) && !defined(OS_IOS)
+ mock_cr_app::RegisterMockCrApp();
+
// On Mac, a test Framework bundle is created that links locale.pak and
// chrome_100_percent.pak at the appropriate places to ui_test.pak.
base::mac::SetOverrideFrameworkBundlePath(
diff --git a/ui/gfx/test/run_all_unittests.cc b/ui/gfx/test/run_all_unittests.cc
index 5282da1..2c2f4ee7 100644
--- a/ui/gfx/test/run_all_unittests.cc
+++ b/ui/gfx/test/run_all_unittests.cc
@@ -17,6 +17,10 @@
#include "ui/gfx/android/gfx_jni_registrar.h"
#endif
+#if defined(OS_MACOSX) && !defined(OS_IOS)
+#include "base/test/mock_chrome_application_mac.h"
+#endif
+
namespace {
class GfxTestSuite : public base::TestSuite {
@@ -31,6 +35,10 @@
gfx::android::RegisterJni(base::android::AttachCurrentThread());
#endif
+#if defined(OS_MACOSX) && !defined(OS_IOS)
+ mock_cr_app::RegisterMockCrApp();
+#endif
+
ui::RegisterPathProvider();
base::FilePath ui_test_pak_path;
diff --git a/ui/gfx/test/ui_cocoa_test_helper.mm b/ui/gfx/test/ui_cocoa_test_helper.mm
index 7f50bb3..63e32c3 100644
--- a/ui/gfx/test/ui_cocoa_test_helper.mm
+++ b/ui/gfx/test/ui_cocoa_test_helper.mm
@@ -7,6 +7,7 @@
#include "base/debug/debugger.h"
#include "base/logging.h"
#include "base/stl_util.h"
+#include "base/test/mock_chrome_application_mac.h"
#include "base/test/test_timeouts.h"
namespace {
@@ -16,6 +17,11 @@
// to flag intentional leaks without having to suppress all calls to
// potentially leaky functions.
void NOINLINE ForceSystemLeaks() {
+ // If a test suite hasn't already initialized NSApp, register the mock one
+ // now.
+ if (!NSApp)
+ mock_cr_app::RegisterMockCrApp();
+
// First NSCursor push always leaks.
[[NSCursor openHandCursor] push];
[NSCursor pop];
diff --git a/ui/message_center/test/run_all_unittests.cc b/ui/message_center/test/run_all_unittests.cc
index 274b1767..7c2c8cb 100644
--- a/ui/message_center/test/run_all_unittests.cc
+++ b/ui/message_center/test/run_all_unittests.cc
@@ -12,7 +12,9 @@
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_paths.h"
-#if !defined(OS_MACOSX)
+#if defined(OS_MACOSX)
+#include "base/test/mock_chrome_application_mac.h"
+#else
#include "ui/gl/gl_surface.h"
#endif
@@ -24,7 +26,9 @@
protected:
virtual void Initialize() override {
-#if !defined(OS_MACOSX)
+#if defined(OS_MACOSX)
+ mock_cr_app::RegisterMockCrApp();
+#else
gfx::GLSurface::InitializeOneOffForTests();
#endif
base::TestSuite::Initialize();