OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chrome/browser/autocomplete/search_provider.h" | 5 #include "chrome/browser/autocomplete/search_provider.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
19 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" | 19 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
20 #include "chrome/browser/autocomplete/autocomplete_controller.h" | 20 #include "chrome/browser/autocomplete/autocomplete_controller.h" |
21 #include "chrome/browser/autocomplete/autocomplete_input.h" | 21 #include "chrome/browser/autocomplete/autocomplete_input.h" |
22 #include "chrome/browser/autocomplete/autocomplete_match.h" | 22 #include "chrome/browser/autocomplete/autocomplete_match.h" |
23 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 23 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
24 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 24 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
25 #include "chrome/browser/autocomplete/history_url_provider.h" | 25 #include "chrome/browser/autocomplete/history_url_provider.h" |
26 #include "chrome/browser/history/history_service.h" | 26 #include "chrome/browser/history/history_service.h" |
27 #include "chrome/browser/history/history_service_factory.h" | 27 #include "chrome/browser/history/history_service_factory.h" |
28 #include "chrome/browser/omnibox/omnibox_field_trial.h" | 28 #include "chrome/browser/omnibox/omnibox_field_trial.h" |
29 #include "chrome/browser/search/search.h" | 29 #include "chrome/browser/search/search.h" |
| 30 #include "chrome/browser/search_engines/search_engine_type.h" |
30 #include "chrome/browser/search_engines/template_url.h" | 31 #include "chrome/browser/search_engines/template_url.h" |
31 #include "chrome/browser/search_engines/template_url_service.h" | 32 #include "chrome/browser/search_engines/template_url_service.h" |
32 #include "chrome/browser/search_engines/template_url_service_factory.h" | 33 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 34 #include "chrome/browser/signin/signin_manager.h" |
| 35 #include "chrome/browser/signin/signin_manager_factory.h" |
| 36 #include "chrome/browser/sync/profile_sync_service.h" |
| 37 #include "chrome/browser/sync/profile_sync_service_factory.h" |
33 #include "chrome/common/chrome_switches.h" | 38 #include "chrome/common/chrome_switches.h" |
34 #include "chrome/common/metrics/variations/variations_util.h" | 39 #include "chrome/common/metrics/variations/variations_util.h" |
35 #include "chrome/common/pref_names.h" | 40 #include "chrome/common/pref_names.h" |
36 #include "chrome/test/base/testing_browser_process.h" | 41 #include "chrome/test/base/testing_browser_process.h" |
37 #include "chrome/test/base/testing_profile.h" | 42 #include "chrome/test/base/testing_profile.h" |
38 #include "components/variations/entropy_provider.h" | 43 #include "components/variations/entropy_provider.h" |
39 #include "content/public/test/test_browser_thread_bundle.h" | 44 #include "content/public/test/test_browser_thread_bundle.h" |
40 #include "net/url_request/test_url_fetcher_factory.h" | 45 #include "net/url_request/test_url_fetcher_factory.h" |
41 #include "net/url_request/url_request_status.h" | 46 #include "net/url_request/url_request_status.h" |
42 #include "testing/gtest/include/gtest/gtest.h" | 47 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 2930 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2973 | 2978 |
2974 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true); | 2979 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true); |
2975 term = term1_.substr(0, term1_.length() - 1); | 2980 term = term1_.substr(0, term1_.length() - 1); |
2976 QueryForInput(term, true, false); | 2981 QueryForInput(term, true, false); |
2977 ASSERT_FALSE(provider_->matches().empty()); | 2982 ASSERT_FALSE(provider_->matches().empty()); |
2978 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 2983 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
2979 provider_->matches()[0].type); | 2984 provider_->matches()[0].type); |
2980 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); | 2985 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); |
2981 EXPECT_TRUE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); | 2986 EXPECT_TRUE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); |
2982 } | 2987 } |
| 2988 |
| 2989 TEST_F(SearchProviderTest, CanSendURL) { |
| 2990 TemplateURLData template_url_data; |
| 2991 template_url_data.short_name = ASCIIToUTF16("t"); |
| 2992 template_url_data.SetURL("http://www.google.com/{searchTerms}"); |
| 2993 template_url_data.suggestions_url = "http://www.google.com/{searchTerms}"; |
| 2994 template_url_data.instant_url = "http://does/not/exist?strk=1"; |
| 2995 template_url_data.search_terms_replacement_key = "strk"; |
| 2996 template_url_data.id = SEARCH_ENGINE_GOOGLE; |
| 2997 TemplateURL google_template_url(&profile_, template_url_data); |
| 2998 |
| 2999 // Create field trial. |
| 3000 base::FieldTrial* field_trial = base::FieldTrialList::CreateFieldTrial( |
| 3001 "AutocompleteDynamicTrial_2", "EnableZeroSuggest"); |
| 3002 field_trial->group(); |
| 3003 |
| 3004 // Not signed in. |
| 3005 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3006 GURL("http://www.google.com/search"), |
| 3007 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3008 AutocompleteInput::OTHER, &profile_)); |
| 3009 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(&profile_); |
| 3010 signin->SetAuthenticatedUsername("test"); |
| 3011 |
| 3012 // All conditions should be met. |
| 3013 EXPECT_TRUE(SearchProvider::CanSendURL( |
| 3014 GURL("http://www.google.com/search"), |
| 3015 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3016 AutocompleteInput::OTHER, &profile_)); |
| 3017 |
| 3018 // Not in field trial. |
| 3019 ResetFieldTrialList(); |
| 3020 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3021 GURL("http://www.google.com/search"), |
| 3022 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3023 AutocompleteInput::OTHER, &profile_)); |
| 3024 field_trial = base::FieldTrialList::CreateFieldTrial( |
| 3025 "AutocompleteDynamicTrial_2", "EnableZeroSuggest"); |
| 3026 field_trial->group(); |
| 3027 |
| 3028 // Invalid page URL. |
| 3029 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3030 GURL("badpageurl"), |
| 3031 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3032 AutocompleteInput::OTHER, &profile_)); |
| 3033 |
| 3034 // Invalid page classification. |
| 3035 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3036 GURL("http://www.google.com/search"), |
| 3037 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3038 AutocompleteInput::INSTANT_NEW_TAB_PAGE_WITH_FAKEBOX_AS_STARTING_FOCUS, |
| 3039 &profile_)); |
| 3040 |
| 3041 // Invalid page classification. |
| 3042 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3043 GURL("http://www.google.com/search"), |
| 3044 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3045 AutocompleteInput::INSTANT_NEW_TAB_PAGE_WITH_OMNIBOX_AS_STARTING_FOCUS, |
| 3046 &profile_)); |
| 3047 |
| 3048 // HTTPS page URL on same domain as provider. |
| 3049 EXPECT_TRUE(SearchProvider::CanSendURL( |
| 3050 GURL("https://www.google.com/search"), |
| 3051 GURL("https://www.google.com/complete/search"), |
| 3052 &google_template_url, AutocompleteInput::OTHER, &profile_)); |
| 3053 |
| 3054 // Non-HTTP[S] page URL on same domain as provider. |
| 3055 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3056 GURL("ftp://www.google.com/search"), |
| 3057 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3058 AutocompleteInput::OTHER, &profile_)); |
| 3059 |
| 3060 // Non-HTTP page URL on different domain. |
| 3061 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3062 GURL("https://www.notgoogle.com/search"), |
| 3063 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3064 AutocompleteInput::OTHER, &profile_)); |
| 3065 |
| 3066 // Non-HTTPS provider. |
| 3067 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3068 GURL("http://www.google.com/search"), |
| 3069 GURL("http://www.google.com/complete/search"), &google_template_url, |
| 3070 AutocompleteInput::OTHER, &profile_)); |
| 3071 |
| 3072 // Suggest disabled. |
| 3073 profile_.GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false); |
| 3074 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3075 GURL("http://www.google.com/search"), |
| 3076 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3077 AutocompleteInput::OTHER, &profile_)); |
| 3078 profile_.GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); |
| 3079 |
| 3080 // Incognito. |
| 3081 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3082 GURL("http://www.google.com/search"), |
| 3083 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3084 AutocompleteInput::OTHER, profile_.GetOffTheRecordProfile())); |
| 3085 |
| 3086 // Tab sync not enabled. |
| 3087 profile_.GetPrefs()->SetBoolean(prefs::kSyncKeepEverythingSynced, false); |
| 3088 profile_.GetPrefs()->SetBoolean(prefs::kSyncTabs, false); |
| 3089 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3090 GURL("http://www.google.com/search"), |
| 3091 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3092 AutocompleteInput::OTHER, &profile_)); |
| 3093 profile_.GetPrefs()->SetBoolean(prefs::kSyncTabs, true); |
| 3094 |
| 3095 // Tab sync is encrypted. |
| 3096 ProfileSyncService* service = |
| 3097 ProfileSyncServiceFactory::GetInstance()->GetForProfile(&profile_); |
| 3098 syncer::ModelTypeSet encrypted_types = service->GetEncryptedDataTypes(); |
| 3099 encrypted_types.Put(syncer::SESSIONS); |
| 3100 service->OnEncryptedTypesChanged(encrypted_types, false); |
| 3101 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3102 GURL("http://www.google.com/search"), |
| 3103 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3104 AutocompleteInput::OTHER, &profile_)); |
| 3105 encrypted_types.Remove(syncer::SESSIONS); |
| 3106 service->OnEncryptedTypesChanged(encrypted_types, false); |
| 3107 |
| 3108 // Check that there were no side effects from previous tests. |
| 3109 EXPECT_TRUE(SearchProvider::CanSendURL( |
| 3110 GURL("http://www.google.com/search"), |
| 3111 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3112 AutocompleteInput::OTHER, &profile_)); |
| 3113 } |
OLD | NEW |