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.
5 #include "chrome/browser/extensions/tab_helper.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "third_party/skia/include/core/SkBitmap.h"
12 SkBitmap
CreateSquareBitmapWithColor(int size
, SkColor color
) {
14 bitmap
.setConfig(SkBitmap::kARGB_8888_Config
, size
, size
);
16 bitmap
.eraseColor(color
);
20 void ValidateBitmapSizeAndColor(SkBitmap bitmap
, int size
, SkColor color
) {
21 // Obtain pixel lock to access pixels.
22 SkAutoLockPixels
lock(bitmap
);
23 EXPECT_EQ(color
, bitmap
.getColor(0, 0));
24 EXPECT_EQ(size
, bitmap
.width());
25 EXPECT_EQ(size
, bitmap
.height());
28 class TabHelperTest
: public testing::Test
{
33 virtual ~TabHelperTest() {
37 DISALLOW_COPY_AND_ASSIGN(TabHelperTest
);
42 TEST_F(TabHelperTest
, ConstrainBitmapsToSizes
) {
43 std::set
<int> desired_sizes
;
44 desired_sizes
.insert(16);
45 desired_sizes
.insert(32);
46 desired_sizes
.insert(128);
47 desired_sizes
.insert(256);
50 std::vector
<SkBitmap
> bitmaps
;
51 bitmaps
.push_back(CreateSquareBitmapWithColor(16, SK_ColorRED
));
52 bitmaps
.push_back(CreateSquareBitmapWithColor(32, SK_ColorGREEN
));
53 bitmaps
.push_back(CreateSquareBitmapWithColor(48, SK_ColorBLUE
));
54 bitmaps
.push_back(CreateSquareBitmapWithColor(144, SK_ColorYELLOW
));
56 std::map
<int, SkBitmap
> results(
57 extensions::TabHelper::ConstrainBitmapsToSizes(bitmaps
, desired_sizes
));
59 EXPECT_EQ(3u, results
.size());
60 ValidateBitmapSizeAndColor(results
[16], 16, SK_ColorRED
);
61 ValidateBitmapSizeAndColor(results
[32], 32, SK_ColorGREEN
);
62 ValidateBitmapSizeAndColor(results
[128], 128, SK_ColorYELLOW
);
65 std::vector
<SkBitmap
> bitmaps
;
66 bitmaps
.push_back(CreateSquareBitmapWithColor(512, SK_ColorRED
));
67 bitmaps
.push_back(CreateSquareBitmapWithColor(18, SK_ColorGREEN
));
68 bitmaps
.push_back(CreateSquareBitmapWithColor(33, SK_ColorBLUE
));
69 bitmaps
.push_back(CreateSquareBitmapWithColor(17, SK_ColorYELLOW
));
71 std::map
<int, SkBitmap
> results(
72 extensions::TabHelper::ConstrainBitmapsToSizes(bitmaps
, desired_sizes
));
74 EXPECT_EQ(3u, results
.size());
75 ValidateBitmapSizeAndColor(results
[16], 16, SK_ColorYELLOW
);
76 ValidateBitmapSizeAndColor(results
[32], 32, SK_ColorBLUE
);
77 ValidateBitmapSizeAndColor(results
[256], 256, SK_ColorRED
);
81 TEST_F(TabHelperTest
, GenerateIcons
) {
83 // The 32x32 icon should be generated from the 16x16 icon.
84 std::map
<int, SkBitmap
> bitmaps
;
85 bitmaps
[16] = CreateSquareBitmapWithColor(16, SK_ColorRED
);
86 extensions::TabHelper::GenerateContainerIcon(&bitmaps
, 32);
87 EXPECT_EQ(1u, bitmaps
.count(32));
88 EXPECT_EQ(32, bitmaps
[32].width());
91 // The 32x32 icon should not be generated because no smaller icon exists.
92 std::map
<int, SkBitmap
> bitmaps
;
93 bitmaps
[48] = CreateSquareBitmapWithColor(48, SK_ColorRED
);
94 extensions::TabHelper::GenerateContainerIcon(&bitmaps
, 32);
95 EXPECT_EQ(0u, bitmaps
.count(32));
98 // The 32x32 icon should not be generated with no base icons.
99 std::map
<int, SkBitmap
> bitmaps
;
100 extensions::TabHelper::GenerateContainerIcon(&bitmaps
, 32);
101 EXPECT_EQ(0u, bitmaps
.count(32));