summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/html_dialog_view_browsertest.cc
blob: bbcc6a0c44f5b4c3bb0394405ec336cc7c9aa590 (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
// Copyright (c) 2010 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 "chrome/test/ui/ui_test.h"

#include "base/file_path.h"
#include "base/message_loop.h"
#include "chrome/browser/browser_thread.h"
#include "chrome/browser/dom_ui/html_dialog_ui.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/renderer_host/render_widget_host_view.h"
#include "chrome/browser/views/html_dialog_view.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/in_process_browser_test.h"
#include "chrome/test/ui_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "views/widget/widget.h"
#include "views/window/window.h"

using testing::Eq;

namespace {

// Window non-client-area means that the minimum size for the window
// won't be the actual minimum size - our layout and resizing code
// makes sure the chrome is always visible.
const int kMinimumWidthToTestFor = 20;
const int kMinimumHeightToTestFor = 30;

class TestHtmlDialogUIDelegate : public HtmlDialogUIDelegate {
 public:
  TestHtmlDialogUIDelegate() {}
  virtual ~TestHtmlDialogUIDelegate() {}

  // HTMLDialogUIDelegate implementation:
  virtual bool IsDialogModal() const {
    return true;
  }
  virtual std::wstring GetDialogTitle() const {
    return std::wstring(L"Test");
  }
  virtual GURL GetDialogContentURL() const {
    return GURL(chrome::kAboutBlankURL);
  }
  virtual void GetDOMMessageHandlers(
      std::vector<DOMMessageHandler*>* handlers) const { }
  virtual void GetDialogSize(gfx::Size* size) const {
    size->set_width(40);
    size->set_height(40);
  }
  virtual std::string GetDialogArgs() const {
    return std::string();
  }
  virtual void OnDialogClosed(const std::string& json_retval) { }
  virtual void OnCloseContents(TabContents* source, bool* out_close_dialog) {
    if (out_close_dialog)
      *out_close_dialog = true;
  }
};

}  // namespace

class HtmlDialogBrowserTest : public InProcessBrowserTest {
 public:
  HtmlDialogBrowserTest() {}

#if defined(OS_WIN)
  class WindowChangedObserver : public base::MessagePumpForUI::Observer {
   public:
    WindowChangedObserver() {}

    static WindowChangedObserver* Get() {
      return Singleton<WindowChangedObserver>::get();
    }

    // This method is called before processing a message.
    virtual void WillProcessMessage(const MSG& msg) {}

    // This method is called after processing a message.
    virtual void DidProcessMessage(const MSG& msg) {
      // Either WM_PAINT or WM_TIMER indicates the actual work of
      // pushing through the window resizing messages is done since
      // they are lower priority (we don't get to see the
      // WM_WINDOWPOSCHANGED message here).
      if (msg.message == WM_PAINT || msg.message == WM_TIMER)
        MessageLoop::current()->Quit();
    }
  };
#elif !defined(OS_MACOSX)
  class WindowChangedObserver : public base::MessagePumpForUI::Observer {
   public:
    WindowChangedObserver() {}

    static WindowChangedObserver* Get() {
      return Singleton<WindowChangedObserver>::get();
    }

    // This method is called before processing a message.
    virtual void WillProcessEvent(GdkEvent* event) {}

    // This method is called after processing a message.
    virtual void DidProcessEvent(GdkEvent* event) {
      // Quit once the GDK_CONFIGURE event has been processed - seeing
      // this means the window sizing request that was made actually
      // happened.
      if (event->type == GDK_CONFIGURE)
        MessageLoop::current()->Quit();
    }
  };
#endif
};

#if defined(OS_LINUX)
#define MAYBE_SizeWindow SizeWindow
#else
// http://code.google.com/p/chromium/issues/detail?id=52602
// Windows has some issues resizing windows- an off by one problem,
// and a minimum size that seems too big.  This file isn't included in
// Mac builds yet.
#define MAYBE_SizeWindow DISABLED_SizeWindow
#endif

IN_PROC_BROWSER_TEST_F(HtmlDialogBrowserTest, MAYBE_SizeWindow) {
  HtmlDialogUIDelegate* delegate = new TestHtmlDialogUIDelegate();

  HtmlDialogView* html_view =
      new HtmlDialogView(browser()->profile(), delegate);
  TabContents* tab_contents = browser()->GetSelectedTabContents();
  ASSERT_TRUE(tab_contents != NULL);
  views::Window::CreateChromeWindow(tab_contents->GetMessageBoxRootWindow(),
                                    gfx::Rect(), html_view);
  html_view->InitDialog();
  html_view->window()->Show();

  MessageLoopForUI::current()->AddObserver(WindowChangedObserver::Get());

  gfx::Rect bounds;
  html_view->GetWidget()->GetBounds(&bounds, false);

  gfx::Rect set_bounds = bounds;
  gfx::Rect actual_bounds, rwhv_bounds;

  // Bigger than the default in both dimensions.
  set_bounds.set_width(400);
  set_bounds.set_height(300);

  html_view->MoveContents(tab_contents, set_bounds);
  ui_test_utils::RunMessageLoop();
  html_view->GetWidget()->GetBounds(&actual_bounds, false);
  EXPECT_EQ(set_bounds, actual_bounds);

  rwhv_bounds =
      html_view->tab_contents()->GetRenderWidgetHostView()->GetViewBounds();
  EXPECT_LT(0, rwhv_bounds.width());
  EXPECT_LT(0, rwhv_bounds.height());
  EXPECT_GE(set_bounds.width(), rwhv_bounds.width());
  EXPECT_GE(set_bounds.height(), rwhv_bounds.height());

  // Larger in one dimension and smaller in the other.
  set_bounds.set_width(550);
  set_bounds.set_height(250);

  html_view->MoveContents(tab_contents, set_bounds);
  ui_test_utils::RunMessageLoop();
  html_view->GetWidget()->GetBounds(&actual_bounds, false);
  EXPECT_EQ(set_bounds, actual_bounds);

  rwhv_bounds =
      html_view->tab_contents()->GetRenderWidgetHostView()->GetViewBounds();
  EXPECT_LT(0, rwhv_bounds.width());
  EXPECT_LT(0, rwhv_bounds.height());
  EXPECT_GE(set_bounds.width(), rwhv_bounds.width());
  EXPECT_GE(set_bounds.height(), rwhv_bounds.height());

  // Get very small.
  set_bounds.set_width(kMinimumWidthToTestFor);
  set_bounds.set_height(kMinimumHeightToTestFor);

  html_view->MoveContents(tab_contents, set_bounds);
  ui_test_utils::RunMessageLoop();
  html_view->GetWidget()->GetBounds(&actual_bounds, false);
  EXPECT_EQ(set_bounds, actual_bounds);

  rwhv_bounds =
      html_view->tab_contents()->GetRenderWidgetHostView()->GetViewBounds();
  EXPECT_LT(0, rwhv_bounds.width());
  EXPECT_LT(0, rwhv_bounds.height());
  EXPECT_GE(set_bounds.width(), rwhv_bounds.width());
  EXPECT_GE(set_bounds.height(), rwhv_bounds.height());

  // Check to make sure we can't get to 0x0
  set_bounds.set_width(0);
  set_bounds.set_height(0);

  html_view->MoveContents(tab_contents, set_bounds);
  ui_test_utils::RunMessageLoop();
  html_view->GetWidget()->GetBounds(&actual_bounds, false);
  EXPECT_LT(0, actual_bounds.width());
  EXPECT_LT(0, actual_bounds.height());

  MessageLoopForUI::current()->RemoveObserver(WindowChangedObserver::Get());
}