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

Unified Diff: ui/views/window/dialog_delegate_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
Index: ui/views/window/dialog_delegate_unittest.cc
diff --git a/ui/views/window/dialog_delegate_unittest.cc b/ui/views/window/dialog_delegate_unittest.cc
index 29879191e9b3937e6dbd2c9ef43cd4c0aa608953..91c98e5f34b39f298407baa18295066dac323518 100644
--- a/ui/views/window/dialog_delegate_unittest.cc
+++ b/ui/views/window/dialog_delegate_unittest.cc
@@ -2,37 +2,209 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/strings/utf_string_conversions.h"
#include "ui/base/hit_test.h"
#include "ui/views/bubble/bubble_border.h"
#include "ui/views/bubble/bubble_frame_view.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/test/views_test_base.h"
#include "ui/views/widget/widget.h"
+#include "ui/views/window/dialog_client_view.h"
#include "ui/views/window/dialog_delegate.h"
namespace views {
-typedef ViewsTestBase DialogTest;
-
namespace {
-class TestDialog : public DialogDelegateView {
+class TestDialog : public DialogDelegateView, public ButtonListener {
public:
- TestDialog() {}
+ TestDialog()
+ : canceled_(false),
+ accepted_(false),
+ closeable_(false),
+ last_pressed_button_(NULL) {}
virtual ~TestDialog() {}
- // BubbleDelegateView overrides:
+ // DialogDelegateView overrides:
+ virtual bool Cancel() OVERRIDE {
+ canceled_ = true;
+ return closeable_;
+ }
+ virtual bool Accept() OVERRIDE {
+ accepted_ = true;
+ return closeable_;
+ }
+
+ // View override:
virtual gfx::Size GetPreferredSize() OVERRIDE { return gfx::Size(200, 200); }
+ // ButtonListener override:
+ virtual void ButtonPressed(Button* sender, const ui::Event& event) OVERRIDE {
+ last_pressed_button_ = sender;
+ }
+
+ Button* last_pressed_button() const { return last_pressed_button_; }
+
+ void PressEnterAndCheckStates(Button* button) {
+ ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false);
+ GetFocusManager()->OnKeyEvent(key_event);
+ const DialogClientView* client_view = GetDialogClientView();
+ EXPECT_EQ(canceled_, client_view->cancel_button()->is_default());
+ EXPECT_EQ(accepted_, client_view->ok_button()->is_default());
+ // This view does not listen for ok or cancel clicks, DialogClientView does.
+ CheckAndResetStates(button == client_view->cancel_button(),
+ button == client_view->ok_button(),
+ (canceled_ || accepted_ ) ? NULL : button);
+ }
+
+ void CheckAndResetStates(bool canceled, bool accepted, Button* last_pressed) {
+ EXPECT_EQ(canceled, canceled_);
+ canceled_ = false;
+ EXPECT_EQ(accepted, accepted_);
+ accepted_ = false;
+ EXPECT_EQ(last_pressed, last_pressed_button_);
+ last_pressed_button_ = NULL;
+ }
+
+ void TearDown() {
+ closeable_ = true;
+ GetWidget()->Close();
+ }
+
private:
+ bool canceled_;
+ bool accepted_;
+ // Prevent the dialog from closing, for repeated ok and cancel button clicks.
+ bool closeable_;
+ Button* last_pressed_button_;
+
DISALLOW_COPY_AND_ASSIGN(TestDialog);
};
+class DialogTest : public ViewsTestBase {
+ public:
+ DialogTest() : dialog_(NULL) {}
+ virtual ~DialogTest() {}
+
+ virtual void SetUp() OVERRIDE {
+ ViewsTestBase::SetUp();
+ dialog_ = new TestDialog();
+ DialogDelegate::CreateDialogWidget(dialog_, GetContext(), NULL)->Show();
+ }
+
+ virtual void TearDown() OVERRIDE {
+ dialog_->TearDown();
+ ViewsTestBase::TearDown();
+ }
+
+ TestDialog* dialog() const { return dialog_; }
+
+ private:
+ TestDialog* dialog_;
+
+ DISALLOW_COPY_AND_ASSIGN(DialogTest);
+};
+
} // namespace
+TEST_F(DialogTest, DefaultButtons) {
+ DialogClientView* client_view = dialog()->GetDialogClientView();
+ LabelButton* ok_button = client_view->ok_button();
+ LabelButton* cancel_button = client_view->cancel_button();
+
+ // DialogDelegate's default button (ok) should be default (and handle enter).
+ EXPECT_EQ(ui::DIALOG_BUTTON_OK, dialog()->GetDefaultDialogButton());
+ dialog()->PressEnterAndCheckStates(ok_button);
+
+ // Focus another button in the dialog, it should become the default.
+ LabelButton* button_1 = new LabelButton(dialog(), string16());
+ client_view->AddChildView(button_1);
+ client_view->OnWillChangeFocus(ok_button, button_1);
+ EXPECT_TRUE(button_1->is_default());
+ dialog()->PressEnterAndCheckStates(button_1);
+
+ // Focus a Checkbox (not a push button), OK should become the default again.
+ Checkbox* checkbox = new Checkbox(string16());
+ client_view->AddChildView(checkbox);
+ client_view->OnWillChangeFocus(button_1, checkbox);
+ EXPECT_FALSE(button_1->is_default());
+ dialog()->PressEnterAndCheckStates(ok_button);
+
+ // Focus yet another button in the dialog, it should become the default.
+ LabelButton* button_2 = new LabelButton(dialog(), string16());
+ client_view->AddChildView(button_2);
+ client_view->OnWillChangeFocus(checkbox, button_2);
+ EXPECT_FALSE(button_1->is_default());
+ EXPECT_TRUE(button_2->is_default());
+ dialog()->PressEnterAndCheckStates(button_2);
+
+ // Focus nothing, OK should become the default again.
+ client_view->OnWillChangeFocus(button_2, NULL);
+ EXPECT_FALSE(button_1->is_default());
+ EXPECT_FALSE(button_2->is_default());
+ dialog()->PressEnterAndCheckStates(ok_button);
+
+ // A ButtonDropDown will handle events, but ok will be still be default.
+ ButtonDropDown* drop_down = new ButtonDropDown(dialog(), NULL);
+ dialog()->AddChildView(drop_down);
+ drop_down->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
+ const gfx::Point point(1, 1);
+ client_view->OnWillChangeFocus(NULL, drop_down);
+ drop_down->OnMousePressed(ui::MouseEvent(
+ ui::ET_MOUSE_PRESSED, point, point, ui::EF_LEFT_MOUSE_BUTTON));
+ drop_down->OnMouseReleased(ui::MouseEvent(
+ ui::ET_MOUSE_RELEASED, point, point, ui::EF_LEFT_MOUSE_BUTTON));
+ dialog()->CheckAndResetStates(false, false, drop_down);
+ EXPECT_FALSE(button_1->is_default());
+ EXPECT_FALSE(button_2->is_default());
+ dialog()->PressEnterAndCheckStates(ok_button);
+
+ // Focus the cancel button, it should become the default.
+ client_view->OnWillChangeFocus(drop_down, cancel_button);
+ EXPECT_FALSE(button_1->is_default());
+ EXPECT_FALSE(button_2->is_default());
+ dialog()->PressEnterAndCheckStates(cancel_button);
+}
+
+TEST_F(DialogTest, AcceptAndCancel) {
+ DialogClientView* client_view = dialog()->GetDialogClientView();
+ LabelButton* ok_button = client_view->ok_button();
+ LabelButton* cancel_button = client_view->cancel_button();
+
+ // Check that return/escape accelerators accept/cancel dialogs.
+ const ui::KeyEvent return_key(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false);
+ dialog()->GetFocusManager()->OnKeyEvent(return_key);
+ dialog()->CheckAndResetStates(false, true, NULL);
+ const ui::KeyEvent escape_key(ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, 0, false);
+ dialog()->GetFocusManager()->OnKeyEvent(escape_key);
+ dialog()->CheckAndResetStates(true, false, NULL);
+
+ // Check ok and cancel button behavior on a directed return key events.
+ ok_button->OnKeyPressed(return_key);
+ dialog()->CheckAndResetStates(false, true, NULL);
+ cancel_button->OnKeyPressed(return_key);
+ dialog()->CheckAndResetStates(true, false, NULL);
+
+ // Check that return accelerators cancel dialogs if cancel is focused.
+ cancel_button->RequestFocus();
+ dialog()->GetFocusManager()->OnKeyEvent(return_key);
+ dialog()->CheckAndResetStates(true, false, NULL);
+}
+
+TEST_F(DialogTest, RemoveDefaultButton) {
+ // Removing buttons from the dialog here should not cause a crash on close.
+ Button* ok_button = dialog()->GetDialogClientView()->ok_button();
+ dialog()->GetDialogClientView()->RemoveChildView(ok_button);
+ delete ok_button;
+ Button* cancel_button = dialog()->GetDialogClientView()->cancel_button();
+ dialog()->GetDialogClientView()->RemoveChildView(cancel_button);
+ delete cancel_button;
+}
+
TEST_F(DialogTest, HitTest) {
- TestDialog* dialog = new TestDialog();
- DialogDelegate::CreateDialogWidget(dialog, NULL, GetContext());
- const NonClientView* view = dialog->GetWidget()->non_client_view();
+ const NonClientView* view = dialog()->GetWidget()->non_client_view();
if (DialogDelegate::UseNewStyle()) {
// Ensure that the new style's BubbleFrameView hit-tests as expected.
« ui/views/window/dialog_client_view.cc ('K') | « ui/views/window/dialog_client_view_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698