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

Side by Side Diff: components/image_fetcher/image_data_fetcher_unittest.cc

Issue 2074093002: Add a unittest for image_data_fetcher (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments treib@ Created 4 years, 6 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/image_fetcher/image_data_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/image_fetcher/image_data_fetcher.h"
6
7 #include <memory>
8
9 #include "base/callback.h"
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/message_loop/message_loop.h"
13 #include "net/url_request/test_url_fetcher_factory.h"
14 #include "net/url_request/url_request_status.h"
15 #include "net/url_request/url_request_test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace {
19
20 const GURL kImageURL("http://www.example.com/image1");
Bernhard Bauer 2016/06/20 17:28:38 This will still create a static initializer. The o
markusheintz_ 2016/06/21 15:56:08 sry forgot. Done
21 const char kURLResponseData[] = "EncodedImageData";
22
23 } // namespace
24
25 namespace image_fetcher {
26
27 class ImageDataFetcherTest : public testing::Test {
28 public:
29 ImageDataFetcherTest()
30 : message_loop_(),
31 test_request_context_getter_(
32 new net::TestURLRequestContextGetter(message_loop_.task_runner())),
33 image_data_fetcher_(test_request_context_getter_.get()),
34 num_callbacks_run_(0) {}
35 ~ImageDataFetcherTest() override {}
36
37 // Callback that is called by the FetchImageData test.
38 void OnImageDataFetched(const std::string& image_data) {
39 EXPECT_EQ(std::string(kURLResponseData), image_data);
40 }
41
42 // Callback that is called by the FetchImageData_FailedRequest test.
43 void OnImageDataFetchedFailedRequest(const std::string& image_data) {
44 EXPECT_EQ(std::string(), image_data);
45 }
46
47 // Callback that is called by the FetchImageData_MultipleRequests test.
48 void OnImageDataFetchedMultipleRequests(const std::string& image_data) {
49 ++num_callbacks_run_;
50 }
51
52 protected:
53 base::MessageLoop message_loop_;
54
55 scoped_refptr<net::URLRequestContextGetter> test_request_context_getter_;
56
57 ImageDataFetcher image_data_fetcher_;
58
59 net::TestURLFetcherFactory fetcher_factory_;
60
61 // The number of times the OnImageDataFetchedMultipleRequests callback was
62 // called during a test run.
63 int num_callbacks_run_;
64
65 private:
66 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcherTest);
67 };
68
69 TEST_F(ImageDataFetcherTest, FetchImageData) {
70 image_data_fetcher_.FetchImageData(
71 kImageURL,
72 base::Bind(&ImageDataFetcherTest::OnImageDataFetched,
73 base::Unretained(this)a));
Bernhard Bauer 2016/06/20 17:28:38 typo?
markusheintz_ 2016/06/21 15:56:08 Done.
74
75 // Get and configure the TestURLFetcher.
76 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
77 ASSERT_NE(nullptr, test_url_fetcher);
78 test_url_fetcher->set_status(
79 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK));
80 test_url_fetcher->SetResponseString(std::string(kURLResponseData));
Bernhard Bauer 2016/06/20 17:28:38 std::string has an implicit constructor that takes
markusheintz_ 2016/06/21 15:56:08 Done.
81
82 // Call the URLFetcher delegate to continue the test.
83 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
84 }
85
86 TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) {
87 image_data_fetcher_.FetchImageData(
88 kImageURL,
89 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest,
90 base::Unretained(this)));
91
92 // Get and configure the TestURLFetcher.
93 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
94 ASSERT_NE(nullptr, test_url_fetcher);
95 test_url_fetcher->set_status(
96 net::URLRequestStatus(net::URLRequestStatus::FAILED,
97 net::ERR_INVALID_URL));
98
99 // Call the URLFetcher delegate to continue the test.
100 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
101 }
102
103 TEST_F(ImageDataFetcherTest, FetchImageData_MultipleRequests) {
104 ImageDataFetcher::ImageDataFetcherCallback callback1 =
105 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedMultipleRequests,
106 base::Unretained(this));
107 ImageDataFetcher::ImageDataFetcherCallback callback2 =
108 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedMultipleRequests,
109 base::Unretained(this));
110
111 image_data_fetcher_.FetchImageData(kImageURL, callback1);
112 image_data_fetcher_.FetchImageData(kImageURL, callback2);
113
114 // Multiple calls to the FetchImageData for the same URL will result in
Bernhard Bauer 2016/06/20 17:28:38 No "the" before FetchImageData
markusheintz_ 2016/06/21 15:56:08 Done.
115 // multiple URLFetcher beeing created.
Marc Treib 2016/06/20 15:21:42 nit: being
Bernhard Bauer 2016/06/20 17:28:38 URLFetchers
markusheintz_ 2016/06/21 15:56:08 Done.
markusheintz_ 2016/06/21 15:56:08 Done.
116 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
117 ASSERT_NE(nullptr, test_url_fetcher);
118 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
119
120 test_url_fetcher = fetcher_factory_.GetFetcherByID(1);
121 ASSERT_NE(nullptr, test_url_fetcher);
122 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
123
124 EXPECT_EQ(num_callbacks_run_, 2);
125 }
126
127 } // namespace image_fetcher
OLDNEW
« no previous file with comments | « components/image_fetcher/image_data_fetcher.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698