// Copyright 2015 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "ios/chrome/browser/favicon/large_icon_cache.h" #include "base/macros.h" #include "components/favicon_base/fallback_icon_style.h" #include "components/favicon_base/favicon_types.h" #include "skia/ext/skia_utils_ios.h" #include "testing/gtest/include/gtest/gtest.h" #include "ui/gfx/codec/png_codec.h" namespace { const char kDummyUrl[] = "http://www.example.com"; const char kDummyUrl2[] = "http://www.example2.com"; const SkColor kTestColor = SK_ColorRED; favicon_base::FaviconRawBitmapResult CreateTestBitmap(int w, int h, SkColor color) { favicon_base::FaviconRawBitmapResult result; result.expired = false; // Create bitmap and fill with |color|. scoped_refptr data(new base::RefCountedBytes()); SkBitmap bitmap; bitmap.allocN32Pixels(w, h); bitmap.eraseColor(color); gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &data->data()); result.bitmap_data = data; result.pixel_size = gfx::Size(w, h); result.icon_url = GURL(kDummyUrl); result.icon_type = favicon_base::TOUCH_ICON; CHECK(result.is_valid()); return result; } class LargeIconCacheTest : public testing::Test { public: LargeIconCacheTest() { expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle()); expected_fallback_icon_style_->background_color = kTestColor; expected_bitmap_ = CreateTestBitmap(24, 24, kTestColor); large_icon_cache_.reset(new LargeIconCache); } ~LargeIconCacheTest() override {} protected: scoped_ptr large_icon_cache_; favicon_base::FaviconRawBitmapResult expected_bitmap_; scoped_ptr expected_fallback_icon_style_; bool is_callback_invoked_; private: DISALLOW_COPY_AND_ASSIGN(LargeIconCacheTest); }; TEST_F(LargeIconCacheTest, EmptyCache) { scoped_ptr large_icon_cache(new LargeIconCache); EXPECT_EQ(nullptr, large_icon_cache->GetCachedResult(GURL(kDummyUrl))); } TEST_F(LargeIconCacheTest, RetreiveItem) { scoped_ptr expected_result1; scoped_ptr expected_result2; expected_result1.reset(new favicon_base::LargeIconResult(expected_bitmap_)); expected_result2.reset(new favicon_base::LargeIconResult( new favicon_base::FallbackIconStyle(*expected_fallback_icon_style_))); large_icon_cache_->SetCachedResult(GURL(kDummyUrl), *expected_result1); large_icon_cache_->SetCachedResult(GURL(kDummyUrl2), *expected_result2); scoped_ptr result1 = large_icon_cache_->GetCachedResult(GURL(kDummyUrl)); EXPECT_EQ(true, result1->bitmap.is_valid()); EXPECT_EQ(expected_result1->bitmap.pixel_size, result1->bitmap.pixel_size); scoped_ptr result2 = large_icon_cache_->GetCachedResult(GURL(kDummyUrl2)); EXPECT_EQ(false, result2->bitmap.is_valid()); EXPECT_EQ(expected_result2->fallback_icon_style->background_color, result2->fallback_icon_style->background_color); // Test overwriting kDummyUrl. large_icon_cache_->SetCachedResult(GURL(kDummyUrl), *expected_result2); scoped_ptr result3 = large_icon_cache_->GetCachedResult(GURL(kDummyUrl2)); EXPECT_EQ(false, result3->bitmap.is_valid()); EXPECT_EQ(expected_result2->fallback_icon_style->background_color, result3->fallback_icon_style->background_color); } } // namespace