OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
13 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
20 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
21 #include "base/test/histogram_tester.h" | 21 #include "base/test/histogram_tester.h" |
22 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
23 #include "base/time/time.h" | 23 #include "base/time/time.h" |
24 #include "components/image_fetcher/image_decoder.h" | 24 #include "components/image_fetcher/image_decoder.h" |
25 #include "components/image_fetcher/image_fetcher.h" | 25 #include "components/image_fetcher/image_fetcher.h" |
26 #include "components/ntp_snippets/category_factory.h" | 26 #include "components/ntp_snippets/category_factory.h" |
| 27 #include "components/ntp_snippets/mock_content_suggestions_provider_observer.h" |
27 #include "components/ntp_snippets/ntp_snippet.h" | 28 #include "components/ntp_snippets/ntp_snippet.h" |
28 #include "components/ntp_snippets/ntp_snippets_database.h" | 29 #include "components/ntp_snippets/ntp_snippets_database.h" |
29 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 30 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
30 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 31 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
31 #include "components/ntp_snippets/ntp_snippets_test_utils.h" | 32 #include "components/ntp_snippets/ntp_snippets_test_utils.h" |
32 #include "components/ntp_snippets/switches.h" | 33 #include "components/ntp_snippets/switches.h" |
33 #include "components/prefs/testing_pref_service.h" | 34 #include "components/prefs/testing_pref_service.h" |
34 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 35 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
35 #include "components/signin/core/browser/fake_signin_manager.h" | 36 #include "components/signin/core/browser/fake_signin_manager.h" |
36 #include "google_apis/google_api_keys.h" | 37 #include "google_apis/google_api_keys.h" |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 class MockScheduler : public NTPSnippetsScheduler { | 220 class MockScheduler : public NTPSnippetsScheduler { |
220 public: | 221 public: |
221 MOCK_METHOD4(Schedule, | 222 MOCK_METHOD4(Schedule, |
222 bool(base::TimeDelta period_wifi_charging, | 223 bool(base::TimeDelta period_wifi_charging, |
223 base::TimeDelta period_wifi, | 224 base::TimeDelta period_wifi, |
224 base::TimeDelta period_fallback, | 225 base::TimeDelta period_fallback, |
225 base::Time reschedule_time)); | 226 base::Time reschedule_time)); |
226 MOCK_METHOD0(Unschedule, bool()); | 227 MOCK_METHOD0(Unschedule, bool()); |
227 }; | 228 }; |
228 | 229 |
229 class MockProviderObserver : public ContentSuggestionsProvider::Observer { | |
230 public: | |
231 void OnNewSuggestions(ContentSuggestionsProvider* provider, | |
232 Category category, | |
233 std::vector<ContentSuggestion> suggestions) override {} | |
234 MOCK_METHOD3(OnCategoryStatusChanged, | |
235 void(ContentSuggestionsProvider* provider, | |
236 Category category, | |
237 CategoryStatus new_status)); | |
238 }; | |
239 | |
240 class WaitForDBLoad { | 230 class WaitForDBLoad { |
241 public: | 231 public: |
242 WaitForDBLoad(MockProviderObserver* observer, NTPSnippetsService* service) | 232 WaitForDBLoad(MockContentSuggestionsProviderObserver* observer, |
| 233 NTPSnippetsService* service) |
243 : observer_(observer) { | 234 : observer_(observer) { |
244 EXPECT_CALL(*observer_, OnCategoryStatusChanged(_, _, _)) | 235 EXPECT_CALL(*observer_, OnCategoryStatusChanged(_, _, _)) |
245 .WillOnce(Invoke(this, &WaitForDBLoad::OnCategoryStatusChanged)); | 236 .WillOnce(Invoke(this, &WaitForDBLoad::OnCategoryStatusChanged)); |
246 if (!service->ready()) | 237 if (!service->ready()) |
247 run_loop_.Run(); | 238 run_loop_.Run(); |
248 } | 239 } |
249 | 240 |
250 ~WaitForDBLoad() { Mock::VerifyAndClearExpectations(observer_); } | 241 ~WaitForDBLoad() { Mock::VerifyAndClearExpectations(observer_); } |
251 | 242 |
252 private: | 243 private: |
253 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, | 244 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, |
254 Category category, | 245 Category category, |
255 CategoryStatus new_status) { | 246 CategoryStatus new_status) { |
256 EXPECT_EQ(new_status, CategoryStatus::AVAILABLE_LOADING); | 247 EXPECT_EQ(new_status, CategoryStatus::AVAILABLE_LOADING); |
257 run_loop_.Quit(); | 248 run_loop_.Quit(); |
258 } | 249 } |
259 | 250 |
260 MockProviderObserver* observer_; | 251 MockContentSuggestionsProviderObserver* observer_; |
261 base::RunLoop run_loop_; | 252 base::RunLoop run_loop_; |
262 | 253 |
263 DISALLOW_COPY_AND_ASSIGN(WaitForDBLoad); | 254 DISALLOW_COPY_AND_ASSIGN(WaitForDBLoad); |
264 }; | 255 }; |
265 | 256 |
266 } // namespace | 257 } // namespace |
267 | 258 |
268 class NTPSnippetsServiceTest : public test::NTPSnippetsTestBase { | 259 class NTPSnippetsServiceTest : public test::NTPSnippetsTestBase { |
269 public: | 260 public: |
270 NTPSnippetsServiceTest() | 261 NTPSnippetsServiceTest() |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 return service()->MakeUniqueID(articles_category(), within_category_id); | 335 return service()->MakeUniqueID(articles_category(), within_category_id); |
345 } | 336 } |
346 | 337 |
347 Category articles_category() { | 338 Category articles_category() { |
348 return category_factory_.FromKnownCategory(KnownCategories::ARTICLES); | 339 return category_factory_.FromKnownCategory(KnownCategories::ARTICLES); |
349 } | 340 } |
350 | 341 |
351 protected: | 342 protected: |
352 const GURL& test_url() { return test_url_; } | 343 const GURL& test_url() { return test_url_; } |
353 NTPSnippetsService* service() { return service_.get(); } | 344 NTPSnippetsService* service() { return service_.get(); } |
354 MockProviderObserver& observer() { return observer_; } | 345 MockContentSuggestionsProviderObserver& observer() { return observer_; } |
355 MockScheduler& mock_scheduler() { return scheduler_; } | 346 MockScheduler& mock_scheduler() { return scheduler_; } |
356 | 347 |
357 // Provide the json to be returned by the fake fetcher. | 348 // Provide the json to be returned by the fake fetcher. |
358 void SetUpFetchResponse(const std::string& json) { | 349 void SetUpFetchResponse(const std::string& json) { |
359 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, | 350 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, |
360 net::URLRequestStatus::SUCCESS); | 351 net::URLRequestStatus::SUCCESS); |
361 } | 352 } |
362 | 353 |
363 void LoadFromJSONString(const std::string& json) { | 354 void LoadFromJSONString(const std::string& json) { |
364 SetUpFetchResponse(json); | 355 SetUpFetchResponse(json); |
365 service()->FetchSnippets(true); | 356 service()->FetchSnippets(true); |
366 base::RunLoop().RunUntilIdle(); | 357 base::RunLoop().RunUntilIdle(); |
367 } | 358 } |
368 | 359 |
369 private: | 360 private: |
370 base::MessageLoop message_loop_; | 361 base::MessageLoop message_loop_; |
371 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 362 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
372 // Instantiation of factory automatically sets itself as URLFetcher's factory. | 363 // Instantiation of factory automatically sets itself as URLFetcher's factory. |
373 net::FakeURLFetcherFactory fake_url_fetcher_factory_; | 364 net::FakeURLFetcherFactory fake_url_fetcher_factory_; |
374 const GURL test_url_; | 365 const GURL test_url_; |
375 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 366 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
376 MockScheduler scheduler_; | 367 MockScheduler scheduler_; |
377 MockProviderObserver observer_; | 368 MockContentSuggestionsProviderObserver observer_; |
378 CategoryFactory category_factory_; | 369 CategoryFactory category_factory_; |
379 // Last so that the dependencies are deleted after the service. | 370 // Last so that the dependencies are deleted after the service. |
380 std::unique_ptr<NTPSnippetsService> service_; | 371 std::unique_ptr<NTPSnippetsService> service_; |
381 | 372 |
382 base::ScopedTempDir database_dir_; | 373 base::ScopedTempDir database_dir_; |
383 | 374 |
384 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); | 375 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); |
385 }; | 376 }; |
386 | 377 |
387 TEST_F(NTPSnippetsServiceTest, ScheduleOnStart) { | 378 TEST_F(NTPSnippetsServiceTest, ScheduleOnStart) { |
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
880 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 871 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
881 service()->OnDisabledReasonChanged(DisabledReason::NONE); | 872 service()->OnDisabledReasonChanged(DisabledReason::NONE); |
882 EXPECT_CALL(observer(), | 873 EXPECT_CALL(observer(), |
883 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE)); | 874 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE)); |
884 base::RunLoop().RunUntilIdle(); | 875 base::RunLoop().RunUntilIdle(); |
885 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); | 876 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); |
886 EXPECT_FALSE(service()->GetSnippetsForTesting().empty()); | 877 EXPECT_FALSE(service()->GetSnippetsForTesting().empty()); |
887 } | 878 } |
888 | 879 |
889 } // namespace ntp_snippets | 880 } // namespace ntp_snippets |
OLD | NEW |