summaryrefslogtreecommitdiffstats
path: root/ui/views/controls/button/image_button_unittest.cc
blob: 9b719646738c7c9e0be305cb52e9a2bc20f1ff15 (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
104
105
106
107
108
109
110
// Copyright (c) 2012 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 "testing/gtest/include/gtest/gtest.h"
#include "ui/base/layout.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/test/views_test_base.h"

namespace {

gfx::ImageSkia CreateTestImage(int width, int height) {
  SkBitmap bitmap;
  bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
  bitmap.allocPixels();
  return gfx::ImageSkia(bitmap);
}

}  // namespace

namespace views {

typedef ViewsTestBase ImageButtonTest;

TEST_F(ImageButtonTest, Basics) {
  ImageButton button(NULL);

  // Our image to paint starts empty.
  EXPECT_TRUE(button.GetImageToPaint().isNull());

  // Without a theme, buttons are 16x14 by default.
  EXPECT_EQ("16x14", button.GetPreferredSize().ToString());

  // We can set a preferred size when we have no image.
  button.SetPreferredSize(gfx::Size(5, 15));
  EXPECT_EQ("5x15", button.GetPreferredSize().ToString());

  // Set a normal image.
  gfx::ImageSkia normal_image = CreateTestImage(10, 20);
  button.SetImage(CustomButton::BS_NORMAL, &normal_image);

  // Image uses normal image for painting.
  EXPECT_FALSE(button.GetImageToPaint().isNull());
  EXPECT_EQ(10, button.GetImageToPaint().width());
  EXPECT_EQ(20, button.GetImageToPaint().height());

  // Preferred size is the normal button size.
  EXPECT_EQ("10x20", button.GetPreferredSize().ToString());

  // Set a pushed image.
  gfx::ImageSkia pushed_image = CreateTestImage(11, 21);
  button.SetImage(CustomButton::BS_PUSHED, &pushed_image);

  // By convention, preferred size doesn't change, even though pushed image
  // is bigger.
  EXPECT_EQ("10x20", button.GetPreferredSize().ToString());

  // We're still painting the normal image.
  EXPECT_FALSE(button.GetImageToPaint().isNull());
  EXPECT_EQ(10, button.GetImageToPaint().width());
  EXPECT_EQ(20, button.GetImageToPaint().height());

  // Set an overlay image.
  gfx::ImageSkia overlay_image = CreateTestImage(12, 22);
  button.SetOverlayImage(&overlay_image);
  EXPECT_EQ(12, button.overlay_image_.width());
  EXPECT_EQ(22, button.overlay_image_.height());

  // By convention, preferred size doesn't change, even though pushed image
  // is bigger.
  EXPECT_EQ("10x20", button.GetPreferredSize().ToString());

  // We're still painting the normal image.
  EXPECT_FALSE(button.GetImageToPaint().isNull());
  EXPECT_EQ(10, button.GetImageToPaint().width());
  EXPECT_EQ(20, button.GetImageToPaint().height());

  // Reset the overlay image.
  button.SetOverlayImage(NULL);
  EXPECT_TRUE(button.overlay_image_.isNull());
}

TEST_F(ImageButtonTest, ImagePositionWithBorder) {
  ImageButton button(NULL);
  gfx::ImageSkia image = CreateTestImage(20, 30);
  button.SetImage(CustomButton::BS_NORMAL, &image);

  // The image should be painted at the top-left corner.
  EXPECT_EQ(gfx::Point().ToString(),
            button.ComputeImagePaintPosition(image).ToString());

  button.set_border(views::Border::CreateEmptyBorder(10, 5, 0, 0));
  EXPECT_EQ(gfx::Point(5, 10).ToString(),
            button.ComputeImagePaintPosition(image).ToString());

  button.set_border(NULL);
  button.SetBounds(0, 0, 50, 50);
  EXPECT_EQ(gfx::Point().ToString(),
            button.ComputeImagePaintPosition(image).ToString());

  button.SetImageAlignment(ImageButton::ALIGN_CENTER,
                           ImageButton::ALIGN_MIDDLE);
  EXPECT_EQ(gfx::Point(15, 10).ToString(),
            button.ComputeImagePaintPosition(image).ToString());
  button.set_border(views::Border::CreateEmptyBorder(10, 10, 0, 0));
  EXPECT_EQ(gfx::Point(20, 15).ToString(),
            button.ComputeImagePaintPosition(image).ToString());
}

}  // namespace views