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 20% |
| rename from chrome/browser/ui/test/scoped_fake_nswindow_main_status.mm |
| rename to ui/base/test/scoped_fake_nswindow_focus.mm |
| index 0f2c27d6a5956435fe2055abe29f46849c8e83c5..4d0124dac1b39acc9378211428544dbf30e8449b 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> |
| @@ -11,37 +11,92 @@ |
| namespace { |
| -NSWindow* g_fake_main_window = nil; |
| +NSWindow* g_fake_focused_window = nil; |
| +void SetFocus(NSWindow* window) { |
| + g_fake_focused_window = window; |
| + [[NSNotificationCenter defaultCenter] |
| + postNotificationName:NSWindowDidBecomeMainNotification |
| + object:g_fake_focused_window]; |
| + [[NSNotificationCenter defaultCenter] |
| + postNotificationName:NSWindowDidBecomeKeyNotification |
| + object:g_fake_focused_window]; |
| } |
| -// Donates a testing implementation of [NSWindow isMainWindow]. |
| -@interface IsMainWindowDonorForWindow : NSObject |
| +void ClearFocus() { |
| + NSWindow* window = g_fake_focused_window; |
| + g_fake_focused_window = nil; |
| + [[NSNotificationCenter defaultCenter] |
| + postNotificationName:NSWindowDidResignKeyNotification |
| + object:window]; |
| + |
| + [[NSNotificationCenter defaultCenter] |
| + postNotificationName:NSWindowDidResignMainNotification |
| + object:window]; |
| +} |
| + |
| +} // namespace |
| + |
| +// Donates testing implementations of NSWindow methods. |
| +@interface FakeNSWindowFocusDonor : NSObject |
| @end |
| -@implementation IsMainWindowDonorForWindow |
| +@implementation FakeNSWindowFocusDonor |
| + |
| +- (BOOL)isKeyWindow { |
| + NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self); |
| + return selfAsWindow == g_fake_focused_window; |
| +} |
| + |
| - (BOOL)isMainWindow { |
| NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self); |
| - return selfAsWindow == g_fake_main_window; |
| + return selfAsWindow == g_fake_focused_window; |
| } |
| -@end |
| -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]; |
| +- (void)makeKeyWindow { |
| + NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self); |
| + if (selfAsWindow == g_fake_focused_window) |
| + return; |
| + |
| + ClearFocus(); |
| + SetFocus(selfAsWindow); |
| } |
| -ScopedFakeNSWindowMainStatus::~ScopedFakeNSWindowMainStatus() { |
| - NSWindow* window = g_fake_main_window; |
| - g_fake_main_window = nil; |
| - [[NSNotificationCenter defaultCenter] |
| - postNotificationName:NSWindowDidResignMainNotification |
| - object:window]; |
| +- (void)makeMainWindow { |
|
tapted
2015/08/06 01:21:17
just call [self makeKeyWindow]? Then it's clearer
jackhou1
2015/08/06 01:51:35
Done.
|
| + NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self); |
| + if (selfAsWindow == g_fake_focused_window) |
| + return; |
| + |
| + ClearFocus(); |
| + SetFocus(selfAsWindow); |
| +} |
| + |
| +@end |
| + |
| +namespace ui { |
| +namespace test { |
| + |
| +ScopedFakeNSWindowFocus::ScopedFakeNSWindowFocus() |
| + : is_main_swizzler_( |
| + new base::mac::ScopedObjCClassSwizzler([NSWindow class], |
|
tapted
2015/08/06 01:21:17
a `using base::mac::ScopedObjCClassSwizzler` might
jackhou1
2015/08/06 01:51:36
Done. Formatting is the same, but a bit easier to
|
| + [FakeNSWindowFocusDonor class], |
| + @selector(isMainWindow))), |
| + make_main_swizzler_( |
| + new base::mac::ScopedObjCClassSwizzler([NSWindow class], |
| + [FakeNSWindowFocusDonor class], |
| + @selector(makeMainWindow))), |
| + is_key_swizzler_( |
| + new base::mac::ScopedObjCClassSwizzler([NSWindow class], |
| + [FakeNSWindowFocusDonor class], |
| + @selector(isKeyWindow))), |
| + make_key_swizzler_( |
| + new base::mac::ScopedObjCClassSwizzler([NSWindow class], |
| + [FakeNSWindowFocusDonor class], |
| + @selector(makeKeyWindow))) {} |
| + |
| +ScopedFakeNSWindowFocus::~ScopedFakeNSWindowFocus() { |
| + ClearFocus(); |
| } |
| + |
| +} // namespace test |
| +} // namespace ui |