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

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: Really sync to tot 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
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");
21 std::string kURLResponseData("EncodedImageData");
Bernhard Bauer 2016/06/20 13:36:16 These will create static initializers. What you wa
markusheintz_ 2016/06/20 14:30:17 Done.
22
23 } // namespace
24
25 class ImageDataFetcherTest : public testing::Test {
26 public:
27 ImageDataFetcherTest() : num_callbacks_run_(0) {}
28 ~ImageDataFetcherTest() override {}
29
30 // testing::Test overrides:
31 void SetUp() override {
32 test_request_context_getter_ =
33 new net::TestURLRequestContextGetter(message_loop_.task_runner());
34
35 fetcher_factory_.reset(new net::TestURLFetcherFactory());
Bernhard Bauer 2016/06/20 13:36:16 You could make this a direct member of the fixture
markusheintz_ 2016/06/20 14:30:17 Done.
36
37 num_callbacks_run_ = 0;
Bernhard Bauer 2016/06/20 13:36:15 You don't need this -- you get a fresh test fixtur
markusheintz_ 2016/06/20 14:30:17 Removed
38 }
39
40 // Callback that is called by the FetchImageData test.
41 void OnImageDataFetched(const std::string& image_data) {
42 EXPECT_EQ(image_data, kURLResponseData);
Bernhard Bauer 2016/06/20 13:36:15 The expected value should go first (for nicer erro
markusheintz_ 2016/06/20 14:30:17 Done.
43 }
44
45 // Callback that is called by the FetchImageData_FailedRequest test.
46 void OnImageDataFetchedFailedRequest(const std::string& image_data) {
47 EXPECT_EQ(image_data, std::string(""));
Bernhard Bauer 2016/06/20 13:36:16 The empty string literal in the constructor is unn
markusheintz_ 2016/06/20 14:30:17 Done.
48 }
49
50 // Callback that is called by the FetchImageData_MultipleRequests test.
51 void OnImageDataFetchedMultipleRequests(const std::string& image_data) {
52 ++num_callbacks_run_;
Bernhard Bauer 2016/06/20 13:36:16 Aren't you going to verify that data that comes ba
markusheintz_ 2016/06/20 14:30:17 That's what the other two tests are for. Here I
53 }
54
55 protected:
56 base::MessageLoop message_loop_;
57
58 scoped_refptr<net::URLRequestContextGetter> test_request_context_getter_;
59
60 std::unique_ptr<net::TestURLFetcherFactory> fetcher_factory_;
61
62 // The number of times the OnImageDataFetchedMultipleRequests callback was
63 // called during a testrun. This counter is reset duing the SetUp method.
Bernhard Bauer 2016/06/20 13:36:16 "during the SetUp method" (but really, the comment
markusheintz_ 2016/06/20 14:30:17 removed
64 int num_callbacks_run_;
65
66 private:
67 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcherTest);
68 };
69
70 TEST_F(ImageDataFetcherTest, FetchImageData) {
71 image_fetcher::ImageDataFetcher image_data_fetcher(
Bernhard Bauer 2016/06/20 13:36:16 You could make this part of the fixture, as you cr
markusheintz_ 2016/06/20 14:30:17 Done.
Marc Treib 2016/06/20 14:47:29 You could put this whole file into the image_fetch
markusheintz_ 2016/06/20 15:06:52 Done.
72 test_request_context_getter_.get());
73 image_fetcher::ImageDataFetcher::ImageDataFetcherCallback callback =
74 base::Bind(&ImageDataFetcherTest::OnImageDataFetched,
75 base::Unretained(this));
76
77 image_data_fetcher.FetchImageData(kImageURL, callback);
78
79 // Get and configure the TestURLFetcher.
80 net::TestURLFetcher* test_url_fetcher = fetcher_factory_->GetFetcherByID(0);
81 ASSERT_NE(test_url_fetcher, nullptr);
Bernhard Bauer 2016/06/20 13:36:16 Expected value first.
markusheintz_ 2016/06/20 14:30:17 Done. Here and below
82 test_url_fetcher->set_status(
83 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK));
84 test_url_fetcher->SetResponseString(kURLResponseData);
85
86 // Call the URLFetcher delegate to continue the test.
Bernhard Bauer 2016/06/20 13:36:16 And then what? Don't you want to verify that the f
markusheintz_ 2016/06/20 14:30:17 You mean whether I want to test the TestURLFetcher
Bernhard Bauer 2016/06/20 17:28:38 Yes, but how do you know the callbacks above are a
87 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
88 }
89
90 TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) {
91 image_fetcher::ImageDataFetcher image_data_fetcher(
92 test_request_context_getter_.get());
93 image_fetcher::ImageDataFetcher::ImageDataFetcherCallback callback =
94 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest,
95 base::Unretained(this));
96
97 image_data_fetcher.FetchImageData(kImageURL, callback);
98
99 // Get and configure the TestURLFetcher.
100 net::TestURLFetcher* test_url_fetcher = fetcher_factory_->GetFetcherByID(0);
101 ASSERT_NE(test_url_fetcher, nullptr);
102 test_url_fetcher->set_status(
103 net::URLRequestStatus(net::URLRequestStatus::FAILED,
104 net::ERR_INVALID_URL));
105
106 // Call the URLFetcher delegate to continue the test.
107 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
108 }
109
110 TEST_F(ImageDataFetcherTest, FetchImageData_MultipleRequests) {
111 image_fetcher::ImageDataFetcher image_data_fetcher(
112 test_request_context_getter_.get());
113
114 image_fetcher::ImageDataFetcher::ImageDataFetcherCallback callback1 =
115 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedMultipleRequests,
116 base::Unretained(this));
117 image_fetcher::ImageDataFetcher::ImageDataFetcherCallback callback2 =
118 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedMultipleRequests,
119 base::Unretained(this));
120
121 image_data_fetcher.FetchImageData(kImageURL, callback1);
122 image_data_fetcher.FetchImageData(kImageURL, callback2);
123
124 net::TestURLFetcher* test_url_fetcher = fetcher_factory_->GetFetcherByID(0);
125 ASSERT_NE(test_url_fetcher, nullptr);
126 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
127
128 test_url_fetcher = fetcher_factory_->GetFetcherByID(1);
129 ASSERT_NE(test_url_fetcher, nullptr);
130 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
131
132 EXPECT_EQ(num_callbacks_run_, 2);
133 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698