OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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. |
| 4 |
| 5 #include "chrome/browser/autocomplete/autocomplete.h" |
| 6 #include "chrome/browser/autocomplete/autocomplete_edit.h" |
| 7 #include "chrome/browser/autocomplete/autocomplete_edit_view.h" |
| 8 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" |
| 9 #include "chrome/browser/browser.h" |
| 10 #include "chrome/browser/browser_window.h" |
| 11 #include "chrome/browser/extensions/extension_apitest.h" |
| 12 #include "chrome/browser/history/history.h" |
| 13 #include "chrome/browser/location_bar.h" |
| 14 #include "chrome/browser/profile.h" |
| 15 #include "chrome/common/chrome_switches.h" |
| 16 #include "chrome/common/notification_type.h" |
| 17 #include "chrome/common/url_constants.h" |
| 18 #include "chrome/test/ui_test_utils.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 std::wstring AutocompleteResultAsString(const AutocompleteResult& result) { |
| 23 std::wstring output(StringPrintf(L"{%d} ", result.size())); |
| 24 for (size_t i = 0; i < result.size(); ++i) { |
| 25 AutocompleteMatch match = result.match_at(i); |
| 26 std::wstring provider_name(ASCIIToWide(match.provider->name())); |
| 27 output.append(StringPrintf(L"[\"%ls\" by \"%ls\"] ", |
| 28 match.contents.c_str(), |
| 29 provider_name.c_str())); |
| 30 } |
| 31 return output; |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
| 36 class OmniboxApiTest : public ExtensionApiTest { |
| 37 protected: |
| 38 LocationBar* GetLocationBar() const { |
| 39 return browser()->window()->GetLocationBar(); |
| 40 } |
| 41 |
| 42 AutocompleteController* GetAutocompleteController() const { |
| 43 return GetLocationBar()->location_entry()->model()->popup_model()-> |
| 44 autocomplete_controller(); |
| 45 } |
| 46 |
| 47 void WaitForHistoryBackendToLoad() { |
| 48 HistoryService* history_service = |
| 49 browser()->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 50 if (!history_service->BackendLoaded()) |
| 51 ui_test_utils::WaitForNotification(NotificationType::HISTORY_LOADED); |
| 52 } |
| 53 |
| 54 void WaitForAutocompleteDone(AutocompleteController* controller) { |
| 55 while (!controller->done()) { |
| 56 ui_test_utils::WaitForNotification( |
| 57 NotificationType::AUTOCOMPLETE_CONTROLLER_DEFAULT_MATCH_UPDATED); |
| 58 } |
| 59 } |
| 60 }; |
| 61 |
| 62 IN_PROC_BROWSER_TEST_F(OmniboxApiTest, Basic) { |
| 63 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 64 switches::kEnableExperimentalExtensionApis); |
| 65 |
| 66 ASSERT_TRUE(StartHTTPServer()); |
| 67 ASSERT_TRUE(RunExtensionTest("omnibox")) << message_; |
| 68 |
| 69 // The results depend on the history backend being loaded. Make sure it is |
| 70 // loaded so that the autocomplete results are consistent. |
| 71 WaitForHistoryBackendToLoad(); |
| 72 |
| 73 LocationBar* location_bar = GetLocationBar(); |
| 74 AutocompleteController* autocomplete_controller = GetAutocompleteController(); |
| 75 |
| 76 // Test that our extension's keyword is suggested to us when we partially type |
| 77 // it. |
| 78 { |
| 79 autocomplete_controller->Start(L"keywor", std::wstring(), |
| 80 true, false, false); |
| 81 |
| 82 WaitForAutocompleteDone(autocomplete_controller); |
| 83 EXPECT_TRUE(autocomplete_controller->done()); |
| 84 EXPECT_EQ(std::wstring(), location_bar->GetInputString()); |
| 85 EXPECT_EQ(std::wstring(), location_bar->location_entry()->GetText()); |
| 86 EXPECT_TRUE(location_bar->location_entry()->IsSelectAll()); |
| 87 |
| 88 // First result should be to search for what was typed, second should be to |
| 89 // enter "extension keyword" mode. |
| 90 const AutocompleteResult& result = autocomplete_controller->result(); |
| 91 ASSERT_EQ(2U, result.size()) << AutocompleteResultAsString(result); |
| 92 AutocompleteMatch match = result.match_at(0); |
| 93 EXPECT_EQ(AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, match.type); |
| 94 EXPECT_FALSE(match.deletable); |
| 95 |
| 96 match = result.match_at(1); |
| 97 ASSERT_TRUE(match.template_url); |
| 98 EXPECT_TRUE(match.template_url->IsExtensionKeyword()); |
| 99 EXPECT_EQ(L"keyword", match.template_url->keyword()); |
| 100 } |
| 101 |
| 102 // Test that our extension can send suggestions back to us. |
| 103 { |
| 104 autocomplete_controller->Start(L"keyword suggestio", std::wstring(), |
| 105 true, false, false); |
| 106 |
| 107 WaitForAutocompleteDone(autocomplete_controller); |
| 108 EXPECT_TRUE(autocomplete_controller->done()); |
| 109 |
| 110 // First result should be to invoke the keyword with what we typed, 2-4 |
| 111 // should be to invoke with suggestions from the extension, and the last |
| 112 // should be to search for what we typed. |
| 113 const AutocompleteResult& result = autocomplete_controller->result(); |
| 114 ASSERT_EQ(5U, result.size()) << AutocompleteResultAsString(result); |
| 115 |
| 116 ASSERT_TRUE(result.match_at(0).template_url); |
| 117 EXPECT_EQ(L"keyword suggestio", result.match_at(0).fill_into_edit); |
| 118 EXPECT_EQ(L"keyword suggestion1", result.match_at(1).fill_into_edit); |
| 119 EXPECT_EQ(L"keyword suggestion2", result.match_at(2).fill_into_edit); |
| 120 EXPECT_EQ(L"keyword suggestion3", result.match_at(3).fill_into_edit); |
| 121 |
| 122 AutocompleteMatch match = result.match_at(4); |
| 123 EXPECT_EQ(AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, match.type); |
| 124 EXPECT_FALSE(match.deletable); |
| 125 } |
| 126 |
| 127 { |
| 128 ResultCatcher catcher; |
| 129 autocomplete_controller->Start(L"keyword command", std::wstring(), |
| 130 true, false, false); |
| 131 location_bar->AcceptInput(); |
| 132 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 133 } |
| 134 } |
OLD | NEW |