OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/controls/button/blue_button.h" | 5 #include "ui/views/controls/button/blue_button.h" |
6 | 6 |
7 #include "base/strings/string16.h" | 7 #include "base/strings/string16.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "ui/gfx/canvas.h" | 9 #include "ui/gfx/canvas.h" |
10 #include "ui/gfx/skia_util.h" | 10 #include "ui/gfx/skia_util.h" |
11 #include "ui/views/controls/button/label_button_border.h" | 11 #include "ui/views/controls/button/label_button_border.h" |
12 #include "ui/views/test/views_test_base.h" | 12 #include "ui/views/test/widget_test.h" |
13 | 13 |
14 namespace views { | 14 namespace views { |
15 | 15 |
16 namespace { | 16 using BlueButtonTest = test::WidgetTest; |
17 | |
18 class TestBlueButton : public BlueButton { | |
19 public: | |
20 TestBlueButton() : BlueButton(NULL, base::ASCIIToUTF16("foo")) {} | |
21 ~TestBlueButton() override {} | |
22 | |
23 using BlueButton::OnNativeThemeChanged; | |
24 | |
25 private: | |
26 DISALLOW_COPY_AND_ASSIGN(TestBlueButton); | |
27 }; | |
28 | |
29 } // namespace | |
30 | |
31 typedef ViewsTestBase BlueButtonTest; | |
32 | 17 |
33 TEST_F(BlueButtonTest, Border) { | 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(); |
| 22 |
34 // Compared to a normal LabelButton... | 23 // Compared to a normal LabelButton... |
35 LabelButton button(NULL, base::ASCIIToUTF16("foo")); | 24 LabelButton* button = new LabelButton(nullptr, base::ASCIIToUTF16("foo")); |
36 button.SetBoundsRect(gfx::Rect(gfx::Point(0, 0), button.GetPreferredSize())); | 25 EXPECT_EQ(Button::STYLE_TEXTBUTTON, button->style()); |
37 gfx::Canvas button_canvas(button.bounds().size(), 1.0, true); | 26 EXPECT_TRUE(button->focus_painter()); |
38 button.border()->Paint(button, &button_canvas); | 27 |
| 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()); |
| 32 |
| 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); |
39 | 37 |
40 // ... a special blue border should be used. | 38 // ... a special blue border should be used. |
41 TestBlueButton blue_button; | 39 BlueButton* blue_button = new BlueButton(nullptr, base::ASCIIToUTF16("foo")); |
42 blue_button.SetBoundsRect(gfx::Rect(gfx::Point(0, 0), | 40 EXPECT_EQ(Button::STYLE_BUTTON, blue_button->style()); |
43 blue_button.GetPreferredSize())); | 41 EXPECT_FALSE(blue_button->focus_painter()); |
44 gfx::Canvas canvas(blue_button.bounds().size(), 1.0, true); | 42 |
45 blue_button.border()->Paint(blue_button, &canvas); | 43 widget->GetContentsView()->AddChildView(blue_button); |
46 EXPECT_EQ(button.GetText(), blue_button.GetText()); | 44 blue_button->SizeToPreferredSize(); |
| 45 |
| 46 gfx::Canvas canvas(blue_button->size(), 1.0, true); |
| 47 blue_button->border()->Paint(*blue_button, &canvas); |
| 48 EXPECT_EQ(button->GetText(), blue_button->GetText()); |
| 49 EXPECT_EQ(button->size(), blue_button->size()); |
47 EXPECT_FALSE(gfx::BitmapsAreEqual(button_canvas.ExtractImageRep().sk_bitmap(), | 50 EXPECT_FALSE(gfx::BitmapsAreEqual(button_canvas.ExtractImageRep().sk_bitmap(), |
48 canvas.ExtractImageRep().sk_bitmap())); | 51 canvas.ExtractImageRep().sk_bitmap())); |
49 | 52 |
50 // Make sure it's still used after the native theme "changes". | 53 widget->CloseNow(); |
51 blue_button.OnNativeThemeChanged(NULL); | |
52 gfx::Canvas canvas2(blue_button.bounds().size(), 1.0, true); | |
53 blue_button.border()->Paint(blue_button, &canvas2); | |
54 | |
55 EXPECT_TRUE(gfx::BitmapsAreEqual(canvas.ExtractImageRep().sk_bitmap(), | |
56 canvas2.ExtractImageRep().sk_bitmap())); | |
57 } | 54 } |
58 | 55 |
59 } // namespace views | 56 } // namespace views |
OLD | NEW |