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

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

Issue 2677993002: Use IOSImageDataFetcherWrapper for favicon (Closed)
Patch Set: Add header Created 3 years, 10 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/image_fetcher/image_data_fetcher.h" 5 #include "components/image_fetcher/image_data_fetcher.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "net/http/http_status_code.h"
13 #include "net/url_request/test_url_fetcher_factory.h" 14 #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_status.h"
15 #include "net/url_request/url_request_test_util.h" 16 #include "net/url_request/url_request_test_util.h"
16 #include "testing/gmock/include/gmock/gmock.h" 17 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 19
19 namespace { 20 namespace {
20 21
21 const char kImageURL[] = "http://www.example.com/image"; 22 const char kImageURL[] = "http://www.example.com/image";
22 const char kURLResponseData[] = "EncodedImageData"; 23 const char kURLResponseData[] = "EncodedImageData";
23 24
24 } // namespace 25 } // namespace
25 26
26 namespace image_fetcher { 27 namespace image_fetcher {
27 28
28 class ImageDataFetcherTest : public testing::Test { 29 class ImageDataFetcherTest : public testing::Test {
29 public: 30 public:
30 ImageDataFetcherTest() 31 ImageDataFetcherTest()
31 : test_request_context_getter_( 32 : test_request_context_getter_(
32 new net::TestURLRequestContextGetter(message_loop_.task_runner())), 33 new net::TestURLRequestContextGetter(message_loop_.task_runner())),
33 image_data_fetcher_(test_request_context_getter_.get()) {} 34 image_data_fetcher_(test_request_context_getter_.get()) {}
34 ~ImageDataFetcherTest() override {} 35 ~ImageDataFetcherTest() override {}
35 36
36 MOCK_METHOD1(OnImageDataFetched, void(const std::string&)); 37 MOCK_METHOD2(OnImageDataFetched, void(const std::string&, int response_code));
37 38
38 MOCK_METHOD1(OnImageDataFetchedFailedRequest, void(const std::string&)); 39 MOCK_METHOD2(OnImageDataFetchedFailedRequest,
40 void(const std::string&, int response_code));
39 41
40 MOCK_METHOD1(OnImageDataFetchedMultipleRequests, void(const std::string&)); 42 MOCK_METHOD2(OnImageDataFetchedMultipleRequests,
43 void(const std::string&, int response_code));
41 44
42 protected: 45 protected:
43 base::MessageLoop message_loop_; 46 base::MessageLoop message_loop_;
44 47
45 scoped_refptr<net::URLRequestContextGetter> test_request_context_getter_; 48 scoped_refptr<net::URLRequestContextGetter> test_request_context_getter_;
46 49
47 ImageDataFetcher image_data_fetcher_; 50 ImageDataFetcher image_data_fetcher_;
48 51
49 net::TestURLFetcherFactory fetcher_factory_; 52 net::TestURLFetcherFactory fetcher_factory_;
50 53
51 private: 54 private:
52 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcherTest); 55 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcherTest);
53 }; 56 };
54 57
55 TEST_F(ImageDataFetcherTest, FetchImageData) { 58 TEST_F(ImageDataFetcherTest, FetchImageData) {
56 image_data_fetcher_.FetchImageData( 59 image_data_fetcher_.FetchImageData(
57 GURL(kImageURL), 60 GURL(kImageURL),
58 base::Bind(&ImageDataFetcherTest::OnImageDataFetched, 61 base::Bind(&ImageDataFetcherTest::OnImageDataFetched,
59 base::Unretained(this))); 62 base::Unretained(this)));
60 EXPECT_CALL(*this, OnImageDataFetched(std::string(kURLResponseData))); 63 EXPECT_CALL(*this,
64 OnImageDataFetched(std::string(kURLResponseData), net::HTTP_OK));
61 65
62 // Get and configure the TestURLFetcher. 66 // Get and configure the TestURLFetcher.
63 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); 67 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
64 ASSERT_NE(nullptr, test_url_fetcher); 68 ASSERT_NE(nullptr, test_url_fetcher);
65 test_url_fetcher->set_status( 69 test_url_fetcher->set_status(
66 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); 70 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK));
67 test_url_fetcher->SetResponseString(kURLResponseData); 71 test_url_fetcher->SetResponseString(kURLResponseData);
72 test_url_fetcher->set_response_code(net::HTTP_OK);
68 73
69 // Call the URLFetcher delegate to continue the test. 74 // Call the URLFetcher delegate to continue the test.
70 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); 75 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
71 } 76 }
72 77
73 TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) { 78 TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) {
74 image_data_fetcher_.FetchImageData( 79 image_data_fetcher_.FetchImageData(
75 GURL(kImageURL), 80 GURL(kImageURL),
76 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest, 81 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest,
77 base::Unretained(this))); 82 base::Unretained(this)));
78 EXPECT_CALL(*this, OnImageDataFetchedFailedRequest(std::string())); 83 EXPECT_CALL(*this,
84 OnImageDataFetchedFailedRequest(
85 std::string(), net::URLFetcher::RESPONSE_CODE_INVALID));
79 86
80 // Get and configure the TestURLFetcher. 87 // Get and configure the TestURLFetcher.
81 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); 88 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
82 ASSERT_NE(nullptr, test_url_fetcher); 89 ASSERT_NE(nullptr, test_url_fetcher);
83 test_url_fetcher->set_status( 90 test_url_fetcher->set_status(
84 net::URLRequestStatus(net::URLRequestStatus::FAILED, 91 net::URLRequestStatus(net::URLRequestStatus::FAILED,
85 net::ERR_INVALID_URL)); 92 net::ERR_INVALID_URL));
86 93
87 // Call the URLFetcher delegate to continue the test. 94 // Call the URLFetcher delegate to continue the test.
88 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); 95 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
89 } 96 }
90 97
91 TEST_F(ImageDataFetcherTest, FetchImageData_MultipleRequests) { 98 TEST_F(ImageDataFetcherTest, FetchImageData_MultipleRequests) {
92 ImageDataFetcher::ImageDataFetcherCallback callback = 99 ImageDataFetcher::ImageDataFetcherCallback callback =
93 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedMultipleRequests, 100 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedMultipleRequests,
94 base::Unretained(this)); 101 base::Unretained(this));
95 EXPECT_CALL(*this, OnImageDataFetchedMultipleRequests(testing::_)).Times(2); 102 EXPECT_CALL(*this, OnImageDataFetchedMultipleRequests(testing::_, testing::_))
103 .Times(2);
96 104
97 image_data_fetcher_.FetchImageData(GURL(kImageURL), callback); 105 image_data_fetcher_.FetchImageData(GURL(kImageURL), callback);
98 image_data_fetcher_.FetchImageData(GURL(kImageURL), callback); 106 image_data_fetcher_.FetchImageData(GURL(kImageURL), callback);
99 107
100 // Multiple calls to FetchImageData for the same URL will result in 108 // Multiple calls to FetchImageData for the same URL will result in
101 // multiple URLFetchers being created. 109 // multiple URLFetchers being created.
102 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); 110 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
103 ASSERT_NE(nullptr, test_url_fetcher); 111 ASSERT_NE(nullptr, test_url_fetcher);
104 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); 112 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
105 113
106 test_url_fetcher = fetcher_factory_.GetFetcherByID(1); 114 test_url_fetcher = fetcher_factory_.GetFetcherByID(1);
107 ASSERT_NE(nullptr, test_url_fetcher); 115 ASSERT_NE(nullptr, test_url_fetcher);
108 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); 116 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
109 } 117 }
110 118
111 } // namespace image_fetcher 119 } // namespace image_fetcher
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698