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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | ui/views/window/dialog_client_view.cc » ('j') | ui/views/window/dialog_client_view.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/view_unittest.cc
diff --git a/ui/views/view_unittest.cc b/ui/views/view_unittest.cc
index aa9457c8b116c53a7b543246336963d987524d37..91a922ecfa3839fbdccdd6be521b678d605bc87e 100644
--- a/ui/views/view_unittest.cc
+++ b/ui/views/view_unittest.cc
@@ -15,7 +15,6 @@
#include "ui/base/events/event.h"
#include "ui/base/keycodes/keyboard_codes.h"
#include "ui/base/l10n/l10n_util.h"
-#include "ui/base/models/simple_menu_model.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"
@@ -23,9 +22,6 @@
#include "ui/gfx/path.h"
#include "ui/gfx/transform.h"
#include "ui/views/background.h"
-#include "ui/views/controls/button/button_dropdown.h"
-#include "ui/views/controls/button/checkbox.h"
-#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/native/native_view_host.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/textfield/textfield.h"
@@ -1547,260 +1543,6 @@ TEST_F(ViewTest, DISABLED_RerouteMouseWheelTest) {
#endif
////////////////////////////////////////////////////////////////////////////////
-// Dialogs' default button
-////////////////////////////////////////////////////////////////////////////////
-
-class MockMenuModel : public ui::MenuModel {
- public:
- MOCK_CONST_METHOD0(HasIcons, bool());
- MOCK_CONST_METHOD0(GetItemCount, int());
- MOCK_CONST_METHOD1(GetTypeAt, ItemType(int index));
- MOCK_CONST_METHOD1(GetSeparatorTypeAt, ui::MenuSeparatorType(int index));
- MOCK_CONST_METHOD1(GetCommandIdAt, int(int index));
- MOCK_CONST_METHOD1(GetLabelAt, string16(int index));
- MOCK_CONST_METHOD1(IsItemDynamicAt, bool(int index));
- MOCK_CONST_METHOD1(GetLabelFontAt, const gfx::Font* (int index));
- MOCK_CONST_METHOD2(GetAcceleratorAt, bool(int index,
- ui::Accelerator* accelerator));
- MOCK_CONST_METHOD1(IsItemCheckedAt, bool(int index));
- MOCK_CONST_METHOD1(GetGroupIdAt, int(int index));
- MOCK_METHOD2(GetIconAt, bool(int index, gfx::Image* icon));
- MOCK_CONST_METHOD1(GetButtonMenuItemAt, ui::ButtonMenuItemModel*(int index));
- MOCK_CONST_METHOD1(IsEnabledAt, bool(int index));
- MOCK_CONST_METHOD1(IsVisibleAt, bool(int index));
- MOCK_CONST_METHOD1(GetSubmenuModelAt, MenuModel*(int index));
- MOCK_METHOD1(HighlightChangedTo, void(int index));
- MOCK_METHOD1(ActivatedAt, void(int index));
- MOCK_METHOD2(ActivatedAt, void(int index, int disposition));
- MOCK_METHOD0(MenuWillShow, void());
- MOCK_METHOD0(MenuClosed, void());
- MOCK_METHOD1(SetMenuModelDelegate, void(ui::MenuModelDelegate* delegate));
- MOCK_CONST_METHOD0(GetMenuModelDelegate, ui::MenuModelDelegate*());
- MOCK_METHOD3(GetModelAndIndexForCommandId, bool(int command_id,
- MenuModel** model, int* index));
-};
-
-class TestDialog : public DialogDelegateView, public ButtonListener {
- public:
- explicit TestDialog(MockMenuModel* mock_menu_model)
- : button1_(NULL),
- button2_(NULL),
- checkbox_(NULL),
- button_drop_(NULL),
- last_pressed_button_(NULL),
- mock_menu_model_(mock_menu_model),
- canceled_(false),
- oked_(false),
- closeable_(false) {
- button1_ = new LabelButton(this, ASCIIToUTF16("Button1"));
- button2_ = new LabelButton(this, ASCIIToUTF16("Button2"));
- checkbox_ = new Checkbox(ASCIIToUTF16("My checkbox"));
- button_drop_ = new ButtonDropDown(this, mock_menu_model_);
- AddChildView(button1_);
- AddChildView(button2_);
- AddChildView(checkbox_);
- AddChildView(button_drop_);
- }
-
- virtual ~TestDialog() {}
-
- void TearDown() {
- // Now we can close safely.
- closeable_ = true;
- GetWidget()->Close();
- }
-
- // Prevent the dialog from really closing (so we can click the OK/Cancel
- // buttons to our heart's content).
- virtual bool Cancel() OVERRIDE {
- canceled_ = true;
- return closeable_;
- }
- virtual bool Accept() OVERRIDE {
- oked_ = true;
- return closeable_;
- }
-
- // ButtonListener implementation.
- virtual void ButtonPressed(Button* sender, const ui::Event& event) OVERRIDE {
- last_pressed_button_ = sender;
- }
-
- void ResetStates() {
- oked_ = false;
- canceled_ = false;
- last_pressed_button_ = NULL;
- }
-
- // Set up expectations for methods that are called when an (empty) menu is
- // shown from a drop down button.
- void ExpectShowDropMenu() {
- if (mock_menu_model_) {
- EXPECT_CALL(*mock_menu_model_, HasIcons());
- EXPECT_CALL(*mock_menu_model_, GetItemCount());
- EXPECT_CALL(*mock_menu_model_, MenuClosed());
- }
- }
-
- LabelButton* button1_;
- LabelButton* button2_;
- Checkbox* checkbox_;
- ButtonDropDown* button_drop_;
- Button* last_pressed_button_;
- MockMenuModel* mock_menu_model_;
-
- bool canceled_;
- bool oked_;
- bool closeable_;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(TestDialog);
-};
-
-class DefaultButtonTest : public ViewTest {
- public:
- enum ButtonID { OK, CANCEL, BUTTON1, BUTTON2, };
-
- DefaultButtonTest() : dialog_(NULL) {}
- virtual ~DefaultButtonTest() {}
-
- virtual void SetUp() OVERRIDE {
- ViewTest::SetUp();
- dialog_ = new TestDialog(NULL);
- DialogDelegate::CreateDialogWidget(dialog_, GetContext(), NULL)->Show();
- }
-
- virtual void TearDown() OVERRIDE {
- dialog_->TearDown();
- ViewTest::TearDown();
- }
-
- protected:
- void SimulatePressingEnterAndCheckDefaultButton(ButtonID button_id) {
- ui::KeyEvent event(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false);
- dialog_->GetFocusManager()->OnKeyEvent(event);
- switch (button_id) {
- case OK:
- EXPECT_TRUE(dialog_->oked_);
- EXPECT_FALSE(dialog_->canceled_);
- EXPECT_FALSE(dialog_->last_pressed_button_);
- break;
- case CANCEL:
- EXPECT_FALSE(dialog_->oked_);
- EXPECT_TRUE(dialog_->canceled_);
- EXPECT_FALSE(dialog_->last_pressed_button_);
- break;
- case BUTTON1:
- EXPECT_FALSE(dialog_->oked_);
- EXPECT_FALSE(dialog_->canceled_);
- EXPECT_TRUE(dialog_->last_pressed_button_ == dialog_->button1_);
- break;
- case BUTTON2:
- EXPECT_FALSE(dialog_->oked_);
- EXPECT_FALSE(dialog_->canceled_);
- EXPECT_TRUE(dialog_->last_pressed_button_ == dialog_->button2_);
- break;
- }
- dialog_->ResetStates();
- }
-
- TestDialog* dialog_;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(DefaultButtonTest);
-};
-
-TEST_F(DefaultButtonTest, DialogDefaultButtonTest) {
- DialogClientView* client_view = dialog_->GetDialogClientView();
- LabelButton* ok_button = client_view->ok_button();
- LabelButton* cancel_button = client_view->cancel_button();
-
- // Window has just been shown, we expect the default button specified in the
- // DialogDelegate.
- EXPECT_TRUE(ok_button->is_default());
-
- // Simulate pressing enter, that should trigger the OK button.
- SimulatePressingEnterAndCheckDefaultButton(OK);
-
- // Simulate focusing another button, it should become the default button.
- client_view->OnWillChangeFocus(ok_button, dialog_->button1_);
- EXPECT_FALSE(ok_button->is_default());
- EXPECT_TRUE(dialog_->button1_->is_default());
- // Simulate pressing enter, that should trigger button1.
- SimulatePressingEnterAndCheckDefaultButton(BUTTON1);
-
- // Now select something that is not a button, the OK should become the default
- // button again.
- client_view->OnWillChangeFocus(dialog_->button1_, dialog_->checkbox_);
- EXPECT_TRUE(ok_button->is_default());
- EXPECT_FALSE(dialog_->button1_->is_default());
- SimulatePressingEnterAndCheckDefaultButton(OK);
-
- // Select yet another button.
- client_view->OnWillChangeFocus(dialog_->checkbox_, dialog_->button2_);
- EXPECT_FALSE(ok_button->is_default());
- EXPECT_FALSE(dialog_->button1_->is_default());
- EXPECT_TRUE(dialog_->button2_->is_default());
- SimulatePressingEnterAndCheckDefaultButton(BUTTON2);
-
- // Focus nothing.
- client_view->OnWillChangeFocus(dialog_->button2_, NULL);
- EXPECT_TRUE(ok_button->is_default());
- EXPECT_FALSE(dialog_->button1_->is_default());
- EXPECT_FALSE(dialog_->button2_->is_default());
- SimulatePressingEnterAndCheckDefaultButton(OK);
-
- // Focus the cancel button.
- client_view->OnWillChangeFocus(NULL, cancel_button);
- EXPECT_FALSE(ok_button->is_default());
- EXPECT_TRUE(cancel_button->is_default());
- EXPECT_FALSE(dialog_->button1_->is_default());
- EXPECT_FALSE(dialog_->button2_->is_default());
- SimulatePressingEnterAndCheckDefaultButton(CANCEL);
-}
-
-class ButtonDropDownTest : public ViewTest {
- public:
- ButtonDropDownTest() : dialog_(NULL), button_as_view_(NULL) {}
- virtual ~ButtonDropDownTest() {}
-
- virtual void SetUp() OVERRIDE {
- ViewTest::SetUp();
- dialog_ = new TestDialog(new MockMenuModel());
- DialogDelegate::CreateDialogWidget(dialog_, GetContext(), NULL)->Show();
- dialog_->button_drop_->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
- // We have to cast the button back into a View in order to invoke it's
- // OnMouseReleased method.
- button_as_view_ = static_cast<View*>(dialog_->button_drop_);
- }
-
- virtual void TearDown() OVERRIDE {
- dialog_->TearDown();
- ViewTest::TearDown();
- }
-
- TestDialog* dialog_;
- // This is owned by dialog_.
- View* button_as_view_;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(ButtonDropDownTest);
-};
-
-// Ensure that regular clicks on the drop down button still work. (i.e. - the
-// click events are processed and the listener gets the click)
-TEST_F(ButtonDropDownTest, RegularClickTest) {
- gfx::Point point(1, 1);
- ui::MouseEvent press_event(ui::ET_MOUSE_PRESSED, point, point,
- ui::EF_LEFT_MOUSE_BUTTON);
- ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED, point, point,
- ui::EF_LEFT_MOUSE_BUTTON);
- button_as_view_->OnMousePressed(press_event);
- button_as_view_->OnMouseReleased(release_event);
- EXPECT_EQ(dialog_->last_pressed_button_, dialog_->button_drop_);
-}
-
-////////////////////////////////////////////////////////////////////////////////
// Native view hierachy
////////////////////////////////////////////////////////////////////////////////
class TestNativeViewHierarchy : public View {
« 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