Home | History | Annotate | Download | only in autocomplete
      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
      3 // found in the LICENSE file.
      4 
      5 #include "base/command_line.h"
      6 #include "base/format_macros.h"
      7 #include "base/path_service.h"
      8 #include "base/strings/stringprintf.h"
      9 #include "base/strings/utf_string_conversions.h"
     10 #include "chrome/browser/autocomplete/autocomplete_input.h"
     11 #include "chrome/browser/autocomplete/autocomplete_match.h"
     12 #include "chrome/browser/autocomplete/autocomplete_provider.h"
     13 #include "chrome/browser/extensions/extension_browsertest.h"
     14 #include "chrome/browser/extensions/extension_service.h"
     15 #include "chrome/browser/extensions/unpacked_installer.h"
     16 #include "chrome/browser/history/history_service.h"
     17 #include "chrome/browser/history/history_service_factory.h"
     18 #include "chrome/browser/profiles/profile.h"
     19 #include "chrome/browser/search_engines/template_url_service_factory.h"
     20 #include "chrome/browser/ui/browser.h"
     21 #include "chrome/browser/ui/browser_commands.h"
     22 #include "chrome/browser/ui/browser_tabstrip.h"
     23 #include "chrome/browser/ui/browser_window.h"
     24 #include "chrome/browser/ui/omnibox/location_bar.h"
     25 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
     26 #include "chrome/browser/ui/omnibox/omnibox_view.h"
     27 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     28 #include "chrome/common/chrome_paths.h"
     29 #include "chrome/common/url_constants.h"
     30 #include "chrome/test/base/in_process_browser_test.h"
     31 #include "chrome/test/base/test_switches.h"
     32 #include "chrome/test/base/ui_test_utils.h"
     33 #include "content/public/browser/notification_service.h"
     34 #include "content/public/browser/notification_types.h"
     35 #include "testing/gtest/include/gtest/gtest.h"
     36 
     37 namespace {
     38 
     39 string16 AutocompleteResultAsString(const AutocompleteResult& result) {
     40   std::string output(base::StringPrintf("{%" PRIuS "} ", result.size()));
     41   for (size_t i = 0; i < result.size(); ++i) {
     42     AutocompleteMatch match = result.match_at(i);
     43     output.append(base::StringPrintf("[\"%s\" by \"%s\"] ",
     44                                      UTF16ToUTF8(match.contents).c_str(),
     45                                      match.provider->GetName()));
     46   }
     47   return UTF8ToUTF16(output);
     48 }
     49 
     50 }  // namespace
     51 
     52 class AutocompleteBrowserTest : public ExtensionBrowserTest {
     53  protected:
     54   void WaitForTemplateURLServiceToLoad() {
     55     ui_test_utils::WaitForTemplateURLServiceToLoad(
     56       TemplateURLServiceFactory::GetForProfile(browser()->profile()));
     57   }
     58 
     59   LocationBar* GetLocationBar() const {
     60     return browser()->window()->GetLocationBar();
     61   }
     62 
     63   AutocompleteController* GetAutocompleteController() const {
     64     return GetLocationBar()->GetLocationEntry()->model()->popup_model()->
     65         autocomplete_controller();
     66   }
     67 };
     68 
     69 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, Basic) {
     70 #if defined(OS_WIN) && defined(USE_ASH)
     71   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
     72   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
     73     return;
     74 #endif
     75 
     76   WaitForTemplateURLServiceToLoad();
     77   LocationBar* location_bar = GetLocationBar();
     78   OmniboxView* location_entry = location_bar->GetLocationEntry();
     79 
     80   EXPECT_TRUE(location_bar->GetInputString().empty());
     81   EXPECT_EQ(UTF8ToUTF16(content::kAboutBlankURL), location_entry->GetText());
     82   // TODO(phajdan.jr): check state of IsSelectAll when it's consistent across
     83   // platforms.
     84 
     85   location_bar->FocusLocation(true);
     86 
     87   EXPECT_TRUE(location_bar->GetInputString().empty());
     88   EXPECT_EQ(UTF8ToUTF16(content::kAboutBlankURL), location_entry->GetText());
     89   EXPECT_TRUE(location_entry->IsSelectAll());
     90 
     91   location_entry->SetUserText(ASCIIToUTF16("chrome"));
     92 
     93   EXPECT_TRUE(location_bar->GetInputString().empty());
     94   EXPECT_EQ(ASCIIToUTF16("chrome"), location_entry->GetText());
     95   EXPECT_FALSE(location_entry->IsSelectAll());
     96 
     97   location_entry->RevertAll();
     98 
     99   EXPECT_TRUE(location_bar->GetInputString().empty());
    100   EXPECT_EQ(UTF8ToUTF16(content::kAboutBlankURL), location_entry->GetText());
    101   EXPECT_FALSE(location_entry->IsSelectAll());
    102 
    103   location_entry->SetUserText(ASCIIToUTF16("chrome"));
    104   location_bar->Revert();
    105 
    106   EXPECT_TRUE(location_bar->GetInputString().empty());
    107   EXPECT_EQ(UTF8ToUTF16(content::kAboutBlankURL), location_entry->GetText());
    108   EXPECT_FALSE(location_entry->IsSelectAll());
    109 }
    110 
    111 // Autocomplete test is flaky on ChromeOS.
    112 // http://crbug.com/52928
    113 #if defined(OS_CHROMEOS)
    114 #define MAYBE_Autocomplete DISABLED_Autocomplete
    115 #else
    116 #define MAYBE_Autocomplete Autocomplete
    117 #endif
    118 
    119 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, MAYBE_Autocomplete) {
    120 #if defined(OS_WIN) && defined(USE_ASH)
    121   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
    122   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
    123     return;
    124 #endif
    125 
    126   WaitForTemplateURLServiceToLoad();
    127   // The results depend on the history backend being loaded. Make sure it is
    128   // loaded so that the autocomplete results are consistent.
    129   ui_test_utils::WaitForHistoryToLoad(
    130       HistoryServiceFactory::GetForProfile(browser()->profile(),
    131                                            Profile::EXPLICIT_ACCESS));
    132 
    133   LocationBar* location_bar = GetLocationBar();
    134   AutocompleteController* autocomplete_controller = GetAutocompleteController();
    135 
    136   {
    137     OmniboxView* location_entry = location_bar->GetLocationEntry();
    138     location_entry->model()->SetInputInProgress(true);
    139     autocomplete_controller->Start(AutocompleteInput(
    140         ASCIIToUTF16("chrome"), string16::npos, string16(), GURL(),
    141         AutocompleteInput::NEW_TAB_PAGE, true, false, true,
    142         AutocompleteInput::SYNCHRONOUS_MATCHES));
    143 
    144     EXPECT_TRUE(autocomplete_controller->done());
    145     EXPECT_TRUE(location_bar->GetInputString().empty());
    146     EXPECT_TRUE(location_entry->GetText().empty());
    147     EXPECT_TRUE(location_entry->IsSelectAll());
    148     const AutocompleteResult& result = autocomplete_controller->result();
    149     // We get two matches because we have a provider for extension apps and the
    150     // Chrome Web Store is a built-in Extension app. For this test, we only care
    151     // about the other match existing.
    152     ASSERT_GE(result.size(), 1U) << AutocompleteResultAsString(result);
    153     AutocompleteMatch match = result.match_at(0);
    154     EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, match.type);
    155     EXPECT_FALSE(match.deletable);
    156   }
    157 
    158   {
    159     location_bar->Revert();
    160     OmniboxView* location_entry = location_bar->GetLocationEntry();
    161 
    162     EXPECT_TRUE(location_bar->GetInputString().empty());
    163     EXPECT_EQ(UTF8ToUTF16(content::kAboutBlankURL), location_entry->GetText());
    164     EXPECT_FALSE(location_entry->IsSelectAll());
    165     const AutocompleteResult& result = autocomplete_controller->result();
    166     EXPECT_TRUE(result.empty()) << AutocompleteResultAsString(result);
    167   }
    168 }
    169 
    170 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, TabAwayRevertSelect) {
    171 #if defined(OS_WIN) && defined(USE_ASH)
    172   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
    173   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
    174     return;
    175 #endif
    176 
    177   WaitForTemplateURLServiceToLoad();
    178   // http://code.google.com/p/chromium/issues/detail?id=38385
    179   // Make sure that tabbing away from an empty omnibox causes a revert
    180   // and select all.
    181   LocationBar* location_bar = GetLocationBar();
    182   OmniboxView* location_entry = location_bar->GetLocationEntry();
    183   EXPECT_EQ(UTF8ToUTF16(content::kAboutBlankURL), location_entry->GetText());
    184   location_entry->SetUserText(string16());
    185   content::WindowedNotificationObserver observer(
    186       content::NOTIFICATION_LOAD_STOP,
    187       content::NotificationService::AllSources());
    188   chrome::AddSelectedTabWithURL(browser(), GURL(content::kAboutBlankURL),
    189                                 content::PAGE_TRANSITION_AUTO_TOPLEVEL);
    190   observer.Wait();
    191   EXPECT_EQ(UTF8ToUTF16(content::kAboutBlankURL), location_entry->GetText());
    192   chrome::CloseTab(browser());
    193   EXPECT_EQ(UTF8ToUTF16(content::kAboutBlankURL), location_entry->GetText());
    194   EXPECT_TRUE(location_entry->IsSelectAll());
    195 }
    196 
    197 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, FocusSearch) {
    198 #if defined(OS_WIN) && defined(USE_ASH)
    199   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
    200   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
    201     return;
    202 #endif
    203 
    204   WaitForTemplateURLServiceToLoad();
    205   LocationBar* location_bar = GetLocationBar();
    206   OmniboxView* location_entry = location_bar->GetLocationEntry();
    207 
    208   // Focus search when omnibox is blank
    209   {
    210     EXPECT_TRUE(location_bar->GetInputString().empty());
    211     EXPECT_EQ(UTF8ToUTF16(content::kAboutBlankURL), location_entry->GetText());
    212 
    213     location_bar->FocusSearch();
    214     EXPECT_TRUE(location_bar->GetInputString().empty());
    215     EXPECT_EQ(ASCIIToUTF16("?"), location_entry->GetText());
    216 
    217     size_t selection_start, selection_end;
    218     location_entry->GetSelectionBounds(&selection_start, &selection_end);
    219     EXPECT_EQ(1U, selection_start);
    220     EXPECT_EQ(1U, selection_end);
    221   }
    222 
    223   // Focus search when omnibox is _not_ alread in forced query mode.
    224   {
    225     location_entry->SetUserText(ASCIIToUTF16("foo"));
    226     EXPECT_TRUE(location_bar->GetInputString().empty());
    227     EXPECT_EQ(ASCIIToUTF16("foo"), location_entry->GetText());
    228 
    229     location_bar->FocusSearch();
    230     EXPECT_TRUE(location_bar->GetInputString().empty());
    231     EXPECT_EQ(ASCIIToUTF16("?"), location_entry->GetText());
    232 
    233     size_t selection_start, selection_end;
    234     location_entry->GetSelectionBounds(&selection_start, &selection_end);
    235     EXPECT_EQ(1U, selection_start);
    236     EXPECT_EQ(1U, selection_end);
    237   }
    238 
    239   // Focus search when omnibox _is_ already in forced query mode, but no query
    240   // has been typed.
    241   {
    242     location_entry->SetUserText(ASCIIToUTF16("?"));
    243     EXPECT_TRUE(location_bar->GetInputString().empty());
    244     EXPECT_EQ(ASCIIToUTF16("?"), location_entry->GetText());
    245 
    246     location_bar->FocusSearch();
    247     EXPECT_TRUE(location_bar->GetInputString().empty());
    248     EXPECT_EQ(ASCIIToUTF16("?"), location_entry->GetText());
    249 
    250     size_t selection_start, selection_end;
    251     location_entry->GetSelectionBounds(&selection_start, &selection_end);
    252     EXPECT_EQ(1U, selection_start);
    253     EXPECT_EQ(1U, selection_end);
    254   }
    255 
    256   // Focus search when omnibox _is_ already in forced query mode, and some query
    257   // has been typed.
    258   {
    259     location_entry->SetUserText(ASCIIToUTF16("?foo"));
    260     EXPECT_TRUE(location_bar->GetInputString().empty());
    261     EXPECT_EQ(ASCIIToUTF16("?foo"), location_entry->GetText());
    262 
    263     location_bar->FocusSearch();
    264     EXPECT_TRUE(location_bar->GetInputString().empty());
    265     EXPECT_EQ(ASCIIToUTF16("?foo"), location_entry->GetText());
    266 
    267     size_t selection_start, selection_end;
    268     location_entry->GetSelectionBounds(&selection_start, &selection_end);
    269     EXPECT_EQ(1U, std::min(selection_start, selection_end));
    270     EXPECT_EQ(4U, std::max(selection_start, selection_end));
    271   }
    272 
    273   // Focus search when omnibox is in forced query mode with leading whitespace.
    274   {
    275     location_entry->SetUserText(ASCIIToUTF16("   ?foo"));
    276     EXPECT_TRUE(location_bar->GetInputString().empty());
    277     EXPECT_EQ(ASCIIToUTF16("   ?foo"), location_entry->GetText());
    278 
    279     location_bar->FocusSearch();
    280     EXPECT_TRUE(location_bar->GetInputString().empty());
    281     EXPECT_EQ(ASCIIToUTF16("   ?foo"), location_entry->GetText());
    282 
    283     size_t selection_start, selection_end;
    284     location_entry->GetSelectionBounds(&selection_start, &selection_end);
    285     EXPECT_EQ(4U, std::min(selection_start, selection_end));
    286     EXPECT_EQ(7U, std::max(selection_start, selection_end));
    287   }
    288 }
    289