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.
5 #include "chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher.h"
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"
22 const bool kAsyncCall = true;
23 const bool kSyncCall = false;
28 // Class to catch events from the NotificationBitmapFetcher for testing.
29 class NotificationBitmapFetcherTestDelegate
30 : public NotificationBitmapFetcherDelegate {
32 explicit NotificationBitmapFetcherTestDelegate(bool async)
33 : called_(false), success_(false), async_(async) {}
35 virtual ~NotificationBitmapFetcherTestDelegate() {
39 // Method inherited from NotificationBitmapFetcherDelegate.
40 virtual void OnFetchComplete(const GURL url,
41 const SkBitmap* bitmap) OVERRIDE {
46 bitmap->deepCopyTo(&bitmap_, bitmap->getConfig());
48 // For async calls, we need to quit the message loop so the test can
51 base::MessageLoop::current()->Quit();
55 GURL url() const { return url_; }
56 bool success() const { return success_; }
57 const SkBitmap& bitmap() const { return bitmap_; }
66 DISALLOW_COPY_AND_ASSIGN(NotificationBitmapFetcherTestDelegate);
69 class NotificationBitmapFetcherBrowserTest : public InProcessBrowserTest {
71 virtual void SetUp() OVERRIDE {
72 url_fetcher_factory_.reset(new net::FakeURLFetcherFactory(NULL));
73 InProcessBrowserTest::SetUp();
77 scoped_ptr<net::FakeURLFetcherFactory> url_fetcher_factory_;
81 #define MAYBE_StartTest DISABLED_StartTest
83 #define MAYBE_StartTest StartTest
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.
90 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest,
92 GURL url("http://example.com/this-should-work");
94 // Put some realistic looking bitmap data into the url_fetcher.
97 // Put a real bitmap into "image". 2x2 bitmap of green 32 bit pixels.
98 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
100 image.eraseColor(SK_ColorGREEN);
102 // Encode the bits as a PNG.
103 std::vector<unsigned char> compressed;
104 ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &compressed));
106 // Copy the bits into the string, and put them into the FakeURLFetcher.
107 std::string image_string(compressed.begin(), compressed.end());
109 // Set up a delegate to wait for the callback.
110 NotificationBitmapFetcherTestDelegate delegate(kAsyncCall);
112 NotificationBitmapFetcher fetcher(url, &delegate);
114 url_fetcher_factory_->SetFakeResponse(url, image_string, net::HTTP_OK);
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());
120 // Blocks until test delegate is notified via a callback.
121 content::RunMessageLoop();
123 ASSERT_TRUE(delegate.success());
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));
130 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest,
131 OnImageDecodedTest) {
132 GURL url("http://example.com/this-should-work-as-well");
135 // Put a real bitmap into "image". 2x2 bitmap of green 16 bit pixels.
136 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
138 image.eraseColor(SK_ColorGREEN);
140 NotificationBitmapFetcherTestDelegate delegate(kSyncCall);
142 NotificationBitmapFetcher fetcher(url, &delegate);
144 fetcher.OnImageDecoded(NULL, image);
146 // Ensure image is marked as succeeded.
147 EXPECT_TRUE(delegate.success());
149 // Test that the image is what we expect.
150 EXPECT_TRUE(gfx::BitmapsAreEqual(image, delegate.bitmap()));
153 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest,
154 OnURLFetchFailureTest) {
155 GURL url("http://example.com/this-should-be-fetch-failure");
157 // We intentionally put no data into the bitmap to simulate a failure.
159 // Set up a delegate to wait for the callback.
160 NotificationBitmapFetcherTestDelegate delegate(kAsyncCall);
162 NotificationBitmapFetcher fetcher(url, &delegate);
164 url_fetcher_factory_->SetFakeResponse(url,
166 net::HTTP_INTERNAL_SERVER_ERROR);
168 fetcher.Start(browser()->profile());
170 // Blocks until test delegate is notified via a callback.
171 content::RunMessageLoop();
173 EXPECT_FALSE(delegate.success());
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);
184 fetcher.Start(browser()->profile());
186 // Blocks until test delegate is notified via a callback.
187 content::RunMessageLoop();
189 EXPECT_FALSE(delegate.success());
192 } // namespace notifier