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

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

Issue 1937063002: [NTP Snippets] Add metric for fetch error cases (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@record_article_fetch_errors_0
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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/ntp_snippets/ntp_snippets_fetcher.h" 5 #include "components/ntp_snippets/ntp_snippets_fetcher.h"
6 6
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/test/histogram_tester.h"
9 #include "base/thread_task_runner_handle.h" 10 #include "base/thread_task_runner_handle.h"
10 #include "net/url_request/test_url_fetcher_factory.h" 11 #include "net/url_request/test_url_fetcher_factory.h"
11 #include "net/url_request/url_request_test_util.h" 12 #include "net/url_request/url_request_test_util.h"
12 #include "testing/gmock/include/gmock/gmock.h" 13 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 15
15 namespace ntp_snippets { 16 namespace ntp_snippets {
16 namespace { 17 namespace {
17 18
19 using testing::ElementsAre;
20 using testing::IsEmpty;
18 using testing::IsNull; 21 using testing::IsNull;
19 using testing::NotNull; 22 using testing::NotNull;
20 23
21 const char kTestContentSnippetsServerUrl[] = 24 const char kTestContentSnippetsServerUrl[] =
22 "https://chromereader-pa.googleapis.com/v1/fetch?key=dummytoken"; 25 "https://chromereader-pa.googleapis.com/v1/fetch?key=dummytoken";
23 26
24 class MockSnippetsAvailableCallback { 27 class MockSnippetsAvailableCallback {
25 public: 28 public:
26 MOCK_METHOD2(Run, void(const std::string& snippets_json, 29 MOCK_METHOD2(Run, void(const std::string& snippets_json,
27 const std::string& status_message)); 30 const std::string& status_message));
(...skipping 14 matching lines...) Expand all
42 } 45 }
43 46
44 NTPSnippetsFetcher& snippets_fetcher() { return snippets_fetcher_; } 47 NTPSnippetsFetcher& snippets_fetcher() { return snippets_fetcher_; }
45 48
46 net::FakeURLFetcherFactory& fake_url_fetcher_factory() { 49 net::FakeURLFetcherFactory& fake_url_fetcher_factory() {
47 return fake_url_fetcher_factory_; 50 return fake_url_fetcher_factory_;
48 } 51 }
49 52
50 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; } 53 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; }
51 54
55 base::HistogramTester& histogram_tester() { return histogram_tester_; }
56
52 private: 57 private:
53 // Instantiation of factory automatically sets itself as URLFetcher's factory. 58 // Instantiation of factory automatically sets itself as URLFetcher's factory.
54 net::FakeURLFetcherFactory fake_url_fetcher_factory_; 59 net::FakeURLFetcherFactory fake_url_fetcher_factory_;
55 // Needed to use ThreadTaskRunnerHandle. 60 // Needed to use ThreadTaskRunnerHandle.
56 base::MessageLoop message_loop_; 61 base::MessageLoop message_loop_;
57 NTPSnippetsFetcher snippets_fetcher_; 62 NTPSnippetsFetcher snippets_fetcher_;
58 MockSnippetsAvailableCallback mock_callback_; 63 MockSnippetsAvailableCallback mock_callback_;
59 std::unique_ptr< 64 std::unique_ptr<
60 NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> 65 NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription>
61 snippets_fetcher_subscription_; 66 snippets_fetcher_subscription_;
67 base::HistogramTester histogram_tester_;
62 68
63 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); 69 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest);
64 }; 70 };
65 71
66 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) { 72 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) {
67 // The lack of registered baked in responses would cause any fetch to fail. 73 // The lack of registered baked in responses would cause any fetch to fail.
68 base::RunLoop().RunUntilIdle(); 74 base::RunLoop().RunUntilIdle();
75 EXPECT_THAT(
76 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResponseCode"),
77 IsEmpty());
78 EXPECT_THAT(histogram_tester().GetAllSamples(
79 "NewTabPage.Snippets.FailedRequestErrorCode"),
80 IsEmpty());
69 } 81 }
70 82
71 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfully) { 83 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfully) {
72 const std::string json_str = "{ \"recos\": [] }"; 84 const std::string json_str = "{ \"recos\": [] }";
73 fake_url_fetcher_factory().SetFakeResponse( 85 fake_url_fetcher_factory().SetFakeResponse(
74 GURL(kTestContentSnippetsServerUrl), 86 GURL(kTestContentSnippetsServerUrl),
75 /*data=*/json_str, net::HTTP_OK, net::URLRequestStatus::SUCCESS); 87 /*data=*/json_str, net::HTTP_OK, net::URLRequestStatus::SUCCESS);
76 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str, 88 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str,
77 /*status_message=*/std::string())) 89 /*status_message=*/std::string()))
78 .Times(1); 90 .Times(1);
79 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 91 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
80 /*count=*/1); 92 /*count=*/1);
81 base::RunLoop().RunUntilIdle(); 93 base::RunLoop().RunUntilIdle();
94 EXPECT_THAT(
95 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResponseCode"),
96 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
97 EXPECT_THAT(histogram_tester().GetAllSamples(
98 "NewTabPage.Snippets.FailedRequestErrorCode"),
99 IsEmpty());
82 } 100 }
83 101
84 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { 102 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) {
85 fake_url_fetcher_factory().SetFakeResponse( 103 fake_url_fetcher_factory().SetFakeResponse(
86 GURL(kTestContentSnippetsServerUrl), 104 GURL(kTestContentSnippetsServerUrl),
87 /*data=*/std::string(), net::HTTP_NOT_FOUND, 105 /*data=*/std::string(), net::HTTP_NOT_FOUND,
88 net::URLRequestStatus::FAILED); 106 net::URLRequestStatus::FAILED);
89 EXPECT_CALL(mock_callback(), 107 EXPECT_CALL(mock_callback(),
90 Run(/*snippets_json=*/std::string(), 108 Run(/*snippets_json=*/std::string(),
91 /*status_message=*/"URLRequestStatus error -2")) 109 /*status_message=*/"URLRequestStatus error -2"))
92 .Times(1); 110 .Times(1);
93 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 111 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
94 /*count=*/1); 112 /*count=*/1);
95 base::RunLoop().RunUntilIdle(); 113 base::RunLoop().RunUntilIdle();
114 EXPECT_THAT(
115 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResponseCode"),
116 IsEmpty());
117 EXPECT_THAT(histogram_tester().GetAllSamples(
118 "NewTabPage.Snippets.FailedRequestErrorCode"),
119 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1)));
96 } 120 }
97 121
98 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { 122 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) {
99 fake_url_fetcher_factory().SetFakeResponse( 123 fake_url_fetcher_factory().SetFakeResponse(
100 GURL(kTestContentSnippetsServerUrl), 124 GURL(kTestContentSnippetsServerUrl),
101 /*data=*/std::string(), net::HTTP_NOT_FOUND, 125 /*data=*/std::string(), net::HTTP_NOT_FOUND,
102 net::URLRequestStatus::SUCCESS); 126 net::URLRequestStatus::SUCCESS);
103 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/std::string(), 127 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/std::string(),
104 /*status_message=*/"HTTP error 404")) 128 /*status_message=*/"HTTP error 404"))
105 .Times(1); 129 .Times(1);
106 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 130 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
107 /*count=*/1); 131 /*count=*/1);
108 base::RunLoop().RunUntilIdle(); 132 base::RunLoop().RunUntilIdle();
133 EXPECT_THAT(
134 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResponseCode"),
135 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1)));
136 EXPECT_THAT(histogram_tester().GetAllSamples(
137 "NewTabPage.Snippets.FailedRequestErrorCode"),
138 IsEmpty());
109 } 139 }
110 140
111 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { 141 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) {
112 const std::string json_str = "{ \"recos\": [] }"; 142 const std::string json_str = "{ \"recos\": [] }";
113 fake_url_fetcher_factory().SetFakeResponse( 143 fake_url_fetcher_factory().SetFakeResponse(
114 GURL(kTestContentSnippetsServerUrl), 144 GURL(kTestContentSnippetsServerUrl),
115 /*data=*/json_str, net::HTTP_OK, net::URLRequestStatus::SUCCESS); 145 /*data=*/json_str, net::HTTP_OK, net::URLRequestStatus::SUCCESS);
116 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str, 146 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str,
117 /*status_message=*/std::string())) 147 /*status_message=*/std::string()))
118 .Times(1); 148 .Times(1);
119 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 149 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
120 /*count=*/1); 150 /*count=*/1);
121 // Second call to FetchSnippets() overrides/cancels the previous. Callback is 151 // Second call to FetchSnippets() overrides/cancels the previous. Callback is
122 // expected to be called once. 152 // expected to be called once.
123 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 153 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
124 /*count=*/1); 154 /*count=*/1);
125 base::RunLoop().RunUntilIdle(); 155 base::RunLoop().RunUntilIdle();
156 EXPECT_THAT(
157 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResponseCode"),
158 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
159 EXPECT_THAT(histogram_tester().GetAllSamples(
160 "NewTabPage.Snippets.FailedRequestErrorCode"),
161 IsEmpty());
126 } 162 }
127 163
128 } // namespace 164 } // namespace
129 } // namespace ntp_snippets 165 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698