Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / bitmap_fetcher_browsertest.cc
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.h"
6
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"
20
21 namespace {
22 const bool kAsyncCall = true;
23 const bool kSyncCall = false;
24 }  // namespace
25
26 namespace chrome {
27
28 // Class to catch events from the BitmapFetcher for testing.
29 class BitmapFetcherTestDelegate : public BitmapFetcherDelegate {
30  public:
31   explicit BitmapFetcherTestDelegate(bool async)
32       : called_(false), success_(false), async_(async) {}
33
34   virtual ~BitmapFetcherTestDelegate() { EXPECT_TRUE(called_); }
35
36   // Method inherited from BitmapFetcherDelegate.
37   virtual void OnFetchComplete(const GURL url,
38                                const SkBitmap* bitmap) OVERRIDE {
39     called_ = true;
40     url_ = url;
41     if (NULL != bitmap) {
42       success_ = true;
43       bitmap->deepCopyTo(&bitmap_);
44     }
45     // For async calls, we need to quit the message loop so the test can
46     // continue.
47     if (async_) {
48       base::MessageLoop::current()->Quit();
49     }
50   }
51
52   GURL url() const { return url_; }
53   bool success() const { return success_; }
54   const SkBitmap& bitmap() const { return bitmap_; }
55
56  private:
57   bool called_;
58   GURL url_;
59   bool success_;
60   bool async_;
61   SkBitmap bitmap_;
62
63   DISALLOW_COPY_AND_ASSIGN(BitmapFetcherTestDelegate);
64 };
65
66 class BitmapFetcherBrowserTest : public InProcessBrowserTest {
67  public:
68   virtual void SetUp() OVERRIDE {
69     url_fetcher_factory_.reset(new net::FakeURLFetcherFactory(NULL));
70     InProcessBrowserTest::SetUp();
71   }
72
73  protected:
74   scoped_ptr<net::FakeURLFetcherFactory> url_fetcher_factory_;
75 };
76
77 #if defined(OS_WIN)
78 #define MAYBE_StartTest DISABLED_StartTest
79 #else
80 #define MAYBE_StartTest StartTest
81 #endif
82
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.
86
87 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, MAYBE_StartTest) {
88   GURL url("http://example.com/this-should-work");
89
90   // Put some realistic looking bitmap data into the url_fetcher.
91   SkBitmap image;
92
93   // Put a real bitmap into "image".  2x2 bitmap of green 32 bit pixels.
94   image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
95   image.allocPixels();
96   image.eraseColor(SK_ColorGREEN);
97
98   // Encode the bits as a PNG.
99   std::vector<unsigned char> compressed;
100   ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &compressed));
101
102   // Copy the bits into the string, and put them into the FakeURLFetcher.
103   std::string image_string(compressed.begin(), compressed.end());
104
105   // Set up a delegate to wait for the callback.
106   BitmapFetcherTestDelegate delegate(kAsyncCall);
107
108   BitmapFetcher fetcher(url, &delegate);
109
110   url_fetcher_factory_->SetFakeResponse(
111       url, image_string, net::HTTP_OK, net::URLRequestStatus::SUCCESS);
112
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());
116
117   // Blocks until test delegate is notified via a callback.
118   content::RunMessageLoop();
119
120   ASSERT_TRUE(delegate.success());
121
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));
125 }
126
127 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, OnImageDecodedTest) {
128   GURL url("http://example.com/this-should-work-as-well");
129   SkBitmap image;
130
131   // Put a real bitmap into "image".  2x2 bitmap of green 16 bit pixels.
132   image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
133   image.allocPixels();
134   image.eraseColor(SK_ColorGREEN);
135
136   BitmapFetcherTestDelegate delegate(kSyncCall);
137
138   BitmapFetcher fetcher(url, &delegate);
139
140   fetcher.OnImageDecoded(NULL, image);
141
142   // Ensure image is marked as succeeded.
143   EXPECT_TRUE(delegate.success());
144
145   // Test that the image is what we expect.
146   EXPECT_TRUE(gfx::BitmapsAreEqual(image, delegate.bitmap()));
147 }
148
149 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, OnURLFetchFailureTest) {
150   GURL url("http://example.com/this-should-be-fetch-failure");
151
152   // We intentionally put no data into the bitmap to simulate a failure.
153
154   // Set up a delegate to wait for the callback.
155   BitmapFetcherTestDelegate delegate(kAsyncCall);
156
157   BitmapFetcher fetcher(url, &delegate);
158
159   url_fetcher_factory_->SetFakeResponse(url,
160                                         std::string(),
161                                         net::HTTP_INTERNAL_SERVER_ERROR,
162                                         net::URLRequestStatus::FAILED);
163
164   fetcher.Start(browser()->profile());
165
166   // Blocks until test delegate is notified via a callback.
167   content::RunMessageLoop();
168
169   EXPECT_FALSE(delegate.success());
170 }
171
172 // Flaky on Win XP Debug: crbug.com/316488
173 #if defined(OS_WIN) && !defined(NDEBUG)
174 #define MAYBE_HandleImageFailedTest DISABLED_HandleImageFailedTest
175 #else
176 #define MAYBE_HandleImageFailedTest HandleImageFailedTest
177 #endif
178
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"),
185                                         net::HTTP_OK,
186                                         net::URLRequestStatus::SUCCESS);
187
188   fetcher.Start(browser()->profile());
189
190   // Blocks until test delegate is notified via a callback.
191   content::RunMessageLoop();
192
193   EXPECT_FALSE(delegate.success());
194 }
195
196 }  // namespace chrome