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 "ui/views/controls/button/custom_button.h" 6 7 #include "testing/gtest/include/gtest/gtest.h" 8 #include "ui/base/layout.h" 9 #include "ui/gfx/screen.h" 10 #include "ui/views/test/views_test_base.h" 11 12 #if defined(USE_AURA) 13 #include "ui/aura/root_window.h" 14 #include "ui/aura/test/test_cursor_client.h" 15 #endif 16 17 namespace views { 18 19 namespace { 20 21 class TestCustomButton : public CustomButton { 22 public: 23 explicit TestCustomButton(ButtonListener* listener) 24 : CustomButton(listener) { 25 } 26 27 virtual ~TestCustomButton() {} 28 29 private: 30 DISALLOW_COPY_AND_ASSIGN(TestCustomButton); 31 }; 32 33 } // namespace 34 35 typedef ViewsTestBase CustomButtonTest; 36 37 // Tests that hover state changes correctly when visiblity/enableness changes. 38 TEST_F(CustomButtonTest, HoverStateOnVisibilityChange) { 39 // Create a widget so that the CustomButton can query the hover state 40 // correctly. 41 scoped_ptr<Widget> widget(new Widget); 42 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); 43 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 44 params.bounds = gfx::Rect(0, 0, 650, 650); 45 widget->Init(params); 46 widget->Show(); 47 48 #if defined(USE_AURA) 49 aura::test::TestCursorClient cursor_client( 50 widget->GetNativeView()->GetRootWindow()); 51 #endif 52 53 // Position the widget in a way so that it is under the cursor. 54 gfx::Point cursor = gfx::Screen::GetScreenFor( 55 widget->GetNativeView())->GetCursorScreenPoint(); 56 gfx::Rect widget_bounds = widget->GetWindowBoundsInScreen(); 57 widget_bounds.set_origin(cursor); 58 widget->SetBounds(widget_bounds); 59 60 TestCustomButton* button = new TestCustomButton(NULL); 61 widget->SetContentsView(button); 62 63 gfx::Point center(10, 10); 64 button->OnMousePressed(ui::MouseEvent(ui::ET_MOUSE_PRESSED, center, center, 65 ui::EF_LEFT_MOUSE_BUTTON)); 66 EXPECT_EQ(CustomButton::STATE_PRESSED, button->state()); 67 68 button->OnMouseReleased(ui::MouseEvent(ui::ET_MOUSE_RELEASED, center, center, 69 ui::EF_LEFT_MOUSE_BUTTON)); 70 EXPECT_EQ(CustomButton::STATE_HOVERED, button->state()); 71 72 button->SetEnabled(false); 73 EXPECT_EQ(CustomButton::STATE_DISABLED, button->state()); 74 75 button->SetEnabled(true); 76 EXPECT_EQ(CustomButton::STATE_HOVERED, button->state()); 77 78 button->SetVisible(false); 79 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state()); 80 81 button->SetVisible(true); 82 EXPECT_EQ(CustomButton::STATE_HOVERED, button->state()); 83 84 #if defined(USE_AURA) 85 // In Aura views, no new hover effects are invoked if mouse events 86 // are disabled. 87 cursor_client.DisableMouseEvents(); 88 89 button->SetEnabled(false); 90 EXPECT_EQ(CustomButton::STATE_DISABLED, button->state()); 91 92 button->SetEnabled(true); 93 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state()); 94 95 button->SetVisible(false); 96 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state()); 97 98 button->SetVisible(true); 99 EXPECT_EQ(CustomButton::STATE_NORMAL, button->state()); 100 #endif 101 } 102 103 } // namespace views 104