Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: components/ntp_snippets/ntp_snippets_fetcher_unittest.cc

Issue 1943783002: [NTP Snippets] Add unit tests for NTPSnippetsFetcher (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed minor comments Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/ntp_snippets/ntp_snippets_fetcher.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/memory/ptr_util.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "google_apis/google_api_keys.h"
13 #include "net/url_request/test_url_fetcher_factory.h"
14 #include "net/url_request/url_request_test_util.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace ntp_snippets {
19 namespace {
20
21 using testing::IsEmpty;
22 using testing::IsNull;
23 using testing::Not;
24 using testing::NotNull;
25
26 const char kTestContentSnippetsServerFormat[] =
27 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s";
28
29 class MockSnippetsAvailableCallback {
30 public:
31 MOCK_METHOD2(Run, void(const std::string& snippets_json,
32 const std::string& status_message));
33 };
34
35 // Factory for FakeURLFetcher objects that always generate errors.
36 class FailingFakeURLFetcherFactory : public net::URLFetcherFactory {
37 public:
38 std::unique_ptr<net::URLFetcher> CreateURLFetcher(
39 int id, const GURL& url, net::URLFetcher::RequestType request_type,
40 net::URLFetcherDelegate* d) override {
41 return base::WrapUnique(new net::FakeURLFetcher(
42 url, d, /*response_data=*/std::string(), net::HTTP_NOT_FOUND,
43 net::URLRequestStatus::FAILED));
44 }
45 };
46
47 class NTPSnippetsFetcherTest : public testing::Test {
48 public:
49 NTPSnippetsFetcherTest()
50 : fake_url_fetcher_factory_(
51 /*default_factory=*/&failing_url_fetcher_factory_),
52 snippets_fetcher_(scoped_refptr<base::SequencedTaskRunner>(),
53 scoped_refptr<net::TestURLRequestContextGetter>(
54 new net::TestURLRequestContextGetter(
55 base::ThreadTaskRunnerHandle::Get())),
56 /*is_stable_channel=*/true),
57 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat,
58 google_apis::GetAPIKey().c_str())) {
59 snippets_fetcher_subscription_ = snippets_fetcher_.AddCallback(
60 base::Bind(&MockSnippetsAvailableCallback::Run,
61 base::Unretained(&mock_callback_)));
62 }
63
64 net::FakeURLFetcherFactory& fake_url_fetcher_factory() {
65 return fake_url_fetcher_factory_;
66 }
67
68 NTPSnippetsFetcher& snippets_fetcher() { return snippets_fetcher_; }
69 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; }
70 void RunUntilIdle() { message_loop_.RunUntilIdle(); }
71 const GURL& test_url() { return test_url_; }
72
73 private:
74 FailingFakeURLFetcherFactory failing_url_fetcher_factory_;
75 // Instantiation of factory automatically sets itself as URLFetcher's factory.
76 net::FakeURLFetcherFactory fake_url_fetcher_factory_;
77 // Needed to use ThreadTaskRunnerHandle.
78 base::MessageLoop message_loop_;
79 NTPSnippetsFetcher snippets_fetcher_;
80 MockSnippetsAvailableCallback mock_callback_;
81 std::unique_ptr<
82 NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription>
83 snippets_fetcher_subscription_;
84 const GURL test_url_;
85
86 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest);
87 };
88
89 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) {
90 // The lack of registered baked in responses would cause any fetch to fail.
91 RunUntilIdle();
92 }
93
94 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfully) {
95 const std::string json_str = "{ \"recos\": [] }";
96 fake_url_fetcher_factory().SetFakeResponse(test_url(),
97 /*data=*/json_str, net::HTTP_OK,
98 net::URLRequestStatus::SUCCESS);
99 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str,
100 /*status_message=*/std::string()))
101 .Times(1);
102 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
103 /*count=*/1);
104 RunUntilIdle();
105 }
106
107 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) {
108 fake_url_fetcher_factory().SetFakeResponse(test_url(),
109 /*data=*/std::string(),
110 net::HTTP_NOT_FOUND,
111 net::URLRequestStatus::FAILED);
112 EXPECT_CALL(mock_callback(),
113 Run(/*snippets_json=*/std::string(),
114 /*status_message=*/"URLRequestStatus error -2"))
115 .Times(1);
116 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
117 /*count=*/1);
118 RunUntilIdle();
119 }
120
121 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) {
122 fake_url_fetcher_factory().SetFakeResponse(test_url(),
123 /*data=*/std::string(),
124 net::HTTP_NOT_FOUND,
125 net::URLRequestStatus::SUCCESS);
126 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/std::string(),
127 /*status_message=*/"HTTP error 404"))
128 .Times(1);
129 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
130 /*count=*/1);
131 RunUntilIdle();
132 }
133
134 // This test actually verifies that the test setup itself is sane, to prevent
135 // hard-to-reproduce test failures.
136 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) {
137 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/std::string(),
138 /*status_message=*/Not(IsEmpty())))
139 .Times(1);
140 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
141 /*count=*/1);
142 RunUntilIdle();
143 }
144
145 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) {
146 const std::string json_str = "{ \"recos\": [] }";
147 fake_url_fetcher_factory().SetFakeResponse(test_url(),
148 /*data=*/json_str, net::HTTP_OK,
149 net::URLRequestStatus::SUCCESS);
150 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str,
151 /*status_message=*/std::string()))
152 .Times(1);
153 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
154 /*count=*/1);
155 // Second call to FetchSnippets() overrides/cancels the previous. Callback is
156 // expected to be called once.
157 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
158 /*count=*/1);
159 RunUntilIdle();
160 }
161
162 } // namespace
163 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_fetcher.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698