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 "chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher
.h" |
| 6 |
| 7 #include "base/compiler_specific.h" |
| 8 #include "chrome/test/base/in_process_browser_test.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/test/test_utils.h" |
| 11 #include "net/url_request/test_url_fetcher_factory.h" |
| 12 #include "net/url_request/url_fetcher.h" |
| 13 #include "net/url_request/url_request_status.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 16 #include "ui/gfx/codec/png_codec.h" |
| 17 #include "ui/gfx/size.h" |
| 18 #include "ui/gfx/skia_util.h" |
| 19 |
| 20 namespace { |
| 21 // FF0000FF is 100% alpha and max green.(A, R, B, G) |
| 22 uint32 kMaxGreen = 0xFF0000FF; |
| 23 const bool kAsyncCall = true; |
| 24 const bool kSyncCall = false; |
| 25 } // namespace |
| 26 |
| 27 namespace notifier { |
| 28 |
| 29 // Class to catch events from the NotificationBitmapFetcher for testing. |
| 30 class NotificationBitmapFetcherTestDelegate |
| 31 : public NotificationBitmapFetcherDelegate { |
| 32 public: |
| 33 explicit NotificationBitmapFetcherTestDelegate(bool async) |
| 34 : success_(false), async_(async) {} |
| 35 |
| 36 virtual ~NotificationBitmapFetcherTestDelegate() {} |
| 37 |
| 38 // Method inherited from NotificationBitmapFetcherDelegate. |
| 39 virtual void OnFetchComplete(const SkBitmap* bitmap) OVERRIDE { |
| 40 if (NULL != bitmap) { |
| 41 success_ = true; |
| 42 bitmap->deepCopyTo(&bitmap_, bitmap->getConfig()); |
| 43 } |
| 44 // For async calls, we need to quit the message loop so the test can |
| 45 // continue. |
| 46 if (async_) { |
| 47 MessageLoop::current()->Quit(); |
| 48 } |
| 49 } |
| 50 |
| 51 bool success() const { return success_; } |
| 52 const SkBitmap& bitmap() const { return bitmap_; } |
| 53 |
| 54 private: |
| 55 bool success_; |
| 56 bool async_; |
| 57 SkBitmap bitmap_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(NotificationBitmapFetcherTestDelegate); |
| 60 }; |
| 61 |
| 62 typedef InProcessBrowserTest NotificationBitmapFetcherBrowserTest; |
| 63 |
| 64 // WARNING: These tests work with --single_process, but not |
| 65 // --single-process. The reason is that the sandbox does not get created |
| 66 // for us by the test process if --single-process is used. |
| 67 |
| 68 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, |
| 69 StartTest) { |
| 70 GURL url("http://localhost"); |
| 71 |
| 72 // Put some realistic looking bitmap data into the url_fetcher. |
| 73 SkBitmap image; |
| 74 |
| 75 // Put a real bitmap into "image". 2x2 bitmap of green 32 bit pixels. |
| 76 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); |
| 77 image.allocPixels(); |
| 78 SkColor c = kMaxGreen; |
| 79 image.eraseColor(c); |
| 80 |
| 81 // Encode the bits as a PNG. |
| 82 std::vector<unsigned char> compressed; |
| 83 ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &compressed)); |
| 84 |
| 85 // Copy the bits into the string, and put them into the FakeURLFetcher. |
| 86 std::string image_string(compressed.begin(), compressed.end()); |
| 87 |
| 88 // Set up a delegate to wait for the callback. |
| 89 NotificationBitmapFetcherTestDelegate delegate(kAsyncCall); |
| 90 |
| 91 NotificationBitmapFetcher fetcher(url, &delegate); |
| 92 |
| 93 scoped_ptr<net::URLFetcher> url_fetcher(new net::FakeURLFetcher( |
| 94 url, &fetcher, image_string, /*success=*/true)); |
| 95 fetcher.SetURLFetcherForTest(url_fetcher.Pass()); |
| 96 |
| 97 // We expect that the image decoder will get called and return |
| 98 // an image in a callback to OnImageDecoded(). |
| 99 fetcher.Start(); |
| 100 |
| 101 // Blocks until test delegate is notified via a callback. |
| 102 content::RunMessageLoop(); |
| 103 |
| 104 ASSERT_TRUE(delegate.success()); |
| 105 |
| 106 // Make sure we get back the bitmap we expect. |
| 107 const SkBitmap& found_image = delegate.bitmap(); |
| 108 EXPECT_TRUE(gfx::BitmapsAreEqual(image, found_image)); |
| 109 } |
| 110 |
| 111 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, |
| 112 OnImageDecodedTest) { |
| 113 GURL url("http://localhost"); |
| 114 SkBitmap image; |
| 115 |
| 116 // Put a real bitmap into "image". 2x2 bitmap of green 16 bit pixels. |
| 117 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); |
| 118 image.allocPixels(); |
| 119 SkColor c = kMaxGreen; |
| 120 image.eraseColor(c); |
| 121 |
| 122 NotificationBitmapFetcherTestDelegate delegate(kSyncCall); |
| 123 |
| 124 NotificationBitmapFetcher fetcher(url, &delegate); |
| 125 |
| 126 fetcher.OnImageDecoded(NULL, image); |
| 127 |
| 128 // Ensure image is marked as succeeded. |
| 129 EXPECT_TRUE(delegate.success()); |
| 130 |
| 131 // Test that the image is what we expect. |
| 132 EXPECT_TRUE(gfx::BitmapsAreEqual(image, delegate.bitmap())); |
| 133 } |
| 134 |
| 135 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, |
| 136 OnURLFetchFailureTest) { |
| 137 GURL url("http://localhost"); |
| 138 |
| 139 // We intentionally put no data into the bitmap to simulate a failure. |
| 140 |
| 141 // Set up a delegate to wait for the callback. |
| 142 NotificationBitmapFetcherTestDelegate delegate(kAsyncCall); |
| 143 |
| 144 NotificationBitmapFetcher fetcher(url, &delegate); |
| 145 |
| 146 scoped_ptr<net::URLFetcher> url_fetcher(new net::FakeURLFetcher( |
| 147 url, &fetcher, std::string(), /*success=*/ false)); |
| 148 fetcher.SetURLFetcherForTest(url_fetcher.Pass()); |
| 149 |
| 150 fetcher.Start(); |
| 151 |
| 152 // Blocks until test delegate is notified via a callback. |
| 153 content::RunMessageLoop(); |
| 154 |
| 155 EXPECT_FALSE(delegate.success()); |
| 156 } |
| 157 |
| 158 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, |
| 159 HandleImageFailedTest) { |
| 160 GURL url("http://localhost"); |
| 161 NotificationBitmapFetcherTestDelegate delegate(kAsyncCall); |
| 162 NotificationBitmapFetcher fetcher(url, &delegate); |
| 163 scoped_ptr<net::URLFetcher> url_fetcher(new net::FakeURLFetcher( |
| 164 url, &fetcher, std::string("Not a real bitmap"), /*success=*/ true)); |
| 165 fetcher.SetURLFetcherForTest(url_fetcher.Pass()); |
| 166 |
| 167 fetcher.Start(); |
| 168 |
| 169 // Blocks until test delegate is notified via a callback. |
| 170 content::RunMessageLoop(); |
| 171 |
| 172 EXPECT_FALSE(delegate.success()); |
| 173 } |
| 174 |
| 175 } // namespace notifier |
OLD | NEW |