- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / sync_notifier / notification_bitmap_fetcher_browsertest.cc
1 // Copyright 2013 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/notifications/sync_notifier/notification_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/browser/browser_thread.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 "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 notifier {
27
28 // Class to catch events from the NotificationBitmapFetcher for testing.
29 class NotificationBitmapFetcherTestDelegate
30     : public NotificationBitmapFetcherDelegate {
31  public:
32   explicit NotificationBitmapFetcherTestDelegate(bool async)
33       : called_(false), success_(false), async_(async) {}
34
35   virtual ~NotificationBitmapFetcherTestDelegate() {
36     EXPECT_TRUE(called_);
37   }
38
39   // Method inherited from NotificationBitmapFetcherDelegate.
40   virtual void OnFetchComplete(const GURL url,
41                                const SkBitmap* bitmap) OVERRIDE {
42     called_ = true;
43     url_ = url;
44     if (NULL != bitmap) {
45       success_ = true;
46       bitmap->deepCopyTo(&bitmap_, bitmap->getConfig());
47     }
48     // For async calls, we need to quit the message loop so the test can
49     // continue.
50     if (async_) {
51       base::MessageLoop::current()->Quit();
52     }
53   }
54
55   GURL url() const { return url_; }
56   bool success() const { return success_; }
57   const SkBitmap& bitmap() const { return bitmap_; }
58
59  private:
60   bool called_;
61   GURL url_;
62   bool success_;
63   bool async_;
64   SkBitmap bitmap_;
65
66   DISALLOW_COPY_AND_ASSIGN(NotificationBitmapFetcherTestDelegate);
67 };
68
69 class NotificationBitmapFetcherBrowserTest : public InProcessBrowserTest {
70  public:
71   virtual void SetUp() OVERRIDE {
72     url_fetcher_factory_.reset(new net::FakeURLFetcherFactory(NULL));
73     InProcessBrowserTest::SetUp();
74   }
75
76  protected:
77   scoped_ptr<net::FakeURLFetcherFactory> url_fetcher_factory_;
78 };
79
80 #if defined(OS_WIN)
81 #define MAYBE_StartTest DISABLED_StartTest
82 #else
83 #define MAYBE_StartTest StartTest
84 #endif
85
86 // WARNING:  These tests work with --single_process, but not
87 // --single-process.  The reason is that the sandbox does not get created
88 // for us by the test process if --single-process is used.
89
90 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest,
91                        MAYBE_StartTest) {
92   GURL url("http://example.com/this-should-work");
93
94   // Put some realistic looking bitmap data into the url_fetcher.
95   SkBitmap image;
96
97   // Put a real bitmap into "image".  2x2 bitmap of green 32 bit pixels.
98   image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
99   image.allocPixels();
100   image.eraseColor(SK_ColorGREEN);
101
102   // Encode the bits as a PNG.
103   std::vector<unsigned char> compressed;
104   ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &compressed));
105
106   // Copy the bits into the string, and put them into the FakeURLFetcher.
107   std::string image_string(compressed.begin(), compressed.end());
108
109   // Set up a delegate to wait for the callback.
110   NotificationBitmapFetcherTestDelegate delegate(kAsyncCall);
111
112   NotificationBitmapFetcher fetcher(url, &delegate);
113
114   url_fetcher_factory_->SetFakeResponse(url, image_string, net::HTTP_OK);
115
116   // We expect that the image decoder will get called and return
117   // an image in a callback to OnImageDecoded().
118   fetcher.Start(browser()->profile());
119
120   // Blocks until test delegate is notified via a callback.
121   content::RunMessageLoop();
122
123   ASSERT_TRUE(delegate.success());
124
125   // Make sure we get back the bitmap we expect.
126   const SkBitmap& found_image = delegate.bitmap();
127   EXPECT_TRUE(gfx::BitmapsAreEqual(image, found_image));
128 }
129
130 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest,
131                        OnImageDecodedTest) {
132   GURL url("http://example.com/this-should-work-as-well");
133   SkBitmap image;
134
135   // Put a real bitmap into "image".  2x2 bitmap of green 16 bit pixels.
136   image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
137   image.allocPixels();
138   image.eraseColor(SK_ColorGREEN);
139
140   NotificationBitmapFetcherTestDelegate delegate(kSyncCall);
141
142   NotificationBitmapFetcher 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(NotificationBitmapFetcherBrowserTest,
154                        OnURLFetchFailureTest) {
155   GURL url("http://example.com/this-should-be-fetch-failure");
156
157   // We intentionally put no data into the bitmap to simulate a failure.
158
159   // Set up a delegate to wait for the callback.
160   NotificationBitmapFetcherTestDelegate delegate(kAsyncCall);
161
162   NotificationBitmapFetcher fetcher(url, &delegate);
163
164   url_fetcher_factory_->SetFakeResponse(url,
165                                         std::string(),
166                                         net::HTTP_INTERNAL_SERVER_ERROR);
167
168   fetcher.Start(browser()->profile());
169
170   // Blocks until test delegate is notified via a callback.
171   content::RunMessageLoop();
172
173   EXPECT_FALSE(delegate.success());
174 }
175
176 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest,
177                        HandleImageFailedTest) {
178   GURL url("http://example.com/this-should-be-a-decode-failure");
179   NotificationBitmapFetcherTestDelegate delegate(kAsyncCall);
180   NotificationBitmapFetcher fetcher(url, &delegate);
181   url_fetcher_factory_->SetFakeResponse(
182       url, std::string("Not a real bitmap"), net::HTTP_OK);
183
184   fetcher.Start(browser()->profile());
185
186   // Blocks until test delegate is notified via a callback.
187   content::RunMessageLoop();
188
189   EXPECT_FALSE(delegate.success());
190 }
191
192 }  // namespace notifier