diff options
author | rohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-15 18:01:58 +0000 |
---|---|---|
committer | rohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-15 18:01:58 +0000 |
commit | 59f3e6d0f59a671ed2f50d14abbec2708cb709b1 (patch) | |
tree | 36b3687d077ef4d5c0e9657c6520175550fadd00 /chrome/browser/cocoa/focus_tracker_unittest.mm | |
parent | d88453afb9292607a16321e23785e5ee2417f776 (diff) | |
download | chromium_src-59f3e6d0f59a671ed2f50d14abbec2708cb709b1.zip chromium_src-59f3e6d0f59a671ed2f50d14abbec2708cb709b1.tar.gz chromium_src-59f3e6d0f59a671ed2f50d14abbec2708cb709b1.tar.bz2 |
[Mac] Restore focus to the previously focused view when dismissing the find bar.
If a result was found, restore focus to the tab contents. This allows for
keyboard navigation using the find bar.
Now with fix for valgrind failure. This CL reverts 26219, which in turn reverted 26214.
BUG=http://crbug.com/12657
BUG=http://crbug.com/21374
TEST=See test case in bug 21374
Review URL: http://codereview.chromium.org/205010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@26231 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/cocoa/focus_tracker_unittest.mm')
-rw-r--r-- | chrome/browser/cocoa/focus_tracker_unittest.mm | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/chrome/browser/cocoa/focus_tracker_unittest.mm b/chrome/browser/cocoa/focus_tracker_unittest.mm new file mode 100644 index 0000000..65ab91e --- /dev/null +++ b/chrome/browser/cocoa/focus_tracker_unittest.mm @@ -0,0 +1,92 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import <Cocoa/Cocoa.h> + +#include "base/scoped_nsobject.h" +#import "chrome/browser/cocoa/cocoa_test_helper.h" +#import "chrome/browser/cocoa/focus_tracker.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/platform_test.h" + +namespace { + +class FocusTrackerTest : public PlatformTest { + public: + virtual void SetUp() { + PlatformTest::SetUp(); + + viewA_.reset([[NSView alloc] initWithFrame:NSZeroRect]); + viewB_.reset([[NSView alloc] initWithFrame:NSZeroRect]); + [helper_.contentView() addSubview:viewA_.get()]; + [helper_.contentView() addSubview:viewB_.get()]; + } + + protected: + CocoaTestHelper helper_; + scoped_nsobject<NSView> viewA_; + scoped_nsobject<NSView> viewB_; +}; + +TEST_F(FocusTrackerTest, SaveRestore) { + NSWindow* window = helper_.window(); + ASSERT_TRUE([window makeFirstResponder:viewA_.get()]); + FocusTracker* tracker = + [[[FocusTracker alloc] initWithWindow:window] autorelease]; + + // Give focus to |viewB_|, then try and restore it to view1. + ASSERT_TRUE([window makeFirstResponder:viewB_.get()]); + EXPECT_TRUE([tracker restoreFocusInWindow:window]); + EXPECT_EQ(viewA_.get(), [window firstResponder]); +} + +TEST_F(FocusTrackerTest, SaveRestoreWithTextView) { + // Valgrind will complain if the text field has zero size. + NSRect frame = NSMakeRect(0, 0, 100, 20); + NSWindow* window = helper_.window(); + NSTextField* text = + [[[NSTextField alloc] initWithFrame:frame] autorelease]; + [helper_.contentView() addSubview:text]; + + ASSERT_TRUE([window makeFirstResponder:text]); + FocusTracker* tracker = + [[[FocusTracker alloc] initWithWindow:window] autorelease]; + + // Give focus to |viewB_|, then try and restore it to the text field. + ASSERT_TRUE([window makeFirstResponder:viewB_.get()]); + EXPECT_TRUE([tracker restoreFocusInWindow:window]); + EXPECT_TRUE([[window firstResponder] isKindOfClass:[NSTextView class]]); +} + +TEST_F(FocusTrackerTest, DontRestoreToViewNotInWindow) { + NSWindow* window = helper_.window(); + NSView* view3 = [[[NSView alloc] initWithFrame:NSZeroRect] autorelease]; + [helper_.contentView() addSubview:view3]; + + ASSERT_TRUE([window makeFirstResponder:view3]); + FocusTracker* tracker = + [[[FocusTracker alloc] initWithWindow:window] autorelease]; + + // Give focus to |viewB_|, then remove view3 from the hierarchy and try + // to restore focus. The restore should fail. + ASSERT_TRUE([window makeFirstResponder:viewB_.get()]); + [view3 removeFromSuperview]; + EXPECT_FALSE([tracker restoreFocusInWindow:window]); +} + +TEST_F(FocusTrackerTest, DontRestoreFocusToViewInDifferentWindow) { + NSWindow* window = helper_.window(); + ASSERT_TRUE([window makeFirstResponder:viewA_.get()]); + FocusTracker* tracker = + [[[FocusTracker alloc] initWithWindow:window] autorelease]; + + // Give focus to |viewB_|, then try and restore focus in a different + // window. It is ok to pass a nil NSWindow here because we only use + // it for direct comparison. + ASSERT_TRUE([window makeFirstResponder:viewB_.get()]); + EXPECT_FALSE([tracker restoreFocusInWindow:nil]); +} + + +} // namespace |