1 // Copyright (c) 2015 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 "chrome/browser/ui/views/omnibox/omnibox_view_views.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/command_updater.h"
9 #include "chrome/browser/ui/omnibox/chrome_omnibox_edit_controller.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 #if defined(OS_CHROMEOS)
15 #include "chrome/browser/chromeos/input_method/input_method_configuration.h"
16 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h"
21 class TestingOmniboxViewViews
: public OmniboxViewViews
{
23 TestingOmniboxViewViews(OmniboxEditController
* controller
,
25 CommandUpdater
* command_updater
)
26 : OmniboxViewViews(controller
, profile
, command_updater
, false, NULL
,
28 update_popup_call_count_(0) {
31 void CheckUpdatePopupCallInfo(size_t call_count
, const base::string16
& text
,
32 const gfx::Range
& selection_range
) {
33 EXPECT_EQ(call_count
, update_popup_call_count_
);
34 EXPECT_EQ(text
, update_popup_text_
);
35 EXPECT_EQ(selection_range
, update_popup_selection_range_
);
40 void UpdatePopup() override
{
41 ++update_popup_call_count_
;
42 update_popup_text_
= text();
43 update_popup_selection_range_
= GetSelectedRange();
46 size_t update_popup_call_count_
;
47 base::string16 update_popup_text_
;
48 gfx::Range update_popup_selection_range_
;
50 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxViewViews
);
53 class TestingOmniboxEditController
: public ChromeOmniboxEditController
{
55 explicit TestingOmniboxEditController(CommandUpdater
* command_updater
)
56 : ChromeOmniboxEditController(command_updater
) {}
59 // ChromeOmniboxEditController:
60 void UpdateWithoutTabRestore() override
{}
61 void OnChanged() override
{}
62 void OnSetFocus() override
{}
63 void ShowURL() override
{}
64 ToolbarModel
* GetToolbarModel() override
{ return nullptr; }
65 const ToolbarModel
* GetToolbarModel() const override
{ return nullptr; }
66 content::WebContents
* GetWebContents() override
{ return nullptr; }
69 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController
);
74 class OmniboxViewViewsTest
: public testing::Test
{
76 OmniboxViewViewsTest()
77 : command_updater_(NULL
),
78 omnibox_edit_controller_(&command_updater_
) {
81 TestingOmniboxViewViews
* omnibox_view() {
82 return omnibox_view_
.get();
85 views::Textfield
* omnibox_textfield() {
86 return omnibox_view();
91 void SetUp() override
{
92 #if defined(OS_CHROMEOS)
93 chromeos::input_method::InitializeForTesting(
94 new chromeos::input_method::MockInputMethodManager
);
96 omnibox_view_
.reset(new TestingOmniboxViewViews(
97 &omnibox_edit_controller_
, &profile_
, &command_updater_
));
98 omnibox_view_
->Init();
101 void TearDown() override
{
102 omnibox_view_
.reset();
103 #if defined(OS_CHROMEOS)
104 chromeos::input_method::Shutdown();
108 content::TestBrowserThreadBundle thread_bundle_
;
109 TestingProfile profile_
;
110 CommandUpdater command_updater_
;
111 TestingOmniboxEditController omnibox_edit_controller_
;
112 scoped_ptr
<TestingOmniboxViewViews
> omnibox_view_
;
115 // Checks that a single change of the text in the omnibox invokes
116 // only one call to OmniboxViewViews::UpdatePopup().
117 TEST_F(OmniboxViewViewsTest
, UpdatePopupCall
) {
118 omnibox_textfield()->InsertChar('a', 0);
119 omnibox_view()->CheckUpdatePopupCallInfo(
120 1, base::ASCIIToUTF16("a"), gfx::Range(1));
122 omnibox_textfield()->InsertChar('b', 0);
123 omnibox_view()->CheckUpdatePopupCallInfo(
124 2, base::ASCIIToUTF16("ab"), gfx::Range(2));
126 ui::KeyEvent
pressed(ui::ET_KEY_PRESSED
, ui::VKEY_BACK
, 0);
127 omnibox_textfield()->OnKeyPressed(pressed
);
128 omnibox_view()->CheckUpdatePopupCallInfo(
129 3, base::ASCIIToUTF16("a"), gfx::Range(1));