Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / find_bar / find_bar_cocoa_controller_unittest.mm
blob722c7feddabe23b6d3b4c82edf4445f7cf4eee9b
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;
19 @end
21 @implementation FindBarCocoaController(Testing)
22 - (FindBarTextField*)findTextField {
23   return findText_;
26 - (NSButton*)nextButton {
27   return nextButton_;
30 - (NSButton*)previousButton {
31   return previousButton_;
33 @end
35 namespace {
37 class FindBarCocoaControllerTest : public CocoaTest {
38  public:
39   void SetUp() override {
40     CocoaTest::SetUp();
41     controller_.reset([[FindBarCocoaController alloc] initWithBrowser:nil]);
42     [[test_window() contentView] addSubview:[controller_ view]];
43   }
45   void TearDown() override {
46     CocoaTest::TearDown();
47     [controller_ stopAnimation];
48   }
50  protected:
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]);
66   ASSERT_TRUE([[controller_ view] isHidden]);
68   [controller_ showFindBar:NO];
69   EXPECT_EQ([findBarView frame].origin.y, 0);
70   EXPECT_TRUE([controller_ isFindBarVisible]);
71   ASSERT_FALSE([[controller_ view] isHidden]);
73   [controller_ hideFindBar:NO];
74   EXPECT_GT([findBarView frame].origin.y, 0);
75   EXPECT_FALSE([controller_ isFindBarVisible]);
76   ASSERT_TRUE([[controller_ view] isHidden]);
79 TEST_F(FindBarCocoaControllerTest, SetFindText) {
80   NSTextField* findTextField = [controller_ findTextField];
82   // Start by making the find bar visible.
83   [controller_ showFindBar:NO];
84   EXPECT_TRUE([controller_ isFindBarVisible]);
86   // Set the find text.
87   NSString* const kFindText = @"Google";
88   [controller_ setFindText:kFindText selectedRange:NSMakeRange(NSNotFound, 0)];
89   EXPECT_EQ(
90       NSOrderedSame,
91       [[findTextField stringValue] compare:kFindText]);
93   // Call clearResults, which doesn't actually clear the find text but
94   // simply sets it back to what it was before.  This is silly, but
95   // matches the behavior on other platforms.  |details| isn't used by
96   // our implementation of clearResults, so it's ok to pass in an
97   // empty |details|.
98   FindNotificationDetails details;
99   [controller_ clearResults:details];
100   EXPECT_EQ(
101       NSOrderedSame,
102       [[findTextField stringValue] compare:kFindText]);
105 TEST_F(FindBarCocoaControllerTest, ResultLabelUpdatesCorrectly) {
106   // TODO(rohitrao): Test this.  It may involve creating some dummy
107   // FindNotificationDetails objects.
110 TEST_F(FindBarCocoaControllerTest, FindTextIsGlobal) {
111   base::scoped_nsobject<FindBarCocoaController> otherController(
112       [[FindBarCocoaController alloc] initWithBrowser:nil]);
113   [[test_window() contentView] addSubview:[otherController view]];
115   // Setting the text in one controller should update the other controller's
116   // text as well.
117   NSString* const kFindText = @"Respect to the man in the ice cream van";
118   [controller_ setFindText:kFindText selectedRange:NSMakeRange(NSNotFound, 0)];
119   EXPECT_EQ(
120       NSOrderedSame,
121       [[controller_ findText] compare:kFindText]);
122   EXPECT_EQ(
123       NSOrderedSame,
124       [[otherController.get() findText] compare:kFindText]);
127 TEST_F(FindBarCocoaControllerTest, SettingFindTextUpdatesFindPboard) {
128   NSString* const kFindText =
129       @"It's not a bird, it's not a plane, it must be Dave who's on the train";
130   [controller_ setFindText:kFindText selectedRange:NSMakeRange(NSNotFound, 0)];
131   EXPECT_EQ(
132       NSOrderedSame,
133       [[[FindPasteboard sharedInstance] findText] compare:kFindText]);
136 }  // namespace