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