1 // Copyright 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.
5 #include "chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher.h"
7 #include "base/compiler_specific.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/test/base/in_process_browser_test.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/test/test_utils.h"
12 #include "net/http/http_status_code.h"
13 #include "net/url_request/test_url_fetcher_factory.h"
14 #include "net/url_request/url_fetcher.h"
15 #include "net/url_request/url_request_status.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "ui/gfx/codec/png_codec.h"
19 #include "ui/gfx/size.h"
20 #include "ui/gfx/skia_util.h"
23 const bool kAsyncCall
= true;
24 const bool kSyncCall
= false;
29 // Class to catch events from the NotificationBitmapFetcher for testing.
30 class NotificationBitmapFetcherTestDelegate
31 : public NotificationBitmapFetcherDelegate
{
33 explicit NotificationBitmapFetcherTestDelegate(bool async
)
34 : called_(false), success_(false), async_(async
) {}
36 virtual ~NotificationBitmapFetcherTestDelegate() {
40 // Method inherited from NotificationBitmapFetcherDelegate.
41 virtual void OnFetchComplete(const GURL url
,
42 const SkBitmap
* bitmap
) OVERRIDE
{
47 bitmap
->deepCopyTo(&bitmap_
, bitmap
->getConfig());
49 // For async calls, we need to quit the message loop so the test can
52 base::MessageLoop::current()->Quit();
56 GURL
url() const { return url_
; }
57 bool success() const { return success_
; }
58 const SkBitmap
& bitmap() const { return bitmap_
; }
67 DISALLOW_COPY_AND_ASSIGN(NotificationBitmapFetcherTestDelegate
);
70 class NotificationBitmapFetcherBrowserTest
: public InProcessBrowserTest
{
72 virtual void SetUp() OVERRIDE
{
73 url_fetcher_factory_
.reset(new net::FakeURLFetcherFactory(NULL
));
74 InProcessBrowserTest::SetUp();
78 scoped_ptr
<net::FakeURLFetcherFactory
> url_fetcher_factory_
;
82 #define MAYBE_StartTest DISABLED_StartTest
84 #define MAYBE_StartTest StartTest
87 // WARNING: These tests work with --single_process, but not
88 // --single-process. The reason is that the sandbox does not get created
89 // for us by the test process if --single-process is used.
91 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest
,
93 GURL
url("http://example.com/this-should-work");
95 // Put some realistic looking bitmap data into the url_fetcher.
98 // Put a real bitmap into "image". 2x2 bitmap of green 32 bit pixels.
99 image
.setConfig(SkBitmap::kARGB_8888_Config
, 2, 2);
101 image
.eraseColor(SK_ColorGREEN
);
103 // Encode the bits as a PNG.
104 std::vector
<unsigned char> compressed
;
105 ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image
, true, &compressed
));
107 // Copy the bits into the string, and put them into the FakeURLFetcher.
108 std::string
image_string(compressed
.begin(), compressed
.end());
110 // Set up a delegate to wait for the callback.
111 NotificationBitmapFetcherTestDelegate
delegate(kAsyncCall
);
113 NotificationBitmapFetcher
fetcher(url
, &delegate
);
115 url_fetcher_factory_
->SetFakeResponse(url
, image_string
, net::HTTP_OK
,
116 net::URLRequestStatus::SUCCESS
);
118 // We expect that the image decoder will get called and return
119 // an image in a callback to OnImageDecoded().
120 fetcher
.Start(browser()->profile());
122 // Blocks until test delegate is notified via a callback.
123 content::RunMessageLoop();
125 ASSERT_TRUE(delegate
.success());
127 // Make sure we get back the bitmap we expect.
128 const SkBitmap
& found_image
= delegate
.bitmap();
129 EXPECT_TRUE(gfx::BitmapsAreEqual(image
, found_image
));
132 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest
,
133 OnImageDecodedTest
) {
134 GURL
url("http://example.com/this-should-work-as-well");
137 // Put a real bitmap into "image". 2x2 bitmap of green 16 bit pixels.
138 image
.setConfig(SkBitmap::kARGB_8888_Config
, 2, 2);
140 image
.eraseColor(SK_ColorGREEN
);
142 NotificationBitmapFetcherTestDelegate
delegate(kSyncCall
);
144 NotificationBitmapFetcher
fetcher(url
, &delegate
);
146 fetcher
.OnImageDecoded(NULL
, image
);
148 // Ensure image is marked as succeeded.
149 EXPECT_TRUE(delegate
.success());
151 // Test that the image is what we expect.
152 EXPECT_TRUE(gfx::BitmapsAreEqual(image
, delegate
.bitmap()));
155 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest
,
156 OnURLFetchFailureTest
) {
157 GURL
url("http://example.com/this-should-be-fetch-failure");
159 // We intentionally put no data into the bitmap to simulate a failure.
161 // Set up a delegate to wait for the callback.
162 NotificationBitmapFetcherTestDelegate
delegate(kAsyncCall
);
164 NotificationBitmapFetcher
fetcher(url
, &delegate
);
166 url_fetcher_factory_
->SetFakeResponse(url
,
168 net::HTTP_INTERNAL_SERVER_ERROR
,
169 net::URLRequestStatus::FAILED
);
171 fetcher
.Start(browser()->profile());
173 // Blocks until test delegate is notified via a callback.
174 content::RunMessageLoop();
176 EXPECT_FALSE(delegate
.success());
179 // Flaky on Win XP Debug: crbug.com/316488
180 #if defined(OS_WIN) && !defined(NDEBUG)
181 #define MAYBE_HandleImageFailedTest DISABLED_HandleImageFailedTest
183 #define MAYBE_HandleImageFailedTest HandleImageFailedTest
186 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest
,
187 MAYBE_HandleImageFailedTest
) {
188 GURL
url("http://example.com/this-should-be-a-decode-failure");
189 NotificationBitmapFetcherTestDelegate
delegate(kAsyncCall
);
190 NotificationBitmapFetcher
fetcher(url
, &delegate
);
191 url_fetcher_factory_
->SetFakeResponse(
192 url
, std::string("Not a real bitmap"),
193 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
195 fetcher
.Start(browser()->profile());
197 // Blocks until test delegate is notified via a callback.
198 content::RunMessageLoop();
200 EXPECT_FALSE(delegate
.success());
203 } // namespace notifier