Chromium Code Reviews| 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..f78d68c60596cc1889ed489b5ce0e957a1f5e53e |
| --- /dev/null |
| +++ b/chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher_browsertest.cc |
| @@ -0,0 +1,184 @@ |
| +// 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/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" |
| + |
| +namespace { |
| +// FF0000FF is 100% alpha and max green.(A, R, B, G) |
| +uint32_t kMaxGreen = 0xFF0000FF; |
|
dcheng
2013/05/23 19:32:25
Minor nit: Chrome prefers the use of the integer t
Pete Williamson
2013/05/24 22:18:06
Done.
|
| +} // namespace |
| + |
| +namespace notifier { |
| + |
| +class NotificationBitmapFetcherBrowserTest : public InProcessBrowserTest { |
|
dcheng
2013/05/23 19:32:25
The convention is to use a typedef in this case. H
Pete Williamson
2013/05/24 22:18:06
Changed to use a typedef
I originally tried to wr
|
| +}; |
| + |
| +// 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); |
| + // 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]; |
| + } |
| + |
| + scoped_refptr<NotificationBitmapFetcher> fetcher = |
| + new NotificationBitmapFetcher(url); |
| + |
| + // 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); |
| + |
| + // 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"); |
| + |
| + scoped_refptr<NotificationBitmapFetcher> fetcher = |
| + new NotificationBitmapFetcher(url); |
| + |
| + // 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); |
| + |
| + // 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"); |
| + |
| + // 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); |
| + |
| + // 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); |
| + |
| + // 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 |