Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | |
| 6 | |
| 7 #include "base/message_loop/message_loop.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "base/strings/stringprintf.h" | |
| 10 #include "base/thread_task_runner_handle.h" | |
| 11 #include "google_apis/google_api_keys.h" | |
| 12 #include "net/url_request/test_url_fetcher_factory.h" | |
| 13 #include "net/url_request/url_request_test_util.h" | |
| 14 #include "testing/gmock/include/gmock/gmock.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace ntp_snippets { | |
| 18 namespace { | |
| 19 | |
| 20 using testing::IsEmpty; | |
| 21 using testing::IsNull; | |
| 22 using testing::Not; | |
| 23 using testing::NotNull; | |
| 24 | |
| 25 const char kTestContentSnippetsServerFormat[] = | |
| 26 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s"; | |
| 27 | |
| 28 class MockSnippetsAvailableCallback { | |
| 29 public: | |
| 30 MOCK_METHOD2(Run, void(const std::string& snippets_json, | |
| 31 const std::string& status_message)); | |
| 32 }; | |
| 33 | |
| 34 // Factory for FakeURLFetcher objects that always generate errors. | |
| 35 class FailingFakeURLFetcherFactory : public net::URLFetcherFactory { | |
| 36 public: | |
| 37 FailingFakeURLFetcherFactory() = default; | |
|
Marc Treib
2016/05/03 11:33:42
Is this required? The default ctor should be gener
mastiz
2016/05/03 11:48:57
Removed. You're right, I incorrectly assumed URLFe
| |
| 38 | |
| 39 std::unique_ptr<net::URLFetcher> CreateURLFetcher( | |
| 40 int id, const GURL& url, net::URLFetcher::RequestType request_type, | |
| 41 net::URLFetcherDelegate* d) override { | |
| 42 return std::unique_ptr<net::FakeURLFetcher>(new net::FakeURLFetcher( | |
|
Marc Treib
2016/05/03 11:33:42
There's base::WrapUnique, so you don't have to spe
mastiz
2016/05/03 11:48:57
Done.
| |
| 43 url, d, /*response_data=*/std::string(), net::HTTP_NOT_FOUND, | |
| 44 net::URLRequestStatus::FAILED)); | |
| 45 } | |
| 46 }; | |
| 47 | |
| 48 class NTPSnippetsFetcherTest : public testing::Test { | |
| 49 public: | |
| 50 NTPSnippetsFetcherTest() | |
| 51 : fake_url_fetcher_factory_( | |
| 52 /*default_factory=*/&failing_url_fetcher_factory_), | |
| 53 snippets_fetcher_(scoped_refptr<base::SequencedTaskRunner>(), | |
| 54 scoped_refptr<net::TestURLRequestContextGetter>( | |
| 55 new net::TestURLRequestContextGetter( | |
| 56 base::ThreadTaskRunnerHandle::Get())), | |
| 57 /*is_stable_channel=*/true), | |
| 58 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat, | |
| 59 google_apis::GetAPIKey().c_str())) { | |
| 60 snippets_fetcher_subscription_ = snippets_fetcher_.AddCallback( | |
| 61 base::Bind(&MockSnippetsAvailableCallback::Run, | |
| 62 base::Unretained(&mock_callback_))); | |
| 63 } | |
| 64 | |
| 65 net::FakeURLFetcherFactory& fake_url_fetcher_factory() { | |
| 66 return fake_url_fetcher_factory_; | |
| 67 } | |
| 68 | |
| 69 NTPSnippetsFetcher& snippets_fetcher() { return snippets_fetcher_; } | |
| 70 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; } | |
| 71 void RunUntilIdle() { message_loop_.RunUntilIdle(); } | |
| 72 const GURL& test_url() { return test_url_; } | |
| 73 | |
| 74 private: | |
| 75 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | |
| 76 // Instantiation of factory automatically sets itself as URLFetcher's factory. | |
| 77 net::FakeURLFetcherFactory fake_url_fetcher_factory_; | |
| 78 // Needed to use ThreadTaskRunnerHandle. | |
| 79 base::MessageLoop message_loop_; | |
| 80 NTPSnippetsFetcher snippets_fetcher_; | |
| 81 MockSnippetsAvailableCallback mock_callback_; | |
| 82 std::unique_ptr< | |
| 83 NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> | |
| 84 snippets_fetcher_subscription_; | |
| 85 const GURL test_url_; | |
| 86 | |
| 87 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); | |
| 88 }; | |
| 89 | |
| 90 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) { | |
| 91 // The lack of registered baked in responses would cause any fetch to fail. | |
| 92 RunUntilIdle(); | |
| 93 } | |
| 94 | |
| 95 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfully) { | |
| 96 const std::string json_str = "{ \"recos\": [] }"; | |
| 97 fake_url_fetcher_factory().SetFakeResponse(test_url(), | |
| 98 /*data=*/json_str, net::HTTP_OK, | |
| 99 net::URLRequestStatus::SUCCESS); | |
| 100 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str, | |
| 101 /*status_message=*/std::string())) | |
| 102 .Times(1); | |
| 103 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), | |
| 104 /*count=*/1); | |
| 105 RunUntilIdle(); | |
| 106 } | |
| 107 | |
| 108 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { | |
| 109 fake_url_fetcher_factory().SetFakeResponse(test_url(), | |
| 110 /*data=*/std::string(), | |
| 111 net::HTTP_NOT_FOUND, | |
| 112 net::URLRequestStatus::FAILED); | |
| 113 EXPECT_CALL(mock_callback(), | |
| 114 Run(/*snippets_json=*/std::string(), | |
| 115 /*status_message=*/"URLRequestStatus error -2")) | |
| 116 .Times(1); | |
| 117 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), | |
| 118 /*count=*/1); | |
| 119 RunUntilIdle(); | |
| 120 } | |
| 121 | |
| 122 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { | |
| 123 fake_url_fetcher_factory().SetFakeResponse(test_url(), | |
| 124 /*data=*/std::string(), | |
| 125 net::HTTP_NOT_FOUND, | |
| 126 net::URLRequestStatus::SUCCESS); | |
| 127 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/std::string(), | |
| 128 /*status_message=*/"HTTP error 404")) | |
| 129 .Times(1); | |
| 130 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), | |
| 131 /*count=*/1); | |
| 132 RunUntilIdle(); | |
| 133 } | |
| 134 | |
| 135 // This test actually verifies that the test setup itself is sane, to prevent | |
| 136 // hard-to-reproduce test failures. | |
| 137 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { | |
| 138 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/std::string(), | |
| 139 /*status_message=*/Not(IsEmpty()))) | |
| 140 .Times(1); | |
| 141 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), | |
| 142 /*count=*/1); | |
| 143 RunUntilIdle(); | |
| 144 } | |
| 145 | |
| 146 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { | |
| 147 const std::string json_str = "{ \"recos\": [] }"; | |
| 148 fake_url_fetcher_factory().SetFakeResponse(test_url(), | |
| 149 /*data=*/json_str, net::HTTP_OK, | |
| 150 net::URLRequestStatus::SUCCESS); | |
| 151 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str, | |
| 152 /*status_message=*/std::string())) | |
| 153 .Times(1); | |
| 154 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), | |
| 155 /*count=*/1); | |
| 156 // Second call to FetchSnippets() overrides/cancels the previous. Callback is | |
| 157 // expected to be called once. | |
| 158 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), | |
| 159 /*count=*/1); | |
| 160 RunUntilIdle(); | |
| 161 } | |
| 162 | |
| 163 } // namespace | |
| 164 } // namespace ntp_snippets | |
| OLD | NEW |