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 {
75 virtual bool InTabbedBrowser() const OVERRIDE {
80 DISALLOW_COPY_AND_ASSIGN(TestingToolbarModelDelegate);
83 class TestingOmniboxEditController : public OmniboxEditController {
85 explicit TestingOmniboxEditController(ToolbarModel* toolbar_model)
86 : OmniboxEditController(NULL),
87 toolbar_model_(toolbar_model) {
89 virtual ~TestingOmniboxEditController() {}
91 // Overridden from OmniboxEditController:
92 virtual void Update(const content::WebContents* contents) OVERRIDE {}
93 virtual void OnChanged() OVERRIDE {}
94 virtual void OnSetFocus() OVERRIDE {}
95 virtual InstantController* GetInstant() OVERRIDE { return NULL; }
96 virtual content::WebContents* GetWebContents() OVERRIDE { return NULL; }
97 virtual ToolbarModel* GetToolbarModel() OVERRIDE { return toolbar_model_; }
98 virtual const ToolbarModel* GetToolbarModel() const OVERRIDE {
99 return toolbar_model_;
103 ToolbarModel* toolbar_model_;
105 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController);
110 class OmniboxViewMacTest : public CocoaProfileTest {
112 void SetModel(OmniboxViewMac* view, OmniboxEditModel* model) {
113 view->model_.reset(model);
117 TEST_F(OmniboxViewMacTest, GetFieldFont) {
118 EXPECT_TRUE(OmniboxViewMac::GetFieldFont());
121 TEST_F(OmniboxViewMacTest, TabToAutocomplete) {
122 OmniboxViewMac view(NULL, profile(), NULL, NULL);
124 // This is deleted by the omnibox view.
125 MockOmniboxEditModel* model =
126 new MockOmniboxEditModel(&view, NULL, profile());
127 SetModel(&view, model);
129 MockOmniboxPopupView popup_view;
130 OmniboxPopupModel popup_model(&popup_view, model);
132 // With popup closed verify that tab doesn't autocomplete.
133 popup_view.set_is_open(false);
134 view.OnDoCommandBySelector(@selector(insertTab:));
135 EXPECT_EQ(0, model->up_or_down_count());
136 view.OnDoCommandBySelector(@selector(insertBacktab:));
137 EXPECT_EQ(0, model->up_or_down_count());
139 // With popup open verify that tab does autocomplete.
140 popup_view.set_is_open(true);
141 view.OnDoCommandBySelector(@selector(insertTab:));
142 EXPECT_EQ(1, model->up_or_down_count());
143 view.OnDoCommandBySelector(@selector(insertBacktab:));
144 EXPECT_EQ(-1, model->up_or_down_count());
147 TEST_F(OmniboxViewMacTest, SetGrayTextAutocompletion) {
148 const NSRect frame = NSMakeRect(0, 0, 50, 30);
149 base::scoped_nsobject<AutocompleteTextField> field(
150 [[AutocompleteTextField alloc] initWithFrame:frame]);
152 TestingToolbarModelDelegate delegate;
153 ToolbarModelImpl toolbar_model(&delegate);
154 TestingOmniboxEditController controller(&toolbar_model);
155 OmniboxViewMac view(&controller, profile(), NULL, field.get());
157 // This is deleted by the omnibox view.
158 MockOmniboxEditModel* model =
159 new MockOmniboxEditModel(&view, &controller, profile());
160 SetModel(&view, model);
162 MockOmniboxPopupView popup_view;
163 OmniboxPopupModel popup_model(&popup_view, model);
165 view.SetUserText(base::ASCIIToUTF16("Alfred"));
166 EXPECT_EQ("Alfred", base::UTF16ToUTF8(view.GetText()));
167 view.SetGrayTextAutocompletion(base::ASCIIToUTF16(" Hitchcock"));
168 EXPECT_EQ("Alfred", base::UTF16ToUTF8(view.GetText()));
169 EXPECT_EQ(" Hitchcock", base::UTF16ToUTF8(view.GetGrayTextAutocompletion()));
171 view.SetUserText(base::string16());
172 EXPECT_EQ(base::string16(), view.GetText());
173 EXPECT_EQ(base::string16(), view.GetGrayTextAutocompletion());
176 TEST_F(OmniboxViewMacTest, UpDownArrow) {
177 OmniboxViewMac view(NULL, profile(), NULL, NULL);
179 // This is deleted by the omnibox view.
180 MockOmniboxEditModel* model =
181 new MockOmniboxEditModel(&view, NULL, profile());
182 SetModel(&view, model);
184 MockOmniboxPopupView popup_view;
185 OmniboxPopupModel popup_model(&popup_view, model);
187 // With popup closed verify that pressing up and down arrow works.
188 popup_view.set_is_open(false);
189 model->set_up_or_down_count(0);
190 view.OnDoCommandBySelector(@selector(moveDown:));
191 EXPECT_EQ(1, model->up_or_down_count());
192 model->set_up_or_down_count(0);
193 view.OnDoCommandBySelector(@selector(moveUp:));
194 EXPECT_EQ(-1, model->up_or_down_count());
196 // With popup open verify that pressing up and down arrow works.
197 popup_view.set_is_open(true);
198 model->set_up_or_down_count(0);
199 view.OnDoCommandBySelector(@selector(moveDown:));
200 EXPECT_EQ(1, model->up_or_down_count());
201 model->set_up_or_down_count(0);
202 view.OnDoCommandBySelector(@selector(moveUp:));
203 EXPECT_EQ(-1, model->up_or_down_count());