| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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/memory/scoped_nsobject.h" | |
| 8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | |
| 9 #import "chrome/browser/ui/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 CocoaTest { | |
| 16 public: | |
| 17 virtual void SetUp() { | |
| 18 CocoaTest::SetUp(); | |
| 19 scoped_nsobject<NSView> view([[NSView alloc] initWithFrame:NSZeroRect]); | |
| 20 viewA_ = view.get(); | |
| 21 [[test_window() contentView] addSubview:viewA_]; | |
| 22 | |
| 23 view.reset([[NSView alloc] initWithFrame:NSZeroRect]); | |
| 24 viewB_ = view.get(); | |
| 25 [[test_window() contentView] addSubview:viewB_]; | |
| 26 } | |
| 27 | |
| 28 protected: | |
| 29 NSView* viewA_; | |
| 30 NSView* viewB_; | |
| 31 }; | |
| 32 | |
| 33 TEST_F(FocusTrackerTest, SaveRestore) { | |
| 34 NSWindow* window = test_window(); | |
| 35 ASSERT_TRUE([window makeFirstResponder:viewA_]); | |
| 36 scoped_nsobject<FocusTracker> tracker( | |
| 37 [[FocusTracker alloc] initWithWindow:window]); | |
| 38 // Give focus to |viewB_|, then try and restore it to view1. | |
| 39 ASSERT_TRUE([window makeFirstResponder:viewB_]); | |
| 40 EXPECT_TRUE([tracker restoreFocusInWindow:window]); | |
| 41 EXPECT_EQ(viewA_, [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 = test_window(); | |
| 48 scoped_nsobject<NSTextField> text([[NSTextField alloc] initWithFrame:frame]); | |
| 49 [[window contentView] addSubview:text]; | |
| 50 | |
| 51 ASSERT_TRUE([window makeFirstResponder:text]); | |
| 52 scoped_nsobject<FocusTracker> tracker([[FocusTracker alloc] | |
| 53 initWithWindow:window]); | |
| 54 // Give focus to |viewB_|, then try and restore it to the text field. | |
| 55 ASSERT_TRUE([window makeFirstResponder:viewB_]); | |
| 56 EXPECT_TRUE([tracker restoreFocusInWindow:window]); | |
| 57 EXPECT_TRUE([[window firstResponder] isKindOfClass:[NSTextView class]]); | |
| 58 } | |
| 59 | |
| 60 TEST_F(FocusTrackerTest, DontRestoreToViewNotInWindow) { | |
| 61 NSWindow* window = test_window(); | |
| 62 scoped_nsobject<NSView> viewC([[NSView alloc] initWithFrame:NSZeroRect]); | |
| 63 [[window contentView] addSubview:viewC]; | |
| 64 | |
| 65 ASSERT_TRUE([window makeFirstResponder:viewC]); | |
| 66 scoped_nsobject<FocusTracker> tracker( | |
| 67 [[FocusTracker alloc] initWithWindow:window]); | |
| 68 | |
| 69 // Give focus to |viewB_|, then remove viewC from the hierarchy and try | |
| 70 // to restore focus. The restore should fail. | |
| 71 ASSERT_TRUE([window makeFirstResponder:viewB_]); | |
| 72 [viewC removeFromSuperview]; | |
| 73 EXPECT_FALSE([tracker restoreFocusInWindow:window]); | |
| 74 } | |
| 75 | |
| 76 TEST_F(FocusTrackerTest, DontRestoreFocusToViewInDifferentWindow) { | |
| 77 NSWindow* window = test_window(); | |
| 78 ASSERT_TRUE([window makeFirstResponder:viewA_]); | |
| 79 scoped_nsobject<FocusTracker> tracker( | |
| 80 [[FocusTracker alloc] initWithWindow:window]); | |
| 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_]); | |
| 86 EXPECT_FALSE([tracker restoreFocusInWindow:nil]); | |
| 87 } | |
| 88 | |
| 89 | |
| 90 } // namespace | |
| OLD | NEW |