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.
5 #include "base/strings/string_util.h"
6 #include "base/strings/sys_string_conversions.h"
7 #include "chrome/browser/ui/browser_window.h"
8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
9 #import "chrome/browser/ui/cocoa/find_bar/find_bar_cocoa_controller.h"
10 #import "chrome/browser/ui/cocoa/find_bar/find_bar_text_field.h"
11 #include "chrome/browser/ui/find_bar/find_notification_details.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/platform_test.h"
14 #import "ui/base/cocoa/find_pasteboard.h"
16 // Expose private variables to make testing easier.
17 @interface FindBarCocoaController(Testing)
18 - (FindBarTextField*)findTextField;
21 @implementation FindBarCocoaController(Testing)
22 - (FindBarTextField*)findTextField {
26 - (NSButton*)nextButton {
30 - (NSButton*)previousButton {
31 return previousButton_;
37 class FindBarCocoaControllerTest : public CocoaTest {
39 virtual void SetUp() {
41 controller_.reset([[FindBarCocoaController alloc] initWithBrowser:nil]);
42 [[test_window() contentView] addSubview:[controller_ view]];
45 virtual void TearDown() {
46 CocoaTest::TearDown();
47 [controller_ stopAnimation];
51 base::scoped_nsobject<FindBarCocoaController> controller_;
54 TEST_VIEW(FindBarCocoaControllerTest, [controller_ view])
56 TEST_F(FindBarCocoaControllerTest, ImagesLoadedProperly) {
57 EXPECT_TRUE([[[controller_ nextButton] image] isValid]);
58 EXPECT_TRUE([[[controller_ previousButton] image] isValid]);
61 TEST_F(FindBarCocoaControllerTest, ShowAndHide) {
62 NSView* findBarView = [controller_ findBarView];
64 ASSERT_GT([findBarView frame].origin.y, 0);
65 ASSERT_FALSE([controller_ isFindBarVisible]);
67 [controller_ showFindBar:NO];
68 EXPECT_EQ([findBarView frame].origin.y, 0);
69 EXPECT_TRUE([controller_ isFindBarVisible]);
71 [controller_ hideFindBar:NO];
72 EXPECT_GT([findBarView frame].origin.y, 0);
73 EXPECT_FALSE([controller_ isFindBarVisible]);
76 TEST_F(FindBarCocoaControllerTest, SetFindText) {
77 NSTextField* findTextField = [controller_ findTextField];
79 // Start by making the find bar visible.
80 [controller_ showFindBar:NO];
81 EXPECT_TRUE([controller_ isFindBarVisible]);
84 NSString* const kFindText = @"Google";
85 [controller_ setFindText:kFindText];
88 [[findTextField stringValue] compare:kFindText]);
90 // Call clearResults, which doesn't actually clear the find text but
91 // simply sets it back to what it was before. This is silly, but
92 // matches the behavior on other platforms. |details| isn't used by
93 // our implementation of clearResults, so it's ok to pass in an
95 FindNotificationDetails details;
96 [controller_ clearResults:details];
99 [[findTextField stringValue] compare:kFindText]);
102 TEST_F(FindBarCocoaControllerTest, ResultLabelUpdatesCorrectly) {
103 // TODO(rohitrao): Test this. It may involve creating some dummy
104 // FindNotificationDetails objects.
107 TEST_F(FindBarCocoaControllerTest, FindTextIsGlobal) {
108 base::scoped_nsobject<FindBarCocoaController> otherController(
109 [[FindBarCocoaController alloc] initWithBrowser:nil]);
110 [[test_window() contentView] addSubview:[otherController view]];
112 // Setting the text in one controller should update the other controller's
114 NSString* const kFindText = @"Respect to the man in the ice cream van";
115 [controller_ setFindText:kFindText];
118 [[controller_ findText] compare:kFindText]);
121 [[otherController.get() findText] compare:kFindText]);
124 TEST_F(FindBarCocoaControllerTest, SettingFindTextUpdatesFindPboard) {
125 NSString* const kFindText =
126 @"It's not a bird, it's not a plane, it must be Dave who's on the train";
127 [controller_ setFindText:kFindText];
130 [[[FindPasteboard sharedInstance] findText] compare:kFindText]);