Chromium Code Reviews| 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 void OnFetchComplete(const SkBitmap* bitmap) OVERRIDE { | |
| 40 if (NULL != bitmap) { | |
| 41 success_ = true; | |
| 42 bitmap_ = *bitmap; | |
| 43 bitmap->deepCopyTo(&bitmap_, bitmap->getConfig()); | |
| 44 } | |
| 45 // For async calls, we need to quit the message loop so the test can | |
| 46 // continue. | |
| 47 if (async_) { | |
| 48 MessageLoop::current()->Quit(); | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 bool success() const { return success_; } | |
| 53 const SkBitmap& bitmap() const { return bitmap_; } | |
| 54 | |
| 55 private: | |
| 56 bool success_; | |
| 57 bool async_; | |
| 58 SkBitmap bitmap_; | |
| 59 | |
| 60 DISALLOW_COPY_AND_ASSIGN(NotificationBitmapFetcherTestDelegate); | |
| 61 }; | |
| 62 | |
| 63 typedef InProcessBrowserTest NotificationBitmapFetcherBrowserTest; | |
| 64 | |
| 65 // WARNING: These tests work with --single_process, but not | |
| 66 // --single-process. The reason is that the sandbox does not get created | |
| 67 // for us by the test process if --single-process is used. | |
| 68 | |
| 69 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
| 70 StartTest) { | |
| 71 GURL url("http://localhost"); | |
| 72 | |
| 73 // Put some realistic looking bitmap data into the url_fetcher. | |
| 74 SkBitmap image; | |
| 75 | |
| 76 // Put a real bitmap into "image". 2x2 bitmap of green 32 bit pixels. | |
| 77 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); | |
| 78 image.allocPixels(); | |
| 79 SkColor c = kMaxGreen; | |
| 80 image.eraseColor(c); | |
| 81 | |
| 82 // Encode the bits as a PNG. | |
| 83 std::vector<unsigned char> compressed; | |
| 84 ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &compressed)); | |
| 85 | |
| 86 // Copy the bits into the string, and put them into the StubURLFetcher. | |
| 87 std::string image_string; | |
| 88 image_string.resize(compressed.size()); | |
| 89 // TODO(petewil): It might be better to have STL to write this loop for us | |
| 90 // with for_each or a function. Make sure it handles embedded nulls. | |
| 91 for (size_t ii = 0; ii < compressed.size(); ++ii) { | |
| 92 image_string[ii] = compressed[ii]; | |
| 93 } | |
| 94 | |
| 95 // Set up a delegate to wait for the callback. | |
| 96 NotificationBitmapFetcherTestDelegate delegate(kAsyncCall); | |
| 97 | |
| 98 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
| 99 new NotificationBitmapFetcher(url, &delegate); | |
| 100 | |
| 101 // The fetcher controls the lifetime of url_fetcher, but we keep a | |
| 102 // pointer to it with stub_url_fetcher. | |
| 103 net::TestURLFetcher* stub_url_fetcher = | |
| 104 new net::TestURLFetcher(1, url, fetcher.get()); | |
| 105 scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); | |
| 106 fetcher->SetURLFetcherForTest(url_fetcher); | |
| 107 stub_url_fetcher->SetResponseString(image_string); | |
| 108 | |
| 109 // We expect that the image decoder will get called and return | |
| 110 // an image in a callback to OnImageDecoded(). | |
| 111 fetcher->Start(); | |
| 112 | |
| 113 // Since the TestURLFetcher doesn't do anything when Start() is called, | |
|
dcheng
2013/05/30 00:38:14
Oops. Perhaps use FakeURLFetcher instead, which do
Pete Williamson
2013/05/30 22:22:18
Done.
| |
| 114 // manually simulate the callback it would generate. | |
| 115 fetcher->OnURLFetchComplete(stub_url_fetcher); | |
| 116 | |
| 117 // Blocks until test delegate is notified via a callback. | |
| 118 content::RunMessageLoop(); | |
| 119 | |
| 120 ASSERT_TRUE(delegate.success()); | |
| 121 | |
| 122 // Make sure we get back the bitmap we expect. | |
| 123 const SkBitmap& found_image = delegate.bitmap(); | |
| 124 EXPECT_TRUE(gfx::BitmapsAreEqual(image, found_image)); | |
| 125 } | |
| 126 | |
| 127 | |
| 128 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
| 129 OnImageDecodedTest) { | |
| 130 GURL url("http://localhost"); | |
| 131 SkBitmap image; | |
| 132 | |
| 133 // Put a real bitmap into "image". 2x2 bitmap of green 16 bit pixels. | |
| 134 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); | |
| 135 image.allocPixels(); | |
| 136 SkColor c = kMaxGreen; | |
| 137 image.eraseColor(c); | |
| 138 | |
| 139 NotificationBitmapFetcherTestDelegate delegate(kSyncCall); | |
| 140 | |
| 141 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
| 142 new NotificationBitmapFetcher(url, &delegate); | |
| 143 | |
| 144 scoped_ptr<net::URLFetcher> url_fetcher( | |
| 145 new net::TestURLFetcher(1, url, fetcher.get())); | |
| 146 fetcher->SetURLFetcherForTest(url_fetcher); | |
| 147 | |
| 148 fetcher->OnImageDecoded(NULL, image); | |
| 149 | |
| 150 // Ensure image is marked as succeeded. | |
| 151 EXPECT_TRUE(delegate.success()); | |
| 152 | |
| 153 // Test that the image is stored and ready. Pixel [0,0] should be green. | |
| 154 EXPECT_TRUE(gfx::BitmapsAreEqual(image, delegate.bitmap())); | |
| 155 | |
| 156 } | |
| 157 | |
| 158 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
| 159 OnURLFetchFailureTest) { | |
|
dcheng
2013/05/30 00:38:14
This test should be able to use NotificationBitmap
Pete Williamson
2013/05/30 22:22:18
Done.
| |
| 160 GURL url("http://localhost"); | |
| 161 | |
| 162 // We intentionally put no data into the bitmap to simulate a failure. | |
| 163 | |
| 164 // Set up a delegate to wait for the callback. | |
| 165 NotificationBitmapFetcherTestDelegate delegate(kSyncCall); | |
| 166 | |
| 167 // The fetcher controls the lifetime of url_fetcher, but we keep a | |
| 168 // pointer to it with stub_url_fetcher. | |
| 169 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
| 170 new NotificationBitmapFetcher(url, &delegate); | |
| 171 | |
| 172 // The fetcher controls the lifetime of url_fetcher, but we keep a | |
| 173 // pointer to it with stub_url_fetcher. | |
| 174 net::TestURLFetcher* stub_url_fetcher = | |
| 175 new net::TestURLFetcher(1, url, fetcher.get()); | |
| 176 scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); | |
| 177 fetcher->SetURLFetcherForTest(url_fetcher); | |
| 178 | |
| 179 // We expect that the fetch complete notification will be sent, but that the | |
| 180 // image will be marked as failed. | |
| 181 fetcher->OnURLFetchComplete(stub_url_fetcher); | |
| 182 | |
| 183 EXPECT_FALSE(delegate.success()); | |
| 184 } | |
| 185 | |
| 186 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
| 187 HandleImageFailedTest) { | |
| 188 GURL url("http://localhost"); | |
| 189 NotificationBitmapFetcherTestDelegate delegate(kSyncCall); | |
| 190 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
| 191 new NotificationBitmapFetcher(url, &delegate); | |
| 192 | |
| 193 scoped_ptr<net::URLFetcher> url_fetcher( | |
| 194 new net::TestURLFetcher(1, url, fetcher.get())); | |
| 195 fetcher->SetURLFetcherForTest(url_fetcher); | |
|
dcheng
2013/05/30 00:38:14
I think it should be pretty easy to make this test
Pete Williamson
2013/05/30 22:22:18
Done.
| |
| 196 | |
| 197 fetcher->OnDecodeImageFailed(NULL); | |
| 198 | |
| 199 EXPECT_FALSE(delegate.success()); | |
| 200 } | |
| 201 | |
| 202 } // namespace notifier | |
| OLD | NEW |