summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/tab_helper_unittest.cc
blob: 71690633e98bd20c24f0d6fa987d770adacd104d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2014 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 "chrome/browser/extensions/tab_helper.h"

#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"

namespace {

SkBitmap CreateSquareBitmapWithColor(int size, SkColor color) {
  SkBitmap bitmap;
  bitmap.setConfig(SkBitmap::kARGB_8888_Config, size, size);
  bitmap.allocPixels();
  bitmap.eraseColor(color);
  return bitmap;
}

void ValidateBitmapSizeAndColor(SkBitmap bitmap, int size, SkColor color) {
  // Obtain pixel lock to access pixels.
  SkAutoLockPixels lock(bitmap);
  EXPECT_EQ(color, bitmap.getColor(0, 0));
  EXPECT_EQ(size, bitmap.width());
  EXPECT_EQ(size, bitmap.height());
}

class TabHelperTest : public testing::Test {
 protected:
  TabHelperTest() {
  }

  virtual ~TabHelperTest() {
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(TabHelperTest);
};

}  // namespace

TEST_F(TabHelperTest, ConstrainBitmapsToSizes) {
  std::set<int> desired_sizes;
  desired_sizes.insert(16);
  desired_sizes.insert(32);
  desired_sizes.insert(128);
  desired_sizes.insert(256);

  {
    std::vector<SkBitmap> bitmaps;
    bitmaps.push_back(CreateSquareBitmapWithColor(16, SK_ColorRED));
    bitmaps.push_back(CreateSquareBitmapWithColor(32, SK_ColorGREEN));
    bitmaps.push_back(CreateSquareBitmapWithColor(48, SK_ColorBLUE));
    bitmaps.push_back(CreateSquareBitmapWithColor(144, SK_ColorYELLOW));

    std::map<int, SkBitmap> results(
        extensions::TabHelper::ConstrainBitmapsToSizes(bitmaps, desired_sizes));

    EXPECT_EQ(3u, results.size());
    ValidateBitmapSizeAndColor(results[16], 16, SK_ColorRED);
    ValidateBitmapSizeAndColor(results[32], 32, SK_ColorGREEN);
    ValidateBitmapSizeAndColor(results[128], 128, SK_ColorYELLOW);
  }
  {
    std::vector<SkBitmap> bitmaps;
    bitmaps.push_back(CreateSquareBitmapWithColor(512, SK_ColorRED));
    bitmaps.push_back(CreateSquareBitmapWithColor(18, SK_ColorGREEN));
    bitmaps.push_back(CreateSquareBitmapWithColor(33, SK_ColorBLUE));
    bitmaps.push_back(CreateSquareBitmapWithColor(17, SK_ColorYELLOW));

    std::map<int, SkBitmap> results(
        extensions::TabHelper::ConstrainBitmapsToSizes(bitmaps, desired_sizes));

    EXPECT_EQ(3u, results.size());
    ValidateBitmapSizeAndColor(results[16], 16, SK_ColorYELLOW);
    ValidateBitmapSizeAndColor(results[32], 32, SK_ColorBLUE);
    ValidateBitmapSizeAndColor(results[256], 256, SK_ColorRED);
  }
}

TEST_F(TabHelperTest, GenerateIcons) {
  {
    // The 32x32 icon should be generated from the 16x16 icon.
    std::map<int, SkBitmap> bitmaps;
    bitmaps[16] = CreateSquareBitmapWithColor(16, SK_ColorRED);
    extensions::TabHelper::GenerateContainerIcon(&bitmaps, 32);
    EXPECT_EQ(1u, bitmaps.count(32));
    EXPECT_EQ(32, bitmaps[32].width());
  }
  {
    // The 32x32 icon should not be generated because no smaller icon exists.
    std::map<int, SkBitmap> bitmaps;
    bitmaps[48] = CreateSquareBitmapWithColor(48, SK_ColorRED);
    extensions::TabHelper::GenerateContainerIcon(&bitmaps, 32);
    EXPECT_EQ(0u, bitmaps.count(32));
  }
  {
    // The 32x32 icon should not be generated with no base icons.
    std::map<int, SkBitmap> bitmaps;
    extensions::TabHelper::GenerateContainerIcon(&bitmaps, 32);
    EXPECT_EQ(0u, bitmaps.count(32));
  }
}