Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/ui/test/scoped_fake_nswindow_main_status.h" | 5 #import "ui/base/test/scoped_fake_nswindow_focus.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #import "base/mac/foundation_util.h" | 9 #import "base/mac/foundation_util.h" |
| 10 #import "base/mac/scoped_objc_class_swizzler.h" | 10 #import "base/mac/scoped_objc_class_swizzler.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 NSWindow* g_fake_main_window = nil; | 14 NSWindow* g_fake_main_window = nil; |
| 15 NSWindow* g_fake_key_window = nil; | |
| 15 | 16 |
| 17 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.
| |
| 18 NSWindow* window, | |
| 19 NSString* notification_name) { | |
| 20 DCHECK(!*state); | |
| 21 *state = window; | |
| 22 [[NSNotificationCenter defaultCenter] postNotificationName:notification_name | |
| 23 object:*state]; | |
| 16 } | 24 } |
| 17 | 25 |
| 18 // Donates a testing implementation of [NSWindow isMainWindow]. | 26 void ClearAndNotify(NSWindow** state, NSString* notification_name) { |
| 27 if (!*state) | |
| 28 return; | |
| 29 | |
| 30 NSWindow* window = *state; | |
| 31 *state = nil; | |
| 32 [[NSNotificationCenter defaultCenter] postNotificationName:notification_name | |
| 33 object:window]; | |
| 34 } | |
| 35 } | |
|
tapted
2015/08/05 07:07:17
blank line +
} // namespace
jackhou1
2015/08/06 00:22:30
Done.
| |
| 36 | |
| 37 // Donates a testing implementation of -[NSWindow isMainWindow]. | |
| 19 @interface IsMainWindowDonorForWindow : NSObject | 38 @interface IsMainWindowDonorForWindow : NSObject |
| 20 @end | 39 @end |
| 21 | 40 |
| 22 @implementation IsMainWindowDonorForWindow | 41 @implementation IsMainWindowDonorForWindow |
| 23 - (BOOL)isMainWindow { | 42 - (BOOL)isMainWindow { |
| 24 NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self); | 43 NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self); |
| 25 return selfAsWindow == g_fake_main_window; | 44 return selfAsWindow == g_fake_main_window; |
| 26 } | 45 } |
| 27 @end | 46 @end |
| 28 | 47 |
| 48 // Donates testing implementations of -[NSWindow isKeyWindow] and | |
| 49 // -[NSWindow makeKeyWindow]. | |
| 50 @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.
| |
| 51 @end | |
| 52 | |
| 53 @implementation IsKeyWindowDonorForWindow | |
| 54 - (void)makeKeyWindow { | |
| 55 NSWindow* window = base::mac::ObjCCastStrict<NSWindow>(self); | |
| 56 if (window == g_fake_key_window) | |
| 57 return; | |
| 58 | |
| 59 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
| |
| 60 SetAndNotify(&g_fake_key_window, window, NSWindowDidBecomeKeyNotification); | |
| 61 } | |
| 62 - (BOOL)isKeyWindow { | |
| 63 NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self); | |
| 64 return selfAsWindow == g_fake_key_window; | |
| 65 } | |
| 66 @end | |
| 67 | |
| 68 namespace ui { | |
| 69 namespace test { | |
| 70 | |
| 29 ScopedFakeNSWindowMainStatus::ScopedFakeNSWindowMainStatus(NSWindow* window) | 71 ScopedFakeNSWindowMainStatus::ScopedFakeNSWindowMainStatus(NSWindow* window) |
| 30 : swizzler_(new base::mac::ScopedObjCClassSwizzler( | 72 : swizzler_(new base::mac::ScopedObjCClassSwizzler( |
| 31 [NSWindow class], | 73 [NSWindow class], |
| 32 [IsMainWindowDonorForWindow class], | 74 [IsMainWindowDonorForWindow class], |
| 33 @selector(isMainWindow))) { | 75 @selector(isMainWindow))) { |
| 34 DCHECK(!g_fake_main_window); | 76 SetAndNotify(&g_fake_main_window, window, NSWindowDidBecomeMainNotification); |
| 35 g_fake_main_window = window; | |
| 36 [[NSNotificationCenter defaultCenter] | |
| 37 postNotificationName:NSWindowDidBecomeMainNotification | |
| 38 object:g_fake_main_window]; | |
| 39 } | 77 } |
| 40 | 78 |
| 41 ScopedFakeNSWindowMainStatus::~ScopedFakeNSWindowMainStatus() { | 79 ScopedFakeNSWindowMainStatus::~ScopedFakeNSWindowMainStatus() { |
| 42 NSWindow* window = g_fake_main_window; | 80 ClearAndNotify(&g_fake_main_window, NSWindowDidResignMainNotification); |
| 43 g_fake_main_window = nil; | |
| 44 [[NSNotificationCenter defaultCenter] | |
| 45 postNotificationName:NSWindowDidResignMainNotification | |
| 46 object:window]; | |
| 47 } | 81 } |
| 82 | |
| 83 ScopedFakeNSWindowKeyStatus::ScopedFakeNSWindowKeyStatus() | |
| 84 : is_key_swizzler_(new base::mac::ScopedObjCClassSwizzler( | |
| 85 [NSWindow class], | |
| 86 [IsKeyWindowDonorForWindow class], | |
| 87 @selector(isKeyWindow))), | |
| 88 make_key_swizzler_(new base::mac::ScopedObjCClassSwizzler( | |
| 89 [NSWindow class], | |
| 90 [IsKeyWindowDonorForWindow class], | |
| 91 @selector(makeKeyWindow))) {} | |
| 92 | |
| 93 ScopedFakeNSWindowKeyStatus::~ScopedFakeNSWindowKeyStatus() { | |
| 94 ClearAndNotify(&g_fake_key_window, NSWindowDidResignKeyNotification); | |
| 95 } | |
| 96 | |
| 97 } // namespace test | |
| 98 } // namespace ui | |
| OLD | NEW |