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 "chrome/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 
15 namespace {
16 
17 // Helper function to check that the image is sized properly
18 // and supports multiple pixel densities.
VerifyScaling(gfx::Image & image,gfx::Size & size)19 void 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 
TEST(ProfileInfoUtilTest,SizedMenuIcon)35 TEST(ProfileInfoUtilTest, SizedMenuIcon) {
36   // Test that an avatar icon isn't changed.
37   const gfx::Image& profile_image(
38       ui::ResourceBundle::GetSharedInstance().GetImageNamed(
39           IDR_PROFILE_AVATAR_0));
40   gfx::Image result =
41       profiles::GetSizedAvatarIcon(profile_image, false, 50, 50);
42 
43   EXPECT_FALSE(gfx::test::IsEmpty(result));
44   EXPECT_TRUE(gfx::test::AreImagesEqual(profile_image, result));
45 
46   // Test that a rectangular picture (e.g., GAIA image) is changed.
47   gfx::Image rect_picture(gfx::test::CreateImage());
48 
49   gfx::Size size(30, 20);
50   gfx::Image result2 = profiles::GetSizedAvatarIcon(
51       rect_picture, true, size.width(), size.height());
52 
53   VerifyScaling(result2, size);
54 }
55 
TEST(ProfileInfoUtilTest,WebUIIcon)56 TEST(ProfileInfoUtilTest, WebUIIcon) {
57   // Test that an avatar icon isn't changed.
58   const gfx::Image& profile_image(
59       ui::ResourceBundle::GetSharedInstance().GetImageNamed(
60           IDR_PROFILE_AVATAR_0));
61   gfx::Image result = profiles::GetAvatarIconForWebUI(profile_image, false);
62   EXPECT_FALSE(gfx::test::IsEmpty(result));
63   EXPECT_TRUE(gfx::test::AreImagesEqual(profile_image, result));
64 
65   // Test that a rectangular picture is changed.
66   gfx::Image rect_picture(gfx::test::CreateImage());
67   gfx::Size size(profiles::kAvatarIconSize, profiles::kAvatarIconSize);
68   gfx::Image result2 = profiles::GetAvatarIconForWebUI(rect_picture, true);
69 
70   VerifyScaling(result2, size);
71 }
72 
TEST(ProfileInfoUtilTest,TitleBarIcon)73 TEST(ProfileInfoUtilTest, TitleBarIcon) {
74   int width = 100;
75   int height = 40;
76 
77   // Test that an avatar icon isn't changed.
78   const gfx::Image& profile_image(
79       ui::ResourceBundle::GetSharedInstance().GetImageNamed(
80           IDR_PROFILE_AVATAR_0));
81   gfx::Image result = profiles::GetAvatarIconForTitleBar(
82       profile_image, false, width, height);
83   EXPECT_FALSE(gfx::test::IsEmpty(result));
84   EXPECT_TRUE(gfx::test::AreImagesEqual(profile_image, result));
85 
86   // Test that a rectangular picture is changed.
87   gfx::Image rect_picture(gfx::test::CreateImage());
88 
89   gfx::Size size(width, height);
90   gfx::Image result2 = profiles::GetAvatarIconForTitleBar(
91       rect_picture, true, width, height);
92 
93   VerifyScaling(result2, size);
94 }
95 
96 }  // namespace
97