1 // Copyright (c) 2011 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/basictypes.h" 6 #include "base/message_loop.h" 7 #include "base/utf_string_conversions.h" 8 #include "chrome/test/testing_browser_process_test.h" 9 #include "chrome/browser/autocomplete/autocomplete_match.h" 10 #include "chrome/browser/autocomplete/extension_app_provider.h" 11 #include "chrome/browser/history/history.h" 12 #include "chrome/browser/history/url_database.h" 13 #include "chrome/test/testing_profile.h" 14 15 class ExtensionAppProviderTest : public TestingBrowserProcessTest { 16 protected: 17 struct test_data { 18 const string16 input; 19 const size_t num_results; 20 const GURL output[3]; 21 }; 22 23 ExtensionAppProviderTest() : history_service_(NULL) { } 24 virtual ~ExtensionAppProviderTest() { } 25 26 virtual void SetUp() OVERRIDE; 27 28 void RunTest(test_data* keyword_cases, 29 int num_cases); 30 31 protected: 32 MessageLoopForUI message_loop_; 33 scoped_refptr<ExtensionAppProvider> app_provider_; 34 scoped_ptr<TestingProfile> profile_; 35 HistoryService* history_service_; 36 }; 37 38 void ExtensionAppProviderTest::SetUp() { 39 profile_.reset(new TestingProfile()); 40 profile_->CreateHistoryService(true, false); 41 profile_->BlockUntilHistoryProcessesPendingRequests(); 42 history_service_ = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); 43 44 app_provider_ = new ExtensionAppProvider(NULL, profile_.get()); 45 46 struct ExtensionApps { 47 std::string app_name; 48 std::string url; 49 std::string title; 50 int typed_count; 51 } kExtensionApps[] = { 52 {"COYB", "http://asdf/", "COYB", 7}, 53 {"NSNO", "http://fdsa/", "NSNO", 2}, 54 }; 55 56 history::URLDatabase* url_db = history_service_->InMemoryDatabase(); 57 58 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExtensionApps); ++i) { 59 // Populate the Extension Apps list. 60 app_provider_->AddExtensionAppForTesting(kExtensionApps[i].app_name, 61 kExtensionApps[i].url); 62 63 // Populate the InMemoryDatabase. 64 history::URLRow info(GURL(kExtensionApps[i].url)); 65 info.set_title(UTF8ToUTF16(kExtensionApps[i].title)); 66 info.set_typed_count(kExtensionApps[i].typed_count); 67 url_db->AddURL(info); 68 } 69 } 70 71 void ExtensionAppProviderTest::RunTest( 72 test_data* keyword_cases, 73 int num_cases) { 74 ACMatches matches; 75 for (int i = 0; i < num_cases; ++i) { 76 AutocompleteInput input(keyword_cases[i].input, string16(), true, 77 false, true, AutocompleteInput::ALL_MATCHES); 78 app_provider_->Start(input, false); 79 EXPECT_TRUE(app_provider_->done()); 80 matches = app_provider_->matches(); 81 EXPECT_EQ(keyword_cases[i].num_results, matches.size()) 82 << ASCIIToUTF16("Input was: ") + keyword_cases[i].input; 83 if (matches.size() == keyword_cases[i].num_results) { 84 for (size_t j = 0; j < keyword_cases[i].num_results; ++j) 85 EXPECT_EQ(keyword_cases[i].output[j], matches[j].destination_url); 86 } 87 } 88 } 89 90 TEST_F(ExtensionAppProviderTest, BasicMatching) { 91 test_data edit_cases[] = { 92 // Searching for a nonexistent value should give nothing. 93 {ASCIIToUTF16("Not Found"), 0, { GURL() }}, 94 95 // The letter 'o' appears in both extension apps. 96 {ASCIIToUTF16("o"), 2, { GURL("http://asdf/"), 97 GURL("http://fdsa/") }}, 98 // The string 'co' appears in one extension app. 99 {ASCIIToUTF16("co"), 1, { GURL("http://asdf/") }}, 100 // Try with URL matching. 101 {ASCIIToUTF16("http://asdf/"), 1, { GURL("http://asdf/") }}, 102 {ASCIIToUTF16("http://fdsa/"), 1, { GURL("http://fdsa/") }}, 103 }; 104 105 RunTest(edit_cases, ARRAYSIZE_UNSAFE(edit_cases)); 106 } 107