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.
5 #include "ui/views/controls/button/blue_button.h"
7 #include "base/strings/string16.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/skia_util.h"
11 #include "ui/views/controls/button/label_button_border.h"
12 #include "ui/views/test/widget_test.h"
16 using BlueButtonTest
= test::WidgetTest
;
18 TEST_F(BlueButtonTest
, Border
) {
19 // The buttons must be added to a Widget so that borders are correctly
20 // applied once the NativeTheme is determined.
21 Widget
* widget
= CreateTopLevelPlatformWidget();
23 // Compared to a normal LabelButton...
24 LabelButton
* button
= new LabelButton(nullptr, base::ASCIIToUTF16("foo"));
25 EXPECT_EQ(Button::STYLE_TEXTBUTTON
, button
->style());
26 EXPECT_TRUE(button
->focus_painter());
28 // Switch to the same style as BlueButton for a more compelling comparison.
29 button
->SetStyle(Button::STYLE_BUTTON
);
30 EXPECT_EQ(Button::STYLE_BUTTON
, button
->style());
31 EXPECT_FALSE(button
->focus_painter());
33 widget
->GetContentsView()->AddChildView(button
);
34 button
->SizeToPreferredSize();
35 gfx::Canvas
button_canvas(button
->size(), 1.0, true);
36 button
->border()->Paint(*button
, &button_canvas
);
38 // ... a special blue border should be used.
39 BlueButton
* blue_button
= new BlueButton(nullptr, base::ASCIIToUTF16("foo"));
40 EXPECT_EQ(Button::STYLE_BUTTON
, blue_button
->style());
41 EXPECT_FALSE(blue_button
->focus_painter());
43 widget
->GetContentsView()->AddChildView(blue_button
);
44 blue_button
->SizeToPreferredSize();
45 #if defined(OS_MACOSX)
46 // On Mac, the default styled border has a large minimum width. To ensure that
47 // the bitmaps are comparable, the size needs to match (checked below).
48 // Increase the minimum size of the blue button to pass the size check.
49 EXPECT_NE(button
->size(), blue_button
->size()); // Verify this is needed.
50 blue_button
->SetMinSize(button
->border()->GetMinimumSize());
51 blue_button
->SizeToPreferredSize();
54 gfx::Canvas
canvas(blue_button
->size(), 1.0, true);
55 blue_button
->border()->Paint(*blue_button
, &canvas
);
56 EXPECT_EQ(button
->GetText(), blue_button
->GetText());
57 EXPECT_EQ(button
->size(), blue_button
->size());
58 EXPECT_FALSE(gfx::BitmapsAreEqual(button_canvas
.ExtractImageRep().sk_bitmap(),
59 canvas
.ExtractImageRep().sk_bitmap()));