• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"
6 
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/base/load_flags.h"
13 #include "net/http/http_status_code.h"
14 #include "net/url_request/test_url_fetcher_factory.h"
15 #include "net/url_request/url_fetcher.h"
16 #include "net/url_request/url_request_status.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "ui/gfx/codec/png_codec.h"
20 #include "ui/gfx/size.h"
21 #include "ui/gfx/skia_util.h"
22 
23 namespace {
24 const bool kAsyncCall = true;
25 const bool kSyncCall = false;
26 }  // namespace
27 
28 namespace chrome {
29 
30 // Class to catch events from the BitmapFetcher for testing.
31 class BitmapFetcherTestDelegate : public BitmapFetcherDelegate {
32  public:
BitmapFetcherTestDelegate(bool async)33   explicit BitmapFetcherTestDelegate(bool async)
34       : called_(false), success_(false), async_(async) {}
35 
~BitmapFetcherTestDelegate()36   virtual ~BitmapFetcherTestDelegate() { EXPECT_TRUE(called_); }
37 
38   // Method inherited from BitmapFetcherDelegate.
OnFetchComplete(const GURL url,const SkBitmap * bitmap)39   virtual void OnFetchComplete(const GURL url,
40                                const SkBitmap* bitmap) OVERRIDE {
41     called_ = true;
42     url_ = url;
43     if (NULL != bitmap) {
44       success_ = true;
45       bitmap->deepCopyTo(&bitmap_);
46     }
47     // For async calls, we need to quit the message loop so the test can
48     // continue.
49     if (async_) {
50       base::MessageLoop::current()->Quit();
51     }
52   }
53 
url() const54   GURL url() const { return url_; }
success() const55   bool success() const { return success_; }
bitmap() const56   const SkBitmap& bitmap() const { return bitmap_; }
57 
58  private:
59   bool called_;
60   GURL url_;
61   bool success_;
62   bool async_;
63   SkBitmap bitmap_;
64 
65   DISALLOW_COPY_AND_ASSIGN(BitmapFetcherTestDelegate);
66 };
67 
68 class BitmapFetcherBrowserTest : public InProcessBrowserTest {
69  public:
SetUp()70   virtual void SetUp() OVERRIDE {
71     url_fetcher_factory_.reset(new net::FakeURLFetcherFactory(NULL));
72     InProcessBrowserTest::SetUp();
73   }
74 
75  protected:
76   scoped_ptr<net::FakeURLFetcherFactory> url_fetcher_factory_;
77 };
78 
79 #if defined(OS_WIN)
80 #define MAYBE_StartTest DISABLED_StartTest
81 #else
82 #define MAYBE_StartTest StartTest
83 #endif
84 
85 // WARNING:  These tests work with --single_process, but not
86 // --single-process.  The reason is that the sandbox does not get created
87 // for us by the test process if --single-process is used.
88 
IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest,MAYBE_StartTest)89 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, MAYBE_StartTest) {
90   GURL url("http://example.com/this-should-work");
91 
92   // Put some realistic looking bitmap data into the url_fetcher.
93   SkBitmap image;
94 
95   // Put a real bitmap into "image".  2x2 bitmap of green 32 bit pixels.
96   image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
97   image.allocPixels();
98   image.eraseColor(SK_ColorGREEN);
99 
100   // Encode the bits as a PNG.
101   std::vector<unsigned char> compressed;
102   ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &compressed));
103 
104   // Copy the bits into the string, and put them into the FakeURLFetcher.
105   std::string image_string(compressed.begin(), compressed.end());
106 
107   // Set up a delegate to wait for the callback.
108   BitmapFetcherTestDelegate delegate(kAsyncCall);
109 
110   BitmapFetcher fetcher(url, &delegate);
111 
112   url_fetcher_factory_->SetFakeResponse(
113       url, image_string, net::HTTP_OK, net::URLRequestStatus::SUCCESS);
114 
115   // We expect that the image decoder will get called and return
116   // an image in a callback to OnImageDecoded().
117   fetcher.Start(
118       browser()->profile()->GetRequestContext(),
119       std::string(),
120       net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
121       net::LOAD_NORMAL);
122 
123   // Blocks until test delegate is notified via a callback.
124   content::RunMessageLoop();
125 
126   ASSERT_TRUE(delegate.success());
127 
128   // Make sure we get back the bitmap we expect.
129   const SkBitmap& found_image = delegate.bitmap();
130   EXPECT_TRUE(gfx::BitmapsAreEqual(image, found_image));
131 }
132 
IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest,OnImageDecodedTest)133 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, OnImageDecodedTest) {
134   GURL url("http://example.com/this-should-work-as-well");
135   SkBitmap image;
136 
137   // Put a real bitmap into "image".  2x2 bitmap of green 16 bit pixels.
138   image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
139   image.allocPixels();
140   image.eraseColor(SK_ColorGREEN);
141 
142   BitmapFetcherTestDelegate delegate(kSyncCall);
143 
144   BitmapFetcher fetcher(url, &delegate);
145 
146   fetcher.OnImageDecoded(NULL, image);
147 
148   // Ensure image is marked as succeeded.
149   EXPECT_TRUE(delegate.success());
150 
151   // Test that the image is what we expect.
152   EXPECT_TRUE(gfx::BitmapsAreEqual(image, delegate.bitmap()));
153 }
154 
IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest,OnURLFetchFailureTest)155 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, OnURLFetchFailureTest) {
156   GURL url("http://example.com/this-should-be-fetch-failure");
157 
158   // We intentionally put no data into the bitmap to simulate a failure.
159 
160   // Set up a delegate to wait for the callback.
161   BitmapFetcherTestDelegate delegate(kAsyncCall);
162 
163   BitmapFetcher fetcher(url, &delegate);
164 
165   url_fetcher_factory_->SetFakeResponse(url,
166                                         std::string(),
167                                         net::HTTP_INTERNAL_SERVER_ERROR,
168                                         net::URLRequestStatus::FAILED);
169 
170   fetcher.Start(
171       browser()->profile()->GetRequestContext(),
172       std::string(),
173       net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
174       net::LOAD_NORMAL);
175 
176   // Blocks until test delegate is notified via a callback.
177   content::RunMessageLoop();
178 
179   EXPECT_FALSE(delegate.success());
180 }
181 
182 // Flaky on Win XP Debug: crbug.com/316488
183 #if defined(OS_WIN) && !defined(NDEBUG)
184 #define MAYBE_HandleImageFailedTest DISABLED_HandleImageFailedTest
185 #else
186 #define MAYBE_HandleImageFailedTest HandleImageFailedTest
187 #endif
188 
IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest,MAYBE_HandleImageFailedTest)189 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, MAYBE_HandleImageFailedTest) {
190   GURL url("http://example.com/this-should-be-a-decode-failure");
191   BitmapFetcherTestDelegate delegate(kAsyncCall);
192   BitmapFetcher fetcher(url, &delegate);
193   url_fetcher_factory_->SetFakeResponse(url,
194                                         std::string("Not a real bitmap"),
195                                         net::HTTP_OK,
196                                         net::URLRequestStatus::SUCCESS);
197 
198   fetcher.Start(
199       browser()->profile()->GetRequestContext(),
200       std::string(),
201       net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
202       net::LOAD_NORMAL);
203 
204   // Blocks until test delegate is notified via a callback.
205   content::RunMessageLoop();
206 
207   EXPECT_FALSE(delegate.success());
208 }
209 
210 }  // namespace chrome
211