Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: ui/views/view_unittest.cc

Issue 17127003: Refine DialogClientView button code and unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove unnecessary override. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 <map> 5 #include <map>
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/rand_util.h" 8 #include "base/rand_util.h"
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "grit/ui_strings.h" 11 #include "grit/ui_strings.h"
12 #include "testing/gmock/include/gmock/gmock.h" 12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "ui/base/accelerators/accelerator.h" 13 #include "ui/base/accelerators/accelerator.h"
14 #include "ui/base/clipboard/clipboard.h" 14 #include "ui/base/clipboard/clipboard.h"
15 #include "ui/base/events/event.h" 15 #include "ui/base/events/event.h"
16 #include "ui/base/keycodes/keyboard_codes.h" 16 #include "ui/base/keycodes/keyboard_codes.h"
17 #include "ui/base/l10n/l10n_util.h" 17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/base/models/simple_menu_model.h"
19 #include "ui/compositor/compositor.h" 18 #include "ui/compositor/compositor.h"
20 #include "ui/compositor/layer.h" 19 #include "ui/compositor/layer.h"
21 #include "ui/compositor/layer_animator.h" 20 #include "ui/compositor/layer_animator.h"
22 #include "ui/gfx/canvas.h" 21 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/path.h" 22 #include "ui/gfx/path.h"
24 #include "ui/gfx/transform.h" 23 #include "ui/gfx/transform.h"
25 #include "ui/views/background.h" 24 #include "ui/views/background.h"
26 #include "ui/views/controls/button/button_dropdown.h"
27 #include "ui/views/controls/button/checkbox.h"
28 #include "ui/views/controls/button/label_button.h"
29 #include "ui/views/controls/native/native_view_host.h" 25 #include "ui/views/controls/native/native_view_host.h"
30 #include "ui/views/controls/scroll_view.h" 26 #include "ui/views/controls/scroll_view.h"
31 #include "ui/views/controls/textfield/textfield.h" 27 #include "ui/views/controls/textfield/textfield.h"
32 #include "ui/views/focus/accelerator_handler.h" 28 #include "ui/views/focus/accelerator_handler.h"
33 #include "ui/views/focus/view_storage.h" 29 #include "ui/views/focus/view_storage.h"
34 #include "ui/views/test/views_test_base.h" 30 #include "ui/views/test/views_test_base.h"
35 #include "ui/views/view.h" 31 #include "ui/views/view.h"
36 #include "ui/views/views_delegate.h" 32 #include "ui/views/views_delegate.h"
37 #include "ui/views/widget/native_widget.h" 33 #include "ui/views/widget/native_widget.h"
38 #include "ui/views/widget/root_view.h" 34 #include "ui/views/widget/root_view.h"
(...skipping 1501 matching lines...) Expand 10 before | Expand all | Expand 10 after
1540 ::SendMessage(view_with_controls->text_field_->GetTestingHandle(), 1536 ::SendMessage(view_with_controls->text_field_->GetTestingHandle(),
1541 WM_MOUSEWHEEL, MAKEWPARAM(0, -20), MAKELPARAM(50, 50)); 1537 WM_MOUSEWHEEL, MAKEWPARAM(0, -20), MAKELPARAM(50, 50));
1542 EXPECT_EQ(80, scroll_view->GetVisibleRect().y()); 1538 EXPECT_EQ(80, scroll_view->GetVisibleRect().y());
1543 1539
1544 window1->CloseNow(); 1540 window1->CloseNow();
1545 window2->CloseNow(); 1541 window2->CloseNow();
1546 } 1542 }
1547 #endif 1543 #endif
1548 1544
1549 //////////////////////////////////////////////////////////////////////////////// 1545 ////////////////////////////////////////////////////////////////////////////////
1550 // Dialogs' default button
1551 ////////////////////////////////////////////////////////////////////////////////
1552
1553 class MockMenuModel : public ui::MenuModel {
1554 public:
1555 MOCK_CONST_METHOD0(HasIcons, bool());
1556 MOCK_CONST_METHOD0(GetItemCount, int());
1557 MOCK_CONST_METHOD1(GetTypeAt, ItemType(int index));
1558 MOCK_CONST_METHOD1(GetSeparatorTypeAt, ui::MenuSeparatorType(int index));
1559 MOCK_CONST_METHOD1(GetCommandIdAt, int(int index));
1560 MOCK_CONST_METHOD1(GetLabelAt, string16(int index));
1561 MOCK_CONST_METHOD1(IsItemDynamicAt, bool(int index));
1562 MOCK_CONST_METHOD1(GetLabelFontAt, const gfx::Font* (int index));
1563 MOCK_CONST_METHOD2(GetAcceleratorAt, bool(int index,
1564 ui::Accelerator* accelerator));
1565 MOCK_CONST_METHOD1(IsItemCheckedAt, bool(int index));
1566 MOCK_CONST_METHOD1(GetGroupIdAt, int(int index));
1567 MOCK_METHOD2(GetIconAt, bool(int index, gfx::Image* icon));
1568 MOCK_CONST_METHOD1(GetButtonMenuItemAt, ui::ButtonMenuItemModel*(int index));
1569 MOCK_CONST_METHOD1(IsEnabledAt, bool(int index));
1570 MOCK_CONST_METHOD1(IsVisibleAt, bool(int index));
1571 MOCK_CONST_METHOD1(GetSubmenuModelAt, MenuModel*(int index));
1572 MOCK_METHOD1(HighlightChangedTo, void(int index));
1573 MOCK_METHOD1(ActivatedAt, void(int index));
1574 MOCK_METHOD2(ActivatedAt, void(int index, int disposition));
1575 MOCK_METHOD0(MenuWillShow, void());
1576 MOCK_METHOD0(MenuClosed, void());
1577 MOCK_METHOD1(SetMenuModelDelegate, void(ui::MenuModelDelegate* delegate));
1578 MOCK_CONST_METHOD0(GetMenuModelDelegate, ui::MenuModelDelegate*());
1579 MOCK_METHOD3(GetModelAndIndexForCommandId, bool(int command_id,
1580 MenuModel** model, int* index));
1581 };
1582
1583 class TestDialog : public DialogDelegateView, public ButtonListener {
1584 public:
1585 explicit TestDialog(MockMenuModel* mock_menu_model)
1586 : button1_(NULL),
1587 button2_(NULL),
1588 checkbox_(NULL),
1589 button_drop_(NULL),
1590 last_pressed_button_(NULL),
1591 mock_menu_model_(mock_menu_model),
1592 canceled_(false),
1593 oked_(false),
1594 closeable_(false) {
1595 button1_ = new LabelButton(this, ASCIIToUTF16("Button1"));
1596 button2_ = new LabelButton(this, ASCIIToUTF16("Button2"));
1597 checkbox_ = new Checkbox(ASCIIToUTF16("My checkbox"));
1598 button_drop_ = new ButtonDropDown(this, mock_menu_model_);
1599 AddChildView(button1_);
1600 AddChildView(button2_);
1601 AddChildView(checkbox_);
1602 AddChildView(button_drop_);
1603 }
1604
1605 virtual ~TestDialog() {}
1606
1607 void TearDown() {
1608 // Now we can close safely.
1609 closeable_ = true;
1610 GetWidget()->Close();
1611 }
1612
1613 // Prevent the dialog from really closing (so we can click the OK/Cancel
1614 // buttons to our heart's content).
1615 virtual bool Cancel() OVERRIDE {
1616 canceled_ = true;
1617 return closeable_;
1618 }
1619 virtual bool Accept() OVERRIDE {
1620 oked_ = true;
1621 return closeable_;
1622 }
1623
1624 // ButtonListener implementation.
1625 virtual void ButtonPressed(Button* sender, const ui::Event& event) OVERRIDE {
1626 last_pressed_button_ = sender;
1627 }
1628
1629 void ResetStates() {
1630 oked_ = false;
1631 canceled_ = false;
1632 last_pressed_button_ = NULL;
1633 }
1634
1635 // Set up expectations for methods that are called when an (empty) menu is
1636 // shown from a drop down button.
1637 void ExpectShowDropMenu() {
1638 if (mock_menu_model_) {
1639 EXPECT_CALL(*mock_menu_model_, HasIcons());
1640 EXPECT_CALL(*mock_menu_model_, GetItemCount());
1641 EXPECT_CALL(*mock_menu_model_, MenuClosed());
1642 }
1643 }
1644
1645 LabelButton* button1_;
1646 LabelButton* button2_;
1647 Checkbox* checkbox_;
1648 ButtonDropDown* button_drop_;
1649 Button* last_pressed_button_;
1650 MockMenuModel* mock_menu_model_;
1651
1652 bool canceled_;
1653 bool oked_;
1654 bool closeable_;
1655
1656 private:
1657 DISALLOW_COPY_AND_ASSIGN(TestDialog);
1658 };
1659
1660 class DefaultButtonTest : public ViewTest {
1661 public:
1662 enum ButtonID { OK, CANCEL, BUTTON1, BUTTON2, };
1663
1664 DefaultButtonTest() : dialog_(NULL) {}
1665 virtual ~DefaultButtonTest() {}
1666
1667 virtual void SetUp() OVERRIDE {
1668 ViewTest::SetUp();
1669 dialog_ = new TestDialog(NULL);
1670 DialogDelegate::CreateDialogWidget(dialog_, GetContext(), NULL)->Show();
1671 }
1672
1673 virtual void TearDown() OVERRIDE {
1674 dialog_->TearDown();
1675 ViewTest::TearDown();
1676 }
1677
1678 protected:
1679 void SimulatePressingEnterAndCheckDefaultButton(ButtonID button_id) {
1680 ui::KeyEvent event(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false);
1681 dialog_->GetFocusManager()->OnKeyEvent(event);
1682 switch (button_id) {
1683 case OK:
1684 EXPECT_TRUE(dialog_->oked_);
1685 EXPECT_FALSE(dialog_->canceled_);
1686 EXPECT_FALSE(dialog_->last_pressed_button_);
1687 break;
1688 case CANCEL:
1689 EXPECT_FALSE(dialog_->oked_);
1690 EXPECT_TRUE(dialog_->canceled_);
1691 EXPECT_FALSE(dialog_->last_pressed_button_);
1692 break;
1693 case BUTTON1:
1694 EXPECT_FALSE(dialog_->oked_);
1695 EXPECT_FALSE(dialog_->canceled_);
1696 EXPECT_TRUE(dialog_->last_pressed_button_ == dialog_->button1_);
1697 break;
1698 case BUTTON2:
1699 EXPECT_FALSE(dialog_->oked_);
1700 EXPECT_FALSE(dialog_->canceled_);
1701 EXPECT_TRUE(dialog_->last_pressed_button_ == dialog_->button2_);
1702 break;
1703 }
1704 dialog_->ResetStates();
1705 }
1706
1707 TestDialog* dialog_;
1708
1709 private:
1710 DISALLOW_COPY_AND_ASSIGN(DefaultButtonTest);
1711 };
1712
1713 TEST_F(DefaultButtonTest, DialogDefaultButtonTest) {
1714 DialogClientView* client_view = dialog_->GetDialogClientView();
1715 LabelButton* ok_button = client_view->ok_button();
1716 LabelButton* cancel_button = client_view->cancel_button();
1717
1718 // Window has just been shown, we expect the default button specified in the
1719 // DialogDelegate.
1720 EXPECT_TRUE(ok_button->is_default());
1721
1722 // Simulate pressing enter, that should trigger the OK button.
1723 SimulatePressingEnterAndCheckDefaultButton(OK);
1724
1725 // Simulate focusing another button, it should become the default button.
1726 client_view->OnWillChangeFocus(ok_button, dialog_->button1_);
1727 EXPECT_FALSE(ok_button->is_default());
1728 EXPECT_TRUE(dialog_->button1_->is_default());
1729 // Simulate pressing enter, that should trigger button1.
1730 SimulatePressingEnterAndCheckDefaultButton(BUTTON1);
1731
1732 // Now select something that is not a button, the OK should become the default
1733 // button again.
1734 client_view->OnWillChangeFocus(dialog_->button1_, dialog_->checkbox_);
1735 EXPECT_TRUE(ok_button->is_default());
1736 EXPECT_FALSE(dialog_->button1_->is_default());
1737 SimulatePressingEnterAndCheckDefaultButton(OK);
1738
1739 // Select yet another button.
1740 client_view->OnWillChangeFocus(dialog_->checkbox_, dialog_->button2_);
1741 EXPECT_FALSE(ok_button->is_default());
1742 EXPECT_FALSE(dialog_->button1_->is_default());
1743 EXPECT_TRUE(dialog_->button2_->is_default());
1744 SimulatePressingEnterAndCheckDefaultButton(BUTTON2);
1745
1746 // Focus nothing.
1747 client_view->OnWillChangeFocus(dialog_->button2_, NULL);
1748 EXPECT_TRUE(ok_button->is_default());
1749 EXPECT_FALSE(dialog_->button1_->is_default());
1750 EXPECT_FALSE(dialog_->button2_->is_default());
1751 SimulatePressingEnterAndCheckDefaultButton(OK);
1752
1753 // Focus the cancel button.
1754 client_view->OnWillChangeFocus(NULL, cancel_button);
1755 EXPECT_FALSE(ok_button->is_default());
1756 EXPECT_TRUE(cancel_button->is_default());
1757 EXPECT_FALSE(dialog_->button1_->is_default());
1758 EXPECT_FALSE(dialog_->button2_->is_default());
1759 SimulatePressingEnterAndCheckDefaultButton(CANCEL);
1760 }
1761
1762 class ButtonDropDownTest : public ViewTest {
1763 public:
1764 ButtonDropDownTest() : dialog_(NULL), button_as_view_(NULL) {}
1765 virtual ~ButtonDropDownTest() {}
1766
1767 virtual void SetUp() OVERRIDE {
1768 ViewTest::SetUp();
1769 dialog_ = new TestDialog(new MockMenuModel());
1770 DialogDelegate::CreateDialogWidget(dialog_, GetContext(), NULL)->Show();
1771 dialog_->button_drop_->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
1772 // We have to cast the button back into a View in order to invoke it's
1773 // OnMouseReleased method.
1774 button_as_view_ = static_cast<View*>(dialog_->button_drop_);
1775 }
1776
1777 virtual void TearDown() OVERRIDE {
1778 dialog_->TearDown();
1779 ViewTest::TearDown();
1780 }
1781
1782 TestDialog* dialog_;
1783 // This is owned by dialog_.
1784 View* button_as_view_;
1785
1786 private:
1787 DISALLOW_COPY_AND_ASSIGN(ButtonDropDownTest);
1788 };
1789
1790 // Ensure that regular clicks on the drop down button still work. (i.e. - the
1791 // click events are processed and the listener gets the click)
1792 TEST_F(ButtonDropDownTest, RegularClickTest) {
1793 gfx::Point point(1, 1);
1794 ui::MouseEvent press_event(ui::ET_MOUSE_PRESSED, point, point,
1795 ui::EF_LEFT_MOUSE_BUTTON);
1796 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED, point, point,
1797 ui::EF_LEFT_MOUSE_BUTTON);
1798 button_as_view_->OnMousePressed(press_event);
1799 button_as_view_->OnMouseReleased(release_event);
1800 EXPECT_EQ(dialog_->last_pressed_button_, dialog_->button_drop_);
1801 }
1802
1803 ////////////////////////////////////////////////////////////////////////////////
1804 // Native view hierachy 1546 // Native view hierachy
1805 //////////////////////////////////////////////////////////////////////////////// 1547 ////////////////////////////////////////////////////////////////////////////////
1806 class TestNativeViewHierarchy : public View { 1548 class TestNativeViewHierarchy : public View {
1807 public: 1549 public:
1808 TestNativeViewHierarchy() { 1550 TestNativeViewHierarchy() {
1809 } 1551 }
1810 1552
1811 virtual void NativeViewHierarchyChanged( 1553 virtual void NativeViewHierarchyChanged(
1812 bool attached, 1554 bool attached,
1813 gfx::NativeView native_view, 1555 gfx::NativeView native_view,
(...skipping 1620 matching lines...) Expand 10 before | Expand all | Expand 10 after
3434 const std::vector<ui::Layer*>& child_layers_post = root_layer->children(); 3176 const std::vector<ui::Layer*>& child_layers_post = root_layer->children();
3435 ASSERT_EQ(3u, child_layers_post.size()); 3177 ASSERT_EQ(3u, child_layers_post.size());
3436 EXPECT_EQ(v1->layer(), child_layers_post[0]); 3178 EXPECT_EQ(v1->layer(), child_layers_post[0]);
3437 EXPECT_EQ(v2->layer(), child_layers_post[1]); 3179 EXPECT_EQ(v2->layer(), child_layers_post[1]);
3438 EXPECT_EQ(v1_old_layer, child_layers_post[2]); 3180 EXPECT_EQ(v1_old_layer, child_layers_post[2]);
3439 } 3181 }
3440 3182
3441 #endif // USE_AURA 3183 #endif // USE_AURA
3442 3184
3443 } // namespace views 3185 } // namespace views
OLDNEW
« no previous file with comments | « no previous file | ui/views/window/dialog_client_view.cc » ('j') | ui/views/window/dialog_client_view.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698