1 // Copyright 2013 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 "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h"
7 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
8 #include "chrome/test/base/testing_profile.h"
9 #include "components/omnibox/browser/autocomplete_controller.h"
10 #include "components/omnibox/browser/autocomplete_provider.h"
11 #include "components/omnibox/browser/omnibox_client.h"
12 #include "components/omnibox/browser/omnibox_controller.h"
13 #include "components/sessions/session_id.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "testing/gtest/include/gtest/gtest.h"
18 class TestOmniboxClient
: public OmniboxClient
{
20 explicit TestOmniboxClient(Profile
* profile
)
22 scheme_classifier_(profile
) {}
23 ~TestOmniboxClient() override
{}
26 scoped_ptr
<AutocompleteProviderClient
> CreateAutocompleteProviderClient()
28 return make_scoped_ptr(new ChromeAutocompleteProviderClient(profile_
));
30 scoped_ptr
<OmniboxNavigationObserver
> CreateOmniboxNavigationObserver(
31 const base::string16
& text
,
32 const AutocompleteMatch
& match
,
33 const AutocompleteMatch
& alternate_nav_match
) override
{
36 bool CurrentPageExists() const override
{ return true; }
37 const GURL
& GetURL() const override
{ return GURL::EmptyGURL(); }
38 const base::string16
& GetTitle() const override
{
39 return base::EmptyString16();
41 gfx::Image
GetFavicon() const override
{ return gfx::Image(); }
42 bool IsInstantNTP() const override
{ return false; }
43 bool IsSearchResultsPage() const override
{ return false; }
44 bool IsLoading() const override
{ return false; }
45 bool IsPasteAndGoEnabled() const override
{ return false; }
46 bool IsNewTabPage(const std::string
& url
) const override
{ return false; }
47 bool IsHomePage(const std::string
& url
) const override
{ return false; }
48 const SessionID
& GetSessionID() const override
{ return session_id_
; }
49 bookmarks::BookmarkModel
* GetBookmarkModel() override
{ return nullptr; }
50 TemplateURLService
* GetTemplateURLService() override
{ return nullptr; }
51 const AutocompleteSchemeClassifier
& GetSchemeClassifier() const override
{
52 return scheme_classifier_
;
54 AutocompleteClassifier
* GetAutocompleteClassifier() override
{
57 gfx::Image
GetIconIfExtensionMatch(
58 const AutocompleteMatch
& match
) const override
{
61 bool ProcessExtensionKeyword(TemplateURL
* template_url
,
62 const AutocompleteMatch
& match
,
63 WindowOpenDisposition disposition
,
64 OmniboxNavigationObserver
* observer
) override
{
67 void OnInputStateChanged() override
{}
68 void OnFocusChanged(OmniboxFocusState state
,
69 OmniboxFocusChangeReason reason
) override
{}
70 void OnResultChanged(const AutocompleteResult
& result
,
71 bool default_match_changed
,
72 const base::Callback
<void(const SkBitmap
& bitmap
)>&
73 on_bitmap_fetched
) override
{}
74 void OnCurrentMatchChanged(const AutocompleteMatch
& match
) override
{}
75 void OnTextChanged(const AutocompleteMatch
& current_match
,
76 bool user_input_in_progress
,
77 base::string16
& user_text
,
78 const AutocompleteResult
& result
,
80 bool has_focus
) override
{}
81 void OnInputAccepted(const AutocompleteMatch
& match
) override
{}
82 void OnRevert() override
{}
83 void OnURLOpenedFromOmnibox(OmniboxLog
* log
) override
{}
84 void OnBookmarkLaunched() override
{}
85 void DiscardNonCommittedNavigations() override
{}
89 ChromeAutocompleteSchemeClassifier scheme_classifier_
;
90 SessionID session_id_
;
92 DISALLOW_COPY_AND_ASSIGN(TestOmniboxClient
);
97 class OmniboxControllerTest
: public testing::Test
{
99 OmniboxControllerTest();
100 ~OmniboxControllerTest() override
;
102 void CreateController();
103 void AssertProviders(int expected_providers
);
105 const AutocompleteController::Providers
& GetAutocompleteProviders() const {
106 return omnibox_controller_
->autocomplete_controller()->providers();
111 void SetUp() override
;
112 void TearDown() override
;
114 content::TestBrowserThreadBundle thread_bundle_
;
115 TestingProfile profile_
;
116 scoped_ptr
<TestOmniboxClient
> omnibox_client_
;
117 scoped_ptr
<OmniboxController
> omnibox_controller_
;
119 DISALLOW_COPY_AND_ASSIGN(OmniboxControllerTest
);
122 OmniboxControllerTest::OmniboxControllerTest() {
125 OmniboxControllerTest::~OmniboxControllerTest() {
128 void OmniboxControllerTest::CreateController() {
129 DCHECK(omnibox_client_
);
130 omnibox_controller_
.reset(new OmniboxController(NULL
, omnibox_client_
.get()));
133 // Checks that the list of autocomplete providers used by the OmniboxController
134 // matches the one in the |expected_providers| bit field.
135 void OmniboxControllerTest::AssertProviders(int expected_providers
) {
136 const AutocompleteController::Providers
& providers
=
137 GetAutocompleteProviders();
139 for (size_t i
= 0; i
< providers
.size(); ++i
) {
140 // Ensure this is a provider we wanted.
141 int type
= providers
[i
]->type();
142 ASSERT_TRUE(expected_providers
& type
);
144 // Remove it from expectations so we fail if it's there twice.
145 expected_providers
&= ~type
;
148 // Ensure we saw all the providers we expected.
149 ASSERT_EQ(0, expected_providers
);
152 void OmniboxControllerTest::SetUp() {
153 omnibox_client_
.reset(new TestOmniboxClient(&profile_
));
156 void OmniboxControllerTest::TearDown() {
157 omnibox_controller_
.reset();
158 omnibox_client_
.reset();
161 TEST_F(OmniboxControllerTest
, CheckDefaultAutocompleteProviders
) {
163 // First collect the basic providers.
164 int observed_providers
= 0;
165 const AutocompleteController::Providers
& providers
=
166 GetAutocompleteProviders();
167 for (size_t i
= 0; i
< providers
.size(); ++i
)
168 observed_providers
|= providers
[i
]->type();
169 // Ensure we have at least one provider.
170 ASSERT_NE(0, observed_providers
);
172 // Ensure instant extended includes all the provides in classic Chrome.
173 int providers_with_instant_extended
= observed_providers
;
174 // TODO(beaudoin): remove TYPE_SEARCH once it's no longer needed to pass
175 // the Instant suggestion through via FinalizeInstantQuery.
177 AssertProviders(providers_with_instant_extended
);