OLD | NEW |
1 // Copyright 2011 The Chromium Authors. All rights reserved. | 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 | 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 #import "ios/chrome/browser/net/image_fetcher.h" | 5 #import "ios/chrome/browser/net/image_fetcher/image_fetcher.h" |
6 | 6 |
7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
8 | 8 |
9 #include "base/ios/ios_util.h" | 9 #include "base/ios/ios_util.h" |
10 #include "base/mac/scoped_block.h" | 10 #include "base/mac/scoped_block.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
18 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
19 #include "net/url_request/test_url_fetcher_factory.h" | 19 #include "net/url_request/test_url_fetcher_factory.h" |
20 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "testing/platform_test.h" | 22 #include "testing/platform_test.h" |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 static unsigned char kJPGImage[] = { | 26 static unsigned char kJPGImage[] = { |
27 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, | 27 255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 1, 0, |
28 114,101,97,116,101,100,32,119,105,116,104,32,71,73,77,80,255,219,0,67, | 28 72, 0, 72, 0, 0, 255, 254, 0, 19, 67, 114, 101, 97, 116, 101, |
29 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, | 29 100, 32, 119, 105, 116, 104, 32, 71, 73, 77, 80, 255, 219, 0, 67, |
30 17,15,17,17,19,22,28,23,19,20,26,21,17,17,24,33,24,26,29,29,31,31,31, | 30 0, 5, 3, 4, 4, 4, 3, 5, 4, 4, 4, 5, 5, 5, 6, |
31 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, | 31 7, 12, 8, 7, 7, 7, 7, 15, 11, 11, 9, 12, 17, 15, 18, |
32 30,20,17,20,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, | 32 18, 17, 15, 17, 17, 19, 22, 28, 23, 19, 20, 26, 21, 17, 17, |
33 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, | 33 24, 33, 24, 26, 29, 29, 31, 31, 31, 19, 23, 34, 36, 34, 30, |
34 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, | 34 36, 28, 30, 31, 30, 255, 219, 0, 67, 1, 5, 5, 5, 7, 6, |
35 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, | 35 7, 14, 8, 8, 14, 30, 20, 17, 20, 30, 30, 30, 30, 30, 30, |
36 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, | 36 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, |
37 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, | 37 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, |
38 0,12,3,1,0,2,17,3,17,0,63,0,178,192,7,255,217 | 38 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 255, |
39 }; | 39 192, 0, 17, 8, 0, 1, 0, 1, 3, 1, 34, 0, 2, 17, 1, |
| 40 3, 17, 1, 255, 196, 0, 21, 0, 1, 1, 0, 0, 0, 0, 0, |
| 41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 255, 196, 0, 20, |
| 42 16, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 43 0, 0, 0, 255, 196, 0, 20, 1, 1, 0, 0, 0, 0, 0, 0, |
| 44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 196, 0, 20, 17, |
| 45 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 46 0, 0, 255, 218, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, |
| 47 0, 178, 192, 7, 255, 217}; |
40 | 48 |
41 static unsigned char kPNGImage[] = { | 49 static unsigned char kPNGImage[] = { |
42 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, | 50 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, |
43 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, | 51 82, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 55, |
44 89,115,0,0,11,18,0,0,11,18,1,210,221,126,252,0,0,0,9,118,112,65,103,0, | 52 110, 249, 36, 0, 0, 0, 2, 98, 75, 71, 68, 0, 1, 221, 138, |
45 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, | 53 19, 164, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 11, 18, 0, |
46 0,130,0,129,221,67,106,244,0,0,0,25,116,69,88,116,99,111,109,109,101, | 54 0, 11, 18, 1, 210, 221, 126, 252, 0, 0, 0, 9, 118, 112, 65, |
47 110,116,0,67,114,101,97,116,101,100,32,119,105,116,104,32,71,73,77,80, | 55 103, 0, 0, 0, 1, 0, 0, 0, 1, 0, 199, 149, 95, 237, 0, |
48 231,175,64,203,0,0,0,37,116,69,88,116,100,97,116,101,58,99,114,101,97, | 56 0, 0, 10, 73, 68, 65, 84, 8, 215, 99, 104, 0, 0, 0, 130, |
49 116,101,0,50,48,49,49,45,48,54,45,50,50,84,49,54,58,49,54,58,52,54,43, | 57 0, 129, 221, 67, 106, 244, 0, 0, 0, 25, 116, 69, 88, 116, 99, |
50 48,50,58,48,48,31,248,231,223,0,0,0,37,116,69,88,116,100,97,116,101,58, | 58 111, 109, 109, 101, 110, 116, 0, 67, 114, 101, 97, 116, 101, 100, 32, |
51 109,111,100,105,102,121,0,50,48,49,49,45,48,54,45,50,50,84,49,54,58,49, | 59 119, 105, 116, 104, 32, 71, 73, 77, 80, 231, 175, 64, 203, 0, 0, |
52 54,58,52,54,43,48,50,58,48,48,110,165,95,99,0,0,0,17,116,69,88,116,106, | 60 0, 37, 116, 69, 88, 116, 100, 97, 116, 101, 58, 99, 114, 101, 97, |
53 112,101,103,58,99,111,108,111,114,115,112,97,99,101,0,50,44,117,85,159, | 61 116, 101, 0, 50, 48, 49, 49, 45, 48, 54, 45, 50, 50, 84, 49, |
54 0,0,0,32,116,69,88,116,106,112,101,103,58,115,97,109,112,108,105,110, | 62 54, 58, 49, 54, 58, 52, 54, 43, 48, 50, 58, 48, 48, 31, 248, |
55 103,45,102,97,99,116,111,114,0,50,120,50,44,49,120,49,44,49,120,49,73, | 63 231, 223, 0, 0, 0, 37, 116, 69, 88, 116, 100, 97, 116, 101, 58, |
56 250,166,180,0,0,0,0,73,69,78,68,174,66,96,130 | 64 109, 111, 100, 105, 102, 121, 0, 50, 48, 49, 49, 45, 48, 54, 45, |
57 }; | 65 50, 50, 84, 49, 54, 58, 49, 54, 58, 52, 54, 43, 48, 50, 58, |
| 66 48, 48, 110, 165, 95, 99, 0, 0, 0, 17, 116, 69, 88, 116, 106, |
| 67 112, 101, 103, 58, 99, 111, 108, 111, 114, 115, 112, 97, 99, 101, 0, |
| 68 50, 44, 117, 85, 159, 0, 0, 0, 32, 116, 69, 88, 116, 106, 112, |
| 69 101, 103, 58, 115, 97, 109, 112, 108, 105, 110, 103, 45, 102, 97, 99, |
| 70 116, 111, 114, 0, 50, 120, 50, 44, 49, 120, 49, 44, 49, 120, 49, |
| 71 73, 250, 166, 180, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, |
| 72 130}; |
58 | 73 |
59 static unsigned char kWEBPImage[] = { | 74 static unsigned char kWEBPImage[] = { |
60 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, | 75 82, 73, 70, 70, 74, 0, 0, 0, 87, 69, 66, 80, 86, 80, 56, 88, 10, |
61 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, | 76 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 76, 80, 72, |
62 48,1,0,157,1,42,1,0,1,0,3,0,52,37,164,0,3,112,0,254,251,253,80,0 | 77 12, 0, 0, 0, 1, 7, 16, 17, 253, 15, 68, 68, 255, 3, 0, 0, 86, |
63 }; | 78 80, 56, 32, 24, 0, 0, 0, 48, 1, 0, 157, 1, 42, 1, 0, 1, 0, |
| 79 3, 0, 52, 37, 164, 0, 3, 112, 0, 254, 251, 253, 80, 0}; |
64 | 80 |
65 static const char kTestUrl[] = "http://www.img.com"; | 81 static const char kTestUrl[] = "http://www.img.com"; |
66 | 82 |
67 static const char kWEBPHeaderResponse[] = | 83 static const char kWEBPHeaderResponse[] = |
68 "HTTP/1.1 200 OK\0Content-type: image/webp\0\0"; | 84 "HTTP/1.1 200 OK\0Content-type: image/webp\0\0"; |
69 | 85 |
70 } // namespace | 86 } // namespace |
71 | 87 |
72 class ImageFetcherTest : public PlatformTest { | 88 class ImageFetcherTest : public PlatformTest { |
73 protected: | 89 protected: |
74 ImageFetcherTest() | 90 ImageFetcherTest() |
75 : worker_thread_("TestThread"), | 91 : worker_thread_("TestThread"), |
76 callback_([^(const GURL& original_url, | 92 callback_( |
77 int http_response_code, | 93 [^(const GURL& original_url, int http_response_code, NSData* data) { |
78 NSData * data) { | 94 result_ = [UIImage imageWithData:data]; |
79 result_ = [UIImage imageWithData:data]; | 95 called_ = true; |
80 called_ = true; | 96 } copy]) { |
81 } copy]) { | |
82 worker_thread_.Start(); | 97 worker_thread_.Start(); |
83 | 98 |
84 image_fetcher_ = | 99 image_fetcher_ = |
85 base::MakeUnique<ImageFetcher>(worker_thread_.task_runner()); | 100 base::MakeUnique<ImageFetcher>(worker_thread_.task_runner()); |
86 image_fetcher_->SetRequestContextGetter( | 101 image_fetcher_->SetRequestContextGetter( |
87 new net::TestURLRequestContextGetter( | 102 new net::TestURLRequestContextGetter( |
88 base::ThreadTaskRunnerHandle::Get())); | 103 base::ThreadTaskRunnerHandle::Get())); |
89 } | 104 } |
90 | 105 |
91 net::TestURLFetcher* SetupFetcher() { | 106 net::TestURLFetcher* SetupFetcher() { |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 base::RunLoop().RunUntilIdle(); | 204 base::RunLoop().RunUntilIdle(); |
190 EXPECT_EQ(nil, result_); | 205 EXPECT_EQ(nil, result_); |
191 EXPECT_FALSE(called_); | 206 EXPECT_FALSE(called_); |
192 } | 207 } |
193 | 208 |
194 TEST_F(ImageFetcherTest, TestCallbacksNotCalledDuringDeletion) { | 209 TEST_F(ImageFetcherTest, TestCallbacksNotCalledDuringDeletion) { |
195 image_fetcher_->StartDownload(GURL(kTestUrl), callback_); | 210 image_fetcher_->StartDownload(GURL(kTestUrl), callback_); |
196 image_fetcher_.reset(); | 211 image_fetcher_.reset(); |
197 EXPECT_FALSE(called_); | 212 EXPECT_FALSE(called_); |
198 } | 213 } |
199 | |
OLD | NEW |