Chromium Code Reviews| Index: chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher_unittest.cc |
| diff --git a/chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher_unittest.cc b/chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..db7b5a6c4de4eba5176471856d276edfe39e06fc |
| --- /dev/null |
| +++ b/chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher_unittest.cc |
| @@ -0,0 +1,213 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/message_loop.h" |
| +#include "chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/test/test_browser_thread.h" |
| +#include "net/base/host_port_pair.h" |
| +#include "net/url_request/url_fetcher.h" |
| +#include "net/url_request/url_request_status.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/skia/include/core/SKBitmap.h" |
| + |
| +namespace { |
| +// FF0000FF is 100% alpha and max green.(A, R, B, G) |
| +uint32_t kMaxGreen = 0xFF0000FF; |
| + |
| +} // namespace |
| + |
| +namespace net { |
| + class HttpRequestHeaders; |
| + class HttpResponseHeaders; |
| + class URLRequestContextGetter; |
| +} |
| + |
| +namespace notifier { |
| + |
| + |
| +// TODO(reviewers): Should this stub class and the identical one in the browser |
| +// test share an implementation file? If so, is a .h file OK for stubs? |
| +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.
|
| + public: |
| + typedef std::vector<std::string> ResponseCookies; |
| + StubURLFetcher() : fetch_started_(false) {} |
| + |
| + virtual ~StubURLFetcher() {} |
| + static URLFetcher* Create(const GURL& url, |
| + URLFetcher::RequestType request_type, |
| + net::URLFetcherDelegate* d) { return NULL; } |
| + static URLFetcher* Create(int id, |
| + const GURL& url, |
| + URLFetcher::RequestType request_type, |
| + net::URLFetcherDelegate* d) { return NULL; } |
| + static void CancelAll() {} |
| + static void SetEnableInterceptionForTests(bool enabled) {} |
| + static void SetIgnoreCertificateRequests(bool ignored) {} |
| + void SetUploadData(const std::string& upload_content_type, |
| + const std::string& upload_content) {} |
| + void SetUploadFilePath( |
| + const std::string& upload_content_type, |
| + const base::FilePath& file_path, |
| + uint64 range_offset, |
| + uint64 range_length, |
| + scoped_refptr<base::TaskRunner> file_task_runner) {} |
| + void SetChunkedUpload(const std::string& upload_content_type) {} |
| + void AppendChunkToUpload(const std::string& data, |
| + bool is_last_chunk) {} |
| + void SetLoadFlags(int load_flags) {} |
| + int GetLoadFlags() const { return 0; } |
| + void SetReferrer(const std::string& referrer) {} |
| + void SetExtraRequestHeaders( |
| + const std::string& extra_request_headers) {} |
| + void AddExtraRequestHeader(const std::string& header_line) {} |
| + void GetExtraRequestHeaders(net::HttpRequestHeaders* headers) const {} |
| + void SetRequestContext( |
| + net::URLRequestContextGetter* request_context_getter) {} |
| + void SetFirstPartyForCookies( |
| + const GURL& first_party_for_cookies) {} |
| + void SetURLRequestUserData( |
| + const void* key, |
| + const CreateDataCallback& create_data_callback) {} |
| + void SetStopOnRedirect(bool stop_on_redirect) {} |
| + void SetAutomaticallyRetryOn5xx(bool retry) {} |
| + void SetMaxRetriesOn5xx(int max_retries) {} |
| + int GetMaxRetriesOn5xx() const { return 0; } |
| + base::TimeDelta GetBackoffDelay() const { return base::TimeDelta(); } |
| + void SetAutomaticallyRetryOnNetworkChanges(int max_retries) {} |
| + void SaveResponseToFileAtPath( |
| + const base::FilePath& file_path, |
| + scoped_refptr<base::TaskRunner> file_task_runner) {} |
| + void SaveResponseToTemporaryFile( |
| + scoped_refptr<base::TaskRunner> file_task_runner) {} |
| + net::HttpResponseHeaders* GetResponseHeaders() const { return NULL; } |
| + net::HostPortPair GetSocketAddress() const { return net::HostPortPair(); } |
| + bool WasFetchedViaProxy() const { return true; } |
| + void Start() { |
| + fetch_started_ = true; |
| + } |
| + const GURL& GetOriginalURL() const { return gurl_; } |
| + const GURL& GetURL() const { return gurl_; } |
| + const net::URLRequestStatus& GetStatus() const { |
| + return request_status; |
| + } |
| + int GetResponseCode() const { return 0; } |
| + const net::ResponseCookies& GetCookies() const { |
| + return response_cookies_; |
| + } |
| + bool FileErrorOccurred(int* out_error_code) const { return false; } |
| + void ReceivedContentWasMalformed() {} |
| + bool GetResponseAsString(std::string* out_response_string) const { |
| + return true; |
| + } |
| + bool GetResponseAsFilePath( |
| + bool take_ownership, |
| + base::FilePath* out_response_path) const { return true; } |
| + bool fetch_started() { |
| + return fetch_started_; |
| + } |
| + |
| + private: |
| + GURL gurl_; |
| + net::ResponseCookies response_cookies_; |
| + net::URLRequestStatus request_status; |
| + bool fetch_started_; |
| + |
| +}; |
| + |
| +class NotificationBitmapFetcherTest : public testing::Test { |
| + public: |
| + NotificationBitmapFetcherTest() |
| + : ui_thread_(content::BrowserThread::UI, &message_loop_) {} |
| + |
| + private: |
| + MessageLoopForIO message_loop_; |
| + content::TestBrowserThread ui_thread_; |
| +}; |
| + |
| +TEST_F(NotificationBitmapFetcherTest, ImageReadyTest) { |
| + GURL url("http://localhost"); |
| + scoped_ptr<net::URLFetcher> url_fetcher(new StubURLFetcher()); |
| + scoped_refptr<base::MessageLoopProxy> task_runner = |
| + content::BrowserThread::GetMessageLoopProxyForThread( |
| + content::BrowserThread::UI); |
| + |
| + scoped_refptr<NotificationBitmapFetcher> fetcher = |
| + new NotificationBitmapFetcher(url, url_fetcher, task_runner); |
| + |
| + EXPECT_EQ(false, fetcher->image_ready()); |
| + fetcher->image_ready_ = true; |
| + EXPECT_EQ(true, fetcher->image_ready()); |
| +} |
| + |
| +TEST_F(NotificationBitmapFetcherTest, StartFetchTest) { |
| + GURL url("http://localhost"); |
| + StubURLFetcher* stub_url_fetcher = new StubURLFetcher(); |
| + scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); |
| + scoped_refptr<base::MessageLoopProxy> task_runner = |
| + content::BrowserThread::GetMessageLoopProxyForThread( |
| + content::BrowserThread::UI); |
| + |
| + scoped_refptr<NotificationBitmapFetcher> fetcher = |
| + new NotificationBitmapFetcher(url, url_fetcher, task_runner); |
| + fetcher->StartImageFetch(); |
| + |
| + // The Start method should have been called. |
| + EXPECT_EQ(true, stub_url_fetcher->fetch_started()); |
| +} |
| + |
| +TEST_F(NotificationBitmapFetcherTest, HandleImageDecodedTest) { |
| + GURL url("http://localhost"); |
| + StubURLFetcher* stub_url_fetcher = new StubURLFetcher(); |
| + scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); |
| + scoped_refptr<base::MessageLoopProxy> task_runner = |
| + content::BrowserThread::GetMessageLoopProxyForThread( |
| + content::BrowserThread::UI); |
| + scoped_ptr<SkBitmap> image(new SkBitmap()); |
| + |
| + // Put a real bitmap into "image". 2x2 bitmap of green 16 bit pixels. |
| + image->setConfig(SkBitmap::kRGB_565_Config, 2, 2); |
| + image->allocPixels(); |
| + SkColor c = kMaxGreen; |
| + image->eraseColor(c); |
| + // Test that the image is stored and ready. Pixel [0,0] should be green. |
| + EXPECT_EQ(8, image->getSize()); |
| + EXPECT_EQ(kMaxGreen, image->getColor(0, 0)); |
| + |
| + scoped_refptr<NotificationBitmapFetcher> fetcher = |
| + new NotificationBitmapFetcher(url, url_fetcher, task_runner); |
| + EXPECT_EQ(false, fetcher->image_ready()); |
| + |
| + fetcher->HandleImageDecoded(image.Pass()); |
| + |
| + // Ensure image is marked as succeeded. |
| + EXPECT_EQ(false, fetcher->image_failed()); |
| + EXPECT_EQ(true, fetcher->image_ready()); |
| + // Test that the image is stored and ready. Pixel [0,0] should be green. |
| + EXPECT_EQ(8, fetcher->bitmap()->getSize()); |
| + EXPECT_EQ(2, fetcher->bitmap()->width()); |
| + EXPECT_EQ(2, fetcher->bitmap()->height()); |
| + EXPECT_TRUE(fetcher->bitmap()->getPixels() != NULL); |
| + EXPECT_EQ(kMaxGreen, fetcher->bitmap()->getColor(0, 0)); |
| +} |
| + |
| +TEST_F(NotificationBitmapFetcherTest, HandleImageFailedTest) { |
| + GURL url("http://localhost"); |
| + StubURLFetcher* stub_url_fetcher = new StubURLFetcher(); |
| + scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); |
| + scoped_refptr<base::MessageLoopProxy> task_runner = |
| + content::BrowserThread::GetMessageLoopProxyForThread( |
| + content::BrowserThread::UI); |
| + |
| + scoped_refptr<NotificationBitmapFetcher> fetcher = |
| + new NotificationBitmapFetcher(url, url_fetcher, task_runner); |
| + EXPECT_EQ(false, fetcher->image_failed_); |
| + |
| + fetcher->HandleImageFailed(); |
| + |
| + EXPECT_EQ(true, fetcher->image_failed_); |
| + EXPECT_EQ(false, fetcher->image_ready()); |
| +} |
| + |
| +} // namespace notifier |