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