Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Side by Side Diff: components/ntp_snippets/ntp_snippets_service_unittest.cc

Issue 2228553003: a provider of Physical Web pages. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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()) run_loop_.Run();
247 run_loop_.Run();
248 } 238 }
249 239
250 ~WaitForDBLoad() { Mock::VerifyAndClearExpectations(observer_); } 240 ~WaitForDBLoad() { Mock::VerifyAndClearExpectations(observer_); }
251 241
252 private: 242 private:
253 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, 243 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider,
254 Category category, 244 Category category, CategoryStatus new_status) {
255 CategoryStatus new_status) {
256 EXPECT_EQ(new_status, CategoryStatus::AVAILABLE_LOADING); 245 EXPECT_EQ(new_status, CategoryStatus::AVAILABLE_LOADING);
257 run_loop_.Quit(); 246 run_loop_.Quit();
258 } 247 }
259 248
260 MockProviderObserver* observer_; 249 MockContentSuggestionsProviderObserver* observer_;
261 base::RunLoop run_loop_; 250 base::RunLoop run_loop_;
262 251
263 DISALLOW_COPY_AND_ASSIGN(WaitForDBLoad); 252 DISALLOW_COPY_AND_ASSIGN(WaitForDBLoad);
264 }; 253 };
265 254
266 } // namespace 255 } // namespace
267 256
268 class NTPSnippetsServiceTest : public test::NTPSnippetsTestBase { 257 class NTPSnippetsServiceTest : public test::NTPSnippetsTestBase {
269 public: 258 public:
270 NTPSnippetsServiceTest() 259 NTPSnippetsServiceTest()
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 return service()->MakeUniqueID(articles_category(), within_category_id); 333 return service()->MakeUniqueID(articles_category(), within_category_id);
345 } 334 }
346 335
347 Category articles_category() { 336 Category articles_category() {
348 return category_factory_.FromKnownCategory(KnownCategories::ARTICLES); 337 return category_factory_.FromKnownCategory(KnownCategories::ARTICLES);
349 } 338 }
350 339
351 protected: 340 protected:
352 const GURL& test_url() { return test_url_; } 341 const GURL& test_url() { return test_url_; }
353 NTPSnippetsService* service() { return service_.get(); } 342 NTPSnippetsService* service() { return service_.get(); }
354 MockProviderObserver& observer() { return observer_; } 343 MockContentSuggestionsProviderObserver& observer() { return observer_; }
355 MockScheduler& mock_scheduler() { return scheduler_; } 344 MockScheduler& mock_scheduler() { return scheduler_; }
356 345
357 // Provide the json to be returned by the fake fetcher. 346 // Provide the json to be returned by the fake fetcher.
358 void SetUpFetchResponse(const std::string& json) { 347 void SetUpFetchResponse(const std::string& json) {
359 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, 348 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK,
360 net::URLRequestStatus::SUCCESS); 349 net::URLRequestStatus::SUCCESS);
361 } 350 }
362 351
363 void LoadFromJSONString(const std::string& json) { 352 void LoadFromJSONString(const std::string& json) {
364 SetUpFetchResponse(json); 353 SetUpFetchResponse(json);
365 service()->FetchSnippets(true); 354 service()->FetchSnippets(true);
366 base::RunLoop().RunUntilIdle(); 355 base::RunLoop().RunUntilIdle();
367 } 356 }
368 357
369 private: 358 private:
370 base::MessageLoop message_loop_; 359 base::MessageLoop message_loop_;
371 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; 360 FailingFakeURLFetcherFactory failing_url_fetcher_factory_;
372 // Instantiation of factory automatically sets itself as URLFetcher's factory. 361 // Instantiation of factory automatically sets itself as URLFetcher's factory.
373 net::FakeURLFetcherFactory fake_url_fetcher_factory_; 362 net::FakeURLFetcherFactory fake_url_fetcher_factory_;
374 const GURL test_url_; 363 const GURL test_url_;
375 std::unique_ptr<OAuth2TokenService> fake_token_service_; 364 std::unique_ptr<OAuth2TokenService> fake_token_service_;
376 MockScheduler scheduler_; 365 MockScheduler scheduler_;
377 MockProviderObserver observer_; 366 MockContentSuggestionsProviderObserver observer_;
378 CategoryFactory category_factory_; 367 CategoryFactory category_factory_;
379 // Last so that the dependencies are deleted after the service. 368 // Last so that the dependencies are deleted after the service.
380 std::unique_ptr<NTPSnippetsService> service_; 369 std::unique_ptr<NTPSnippetsService> service_;
381 370
382 base::ScopedTempDir database_dir_; 371 base::ScopedTempDir database_dir_;
383 372
384 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); 373 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest);
385 }; 374 };
386 375
387 TEST_F(NTPSnippetsServiceTest, ScheduleOnStart) { 376 TEST_F(NTPSnippetsServiceTest, ScheduleOnStart) {
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); 869 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
881 service()->OnDisabledReasonChanged(DisabledReason::NONE); 870 service()->OnDisabledReasonChanged(DisabledReason::NONE);
882 EXPECT_CALL(observer(), 871 EXPECT_CALL(observer(),
883 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE)); 872 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE));
884 base::RunLoop().RunUntilIdle(); 873 base::RunLoop().RunUntilIdle();
885 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); 874 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_);
886 EXPECT_FALSE(service()->GetSnippetsForTesting().empty()); 875 EXPECT_FALSE(service()->GetSnippetsForTesting().empty());
887 } 876 }
888 877
889 } // namespace ntp_snippets 878 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698