Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / bitmap_fetcher / 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/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 const bool kAsyncCall = true;
24 const bool kSyncCall = false;
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) : called_(false),
32                                                    success_(false),
33                                                    async_(async) {}
34
35   ~BitmapFetcherTestDelegate() override { EXPECT_TRUE(called_); }
36
37   // Method inherited from BitmapFetcherDelegate.
38   void OnFetchComplete(const GURL url, const SkBitmap* bitmap) override {
39     called_ = true;
40     url_ = url;
41     if (bitmap) {
42       success_ = true;
43       bitmap->deepCopyTo(&bitmap_);
44     }
45     // For async calls, we need to quit the run loop so the test can continue.
46     if (async_)
47       run_loop_.Quit();
48   }
49
50   // Waits until OnFetchComplete() is called. Should only be used for
51   // async tests.
52   void Wait() {
53     ASSERT_TRUE(async_);
54     run_loop_.Run();
55   }
56
57   GURL url() const { return url_; }
58   bool success() const { return success_; }
59   const SkBitmap& bitmap() const { return bitmap_; }
60
61  private:
62   base::RunLoop run_loop_;
63   bool called_;
64   GURL url_;
65   bool success_;
66   bool async_;
67   SkBitmap bitmap_;
68
69   DISALLOW_COPY_AND_ASSIGN(BitmapFetcherTestDelegate);
70 };
71
72 class BitmapFetcherBrowserTest : public InProcessBrowserTest {
73  public:
74   void SetUp() override {
75     url_fetcher_factory_.reset(
76         new net::FakeURLFetcherFactory(&url_fetcher_impl_factory_));
77     InProcessBrowserTest::SetUp();
78   }
79
80  protected:
81   net::URLFetcherImplFactory url_fetcher_impl_factory_;
82   scoped_ptr<net::FakeURLFetcherFactory> url_fetcher_factory_;
83 };
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
89 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, 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.allocN32Pixels(2, 2);
97   image.eraseColor(SK_ColorGREEN);
98
99   // Encode the bits as a PNG.
100   std::vector<unsigned char> compressed;
101   ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &compressed));
102
103   // Copy the bits into the string, and put them into the FakeURLFetcher.
104   std::string image_string(compressed.begin(), compressed.end());
105
106   // Set up a delegate to wait for the callback.
107   BitmapFetcherTestDelegate delegate(kAsyncCall);
108
109   BitmapFetcher fetcher(url, &delegate);
110
111   url_fetcher_factory_->SetFakeResponse(
112       url, image_string, net::HTTP_OK, net::URLRequestStatus::SUCCESS);
113
114   // We expect that the image decoder will get called and return
115   // an image in a callback to OnImageDecoded().
116   fetcher.Start(
117       browser()->profile()->GetRequestContext(),
118       std::string(),
119       net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
120       net::LOAD_NORMAL);
121
122   // Blocks until test delegate is notified via a callback.
123   delegate.Wait();
124
125   ASSERT_TRUE(delegate.success());
126
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));
130 }
131
132 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, OnImageDecodedTest) {
133   GURL url("http://example.com/this-should-work-as-well");
134   SkBitmap image;
135
136   // Put a real bitmap into "image".  2x2 bitmap of green 16 bit pixels.
137   image.allocN32Pixels(2, 2);
138   image.eraseColor(SK_ColorGREEN);
139
140   BitmapFetcherTestDelegate delegate(kSyncCall);
141
142   BitmapFetcher fetcher(url, &delegate);
143
144   fetcher.OnImageDecoded(NULL, image);
145
146   // Ensure image is marked as succeeded.
147   EXPECT_TRUE(delegate.success());
148
149   // Test that the image is what we expect.
150   EXPECT_TRUE(gfx::BitmapsAreEqual(image, delegate.bitmap()));
151 }
152
153 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, OnURLFetchFailureTest) {
154   GURL url("http://example.com/this-should-be-fetch-failure");
155
156   // We intentionally put no data into the bitmap to simulate a failure.
157
158   // Set up a delegate to wait for the callback.
159   BitmapFetcherTestDelegate delegate(kAsyncCall);
160
161   BitmapFetcher fetcher(url, &delegate);
162
163   url_fetcher_factory_->SetFakeResponse(url,
164                                         std::string(),
165                                         net::HTTP_INTERNAL_SERVER_ERROR,
166                                         net::URLRequestStatus::FAILED);
167
168   fetcher.Start(
169       browser()->profile()->GetRequestContext(),
170       std::string(),
171       net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
172       net::LOAD_NORMAL);
173
174   // Blocks until test delegate is notified via a callback.
175   delegate.Wait();
176
177   EXPECT_FALSE(delegate.success());
178 }
179
180 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, 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"),
186                                         net::HTTP_OK,
187                                         net::URLRequestStatus::SUCCESS);
188
189   fetcher.Start(
190       browser()->profile()->GetRequestContext(),
191       std::string(),
192       net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
193       net::LOAD_NORMAL);
194
195   // Blocks until test delegate is notified via a callback.
196   delegate.Wait();
197
198   EXPECT_FALSE(delegate.success());
199 }
200
201 }  // namespace chrome