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 b70d2c3d9cbd91acdc13545d2ae2267844ce9ab7..cd5e12633c788b6f6b76227f36f6cb78677b118a 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 |
| @@ -7,31 +7,65 @@ |
| #include <string> |
| #include <vector> |
| +#include "base/memory/ptr_util.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/physical_web_pages/fake_physical_web_data_source.h" |
| +#include "components/prefs/testing_pref_service.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +using testing::_; |
| +using testing::AnyNumber; |
| +using testing::ElementsAre; |
| +using testing::StrictMock; |
| using testing::UnorderedElementsAre; |
| namespace ntp_snippets { |
| namespace { |
| -UrlInfo CreateUrlInfo(const std::string& site_url) { |
| - UrlInfo url_info; |
| - url_info.site_url = GURL(site_url); |
| - return url_info; |
| +std::unique_ptr<base::DictionaryValue> CreatePhysicalWebPage( |
| + const std::string& resolved_url, |
| + double distance_estimate, |
| + int scan_timestamp, |
| + const std::string title, |
| + const std::string description, |
|
Marc Treib
2016/11/23 09:47:52
&
vitaliii
2016/11/23 14:16:53
Done.
|
| + const std::string& scanned_url) { |
| + std::unique_ptr<base::DictionaryValue> page = |
| + base::MakeUnique<base::DictionaryValue>(); |
|
Marc Treib
2016/11/23 09:47:52
optional nit: auto page = ...
(the type is already
vitaliii
2016/11/23 14:16:53
Done.
|
| + page->SetString(kPhysicalWebScannedUrlKey, scanned_url); |
| + page->SetDouble(kPhysicalWebDistanceEstimateKey, distance_estimate); |
| + page->SetInteger(kPhysicalWebScanTimestampKey, scan_timestamp); |
| + page->SetString(kPhysicalWebResolvedUrlKey, resolved_url); |
| + page->SetString(kPhysicalWebTitleKey, title); |
| + page->SetString(kPhysicalWebDescriptionKey, description); |
| + return page; |
| } |
| -std::vector<UrlInfo> CreateUrlInfos(const std::vector<std::string>& site_urls) { |
| - std::vector<UrlInfo> url_infos; |
| - for (const std::string& site_url : site_urls) { |
| - url_infos.emplace_back(CreateUrlInfo(site_url)); |
| +std::unique_ptr<base::DictionaryValue> |
| +CreateDummyPhysicalWebPage(int id, double distance, int timestamp) { |
| + const std::string id_string = base::IntToString(id); |
| + return CreatePhysicalWebPage("https://resolved_url.com/" + id_string, |
| + distance, timestamp, "title " + id_string, |
| + "description " + id_string, |
| + "https://scanned_url.com/" + id_string); |
| +} |
| + |
| +std::unique_ptr<base::ListValue> CreateDummyPhysicalWebPages( |
| + const std::vector<int> ids) { |
|
Marc Treib
2016/11/23 09:47:52
&
vitaliii
2016/11/23 14:16:53
Done.
|
| + int distance = 1; |
| + int timestamp = ids.size(); |
| + std::unique_ptr<base::ListValue> list = base::MakeUnique<base::ListValue>(); |
| + for (int id : ids) { |
| + list->Append(CreateDummyPhysicalWebPage(id, distance, timestamp)); |
| + ++distance; |
| + --timestamp; |
| } |
| - return url_infos; |
| + return list; |
| } |
| MATCHER_P(HasUrl, url, "") { |
| @@ -42,22 +76,108 @@ MATCHER_P(HasUrl, url, "") { |
| } // namespace |
| -TEST(PhysicalWebPageSuggestionsProviderTest, ShouldCreateSuggestions) { |
| - MockContentSuggestionsProviderObserver observer; |
| - CategoryFactory category_factory; |
| - Category category = |
| - category_factory.FromKnownCategory(KnownCategories::PHYSICAL_WEB_PAGES); |
| - PhysicalWebPageSuggestionsProvider provider(&observer, &category_factory); |
| - const std::string first_url = "http://test1.com/"; |
| - const std::string second_url = "http://test2.com/"; |
| - const std::vector<UrlInfo> url_infos = |
| - CreateUrlInfos({first_url, second_url}); |
| - |
| - EXPECT_CALL(observer, |
| +class PhysicalWebPageSuggestionsProviderTest : public testing::Test { |
| + public: |
| + PhysicalWebPageSuggestionsProviderTest() |
| + : pref_service_(new TestingPrefServiceSimple()) {} |
|
Marc Treib
2016/11/23 09:47:52
nit: MakeUnique, to make clear what's going on?
vitaliii
2016/11/23 14:16:53
Done.
|
| + |
| + void IgnoreOnCategoryStatusChangedToAvailable() { |
| + EXPECT_CALL(observer_, OnCategoryStatusChanged(_, provided_category(), |
| + CategoryStatus::AVAILABLE)) |
| + .Times(AnyNumber()); |
| + EXPECT_CALL(observer_, |
| + OnCategoryStatusChanged(_, provided_category(), |
| + CategoryStatus::AVAILABLE_LOADING)) |
| + .Times(AnyNumber()); |
| + } |
| + |
| + void IgnoreOnSuggestionInvalidated() { |
| + EXPECT_CALL(observer_, OnSuggestionInvalidated(_, _)).Times(AnyNumber()); |
| + } |
| + |
| + PhysicalWebPageSuggestionsProvider* CreateProvider() { |
| + DCHECK(!provider_); |
| + provider_ = base::MakeUnique<PhysicalWebPageSuggestionsProvider>( |
| + &observer_, &category_factory_, &physical_web_data_source_); |
| + return provider_.get(); |
| + } |
| + |
| + void DestroyProvider() { provider_.reset(); } |
| + |
| + Category provided_category() { |
| + return category_factory_.FromKnownCategory( |
| + ntp_snippets::KnownCategories::PHYSICAL_WEB_PAGES); |
|
Marc Treib
2016/11/23 09:47:52
nit: "ntp_snippets::" not required
vitaliii
2016/11/23 14:16:53
Done.
|
| + } |
| + |
| + void FireUrlFound(const std::string& url) { |
| + DCHECK(provider_); |
| + provider_->OnFound(url); |
| + } |
| + |
| + void FireUrlLost(const std::string& url) { |
| + DCHECK(provider_); |
| + provider_->OnLost(url); |
| + } |
| + |
| + void FireUrlDistanceChanged(const std::string& url, double new_distance) { |
| + DCHECK(provider_); |
| + provider_->OnDistanceChanged(url, new_distance); |
| + } |
| + |
| + ContentSuggestionsProvider* provider() { |
| + DCHECK(provider_); |
| + return provider_.get(); |
| + } |
| + |
| + FakePhysicalWebDataSource* physical_web_data_source() { |
| + return &physical_web_data_source_; |
| + } |
| + MockContentSuggestionsProviderObserver* observer() { return &observer_; } |
| + TestingPrefServiceSimple* pref_service() { return pref_service_.get(); } |
| + |
| + private: |
| + FakePhysicalWebDataSource physical_web_data_source_; |
| + StrictMock<MockContentSuggestionsProviderObserver> observer_; |
| + CategoryFactory category_factory_; |
| + std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| + // Last so that the dependencies are deleted after the provider. |
| + std::unique_ptr<PhysicalWebPageSuggestionsProvider> provider_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PhysicalWebPageSuggestionsProviderTest); |
| +}; |
| + |
| +TEST_F(PhysicalWebPageSuggestionsProviderTest, |
| + ShouldSubmitSuggestionsOnStartup) { |
| + physical_web_data_source()->SetMetadata( |
| + CreateDummyPhysicalWebPages({1, 2, 3})); |
| + EXPECT_CALL(*observer(), |
| + OnCategoryStatusChanged(_, provided_category(), |
| + CategoryStatus::AVAILABLE_LOADING)); |
| + EXPECT_CALL(*observer(), OnCategoryStatusChanged(_, provided_category(), |
| + CategoryStatus::AVAILABLE)); |
| + EXPECT_CALL(*observer(), |
| OnNewSuggestions( |
| - &provider, category, |
| - UnorderedElementsAre(HasUrl(first_url), HasUrl(second_url)))); |
| - provider.OnDisplayableUrlsChanged(url_infos); |
| + _, provided_category(), |
| + UnorderedElementsAre(HasUrl("https://resolved_url.com/1"), |
| + HasUrl("https://resolved_url.com/2"), |
| + HasUrl("https://resolved_url.com/3")))); |
| + CreateProvider(); |
| +} |
| + |
| +TEST_F(PhysicalWebPageSuggestionsProviderTest, ShouldSortByDistance) { |
| + IgnoreOnCategoryStatusChangedToAvailable(); |
| + IgnoreOnSuggestionInvalidated(); |
| + // |CreateDummyPhysicalWebPages| builds pages with distances 1, 2 and 3 |
| + // respectively. |
| + physical_web_data_source()->SetMetadata( |
| + CreateDummyPhysicalWebPages({3, 2, 1})); |
| + EXPECT_CALL( |
| + *observer(), |
| + OnNewSuggestions(_, provided_category(), |
| + ElementsAre(HasUrl("https://resolved_url.com/3"), |
| + HasUrl("https://resolved_url.com/2"), |
| + HasUrl("https://resolved_url.com/1")))); |
| + CreateProvider(); |
| } |
| } // namespace ntp_snippets |