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/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/test/base/in_process_browser_test.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 BitmapFetcher for testing.
30 class BitmapFetcherTestDelegate
: public BitmapFetcherDelegate
{
32 explicit BitmapFetcherTestDelegate(bool async
)
33 : called_(false), success_(false), async_(async
) {}
35 virtual ~BitmapFetcherTestDelegate() { EXPECT_TRUE(called_
); }
37 // Method inherited from BitmapFetcherDelegate.
38 virtual void OnFetchComplete(const GURL url
,
39 const SkBitmap
* bitmap
) OVERRIDE
{
44 bitmap
->deepCopyTo(&bitmap_
);
46 // For async calls, we need to quit the message loop so the test can
49 base::MessageLoop::current()->Quit();
53 GURL
url() const { return url_
; }
54 bool success() const { return success_
; }
55 const SkBitmap
& bitmap() const { return bitmap_
; }
64 DISALLOW_COPY_AND_ASSIGN(BitmapFetcherTestDelegate
);
67 class BitmapFetcherBrowserTest
: public InProcessBrowserTest
{
69 virtual void SetUp() OVERRIDE
{
70 url_fetcher_factory_
.reset(new net::FakeURLFetcherFactory(NULL
));
71 InProcessBrowserTest::SetUp();
75 scoped_ptr
<net::FakeURLFetcherFactory
> url_fetcher_factory_
;
79 #define MAYBE_StartTest DISABLED_StartTest
81 #define MAYBE_StartTest StartTest
84 // WARNING: These tests work with --single_process, but not
85 // --single-process. The reason is that the sandbox does not get created
86 // for us by the test process if --single-process is used.
88 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest
, MAYBE_StartTest
) {
89 GURL
url("http://example.com/this-should-work");
91 // Put some realistic looking bitmap data into the url_fetcher.
94 // Put a real bitmap into "image". 2x2 bitmap of green 32 bit pixels.
95 image
.setConfig(SkBitmap::kARGB_8888_Config
, 2, 2);
97 image
.eraseColor(SK_ColorGREEN
);
99 // Encode the bits as a PNG.
100 std::vector
<unsigned char> compressed
;
101 ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image
, true, &compressed
));
103 // Copy the bits into the string, and put them into the FakeURLFetcher.
104 std::string
image_string(compressed
.begin(), compressed
.end());
106 // Set up a delegate to wait for the callback.
107 BitmapFetcherTestDelegate
delegate(kAsyncCall
);
109 BitmapFetcher
fetcher(url
, &delegate
);
111 url_fetcher_factory_
->SetFakeResponse(
112 url
, image_string
, net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
114 // We expect that the image decoder will get called and return
115 // an image in a callback to OnImageDecoded().
116 fetcher
.Start(browser()->profile()->GetRequestContext());
118 // Blocks until test delegate is notified via a callback.
119 content::RunMessageLoop();
121 ASSERT_TRUE(delegate
.success());
123 // Make sure we get back the bitmap we expect.
124 const SkBitmap
& found_image
= delegate
.bitmap();
125 EXPECT_TRUE(gfx::BitmapsAreEqual(image
, found_image
));
128 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest
, OnImageDecodedTest
) {
129 GURL
url("http://example.com/this-should-work-as-well");
132 // Put a real bitmap into "image". 2x2 bitmap of green 16 bit pixels.
133 image
.setConfig(SkBitmap::kARGB_8888_Config
, 2, 2);
135 image
.eraseColor(SK_ColorGREEN
);
137 BitmapFetcherTestDelegate
delegate(kSyncCall
);
139 BitmapFetcher
fetcher(url
, &delegate
);
141 fetcher
.OnImageDecoded(NULL
, image
);
143 // Ensure image is marked as succeeded.
144 EXPECT_TRUE(delegate
.success());
146 // Test that the image is what we expect.
147 EXPECT_TRUE(gfx::BitmapsAreEqual(image
, delegate
.bitmap()));
150 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest
, OnURLFetchFailureTest
) {
151 GURL
url("http://example.com/this-should-be-fetch-failure");
153 // We intentionally put no data into the bitmap to simulate a failure.
155 // Set up a delegate to wait for the callback.
156 BitmapFetcherTestDelegate
delegate(kAsyncCall
);
158 BitmapFetcher
fetcher(url
, &delegate
);
160 url_fetcher_factory_
->SetFakeResponse(url
,
162 net::HTTP_INTERNAL_SERVER_ERROR
,
163 net::URLRequestStatus::FAILED
);
165 fetcher
.Start(browser()->profile()->GetRequestContext());
167 // Blocks until test delegate is notified via a callback.
168 content::RunMessageLoop();
170 EXPECT_FALSE(delegate
.success());
173 // Flaky on Win XP Debug: crbug.com/316488
174 #if defined(OS_WIN) && !defined(NDEBUG)
175 #define MAYBE_HandleImageFailedTest DISABLED_HandleImageFailedTest
177 #define MAYBE_HandleImageFailedTest HandleImageFailedTest
180 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest
, MAYBE_HandleImageFailedTest
) {
181 GURL
url("http://example.com/this-should-be-a-decode-failure");
182 BitmapFetcherTestDelegate
delegate(kAsyncCall
);
183 BitmapFetcher
fetcher(url
, &delegate
);
184 url_fetcher_factory_
->SetFakeResponse(url
,
185 std::string("Not a real bitmap"),
187 net::URLRequestStatus::SUCCESS
);
189 fetcher
.Start(browser()->profile()->GetRequestContext());
191 // Blocks until test delegate is notified via a callback.
192 content::RunMessageLoop();
194 EXPECT_FALSE(delegate
.success());
197 } // namespace chrome