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   // Set an overlay image.
     65   gfx::ImageSkia overlay_image = CreateTestImage(12, 22);
     66   button.SetOverlayImage(&overlay_image);
     67   EXPECT_EQ(12, button.overlay_image_.width());
     68   EXPECT_EQ(22, button.overlay_image_.height());
     69 
     70   // By convention, preferred size doesn't change, even though pushed image
     71   // is bigger.
     72   EXPECT_EQ("10x20", button.GetPreferredSize().ToString());
     73 
     74   // We're still painting the normal image.
     75   EXPECT_FALSE(button.GetImageToPaint().isNull());
     76   EXPECT_EQ(10, button.GetImageToPaint().width());
     77   EXPECT_EQ(20, button.GetImageToPaint().height());
     78 
     79   // Reset the overlay image.
     80   button.SetOverlayImage(NULL);
     81   EXPECT_TRUE(button.overlay_image_.isNull());
     82 }
     83 
     84 TEST_F(ImageButtonTest, SetAndGetImage) {
     85   ImageButton button(NULL);
     86 
     87   // Images start as null.
     88   EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull());
     89   EXPECT_TRUE(button.GetImage(Button::STATE_HOVERED).isNull());
     90   EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull());
     91   EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull());
     92 
     93   // Setting images works as expected.
     94   gfx::ImageSkia image1 = CreateTestImage(10, 11);
     95   gfx::ImageSkia image2 = CreateTestImage(20, 21);
     96   button.SetImage(Button::STATE_NORMAL, &image1);
     97   button.SetImage(Button::STATE_HOVERED, &image2);
     98   EXPECT_TRUE(
     99       button.GetImage(Button::STATE_NORMAL).BackedBySameObjectAs(image1));
    100   EXPECT_TRUE(
    101       button.GetImage(Button::STATE_HOVERED).BackedBySameObjectAs(image2));
    102   EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull());
    103   EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull());
    104 
    105   // ImageButton supports NULL image pointers.
    106   button.SetImage(Button::STATE_NORMAL, NULL);
    107   EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull());
    108 }
    109 
    110 TEST_F(ImageButtonTest, ImagePositionWithBorder) {
    111   ImageButton button(NULL);
    112   gfx::ImageSkia image = CreateTestImage(20, 30);
    113   button.SetImage(CustomButton::STATE_NORMAL, &image);
    114 
    115   // The image should be painted at the top-left corner.
    116   EXPECT_EQ(gfx::Point().ToString(),
    117             button.ComputeImagePaintPosition(image).ToString());
    118 
    119   button.set_border(views::Border::CreateEmptyBorder(10, 5, 0, 0));
    120   EXPECT_EQ(gfx::Point(5, 10).ToString(),
    121             button.ComputeImagePaintPosition(image).ToString());
    122 
    123   button.set_border(NULL);
    124   button.SetBounds(0, 0, 50, 50);
    125   EXPECT_EQ(gfx::Point().ToString(),
    126             button.ComputeImagePaintPosition(image).ToString());
    127 
    128   button.SetImageAlignment(ImageButton::ALIGN_CENTER,
    129                            ImageButton::ALIGN_MIDDLE);
    130   EXPECT_EQ(gfx::Point(15, 10).ToString(),
    131             button.ComputeImagePaintPosition(image).ToString());
    132   button.set_border(views::Border::CreateEmptyBorder(10, 10, 0, 0));
    133   EXPECT_EQ(gfx::Point(20, 15).ToString(),
    134             button.ComputeImagePaintPosition(image).ToString());
    135 }
    136 
    137 TEST_F(ImageButtonTest, LeftAlignedMirrored) {
    138   ImageButton button(NULL);
    139   gfx::ImageSkia image = CreateTestImage(20, 30);
    140   button.SetImage(CustomButton::STATE_NORMAL, &image);
    141   button.SetBounds(0, 0, 50, 30);
    142   button.SetImageAlignment(ImageButton::ALIGN_LEFT,
    143                            ImageButton::ALIGN_BOTTOM);
    144   button.SetDrawImageMirrored(true);
    145 
    146   // Because the coordinates are flipped, we should expect this to draw as if
    147   // it were ALIGN_RIGHT.
    148   EXPECT_EQ(gfx::Point(30, 0).ToString(),
    149             button.ComputeImagePaintPosition(image).ToString());
    150 }
    151 
    152 TEST_F(ImageButtonTest, RightAlignedMirrored) {
    153   ImageButton button(NULL);
    154   gfx::ImageSkia image = CreateTestImage(20, 30);
    155   button.SetImage(CustomButton::STATE_NORMAL, &image);
    156   button.SetBounds(0, 0, 50, 30);
    157   button.SetImageAlignment(ImageButton::ALIGN_RIGHT,
    158                            ImageButton::ALIGN_BOTTOM);
    159   button.SetDrawImageMirrored(true);
    160 
    161   // Because the coordinates are flipped, we should expect this to draw as if
    162   // it were ALIGN_LEFT.
    163   EXPECT_EQ(gfx::Point(0, 0).ToString(),
    164             button.ComputeImagePaintPosition(image).ToString());
    165 }
    166 
    167 }  // namespace views
    168