| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/strings/string16.h" | 5 #include "base/strings/string16.h" |
| 6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "chrome/browser/autocomplete/autocomplete_controller.h" | 7 #include "chrome/browser/autocomplete/autocomplete_controller.h" |
| 8 #include "chrome/browser/autocomplete/autocomplete_input.h" | 8 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 9 #include "chrome/browser/autocomplete/autocomplete_match.h" | 9 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 10 #include "chrome/browser/autocomplete/autocomplete_result.h" | 10 #include "chrome/browser/autocomplete/autocomplete_result.h" |
| 11 #include "chrome/browser/extensions/api/omnibox/omnibox_api_testbase.h" | 11 #include "chrome/browser/extensions/api/omnibox/omnibox_api_testbase.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/search_engines/template_url_service_factory.h" | 13 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/omnibox/location_bar.h" | 15 #include "chrome/browser/ui/omnibox/location_bar.h" |
| 16 #include "chrome/browser/ui/omnibox/omnibox_view.h" | 16 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
| 17 #include "chrome/test/base/ui_test_utils.h" | 17 #include "chrome/test/base/ui_test_utils.h" |
| 18 #include "components/metrics/proto/omnibox_event.pb.h" |
| 18 #include "ui/base/window_open_disposition.h" | 19 #include "ui/base/window_open_disposition.h" |
| 19 | 20 |
| 20 using base::ASCIIToUTF16; | 21 using base::ASCIIToUTF16; |
| 22 using metrics::OmniboxEventProto; |
| 21 | 23 |
| 22 IN_PROC_BROWSER_TEST_F(OmniboxApiTest, Basic) { | 24 IN_PROC_BROWSER_TEST_F(OmniboxApiTest, Basic) { |
| 23 ASSERT_TRUE(RunExtensionTest("omnibox")) << message_; | 25 ASSERT_TRUE(RunExtensionTest("omnibox")) << message_; |
| 24 | 26 |
| 25 // The results depend on the TemplateURLService being loaded. Make sure it is | 27 // The results depend on the TemplateURLService being loaded. Make sure it is |
| 26 // loaded so that the autocomplete results are consistent. | 28 // loaded so that the autocomplete results are consistent. |
| 27 ui_test_utils::WaitForTemplateURLServiceToLoad( | 29 ui_test_utils::WaitForTemplateURLServiceToLoad( |
| 28 TemplateURLServiceFactory::GetForProfile(browser()->profile())); | 30 TemplateURLServiceFactory::GetForProfile(browser()->profile())); |
| 29 | 31 |
| 30 AutocompleteController* autocomplete_controller = | 32 AutocompleteController* autocomplete_controller = |
| 31 GetAutocompleteController(browser()); | 33 GetAutocompleteController(browser()); |
| 32 | 34 |
| 33 // Test that our extension's keyword is suggested to us when we partially type | 35 // Test that our extension's keyword is suggested to us when we partially type |
| 34 // it. | 36 // it. |
| 35 { | 37 { |
| 36 autocomplete_controller->Start( | 38 autocomplete_controller->Start( |
| 37 AutocompleteInput(ASCIIToUTF16("keywor"), base::string16::npos, | 39 AutocompleteInput(ASCIIToUTF16("keywor"), base::string16::npos, |
| 38 base::string16(), GURL(), AutocompleteInput::NTP, | 40 base::string16(), GURL(), OmniboxEventProto::NTP, |
| 39 true, false, true, true)); | 41 true, false, true, true)); |
| 40 WaitForAutocompleteDone(autocomplete_controller); | 42 WaitForAutocompleteDone(autocomplete_controller); |
| 41 EXPECT_TRUE(autocomplete_controller->done()); | 43 EXPECT_TRUE(autocomplete_controller->done()); |
| 42 | 44 |
| 43 // Now, peek into the controller to see if it has the results we expect. | 45 // Now, peek into the controller to see if it has the results we expect. |
| 44 // First result should be to search for what was typed, second should be to | 46 // First result should be to search for what was typed, second should be to |
| 45 // enter "extension keyword" mode. | 47 // enter "extension keyword" mode. |
| 46 const AutocompleteResult& result = autocomplete_controller->result(); | 48 const AutocompleteResult& result = autocomplete_controller->result(); |
| 47 ASSERT_EQ(2U, result.size()) << AutocompleteResultAsString(result); | 49 ASSERT_EQ(2U, result.size()) << AutocompleteResultAsString(result); |
| 48 AutocompleteMatch match = result.match_at(0); | 50 AutocompleteMatch match = result.match_at(0); |
| 49 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, match.type); | 51 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, match.type); |
| 50 EXPECT_FALSE(match.deletable); | 52 EXPECT_FALSE(match.deletable); |
| 51 | 53 |
| 52 match = result.match_at(1); | 54 match = result.match_at(1); |
| 53 EXPECT_EQ(ASCIIToUTF16("keyword"), match.keyword); | 55 EXPECT_EQ(ASCIIToUTF16("keyword"), match.keyword); |
| 54 } | 56 } |
| 55 | 57 |
| 56 // Test that our extension can send suggestions back to us. | 58 // Test that our extension can send suggestions back to us. |
| 57 { | 59 { |
| 58 autocomplete_controller->Start( | 60 autocomplete_controller->Start( |
| 59 AutocompleteInput(ASCIIToUTF16("keyword suggestio"), | 61 AutocompleteInput(ASCIIToUTF16("keyword suggestio"), |
| 60 base::string16::npos, base::string16(), GURL(), | 62 base::string16::npos, base::string16(), GURL(), |
| 61 AutocompleteInput::NTP, true, false, true, true)); | 63 OmniboxEventProto::NTP, true, false, true, true)); |
| 62 WaitForAutocompleteDone(autocomplete_controller); | 64 WaitForAutocompleteDone(autocomplete_controller); |
| 63 EXPECT_TRUE(autocomplete_controller->done()); | 65 EXPECT_TRUE(autocomplete_controller->done()); |
| 64 | 66 |
| 65 // Now, peek into the controller to see if it has the results we expect. | 67 // Now, peek into the controller to see if it has the results we expect. |
| 66 // First result should be to invoke the keyword with what we typed, 2-4 | 68 // First result should be to invoke the keyword with what we typed, 2-4 |
| 67 // should be to invoke with suggestions from the extension, and the last | 69 // should be to invoke with suggestions from the extension, and the last |
| 68 // should be to search for what we typed. | 70 // should be to search for what we typed. |
| 69 const AutocompleteResult& result = autocomplete_controller->result(); | 71 const AutocompleteResult& result = autocomplete_controller->result(); |
| 70 ASSERT_EQ(5U, result.size()) << AutocompleteResultAsString(result); | 72 ASSERT_EQ(5U, result.size()) << AutocompleteResultAsString(result); |
| 71 | 73 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 OmniboxView* omnibox_view = location_bar->GetOmniboxView(); | 162 OmniboxView* omnibox_view = location_bar->GetOmniboxView(); |
| 161 ResultCatcher catcher; | 163 ResultCatcher catcher; |
| 162 AutocompleteController* autocomplete_controller = | 164 AutocompleteController* autocomplete_controller = |
| 163 GetAutocompleteController(browser()); | 165 GetAutocompleteController(browser()); |
| 164 omnibox_view->OnBeforePossibleChange(); | 166 omnibox_view->OnBeforePossibleChange(); |
| 165 omnibox_view->SetUserText(ASCIIToUTF16("keyword command")); | 167 omnibox_view->SetUserText(ASCIIToUTF16("keyword command")); |
| 166 omnibox_view->OnAfterPossibleChange(); | 168 omnibox_view->OnAfterPossibleChange(); |
| 167 | 169 |
| 168 autocomplete_controller->Start( | 170 autocomplete_controller->Start( |
| 169 AutocompleteInput(ASCIIToUTF16("keyword command"), base::string16::npos, | 171 AutocompleteInput(ASCIIToUTF16("keyword command"), base::string16::npos, |
| 170 base::string16(), GURL(), AutocompleteInput::NTP, | 172 base::string16(), GURL(), OmniboxEventProto::NTP, |
| 171 true, false, true, true)); | 173 true, false, true, true)); |
| 172 omnibox_view->model()->AcceptInput(CURRENT_TAB, false); | 174 omnibox_view->model()->AcceptInput(CURRENT_TAB, false); |
| 173 WaitForAutocompleteDone(autocomplete_controller); | 175 WaitForAutocompleteDone(autocomplete_controller); |
| 174 EXPECT_TRUE(autocomplete_controller->done()); | 176 EXPECT_TRUE(autocomplete_controller->done()); |
| 175 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 177 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 176 | 178 |
| 177 omnibox_view->OnBeforePossibleChange(); | 179 omnibox_view->OnBeforePossibleChange(); |
| 178 omnibox_view->SetUserText(ASCIIToUTF16("keyword newtab")); | 180 omnibox_view->SetUserText(ASCIIToUTF16("keyword newtab")); |
| 179 omnibox_view->OnAfterPossibleChange(); | 181 omnibox_view->OnAfterPossibleChange(); |
| 180 WaitForAutocompleteDone(autocomplete_controller); | 182 WaitForAutocompleteDone(autocomplete_controller); |
| 181 EXPECT_TRUE(autocomplete_controller->done()); | 183 EXPECT_TRUE(autocomplete_controller->done()); |
| 182 | 184 |
| 183 autocomplete_controller->Start( | 185 autocomplete_controller->Start( |
| 184 AutocompleteInput(ASCIIToUTF16("keyword newtab"), base::string16::npos, | 186 AutocompleteInput(ASCIIToUTF16("keyword newtab"), base::string16::npos, |
| 185 base::string16(), GURL(), AutocompleteInput::NTP, | 187 base::string16(), GURL(), OmniboxEventProto::NTP, |
| 186 true, false, true, true)); | 188 true, false, true, true)); |
| 187 omnibox_view->model()->AcceptInput(NEW_FOREGROUND_TAB, false); | 189 omnibox_view->model()->AcceptInput(NEW_FOREGROUND_TAB, false); |
| 188 WaitForAutocompleteDone(autocomplete_controller); | 190 WaitForAutocompleteDone(autocomplete_controller); |
| 189 EXPECT_TRUE(autocomplete_controller->done()); | 191 EXPECT_TRUE(autocomplete_controller->done()); |
| 190 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 192 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 191 } | 193 } |
| 192 | 194 |
| 193 // Tests that we get suggestions from and send input to the incognito context | 195 // Tests that we get suggestions from and send input to the incognito context |
| 194 // of an incognito split mode extension. | 196 // of an incognito split mode extension. |
| 195 // http://crbug.com/100927 | 197 // http://crbug.com/100927 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 213 | 215 |
| 214 LocationBar* location_bar = GetLocationBar(incognito_browser); | 216 LocationBar* location_bar = GetLocationBar(incognito_browser); |
| 215 AutocompleteController* autocomplete_controller = | 217 AutocompleteController* autocomplete_controller = |
| 216 GetAutocompleteController(incognito_browser); | 218 GetAutocompleteController(incognito_browser); |
| 217 | 219 |
| 218 // Test that we get the incognito-specific suggestions. | 220 // Test that we get the incognito-specific suggestions. |
| 219 { | 221 { |
| 220 autocomplete_controller->Start( | 222 autocomplete_controller->Start( |
| 221 AutocompleteInput(ASCIIToUTF16("keyword suggestio"), | 223 AutocompleteInput(ASCIIToUTF16("keyword suggestio"), |
| 222 base::string16::npos, base::string16(), GURL(), | 224 base::string16::npos, base::string16(), GURL(), |
| 223 AutocompleteInput::NTP, true, false, true, true)); | 225 OmniboxEventProto::NTP, true, false, true, true)); |
| 224 WaitForAutocompleteDone(autocomplete_controller); | 226 WaitForAutocompleteDone(autocomplete_controller); |
| 225 EXPECT_TRUE(autocomplete_controller->done()); | 227 EXPECT_TRUE(autocomplete_controller->done()); |
| 226 | 228 |
| 227 // First result should be to invoke the keyword with what we typed, 2-4 | 229 // First result should be to invoke the keyword with what we typed, 2-4 |
| 228 // should be to invoke with suggestions from the extension, and the last | 230 // should be to invoke with suggestions from the extension, and the last |
| 229 // should be to search for what we typed. | 231 // should be to search for what we typed. |
| 230 const AutocompleteResult& result = autocomplete_controller->result(); | 232 const AutocompleteResult& result = autocomplete_controller->result(); |
| 231 ASSERT_EQ(5U, result.size()) << AutocompleteResultAsString(result); | 233 ASSERT_EQ(5U, result.size()) << AutocompleteResultAsString(result); |
| 232 ASSERT_FALSE(result.match_at(0).keyword.empty()); | 234 ASSERT_FALSE(result.match_at(0).keyword.empty()); |
| 233 EXPECT_EQ(ASCIIToUTF16("keyword suggestion3 incognito"), | 235 EXPECT_EQ(ASCIIToUTF16("keyword suggestion3 incognito"), |
| 234 result.match_at(3).fill_into_edit); | 236 result.match_at(3).fill_into_edit); |
| 235 } | 237 } |
| 236 | 238 |
| 237 // Test that our input is sent to the incognito context. The test will do a | 239 // Test that our input is sent to the incognito context. The test will do a |
| 238 // text comparison and succeed only if "command incognito" is sent to the | 240 // text comparison and succeed only if "command incognito" is sent to the |
| 239 // incognito context. | 241 // incognito context. |
| 240 { | 242 { |
| 241 ResultCatcher catcher; | 243 ResultCatcher catcher; |
| 242 autocomplete_controller->Start( | 244 autocomplete_controller->Start( |
| 243 AutocompleteInput(ASCIIToUTF16("keyword command incognito"), | 245 AutocompleteInput(ASCIIToUTF16("keyword command incognito"), |
| 244 base::string16::npos, base::string16(), GURL(), | 246 base::string16::npos, base::string16(), GURL(), |
| 245 AutocompleteInput::NTP, true, false, true, true)); | 247 OmniboxEventProto::NTP, true, false, true, true)); |
| 246 location_bar->AcceptInput(); | 248 location_bar->AcceptInput(); |
| 247 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 249 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 248 } | 250 } |
| 249 } | 251 } |
| OLD | NEW |