OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/reading_list/reading_list_suggestions_provider
.h" | 5 #include "components/ntp_snippets/reading_list/reading_list_suggestions_provider
.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/test/simple_test_clock.h" | 8 #include "base/test/simple_test_clock.h" |
9 #include "components/ntp_snippets/mock_content_suggestions_provider_observer.h" | 9 #include "components/ntp_snippets/mock_content_suggestions_provider_observer.h" |
10 #include "components/reading_list/core/reading_list_model_impl.h" | 10 #include "components/reading_list/core/reading_list_model_impl.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace ntp_snippets { | 14 namespace ntp_snippets { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
| 18 using ::testing::_; |
| 19 using ::testing::ElementsAre; |
| 20 using ::testing::IsEmpty; |
| 21 using ::testing::Property; |
| 22 |
18 class ReadingListSuggestionsProviderTest : public ::testing::Test { | 23 class ReadingListSuggestionsProviderTest : public ::testing::Test { |
19 public: | 24 public: |
20 ReadingListSuggestionsProviderTest() { | 25 ReadingListSuggestionsProviderTest() { |
| 26 std::unique_ptr<base::SimpleTestClock> clock = |
| 27 base::MakeUnique<base::SimpleTestClock>(); |
| 28 clock_ = clock.get(); |
21 model_ = base::MakeUnique<ReadingListModelImpl>( | 29 model_ = base::MakeUnique<ReadingListModelImpl>( |
22 /*storage_layer=*/nullptr, /*pref_service=*/nullptr, | 30 /*storage_layer=*/nullptr, /*pref_service=*/nullptr, std::move(clock)); |
23 base::MakeUnique<base::SimpleTestClock>()); | 31 } |
| 32 |
| 33 void CreateProvider() { |
24 EXPECT_CALL(observer_, | 34 EXPECT_CALL(observer_, |
25 OnCategoryStatusChanged(testing::_, ReadingListCategory(), | 35 OnCategoryStatusChanged(_, ReadingListCategory(), |
26 CategoryStatus::AVAILABLE_LOADING)) | 36 CategoryStatus::AVAILABLE_LOADING)) |
27 .RetiresOnSaturation(); | 37 .RetiresOnSaturation(); |
28 | 38 EXPECT_CALL(observer_, OnCategoryStatusChanged(_, ReadingListCategory(), |
| 39 CategoryStatus::AVAILABLE)) |
| 40 .RetiresOnSaturation(); |
29 provider_ = base::MakeUnique<ReadingListSuggestionsProvider>(&observer_, | 41 provider_ = base::MakeUnique<ReadingListSuggestionsProvider>(&observer_, |
30 model_.get()); | 42 model_.get()); |
31 } | 43 } |
32 | 44 |
33 Category ReadingListCategory() { | 45 Category ReadingListCategory() { |
34 return Category::FromKnownCategory(KnownCategories::READING_LIST); | 46 return Category::FromKnownCategory(KnownCategories::READING_LIST); |
35 } | 47 } |
36 | 48 |
37 protected: | 49 protected: |
| 50 base::SimpleTestClock* clock_; |
38 std::unique_ptr<ReadingListModelImpl> model_; | 51 std::unique_ptr<ReadingListModelImpl> model_; |
39 testing::StrictMock<MockContentSuggestionsProviderObserver> observer_; | 52 testing::StrictMock<MockContentSuggestionsProviderObserver> observer_; |
40 std::unique_ptr<ReadingListSuggestionsProvider> provider_; | 53 std::unique_ptr<ReadingListSuggestionsProvider> provider_; |
41 }; | 54 }; |
42 | 55 |
43 TEST_F(ReadingListSuggestionsProviderTest, CategoryInfo) { | 56 TEST_F(ReadingListSuggestionsProviderTest, CategoryInfo) { |
| 57 EXPECT_CALL(observer_, OnNewSuggestions(_, ReadingListCategory(), IsEmpty())) |
| 58 .RetiresOnSaturation(); |
| 59 CreateProvider(); |
| 60 |
44 CategoryInfo categoryInfo = provider_->GetCategoryInfo(ReadingListCategory()); | 61 CategoryInfo categoryInfo = provider_->GetCategoryInfo(ReadingListCategory()); |
45 EXPECT_EQ(ContentSuggestionsAdditionalAction::VIEW_ALL, | 62 EXPECT_EQ(ContentSuggestionsAdditionalAction::VIEW_ALL, |
46 categoryInfo.additional_action()); | 63 categoryInfo.additional_action()); |
47 } | 64 } |
48 | 65 |
| 66 TEST_F(ReadingListSuggestionsProviderTest, ReturnsThreeLatestUnreadSuggestion) { |
| 67 GURL url_unread1 = GURL("http://www.foo1.bar"); |
| 68 GURL url_unread2 = GURL("http://www.foo2.bar"); |
| 69 GURL url_unread3 = GURL("http://www.foo3.bar"); |
| 70 GURL url_unread4 = GURL("http://www.foo4.bar"); |
| 71 GURL url_read1 = GURL("http://www.bar.foor"); |
| 72 std::string title_unread1 = "title1"; |
| 73 std::string title_unread2 = "title2"; |
| 74 std::string title_unread3 = "title3"; |
| 75 std::string title_unread4 = "title4"; |
| 76 std::string title_read1 = "title_read1"; |
| 77 model_->AddEntry(url_unread1, title_unread1, |
| 78 reading_list::ADDED_VIA_CURRENT_APP); |
| 79 clock_->Advance(base::TimeDelta::FromMilliseconds(10)); |
| 80 model_->AddEntry(url_unread2, title_unread2, |
| 81 reading_list::ADDED_VIA_CURRENT_APP); |
| 82 clock_->Advance(base::TimeDelta::FromMilliseconds(10)); |
| 83 model_->AddEntry(url_read1, title_read1, reading_list::ADDED_VIA_CURRENT_APP); |
| 84 model_->SetReadStatus(url_read1, true); |
| 85 clock_->Advance(base::TimeDelta::FromMilliseconds(10)); |
| 86 model_->AddEntry(url_unread3, title_unread3, |
| 87 reading_list::ADDED_VIA_CURRENT_APP); |
| 88 clock_->Advance(base::TimeDelta::FromMilliseconds(10)); |
| 89 model_->AddEntry(url_unread4, title_unread4, |
| 90 reading_list::ADDED_VIA_CURRENT_APP); |
| 91 |
| 92 EXPECT_CALL(observer_, |
| 93 OnNewSuggestions( |
| 94 _, ReadingListCategory(), |
| 95 ElementsAre(Property(&ContentSuggestion::url, url_unread4), |
| 96 Property(&ContentSuggestion::url, url_unread3), |
| 97 Property(&ContentSuggestion::url, url_unread2)))); |
| 98 |
| 99 CreateProvider(); |
| 100 } |
| 101 |
| 102 // Tests that the provider returns only unread suggestions even if there is less |
| 103 // unread suggestions than the maximum number of suggestions. |
| 104 TEST_F(ReadingListSuggestionsProviderTest, ReturnsOnlyUnreadSuggestion) { |
| 105 GURL url_unread1 = GURL("http://www.foo1.bar"); |
| 106 GURL url_read1 = GURL("http://www.bar.foor"); |
| 107 std::string title_unread1 = "title1"; |
| 108 std::string title_read1 = "title_read1"; |
| 109 model_->AddEntry(url_unread1, title_unread1, |
| 110 reading_list::ADDED_VIA_CURRENT_APP); |
| 111 clock_->Advance(base::TimeDelta::FromMilliseconds(10)); |
| 112 model_->AddEntry(url_read1, title_read1, reading_list::ADDED_VIA_CURRENT_APP); |
| 113 model_->SetReadStatus(url_read1, true); |
| 114 |
| 115 EXPECT_CALL(observer_, |
| 116 OnNewSuggestions( |
| 117 _, ReadingListCategory(), |
| 118 ElementsAre(Property(&ContentSuggestion::url, url_unread1)))); |
| 119 |
| 120 CreateProvider(); |
| 121 } |
| 122 |
49 } // namespace | 123 } // namespace |
| 124 |
50 } // namespace ntp_snippets | 125 } // namespace ntp_snippets |
OLD | NEW |