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.setConfig(SkBitmap::kARGB_8888_Config, width, height);
     16   bitmap.allocPixels();
     17   return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
     18 }
     19 
     20 }  // namespace
     21 
     22 namespace views {
     23 
     24 typedef ViewsTestBase ImageButtonTest;
     25 
     26 TEST_F(ImageButtonTest, Basics) {
     27   ImageButton button(NULL);
     28 
     29   // Our image to paint starts empty.
     30   EXPECT_TRUE(button.GetImageToPaint().isNull());
     31 
     32   // Without a theme, buttons are 16x14 by default.
     33   EXPECT_EQ("16x14", button.GetPreferredSize().ToString());
     34 
     35   // We can set a preferred size when we have no image.
     36   button.SetPreferredSize(gfx::Size(5, 15));
     37   EXPECT_EQ("5x15", 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 
     65 TEST_F(ImageButtonTest, SetAndGetImage) {
     66   ImageButton button(NULL);
     67 
     68   // Images start as null.
     69   EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull());
     70   EXPECT_TRUE(button.GetImage(Button::STATE_HOVERED).isNull());
     71   EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull());
     72   EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull());
     73 
     74   // Setting images works as expected.
     75   gfx::ImageSkia image1 = CreateTestImage(10, 11);
     76   gfx::ImageSkia image2 = CreateTestImage(20, 21);
     77   button.SetImage(Button::STATE_NORMAL, &image1);
     78   button.SetImage(Button::STATE_HOVERED, &image2);
     79   EXPECT_TRUE(
     80       button.GetImage(Button::STATE_NORMAL).BackedBySameObjectAs(image1));
     81   EXPECT_TRUE(
     82       button.GetImage(Button::STATE_HOVERED).BackedBySameObjectAs(image2));
     83   EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull());
     84   EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull());
     85 
     86   // ImageButton supports NULL image pointers.
     87   button.SetImage(Button::STATE_NORMAL, NULL);
     88   EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull());
     89 }
     90 
     91 TEST_F(ImageButtonTest, ImagePositionWithBorder) {
     92   ImageButton button(NULL);
     93   gfx::ImageSkia image = CreateTestImage(20, 30);
     94   button.SetImage(CustomButton::STATE_NORMAL, &image);
     95 
     96   // The image should be painted at the top-left corner.
     97   EXPECT_EQ(gfx::Point().ToString(),
     98             button.ComputeImagePaintPosition(image).ToString());
     99 
    100   button.SetBorder(views::Border::CreateEmptyBorder(10, 5, 0, 0));
    101   EXPECT_EQ(gfx::Point(5, 10).ToString(),
    102             button.ComputeImagePaintPosition(image).ToString());
    103 
    104   button.SetBorder(Border::NullBorder());
    105   button.SetBounds(0, 0, 50, 50);
    106   EXPECT_EQ(gfx::Point().ToString(),
    107             button.ComputeImagePaintPosition(image).ToString());
    108 
    109   button.SetImageAlignment(ImageButton::ALIGN_CENTER,
    110                            ImageButton::ALIGN_MIDDLE);
    111   EXPECT_EQ(gfx::Point(15, 10).ToString(),
    112             button.ComputeImagePaintPosition(image).ToString());
    113   button.SetBorder(views::Border::CreateEmptyBorder(10, 10, 0, 0));
    114   EXPECT_EQ(gfx::Point(20, 15).ToString(),
    115             button.ComputeImagePaintPosition(image).ToString());
    116 }
    117 
    118 TEST_F(ImageButtonTest, LeftAlignedMirrored) {
    119   ImageButton button(NULL);
    120   gfx::ImageSkia image = CreateTestImage(20, 30);
    121   button.SetImage(CustomButton::STATE_NORMAL, &image);
    122   button.SetBounds(0, 0, 50, 30);
    123   button.SetImageAlignment(ImageButton::ALIGN_LEFT,
    124                            ImageButton::ALIGN_BOTTOM);
    125   button.SetDrawImageMirrored(true);
    126 
    127   // Because the coordinates are flipped, we should expect this to draw as if
    128   // it were ALIGN_RIGHT.
    129   EXPECT_EQ(gfx::Point(30, 0).ToString(),
    130             button.ComputeImagePaintPosition(image).ToString());
    131 }
    132 
    133 TEST_F(ImageButtonTest, RightAlignedMirrored) {
    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_RIGHT,
    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_LEFT.
    144   EXPECT_EQ(gfx::Point(0, 0).ToString(),
    145             button.ComputeImagePaintPosition(image).ToString());
    146 }
    147 
    148 }  // namespace views
    149