Chromium Code Reviews| Index: components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider_unittest.cc |
| diff --git a/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider_unittest.cc b/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider_unittest.cc |
| index a1dc94ddb58ccf6e40b420f116844919c14da42a..2ce0ac68bf12c88414ad7bd95f843fbcafbd3e20 100644 |
| --- a/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider_unittest.cc |
| +++ b/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider_unittest.cc |
| @@ -8,12 +8,16 @@ |
| #include <string> |
| #include <vector> |
| +#include "base/bind.h" |
| #include "base/memory/ptr_util.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/run_loop.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "components/ntp_snippets/category.h" |
| #include "components/ntp_snippets/category_factory.h" |
| #include "components/ntp_snippets/content_suggestions_provider.h" |
| #include "components/ntp_snippets/mock_content_suggestions_provider_observer.h" |
| +#include "components/ntp_snippets/status.h" |
| #include "components/physical_web/data_source/fake_physical_web_data_source.h" |
| #include "components/prefs/testing_pref_service.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| @@ -26,6 +30,8 @@ using physical_web::FakePhysicalWebDataSource; |
| using testing::_; |
| using testing::AnyNumber; |
| using testing::ElementsAre; |
| +using testing::get; |
| +using testing::Pointwise; |
| using testing::StrictMock; |
| using testing::UnorderedElementsAre; |
| @@ -35,8 +41,25 @@ namespace { |
| MATCHER_P(HasUrl, url, "") { |
| *result_listener << "expected URL: " << url |
| - << "has URL: " << arg.url().spec(); |
| - return arg.url().spec() == url; |
| + << ", but has URL: " << arg.url().spec(); |
| + return url == arg.url().spec(); |
| +} |
| + |
| +MATCHER(HasUrlPointwise, "") { |
| + *result_listener << "expected URL: " << get<1>(arg) |
| + << ", but has URL: " << get<0>(arg).url().spec(); |
| + return get<1>(arg) == get<0>(arg).url().spec(); |
| +} |
| + |
| +void CompareFetchMoreResult(const base::Closure& done_closure, |
| + Status expected_status_code, |
| + std::vector<std::string> expected_suggestions_urls, |
| + Status actual_status_code, |
| + std::vector<ContentSuggestion> actual_suggestions) { |
|
Marc Treib
2016/12/06 10:01:34
Should the vectors be const& ?
vitaliii
2016/12/06 12:45:52
Done.
The last vector cannot be const, because the
|
| + EXPECT_EQ(actual_status_code.status, expected_status_code.status); |
| + EXPECT_THAT(actual_suggestions, |
| + Pointwise(HasUrlPointwise(), expected_suggestions_urls)); |
| + done_closure.Run(); |
| } |
| } // namespace |
| @@ -102,6 +125,8 @@ class PhysicalWebPageSuggestionsProviderTest : public testing::Test { |
| StrictMock<MockContentSuggestionsProviderObserver> observer_; |
| CategoryFactory category_factory_; |
| std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| + // Added in order to test provider's |Fetch| method. |
| + base::MessageLoop message_loop_; |
| // Last so that the dependencies are deleted after the provider. |
| std::unique_ptr<PhysicalWebPageSuggestionsProvider> provider_; |
| @@ -139,4 +164,48 @@ TEST_F(PhysicalWebPageSuggestionsProviderTest, ShouldSortByDistance) { |
| CreateProvider(); |
| } |
| +TEST_F(PhysicalWebPageSuggestionsProviderTest, |
| + FetchMoreShouldFilterAndReturnSortedSuggestions) { |
| + IgnoreOnCategoryStatusChangedToAvailable(); |
| + IgnoreOnSuggestionInvalidated(); |
| + std::vector<int> ids; |
| + const int kNumSuggestionsInSection = 10; |
| + const int kNumSuggestionsInSource = 3 * kNumSuggestionsInSection; |
| + for (int i = 1; i <= kNumSuggestionsInSource; ++i) { |
| + ids.push_back(i); |
| + } |
| + // |CreateDummyPhysicalWebPages| builds pages with distances 1, 2, 3, ... , |
| + // so we know the order of suggestions in the provider. |
| + physical_web_data_source()->SetMetadata(CreateDummyPhysicalWebPages(ids)); |
| + EXPECT_CALL(*observer(), OnNewSuggestions(_, provided_category(), _)); |
| + CreateProvider(); |
| + |
| + const std::string dummy_resolved_url = "https://resolved_url.com/"; |
| + std::set<std::string> known_ids; |
| + for (int i = 1; i <= kNumSuggestionsInSection; ++i) { |
| + known_ids.insert(dummy_resolved_url + base::IntToString(i)); |
| + } |
| + known_ids.insert(dummy_resolved_url + base::IntToString(12)); |
| + known_ids.insert(dummy_resolved_url + base::IntToString(17)); |
| + std::vector<std::string> expected_suggestion_urls; |
| + for (int i = 1; i <= kNumSuggestionsInSource; ++i) { |
| + if (expected_suggestion_urls.size() == kNumSuggestionsInSection) { |
| + break; |
| + } |
| + std::string url = dummy_resolved_url + base::IntToString(i); |
| + if (!known_ids.count(url)) { |
| + expected_suggestion_urls.push_back(url); |
| + } |
| + } |
| + |
| + // Added to wait for |Fetch| callback to be called. |
| + base::RunLoop run_loop; |
| + provider()->Fetch( |
| + provided_category(), known_ids, |
| + base::Bind(CompareFetchMoreResult, run_loop.QuitClosure(), |
| + Status(StatusCode::SUCCESS), expected_suggestion_urls)); |
| + // Wait for the callback to be called. |
| + run_loop.Run(); |
| +} |
| + |
| } // namespace ntp_snippets |