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 // Valgrind will complain if the text field has zero size. |
| 46 NSRect frame = NSMakeRect(0, 0, 100, 20); |
| 47 NSWindow* window = helper_.window(); |
| 48 NSTextField* text = |
| 49 [[[NSTextField alloc] initWithFrame:frame] autorelease]; |
| 50 [helper_.contentView() addSubview:text]; |
| 51 |
| 52 ASSERT_TRUE([window makeFirstResponder:text]); |
| 53 FocusTracker* tracker = |
| 54 [[[FocusTracker alloc] initWithWindow:window] autorelease]; |
| 55 |
| 56 // Give focus to |viewB_|, then try and restore it to the text field. |
| 57 ASSERT_TRUE([window makeFirstResponder:viewB_.get()]); |
| 58 EXPECT_TRUE([tracker restoreFocusInWindow:window]); |
| 59 EXPECT_TRUE([[window firstResponder] isKindOfClass:[NSTextView class]]); |
| 60 } |
| 61 |
| 62 TEST_F(FocusTrackerTest, DontRestoreToViewNotInWindow) { |
| 63 NSWindow* window = helper_.window(); |
| 64 NSView* view3 = [[[NSView alloc] initWithFrame:NSZeroRect] autorelease]; |
| 65 [helper_.contentView() addSubview:view3]; |
| 66 |
| 67 ASSERT_TRUE([window makeFirstResponder:view3]); |
| 68 FocusTracker* tracker = |
| 69 [[[FocusTracker alloc] initWithWindow:window] autorelease]; |
| 70 |
| 71 // Give focus to |viewB_|, then remove view3 from the hierarchy and try |
| 72 // to restore focus. The restore should fail. |
| 73 ASSERT_TRUE([window makeFirstResponder:viewB_.get()]); |
| 74 [view3 removeFromSuperview]; |
| 75 EXPECT_FALSE([tracker restoreFocusInWindow:window]); |
| 76 } |
| 77 |
| 78 TEST_F(FocusTrackerTest, DontRestoreFocusToViewInDifferentWindow) { |
| 79 NSWindow* window = helper_.window(); |
| 80 ASSERT_TRUE([window makeFirstResponder:viewA_.get()]); |
| 81 FocusTracker* tracker = |
| 82 [[[FocusTracker alloc] initWithWindow:window] autorelease]; |
| 83 |
| 84 // Give focus to |viewB_|, then try and restore focus in a different |
| 85 // window. It is ok to pass a nil NSWindow here because we only use |
| 86 // it for direct comparison. |
| 87 ASSERT_TRUE([window makeFirstResponder:viewB_.get()]); |
| 88 EXPECT_FALSE([tracker restoreFocusInWindow:nil]); |
| 89 } |
| 90 |
| 91 |
| 92 } // namespace |
OLD | NEW |