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/chrome_omnibox_client.h"
9 #include "chrome/browser/ui/omnibox/chrome_omnibox_edit_controller.h"
10 #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h"
11 #include "chrome/browser/ui/toolbar/toolbar_model_impl.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "components/omnibox/browser/omnibox_popup_model.h"
14 #include "components/omnibox/browser/omnibox_popup_view.h"
15 #include "testing/platform_test.h"
16 #include "ui/gfx/font.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/gfx/image/image.h"
22 class MockOmniboxEditModel : public OmniboxEditModel {
24 MockOmniboxEditModel(OmniboxView* view,
25 OmniboxEditController* controller,
30 make_scoped_ptr(new ChromeOmniboxClient(controller, profile))),
31 up_or_down_count_(0) {}
33 void OnUpOrDownKeyPressed(int count) override { up_or_down_count_ = count; }
35 int up_or_down_count() const { return up_or_down_count_; }
37 void set_up_or_down_count(int count) {
38 up_or_down_count_ = count;
42 int up_or_down_count_;
44 DISALLOW_COPY_AND_ASSIGN(MockOmniboxEditModel);
47 class MockOmniboxPopupView : public OmniboxPopupView {
49 MockOmniboxPopupView() : is_open_(false) {}
50 ~MockOmniboxPopupView() override {}
52 // Overridden from OmniboxPopupView:
53 bool IsOpen() const override { return is_open_; }
54 void InvalidateLine(size_t line) override {}
55 void UpdatePopupAppearance() override {}
56 gfx::Rect GetTargetBounds() override { return gfx::Rect(); }
57 void PaintUpdatesNow() override {}
58 void OnDragCanceled() override {}
60 void set_is_open(bool is_open) { is_open_ = is_open; }
65 DISALLOW_COPY_AND_ASSIGN(MockOmniboxPopupView);
68 class TestingToolbarModelDelegate : public ToolbarModelDelegate {
70 TestingToolbarModelDelegate() {}
71 ~TestingToolbarModelDelegate() override {}
73 // Overridden from ToolbarModelDelegate:
74 content::WebContents* GetActiveWebContents() const override { return NULL; }
77 DISALLOW_COPY_AND_ASSIGN(TestingToolbarModelDelegate);
80 class TestingOmniboxEditController : public ChromeOmniboxEditController {
82 explicit TestingOmniboxEditController(ToolbarModel* toolbar_model)
83 : ChromeOmniboxEditController(NULL), toolbar_model_(toolbar_model) {}
84 ~TestingOmniboxEditController() override {}
87 // Overridden from ChromeOmniboxEditController:
88 void UpdateWithoutTabRestore() override {}
89 void OnChanged() override {}
90 void OnSetFocus() override {}
91 void ShowURL() override {}
92 ToolbarModel* GetToolbarModel() override { return toolbar_model_; }
93 const ToolbarModel* GetToolbarModel() const override {
94 return toolbar_model_;
96 content::WebContents* GetWebContents() override { return nullptr; }
99 ToolbarModel* toolbar_model_;
101 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController);
106 class OmniboxViewMacTest : public CocoaProfileTest {
108 void SetModel(OmniboxViewMac* view, OmniboxEditModel* model) {
109 view->model_.reset(model);
113 TEST_F(OmniboxViewMacTest, GetFieldFont) {
114 EXPECT_TRUE(OmniboxViewMac::GetFieldFont(gfx::Font::NORMAL));
117 TEST_F(OmniboxViewMacTest, TabToAutocomplete) {
118 OmniboxViewMac view(NULL, profile(), NULL, NULL);
120 // This is deleted by the omnibox view.
121 MockOmniboxEditModel* model =
122 new MockOmniboxEditModel(&view, NULL, profile());
123 SetModel(&view, model);
125 MockOmniboxPopupView popup_view;
126 OmniboxPopupModel popup_model(&popup_view, model);
128 // With popup closed verify that tab doesn't autocomplete.
129 popup_view.set_is_open(false);
130 view.OnDoCommandBySelector(@selector(insertTab:));
131 EXPECT_EQ(0, model->up_or_down_count());
132 view.OnDoCommandBySelector(@selector(insertBacktab:));
133 EXPECT_EQ(0, model->up_or_down_count());
135 // With popup open verify that tab does autocomplete.
136 popup_view.set_is_open(true);
137 view.OnDoCommandBySelector(@selector(insertTab:));
138 EXPECT_EQ(1, model->up_or_down_count());
139 view.OnDoCommandBySelector(@selector(insertBacktab:));
140 EXPECT_EQ(-1, model->up_or_down_count());
143 TEST_F(OmniboxViewMacTest, SetGrayTextAutocompletion) {
144 const NSRect frame = NSMakeRect(0, 0, 50, 30);
145 base::scoped_nsobject<AutocompleteTextField> field(
146 [[AutocompleteTextField alloc] initWithFrame:frame]);
148 TestingToolbarModelDelegate delegate;
149 ToolbarModelImpl toolbar_model(&delegate);
150 TestingOmniboxEditController controller(&toolbar_model);
151 OmniboxViewMac view(&controller, profile(), NULL, field.get());
153 // This is deleted by the omnibox view.
154 MockOmniboxEditModel* model =
155 new MockOmniboxEditModel(&view, &controller, profile());
156 SetModel(&view, model);
158 MockOmniboxPopupView popup_view;
159 OmniboxPopupModel popup_model(&popup_view, model);
161 view.SetUserText(base::ASCIIToUTF16("Alfred"));
162 EXPECT_EQ("Alfred", base::UTF16ToUTF8(view.GetText()));
163 view.SetGrayTextAutocompletion(base::ASCIIToUTF16(" Hitchcock"));
164 EXPECT_EQ("Alfred", base::UTF16ToUTF8(view.GetText()));
165 EXPECT_EQ(" Hitchcock", base::UTF16ToUTF8(view.GetGrayTextAutocompletion()));
167 view.SetUserText(base::string16());
168 EXPECT_EQ(base::string16(), view.GetText());
169 EXPECT_EQ(base::string16(), view.GetGrayTextAutocompletion());
172 TEST_F(OmniboxViewMacTest, UpDownArrow) {
173 OmniboxViewMac view(NULL, profile(), NULL, NULL);
175 // This is deleted by the omnibox view.
176 MockOmniboxEditModel* model =
177 new MockOmniboxEditModel(&view, NULL, profile());
178 SetModel(&view, model);
180 MockOmniboxPopupView popup_view;
181 OmniboxPopupModel popup_model(&popup_view, model);
183 // With popup closed verify that pressing up and down arrow works.
184 popup_view.set_is_open(false);
185 model->set_up_or_down_count(0);
186 view.OnDoCommandBySelector(@selector(moveDown:));
187 EXPECT_EQ(1, model->up_or_down_count());
188 model->set_up_or_down_count(0);
189 view.OnDoCommandBySelector(@selector(moveUp:));
190 EXPECT_EQ(-1, model->up_or_down_count());
192 // With popup open verify that pressing up and down arrow works.
193 popup_view.set_is_open(true);
194 model->set_up_or_down_count(0);
195 view.OnDoCommandBySelector(@selector(moveDown:));
196 EXPECT_EQ(1, model->up_or_down_count());
197 model->set_up_or_down_count(0);
198 view.OnDoCommandBySelector(@selector(moveUp:));
199 EXPECT_EQ(-1, model->up_or_down_count());