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 "chrome/browser/profiles/profile_avatar_icon_util.h"
6
7#include "grit/theme_resources.h"
8#include "testing/gtest/include/gtest/gtest.h"
9#include "ui/base/resource/resource_bundle.h"
10#include "ui/gfx/canvas.h"
11#include "ui/gfx/image/image_skia.h"
12#include "ui/gfx/image/image_skia_rep.h"
13#include "ui/gfx/image/image_unittest_util.h"
14
15namespace {
16
17// Helper function to check that the image is sized properly
18// and supports multiple pixel densities.
19void VerifyScaling(gfx::Image& image, gfx::Size& size) {
20  gfx::Size canvas_size(100, 100);
21  gfx::Canvas canvas(canvas_size, 1.0f, false);
22  gfx::Canvas canvas2(canvas_size, 2.0f, false);
23
24  ASSERT_FALSE(gfx::test::IsEmpty(image));
25  EXPECT_EQ(image.Size(), size);
26
27  gfx::ImageSkia image_skia = *image.ToImageSkia();
28  canvas.DrawImageInt(image_skia, 15, 10);
29  EXPECT_TRUE(image.ToImageSkia()->HasRepresentation(1.0f));
30
31  canvas2.DrawImageInt(image_skia, 15, 10);
32  EXPECT_TRUE(image.ToImageSkia()->HasRepresentation(2.0f));
33}
34
35TEST(ProfileInfoUtilTest, SizedMenuIcon) {
36  // Test that an avatar icon isn't changed.
37  const gfx::Image& profile_image(
38      ResourceBundle::GetSharedInstance().GetImageNamed(IDR_PROFILE_AVATAR_0));
39  gfx::Image result =
40      profiles::GetSizedAvatarIcon(profile_image, false, 50, 50);
41
42  EXPECT_FALSE(gfx::test::IsEmpty(result));
43  EXPECT_TRUE(gfx::test::IsEqual(profile_image, result));
44
45  // Test that a rectangular picture (e.g., GAIA image) is changed.
46  gfx::Image rect_picture(gfx::test::CreateImage());
47
48  gfx::Size size(30, 20);
49  gfx::Image result2 = profiles::GetSizedAvatarIcon(
50      rect_picture, true, size.width(), size.height());
51
52  VerifyScaling(result2, size);
53}
54
55TEST(ProfileInfoUtilTest, MenuIcon) {
56  // Test that an avatar icon isn't changed.
57  const gfx::Image& profile_image(
58      ResourceBundle::GetSharedInstance().GetImageNamed(IDR_PROFILE_AVATAR_0));
59  gfx::Image result = profiles::GetAvatarIconForMenu(profile_image, false);
60  EXPECT_FALSE(gfx::test::IsEmpty(result));
61  EXPECT_TRUE(gfx::test::IsEqual(profile_image, result));
62
63  // Test that a rectangular picture is changed.
64  gfx::Image rect_picture(gfx::test::CreateImage());
65  gfx::Size size(profiles::kAvatarIconWidth, profiles::kAvatarIconHeight);
66  gfx::Image result2 = profiles::GetAvatarIconForMenu(rect_picture, true);
67
68  VerifyScaling(result2, size);
69}
70
71TEST(ProfileInfoUtilTest, WebUIIcon) {
72  // Test that an avatar icon isn't changed.
73  const gfx::Image& profile_image(
74      ResourceBundle::GetSharedInstance().GetImageNamed(IDR_PROFILE_AVATAR_0));
75  gfx::Image result = profiles::GetAvatarIconForWebUI(profile_image, false);
76  EXPECT_FALSE(gfx::test::IsEmpty(result));
77  EXPECT_TRUE(gfx::test::IsEqual(profile_image, result));
78
79  // Test that a rectangular picture is changed.
80  gfx::Image rect_picture(gfx::test::CreateImage());
81  gfx::Size size(profiles::kAvatarIconWidth, profiles::kAvatarIconHeight);
82  gfx::Image result2 = profiles::GetAvatarIconForWebUI(rect_picture, true);
83
84  VerifyScaling(result2, size);
85}
86
87TEST(ProfileInfoUtilTest, TitleBarIcon) {
88  int width = 100;
89  int height = 40;
90
91  // Test that an avatar icon isn't changed.
92  const gfx::Image& profile_image(
93      ResourceBundle::GetSharedInstance().GetImageNamed(IDR_PROFILE_AVATAR_0));
94  gfx::Image result = profiles::GetAvatarIconForTitleBar(
95      profile_image, false, width, height);
96  EXPECT_FALSE(gfx::test::IsEmpty(result));
97  EXPECT_TRUE(gfx::test::IsEqual(profile_image, result));
98
99  // Test that a rectangular picture is changed.
100  gfx::Image rect_picture(gfx::test::CreateImage());
101
102  gfx::Size size(width, height);
103  gfx::Image result2 = profiles::GetAvatarIconForTitleBar(
104      rect_picture, true, width, height);
105
106  VerifyScaling(result2, size);
107}
108
109}  // namespace
110