Home | History | Annotate | Download | only in button
      1 // Copyright (c) 2012 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 "testing/gtest/include/gtest/gtest.h"
      6 #include "ui/base/layout.h"
      7 #include "ui/views/border.h"
      8 #include "ui/views/controls/button/image_button.h"
      9 #include "ui/views/test/views_test_base.h"
     10 
     11 namespace {
     12 
     13 gfx::ImageSkia CreateTestImage(int width, int height) {
     14   SkBitmap bitmap;
     15   bitmap.allocN32Pixels(width, height);
     16   return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
     17 }
     18 
     19 }  // namespace
     20 
     21 namespace views {
     22 
     23 typedef ViewsTestBase ImageButtonTest;
     24 
     25 TEST_F(ImageButtonTest, Basics) {
     26   ImageButton button(NULL);
     27 
     28   // Our image to paint starts empty.
     29   EXPECT_TRUE(button.GetImageToPaint().isNull());
     30 
     31   // Without an image, buttons are 16x14 by default.
     32   EXPECT_EQ("16x14", button.GetPreferredSize().ToString());
     33 
     34   // The minimum image size should be applied even when there is no image.
     35   button.SetMinimumImageSize(gfx::Size(5, 15));
     36   EXPECT_EQ("5x15", button.minimum_image_size().ToString());
     37   EXPECT_EQ("16x15", button.GetPreferredSize().ToString());
     38 
     39   // Set a normal image.
     40   gfx::ImageSkia normal_image = CreateTestImage(10, 20);
     41   button.SetImage(CustomButton::STATE_NORMAL, &normal_image);
     42 
     43   // Image uses normal image for painting.
     44   EXPECT_FALSE(button.GetImageToPaint().isNull());
     45   EXPECT_EQ(10, button.GetImageToPaint().width());
     46   EXPECT_EQ(20, button.GetImageToPaint().height());
     47 
     48   // Preferred size is the normal button size.
     49   EXPECT_EQ("10x20", button.GetPreferredSize().ToString());
     50 
     51   // Set a pushed image.
     52   gfx::ImageSkia pushed_image = CreateTestImage(11, 21);
     53   button.SetImage(CustomButton::STATE_PRESSED, &pushed_image);
     54 
     55   // By convention, preferred size doesn't change, even though pushed image
     56   // is bigger.
     57   EXPECT_EQ("10x20", button.GetPreferredSize().ToString());
     58 
     59   // We're still painting the normal image.
     60   EXPECT_FALSE(button.GetImageToPaint().isNull());
     61   EXPECT_EQ(10, button.GetImageToPaint().width());
     62   EXPECT_EQ(20, button.GetImageToPaint().height());
     63 
     64   // The minimum image size should make the preferred size bigger.
     65   button.SetMinimumImageSize(gfx::Size(15, 5));
     66   EXPECT_EQ("15x5", button.minimum_image_size().ToString());
     67   EXPECT_EQ("15x20", button.GetPreferredSize().ToString());
     68   button.SetMinimumImageSize(gfx::Size(15, 25));
     69   EXPECT_EQ("15x25", button.minimum_image_size().ToString());
     70   EXPECT_EQ("15x25", button.GetPreferredSize().ToString());
     71 }
     72 
     73 TEST_F(ImageButtonTest, SetAndGetImage) {
     74   ImageButton button(NULL);
     75 
     76   // Images start as null.
     77   EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull());
     78   EXPECT_TRUE(button.GetImage(Button::STATE_HOVERED).isNull());
     79   EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull());
     80   EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull());
     81 
     82   // Setting images works as expected.
     83   gfx::ImageSkia image1 = CreateTestImage(10, 11);
     84   gfx::ImageSkia image2 = CreateTestImage(20, 21);
     85   button.SetImage(Button::STATE_NORMAL, &image1);
     86   button.SetImage(Button::STATE_HOVERED, &image2);
     87   EXPECT_TRUE(
     88       button.GetImage(Button::STATE_NORMAL).BackedBySameObjectAs(image1));
     89   EXPECT_TRUE(
     90       button.GetImage(Button::STATE_HOVERED).BackedBySameObjectAs(image2));
     91   EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull());
     92   EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull());
     93 
     94   // ImageButton supports NULL image pointers.
     95   button.SetImage(Button::STATE_NORMAL, NULL);
     96   EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull());
     97 }
     98 
     99 TEST_F(ImageButtonTest, ImagePositionWithBorder) {
    100   ImageButton button(NULL);
    101   gfx::ImageSkia image = CreateTestImage(20, 30);
    102   button.SetImage(CustomButton::STATE_NORMAL, &image);
    103 
    104   // The image should be painted at the top-left corner.
    105   EXPECT_EQ(gfx::Point().ToString(),
    106             button.ComputeImagePaintPosition(image).ToString());
    107 
    108   button.SetBorder(views::Border::CreateEmptyBorder(10, 5, 0, 0));
    109   EXPECT_EQ(gfx::Point(5, 10).ToString(),
    110             button.ComputeImagePaintPosition(image).ToString());
    111 
    112   button.SetBorder(Border::NullBorder());
    113   button.SetBounds(0, 0, 50, 50);
    114   EXPECT_EQ(gfx::Point().ToString(),
    115             button.ComputeImagePaintPosition(image).ToString());
    116 
    117   button.SetImageAlignment(ImageButton::ALIGN_CENTER,
    118                            ImageButton::ALIGN_MIDDLE);
    119   EXPECT_EQ(gfx::Point(15, 10).ToString(),
    120             button.ComputeImagePaintPosition(image).ToString());
    121   button.SetBorder(views::Border::CreateEmptyBorder(10, 10, 0, 0));
    122   EXPECT_EQ(gfx::Point(20, 15).ToString(),
    123             button.ComputeImagePaintPosition(image).ToString());
    124 
    125   // The entire button's size should take the border into account.
    126   EXPECT_EQ(gfx::Size(30, 40).ToString(), button.GetPreferredSize().ToString());
    127 
    128   // The border should be added on top of the minimum image size.
    129   button.SetMinimumImageSize(gfx::Size(30, 5));
    130   EXPECT_EQ(gfx::Size(40, 40).ToString(), button.GetPreferredSize().ToString());
    131 }
    132 
    133 TEST_F(ImageButtonTest, LeftAlignedMirrored) {
    134   ImageButton button(NULL);
    135   gfx::ImageSkia image = CreateTestImage(20, 30);
    136   button.SetImage(CustomButton::STATE_NORMAL, &image);
    137   button.SetBounds(0, 0, 50, 30);
    138   button.SetImageAlignment(ImageButton::ALIGN_LEFT,
    139                            ImageButton::ALIGN_BOTTOM);
    140   button.SetDrawImageMirrored(true);
    141 
    142   // Because the coordinates are flipped, we should expect this to draw as if
    143   // it were ALIGN_RIGHT.
    144   EXPECT_EQ(gfx::Point(30, 0).ToString(),
    145             button.ComputeImagePaintPosition(image).ToString());
    146 }
    147 
    148 TEST_F(ImageButtonTest, RightAlignedMirrored) {
    149   ImageButton button(NULL);
    150   gfx::ImageSkia image = CreateTestImage(20, 30);
    151   button.SetImage(CustomButton::STATE_NORMAL, &image);
    152   button.SetBounds(0, 0, 50, 30);
    153   button.SetImageAlignment(ImageButton::ALIGN_RIGHT,
    154                            ImageButton::ALIGN_BOTTOM);
    155   button.SetDrawImageMirrored(true);
    156 
    157   // Because the coordinates are flipped, we should expect this to draw as if
    158   // it were ALIGN_LEFT.
    159   EXPECT_EQ(gfx::Point(0, 0).ToString(),
    160             button.ComputeImagePaintPosition(image).ToString());
    161 }
    162 
    163 }  // namespace views
    164