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..3532d66ffec5d9438d82f26772a112f860c02a62 |
--- /dev/null |
+++ b/chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher_browsertest.cc |
@@ -0,0 +1,206 @@ |
+// 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 "base/compiler_specific.h" |
+#include "chrome/test/base/in_process_browser_test.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/test/test_utils.h" |
+#include "net/url_request/test_url_fetcher_factory.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" |
+#include "ui/gfx/skia_util.h" |
+ |
+namespace { |
+// FF0000FF is 100% alpha and max green.(A, R, B, G) |
+uint32 kMaxGreen = 0xFF0000FF; |
+const bool ASYNC_CALL = true; |
dcheng
2013/05/29 20:40:29
Note that naming convention for constants is kFoo.
Pete Williamson
2013/05/30 00:13:18
Done.
|
+} // namespace |
+ |
+namespace notifier { |
+ |
+// Class to catch events from the NotificationBitmapFetcher for testing. |
+class NotificationBitmapFetcherTestDelegate |
+ : public NotificationBitmapFetcherDelegate { |
+ public: |
+ explicit NotificationBitmapFetcherTestDelegate(bool async = false) |
dcheng
2013/05/29 20:40:29
No default args.
Pete Williamson
2013/05/30 00:13:18
Done.
|
+ : success_(false), async_(async) {} |
+ |
+ virtual ~NotificationBitmapFetcherTestDelegate() {} |
+ |
+ // Method inherited from NotificationBitmapFetcherDelegate |
+ void OnFetchComplete(GURL url, const SkBitmap* bitmap) OVERRIDE { |
+ url_ = url; |
+ if (NULL != bitmap) { |
+ success_ = true; |
+ bitmap_ = *bitmap; |
dcheng
2013/05/29 20:40:29
I suspect operator= is sufficient. Invoking deepCo
Pete Williamson
2013/05/30 00:13:18
My testing shows that operator= is not sufficient
dcheng
2013/05/30 00:38:14
It seems strange to use both operator= and deepCop
Pete Williamson
2013/05/30 22:22:17
OK, using just deepCopyTo seems to work, Done.
|
+ bitmap->deepCopyTo(&bitmap_, bitmap->getConfig()); |
+ } |
+ // For async calls, we need to quit the message loop so the test can |
+ // continue. |
+ if (async_) { |
+ MessageLoop::current()->Quit(); |
+ } |
+ } |
+ |
+ GURL url() const { return url_; } |
+ bool success() const { return success_; } |
+ const SkBitmap& bitmap() const { return bitmap_; } |
+ |
+ private: |
+ GURL url_; |
+ bool success_; |
+ bool async_; |
+ SkBitmap bitmap_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(NotificationBitmapFetcherTestDelegate); |
+}; |
+ |
+typedef InProcessBrowserTest NotificationBitmapFetcherBrowserTest; |
+ |
+// 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"); |
+ |
+ // 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); |
+ |
+ // Encode the bits as a PNG. |
+ std::vector<unsigned char> compressed; |
+ ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &compressed)); |
+ |
+ // Copy the bits into the string, and put them into the StubURLFetcher. |
+ std::string image_string; |
dcheng
2013/05/29 20:40:29
std::string image_string(compressed.begin(), compr
Pete Williamson
2013/05/30 00:13:18
Done. Thanks! I didn't realize that string would
dcheng
2013/05/30 00:38:14
I don't see the change.
Pete Williamson
2013/05/30 22:22:17
Oops, it got undone when undoing and redoing to fi
|
+ 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]; |
+ } |
+ |
+ // Set up a delegate to wait for the callback. |
+ NotificationBitmapFetcherTestDelegate delegate(ASYNC_CALL); |
+ |
+ scoped_refptr<NotificationBitmapFetcher> fetcher = |
+ new NotificationBitmapFetcher(url, &delegate); |
+ |
+ // The fetcher controls the lifetime of url_fetcher, but we keep a |
+ // pointer to it with stub_url_fetcher. |
+ net::TestURLFetcher* stub_url_fetcher = |
+ new net::TestURLFetcher(1, url, fetcher.get()); |
+ scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); |
+ fetcher->SetURLFetcherForTest(url_fetcher); |
+ stub_url_fetcher->SetResponseString(image_string); |
+ |
+ // We expect that the image decoder will get called and return |
+ // an image in a callback to OnImageDecoded(). |
+ fetcher->OnURLFetchComplete(stub_url_fetcher); |
+ |
+ // Blocks until test delegate is notified via a callback. |
+ content::RunMessageLoop(); |
+ |
+ ASSERT_TRUE(delegate.success()); |
+ |
+ // Make sure we get back the bitmap we expect. |
+ const SkBitmap& found_image = delegate.bitmap(); |
+ EXPECT_TRUE(gfx::BitmapsAreEqual(image, found_image)); |
+} |
+ |
+ |
+IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, |
+ OnImageDecodedTest) { |
+ GURL url("http://localhost"); |
+ 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)); |
+ |
+ NotificationBitmapFetcherTestDelegate delegate; |
+ |
+ scoped_refptr<NotificationBitmapFetcher> fetcher = |
+ new NotificationBitmapFetcher(url, &delegate); |
+ |
+ scoped_ptr<net::URLFetcher> url_fetcher( |
+ new net::TestURLFetcher(1, url, fetcher.get())); |
+ fetcher->SetURLFetcherForTest(url_fetcher); |
+ |
+ fetcher->OnImageDecoded(NULL, *image.get()); |
+ |
+ // Ensure image is marked as succeeded. |
+ ASSERT_TRUE(delegate.success()); |
+ |
+ // Test that the image is stored and ready. Pixel [0,0] should be green. |
+ EXPECT_EQ(8, delegate.bitmap().getSize()); |
dcheng
2013/05/29 20:40:29
Will gfx::BitmapsAreEqual work here?
Pete Williamson
2013/05/30 00:13:18
Done. It turns out that it only works for ARGB_88
|
+ EXPECT_EQ(2, delegate.bitmap().width()); |
+ EXPECT_EQ(2, delegate.bitmap().height()); |
+ EXPECT_TRUE(delegate.bitmap().getPixels() != NULL); |
+ EXPECT_EQ(kMaxGreen, delegate.bitmap().getColor(0, 0)); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, |
+ OnURLFetchFailureTest) { |
+ GURL url("http://localhost"); |
+ |
+ // We intentionally put no data into the bitmap to simulate a failure. |
+ |
+ // Set up a delegate to wait for the callback. |
+ NotificationBitmapFetcherTestDelegate delegate; |
+ |
+ // 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, &delegate); |
+ |
+ // The fetcher controls the lifetime of url_fetcher, but we keep a |
+ // pointer to it with stub_url_fetcher. |
+ net::TestURLFetcher* stub_url_fetcher = |
+ new net::TestURLFetcher(1, url, fetcher.get()); |
+ scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); |
+ fetcher->SetURLFetcherForTest(url_fetcher); |
+ |
+ // We expect that the fetch complete notification will be sent, but that the |
+ // image will be marked as failed. |
+ fetcher->OnURLFetchComplete(stub_url_fetcher); |
+ |
+ EXPECT_FALSE(delegate.success()); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, |
+ HandleImageFailedTest) { |
+ GURL url("http://localhost"); |
+ NotificationBitmapFetcherTestDelegate delegate; |
+ scoped_refptr<NotificationBitmapFetcher> fetcher = |
+ new NotificationBitmapFetcher(url, &delegate); |
+ |
+ scoped_ptr<net::URLFetcher> url_fetcher( |
+ new net::TestURLFetcher(1, url, fetcher.get())); |
+ fetcher->SetURLFetcherForTest(url_fetcher); |
+ |
+ fetcher->OnDecodeImageFailed(NULL); |
+ |
+ EXPECT_FALSE(delegate.success()); |
+} |
+ |
+} // namespace notifier |