OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/ntp_snippets/ntp_snippets_fetcher.h" | 5 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "base/test/histogram_tester.h" | 13 #include "base/test/histogram_tester.h" |
14 #include "base/test/test_mock_time_task_runner.h" | 14 #include "base/test/test_mock_time_task_runner.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "components/ntp_snippets/category_factory.h" | 18 #include "components/ntp_snippets/category_factory.h" |
19 #include "components/ntp_snippets/ntp_snippet.h" | 19 #include "components/ntp_snippets/ntp_snippet.h" |
20 #include "components/ntp_snippets/ntp_snippets_constants.h" | 20 #include "components/ntp_snippets/ntp_snippets_constants.h" |
21 #include "components/prefs/testing_pref_service.h" | 21 #include "components/prefs/testing_pref_service.h" |
22 #include "components/signin/core/browser/account_tracker_service.h" | 22 #include "components/signin/core/browser/account_tracker_service.h" |
23 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 23 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
24 #include "components/signin/core/browser/fake_signin_manager.h" | 24 #include "components/signin/core/browser/fake_signin_manager.h" |
25 #include "components/signin/core/browser/test_signin_client.h" | 25 #include "components/signin/core/browser/test_signin_client.h" |
26 #include "components/variations/entropy_provider.h" | 26 #include "components/variations/entropy_provider.h" |
27 #include "components/variations/variations_associated_data.h" | 27 #include "components/variations/variations_associated_data.h" |
28 #include "google_apis/google_api_keys.h" | |
29 #include "net/url_request/test_url_fetcher_factory.h" | 28 #include "net/url_request/test_url_fetcher_factory.h" |
30 #include "net/url_request/url_request_test_util.h" | 29 #include "net/url_request/url_request_test_util.h" |
31 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
33 | 32 |
34 namespace ntp_snippets { | 33 namespace ntp_snippets { |
35 | 34 |
36 namespace { | 35 namespace { |
37 | 36 |
38 using testing::_; | 37 using testing::_; |
39 using testing::ElementsAre; | 38 using testing::ElementsAre; |
40 using testing::Eq; | 39 using testing::Eq; |
41 using testing::IsEmpty; | 40 using testing::IsEmpty; |
42 using testing::IsNull; | 41 using testing::IsNull; |
43 using testing::Not; | 42 using testing::Not; |
44 using testing::NotNull; | 43 using testing::NotNull; |
45 using testing::PrintToString; | 44 using testing::PrintToString; |
46 using testing::SizeIs; | 45 using testing::SizeIs; |
47 using testing::StartsWith; | 46 using testing::StartsWith; |
48 using testing::WithArg; | 47 using testing::WithArg; |
49 | 48 |
50 const char kTestChromeReaderUrlFormat[] = | 49 const char kAPIKey[] = "fakeAPIkey"; |
51 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s"; | 50 const char kTestChromeReaderUrl[] = |
52 const char kTestChromeContentSuggestionsUrlFormat[] = | 51 "https://chromereader-pa.googleapis.com/v1/fetch?key=fakeAPIkey"; |
| 52 const char kTestChromeContentSuggestionsUrl[] = |
53 "https://chromecontentsuggestions-pa.googleapis.com/v1/suggestions/" | 53 "https://chromecontentsuggestions-pa.googleapis.com/v1/suggestions/" |
54 "fetch?key=%s"; | 54 "fetch?key=fakeAPIkey"; |
55 | 55 |
56 // Artificial time delay for JSON parsing. | 56 // Artificial time delay for JSON parsing. |
57 const int64_t kTestJsonParsingLatencyMs = 20; | 57 const int64_t kTestJsonParsingLatencyMs = 20; |
58 | 58 |
59 ACTION_P(MovePointeeTo, ptr) { | 59 ACTION_P(MovePointeeTo, ptr) { |
60 *ptr = std::move(*arg0); | 60 *ptr = std::move(*arg0); |
61 } | 61 } |
62 | 62 |
63 MATCHER(HasValue, "") { | 63 MATCHER(HasValue, "") { |
64 return static_cast<bool>(*arg); | 64 return static_cast<bool>(*arg); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 | 131 |
132 void ParseJsonDelayed( | 132 void ParseJsonDelayed( |
133 const std::string& json, | 133 const std::string& json, |
134 const ntp_snippets::NTPSnippetsFetcher::SuccessCallback& success_callback, | 134 const ntp_snippets::NTPSnippetsFetcher::SuccessCallback& success_callback, |
135 const ntp_snippets::NTPSnippetsFetcher::ErrorCallback& error_callback) { | 135 const ntp_snippets::NTPSnippetsFetcher::ErrorCallback& error_callback) { |
136 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 136 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
137 FROM_HERE, base::Bind(&ParseJson, json, success_callback, error_callback), | 137 FROM_HERE, base::Bind(&ParseJson, json, success_callback, error_callback), |
138 base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs)); | 138 base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs)); |
139 } | 139 } |
140 | 140 |
141 GURL GetFetcherUrl(const char* url_format) { | |
142 return GURL(base::StringPrintf(url_format, google_apis::GetAPIKey().c_str())); | |
143 } | |
144 | |
145 } // namespace | 141 } // namespace |
146 | 142 |
147 class NTPSnippetsFetcherTest : public testing::Test { | 143 class NTPSnippetsFetcherTest : public testing::Test { |
148 public: | 144 public: |
149 NTPSnippetsFetcherTest() | 145 NTPSnippetsFetcherTest() |
150 : NTPSnippetsFetcherTest(GetFetcherUrl(kTestChromeReaderUrlFormat), | 146 : NTPSnippetsFetcherTest(GURL(kTestChromeReaderUrl), |
151 std::map<std::string, std::string>()) {} | 147 std::map<std::string, std::string>()) {} |
152 | 148 |
153 NTPSnippetsFetcherTest(const GURL& gurl, | 149 NTPSnippetsFetcherTest(const GURL& gurl, |
154 const std::map<std::string, std::string>& params) | 150 const std::map<std::string, std::string>& params) |
155 : params_manager_(ntp_snippets::kStudyName, params), | 151 : params_manager_(ntp_snippets::kStudyName, params), |
156 mock_task_runner_(new base::TestMockTimeTaskRunner()), | 152 mock_task_runner_(new base::TestMockTimeTaskRunner()), |
157 mock_task_runner_handle_(mock_task_runner_), | 153 mock_task_runner_handle_(mock_task_runner_), |
158 signin_client_(new TestSigninClient(nullptr)), | 154 signin_client_(new TestSigninClient(nullptr)), |
159 account_tracker_(new AccountTrackerService()), | 155 account_tracker_(new AccountTrackerService()), |
160 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(), | 156 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(), |
161 account_tracker_.get())), | 157 account_tracker_.get())), |
162 fake_token_service_(new FakeProfileOAuth2TokenService()), | 158 fake_token_service_(new FakeProfileOAuth2TokenService()), |
163 pref_service_(new TestingPrefServiceSimple()), | 159 pref_service_(new TestingPrefServiceSimple()), |
164 test_lang_("en-US"), | 160 test_lang_("en-US"), |
165 test_url_(gurl) { | 161 test_url_(gurl) { |
166 RequestThrottler::RegisterProfilePrefs(pref_service_->registry()); | 162 RequestThrottler::RegisterProfilePrefs(pref_service_->registry()); |
167 | 163 |
168 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( | 164 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( |
169 fake_signin_manager_.get(), fake_token_service_.get(), | 165 fake_signin_manager_.get(), fake_token_service_.get(), |
170 scoped_refptr<net::TestURLRequestContextGetter>( | 166 scoped_refptr<net::TestURLRequestContextGetter>( |
171 new net::TestURLRequestContextGetter(mock_task_runner_.get())), | 167 new net::TestURLRequestContextGetter(mock_task_runner_.get())), |
172 pref_service_.get(), &category_factory_, base::Bind(&ParseJsonDelayed), | 168 pref_service_.get(), &category_factory_, base::Bind(&ParseJsonDelayed), |
173 /*is_stable_channel=*/true); | 169 kAPIKey); |
174 | 170 |
175 snippets_fetcher_->SetCallback( | 171 snippets_fetcher_->SetCallback( |
176 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, | 172 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, |
177 base::Unretained(&mock_callback_))); | 173 base::Unretained(&mock_callback_))); |
178 snippets_fetcher_->SetTickClockForTesting( | 174 snippets_fetcher_->SetTickClockForTesting( |
179 mock_task_runner_->GetMockTickClock()); | 175 mock_task_runner_->GetMockTickClock()); |
180 test_hosts_.insert("www.somehost.com"); | 176 test_hosts_.insert("www.somehost.com"); |
181 test_excluded_.insert("1234567890"); | 177 test_excluded_.insert("1234567890"); |
182 // Increase initial time such that ticks are non-zero. | 178 // Increase initial time such that ticks are non-zero. |
183 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); | 179 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 std::set<std::string> test_excluded_; | 228 std::set<std::string> test_excluded_; |
233 base::HistogramTester histogram_tester_; | 229 base::HistogramTester histogram_tester_; |
234 | 230 |
235 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); | 231 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); |
236 }; | 232 }; |
237 | 233 |
238 class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest { | 234 class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest { |
239 public: | 235 public: |
240 NTPSnippetsContentSuggestionsFetcherTest() | 236 NTPSnippetsContentSuggestionsFetcherTest() |
241 : NTPSnippetsFetcherTest( | 237 : NTPSnippetsFetcherTest( |
242 GetFetcherUrl(kTestChromeContentSuggestionsUrlFormat), | 238 GURL(kTestChromeContentSuggestionsUrl), |
243 {{"content_suggestions_backend", kContentSuggestionsServer}}) {} | 239 {{"content_suggestions_backend", kContentSuggestionsServer}}) {} |
244 }; | 240 }; |
245 | 241 |
246 class NTPSnippetsFetcherHostRestrictedTest : public NTPSnippetsFetcherTest { | 242 class NTPSnippetsFetcherHostRestrictedTest : public NTPSnippetsFetcherTest { |
247 public: | 243 public: |
248 NTPSnippetsFetcherHostRestrictedTest() | 244 NTPSnippetsFetcherHostRestrictedTest() |
249 : NTPSnippetsFetcherTest(GetFetcherUrl(kTestChromeReaderUrlFormat), | 245 : NTPSnippetsFetcherTest(GURL(kTestChromeReaderUrl), |
250 {{"fetching_host_restrict", "on"}}) {} | 246 {{"fetching_host_restrict", "on"}}) {} |
251 }; | 247 }; |
252 | 248 |
253 TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { | 249 TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { |
254 NTPSnippetsFetcher::RequestParams params; | 250 NTPSnippetsFetcher::RequestParams params; |
255 params.obfuscated_gaia_id = "0BFUSGAIA"; | 251 params.obfuscated_gaia_id = "0BFUSGAIA"; |
256 params.only_return_personalized_results = true; | 252 params.only_return_personalized_results = true; |
257 params.user_locale = "en"; | 253 params.user_locale = "en"; |
258 params.host_restricts = {"chromium.org"}; | 254 params.host_restricts = {"chromium.org"}; |
259 params.excluded_ids = {"1234567890"}; | 255 params.excluded_ids = {"1234567890"}; |
(...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
805 if (snippets) { | 801 if (snippets) { |
806 // Matchers above aren't any more precise than this, so this is sufficient | 802 // Matchers above aren't any more precise than this, so this is sufficient |
807 // for test-failure diagnostics. | 803 // for test-failure diagnostics. |
808 return os << "list with " << snippets->size() << " elements"; | 804 return os << "list with " << snippets->size() << " elements"; |
809 } else { | 805 } else { |
810 return os << "null"; | 806 return os << "null"; |
811 } | 807 } |
812 } | 808 } |
813 | 809 |
814 } // namespace ntp_snippets | 810 } // namespace ntp_snippets |
OLD | NEW |