1 // Copyright 2014 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/app_list/search/omnibox_result.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/ui/app_list/app_list_test_util.h"
10 #include "chrome/browser/ui/app_list/test/test_app_list_controller_delegate.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/omnibox/browser/autocomplete_match.h"
13 #include "components/omnibox/browser/autocomplete_match_type.h"
14 #include "components/search_engines/template_url.h"
21 const char kFullQuery
[] = "Hello World";
22 const char kPartialQuery
[] = "Hello Wo";
23 const char kExampleDescription
[] = "A website";
24 const char kExampleUrl
[] = "http://example.com/hello";
25 const int kRelevance
= 750;
26 const double kAppListRelevance
= 0.5;
28 const char kWeatherQuery
[] = "weather";
29 const char kExampleKeyword
[] = "example.com";
30 const char kExampleWeatherUrl
[] = "http://example.com/weather";
31 // Typically, this would be "google", "google.com" or "google.com.<country>",
32 // but users can change it to whatever they want, so we assume they have.
33 const char kGoogleKeyword
[] = "mygoog";
34 const char kGoogleDescription
[] = "Google Search";
35 const char kGoogleWeatherUrl
[] = "http://google.com/search?q=weather";
36 const char kGoogleInternationalWeatherUrl
[] =
37 "http://google.com.au/search?q=weather";
41 class OmniboxResultTest
: public AppListTestBase
{
43 OmniboxResultTest() {}
44 ~OmniboxResultTest() override
{}
46 // AppListTestBase overrides:
47 void SetUp() override
{
48 AppListTestBase::SetUp();
50 app_list_controller_delegate_
.reset(
51 new ::test::TestAppListControllerDelegate
);
54 scoped_ptr
<OmniboxResult
> CreateOmniboxResult(
55 const std::string
& original_query
,
57 const std::string
& destination_url
,
58 const std::string
& contents
,
59 const std::string
& description
,
60 AutocompleteMatchType::Type type
,
61 const std::string
& keyword
,
62 bool is_voice_query
) {
63 AutocompleteMatch match
;
64 match
.search_terms_args
.reset(
65 new TemplateURLRef::SearchTermsArgs(base::UTF8ToUTF16(original_query
)));
66 match
.search_terms_args
->original_query
= base::UTF8ToUTF16(original_query
);
67 match
.relevance
= relevance
;
68 match
.destination_url
= GURL(destination_url
);
69 match
.contents
= base::UTF8ToUTF16(contents
);
70 match
.description
= base::UTF8ToUTF16(description
);
72 match
.keyword
= base::UTF8ToUTF16(keyword
);
74 return scoped_ptr
<OmniboxResult
>(
75 new OmniboxResult(profile_
.get(), app_list_controller_delegate_
.get(),
76 nullptr, is_voice_query
, match
));
79 const GURL
& GetLastOpenedUrl() const {
80 return app_list_controller_delegate_
->last_opened_url();
84 scoped_ptr
<::test::TestAppListControllerDelegate
>
85 app_list_controller_delegate_
;
87 DISALLOW_COPY_AND_ASSIGN(OmniboxResultTest
);
90 TEST_F(OmniboxResultTest
, Basic
) {
91 scoped_ptr
<OmniboxResult
> result
= CreateOmniboxResult(
92 kFullQuery
, kRelevance
, kExampleUrl
, kFullQuery
, kExampleDescription
,
93 AutocompleteMatchType::HISTORY_URL
, kExampleKeyword
, false);
95 EXPECT_EQ(base::ASCIIToUTF16(kFullQuery
), result
->title());
96 EXPECT_EQ(base::ASCIIToUTF16(kExampleDescription
), result
->details());
97 EXPECT_EQ(kAppListRelevance
, result
->relevance());
98 EXPECT_FALSE(result
->voice_result());
101 EXPECT_EQ(kExampleUrl
, GetLastOpenedUrl().spec());
104 TEST_F(OmniboxResultTest
, VoiceResult
) {
105 // Searching for part of a word, and getting a HISTORY_URL result with that
106 // exact string in the title. Should not be automatically launchable as a
107 // voice result (because it is not a web search type result).
109 scoped_ptr
<OmniboxResult
> result
= CreateOmniboxResult(
110 kPartialQuery
, kRelevance
, kExampleUrl
, kPartialQuery
,
111 kExampleDescription
, AutocompleteMatchType::HISTORY_URL
,
112 kExampleKeyword
, false);
113 EXPECT_FALSE(result
->voice_result());
116 // Searching for part of a word, and getting a SEARCH_WHAT_YOU_TYPED result.
117 // Such a result should be promoted as a voice result.
119 scoped_ptr
<OmniboxResult
> result
= CreateOmniboxResult(
120 kPartialQuery
, kRelevance
, kExampleUrl
, kPartialQuery
,
121 kExampleDescription
, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
,
122 kExampleKeyword
, false);
123 EXPECT_TRUE(result
->voice_result());
126 // Searching for part of a word, and getting a SEARCH_HISTORY result for that
127 // exact string. Such a result should be promoted as a voice result.
129 scoped_ptr
<OmniboxResult
> result
= CreateOmniboxResult(
130 kPartialQuery
, kRelevance
, kExampleUrl
, kPartialQuery
,
131 kExampleDescription
, AutocompleteMatchType::SEARCH_HISTORY
,
132 kExampleKeyword
, false);
133 EXPECT_TRUE(result
->voice_result());
136 // Searching for part of a word, and getting a SEARCH_HISTORY result for a
137 // different string. Should not be automatically launchable as a voice result
138 // (because it does not exactly match what you typed).
140 scoped_ptr
<OmniboxResult
> result
= CreateOmniboxResult(
141 kPartialQuery
, kRelevance
, kExampleUrl
, kFullQuery
, kExampleDescription
,
142 AutocompleteMatchType::SEARCH_HISTORY
, kExampleKeyword
, false);
143 EXPECT_FALSE(result
->voice_result());
147 TEST_F(OmniboxResultTest
, VoiceQuery
) {
148 // A voice query to a random domain. URL should not change.
150 scoped_ptr
<OmniboxResult
> result
= CreateOmniboxResult(
151 kWeatherQuery
, kRelevance
, kExampleWeatherUrl
, kWeatherQuery
,
152 kExampleDescription
, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
,
153 kExampleKeyword
, true);
155 EXPECT_EQ(kExampleWeatherUrl
, GetLastOpenedUrl().spec());
158 // A voice query to a Google domain. URL should have magic "speak back" query
159 // parameter appended.
161 scoped_ptr
<OmniboxResult
> result
= CreateOmniboxResult(
162 kWeatherQuery
, kRelevance
, kGoogleWeatherUrl
, kWeatherQuery
,
163 kGoogleDescription
, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
,
164 kGoogleKeyword
, true);
166 EXPECT_EQ("http://google.com/search?q=weather&gs_ivs=1",
167 GetLastOpenedUrl().spec());
170 // A voice query to a Google international domain. URL should have magic
171 // "speak back" query parameter appended.
173 scoped_ptr
<OmniboxResult
> result
= CreateOmniboxResult(
174 kWeatherQuery
, kRelevance
, kGoogleInternationalWeatherUrl
,
175 kWeatherQuery
, kGoogleDescription
,
176 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
, kGoogleKeyword
, true);
178 EXPECT_EQ("http://google.com.au/search?q=weather&gs_ivs=1",
179 GetLastOpenedUrl().spec());
182 // A non-voice query to a Google domain. URL should not change.
184 scoped_ptr
<OmniboxResult
> result
= CreateOmniboxResult(
185 kWeatherQuery
, kRelevance
, kGoogleWeatherUrl
, kWeatherQuery
,
186 kGoogleDescription
, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
,
187 kGoogleKeyword
, false);
189 EXPECT_EQ(kGoogleWeatherUrl
, GetLastOpenedUrl().spec());
194 } // namespace app_list