OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 "base/message_loop.h" | |
6 #include "chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher .h" | |
7 #include "content/public/browser/browser_thread.h" | |
8 #include "content/public/test/test_browser_thread.h" | |
9 #include "net/base/host_port_pair.h" | |
10 #include "net/url_request/url_fetcher.h" | |
11 #include "net/url_request/url_request_status.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 #include "third_party/skia/include/core/SKBitmap.h" | |
14 | |
15 namespace { | |
16 // FF0000FF is 100% alpha and max green.(A, R, B, G) | |
17 uint32_t kMaxGreen = 0xFF0000FF; | |
18 | |
19 } // namespace | |
20 | |
21 namespace net { | |
22 class HttpRequestHeaders; | |
23 class HttpResponseHeaders; | |
24 class URLRequestContextGetter; | |
25 } | |
26 | |
27 namespace notifier { | |
28 | |
29 | |
30 // TODO(reviewers): Should this stub class and the identical one in the browser | |
31 // test share an implementation file? If so, is a .h file OK for stubs? | |
32 class StubURLFetcher : public net::URLFetcher { | |
dcheng
2013/05/22 19:38:35
Is it possible to use TestURLFetcher instead? If i
Pete Williamson
2013/05/23 16:47:29
Done.
| |
33 public: | |
34 typedef std::vector<std::string> ResponseCookies; | |
35 StubURLFetcher() : fetch_started_(false) {} | |
36 | |
37 virtual ~StubURLFetcher() {} | |
38 static URLFetcher* Create(const GURL& url, | |
39 URLFetcher::RequestType request_type, | |
40 net::URLFetcherDelegate* d) { return NULL; } | |
41 static URLFetcher* Create(int id, | |
42 const GURL& url, | |
43 URLFetcher::RequestType request_type, | |
44 net::URLFetcherDelegate* d) { return NULL; } | |
45 static void CancelAll() {} | |
46 static void SetEnableInterceptionForTests(bool enabled) {} | |
47 static void SetIgnoreCertificateRequests(bool ignored) {} | |
48 void SetUploadData(const std::string& upload_content_type, | |
49 const std::string& upload_content) {} | |
50 void SetUploadFilePath( | |
51 const std::string& upload_content_type, | |
52 const base::FilePath& file_path, | |
53 uint64 range_offset, | |
54 uint64 range_length, | |
55 scoped_refptr<base::TaskRunner> file_task_runner) {} | |
56 void SetChunkedUpload(const std::string& upload_content_type) {} | |
57 void AppendChunkToUpload(const std::string& data, | |
58 bool is_last_chunk) {} | |
59 void SetLoadFlags(int load_flags) {} | |
60 int GetLoadFlags() const { return 0; } | |
61 void SetReferrer(const std::string& referrer) {} | |
62 void SetExtraRequestHeaders( | |
63 const std::string& extra_request_headers) {} | |
64 void AddExtraRequestHeader(const std::string& header_line) {} | |
65 void GetExtraRequestHeaders(net::HttpRequestHeaders* headers) const {} | |
66 void SetRequestContext( | |
67 net::URLRequestContextGetter* request_context_getter) {} | |
68 void SetFirstPartyForCookies( | |
69 const GURL& first_party_for_cookies) {} | |
70 void SetURLRequestUserData( | |
71 const void* key, | |
72 const CreateDataCallback& create_data_callback) {} | |
73 void SetStopOnRedirect(bool stop_on_redirect) {} | |
74 void SetAutomaticallyRetryOn5xx(bool retry) {} | |
75 void SetMaxRetriesOn5xx(int max_retries) {} | |
76 int GetMaxRetriesOn5xx() const { return 0; } | |
77 base::TimeDelta GetBackoffDelay() const { return base::TimeDelta(); } | |
78 void SetAutomaticallyRetryOnNetworkChanges(int max_retries) {} | |
79 void SaveResponseToFileAtPath( | |
80 const base::FilePath& file_path, | |
81 scoped_refptr<base::TaskRunner> file_task_runner) {} | |
82 void SaveResponseToTemporaryFile( | |
83 scoped_refptr<base::TaskRunner> file_task_runner) {} | |
84 net::HttpResponseHeaders* GetResponseHeaders() const { return NULL; } | |
85 net::HostPortPair GetSocketAddress() const { return net::HostPortPair(); } | |
86 bool WasFetchedViaProxy() const { return true; } | |
87 void Start() { | |
88 fetch_started_ = true; | |
89 } | |
90 const GURL& GetOriginalURL() const { return gurl_; } | |
91 const GURL& GetURL() const { return gurl_; } | |
92 const net::URLRequestStatus& GetStatus() const { | |
93 return request_status; | |
94 } | |
95 int GetResponseCode() const { return 0; } | |
96 const net::ResponseCookies& GetCookies() const { | |
97 return response_cookies_; | |
98 } | |
99 bool FileErrorOccurred(int* out_error_code) const { return false; } | |
100 void ReceivedContentWasMalformed() {} | |
101 bool GetResponseAsString(std::string* out_response_string) const { | |
102 return true; | |
103 } | |
104 bool GetResponseAsFilePath( | |
105 bool take_ownership, | |
106 base::FilePath* out_response_path) const { return true; } | |
107 bool fetch_started() { | |
108 return fetch_started_; | |
109 } | |
110 | |
111 private: | |
112 GURL gurl_; | |
113 net::ResponseCookies response_cookies_; | |
114 net::URLRequestStatus request_status; | |
115 bool fetch_started_; | |
116 | |
117 }; | |
118 | |
119 class NotificationBitmapFetcherTest : public testing::Test { | |
120 public: | |
121 NotificationBitmapFetcherTest() | |
122 : ui_thread_(content::BrowserThread::UI, &message_loop_) {} | |
123 | |
124 private: | |
125 MessageLoopForIO message_loop_; | |
126 content::TestBrowserThread ui_thread_; | |
127 }; | |
128 | |
129 TEST_F(NotificationBitmapFetcherTest, ImageReadyTest) { | |
130 GURL url("http://localhost"); | |
131 scoped_ptr<net::URLFetcher> url_fetcher(new StubURLFetcher()); | |
132 scoped_refptr<base::MessageLoopProxy> task_runner = | |
133 content::BrowserThread::GetMessageLoopProxyForThread( | |
134 content::BrowserThread::UI); | |
135 | |
136 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
137 new NotificationBitmapFetcher(url, url_fetcher, task_runner); | |
138 | |
139 EXPECT_EQ(false, fetcher->image_ready()); | |
140 fetcher->image_ready_ = true; | |
141 EXPECT_EQ(true, fetcher->image_ready()); | |
142 } | |
143 | |
144 TEST_F(NotificationBitmapFetcherTest, StartFetchTest) { | |
145 GURL url("http://localhost"); | |
146 StubURLFetcher* stub_url_fetcher = new StubURLFetcher(); | |
147 scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); | |
148 scoped_refptr<base::MessageLoopProxy> task_runner = | |
149 content::BrowserThread::GetMessageLoopProxyForThread( | |
150 content::BrowserThread::UI); | |
151 | |
152 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
153 new NotificationBitmapFetcher(url, url_fetcher, task_runner); | |
154 fetcher->StartImageFetch(); | |
155 | |
156 // The Start method should have been called. | |
157 EXPECT_EQ(true, stub_url_fetcher->fetch_started()); | |
158 } | |
159 | |
160 TEST_F(NotificationBitmapFetcherTest, HandleImageDecodedTest) { | |
161 GURL url("http://localhost"); | |
162 StubURLFetcher* stub_url_fetcher = new StubURLFetcher(); | |
163 scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); | |
164 scoped_refptr<base::MessageLoopProxy> task_runner = | |
165 content::BrowserThread::GetMessageLoopProxyForThread( | |
166 content::BrowserThread::UI); | |
167 scoped_ptr<SkBitmap> image(new SkBitmap()); | |
168 | |
169 // Put a real bitmap into "image". 2x2 bitmap of green 16 bit pixels. | |
170 image->setConfig(SkBitmap::kRGB_565_Config, 2, 2); | |
171 image->allocPixels(); | |
172 SkColor c = kMaxGreen; | |
173 image->eraseColor(c); | |
174 // Test that the image is stored and ready. Pixel [0,0] should be green. | |
175 EXPECT_EQ(8, image->getSize()); | |
176 EXPECT_EQ(kMaxGreen, image->getColor(0, 0)); | |
177 | |
178 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
179 new NotificationBitmapFetcher(url, url_fetcher, task_runner); | |
180 EXPECT_EQ(false, fetcher->image_ready()); | |
181 | |
182 fetcher->HandleImageDecoded(image.Pass()); | |
183 | |
184 // Ensure image is marked as succeeded. | |
185 EXPECT_EQ(false, fetcher->image_failed()); | |
186 EXPECT_EQ(true, fetcher->image_ready()); | |
187 // Test that the image is stored and ready. Pixel [0,0] should be green. | |
188 EXPECT_EQ(8, fetcher->bitmap()->getSize()); | |
189 EXPECT_EQ(2, fetcher->bitmap()->width()); | |
190 EXPECT_EQ(2, fetcher->bitmap()->height()); | |
191 EXPECT_TRUE(fetcher->bitmap()->getPixels() != NULL); | |
192 EXPECT_EQ(kMaxGreen, fetcher->bitmap()->getColor(0, 0)); | |
193 } | |
194 | |
195 TEST_F(NotificationBitmapFetcherTest, HandleImageFailedTest) { | |
196 GURL url("http://localhost"); | |
197 StubURLFetcher* stub_url_fetcher = new StubURLFetcher(); | |
198 scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); | |
199 scoped_refptr<base::MessageLoopProxy> task_runner = | |
200 content::BrowserThread::GetMessageLoopProxyForThread( | |
201 content::BrowserThread::UI); | |
202 | |
203 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
204 new NotificationBitmapFetcher(url, url_fetcher, task_runner); | |
205 EXPECT_EQ(false, fetcher->image_failed_); | |
206 | |
207 fetcher->HandleImageFailed(); | |
208 | |
209 EXPECT_EQ(true, fetcher->image_failed_); | |
210 EXPECT_EQ(false, fetcher->image_ready()); | |
211 } | |
212 | |
213 } // namespace notifier | |
OLD | NEW |