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 "ui/base/test/scoped_fake_nswindow_focus.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 using base::mac::ScopedObjCClassSwizzler; | 12 using base::mac::ScopedObjCClassSwizzler; |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 NSWindow* g_fake_focused_window = nil; | 16 NSWindow* g_fake_focused_window = nil; |
| 17 IMP g_order_out_impl_ = nullptr; |
17 | 18 |
18 void SetFocus(NSWindow* window) { | 19 void SetFocus(NSWindow* window) { |
19 g_fake_focused_window = window; | 20 g_fake_focused_window = window; |
20 [[NSNotificationCenter defaultCenter] | 21 [[NSNotificationCenter defaultCenter] |
21 postNotificationName:NSWindowDidBecomeMainNotification | 22 postNotificationName:NSWindowDidBecomeMainNotification |
22 object:g_fake_focused_window]; | 23 object:g_fake_focused_window]; |
23 [[NSNotificationCenter defaultCenter] | 24 [[NSNotificationCenter defaultCenter] |
24 postNotificationName:NSWindowDidBecomeKeyNotification | 25 postNotificationName:NSWindowDidBecomeKeyNotification |
25 object:g_fake_focused_window]; | 26 object:g_fake_focused_window]; |
26 } | 27 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 return; | 63 return; |
63 | 64 |
64 ClearFocus(); | 65 ClearFocus(); |
65 SetFocus(selfAsWindow); | 66 SetFocus(selfAsWindow); |
66 } | 67 } |
67 | 68 |
68 - (void)makeMainWindow { | 69 - (void)makeMainWindow { |
69 [self makeKeyWindow]; | 70 [self makeKeyWindow]; |
70 } | 71 } |
71 | 72 |
| 73 - (void)orderOut:(id)sender { |
| 74 NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self); |
| 75 if (selfAsWindow == g_fake_focused_window) |
| 76 ClearFocus(); |
| 77 g_order_out_impl_(self, _cmd, sender); |
| 78 } |
| 79 |
72 @end | 80 @end |
73 | 81 |
74 namespace ui { | 82 namespace ui { |
75 namespace test { | 83 namespace test { |
76 | 84 |
77 ScopedFakeNSWindowFocus::ScopedFakeNSWindowFocus() | 85 ScopedFakeNSWindowFocus::ScopedFakeNSWindowFocus() |
78 : is_main_swizzler_( | 86 : is_main_swizzler_( |
79 new ScopedObjCClassSwizzler([NSWindow class], | 87 new ScopedObjCClassSwizzler([NSWindow class], |
80 [FakeNSWindowFocusDonor class], | 88 [FakeNSWindowFocusDonor class], |
81 @selector(isMainWindow))), | 89 @selector(isMainWindow))), |
82 make_main_swizzler_( | 90 make_main_swizzler_( |
83 new ScopedObjCClassSwizzler([NSWindow class], | 91 new ScopedObjCClassSwizzler([NSWindow class], |
84 [FakeNSWindowFocusDonor class], | 92 [FakeNSWindowFocusDonor class], |
85 @selector(makeMainWindow))), | 93 @selector(makeMainWindow))), |
86 is_key_swizzler_( | 94 is_key_swizzler_( |
87 new ScopedObjCClassSwizzler([NSWindow class], | 95 new ScopedObjCClassSwizzler([NSWindow class], |
88 [FakeNSWindowFocusDonor class], | 96 [FakeNSWindowFocusDonor class], |
89 @selector(isKeyWindow))), | 97 @selector(isKeyWindow))), |
90 make_key_swizzler_( | 98 make_key_swizzler_( |
91 new ScopedObjCClassSwizzler([NSWindow class], | 99 new ScopedObjCClassSwizzler([NSWindow class], |
92 [FakeNSWindowFocusDonor class], | 100 [FakeNSWindowFocusDonor class], |
93 @selector(makeKeyWindow))) {} | 101 @selector(makeKeyWindow))), |
| 102 order_out_swizzler_( |
| 103 new ScopedObjCClassSwizzler([NSWindow class], |
| 104 [FakeNSWindowFocusDonor class], |
| 105 @selector(orderOut:))) { |
| 106 g_order_out_impl_ = order_out_swizzler_->GetOriginalImplementation(); |
| 107 } |
94 | 108 |
95 ScopedFakeNSWindowFocus::~ScopedFakeNSWindowFocus() { | 109 ScopedFakeNSWindowFocus::~ScopedFakeNSWindowFocus() { |
96 ClearFocus(); | 110 ClearFocus(); |
97 } | 111 } |
98 | 112 |
99 } // namespace test | 113 } // namespace test |
100 } // namespace ui | 114 } // namespace ui |
OLD | NEW |