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
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
7 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
8 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
9 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
10 #include "chrome/browser/ui/omnibox/omnibox_popup_view.h"
11 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h"
12 #include "chrome/browser/ui/toolbar/toolbar_model_impl.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "testing/platform_test.h"
15 #include "ui/gfx/image/image.h"
16 #include "ui/gfx/rect.h"
20 class MockOmniboxEditModel : public OmniboxEditModel {
22 MockOmniboxEditModel(OmniboxView* view,
23 OmniboxEditController* controller,
25 : OmniboxEditModel(view, controller, profile),
26 up_or_down_count_(0) {
29 virtual void OnUpOrDownKeyPressed(int count) OVERRIDE {
30 up_or_down_count_ = count;
33 int up_or_down_count() const { return up_or_down_count_; }
35 void set_up_or_down_count(int count) {
36 up_or_down_count_ = count;
40 int up_or_down_count_;
42 DISALLOW_COPY_AND_ASSIGN(MockOmniboxEditModel);
45 class MockOmniboxPopupView : public OmniboxPopupView {
47 MockOmniboxPopupView() : is_open_(false) {}
48 virtual ~MockOmniboxPopupView() {}
50 // Overridden from OmniboxPopupView:
51 virtual bool IsOpen() const OVERRIDE { return is_open_; }
52 virtual void InvalidateLine(size_t line) OVERRIDE {}
53 virtual void UpdatePopupAppearance() OVERRIDE {}
54 virtual gfx::Rect GetTargetBounds() OVERRIDE { return gfx::Rect(); }
55 virtual void PaintUpdatesNow() OVERRIDE {}
56 virtual void OnDragCanceled() OVERRIDE {}
58 void set_is_open(bool is_open) { is_open_ = is_open; }
63 DISALLOW_COPY_AND_ASSIGN(MockOmniboxPopupView);
66 class TestingToolbarModelDelegate : public ToolbarModelDelegate {
68 TestingToolbarModelDelegate() {}
69 virtual ~TestingToolbarModelDelegate() {}
71 // Overridden from ToolbarModelDelegate:
72 virtual content::WebContents* GetActiveWebContents() const OVERRIDE {
77 DISALLOW_COPY_AND_ASSIGN(TestingToolbarModelDelegate);
80 class TestingOmniboxEditController : public OmniboxEditController {
82 explicit TestingOmniboxEditController(ToolbarModel* toolbar_model)
83 : OmniboxEditController(NULL),
84 toolbar_model_(toolbar_model) {
86 virtual ~TestingOmniboxEditController() {}
88 // Overridden from OmniboxEditController:
89 virtual void Update(const content::WebContents* contents) OVERRIDE {}
90 virtual void OnChanged() OVERRIDE {}
91 virtual void OnSetFocus() OVERRIDE {}
92 virtual InstantController* GetInstant() OVERRIDE { return NULL; }
93 virtual content::WebContents* GetWebContents() OVERRIDE { return NULL; }
94 virtual ToolbarModel* GetToolbarModel() OVERRIDE { return toolbar_model_; }
95 virtual const ToolbarModel* GetToolbarModel() const OVERRIDE {
96 return toolbar_model_;
100 ToolbarModel* toolbar_model_;
102 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController);
107 class OmniboxViewMacTest : public CocoaProfileTest {
109 void SetModel(OmniboxViewMac* view, OmniboxEditModel* model) {
110 view->model_.reset(model);
114 TEST_F(OmniboxViewMacTest, GetFieldFont) {
115 EXPECT_TRUE(OmniboxViewMac::GetFieldFont());
118 TEST_F(OmniboxViewMacTest, TabToAutocomplete) {
119 OmniboxViewMac view(NULL, profile(), NULL, NULL);
121 // This is deleted by the omnibox view.
122 MockOmniboxEditModel* model =
123 new MockOmniboxEditModel(&view, NULL, profile());
124 SetModel(&view, model);
126 MockOmniboxPopupView popup_view;
127 OmniboxPopupModel popup_model(&popup_view, model);
129 // With popup closed verify that tab doesn't autocomplete.
130 popup_view.set_is_open(false);
131 view.OnDoCommandBySelector(@selector(insertTab:));
132 EXPECT_EQ(0, model->up_or_down_count());
133 view.OnDoCommandBySelector(@selector(insertBacktab:));
134 EXPECT_EQ(0, model->up_or_down_count());
136 // With popup open verify that tab does autocomplete.
137 popup_view.set_is_open(true);
138 view.OnDoCommandBySelector(@selector(insertTab:));
139 EXPECT_EQ(1, model->up_or_down_count());
140 view.OnDoCommandBySelector(@selector(insertBacktab:));
141 EXPECT_EQ(-1, model->up_or_down_count());
144 TEST_F(OmniboxViewMacTest, SetGrayTextAutocompletion) {
145 const NSRect frame = NSMakeRect(0, 0, 50, 30);
146 base::scoped_nsobject<AutocompleteTextField> field(
147 [[AutocompleteTextField alloc] initWithFrame:frame]);
149 TestingToolbarModelDelegate delegate;
150 ToolbarModelImpl toolbar_model(&delegate);
151 TestingOmniboxEditController controller(&toolbar_model);
152 OmniboxViewMac view(&controller, profile(), NULL, field.get());
154 // This is deleted by the omnibox view.
155 MockOmniboxEditModel* model =
156 new MockOmniboxEditModel(&view, &controller, profile());
157 SetModel(&view, model);
159 MockOmniboxPopupView popup_view;
160 OmniboxPopupModel popup_model(&popup_view, model);
162 view.SetUserText(base::ASCIIToUTF16("Alfred"));
163 EXPECT_EQ("Alfred", base::UTF16ToUTF8(view.GetText()));
164 view.SetGrayTextAutocompletion(base::ASCIIToUTF16(" Hitchcock"));
165 EXPECT_EQ("Alfred", base::UTF16ToUTF8(view.GetText()));
166 EXPECT_EQ(" Hitchcock", base::UTF16ToUTF8(view.GetGrayTextAutocompletion()));
168 view.SetUserText(base::string16());
169 EXPECT_EQ(base::string16(), view.GetText());
170 EXPECT_EQ(base::string16(), view.GetGrayTextAutocompletion());
173 TEST_F(OmniboxViewMacTest, UpDownArrow) {
174 OmniboxViewMac view(NULL, profile(), NULL, NULL);
176 // This is deleted by the omnibox view.
177 MockOmniboxEditModel* model =
178 new MockOmniboxEditModel(&view, NULL, profile());
179 SetModel(&view, model);
181 MockOmniboxPopupView popup_view;
182 OmniboxPopupModel popup_model(&popup_view, model);
184 // With popup closed verify that pressing up and down arrow works.
185 popup_view.set_is_open(false);
186 model->set_up_or_down_count(0);
187 view.OnDoCommandBySelector(@selector(moveDown:));
188 EXPECT_EQ(1, model->up_or_down_count());
189 model->set_up_or_down_count(0);
190 view.OnDoCommandBySelector(@selector(moveUp:));
191 EXPECT_EQ(-1, model->up_or_down_count());
193 // With popup open verify that pressing up and down arrow works.
194 popup_view.set_is_open(true);
195 model->set_up_or_down_count(0);
196 view.OnDoCommandBySelector(@selector(moveDown:));
197 EXPECT_EQ(1, model->up_or_down_count());
198 model->set_up_or_down_count(0);
199 view.OnDoCommandBySelector(@selector(moveUp:));
200 EXPECT_EQ(-1, model->up_or_down_count());