Home | History | Annotate | Download | only in sync_notifier
      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/url_request/test_url_fetcher_factory.h"
     13 #include "net/url_request/url_fetcher.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 #include "third_party/skia/include/core/SkBitmap.h"
     16 #include "ui/gfx/codec/png_codec.h"
     17 #include "ui/gfx/size.h"
     18 #include "ui/gfx/skia_util.h"
     19 
     20 namespace {
     21 const bool kAsyncCall = true;
     22 const bool kSyncCall = false;
     23 }  // namespace
     24 
     25 namespace notifier {
     26 
     27 // Class to catch events from the NotificationBitmapFetcher for testing.
     28 class NotificationBitmapFetcherTestDelegate
     29     : public NotificationBitmapFetcherDelegate {
     30  public:
     31   explicit NotificationBitmapFetcherTestDelegate(bool async)
     32       : called_(false), success_(false), async_(async) {}
     33 
     34   virtual ~NotificationBitmapFetcherTestDelegate() {
     35     EXPECT_TRUE(called_);
     36   }
     37 
     38   // Method inherited from NotificationBitmapFetcherDelegate.
     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_, bitmap->getConfig());
     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 
     54   GURL url() const { return url_; }
     55   bool success() const { return success_; }
     56   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(NotificationBitmapFetcherTestDelegate);
     66 };
     67 
     68 class NotificationBitmapFetcherBrowserTest : public InProcessBrowserTest {
     69  public:
     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 // WARNING:  These tests work with --single_process, but not
     80 // --single-process.  The reason is that the sandbox does not get created
     81 // for us by the test process if --single-process is used.
     82 
     83 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest,
     84                        StartTest) {
     85   GURL url("http://example.com/this-should-work");
     86 
     87   // Put some realistic looking bitmap data into the url_fetcher.
     88   SkBitmap image;
     89 
     90   // Put a real bitmap into "image".  2x2 bitmap of green 32 bit pixels.
     91   image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
     92   image.allocPixels();
     93   image.eraseColor(SK_ColorGREEN);
     94 
     95   // Encode the bits as a PNG.
     96   std::vector<unsigned char> compressed;
     97   ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &compressed));
     98 
     99   // Copy the bits into the string, and put them into the FakeURLFetcher.
    100   std::string image_string(compressed.begin(), compressed.end());
    101 
    102   // Set up a delegate to wait for the callback.
    103   NotificationBitmapFetcherTestDelegate delegate(kAsyncCall);
    104 
    105   NotificationBitmapFetcher fetcher(url, &delegate);
    106 
    107   url_fetcher_factory_->SetFakeResponse(url.spec(), image_string, true);
    108 
    109   // We expect that the image decoder will get called and return
    110   // an image in a callback to OnImageDecoded().
    111   fetcher.Start(browser()->profile());
    112 
    113   // Blocks until test delegate is notified via a callback.
    114   content::RunMessageLoop();
    115 
    116   ASSERT_TRUE(delegate.success());
    117 
    118   // Make sure we get back the bitmap we expect.
    119   const SkBitmap& found_image = delegate.bitmap();
    120   EXPECT_TRUE(gfx::BitmapsAreEqual(image, found_image));
    121 }
    122 
    123 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest,
    124                        OnImageDecodedTest) {
    125   GURL url("http://example.com/this-should-work-as-well");
    126   SkBitmap image;
    127 
    128   // Put a real bitmap into "image".  2x2 bitmap of green 16 bit pixels.
    129   image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
    130   image.allocPixels();
    131   image.eraseColor(SK_ColorGREEN);
    132 
    133   NotificationBitmapFetcherTestDelegate delegate(kSyncCall);
    134 
    135   NotificationBitmapFetcher fetcher(url, &delegate);
    136 
    137   fetcher.OnImageDecoded(NULL, image);
    138 
    139   // Ensure image is marked as succeeded.
    140   EXPECT_TRUE(delegate.success());
    141 
    142   // Test that the image is what we expect.
    143   EXPECT_TRUE(gfx::BitmapsAreEqual(image, delegate.bitmap()));
    144 }
    145 
    146 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest,
    147                        OnURLFetchFailureTest) {
    148   GURL url("http://example.com/this-should-be-fetch-failure");
    149 
    150   // We intentionally put no data into the bitmap to simulate a failure.
    151 
    152   // Set up a delegate to wait for the callback.
    153   NotificationBitmapFetcherTestDelegate delegate(kAsyncCall);
    154 
    155   NotificationBitmapFetcher fetcher(url, &delegate);
    156 
    157   url_fetcher_factory_->SetFakeResponse(url.spec(), std::string(), false);
    158 
    159   fetcher.Start(browser()->profile());
    160 
    161   // Blocks until test delegate is notified via a callback.
    162   content::RunMessageLoop();
    163 
    164   EXPECT_FALSE(delegate.success());
    165 }
    166 
    167 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest,
    168                        HandleImageFailedTest) {
    169   GURL url("http://example.com/this-should-be-a-decode-failure");
    170   NotificationBitmapFetcherTestDelegate delegate(kAsyncCall);
    171   NotificationBitmapFetcher fetcher(url, &delegate);
    172   url_fetcher_factory_->SetFakeResponse(
    173       url.spec(), std::string("Not a real bitmap"), true);
    174 
    175   fetcher.Start(browser()->profile());
    176 
    177   // Blocks until test delegate is notified via a callback.
    178   content::RunMessageLoop();
    179 
    180   EXPECT_FALSE(delegate.success());
    181 }
    182 
    183 }  // namespace notifier
    184