Chromium Code Reviews| Index: ui/base/test/scoped_fake_nswindow_focus.mm |
| diff --git a/chrome/browser/ui/test/scoped_fake_nswindow_main_status.mm b/ui/base/test/scoped_fake_nswindow_focus.mm |
| similarity index 29% |
| rename from chrome/browser/ui/test/scoped_fake_nswindow_main_status.mm |
| rename to ui/base/test/scoped_fake_nswindow_focus.mm |
| index 0f2c27d6a5956435fe2055abe29f46849c8e83c5..c088409d39b33c16c820d9176f8fc9d45668e05c 100644 |
| --- a/chrome/browser/ui/test/scoped_fake_nswindow_main_status.mm |
| +++ b/ui/base/test/scoped_fake_nswindow_focus.mm |
| @@ -2,7 +2,7 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#import "chrome/browser/ui/test/scoped_fake_nswindow_main_status.h" |
| +#import "ui/base/test/scoped_fake_nswindow_focus.h" |
| #import <Cocoa/Cocoa.h> |
| @@ -12,10 +12,29 @@ |
| namespace { |
| NSWindow* g_fake_main_window = nil; |
| +NSWindow* g_fake_key_window = nil; |
| +void SetAndNotify(NSWindow** state, |
|
tapted
2015/08/05 07:07:17
I'm undecided about these methods.
It might be be
jackhou1
2015/08/06 00:22:30
Done.
|
| + NSWindow* window, |
| + NSString* notification_name) { |
| + DCHECK(!*state); |
| + *state = window; |
| + [[NSNotificationCenter defaultCenter] postNotificationName:notification_name |
| + object:*state]; |
| } |
| -// Donates a testing implementation of [NSWindow isMainWindow]. |
| +void ClearAndNotify(NSWindow** state, NSString* notification_name) { |
| + if (!*state) |
| + return; |
| + |
| + NSWindow* window = *state; |
| + *state = nil; |
| + [[NSNotificationCenter defaultCenter] postNotificationName:notification_name |
| + object:window]; |
| +} |
| +} |
|
tapted
2015/08/05 07:07:17
blank line +
} // namespace
jackhou1
2015/08/06 00:22:30
Done.
|
| + |
| +// Donates a testing implementation of -[NSWindow isMainWindow]. |
| @interface IsMainWindowDonorForWindow : NSObject |
| @end |
| @@ -26,22 +45,54 @@ NSWindow* g_fake_main_window = nil; |
| } |
| @end |
| +// Donates testing implementations of -[NSWindow isKeyWindow] and |
| +// -[NSWindow makeKeyWindow]. |
| +@interface IsKeyWindowDonorForWindow : NSObject |
|
tapted
2015/08/05 07:07:17
even if we can't merge the C++ classes, it should
jackhou1
2015/08/06 00:22:30
Done.
|
| +@end |
| + |
| +@implementation IsKeyWindowDonorForWindow |
| +- (void)makeKeyWindow { |
| + NSWindow* window = base::mac::ObjCCastStrict<NSWindow>(self); |
| + if (window == g_fake_key_window) |
| + return; |
| + |
| + ClearAndNotify(&g_fake_key_window, NSWindowDidResignKeyNotification); |
|
tapted
2015/08/05 07:07:17
Before this, it should do
if (![window canBecomeK
jackhou1
2015/08/06 00:22:30
Yeah, that returns NO. If we really want to suppor
|
| + SetAndNotify(&g_fake_key_window, window, NSWindowDidBecomeKeyNotification); |
| +} |
| +- (BOOL)isKeyWindow { |
| + NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self); |
| + return selfAsWindow == g_fake_key_window; |
| +} |
| +@end |
| + |
| +namespace ui { |
| +namespace test { |
| + |
| ScopedFakeNSWindowMainStatus::ScopedFakeNSWindowMainStatus(NSWindow* window) |
| : swizzler_(new base::mac::ScopedObjCClassSwizzler( |
| [NSWindow class], |
| [IsMainWindowDonorForWindow class], |
| @selector(isMainWindow))) { |
| - DCHECK(!g_fake_main_window); |
| - g_fake_main_window = window; |
| - [[NSNotificationCenter defaultCenter] |
| - postNotificationName:NSWindowDidBecomeMainNotification |
| - object:g_fake_main_window]; |
| + SetAndNotify(&g_fake_main_window, window, NSWindowDidBecomeMainNotification); |
| } |
| ScopedFakeNSWindowMainStatus::~ScopedFakeNSWindowMainStatus() { |
| - NSWindow* window = g_fake_main_window; |
| - g_fake_main_window = nil; |
| - [[NSNotificationCenter defaultCenter] |
| - postNotificationName:NSWindowDidResignMainNotification |
| - object:window]; |
| + ClearAndNotify(&g_fake_main_window, NSWindowDidResignMainNotification); |
| } |
| + |
| +ScopedFakeNSWindowKeyStatus::ScopedFakeNSWindowKeyStatus() |
| + : is_key_swizzler_(new base::mac::ScopedObjCClassSwizzler( |
| + [NSWindow class], |
| + [IsKeyWindowDonorForWindow class], |
| + @selector(isKeyWindow))), |
| + make_key_swizzler_(new base::mac::ScopedObjCClassSwizzler( |
| + [NSWindow class], |
| + [IsKeyWindowDonorForWindow class], |
| + @selector(makeKeyWindow))) {} |
| + |
| +ScopedFakeNSWindowKeyStatus::~ScopedFakeNSWindowKeyStatus() { |
| + ClearAndNotify(&g_fake_key_window, NSWindowDidResignKeyNotification); |
| +} |
| + |
| +} // namespace test |
| +} // namespace ui |