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 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.
| |
| 24 } // namespace | |
| 25 | |
| 26 namespace notifier { | |
| 27 | |
| 28 // Class to catch events from the NotificationBitmapFetcher for testing. | |
| 29 class NotificationBitmapFetcherTestDelegate | |
| 30 : public NotificationBitmapFetcherDelegate { | |
| 31 public: | |
| 32 explicit NotificationBitmapFetcherTestDelegate(bool async = false) | |
|
dcheng
2013/05/29 20:40:29
No default args.
Pete Williamson
2013/05/30 00:13:18
Done.
| |
| 33 : success_(false), async_(async) {} | |
| 34 | |
| 35 virtual ~NotificationBitmapFetcherTestDelegate() {} | |
| 36 | |
| 37 // Method inherited from NotificationBitmapFetcherDelegate | |
| 38 void OnFetchComplete(GURL url, const SkBitmap* bitmap) OVERRIDE { | |
| 39 url_ = url; | |
| 40 if (NULL != bitmap) { | |
| 41 success_ = true; | |
| 42 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.
| |
| 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 GURL url() const { return url_; } | |
| 53 bool success() const { return success_; } | |
| 54 const SkBitmap& bitmap() const { return bitmap_; } | |
| 55 | |
| 56 private: | |
| 57 GURL url_; | |
| 58 bool success_; | |
| 59 bool async_; | |
| 60 SkBitmap bitmap_; | |
| 61 | |
| 62 DISALLOW_COPY_AND_ASSIGN(NotificationBitmapFetcherTestDelegate); | |
| 63 }; | |
| 64 | |
| 65 typedef InProcessBrowserTest NotificationBitmapFetcherBrowserTest; | |
| 66 | |
| 67 // WARNING: These tests work with --single_process, but not | |
| 68 // --single-process. The reason is that the sandbox does not get created | |
| 69 // for us by the test process if --single-process is used. | |
| 70 | |
| 71 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
| 72 OnURLFetchCompleteTest) { | |
| 73 GURL url("http://localhost"); | |
| 74 | |
| 75 // Put some realistic looking bitmap data into the url_fetcher. | |
| 76 SkBitmap image; | |
| 77 | |
| 78 // Put a real bitmap into "image". 2x2 bitmap of green 32 bit pixels. | |
| 79 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); | |
| 80 image.allocPixels(); | |
| 81 SkColor c = kMaxGreen; | |
| 82 image.eraseColor(c); | |
| 83 | |
| 84 // Encode the bits as a PNG. | |
| 85 std::vector<unsigned char> compressed; | |
| 86 ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &compressed)); | |
| 87 | |
| 88 // Copy the bits into the string, and put them into the StubURLFetcher. | |
| 89 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
| |
| 90 image_string.resize(compressed.size()); | |
| 91 // TODO(petewil): It might be better to have STL to write this loop for us | |
| 92 // with for_each or a function. Make sure it handles embedded nulls. | |
| 93 for (size_t ii = 0; ii < compressed.size(); ++ii) { | |
| 94 image_string[ii] = compressed[ii]; | |
| 95 } | |
| 96 | |
| 97 // Set up a delegate to wait for the callback. | |
| 98 NotificationBitmapFetcherTestDelegate delegate(ASYNC_CALL); | |
| 99 | |
| 100 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
| 101 new NotificationBitmapFetcher(url, &delegate); | |
| 102 | |
| 103 // The fetcher controls the lifetime of url_fetcher, but we keep a | |
| 104 // pointer to it with stub_url_fetcher. | |
| 105 net::TestURLFetcher* stub_url_fetcher = | |
| 106 new net::TestURLFetcher(1, url, fetcher.get()); | |
| 107 scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); | |
| 108 fetcher->SetURLFetcherForTest(url_fetcher); | |
| 109 stub_url_fetcher->SetResponseString(image_string); | |
| 110 | |
| 111 // We expect that the image decoder will get called and return | |
| 112 // an image in a callback to OnImageDecoded(). | |
| 113 fetcher->OnURLFetchComplete(stub_url_fetcher); | |
| 114 | |
| 115 // Blocks until test delegate is notified via a callback. | |
| 116 content::RunMessageLoop(); | |
| 117 | |
| 118 ASSERT_TRUE(delegate.success()); | |
| 119 | |
| 120 // Make sure we get back the bitmap we expect. | |
| 121 const SkBitmap& found_image = delegate.bitmap(); | |
| 122 EXPECT_TRUE(gfx::BitmapsAreEqual(image, found_image)); | |
| 123 } | |
| 124 | |
| 125 | |
| 126 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
| 127 OnImageDecodedTest) { | |
| 128 GURL url("http://localhost"); | |
| 129 scoped_ptr<SkBitmap> image(new SkBitmap()); | |
| 130 | |
| 131 // Put a real bitmap into "image". 2x2 bitmap of green 16 bit pixels. | |
| 132 image->setConfig(SkBitmap::kRGB_565_Config, 2, 2); | |
| 133 image->allocPixels(); | |
| 134 SkColor c = kMaxGreen; | |
| 135 image->eraseColor(c); | |
| 136 // Test that the image is stored and ready. Pixel [0,0] should be green. | |
| 137 EXPECT_EQ(8, image->getSize()); | |
| 138 EXPECT_EQ(kMaxGreen, image->getColor(0, 0)); | |
| 139 | |
| 140 NotificationBitmapFetcherTestDelegate delegate; | |
| 141 | |
| 142 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
| 143 new NotificationBitmapFetcher(url, &delegate); | |
| 144 | |
| 145 scoped_ptr<net::URLFetcher> url_fetcher( | |
| 146 new net::TestURLFetcher(1, url, fetcher.get())); | |
| 147 fetcher->SetURLFetcherForTest(url_fetcher); | |
| 148 | |
| 149 fetcher->OnImageDecoded(NULL, *image.get()); | |
| 150 | |
| 151 // Ensure image is marked as succeeded. | |
| 152 ASSERT_TRUE(delegate.success()); | |
| 153 | |
| 154 // Test that the image is stored and ready. Pixel [0,0] should be green. | |
| 155 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
| |
| 156 EXPECT_EQ(2, delegate.bitmap().width()); | |
| 157 EXPECT_EQ(2, delegate.bitmap().height()); | |
| 158 EXPECT_TRUE(delegate.bitmap().getPixels() != NULL); | |
| 159 EXPECT_EQ(kMaxGreen, delegate.bitmap().getColor(0, 0)); | |
| 160 } | |
| 161 | |
| 162 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
| 163 OnURLFetchFailureTest) { | |
| 164 GURL url("http://localhost"); | |
| 165 | |
| 166 // We intentionally put no data into the bitmap to simulate a failure. | |
| 167 | |
| 168 // Set up a delegate to wait for the callback. | |
| 169 NotificationBitmapFetcherTestDelegate delegate; | |
| 170 | |
| 171 // The fetcher controls the lifetime of url_fetcher, but we keep a | |
| 172 // pointer to it with stub_url_fetcher. | |
| 173 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
| 174 new NotificationBitmapFetcher(url, &delegate); | |
| 175 | |
| 176 // The fetcher controls the lifetime of url_fetcher, but we keep a | |
| 177 // pointer to it with stub_url_fetcher. | |
| 178 net::TestURLFetcher* stub_url_fetcher = | |
| 179 new net::TestURLFetcher(1, url, fetcher.get()); | |
| 180 scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); | |
| 181 fetcher->SetURLFetcherForTest(url_fetcher); | |
| 182 | |
| 183 // We expect that the fetch complete notification will be sent, but that the | |
| 184 // image will be marked as failed. | |
| 185 fetcher->OnURLFetchComplete(stub_url_fetcher); | |
| 186 | |
| 187 EXPECT_FALSE(delegate.success()); | |
| 188 } | |
| 189 | |
| 190 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
| 191 HandleImageFailedTest) { | |
| 192 GURL url("http://localhost"); | |
| 193 NotificationBitmapFetcherTestDelegate delegate; | |
| 194 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
| 195 new NotificationBitmapFetcher(url, &delegate); | |
| 196 | |
| 197 scoped_ptr<net::URLFetcher> url_fetcher( | |
| 198 new net::TestURLFetcher(1, url, fetcher.get())); | |
| 199 fetcher->SetURLFetcherForTest(url_fetcher); | |
| 200 | |
| 201 fetcher->OnDecodeImageFailed(NULL); | |
| 202 | |
| 203 EXPECT_FALSE(delegate.success()); | |
| 204 } | |
| 205 | |
| 206 } // namespace notifier | |
| OLD | NEW |