Chromium Code Reviews| 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 "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "cc/paint/skia_paint_canvas.h" | |
| 10 #include "ui/base/material_design/material_design_controller.h" | 11 #include "ui/base/material_design/material_design_controller.h" |
| 11 #include "ui/gfx/canvas.h" | 12 #include "ui/gfx/canvas.h" |
| 12 #include "ui/gfx/skia_util.h" | 13 #include "ui/gfx/skia_util.h" |
| 13 #include "ui/views/controls/button/label_button_border.h" | 14 #include "ui/views/controls/button/label_button_border.h" |
| 14 #include "ui/views/test/widget_test.h" | 15 #include "ui/views/test/widget_test.h" |
| 15 | 16 |
| 16 namespace views { | 17 namespace views { |
| 17 | 18 |
| 18 using BlueButtonTest = test::WidgetTest; | 19 using BlueButtonTest = test::WidgetTest; |
| 19 | 20 |
| 20 TEST_F(BlueButtonTest, Border) { | 21 TEST_F(BlueButtonTest, Border) { |
| 21 // The buttons must be added to a Widget so that borders are correctly | 22 // The buttons must be added to a Widget so that borders are correctly |
| 22 // applied once the NativeTheme is determined. | 23 // applied once the NativeTheme is determined. |
| 23 Widget* widget = CreateTopLevelPlatformWidget(); | 24 Widget* widget = CreateTopLevelPlatformWidget(); |
| 24 | 25 |
| 25 // Compared to a normal LabelButton... | 26 // Compared to a normal LabelButton... |
| 26 LabelButton* button = new LabelButton(nullptr, base::ASCIIToUTF16("foo")); | 27 LabelButton* button = new LabelButton(nullptr, base::ASCIIToUTF16("foo")); |
| 27 EXPECT_EQ(Button::STYLE_TEXTBUTTON, button->style()); | 28 EXPECT_EQ(Button::STYLE_TEXTBUTTON, button->style()); |
| 28 // Focus painter by default. | 29 // Focus painter by default. |
| 29 EXPECT_TRUE(button->focus_painter()); | 30 EXPECT_TRUE(button->focus_painter()); |
| 30 | 31 |
| 31 // Switch to the same style as BlueButton for a more compelling comparison. | 32 // Switch to the same style as BlueButton for a more compelling comparison. |
| 32 button->SetStyle(Button::STYLE_BUTTON); | 33 button->SetStyle(Button::STYLE_BUTTON); |
| 33 EXPECT_EQ(Button::STYLE_BUTTON, button->style()); | 34 EXPECT_EQ(Button::STYLE_BUTTON, button->style()); |
| 34 EXPECT_FALSE(button->focus_painter()); | 35 EXPECT_FALSE(button->focus_painter()); |
| 35 | 36 |
| 36 widget->GetContentsView()->AddChildView(button); | 37 widget->GetContentsView()->AddChildView(button); |
| 37 button->SizeToPreferredSize(); | 38 button->SizeToPreferredSize(); |
| 38 gfx::Canvas button_canvas(button->size(), 1.0, true); | 39 |
| 40 SkBitmap button_bitmap; | |
| 41 button_bitmap.allocN32Pixels(button->size().width(), button->size().height(), | |
| 42 true); | |
|
sadrul
2017/03/21 00:15:20
true /* is_opaque */?
(I see the old code did not
danakj
2017/03/21 15:25:56
Done.
| |
| 43 cc::SkiaPaintCanvas button_paint_canvas(button_bitmap); | |
| 44 gfx::Canvas button_canvas(&button_paint_canvas, 1.f); | |
| 39 button->border()->Paint(*button, &button_canvas); | 45 button->border()->Paint(*button, &button_canvas); |
| 40 | 46 |
| 41 // ... a special blue border should be used. | 47 // ... a special blue border should be used. |
| 42 BlueButton* blue_button = new BlueButton(nullptr, base::ASCIIToUTF16("foo")); | 48 BlueButton* blue_button = new BlueButton(nullptr, base::ASCIIToUTF16("foo")); |
| 43 EXPECT_EQ(Button::STYLE_BUTTON, blue_button->style()); | 49 EXPECT_EQ(Button::STYLE_BUTTON, blue_button->style()); |
| 44 EXPECT_FALSE(blue_button->focus_painter()); | 50 EXPECT_FALSE(blue_button->focus_painter()); |
| 45 | 51 |
| 46 widget->GetContentsView()->AddChildView(blue_button); | 52 widget->GetContentsView()->AddChildView(blue_button); |
| 47 blue_button->SizeToPreferredSize(); | 53 blue_button->SizeToPreferredSize(); |
| 48 #if defined(OS_MACOSX) | 54 #if defined(OS_MACOSX) |
| 49 // On Mac, themed STYLE_BUTTON buttons provide blue theming for dialog-default | 55 // On Mac, themed STYLE_BUTTON buttons provide blue theming for dialog-default |
| 50 // buttons. This makes it unlikely that they will appear together with a | 56 // buttons. This makes it unlikely that they will appear together with a |
| 51 // BlueButton on the same dialog. So the sizes don't really need to be | 57 // BlueButton on the same dialog. So the sizes don't really need to be |
| 52 // consistent. However, for the purposes of this test (e.g. to ensure we don't | 58 // consistent. However, for the purposes of this test (e.g. to ensure we don't |
| 53 // accidentally make BlueButtons look like themed buttons on Mac), force the | 59 // accidentally make BlueButtons look like themed buttons on Mac), force the |
| 54 // sizes to match (ignoring the minimum size) so that the bitmaps can be | 60 // sizes to match (ignoring the minimum size) so that the bitmaps can be |
| 55 // compared. | 61 // compared. |
| 56 EXPECT_NE(button->size(), blue_button->size()); // Verify this is needed. | 62 EXPECT_NE(button->size(), blue_button->size()); // Verify this is needed. |
| 57 blue_button->SetSize(button->size()); | 63 blue_button->SetSize(button->size()); |
| 58 #endif | 64 #endif |
| 59 | 65 |
| 60 gfx::Canvas canvas(blue_button->size(), 1.0, true); | 66 SkBitmap blue_button_bitmap; |
| 61 blue_button->border()->Paint(*blue_button, &canvas); | 67 blue_button_bitmap.allocN32Pixels(blue_button->size().width(), |
| 68 blue_button->size().height(), true); | |
| 69 cc::SkiaPaintCanvas blue_button_paint_canvas(blue_button_bitmap); | |
| 70 gfx::Canvas blue_button_canvas(&blue_button_paint_canvas, 1.f); | |
| 71 blue_button->border()->Paint(*blue_button, &blue_button_canvas); | |
| 62 EXPECT_EQ(button->GetText(), blue_button->GetText()); | 72 EXPECT_EQ(button->GetText(), blue_button->GetText()); |
| 63 EXPECT_EQ(button->size(), blue_button->size()); | 73 EXPECT_EQ(button->size(), blue_button->size()); |
| 64 EXPECT_FALSE(gfx::BitmapsAreEqual(button_canvas.ExtractImageRep().sk_bitmap(), | 74 EXPECT_FALSE(gfx::BitmapsAreEqual(button_bitmap, blue_button_bitmap)); |
| 65 canvas.ExtractImageRep().sk_bitmap())); | |
| 66 | 75 |
| 67 widget->CloseNow(); | 76 widget->CloseNow(); |
| 68 } | 77 } |
| 69 | 78 |
| 70 } // namespace views | 79 } // namespace views |
| OLD | NEW |