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

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

Issue 2000233002: [NTP Snippets] Unschedule fetches when the service should be disabled (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@SnippetsDB
Patch Set: Created 4 years, 6 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
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 public: 236 public:
237 MOCK_METHOD0(NTPSnippetsServiceLoaded, void()); 237 MOCK_METHOD0(NTPSnippetsServiceLoaded, void());
238 MOCK_METHOD0(NTPSnippetsServiceShutdown, void()); 238 MOCK_METHOD0(NTPSnippetsServiceShutdown, void());
239 MOCK_METHOD0(NTPSnippetsServiceDisabled, void()); 239 MOCK_METHOD0(NTPSnippetsServiceDisabled, void());
240 }; 240 };
241 241
242 class WaitForDBLoad : public NTPSnippetsServiceObserver { 242 class WaitForDBLoad : public NTPSnippetsServiceObserver {
243 public: 243 public:
244 WaitForDBLoad(NTPSnippetsService* service) : service_(service) { 244 WaitForDBLoad(NTPSnippetsService* service) : service_(service) {
245 service_->AddObserver(this); 245 service_->AddObserver(this);
246 if (!service_->loaded()) 246 if (!service_->ready())
247 run_loop_.Run(); 247 run_loop_.Run();
248 } 248 }
249 249
250 ~WaitForDBLoad() override { 250 ~WaitForDBLoad() override {
251 service_->RemoveObserver(this); 251 service_->RemoveObserver(this);
252 } 252 }
253 253
254 private: 254 private:
255 void NTPSnippetsServiceLoaded() override { 255 void NTPSnippetsServiceLoaded() override {
256 EXPECT_TRUE(service_->loaded()); 256 EXPECT_TRUE(service_->ready());
257 run_loop_.Quit(); 257 run_loop_.Quit();
258 } 258 }
259 259
260 void NTPSnippetsServiceShutdown() override {} 260 void NTPSnippetsServiceShutdown() override {}
261 void NTPSnippetsServiceDisabled() override {} 261 void NTPSnippetsServiceDisabled() override {}
262 262
263 NTPSnippetsService* service_; 263 NTPSnippetsService* service_;
264 base::RunLoop run_loop_; 264 base::RunLoop run_loop_;
265 265
266 DISALLOW_COPY_AND_ASSIGN(WaitForDBLoad); 266 DISALLOW_COPY_AND_ASSIGN(WaitForDBLoad);
(...skipping 27 matching lines...) Expand all
294 service_->Shutdown(); 294 service_->Shutdown();
295 295
296 // We need to run the message loop after deleting the database, because 296 // We need to run the message loop after deleting the database, because
297 // ProtoDatabaseImpl deletes the actual LevelDB asynchronously on the task 297 // ProtoDatabaseImpl deletes the actual LevelDB asynchronously on the task
298 // runner. Without this, we'd get reports of memory leaks. 298 // runner. Without this, we'd get reports of memory leaks.
299 service_.reset(); 299 service_.reset();
300 base::RunLoop().RunUntilIdle(); 300 base::RunLoop().RunUntilIdle();
301 } 301 }
302 302
303 void SetUp() override { 303 void SetUp() override {
304 ResetSyncServiceMock();
304 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); 305 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
305 CreateSnippetsService(/*enabled=*/true); 306 CreateSnippetsService(/*enabled=*/true);
306 } 307 }
307 308
308 void CreateSnippetsService(bool enabled) { 309 void CreateSnippetsService(bool enabled) {
309 if (service_) 310 if (service_)
310 service_->Shutdown(); 311 service_->Shutdown();
311 312
312 scoped_refptr<base::SingleThreadTaskRunner> task_runner( 313 scoped_refptr<base::SingleThreadTaskRunner> task_runner(
313 base::ThreadTaskRunnerHandle::Get()); 314 base::ThreadTaskRunnerHandle::Get());
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 std::unique_ptr<OAuth2TokenService> fake_token_service_; 382 std::unique_ptr<OAuth2TokenService> fake_token_service_;
382 MockScheduler scheduler_; 383 MockScheduler scheduler_;
383 // Last so that the dependencies are deleted after the service. 384 // Last so that the dependencies are deleted after the service.
384 std::unique_ptr<NTPSnippetsService> service_; 385 std::unique_ptr<NTPSnippetsService> service_;
385 386
386 base::ScopedTempDir database_dir_; 387 base::ScopedTempDir database_dir_;
387 388
388 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); 389 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest);
389 }; 390 };
390 391
391 class NTPSnippetsServiceWithSyncTest : public NTPSnippetsServiceTest {
392 public:
393 void SetUp() override {
394 ResetSyncServiceMock();
395 NTPSnippetsServiceTest::SetUp();
396 }
397 };
398
399 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { 392 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest {
400 public: 393 public:
401 void SetUp() override { 394 void SetUp() override {
402 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1); 395 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1);
403 CreateSnippetsService(/*enabled=*/false); 396 CreateSnippetsService(/*enabled=*/false);
404 } 397 }
405 }; 398 };
406 399
407 TEST_F(NTPSnippetsServiceTest, ScheduleIfEnabled) { 400 TEST_F(NTPSnippetsServiceTest, ScheduleIfEnabled) {
408 // SetUp() checks that Schedule is called. 401 // SetUp() checks that Schedule is called.
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after
874 // Discard the snippet via the mashable source corpus ID. 867 // Discard the snippet via the mashable source corpus ID.
875 EXPECT_TRUE(service()->DiscardSnippet(source_urls[0])); 868 EXPECT_TRUE(service()->DiscardSnippet(source_urls[0]));
876 EXPECT_THAT(service()->snippets(), IsEmpty()); 869 EXPECT_THAT(service()->snippets(), IsEmpty());
877 870
878 // The same article from the AOL domain should now be detected as discarded. 871 // The same article from the AOL domain should now be detected as discarded.
879 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( 872 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources(
880 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)})); 873 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)}));
881 ASSERT_THAT(service()->snippets(), IsEmpty()); 874 ASSERT_THAT(service()->snippets(), IsEmpty());
882 } 875 }
883 876
884 TEST_F(NTPSnippetsServiceWithSyncTest, SyncStateCompatibility) { 877 TEST_F(NTPSnippetsServiceTest, SyncStateCompatibility) {
885 // The default test setup has a compatible sync state. 878 // The default test setup has a compatible sync state.
886 EXPECT_FALSE(service()->IsSyncStateIncompatible()); 879 EXPECT_EQ(DisabledReason::NONE, service()->GetDisabledReason());
887 880
888 // History sync disabled. 881 // History sync disabled.
889 ON_CALL(*mock_sync_service(), GetActiveDataTypes()) 882 ON_CALL(*mock_sync_service(), GetActiveDataTypes())
890 .WillByDefault(Return(syncer::ModelTypeSet())); 883 .WillByDefault(Return(syncer::ModelTypeSet()));
891 EXPECT_TRUE(service()->IsSyncStateIncompatible()); 884 EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED,
885 service()->GetDisabledReason());
892 ResetSyncServiceMock(); 886 ResetSyncServiceMock();
893 887
894 // Not done loading. 888 // Not done loading.
895 ON_CALL(*mock_sync_service(), ConfigurationDone()) 889 ON_CALL(*mock_sync_service(), ConfigurationDone())
896 .WillByDefault(Return(false)); 890 .WillByDefault(Return(false));
897 ON_CALL(*mock_sync_service(), GetActiveDataTypes()) 891 ON_CALL(*mock_sync_service(), GetActiveDataTypes())
898 .WillByDefault(Return(syncer::ModelTypeSet())); 892 .WillByDefault(Return(syncer::ModelTypeSet()));
899 EXPECT_FALSE(service()->IsSyncStateIncompatible()); 893 EXPECT_EQ(DisabledReason::HISTORY_SYNC_STATE_UNKNOWN,
894 service()->GetDisabledReason());
900 ResetSyncServiceMock(); 895 ResetSyncServiceMock();
901 896
902 // Sync disabled. 897 // Sync disabled.
903 ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false)); 898 ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false));
904 EXPECT_TRUE(service()->IsSyncStateIncompatible()); 899 EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED,
900 service()->GetDisabledReason());
905 ResetSyncServiceMock(); 901 ResetSyncServiceMock();
906 902
907 // No service. 903 // No service.
908 service()->sync_service_ = nullptr; 904 service()->sync_service_ = nullptr;
909 EXPECT_TRUE(service()->IsSyncStateIncompatible()); 905 EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED,
906 service()->GetDisabledReason());
910 } 907 }
911 908
912 TEST_F(NTPSnippetsServiceWithSyncTest, HistorySyncStateChanges) { 909 TEST_F(NTPSnippetsServiceTest, HistorySyncStateChanges) {
913 MockServiceObserver mock_observer; 910 MockServiceObserver mock_observer;
914 service()->AddObserver(&mock_observer); 911 service()->AddObserver(&mock_observer);
915 912
916 // Simulate user disabled sync. 913 // Simulate user disabled sync.
917 ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false)); 914 ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false));
918 // The service should notify observers it's been disabled and clear the 915 // The service should notify observers it's been disabled and clear the
919 // snippets instead of pulling new ones. 916 // snippets instead of pulling new ones.
920 EXPECT_CALL(mock_observer, NTPSnippetsServiceDisabled()); 917 EXPECT_CALL(mock_observer, NTPSnippetsServiceDisabled());
921 SetUpFetchResponse(GetTestJson({GetSnippet()})); 918 SetUpFetchResponse(GetTestJson({GetSnippet()}));
922 service()->OnStateChanged(); 919 service()->OnStateChanged();
923 base::RunLoop().RunUntilIdle(); 920 base::RunLoop().RunUntilIdle();
921 EXPECT_EQ(NTPSnippetsService::State::DISABLED, service()->state_);
924 EXPECT_THAT(service()->snippets(), IsEmpty()); // No fetch should be made. 922 EXPECT_THAT(service()->snippets(), IsEmpty()); // No fetch should be made.
925 923
926 // Simulate user sign in. 924 // Simulate user sign in.
927 ResetSyncServiceMock(); 925 ResetSyncServiceMock();
928 // The service should be ready again and load snippets. 926 // The service should be ready again and load snippets.
927 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
929 SetUpFetchResponse(GetTestJson({GetSnippet()})); 928 SetUpFetchResponse(GetTestJson({GetSnippet()}));
930 service()->OnStateChanged(); 929 service()->OnStateChanged();
931 base::RunLoop().RunUntilIdle(); 930 base::RunLoop().RunUntilIdle();
931 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_);
932 EXPECT_FALSE(service()->snippets().empty()); 932 EXPECT_FALSE(service()->snippets().empty());
933 933
934 service()->RemoveObserver(&mock_observer); 934 service()->RemoveObserver(&mock_observer);
935 } 935 }
936 936
937 } // namespace ntp_snippets 937 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698