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 "chrome/common/chrome_notification_types.h" | |
| 8 #include "chrome/test/base/in_process_browser_test.h" | |
| 9 #include "content/public/browser/browser_thread.h" | |
| 10 #include "content/public/browser/notification_service.h" | |
| 11 #include "content/public/test/test_utils.h" | |
| 12 #include "net/url_request/test_url_fetcher_factory.h" | |
| 13 #include "net/url_request/url_fetcher.h" | |
| 14 #include "net/url_request/url_request_status.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 #include "third_party/skia/include/core/SKBitmap.h" | |
| 17 #include "ui/gfx/codec/png_codec.h" | |
| 18 #include "ui/gfx/size.h" | |
| 19 | |
| 20 namespace { | |
| 21 // FF0000FF is 100% alpha and max green.(A, R, B, G) | |
| 22 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.
| |
| 23 } // namespace | |
| 24 | |
| 25 namespace notifier { | |
| 26 | |
| 27 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
| |
| 28 }; | |
| 29 | |
| 30 // WARNING: These tests work with --single_process, but not | |
| 31 // --single-process. The reason is that the sandbox does not get created | |
| 32 // for us by the test process if --single-process is used. | |
| 33 | |
| 34 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
| 35 OnURLFetchCompleteTest) { | |
| 36 GURL url("http://localhost"); | |
| 37 | |
| 38 // Put some realistic looking bitmap data into the url_fetcher. | |
| 39 SkBitmap image; | |
| 40 | |
| 41 // Put a real bitmap into "image". 2x2 bitmap of green 32 bit pixels. | |
| 42 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); | |
| 43 image.allocPixels(); | |
| 44 SkColor c = kMaxGreen; | |
| 45 image.eraseColor(c); | |
| 46 // Test that the image is stored and ready. Pixel [0,0] should be green. | |
| 47 EXPECT_EQ(16, image.getSize()); | |
| 48 EXPECT_EQ(kMaxGreen, image.getColor(0, 0)); | |
| 49 | |
| 50 // Get vital stats from the bitmap for width, height, size, etc. | |
| 51 SkAutoLockPixels lock(image); | |
| 52 int width = image.width(); | |
| 53 int height = image.height(); | |
| 54 int row_length = static_cast<int>(image.rowBytes()); | |
| 55 size_t size = row_length * height; | |
| 56 // Actual bitmap data in arrays of RGBAs. | |
| 57 std::vector<unsigned char> data; | |
| 58 data.resize(size); | |
| 59 memcpy(&*data.begin(), image.getAddr(0, 0), size); | |
| 60 | |
| 61 // Encode the bits as a PNG. | |
| 62 std::vector<unsigned char> compressed; | |
| 63 ASSERT_TRUE(gfx::PNGCodec::Encode(&*data.begin(), | |
| 64 gfx::PNGCodec::FORMAT_BGRA, | |
| 65 gfx::Size(width, height), | |
| 66 static_cast<int>(row_length), | |
| 67 true, | |
| 68 std::vector<gfx::PNGCodec::Comment>(), | |
| 69 &compressed)); | |
| 70 | |
| 71 // Copy the bits into the string, and put them into the StubURLFetcher. | |
| 72 std::string image_string; | |
| 73 image_string.resize(compressed.size()); | |
| 74 // TODO(petewil): It might be better to have STL to write this loop for us | |
| 75 // with for_each or a function. Make sure it handles embedded nulls. | |
| 76 for (size_t ii = 0; ii < compressed.size(); ++ii) { | |
| 77 image_string[ii] = compressed[ii]; | |
| 78 } | |
| 79 | |
| 80 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
| 81 new NotificationBitmapFetcher(url); | |
| 82 | |
| 83 // The fetcher controls the lifetime of url_fetcher, but we keep a | |
| 84 // pointer to it with stub_url_fetcher. | |
| 85 net::TestURLFetcher* stub_url_fetcher = | |
| 86 new net::TestURLFetcher(1, url, fetcher.get()); | |
| 87 scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); | |
| 88 fetcher->SetURLFetcherForTest(url_fetcher); | |
| 89 stub_url_fetcher->SetResponseString(image_string); | |
| 90 | |
| 91 // Set up a signal to wait for the bitmap fetch to be done. | |
| 92 content::WindowedNotificationObserver signal( | |
| 93 chrome::NOTIFICATION_NOTIFY_BITMAP_FETCH_COMPLETE, | |
| 94 content::NotificationService::AllSources()); | |
| 95 | |
| 96 // We expect that the image decoder will get called and return | |
| 97 // an image in a callback to OnImageDecoded(). | |
| 98 fetcher->OnURLFetchComplete(stub_url_fetcher); | |
| 99 | |
| 100 // Wait for the callback, test succeeds if callback looks good. | |
| 101 signal.Wait(); | |
| 102 | |
| 103 ASSERT_FALSE(fetcher->image_failed()); | |
| 104 ASSERT_TRUE(fetcher->image_ready()); | |
| 105 | |
| 106 // Make sure we get back the bitmap we expect. | |
| 107 SkBitmap* found_image = fetcher->bitmap(); | |
| 108 EXPECT_EQ(16, found_image->getSize()); | |
| 109 EXPECT_EQ(kMaxGreen, found_image->getColor(0, 0)); | |
| 110 } | |
| 111 | |
| 112 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
| 113 OnDecodeImageFailedTest) { | |
| 114 GURL url("http://localhost"); | |
| 115 | |
| 116 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
| 117 new NotificationBitmapFetcher(url); | |
| 118 | |
| 119 // The fetcher controls the lifetime of url_fetcher, but we keep a | |
| 120 // pointer to it with stub_url_fetcher. | |
| 121 net::TestURLFetcher* stub_url_fetcher = | |
| 122 new net::TestURLFetcher(1, url, fetcher.get()); | |
| 123 scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); | |
| 124 fetcher->SetURLFetcherForTest(url_fetcher); | |
| 125 | |
| 126 // Set up a signal to wait for the bitmap fetch to be done. | |
| 127 content::WindowedNotificationObserver signal( | |
| 128 chrome::NOTIFICATION_NOTIFY_BITMAP_FETCH_COMPLETE, | |
| 129 content::NotificationService::AllSources()); | |
| 130 | |
| 131 // We expect that the image decoder will get called and return | |
| 132 // in a callback to OnImageDecodFailed(). | |
| 133 fetcher->OnURLFetchComplete(stub_url_fetcher); | |
| 134 | |
| 135 // Wait for the callback, test succeeds if callback looks good. | |
| 136 signal.Wait(); | |
| 137 | |
| 138 ASSERT_TRUE(fetcher->image_failed()); | |
| 139 ASSERT_FALSE(fetcher->image_ready()); | |
| 140 | |
| 141 // Make sure we didn't get any bitmap. | |
| 142 SkBitmap* found_image = fetcher->bitmap(); | |
| 143 EXPECT_EQ(NULL, found_image); | |
| 144 } | |
| 145 | |
| 146 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
| 147 OnURLFetchFailureTest) { | |
| 148 GURL url("http://localhost"); | |
| 149 | |
| 150 // We intentionally put no data into the bitmap to simulate a failure. | |
| 151 | |
| 152 // The fetcher controls the lifetime of url_fetcher, but we keep a | |
| 153 // pointer to it with stub_url_fetcher. | |
| 154 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
| 155 new NotificationBitmapFetcher(url); | |
| 156 | |
| 157 // The fetcher controls the lifetime of url_fetcher, but we keep a | |
| 158 // pointer to it with stub_url_fetcher. | |
| 159 net::TestURLFetcher* stub_url_fetcher = | |
| 160 new net::TestURLFetcher(1, url, fetcher.get()); | |
| 161 scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); | |
| 162 fetcher->SetURLFetcherForTest(url_fetcher); | |
| 163 | |
| 164 // Set up a signal to wait for the bitmap fetch to be done. | |
| 165 content::WindowedNotificationObserver signal( | |
| 166 chrome::NOTIFICATION_NOTIFY_BITMAP_FETCH_COMPLETE, | |
| 167 content::NotificationService::AllSources()); | |
| 168 | |
| 169 // We expect that the fetch complete notification will be sent, but that the | |
| 170 // image will be marked as failed. | |
| 171 fetcher->OnURLFetchComplete(stub_url_fetcher); | |
| 172 | |
| 173 // Wait for the callback, test succeeds if callback looks good. | |
| 174 signal.Wait(); | |
| 175 | |
| 176 ASSERT_TRUE(fetcher->image_failed()); | |
| 177 ASSERT_FALSE(fetcher->image_ready()); | |
| 178 | |
| 179 // Make sure we don't get a bitmap. | |
| 180 SkBitmap* found_image = fetcher->bitmap(); | |
| 181 EXPECT_EQ(NULL, found_image); | |
| 182 } | |
| 183 | |
| 184 } // namespace notifier | |
| OLD | NEW |