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

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

Issue 1942913002: Revert of [NTP Snippets] Add unit tests for NTPSnippetsFetcher (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "net/url_request/test_url_fetcher_factory.h"
11 #include "net/url_request/url_request_test_util.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace ntp_snippets {
16 namespace {
17
18 using testing::IsNull;
19 using testing::NotNull;
20
21 const char kTestContentSnippetsServerUrl[] =
22 "https://chromereader-pa.googleapis.com/v1/fetch?key=dummytoken";
23
24 class MockSnippetsAvailableCallback {
25 public:
26 MOCK_METHOD2(Run, void(const std::string& snippets_json,
27 const std::string& status_message));
28 };
29
30 class NTPSnippetsFetcherTest : public testing::Test {
31 public:
32 NTPSnippetsFetcherTest()
33 : fake_url_fetcher_factory_(/*default_factory=*/nullptr),
34 snippets_fetcher_(scoped_refptr<base::SequencedTaskRunner>(),
35 scoped_refptr<net::TestURLRequestContextGetter>(
36 new net::TestURLRequestContextGetter(
37 base::ThreadTaskRunnerHandle::Get())),
38 /*is_stable_channel=*/true) {
39 snippets_fetcher_subscription_ = snippets_fetcher_.AddCallback(
40 base::Bind(&MockSnippetsAvailableCallback::Run,
41 base::Unretained(&mock_callback_)));
42 }
43
44 NTPSnippetsFetcher& snippets_fetcher() { return snippets_fetcher_; }
45
46 net::FakeURLFetcherFactory& fake_url_fetcher_factory() {
47 return fake_url_fetcher_factory_;
48 }
49
50 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; }
51
52 private:
53 // Instantiation of factory automatically sets itself as URLFetcher's factory.
54 net::FakeURLFetcherFactory fake_url_fetcher_factory_;
55 // Needed to use ThreadTaskRunnerHandle.
56 base::MessageLoop message_loop_;
57 NTPSnippetsFetcher snippets_fetcher_;
58 MockSnippetsAvailableCallback mock_callback_;
59 std::unique_ptr<
60 NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription>
61 snippets_fetcher_subscription_;
62
63 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest);
64 };
65
66 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) {
67 // The lack of registered baked in responses would cause any fetch to fail.
68 base::RunLoop().RunUntilIdle();
69 }
70
71 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfully) {
72 const std::string json_str = "{ \"recos\": [] }";
73 fake_url_fetcher_factory().SetFakeResponse(
74 GURL(kTestContentSnippetsServerUrl),
75 /*data=*/json_str, net::HTTP_OK, net::URLRequestStatus::SUCCESS);
76 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str,
77 /*status_message=*/std::string()))
78 .Times(1);
79 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
80 /*count=*/1);
81 base::RunLoop().RunUntilIdle();
82 }
83
84 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) {
85 fake_url_fetcher_factory().SetFakeResponse(
86 GURL(kTestContentSnippetsServerUrl),
87 /*data=*/std::string(), net::HTTP_NOT_FOUND,
88 net::URLRequestStatus::FAILED);
89 EXPECT_CALL(mock_callback(),
90 Run(/*snippets_json=*/std::string(),
91 /*status_message=*/"URLRequestStatus error -2"))
92 .Times(1);
93 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
94 /*count=*/1);
95 base::RunLoop().RunUntilIdle();
96 }
97
98 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) {
99 fake_url_fetcher_factory().SetFakeResponse(
100 GURL(kTestContentSnippetsServerUrl),
101 /*data=*/std::string(), net::HTTP_NOT_FOUND,
102 net::URLRequestStatus::SUCCESS);
103 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/std::string(),
104 /*status_message=*/"HTTP error 404"))
105 .Times(1);
106 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
107 /*count=*/1);
108 base::RunLoop().RunUntilIdle();
109 }
110
111 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) {
112 const std::string json_str = "{ \"recos\": [] }";
113 fake_url_fetcher_factory().SetFakeResponse(
114 GURL(kTestContentSnippetsServerUrl),
115 /*data=*/json_str, net::HTTP_OK, net::URLRequestStatus::SUCCESS);
116 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str,
117 /*status_message=*/std::string()))
118 .Times(1);
119 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
120 /*count=*/1);
121 // Second call to FetchSnippets() overrides/cancels the previous. Callback is
122 // expected to be called once.
123 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
124 /*count=*/1);
125 base::RunLoop().RunUntilIdle();
126 }
127
128 } // namespace
129 } // 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