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/remote/remote_suggestions_provider.h" | 5 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 22 #include "base/test/histogram_tester.h" | 22 #include "base/test/histogram_tester.h" |
| 23 #include "base/test/simple_test_tick_clock.h" | |
| 23 #include "base/threading/thread_task_runner_handle.h" | 24 #include "base/threading/thread_task_runner_handle.h" |
| 24 #include "base/time/time.h" | 25 #include "base/time/time.h" |
| 25 #include "components/image_fetcher/image_decoder.h" | 26 #include "components/image_fetcher/image_decoder.h" |
| 26 #include "components/image_fetcher/image_fetcher.h" | 27 #include "components/image_fetcher/image_fetcher.h" |
| 27 #include "components/image_fetcher/image_fetcher_delegate.h" | 28 #include "components/image_fetcher/image_fetcher_delegate.h" |
| 28 #include "components/ntp_snippets/category_factory.h" | 29 #include "components/ntp_snippets/category_factory.h" |
| 29 #include "components/ntp_snippets/category_info.h" | 30 #include "components/ntp_snippets/category_info.h" |
| 30 #include "components/ntp_snippets/ntp_snippets_constants.h" | 31 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 31 #include "components/ntp_snippets/pref_names.h" | 32 #include "components/ntp_snippets/pref_names.h" |
| 32 #include "components/ntp_snippets/remote/ntp_snippet.h" | 33 #include "components/ntp_snippets/remote/ntp_snippet.h" |
| (...skipping 1553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1586 suggestions.push_back(GetSnippetWithUrl( | 1587 suggestions.push_back(GetSnippetWithUrl( |
| 1587 base::StringPrintf("http://localhost/snippet-id-%d", i))); | 1588 base::StringPrintf("http://localhost/snippet-id-%d", i))); |
| 1588 } | 1589 } |
| 1589 LoadFromJSONString(service.get(), GetTestJson(suggestions)); | 1590 LoadFromJSONString(service.get(), GetTestJson(suggestions)); |
| 1590 // TODO(tschumann): We should probably trim out any additional results and | 1591 // TODO(tschumann): We should probably trim out any additional results and |
| 1591 // only serve the MaxSnippetCount items. | 1592 // only serve the MaxSnippetCount items. |
| 1592 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 1593 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
| 1593 SizeIs(service->GetMaxSnippetCountForTesting() + 1)); | 1594 SizeIs(service->GetMaxSnippetCountForTesting() + 1)); |
| 1594 } | 1595 } |
| 1595 | 1596 |
| 1597 TEST_F(RemoteSuggestionsProviderTest, StoreLastSuccessfullBackgroundFetchTime) { | |
| 1598 // On initialization of the RemoteSuggestionsProvider a background fetch is | |
| 1599 // triggered since the snippets DB is empty. Therefore the service must not be | |
| 1600 // initialized until the test clock is set. | |
| 1601 auto service = MakeSnippetsServiceWithoutInitialization(); | |
| 1602 | |
| 1603 auto simple_test_tick_clock = base::MakeUnique<base::SimpleTestTickClock>(); | |
| 1604 base::SimpleTestTickClock* simple_test_tick_clock_ptr = | |
| 1605 simple_test_tick_clock.get(); | |
| 1606 service->SetTickClockForTesting(std::move(simple_test_tick_clock)); | |
| 1607 | |
| 1608 // Test that the preference is correctly initialized with the default value 0. | |
| 1609 EXPECT_EQ( | |
| 1610 0, pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); | |
| 1611 | |
| 1612 WaitForSnippetsServiceInitialization(service.get(), | |
| 1613 /*set_empty_response=*/true); | |
| 1614 EXPECT_EQ( | |
| 1615 simple_test_tick_clock_ptr->NowTicks().ToInternalValue(), | |
| 1616 pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); | |
| 1617 | |
| 1618 // Advance the time and check whether the time was updated correctly after the | |
| 1619 // background fetch. | |
| 1620 simple_test_tick_clock_ptr->Advance(TimeDelta::FromHours(1)); | |
| 1621 service->FetchSnippetsInTheBackground(); | |
| 1622 base::RunLoop().RunUntilIdle(); | |
| 1623 EXPECT_EQ( | |
| 1624 simple_test_tick_clock_ptr->NowTicks().ToInternalValue(), | |
| 1625 pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); | |
| 1626 } | |
|
Marc Treib
2016/12/08 10:33:49
Optional: You could re-create the service here, to
markusheintz_
2016/12/08 11:37:18
Excellent idea. Once the scheduler refactoring is
| |
| 1627 | |
| 1596 } // namespace ntp_snippets | 1628 } // namespace ntp_snippets |
| OLD | NEW |