diff options
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gyp | 2 | ||||
-rw-r--r-- | base/base.gypi | 2 | ||||
-rw-r--r-- | base/chrome_application_mac.h | 61 | ||||
-rw-r--r-- | base/chrome_application_mac.mm | 68 | ||||
-rw-r--r-- | base/mac_util_unittest.mm | 4 | ||||
-rw-r--r-- | base/message_pump_mac.h | 17 | ||||
-rw-r--r-- | base/message_pump_mac.mm | 11 | ||||
-rw-r--r-- | base/test/mock_chrome_application_mac.h | 28 | ||||
-rw-r--r-- | base/test/mock_chrome_application_mac.mm | 19 |
9 files changed, 67 insertions, 145 deletions
diff --git a/base/base.gyp b/base/base.gyp index f4ddeb5..4c72e8b 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -257,6 +257,8 @@ ], 'sources': [ 'perftimer.cc', + 'test/mock_chrome_application_mac.h', + 'test/mock_chrome_application_mac.mm', 'test/multiprocess_test.cc', 'test/multiprocess_test.h', 'test/perf_test_suite.cc', diff --git a/base/base.gypi b/base/base.gypi index 3b38a0d..dd9236f 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -41,8 +41,6 @@ 'callback.h', 'cancellation_flag.cc', 'cancellation_flag.h', - 'chrome_application_mac.h', - 'chrome_application_mac.mm', 'cocoa_protocols_mac.h', 'command_line.cc', 'command_line.h', diff --git a/base/chrome_application_mac.h b/base/chrome_application_mac.h deleted file mode 100644 index 676959ec..0000000 --- a/base/chrome_application_mac.h +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright (c) 2009 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. - -#ifndef BASE_CHROME_APPLICATION_MAC_H_ -#define BASE_CHROME_APPLICATION_MAC_H_ -#pragma once - -#import <AppKit/AppKit.h> - -#include "base/basictypes.h" -#include "base/scoped_nsobject.h" - -// Event hooks must implement this protocol. -@protocol CrApplicationEventHookProtocol -- (void)hookForEvent:(NSEvent*)theEvent; -@end - - -@interface CrApplication : NSApplication { - @private - BOOL handlingSendEvent_; - // Array of objects implementing the CrApplicationEventHookProtocol - scoped_nsobject<NSMutableArray> eventHooks_; -} -@property(readonly, - getter=isHandlingSendEvent, - nonatomic) BOOL handlingSendEvent; - -// Add or remove an event hook to be called for every sendEvent: -// that the application receives. These handlers are called before -// the normal [NSApplication sendEvent:] call is made. - -// This is not a good alternative to a nested event loop. It should -// be used only when normal event logic and notification breaks down -// (e.g. when clicking outside a canBecomeKey:NO window to "switch -// context" out of it). -- (void)addEventHook:(id<CrApplicationEventHookProtocol>)hook; -- (void)removeEventHook:(id<CrApplicationEventHookProtocol>)hook; - -+ (NSApplication*)sharedApplication; -@end - -namespace chrome_application_mac { - -// Controls the state of |handlingSendEvent_| in the event loop so that it is -// reset properly. -class ScopedSendingEvent { - public: - ScopedSendingEvent(); - ~ScopedSendingEvent(); - - private: - CrApplication* app_; - BOOL handling_; - DISALLOW_COPY_AND_ASSIGN(ScopedSendingEvent); -}; - -} // chrome_application_mac - -#endif // BASE_CHROME_APPLICATION_MAC_H_ diff --git a/base/chrome_application_mac.mm b/base/chrome_application_mac.mm deleted file mode 100644 index a163534..0000000 --- a/base/chrome_application_mac.mm +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright (c) 2009 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. - -#import "chrome_application_mac.h" - -#include "base/logging.h" - -@interface CrApplication () -@property(readwrite, - getter=isHandlingSendEvent, - nonatomic) BOOL handlingSendEvent; -@end - -@implementation CrApplication -@synthesize handlingSendEvent = handlingSendEvent_; - -// Initialize NSApplication using the custom subclass. Check whether NSApp -// was already initialized using another class, because that would break -// some things. -+ (NSApplication*)sharedApplication { - NSApplication* app = [super sharedApplication]; - if (![NSApp isKindOfClass:self]) { - LOG(ERROR) << "NSApp should be of type " << [[self className] UTF8String] - << ", not " << [[NSApp className] UTF8String]; - DCHECK(false) << "NSApp is of wrong type"; - } - return app; -} - -- (id)init { - if ((self = [super init])) { - eventHooks_.reset([[NSMutableArray alloc] init]); - } - return self; -} - -- (void)sendEvent:(NSEvent*)event { - chrome_application_mac::ScopedSendingEvent sendingEventScoper; - for (id<CrApplicationEventHookProtocol> handler in eventHooks_.get()) { - [handler hookForEvent:event]; - } - [super sendEvent:event]; -} - -- (void)addEventHook:(id<CrApplicationEventHookProtocol>)handler { - [eventHooks_ addObject:handler]; -} - -- (void)removeEventHook:(id<CrApplicationEventHookProtocol>)handler { - [eventHooks_ removeObject:handler]; -} - -@end - -namespace chrome_application_mac { - -ScopedSendingEvent::ScopedSendingEvent() - : app_(static_cast<CrApplication*>([CrApplication sharedApplication])), - handling_([app_ isHandlingSendEvent]) { - [app_ setHandlingSendEvent:YES]; -} - -ScopedSendingEvent::~ScopedSendingEvent() { - [app_ setHandlingSendEvent:handling_]; -} - -} // namespace chrome_application_mac diff --git a/base/mac_util_unittest.mm b/base/mac_util_unittest.mm index 7999878..63ea9b2 100644 --- a/base/mac_util_unittest.mm +++ b/base/mac_util_unittest.mm @@ -7,10 +7,10 @@ #include "base/mac_util.h" -#import "base/chrome_application_mac.h" #include "base/file_path.h" #include "base/file_util.h" #include "base/mac/scoped_cftyperef.h" +#include "base/test/mock_chrome_application_mac.h" #include "base/scoped_nsobject.h" #include "base/scoped_ptr.h" #include "testing/gtest/include/gtest/gtest.h" @@ -54,7 +54,7 @@ TEST_F(MacUtilTest, TestLibraryPath) { TEST_F(MacUtilTest, TestGrabWindowSnapshot) { // Launch a test window so we can take a snapshot. - [CrApplication sharedApplication]; + [MockCrApp sharedApplication]; NSRect frame = NSMakeRect(0, 0, 400, 400); scoped_nsobject<NSWindow> window( [[NSWindow alloc] initWithContentRect:frame diff --git a/base/message_pump_mac.h b/base/message_pump_mac.h index e016d54..c30a8ea 100644 --- a/base/message_pump_mac.h +++ b/base/message_pump_mac.h @@ -36,11 +36,20 @@ #include <CoreFoundation/CoreFoundation.h> #include <IOKit/IOKitLib.h> -#if defined(__OBJC__) -@class NSAutoreleasePool; -#else // defined(__OBJC__) +#if !defined(__OBJC__) class NSAutoreleasePool; -#endif // defined(__OBJC__) +#else // !defined(__OBJC__) +#import <AppKit/AppKit.h> + +// Clients must subclass NSApplication and implement this protocol if they use +// MessagePumpMac. +@protocol CrAppProtocol +// Must return true if -[NSApplication sendEvent:] is currently on the stack. +// See the comment for |CreateAutoreleasePool()| in the cc file for why this is +// necessary. +- (BOOL)isHandlingSendEvent; +@end +#endif // !defined(__OBJC__) namespace base { diff --git a/base/message_pump_mac.mm b/base/message_pump_mac.mm index 9091006..8c5461c 100644 --- a/base/message_pump_mac.mm +++ b/base/message_pump_mac.mm @@ -11,7 +11,6 @@ #include <limits> -#import "base/chrome_application_mac.h" #include "base/logging.h" #include "base/time.h" @@ -673,10 +672,6 @@ MessagePumpNSApplication::MessagePumpNSApplication() void MessagePumpNSApplication::DoRun(Delegate* delegate) { bool last_running_own_loop_ = running_own_loop_; - // TODO(dmaclach): Get rid of this gratuitous sharedApplication. - // Tests should be setting up their applications on their own. - [CrApplication sharedApplication]; - if (![NSApp isRunning]) { running_own_loop_ = false; // NSApplication manages autorelease pools itself when run this way. @@ -749,12 +744,12 @@ void MessagePumpNSApplication::Quit() { // autorelease pool stack. // // CrApplication is responsible for setting handlingSendEvent to true just -// before it sends the event throught the event handling mechanism, and +// before it sends the event through the event handling mechanism, and // returning it to its previous value once the event has been sent. NSAutoreleasePool* MessagePumpNSApplication::CreateAutoreleasePool() { NSAutoreleasePool* pool = nil; - DCHECK([NSApp isKindOfClass:[CrApplication class]]); - if (![static_cast<CrApplication*>(NSApp) isHandlingSendEvent]) { + DCHECK([NSApp conformsToProtocol:@protocol(CrAppProtocol)]); + if (![NSApp isHandlingSendEvent]) { pool = MessagePumpCFRunLoopBase::CreateAutoreleasePool(); } return pool; diff --git a/base/test/mock_chrome_application_mac.h b/base/test/mock_chrome_application_mac.h new file mode 100644 index 0000000..e7e2c67 --- /dev/null +++ b/base/test/mock_chrome_application_mac.h @@ -0,0 +1,28 @@ +// Copyright (c) 2010 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. + +#ifndef BASE_TEST_MOCK_CHROME_APPLICATION_MAC_H_ +#define BASE_TEST_MOCK_CHROME_APPLICATION_MAC_H_ +#pragma once + +#if defined(__OBJC__) + +#import <AppKit/AppKit.h> + +#include "base/message_pump_mac.h" + +// A mock implementation of CrAppProtocol that claims that -sendEvent: is never +// on the stack. This can be used in tests that need an NSApplication and use a +// runloop, but don't run nested message loops. +@interface MockCrApp : NSApplication<CrAppProtocol> +@end + +#endif + +// To be used to instantiate MockCrApp from C++ code. +namespace mock_cr_app { +void RegisterMockCrApp(); +} // namespace mock_cr_app + +#endif // BASE_TEST_MOCK_CHROME_APPLICATION_MAC_H_ diff --git a/base/test/mock_chrome_application_mac.mm b/base/test/mock_chrome_application_mac.mm new file mode 100644 index 0000000..f7010c4 --- /dev/null +++ b/base/test/mock_chrome_application_mac.mm @@ -0,0 +1,19 @@ +// Copyright (c) 2010 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 "base/test/mock_chrome_application_mac.h" + +@implementation MockCrApp +- (BOOL)isHandlingSendEvent { + return NO; +} +@end + +namespace mock_cr_app { + +void RegisterMockCrApp() { + [MockCrApp sharedApplication]; +} + +} // namespace mock_cr_app |