1 // Copyright 2014 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/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/test/test_utils.h"
11 #include "net/http/http_status_code.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 #include "ui/gfx/skia_util.h"
22 const bool kAsyncCall
= true;
23 const bool kSyncCall
= false;
28 // Class to catch events from the BitmapFetcher for testing.
29 class BitmapFetcherTestDelegate
: public BitmapFetcherDelegate
{
31 explicit BitmapFetcherTestDelegate(bool async
)
32 : called_(false), success_(false), async_(async
) {}
34 virtual ~BitmapFetcherTestDelegate() { EXPECT_TRUE(called_
); }
36 // Method inherited from BitmapFetcherDelegate.
37 virtual void OnFetchComplete(const GURL url
,
38 const SkBitmap
* bitmap
) OVERRIDE
{
43 bitmap
->deepCopyTo(&bitmap_
, bitmap
->getConfig());
45 // For async calls, we need to quit the message loop so the test can
48 base::MessageLoop::current()->Quit();
52 GURL
url() const { return url_
; }
53 bool success() const { return success_
; }
54 const SkBitmap
& bitmap() const { return bitmap_
; }
63 DISALLOW_COPY_AND_ASSIGN(BitmapFetcherTestDelegate
);
66 class BitmapFetcherBrowserTest
: public InProcessBrowserTest
{
68 virtual void SetUp() OVERRIDE
{
69 url_fetcher_factory_
.reset(new net::FakeURLFetcherFactory(NULL
));
70 InProcessBrowserTest::SetUp();
74 scoped_ptr
<net::FakeURLFetcherFactory
> url_fetcher_factory_
;
78 #define MAYBE_StartTest DISABLED_StartTest
80 #define MAYBE_StartTest StartTest
83 // WARNING: These tests work with --single_process, but not
84 // --single-process. The reason is that the sandbox does not get created
85 // for us by the test process if --single-process is used.
87 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest
, MAYBE_StartTest
) {
88 GURL
url("http://example.com/this-should-work");
90 // Put some realistic looking bitmap data into the url_fetcher.
93 // Put a real bitmap into "image". 2x2 bitmap of green 32 bit pixels.
94 image
.setConfig(SkBitmap::kARGB_8888_Config
, 2, 2);
96 image
.eraseColor(SK_ColorGREEN
);
98 // Encode the bits as a PNG.
99 std::vector
<unsigned char> compressed
;
100 ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image
, true, &compressed
));
102 // Copy the bits into the string, and put them into the FakeURLFetcher.
103 std::string
image_string(compressed
.begin(), compressed
.end());
105 // Set up a delegate to wait for the callback.
106 BitmapFetcherTestDelegate
delegate(kAsyncCall
);
108 BitmapFetcher
fetcher(url
, &delegate
);
110 url_fetcher_factory_
->SetFakeResponse(
111 url
, image_string
, net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
113 // We expect that the image decoder will get called and return
114 // an image in a callback to OnImageDecoded().
115 fetcher
.Start(browser()->profile());
117 // Blocks until test delegate is notified via a callback.
118 content::RunMessageLoop();
120 ASSERT_TRUE(delegate
.success());
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
));
127 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest
, OnImageDecodedTest
) {
128 GURL
url("http://example.com/this-should-work-as-well");
131 // Put a real bitmap into "image". 2x2 bitmap of green 16 bit pixels.
132 image
.setConfig(SkBitmap::kARGB_8888_Config
, 2, 2);
134 image
.eraseColor(SK_ColorGREEN
);
136 BitmapFetcherTestDelegate
delegate(kSyncCall
);
138 BitmapFetcher
fetcher(url
, &delegate
);
140 fetcher
.OnImageDecoded(NULL
, image
);
142 // Ensure image is marked as succeeded.
143 EXPECT_TRUE(delegate
.success());
145 // Test that the image is what we expect.
146 EXPECT_TRUE(gfx::BitmapsAreEqual(image
, delegate
.bitmap()));
149 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest
, OnURLFetchFailureTest
) {
150 GURL
url("http://example.com/this-should-be-fetch-failure");
152 // We intentionally put no data into the bitmap to simulate a failure.
154 // Set up a delegate to wait for the callback.
155 BitmapFetcherTestDelegate
delegate(kAsyncCall
);
157 BitmapFetcher
fetcher(url
, &delegate
);
159 url_fetcher_factory_
->SetFakeResponse(url
,
161 net::HTTP_INTERNAL_SERVER_ERROR
,
162 net::URLRequestStatus::FAILED
);
164 fetcher
.Start(browser()->profile());
166 // Blocks until test delegate is notified via a callback.
167 content::RunMessageLoop();
169 EXPECT_FALSE(delegate
.success());
172 // Flaky on Win XP Debug: crbug.com/316488
173 #if defined(OS_WIN) && !defined(NDEBUG)
174 #define MAYBE_HandleImageFailedTest DISABLED_HandleImageFailedTest
176 #define MAYBE_HandleImageFailedTest HandleImageFailedTest
179 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest
, MAYBE_HandleImageFailedTest
) {
180 GURL
url("http://example.com/this-should-be-a-decode-failure");
181 BitmapFetcherTestDelegate
delegate(kAsyncCall
);
182 BitmapFetcher
fetcher(url
, &delegate
);
183 url_fetcher_factory_
->SetFakeResponse(url
,
184 std::string("Not a real bitmap"),
186 net::URLRequestStatus::SUCCESS
);
188 fetcher
.Start(browser()->profile());
190 // Blocks until test delegate is notified via a callback.
191 content::RunMessageLoop();
193 EXPECT_FALSE(delegate
.success());
196 } // namespace chrome