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 const bool kSuccess = true; | |
| 26 const bool kFailure = false; | |
| 27 } // namespace | |
| 28 | |
| 29 namespace notifier { | |
| 30 | |
| 31 // Class to catch events from the NotificationBitmapFetcher for testing. | |
| 32 class NotificationBitmapFetcherTestDelegate | |
| 33 : public NotificationBitmapFetcherDelegate { | |
| 34 public: | |
| 35 explicit NotificationBitmapFetcherTestDelegate(bool async) | |
|
dcheng
2013/05/30 22:38:09
Since all tests are now async, the async_ paramete
Pete Williamson
2013/05/31 00:17:39
We still have one sync test (and I may add more so
dcheng
2013/05/31 00:27:38
How is StartTest different from OnImageDecodedTest
Pete Williamson
2013/05/31 01:01:44
The primary difference is that we are testing a di
dcheng
2013/05/31 02:34:52
I'm trying to think of a situation where StartTest
Pete Williamson
2013/05/31 16:42:20
If start test fails, I can quickly tell by whether
| |
| 36 : success_(false), async_(async) {} | |
| 37 | |
| 38 virtual ~NotificationBitmapFetcherTestDelegate() {} | |
| 39 | |
| 40 // Method inherited from NotificationBitmapFetcherDelegate | |
| 41 void OnFetchComplete(const SkBitmap* bitmap) OVERRIDE { | |
| 42 if (NULL != bitmap) { | |
| 43 success_ = true; | |
| 44 bitmap->deepCopyTo(&bitmap_, bitmap->getConfig()); | |
| 45 } | |
| 46 // For async calls, we need to quit the message loop so the test can | |
| 47 // continue. | |
| 48 if (async_) { | |
| 49 MessageLoop::current()->Quit(); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 bool success() const { return success_; } | |
| 54 const SkBitmap& bitmap() const { return bitmap_; } | |
| 55 | |
| 56 private: | |
| 57 bool success_; | |
| 58 bool async_; | |
| 59 SkBitmap bitmap_; | |
| 60 | |
| 61 DISALLOW_COPY_AND_ASSIGN(NotificationBitmapFetcherTestDelegate); | |
| 62 }; | |
| 63 | |
| 64 typedef InProcessBrowserTest NotificationBitmapFetcherBrowserTest; | |
| 65 | |
| 66 // WARNING: These tests work with --single_process, but not | |
| 67 // --single-process. The reason is that the sandbox does not get created | |
| 68 // for us by the test process if --single-process is used. | |
| 69 | |
| 70 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
| 71 StartTest) { | |
| 72 GURL url("http://localhost"); | |
| 73 | |
| 74 // Put some realistic looking bitmap data into the url_fetcher. | |
| 75 SkBitmap image; | |
| 76 | |
| 77 // Put a real bitmap into "image". 2x2 bitmap of green 32 bit pixels. | |
| 78 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); | |
| 79 image.allocPixels(); | |
| 80 SkColor c = kMaxGreen; | |
| 81 image.eraseColor(c); | |
| 82 | |
| 83 // Encode the bits as a PNG. | |
| 84 std::vector<unsigned char> compressed; | |
| 85 ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &compressed)); | |
| 86 | |
| 87 // Copy the bits into the string, and put them into the StubURLFetcher. | |
|
dcheng
2013/05/30 22:38:09
Nit: FakeURLFetcher.
Pete Williamson
2013/05/31 01:01:44
Done.
| |
| 88 std::string image_string(compressed.begin(), compressed.end()); | |
| 89 | |
| 90 // Set up a delegate to wait for the callback. | |
| 91 NotificationBitmapFetcherTestDelegate delegate(kAsyncCall); | |
| 92 | |
| 93 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
| 94 new NotificationBitmapFetcher(url, &delegate); | |
| 95 | |
| 96 scoped_ptr<net::URLFetcher> url_fetcher(new net::FakeURLFetcher( | |
| 97 url, fetcher, image_string, kSuccess)); | |
|
dcheng
2013/05/30 22:38:09
This is a personal preference, but I don't think t
Pete Williamson
2013/05/31 00:17:39
I think that knowing the meaning (succeeded, faile
dcheng
2013/05/31 00:27:38
Right, that's why I suggested putting it in commen
Pete Williamson
2013/05/31 01:01:44
Done.
| |
| 98 fetcher->SetURLFetcherForTest(url_fetcher.Pass()); | |
| 99 | |
| 100 // We expect that the image decoder will get called and return | |
| 101 // an image in a callback to OnImageDecoded(). | |
| 102 fetcher->Start(); | |
| 103 | |
| 104 // Blocks until test delegate is notified via a callback. | |
| 105 content::RunMessageLoop(); | |
| 106 | |
| 107 ASSERT_TRUE(delegate.success()); | |
| 108 | |
| 109 // Make sure we get back the bitmap we expect. | |
| 110 const SkBitmap& found_image = delegate.bitmap(); | |
| 111 EXPECT_TRUE(gfx::BitmapsAreEqual(image, found_image)); | |
| 112 } | |
| 113 | |
| 114 | |
| 115 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
| 116 OnImageDecodedTest) { | |
| 117 GURL url("http://localhost"); | |
| 118 SkBitmap image; | |
| 119 | |
| 120 // Put a real bitmap into "image". 2x2 bitmap of green 16 bit pixels. | |
| 121 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); | |
| 122 image.allocPixels(); | |
| 123 SkColor c = kMaxGreen; | |
| 124 image.eraseColor(c); | |
| 125 | |
| 126 NotificationBitmapFetcherTestDelegate delegate(kSyncCall); | |
| 127 | |
| 128 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
| 129 new NotificationBitmapFetcher(url, &delegate); | |
| 130 | |
| 131 scoped_ptr<net::URLFetcher> url_fetcher( | |
| 132 new net::TestURLFetcher(1, url, fetcher.get())); | |
| 133 fetcher->SetURLFetcherForTest(url_fetcher.Pass()); | |
| 134 | |
| 135 fetcher->OnImageDecoded(NULL, image); | |
| 136 | |
| 137 // Ensure image is marked as succeeded. | |
| 138 EXPECT_TRUE(delegate.success()); | |
| 139 | |
| 140 // Test that the image is stored and ready. Pixel [0,0] should be green. | |
| 141 EXPECT_TRUE(gfx::BitmapsAreEqual(image, delegate.bitmap())); | |
| 142 | |
| 143 } | |
| 144 | |
| 145 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
| 146 OnURLFetchFailureTest) { | |
| 147 GURL url("http://localhost"); | |
| 148 | |
| 149 // We intentionally put no data into the bitmap to simulate a failure. | |
| 150 | |
| 151 // Set up a delegate to wait for the callback. | |
| 152 NotificationBitmapFetcherTestDelegate delegate(kAsyncCall); | |
| 153 | |
| 154 // The fetcher controls the lifetime of url_fetcher, but we keep a | |
| 155 // pointer to it with stub_url_fetcher. | |
| 156 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
| 157 new NotificationBitmapFetcher(url, &delegate); | |
| 158 | |
| 159 scoped_ptr<net::URLFetcher> url_fetcher(new net::FakeURLFetcher( | |
| 160 url, fetcher, std::string(), kFailure)); | |
| 161 fetcher->SetURLFetcherForTest(url_fetcher.Pass()); | |
| 162 | |
| 163 fetcher->Start(); | |
| 164 | |
| 165 EXPECT_FALSE(delegate.success()); | |
| 166 } | |
| 167 | |
| 168 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
| 169 HandleImageFailedTest) { | |
| 170 GURL url("http://localhost"); | |
| 171 NotificationBitmapFetcherTestDelegate delegate(kAsyncCall); | |
| 172 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
| 173 new NotificationBitmapFetcher(url, &delegate); | |
| 174 scoped_ptr<net::URLFetcher> url_fetcher(new net::FakeURLFetcher( | |
| 175 url, fetcher, std::string("Not a real bitmap"), kSuccess)); | |
| 176 fetcher->SetURLFetcherForTest(url_fetcher.Pass()); | |
| 177 | |
| 178 fetcher->Start(); | |
| 179 | |
| 180 EXPECT_FALSE(delegate.success()); | |
| 181 } | |
| 182 | |
| 183 } // namespace notifier | |
| OLD | NEW |