Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: ui/base/test/scoped_fake_nswindow_focus.mm

Issue 1268293002: [MacViews] Fix AccessiblePaneViewTest.SetPaneFocusAndRestore. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/base/test/scoped_fake_nswindow_focus.h ('k') | ui/base/ui_base.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_focused_window = nil;
15 15
16 void SetFocus(NSWindow* window) {
17 g_fake_focused_window = window;
18 [[NSNotificationCenter defaultCenter]
19 postNotificationName:NSWindowDidBecomeMainNotification
20 object:g_fake_focused_window];
21 [[NSNotificationCenter defaultCenter]
22 postNotificationName:NSWindowDidBecomeKeyNotification
23 object:g_fake_focused_window];
16 } 24 }
17 25
18 // Donates a testing implementation of [NSWindow isMainWindow]. 26 void ClearFocus() {
19 @interface IsMainWindowDonorForWindow : NSObject 27 NSWindow* window = g_fake_focused_window;
20 @end 28 g_fake_focused_window = nil;
29 [[NSNotificationCenter defaultCenter]
30 postNotificationName:NSWindowDidResignKeyNotification
31 object:window];
21 32
22 @implementation IsMainWindowDonorForWindow
23 - (BOOL)isMainWindow {
24 NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self);
25 return selfAsWindow == g_fake_main_window;
26 }
27 @end
28
29 ScopedFakeNSWindowMainStatus::ScopedFakeNSWindowMainStatus(NSWindow* window)
30 : swizzler_(new base::mac::ScopedObjCClassSwizzler(
31 [NSWindow class],
32 [IsMainWindowDonorForWindow class],
33 @selector(isMainWindow))) {
34 DCHECK(!g_fake_main_window);
35 g_fake_main_window = window;
36 [[NSNotificationCenter defaultCenter]
37 postNotificationName:NSWindowDidBecomeMainNotification
38 object:g_fake_main_window];
39 }
40
41 ScopedFakeNSWindowMainStatus::~ScopedFakeNSWindowMainStatus() {
42 NSWindow* window = g_fake_main_window;
43 g_fake_main_window = nil;
44 [[NSNotificationCenter defaultCenter] 33 [[NSNotificationCenter defaultCenter]
45 postNotificationName:NSWindowDidResignMainNotification 34 postNotificationName:NSWindowDidResignMainNotification
46 object:window]; 35 object:window];
47 } 36 }
37
38 } // namespace
39
40 // Donates testing implementations of NSWindow methods.
41 @interface FakeNSWindowFocusDonor : NSObject
42 @end
43
44 @implementation FakeNSWindowFocusDonor
45
46 - (BOOL)isKeyWindow {
47 NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self);
48 return selfAsWindow == g_fake_focused_window;
49 }
50
51 - (BOOL)isMainWindow {
52 NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self);
53 return selfAsWindow == g_fake_focused_window;
54 }
55
56 - (void)makeKeyWindow {
57 NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self);
58 if (selfAsWindow == g_fake_focused_window)
59 return;
60
61 ClearFocus();
62 SetFocus(selfAsWindow);
63 }
64
65 - (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.
66 NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self);
67 if (selfAsWindow == g_fake_focused_window)
68 return;
69
70 ClearFocus();
71 SetFocus(selfAsWindow);
72 }
73
74 @end
75
76 namespace ui {
77 namespace test {
78
79 ScopedFakeNSWindowFocus::ScopedFakeNSWindowFocus()
80 : is_main_swizzler_(
81 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
82 [FakeNSWindowFocusDonor class],
83 @selector(isMainWindow))),
84 make_main_swizzler_(
85 new base::mac::ScopedObjCClassSwizzler([NSWindow class],
86 [FakeNSWindowFocusDonor class],
87 @selector(makeMainWindow))),
88 is_key_swizzler_(
89 new base::mac::ScopedObjCClassSwizzler([NSWindow class],
90 [FakeNSWindowFocusDonor class],
91 @selector(isKeyWindow))),
92 make_key_swizzler_(
93 new base::mac::ScopedObjCClassSwizzler([NSWindow class],
94 [FakeNSWindowFocusDonor class],
95 @selector(makeKeyWindow))) {}
96
97 ScopedFakeNSWindowFocus::~ScopedFakeNSWindowFocus() {
98 ClearFocus();
99 }
100
101 } // namespace test
102 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/test/scoped_fake_nswindow_focus.h ('k') | ui/base/ui_base.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698