Chromium Code Reviews| 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" |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 Loading... | |
| 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 class NTPSnippetsServiceWithSyncTest : public NTPSnippetsServiceTest { |
| 392 public: | 393 public: |
| 393 void SetUp() override { | 394 void SetUp() override { |
| 394 ResetSyncServiceMock(); | 395 // ResetSyncServiceMock(); |
|
Marc Treib
2016/06/07 08:55:39
?
dgn
2016/06/07 11:22:47
Added that to the base test class, removed this su
Marc Treib
2016/06/07 11:50:37
Yay!
| |
| 395 NTPSnippetsServiceTest::SetUp(); | 396 NTPSnippetsServiceTest::SetUp(); |
| 396 } | 397 } |
| 397 }; | 398 }; |
| 398 | 399 |
| 399 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { | 400 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { |
| 400 public: | 401 public: |
| 401 void SetUp() override { | 402 void SetUp() override { |
| 402 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1); | 403 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1); |
| 403 CreateSnippetsService(/*enabled=*/false); | 404 CreateSnippetsService(/*enabled=*/false); |
| 404 } | 405 } |
| (...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 876 EXPECT_THAT(service()->snippets(), IsEmpty()); | 877 EXPECT_THAT(service()->snippets(), IsEmpty()); |
| 877 | 878 |
| 878 // The same article from the AOL domain should now be detected as discarded. | 879 // The same article from the AOL domain should now be detected as discarded. |
| 879 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( | 880 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( |
| 880 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)})); | 881 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)})); |
| 881 ASSERT_THAT(service()->snippets(), IsEmpty()); | 882 ASSERT_THAT(service()->snippets(), IsEmpty()); |
| 882 } | 883 } |
| 883 | 884 |
| 884 TEST_F(NTPSnippetsServiceWithSyncTest, SyncStateCompatibility) { | 885 TEST_F(NTPSnippetsServiceWithSyncTest, SyncStateCompatibility) { |
| 885 // The default test setup has a compatible sync state. | 886 // The default test setup has a compatible sync state. |
| 886 EXPECT_FALSE(service()->IsSyncStateIncompatible()); | 887 EXPECT_EQ(DisabledReason::NONE, service()->GetDisabledReason()); |
| 887 | 888 |
| 888 // History sync disabled. | 889 // History sync disabled. |
| 889 ON_CALL(*mock_sync_service(), GetActiveDataTypes()) | 890 ON_CALL(*mock_sync_service(), GetActiveDataTypes()) |
| 890 .WillByDefault(Return(syncer::ModelTypeSet())); | 891 .WillByDefault(Return(syncer::ModelTypeSet())); |
| 891 EXPECT_TRUE(service()->IsSyncStateIncompatible()); | 892 EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED, |
| 893 service()->GetDisabledReason()); | |
| 892 ResetSyncServiceMock(); | 894 ResetSyncServiceMock(); |
| 893 | 895 |
| 894 // Not done loading. | 896 // Not done loading. |
| 895 ON_CALL(*mock_sync_service(), ConfigurationDone()) | 897 ON_CALL(*mock_sync_service(), ConfigurationDone()) |
| 896 .WillByDefault(Return(false)); | 898 .WillByDefault(Return(false)); |
| 897 ON_CALL(*mock_sync_service(), GetActiveDataTypes()) | 899 ON_CALL(*mock_sync_service(), GetActiveDataTypes()) |
| 898 .WillByDefault(Return(syncer::ModelTypeSet())); | 900 .WillByDefault(Return(syncer::ModelTypeSet())); |
| 899 EXPECT_FALSE(service()->IsSyncStateIncompatible()); | 901 EXPECT_EQ(DisabledReason::HISTORY_SYNC_STATE_UNKNOWN, |
| 902 service()->GetDisabledReason()); | |
| 900 ResetSyncServiceMock(); | 903 ResetSyncServiceMock(); |
| 901 | 904 |
| 902 // Sync disabled. | 905 // Sync disabled. |
| 903 ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false)); | 906 ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false)); |
| 904 EXPECT_TRUE(service()->IsSyncStateIncompatible()); | 907 EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED, |
| 908 service()->GetDisabledReason()); | |
| 905 ResetSyncServiceMock(); | 909 ResetSyncServiceMock(); |
| 906 | 910 |
| 907 // No service. | 911 // No service. |
| 908 service()->sync_service_ = nullptr; | 912 service()->sync_service_ = nullptr; |
| 909 EXPECT_TRUE(service()->IsSyncStateIncompatible()); | 913 EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED, |
| 914 service()->GetDisabledReason()); | |
| 910 } | 915 } |
| 911 | 916 |
| 912 TEST_F(NTPSnippetsServiceWithSyncTest, HistorySyncStateChanges) { | 917 TEST_F(NTPSnippetsServiceWithSyncTest, HistorySyncStateChanges) { |
| 913 MockServiceObserver mock_observer; | 918 MockServiceObserver mock_observer; |
| 914 service()->AddObserver(&mock_observer); | 919 service()->AddObserver(&mock_observer); |
| 915 | 920 |
| 916 // Simulate user disabled sync. | 921 // Simulate user disabled sync. |
| 917 ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false)); | 922 ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false)); |
| 918 // The service should notify observers it's been disabled and clear the | 923 // The service should notify observers it's been disabled and clear the |
| 919 // snippets instead of pulling new ones. | 924 // snippets instead of pulling new ones. |
| 920 EXPECT_CALL(mock_observer, NTPSnippetsServiceDisabled()); | 925 EXPECT_CALL(mock_observer, NTPSnippetsServiceDisabled()); |
| 921 SetUpFetchResponse(GetTestJson({GetSnippet()})); | 926 SetUpFetchResponse(GetTestJson({GetSnippet()})); |
| 922 service()->OnStateChanged(); | 927 service()->OnStateChanged(); |
| 923 base::RunLoop().RunUntilIdle(); | 928 base::RunLoop().RunUntilIdle(); |
| 929 EXPECT_EQ(NTPSnippetsService::State::DISABLED, service()->state_); | |
| 924 EXPECT_THAT(service()->snippets(), IsEmpty()); // No fetch should be made. | 930 EXPECT_THAT(service()->snippets(), IsEmpty()); // No fetch should be made. |
| 925 | 931 |
| 926 // Simulate user sign in. | 932 // Simulate user sign in. |
| 927 ResetSyncServiceMock(); | 933 ResetSyncServiceMock(); |
| 928 // The service should be ready again and load snippets. | 934 // The service should be ready again and load snippets. |
| 935 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | |
| 929 SetUpFetchResponse(GetTestJson({GetSnippet()})); | 936 SetUpFetchResponse(GetTestJson({GetSnippet()})); |
| 930 service()->OnStateChanged(); | 937 service()->OnStateChanged(); |
| 931 base::RunLoop().RunUntilIdle(); | 938 base::RunLoop().RunUntilIdle(); |
| 939 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); | |
| 932 EXPECT_FALSE(service()->snippets().empty()); | 940 EXPECT_FALSE(service()->snippets().empty()); |
| 933 | 941 |
| 934 service()->RemoveObserver(&mock_observer); | 942 service()->RemoveObserver(&mock_observer); |
| 935 } | 943 } |
| 936 | 944 |
| 937 } // namespace ntp_snippets | 945 } // namespace ntp_snippets |
| OLD | NEW |