OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #import <Cocoa/Cocoa.h> | |
6 | |
7 #include "base/scoped_nsobject.h" | |
8 #import "chrome/browser/cocoa/cocoa_test_helper.h" | |
9 #import "chrome/browser/cocoa/focus_tracker.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 #include "testing/platform_test.h" | |
12 | |
13 namespace { | |
14 | |
15 class FocusTrackerTest : public PlatformTest { | |
16 public: | |
17 virtual void SetUp() { | |
18 PlatformTest::SetUp(); | |
19 | |
20 viewA_.reset([[NSView alloc] initWithFrame:NSZeroRect]); | |
21 viewB_.reset([[NSView alloc] initWithFrame:NSZeroRect]); | |
22 [helper_.contentView() addSubview:viewA_.get()]; | |
23 [helper_.contentView() addSubview:viewB_.get()]; | |
24 } | |
25 | |
26 protected: | |
27 CocoaTestHelper helper_; | |
28 scoped_nsobject<NSView> viewA_; | |
29 scoped_nsobject<NSView> viewB_; | |
30 }; | |
31 | |
32 TEST_F(FocusTrackerTest, SaveRestore) { | |
33 NSWindow* window = helper_.window(); | |
34 ASSERT_TRUE([window makeFirstResponder:viewA_.get()]); | |
35 FocusTracker* tracker = | |
36 [[[FocusTracker alloc] initWithWindow:window] autorelease]; | |
37 | |
38 // Give focus to |viewB_|, then try and restore it to view1. | |
39 ASSERT_TRUE([window makeFirstResponder:viewB_.get()]); | |
40 EXPECT_TRUE([tracker restoreFocusInWindow:window]); | |
41 EXPECT_EQ(viewA_.get(), [window firstResponder]); | |
42 } | |
43 | |
44 TEST_F(FocusTrackerTest, SaveRestoreWithTextView) { | |
45 NSWindow* window = helper_.window(); | |
46 NSTextField* text = | |
47 [[[NSTextField alloc] initWithFrame:NSZeroRect] autorelease]; | |
48 [helper_.contentView() addSubview:text]; | |
49 | |
50 ASSERT_TRUE([window makeFirstResponder:text]); | |
51 FocusTracker* tracker = | |
52 [[[FocusTracker alloc] initWithWindow:window] autorelease]; | |
53 | |
54 // Give focus to |viewB_|, then try and restore it to the text field. | |
55 ASSERT_TRUE([window makeFirstResponder:viewB_.get()]); | |
56 EXPECT_TRUE([tracker restoreFocusInWindow:window]); | |
57 EXPECT_TRUE([[window firstResponder] isKindOfClass:[NSTextView class]]); | |
58 } | |
59 | |
60 TEST_F(FocusTrackerTest, DontRestoreToViewNotInWindow) { | |
61 NSWindow* window = helper_.window(); | |
62 NSView* view3 = [[[NSView alloc] initWithFrame:NSZeroRect] autorelease]; | |
63 [helper_.contentView() addSubview:view3]; | |
64 | |
65 ASSERT_TRUE([window makeFirstResponder:view3]); | |
66 FocusTracker* tracker = | |
67 [[[FocusTracker alloc] initWithWindow:window] autorelease]; | |
68 | |
69 // Give focus to |viewB_|, then remove view3 from the hierarchy and try | |
70 // to restore focus. The restore should fail. | |
71 ASSERT_TRUE([window makeFirstResponder:viewB_.get()]); | |
72 [view3 removeFromSuperview]; | |
73 EXPECT_FALSE([tracker restoreFocusInWindow:window]); | |
74 } | |
75 | |
76 TEST_F(FocusTrackerTest, DontRestoreFocusToViewInDifferentWindow) { | |
77 NSWindow* window = helper_.window(); | |
78 ASSERT_TRUE([window makeFirstResponder:viewA_.get()]); | |
79 FocusTracker* tracker = | |
80 [[[FocusTracker alloc] initWithWindow:window] autorelease]; | |
81 | |
82 // Give focus to |viewB_|, then try and restore focus in a different | |
83 // window. It is ok to pass a nil NSWindow here because we only use | |
84 // it for direct comparison. | |
85 ASSERT_TRUE([window makeFirstResponder:viewB_.get()]); | |
86 EXPECT_FALSE([tracker restoreFocusInWindow:nil]); | |
87 } | |
88 | |
89 | |
90 } // namespace | |
OLD | NEW |