1 // Copyright 2015 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/manifest/manifest_icon_downloader.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 class ManifestIconDownloaderTest
: public testing::Test
{
14 ManifestIconDownloaderTest() = default;
15 ~ManifestIconDownloaderTest() override
= default;
17 int FindBitmap(const int ideal_icon_size_in_px
,
18 const int minimum_icon_size_in_px
,
19 const std::vector
<SkBitmap
>& bitmaps
) {
20 return ManifestIconDownloader::FindClosestBitmapIndex(
21 ideal_icon_size_in_px
, minimum_icon_size_in_px
, bitmaps
);
24 SkBitmap
CreateDummyBitmap(int width
, int height
) {
26 bitmap
.allocN32Pixels(width
, height
);
27 bitmap
.setImmutable();
31 DISALLOW_COPY_AND_ASSIGN(ManifestIconDownloaderTest
);
34 TEST_F(ManifestIconDownloaderTest
, NoIcons
) {
35 ASSERT_EQ(-1, FindBitmap(0, 0, std::vector
<SkBitmap
>()));
38 TEST_F(ManifestIconDownloaderTest
, ExactIsChosen
) {
39 std::vector
<SkBitmap
> bitmaps
;
40 bitmaps
.push_back(CreateDummyBitmap(10, 10));
42 ASSERT_EQ(0, FindBitmap(10, 0, bitmaps
));
45 TEST_F(ManifestIconDownloaderTest
, BiggerIsChosen
) {
46 std::vector
<SkBitmap
> bitmaps
;
47 bitmaps
.push_back(CreateDummyBitmap(20, 20));
49 ASSERT_EQ(0, FindBitmap(10, 0, bitmaps
));
52 TEST_F(ManifestIconDownloaderTest
, SmallerBelowMinimumIsIgnored
) {
53 std::vector
<SkBitmap
> bitmaps
;
54 bitmaps
.push_back(CreateDummyBitmap(10, 10));
56 ASSERT_EQ(-1, FindBitmap(20, 15, bitmaps
));
59 TEST_F(ManifestIconDownloaderTest
, SmallerAboveMinimumIsChosen
) {
60 std::vector
<SkBitmap
> bitmaps
;
61 bitmaps
.push_back(CreateDummyBitmap(15, 15));
63 ASSERT_EQ(0, FindBitmap(20, 15, bitmaps
));
66 TEST_F(ManifestIconDownloaderTest
, ExactIsPreferredOverBigger
) {
67 std::vector
<SkBitmap
> bitmaps
;
68 bitmaps
.push_back(CreateDummyBitmap(20, 20));
69 bitmaps
.push_back(CreateDummyBitmap(10, 10));
71 ASSERT_EQ(1, FindBitmap(10, 0, bitmaps
));
74 TEST_F(ManifestIconDownloaderTest
, ExactIsPreferredOverSmaller
) {
75 std::vector
<SkBitmap
> bitmaps
;
76 bitmaps
.push_back(CreateDummyBitmap(20, 20));
77 bitmaps
.push_back(CreateDummyBitmap(10, 10));
79 ASSERT_EQ(0, FindBitmap(20, 0, bitmaps
));
82 TEST_F(ManifestIconDownloaderTest
, BiggerIsPreferredOverCloserSmaller
) {
83 std::vector
<SkBitmap
> bitmaps
;
84 bitmaps
.push_back(CreateDummyBitmap(20, 20));
85 bitmaps
.push_back(CreateDummyBitmap(10, 10));
87 ASSERT_EQ(0, FindBitmap(11, 0, bitmaps
));
90 TEST_F(ManifestIconDownloaderTest
, ClosestToExactIsChosen
) {
91 std::vector
<SkBitmap
> bitmaps
;
92 bitmaps
.push_back(CreateDummyBitmap(25, 25));
93 bitmaps
.push_back(CreateDummyBitmap(20, 20));
95 ASSERT_EQ(1, FindBitmap(10, 0, bitmaps
));
98 TEST_F(ManifestIconDownloaderTest
, MixedReturnsBiggestClosest
) {
99 std::vector
<SkBitmap
> bitmaps
;
100 bitmaps
.push_back(CreateDummyBitmap(10, 10));
101 bitmaps
.push_back(CreateDummyBitmap(8, 8));
102 bitmaps
.push_back(CreateDummyBitmap(6, 6));
104 ASSERT_EQ(0, FindBitmap(9, 0, bitmaps
));
107 TEST_F(ManifestIconDownloaderTest
, MixedCanReturnMiddle
) {
108 std::vector
<SkBitmap
> bitmaps
;
109 bitmaps
.push_back(CreateDummyBitmap(10, 10));
110 bitmaps
.push_back(CreateDummyBitmap(8, 8));
111 bitmaps
.push_back(CreateDummyBitmap(6, 6));
113 ASSERT_EQ(1, FindBitmap(7, 0, bitmaps
));
116 TEST_F(ManifestIconDownloaderTest
, NonSquareIsIgnored
) {
117 std::vector
<SkBitmap
> bitmaps
;
118 bitmaps
.push_back(CreateDummyBitmap(15, 10));
119 bitmaps
.push_back(CreateDummyBitmap(10, 15));
121 ASSERT_EQ(-1, FindBitmap(15, 0, bitmaps
));
122 ASSERT_EQ(-1, FindBitmap(10, 0, bitmaps
));