| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/zero_suggest_provider.h" | 5 #include "chrome/browser/autocomplete/zero_suggest_provider.h" |
| 6 | 6 |
| 7 #include "base/metrics/field_trial.h" | 7 #include "base/metrics/field_trial.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" | 11 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
| 12 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 12 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
| 13 #include "chrome/browser/search_engines/template_url.h" | 13 #include "chrome/browser/search_engines/template_url.h" |
| 14 #include "chrome/browser/search_engines/template_url_service.h" | 14 #include "chrome/browser/search_engines/template_url_service.h" |
| 15 #include "chrome/browser/search_engines/template_url_service_factory.h" | 15 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 18 #include "components/metrics/proto/omnibox_event.pb.h" |
| 18 #include "components/variations/entropy_provider.h" | 19 #include "components/variations/entropy_provider.h" |
| 19 #include "components/variations/variations_associated_data.h" | 20 #include "components/variations/variations_associated_data.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" | 21 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "net/url_request/test_url_fetcher_factory.h" | 22 #include "net/url_request/test_url_fetcher_factory.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 24 |
| 24 class ZeroSuggestProviderTest : public testing::Test, | 25 class ZeroSuggestProviderTest : public testing::Test, |
| 25 public AutocompleteProviderListener { | 26 public AutocompleteProviderListener { |
| 26 public: | 27 public: |
| 27 ZeroSuggestProviderTest(); | 28 ZeroSuggestProviderTest(); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 base::FieldTrialList::CreateFieldTrial( | 106 base::FieldTrialList::CreateFieldTrial( |
| 106 "AutocompleteDynamicTrial_2", "EnableZeroSuggestPersonalizedExperiment"); | 107 "AutocompleteDynamicTrial_2", "EnableZeroSuggestPersonalizedExperiment"); |
| 107 | 108 |
| 108 // Ensure the cache is empty. | 109 // Ensure the cache is empty. |
| 109 PrefService* prefs = profile_.GetPrefs(); | 110 PrefService* prefs = profile_.GetPrefs(); |
| 110 prefs->SetString(prefs::kZeroSuggestCachedResults, std::string()); | 111 prefs->SetString(prefs::kZeroSuggestCachedResults, std::string()); |
| 111 | 112 |
| 112 std::string url("http://www.cnn.com"); | 113 std::string url("http://www.cnn.com"); |
| 113 AutocompleteInput input( | 114 AutocompleteInput input( |
| 114 base::ASCIIToUTF16(url), base::string16::npos, base::string16(), | 115 base::ASCIIToUTF16(url), base::string16::npos, base::string16(), |
| 115 GURL(url), AutocompleteInput::INVALID_SPEC, true, false, true, true); | 116 GURL(url), metrics::OmniboxEventProto::INVALID_SPEC, true, false, true, |
| 117 true); |
| 116 | 118 |
| 117 provider_->Start(input, false); | 119 provider_->Start(input, false); |
| 118 | 120 |
| 119 EXPECT_TRUE(prefs->GetString(prefs::kZeroSuggestCachedResults).empty()); | 121 EXPECT_TRUE(prefs->GetString(prefs::kZeroSuggestCachedResults).empty()); |
| 120 EXPECT_TRUE(provider_->matches().empty()); | 122 EXPECT_TRUE(provider_->matches().empty()); |
| 121 | 123 |
| 122 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID(1); | 124 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID(1); |
| 123 ASSERT_TRUE(fetcher); | 125 ASSERT_TRUE(fetcher); |
| 124 fetcher->set_response_code(200); | 126 fetcher->set_response_code(200); |
| 125 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," | 127 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," |
| 126 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," | 128 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," |
| 127 "\"google:verbatimrelevance\":1300}]"); | 129 "\"google:verbatimrelevance\":1300}]"); |
| 128 fetcher->SetResponseString(json_response); | 130 fetcher->SetResponseString(json_response); |
| 129 fetcher->delegate()->OnURLFetchComplete(fetcher); | 131 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 130 | 132 |
| 131 base::RunLoop().RunUntilIdle(); | 133 base::RunLoop().RunUntilIdle(); |
| 132 | 134 |
| 133 EXPECT_EQ(4U, provider_->matches().size()); // 3 results + verbatim | 135 EXPECT_EQ(4U, provider_->matches().size()); // 3 results + verbatim |
| 134 EXPECT_EQ(json_response, prefs->GetString(prefs::kZeroSuggestCachedResults)); | 136 EXPECT_EQ(json_response, prefs->GetString(prefs::kZeroSuggestCachedResults)); |
| 135 } | 137 } |
| 136 | 138 |
| 137 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestHasCachedResults) { | 139 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestHasCachedResults) { |
| 138 base::FieldTrialList::CreateFieldTrial( | 140 base::FieldTrialList::CreateFieldTrial( |
| 139 "AutocompleteDynamicTrial_2", "EnableZeroSuggestPersonalizedExperiment"); | 141 "AutocompleteDynamicTrial_2", "EnableZeroSuggestPersonalizedExperiment"); |
| 140 | 142 |
| 141 std::string url("http://www.cnn.com"); | 143 std::string url("http://www.cnn.com"); |
| 142 AutocompleteInput input( | 144 AutocompleteInput input( |
| 143 base::ASCIIToUTF16(url), base::string16::npos, base::string16(), | 145 base::ASCIIToUTF16(url), base::string16::npos, base::string16(), |
| 144 GURL(url), AutocompleteInput::INVALID_SPEC, true, false, true, true); | 146 GURL(url), metrics::OmniboxEventProto::INVALID_SPEC, true, false, true, |
| 147 true); |
| 145 | 148 |
| 146 // Set up the pref to cache the response from the previous run. | 149 // Set up the pref to cache the response from the previous run. |
| 147 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," | 150 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," |
| 148 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," | 151 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," |
| 149 "\"google:verbatimrelevance\":1300}]"); | 152 "\"google:verbatimrelevance\":1300}]"); |
| 150 PrefService* prefs = profile_.GetPrefs(); | 153 PrefService* prefs = profile_.GetPrefs(); |
| 151 prefs->SetString(prefs::kZeroSuggestCachedResults, json_response); | 154 prefs->SetString(prefs::kZeroSuggestCachedResults, json_response); |
| 152 | 155 |
| 153 provider_->Start(input, false); | 156 provider_->Start(input, false); |
| 154 | 157 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 180 prefs->GetString(prefs::kZeroSuggestCachedResults)); | 183 prefs->GetString(prefs::kZeroSuggestCachedResults)); |
| 181 } | 184 } |
| 182 | 185 |
| 183 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestReceivedEmptyResults) { | 186 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestReceivedEmptyResults) { |
| 184 base::FieldTrialList::CreateFieldTrial( | 187 base::FieldTrialList::CreateFieldTrial( |
| 185 "AutocompleteDynamicTrial_2", "EnableZeroSuggestPersonalizedExperiment"); | 188 "AutocompleteDynamicTrial_2", "EnableZeroSuggestPersonalizedExperiment"); |
| 186 | 189 |
| 187 std::string url("http://www.cnn.com"); | 190 std::string url("http://www.cnn.com"); |
| 188 AutocompleteInput input( | 191 AutocompleteInput input( |
| 189 base::ASCIIToUTF16(url), base::string16::npos, base::string16(), | 192 base::ASCIIToUTF16(url), base::string16::npos, base::string16(), |
| 190 GURL(url), AutocompleteInput::INVALID_SPEC, true, false, true, true); | 193 GURL(url), metrics::OmniboxEventProto::INVALID_SPEC, true, false, true, |
| 194 true); |
| 191 | 195 |
| 192 // Set up the pref to cache the response from the previous run. | 196 // Set up the pref to cache the response from the previous run. |
| 193 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," | 197 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," |
| 194 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," | 198 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," |
| 195 "\"google:verbatimrelevance\":1300}]"); | 199 "\"google:verbatimrelevance\":1300}]"); |
| 196 PrefService* prefs = profile_.GetPrefs(); | 200 PrefService* prefs = profile_.GetPrefs(); |
| 197 prefs->SetString(prefs::kZeroSuggestCachedResults, json_response); | 201 prefs->SetString(prefs::kZeroSuggestCachedResults, json_response); |
| 198 | 202 |
| 199 provider_->Start(input, false); | 203 provider_->Start(input, false); |
| 200 | 204 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 213 | 217 |
| 214 base::RunLoop().RunUntilIdle(); | 218 base::RunLoop().RunUntilIdle(); |
| 215 | 219 |
| 216 // Expect that the matches have been cleared. | 220 // Expect that the matches have been cleared. |
| 217 ASSERT_TRUE(provider_->matches().empty()); | 221 ASSERT_TRUE(provider_->matches().empty()); |
| 218 | 222 |
| 219 // Expect the new results have been stored. | 223 // Expect the new results have been stored. |
| 220 EXPECT_EQ(empty_response, | 224 EXPECT_EQ(empty_response, |
| 221 prefs->GetString(prefs::kZeroSuggestCachedResults)); | 225 prefs->GetString(prefs::kZeroSuggestCachedResults)); |
| 222 } | 226 } |
| OLD | NEW |