Index: chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher_browsertest.cc |
diff --git a/chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher_browsertest.cc b/chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher_browsertest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..de277bdb704a464fa08c6e815aee9a4afb899015 |
--- /dev/null |
+++ b/chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher_browsertest.cc |
@@ -0,0 +1,276 @@ |
+// 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 "chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher.h" |
+ |
+#include "chrome/common/chrome_notification_types.h" |
+#include "chrome/test/base/in_process_browser_test.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/notification_service.h" |
+#include "content/public/test/test_utils.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" |
+#include "ui/gfx/codec/png_codec.h" |
+#include "ui/gfx/size.h" |
+ |
+namespace { |
+// FF0000FF is 100% alpha and max green.(A, R, B, G) |
+uint32_t kMaxGreen = 0xFF0000FF; |
+} // namespace |
+ |
+namespace notifier { |
+ |
+class StubURLFetcher : public net::URLFetcher { |
+ 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 { |
+ *out_response_string = return_string_; |
+ return true; |
+ } |
+ bool GetResponseAsFilePath( |
+ bool take_ownership, |
+ base::FilePath* out_response_path) const { return true; } |
+ // For testing only, not a method of the base class, lets us input mock data. |
+ void SetReturnString(std::string& return_string) { |
+ return_string_ = return_string; |
+ } |
+ bool fetch_started() { |
+ return fetch_started_; |
+ } |
+ |
+ private: |
+ GURL gurl_; |
+ net::ResponseCookies response_cookies_; |
+ net::URLRequestStatus request_status; |
+ std::string return_string_; |
+ bool fetch_started_; |
+ |
+}; |
+ |
+class NotificationBitmapFetcherBrowserTest : public InProcessBrowserTest { |
+ |
+}; |
+ |
+// WARNING: These tests work with --single_process, but not |
+// --single-process. The reason is that the sandbox does not get created |
+// for us by the test process if --single-process is used. |
+ |
+IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, |
+ OnURLFetchCompleteTest) { |
+ 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); |
+ |
+ // Put some realistic looking bitmap data into the url_fetcher. |
+ SkBitmap image; |
+ |
+ // Put a real bitmap into "image". 2x2 bitmap of green 32 bit pixels. |
+ image.setConfig(SkBitmap::kARGB_8888_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(16, image.getSize()); |
+ EXPECT_EQ(kMaxGreen, image.getColor(0, 0)); |
+ |
+ // Get vital stats from the bitmap for width, height, size, etc. |
+ SkAutoLockPixels lock(image); |
+ int width = image.width(); |
+ int height = image.height(); |
+ int row_length = static_cast<int>(image.rowBytes()); |
+ size_t size = row_length * height; |
+ // Actual bitmap data in arrays of RGBAs. |
+ std::vector<unsigned char> data; |
+ data.resize(size); |
+ memcpy(&*data.begin(), image.getAddr(0, 0), size); |
+ |
+ // Encode the bits as a PNG. |
+ std::vector<unsigned char> compressed; |
+ ASSERT_TRUE(gfx::PNGCodec::Encode(&*data.begin(), |
+ gfx::PNGCodec::FORMAT_BGRA, |
+ gfx::Size(width, height), |
+ static_cast<int>(row_length), |
+ true, |
+ std::vector<gfx::PNGCodec::Comment>(), |
+ &compressed)); |
+ |
+ // Copy the bits into the string, and put them into the StubURLFetcher. |
+ std::string image_string; |
+ image_string.resize(compressed.size()); |
+ // TODO(petewil): It might be better to have STL to write this loop for us |
+ // with for_each or a function. Make sure it handles embedded nulls. |
+ for (size_t ii = 0; ii < compressed.size(); ++ii) { |
+ image_string[ii] = compressed[ii]; |
+ } |
+ stub_url_fetcher->SetReturnString(image_string); |
+ |
+ // The fetcher controls the lifetime of url_fetcher, but we keep a |
+ // pointer to it with stub_url_fetcher. |
+ scoped_refptr<NotificationBitmapFetcher> fetcher = |
+ new NotificationBitmapFetcher(url, url_fetcher, task_runner); |
+ |
+ // Set up a signal to wait for the bitmap fetch to be done. |
+ content::WindowedNotificationObserver signal( |
+ chrome::NOTIFICATION_NOTIFY_BITMAP_FETCH_COMPLETE, |
+ content::NotificationService::AllSources()); |
+ |
+ // We expect that the image decoder will get called and return |
+ // an image in a callback to OnImageDecoded(). |
+ fetcher->OnURLFetchComplete(stub_url_fetcher); |
+ |
+ // Wait for the callback, test succeeds if callback looks good. |
+ signal.Wait(); |
+ |
+ ASSERT_FALSE(fetcher->image_failed()); |
+ ASSERT_TRUE(fetcher->image_ready()); |
+ |
+ // Make sure we get back the bitmap we expect. |
+ SkBitmap* found_image = fetcher->bitmap(); |
+ EXPECT_EQ(16, found_image->getSize()); |
+ EXPECT_EQ(kMaxGreen, found_image->getColor(0, 0)); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, |
+ OnDecodeImageFailedTest) { |
+ 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); |
+ |
+ // The fetcher controls the lifetime of url_fetcher, but we keep a |
+ // pointer to it with stub_url_fetcher. |
+ scoped_refptr<NotificationBitmapFetcher> fetcher = |
+ new NotificationBitmapFetcher(url, url_fetcher, task_runner); |
+ |
+ // Set up a signal to wait for the bitmap fetch to be done. |
+ content::WindowedNotificationObserver signal( |
+ chrome::NOTIFICATION_NOTIFY_BITMAP_FETCH_COMPLETE, |
+ content::NotificationService::AllSources()); |
+ |
+ // We expect that the image decoder will get called and return |
+ // in a callback to OnImageDecodFailed(). |
+ fetcher->OnURLFetchComplete(stub_url_fetcher); |
+ |
+ // Wait for the callback, test succeeds if callback looks good. |
+ signal.Wait(); |
+ |
+ ASSERT_TRUE(fetcher->image_failed()); |
+ ASSERT_FALSE(fetcher->image_ready()); |
+ |
+ // Make sure we didn't get any bitmap. |
+ SkBitmap* found_image = fetcher->bitmap(); |
+ EXPECT_EQ(NULL, found_image); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, |
+ OnURLFetchFailureTest) { |
+ 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); |
+ |
+ // We intentionally put no data into the bitmap to simulate a failure. |
+ |
+ // The fetcher controls the lifetime of url_fetcher, but we keep a |
+ // pointer to it with stub_url_fetcher. |
+ scoped_refptr<NotificationBitmapFetcher> fetcher = |
+ new NotificationBitmapFetcher(url, url_fetcher, task_runner); |
+ |
+ // Set up a signal to wait for the bitmap fetch to be done. |
+ content::WindowedNotificationObserver signal( |
+ chrome::NOTIFICATION_NOTIFY_BITMAP_FETCH_COMPLETE, |
+ content::NotificationService::AllSources()); |
+ |
+ // We expect that the fetch complete notification will be sent, but that the |
+ // image will be marked as failed. |
+ fetcher->OnURLFetchComplete(stub_url_fetcher); |
+ |
+ // Wait for the callback, test succeeds if callback looks good. |
+ signal.Wait(); |
+ |
+ ASSERT_TRUE(fetcher->image_failed()); |
+ ASSERT_FALSE(fetcher->image_ready()); |
+ |
+ // Make sure we don't get a bitmap. |
+ SkBitmap* found_image = fetcher->bitmap(); |
+ EXPECT_EQ(NULL, found_image); |
+} |
+ |
+} // namespace notifier |