1 // Copyright 2011 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.
5 #import "ios/chrome/browser/net/image_fetcher.h"
7 #import <UIKit/UIKit.h>
9 #include "base/ios/ios_util.h"
10 #include "base/mac/scoped_block.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/run_loop.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "net/http/http_response_headers.h"
15 #include "net/url_request/test_url_fetcher_factory.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "testing/platform_test.h"
22 static unsigned char kJPGImage[] = {
23 255,216,255,224,0,16,74,70,73,70,0,1,1,1,0,72,0,72,0,0,255,254,0,19,67,
24 114,101,97,116,101,100,32,119,105,116,104,32,71,73,77,80,255,219,0,67,
25 0,5,3,4,4,4,3,5,4,4,4,5,5,5,6,7,12,8,7,7,7,7,15,11,11,9,12,17,15,18,18,
26 17,15,17,17,19,22,28,23,19,20,26,21,17,17,24,33,24,26,29,29,31,31,31,
27 19,23,34,36,34,30,36,28,30,31,30,255,219,0,67,1,5,5,5,7,6,7,14,8,8,14,
28 30,20,17,20,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
29 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
30 30,30,30,30,30,30,30,30,255,192,0,17,8,0,1,0,1,3,1,34,0,2,17,1,3,17,1,
31 255,196,0,21,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,255,196,0,20,16,1,
32 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,196,0,20,1,1,0,0,0,0,0,0,0,0,0,0,
33 0,0,0,0,0,0,255,196,0,20,17,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,218,
34 0,12,3,1,0,2,17,3,17,0,63,0,178,192,7,255,217
37 static unsigned char kPNGImage[] = {
38 137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,1,0,0,0,1,1,0,0,0,
39 0,55,110,249,36,0,0,0,2,98,75,71,68,0,1,221,138,19,164,0,0,0,9,112,72,
40 89,115,0,0,11,18,0,0,11,18,1,210,221,126,252,0,0,0,9,118,112,65,103,0,
41 0,0,1,0,0,0,1,0,199,149,95,237,0,0,0,10,73,68,65,84,8,215,99,104,0,0,
42 0,130,0,129,221,67,106,244,0,0,0,25,116,69,88,116,99,111,109,109,101,
43 110,116,0,67,114,101,97,116,101,100,32,119,105,116,104,32,71,73,77,80,
44 231,175,64,203,0,0,0,37,116,69,88,116,100,97,116,101,58,99,114,101,97,
45 116,101,0,50,48,49,49,45,48,54,45,50,50,84,49,54,58,49,54,58,52,54,43,
46 48,50,58,48,48,31,248,231,223,0,0,0,37,116,69,88,116,100,97,116,101,58,
47 109,111,100,105,102,121,0,50,48,49,49,45,48,54,45,50,50,84,49,54,58,49,
48 54,58,52,54,43,48,50,58,48,48,110,165,95,99,0,0,0,17,116,69,88,116,106,
49 112,101,103,58,99,111,108,111,114,115,112,97,99,101,0,50,44,117,85,159,
50 0,0,0,32,116,69,88,116,106,112,101,103,58,115,97,109,112,108,105,110,
51 103,45,102,97,99,116,111,114,0,50,120,50,44,49,120,49,44,49,120,49,73,
52 250,166,180,0,0,0,0,73,69,78,68,174,66,96,130
55 static unsigned char kWEBPImage[] = {
56 82,73,70,70,74,0,0,0,87,69,66,80,86,80,56,88,10,0,0,0,16,0,0,0,0,0,0,0,0,0,
57 65,76,80,72,12,0,0,0,1,7,16,17,253,15,68,68,255,3,0,0,86,80,56,32,24,0,0,0,
58 48,1,0,157,1,42,1,0,1,0,3,0,52,37,164,0,3,112,0,254,251,253,80,0
61 static const char kTestUrl[] = "http://www.img.com";
63 static const char kWEBPHeaderResponse[] =
64 "HTTP/1.1 200 OK\0Content-type: image/webp\0\0";
68 // TODO(ios): remove the static_cast<UIImage*>(nil) once all the bots have
69 // Xcode 6.0 or later installed, http://crbug.com/440857
71 class ImageFetcherTest : public PlatformTest {
74 : pool_(new base::SequencedWorkerPool(1, "TestPool")),
75 image_fetcher_(new image_fetcher::ImageFetcher(pool_)),
79 [^(const GURL& original_url, int http_response_code, NSData* data) {
80 result_ = [UIImage imageWithData:data];
83 image_fetcher_->SetRequestContextGetter(
84 new net::TestURLRequestContextGetter(
85 base::ThreadTaskRunnerHandle::Get()));
88 ~ImageFetcherTest() override { pool_->Shutdown(); }
90 net::TestURLFetcher* SetupFetcher() {
91 image_fetcher_->StartDownload(GURL(kTestUrl), callback_);
92 EXPECT_EQ(static_cast<UIImage*>(nil), result_);
93 EXPECT_EQ(false, called_);
94 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
96 DCHECK(fetcher->delegate());
100 base::MessageLoop loop_;
101 base::mac::ScopedBlock<image_fetcher::ImageFetchedCallback> callback_;
102 net::TestURLFetcherFactory factory_;
103 scoped_refptr<base::SequencedWorkerPool> pool_;
104 scoped_ptr<image_fetcher::ImageFetcher> image_fetcher_;
109 TEST_F(ImageFetcherTest, TestError) {
110 net::TestURLFetcher* fetcher = SetupFetcher();
111 fetcher->set_response_code(404);
112 fetcher->delegate()->OnURLFetchComplete(fetcher);
113 EXPECT_EQ(static_cast<UIImage*>(nil), result_);
114 EXPECT_TRUE(called_);
117 TEST_F(ImageFetcherTest, TestJpg) {
118 net::TestURLFetcher* fetcher = SetupFetcher();
119 fetcher->set_response_code(200);
120 fetcher->SetResponseString(std::string((char*)kJPGImage, sizeof(kJPGImage)));
121 fetcher->delegate()->OnURLFetchComplete(fetcher);
122 EXPECT_NE(static_cast<UIImage*>(nil), result_);
123 EXPECT_TRUE(called_);
126 TEST_F(ImageFetcherTest, TestPng) {
127 net::TestURLFetcher* fetcher = SetupFetcher();
128 fetcher->set_response_code(200);
129 fetcher->SetResponseString(std::string((char*)kPNGImage, sizeof(kPNGImage)));
130 fetcher->delegate()->OnURLFetchComplete(fetcher);
131 EXPECT_NE(static_cast<UIImage*>(nil), result_);
132 EXPECT_TRUE(called_);
135 TEST_F(ImageFetcherTest, TestGoodWebP) {
136 // TODO(droger): This test fails on iOS 9 x64 devices. http://crbug.com/523235
137 #if defined(OS_IOS) && defined(ARCH_CPU_ARM64) && !TARGET_IPHONE_SIMULATOR
138 if (base::ios::IsRunningOnIOS9OrLater())
141 net::TestURLFetcher* fetcher = SetupFetcher();
142 fetcher->set_response_code(200);
143 fetcher->SetResponseString(
144 std::string((char*)kWEBPImage, sizeof(kWEBPImage)));
145 scoped_refptr<net::HttpResponseHeaders> headers(new net::HttpResponseHeaders(
146 std::string(kWEBPHeaderResponse, arraysize(kWEBPHeaderResponse))));
147 fetcher->set_response_headers(headers);
148 fetcher->delegate()->OnURLFetchComplete(fetcher);
149 pool_->FlushForTesting();
150 base::RunLoop().RunUntilIdle();
151 EXPECT_NE(static_cast<UIImage*>(nil), result_);
152 EXPECT_TRUE(called_);
155 TEST_F(ImageFetcherTest, TestBadWebP) {
156 net::TestURLFetcher* fetcher = SetupFetcher();
157 fetcher->set_response_code(200);
158 fetcher->SetResponseString("This is not a valid WebP image");
159 scoped_refptr<net::HttpResponseHeaders> headers(new net::HttpResponseHeaders(
160 std::string(kWEBPHeaderResponse, arraysize(kWEBPHeaderResponse))));
161 fetcher->set_response_headers(headers);
162 fetcher->delegate()->OnURLFetchComplete(fetcher);
163 pool_->FlushForTesting();
164 base::RunLoop().RunUntilIdle();
165 EXPECT_EQ(static_cast<UIImage*>(nil), result_);
166 EXPECT_TRUE(called_);
169 TEST_F(ImageFetcherTest, DeleteDuringWebPDecoding) {
170 net::TestURLFetcher* fetcher = SetupFetcher();
171 fetcher->set_response_code(200);
172 fetcher->SetResponseString(
173 std::string((char*)kWEBPImage, sizeof(kWEBPImage)));
174 scoped_refptr<net::HttpResponseHeaders> headers(new net::HttpResponseHeaders(
175 std::string(kWEBPHeaderResponse, arraysize(kWEBPHeaderResponse))));
176 fetcher->set_response_headers(headers);
177 fetcher->delegate()->OnURLFetchComplete(fetcher);
178 // Delete the image fetcher, and check that the callback is not called.
179 image_fetcher_.reset();
180 pool_->FlushForTesting();
181 base::RunLoop().RunUntilIdle();
182 EXPECT_EQ(static_cast<UIImage*>(nil), result_);
183 EXPECT_FALSE(called_);
186 TEST_F(ImageFetcherTest, TestCallbacksNotCalledDuringDeletion) {
187 image_fetcher_->StartDownload(GURL(kTestUrl), callback_);
188 image_fetcher_.reset();
189 EXPECT_FALSE(called_);