dialog_client_view_unittest.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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 "base/basictypes.h"
6#include "base/strings/utf_string_conversions.h"
7#include "testing/gtest/include/gtest/gtest.h"
8#include "ui/base/ui_base_types.h"
9#include "ui/views/test/test_views.h"
10#include "ui/views/window/dialog_client_view.h"
11#include "ui/views/window/dialog_delegate.h"
12
13namespace views {
14
15class TestDialogClientView : public DialogClientView {
16 public:
17  TestDialogClientView(View* contents_view,
18                       DialogDelegate* delegate)
19      : DialogClientView(contents_view),
20        delegate_(delegate) {}
21  virtual ~TestDialogClientView() {}
22
23  // DialogClientView implementation.
24  virtual DialogDelegate* GetDialogDelegate() const OVERRIDE {
25    return delegate_;
26  }
27
28  View* GetContentsView() {
29    return contents_view();
30  }
31
32  void CreateExtraViews() {
33    CreateExtraView();
34    CreateFootnoteView();
35  }
36
37 private:
38  DialogDelegate* delegate_;
39
40  DISALLOW_COPY_AND_ASSIGN(TestDialogClientView);
41};
42
43class DialogClientViewTest : public testing::Test,
44                             public DialogDelegateView {
45 public:
46  DialogClientViewTest()
47      : dialog_buttons_(ui::DIALOG_BUTTON_NONE),
48        extra_view_(NULL),
49        footnote_view_(NULL) {}
50  virtual ~DialogClientViewTest() {}
51
52  // testing::Test implementation.
53  virtual void SetUp() OVERRIDE {
54    dialog_buttons_ = ui::DIALOG_BUTTON_NONE;
55    contents_.reset(new StaticSizedView(gfx::Size(100, 200)));
56    client_view_.reset(new TestDialogClientView(contents_.get(), this));
57  }
58
59  // DialogDelegateView implementation.
60  virtual View* GetContentsView() OVERRIDE {
61    return contents_.get();
62  }
63  virtual View* CreateExtraView() OVERRIDE {
64    return extra_view_;
65  }
66  virtual View* CreateFootnoteView() OVERRIDE {
67    return footnote_view_;
68  }
69  virtual int GetDialogButtons() const OVERRIDE {
70    return dialog_buttons_;
71  }
72
73 protected:
74  void ResizeAndLayoutClientView() {
75    client_view_->SizeToPreferredSize();
76    client_view_->Layout();
77  }
78
79  // Maes sure that the content view is sized correctly. Width must be at least
80  // the requested amount, but height should always match exactly.
81  void CheckContentsIsSetToPreferredSize() {
82    ResizeAndLayoutClientView();
83    gfx::Size preferred_size = contents_->GetPreferredSize();
84    EXPECT_EQ(preferred_size.height(), contents_->bounds().height());
85    EXPECT_LE(preferred_size.width(), contents_->bounds().width());
86    EXPECT_EQ(contents_->bounds().x(), client_view()->bounds().x());
87    EXPECT_EQ(contents_->bounds().y(), client_view()->bounds().y());
88    EXPECT_EQ(contents_->bounds().right(), client_view()->bounds().right());
89  }
90
91  // Sets the buttons to show in the dialog and refreshes the dialog.
92  void SetDialogButtons(int dialog_buttons) {
93    dialog_buttons_ = dialog_buttons;
94    client_view_->UpdateDialogButtons();
95  }
96
97  // Sets the extra view.
98  void SetExtraView(View* view) {
99    DCHECK(!extra_view_);
100    extra_view_ = view;
101    client_view_->CreateExtraViews();
102  }
103
104  // Sets the footnote view.
105  void SetFootnoteView(View* view) {
106    DCHECK(!footnote_view_);
107    footnote_view_ = view;
108    client_view_->CreateExtraViews();
109  }
110
111  TestDialogClientView* client_view() { return client_view_.get(); }
112
113 private:
114  // The contents of the dialog.
115  scoped_ptr<View> contents_;
116  // The DialogClientView that's being tested.
117  scoped_ptr<TestDialogClientView> client_view_;
118  // The bitmask of buttons to show in the dialog.
119  int dialog_buttons_;
120  View* extra_view_;  // weak
121  View* footnote_view_;  // weak
122
123  DISALLOW_COPY_AND_ASSIGN(DialogClientViewTest);
124};
125
126TEST_F(DialogClientViewTest, ButtonStateCanChange) {
127  ResizeAndLayoutClientView();
128  int no_button_height = client_view()->bounds().height();
129
130  SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL);
131  ResizeAndLayoutClientView();
132  EXPECT_GT(client_view()->bounds().height(), no_button_height);
133}
134
135// Test that the contents view gets its preferred size in the basic dialog
136// configuration.
137TEST_F(DialogClientViewTest, ContentsSize) {
138  CheckContentsIsSetToPreferredSize();
139  EXPECT_EQ(GetContentsView()->bounds().bottom(),
140            client_view()->bounds().bottom());
141}
142
143// Test the effect of the button strip on layout.
144TEST_F(DialogClientViewTest, LayoutWithButtons) {
145  SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL);
146  CheckContentsIsSetToPreferredSize();
147  EXPECT_LT(GetContentsView()->bounds().bottom(),
148            client_view()->bounds().bottom());
149  gfx::Size no_extra_view_size = client_view()->bounds().size();
150
151  View* extra_view = new StaticSizedView(gfx::Size(200, 200));
152  SetExtraView(extra_view);
153  CheckContentsIsSetToPreferredSize();
154  EXPECT_GT(client_view()->bounds().height(), no_extra_view_size.height());
155  int width_of_extra_view = extra_view->bounds().width();
156
157  // Visibility of extra view is respected.
158  extra_view->SetVisible(false);
159  CheckContentsIsSetToPreferredSize();
160  EXPECT_EQ(no_extra_view_size.height(), client_view()->bounds().height());
161  EXPECT_EQ(no_extra_view_size.width(), client_view()->bounds().width());
162
163  // Try with a reduced-size dialog.
164  extra_view->SetVisible(true);
165  client_view()->SetBoundsRect(gfx::Rect(gfx::Point(0, 0), no_extra_view_size));
166  client_view()->Layout();
167  DCHECK_GT(width_of_extra_view, extra_view->bounds().width());
168}
169
170// Test the effect of the footnote view on layout.
171TEST_F(DialogClientViewTest, LayoutWithFootnote) {
172  CheckContentsIsSetToPreferredSize();
173  gfx::Size no_footnote_size = client_view()->bounds().size();
174
175  View* footnote_view = new StaticSizedView(gfx::Size(200, 200));
176  SetFootnoteView(footnote_view);
177  CheckContentsIsSetToPreferredSize();
178  EXPECT_GT(client_view()->bounds().height(), no_footnote_size.height());
179  EXPECT_EQ(200, footnote_view->bounds().height());
180  gfx::Size with_footnote_size = client_view()->bounds().size();
181  EXPECT_EQ(with_footnote_size.width(), footnote_view->bounds().width());
182
183  SetDialogButtons(ui::DIALOG_BUTTON_CANCEL);
184  CheckContentsIsSetToPreferredSize();
185  EXPECT_LE(with_footnote_size.height(), client_view()->bounds().height());
186  EXPECT_LE(with_footnote_size.width(), client_view()->bounds().width());
187  gfx::Size with_footnote_and_button_size = client_view()->bounds().size();
188
189  SetDialogButtons(ui::DIALOG_BUTTON_NONE);
190  footnote_view->SetVisible(false);
191  CheckContentsIsSetToPreferredSize();
192  EXPECT_EQ(no_footnote_size.height(), client_view()->bounds().height());
193  EXPECT_EQ(no_footnote_size.width(), client_view()->bounds().width());
194}
195
196// Test that GetHeightForWidth is respected for the footnote view.
197TEST_F(DialogClientViewTest, LayoutWithFootnoteHeightForWidth) {
198  CheckContentsIsSetToPreferredSize();
199  gfx::Size no_footnote_size = client_view()->bounds().size();
200
201  View* footnote_view = new ProportionallySizedView(3);
202  SetFootnoteView(footnote_view);
203  CheckContentsIsSetToPreferredSize();
204  EXPECT_GT(client_view()->bounds().height(), no_footnote_size.height());
205  EXPECT_EQ(footnote_view->bounds().width() * 3,
206            footnote_view->bounds().height());
207}
208
209}  // namespace views
210