Home | History | Annotate | Download | only in browser
      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 <string>
      6 
      7 #include "chrome/browser/image_holder.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 
     10 namespace {
     11 
     12 const char kIconUrl1[] = "http://www.google.com/icon1.jpg";
     13 const char kIconUrl2[] = "http://www.google.com/icon2.jpg";
     14 
     15 class TestDelegate : public chrome::ImageHolderDelegate {
     16  public:
     17   TestDelegate() : on_fetch_complete_called_(false) {}
     18   virtual void OnFetchComplete() OVERRIDE {
     19     on_fetch_complete_called_ = true;
     20   }
     21   bool on_fetch_complete_called_;
     22 };
     23 
     24 }  // namespace.
     25 
     26 namespace chrome {
     27 
     28 typedef testing::Test ImageHolderTest;
     29 
     30 TEST_F(ImageHolderTest, CreateBitmapFetcherTest) {
     31   TestDelegate delegate;
     32   ImageHolder image_holder(GURL(kIconUrl1), GURL(kIconUrl2), NULL, &delegate);
     33 
     34   EXPECT_EQ(GURL(kIconUrl1), image_holder.fetchers_[0]->url());
     35   EXPECT_EQ(GURL(kIconUrl2), image_holder.fetchers_[1]->url());
     36   EXPECT_EQ(static_cast<unsigned int>(2), image_holder.fetchers_.size());
     37 
     38   // Adding a dup of an existing URL shouldn't change anything.
     39   image_holder.CreateBitmapFetcher(GURL(kIconUrl2));
     40   EXPECT_EQ(GURL(kIconUrl1), image_holder.fetchers_[0]->url());
     41   EXPECT_EQ(GURL(kIconUrl2), image_holder.fetchers_[1]->url());
     42   EXPECT_EQ(static_cast<unsigned int>(2), image_holder.fetchers_.size());
     43 }
     44 
     45 TEST_F(ImageHolderTest, OnFetchCompleteTest) {
     46   TestDelegate delegate;
     47   ImageHolder image_holder(GURL(kIconUrl1), GURL(), NULL, &delegate);
     48 
     49   // Put a real bitmap into "bitmap".  2x2 bitmap of green 32 bit pixels.
     50   SkBitmap bitmap;
     51   bitmap.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
     52   bitmap.allocPixels();
     53   bitmap.eraseColor(SK_ColorGREEN);
     54 
     55   image_holder.OnFetchComplete(GURL(kIconUrl1), &bitmap);
     56 
     57   // Expect that the app icon has some data in it.
     58   EXPECT_FALSE(image_holder.low_dpi_image().IsEmpty());
     59 
     60   // Expect that we reported the fetch done to the delegate.
     61   EXPECT_TRUE(delegate.on_fetch_complete_called_);
     62 }
     63 
     64 TEST_F(ImageHolderTest, IsFetchingDoneTest) {
     65   TestDelegate delegate;
     66   ImageHolder image_holder1(GURL(kIconUrl1), GURL(kIconUrl2), NULL, &delegate);
     67   ImageHolder image_holder2(GURL(kIconUrl1), GURL(), NULL, &delegate);
     68   ImageHolder image_holder3(GURL(), GURL(kIconUrl2), NULL, &delegate);
     69   ImageHolder image_holder4(GURL(), GURL(), NULL, &delegate);
     70 
     71   // Initially, image holder 4 with no URLs should report done, but no others.
     72   EXPECT_FALSE(image_holder1.IsFetchingDone());
     73   EXPECT_FALSE(image_holder2.IsFetchingDone());
     74   EXPECT_FALSE(image_holder3.IsFetchingDone());
     75   EXPECT_TRUE(image_holder4.IsFetchingDone());
     76 
     77   // Put a real bitmap into "bitmap".  2x2 bitmap of green 32 bit pixels.
     78   SkBitmap bitmap;
     79   bitmap.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
     80   bitmap.allocPixels();
     81   bitmap.eraseColor(SK_ColorGREEN);
     82 
     83   // Add the first icon, and image holder 2 should now also report done.
     84   image_holder1.OnFetchComplete(GURL(kIconUrl1), &bitmap);
     85   image_holder2.OnFetchComplete(GURL(kIconUrl1), &bitmap);
     86   image_holder3.OnFetchComplete(GURL(kIconUrl1), &bitmap);
     87   image_holder4.OnFetchComplete(GURL(kIconUrl1), &bitmap);
     88   EXPECT_FALSE(image_holder1.IsFetchingDone());
     89   EXPECT_TRUE(image_holder2.IsFetchingDone());
     90   EXPECT_FALSE(image_holder3.IsFetchingDone());
     91   EXPECT_TRUE(image_holder4.IsFetchingDone());
     92 
     93   // Add the second image, and now all 4 should report done.
     94   image_holder1.OnFetchComplete(GURL(kIconUrl2), &bitmap);
     95   image_holder2.OnFetchComplete(GURL(kIconUrl2), &bitmap);
     96   image_holder3.OnFetchComplete(GURL(kIconUrl2), &bitmap);
     97   image_holder4.OnFetchComplete(GURL(kIconUrl2), &bitmap);
     98   EXPECT_TRUE(image_holder1.IsFetchingDone());
     99   EXPECT_TRUE(image_holder2.IsFetchingDone());
    100   EXPECT_TRUE(image_holder3.IsFetchingDone());
    101   EXPECT_TRUE(image_holder4.IsFetchingDone());
    102 }
    103 
    104 }  // namespace chrome.
    105