summaryrefslogtreecommitdiffstats
path: root/ui/views/window/dialog_delegate_unittest.cc
blob: fee160e0a9ec32308ee438a86f8251776220a472 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2013 The Chromium Authors. All rights reserved.
// 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/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 {

namespace {

class TestDialog : public DialogDelegateView, public ButtonListener {
 public:
  TestDialog()
      : canceled_(false),
        accepted_(false),
        closeable_(false),
        last_pressed_button_(NULL) {}
  virtual ~TestDialog() {}

  // DialogDelegateView overrides:
  virtual bool Cancel() OVERRIDE {
    canceled_ = true;
    return closeable_;
  }
  virtual bool Accept() OVERRIDE {
    accepted_ = true;
    return closeable_;
  }

  // DialogDelegateView overrides:
  virtual gfx::Size GetPreferredSize() OVERRIDE { return gfx::Size(200, 200); }
  virtual base::string16 GetWindowTitle() const OVERRIDE { return title_; }

  // 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();
  }

  void set_title(const base::string16& title) { title_ = title; }

 private:
  bool canceled_;
  bool accepted_;
  // Prevent the dialog from closing, for repeated ok and cancel button clicks.
  bool closeable_;
  Button* last_pressed_button_;
  base::string16 title_;

  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();

  // 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(), base::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(base::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(), base::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);
}

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.
  delete dialog()->GetDialogClientView()->ok_button();
  delete dialog()->GetDialogClientView()->cancel_button();
}

TEST_F(DialogTest, HitTest) {
  // Ensure that the new style's BubbleFrameView hit-tests as expected.
  const NonClientView* view = dialog()->GetWidget()->non_client_view();
  BubbleFrameView* frame = static_cast<BubbleFrameView*>(view->frame_view());
  const int border = frame->bubble_border()->GetBorderThickness();

  struct {
    const int point;
    const int hit;
  } cases[] = {
    { border,      HTSYSMENU },
    { border + 10, HTSYSMENU },
    { border + 20, HTCAPTION },
    { border + 40, HTCLIENT  },
    { border + 50, HTCLIENT  },
    { 1000,        HTNOWHERE },
  };

  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
    gfx::Point point(cases[i].point, cases[i].point);
    EXPECT_EQ(cases[i].hit, frame->NonClientHitTest(point))
        << " with border: " << border << ", at point " << cases[i].point;
  }
}

TEST_F(DialogTest, InitialBoundsAccommodateTitle) {
  TestDialog* titled_dialog(new TestDialog());
  titled_dialog->set_title(base::ASCIIToUTF16("Title"));
  DialogDelegate::CreateDialogWidget(titled_dialog, GetContext(), NULL);

  // Titled dialogs have taller initial frame bounds than untitled dialogs.
  EXPECT_GT(titled_dialog->GetWidget()->GetWindowBoundsInScreen().height(),
            dialog()->GetWidget()->GetWindowBoundsInScreen().height());

  // Giving the default test dialog a title will make the bounds the same.
  dialog()->set_title(base::ASCIIToUTF16("Title"));
  dialog()->GetWidget()->UpdateWindowTitle();
  View* frame = dialog()->GetWidget()->non_client_view()->frame_view();
  EXPECT_EQ(titled_dialog->GetWidget()->GetWindowBoundsInScreen().height(),
            frame->GetPreferredSize().height());
}

}  // namespace views