popup_controller_common_unittest.cc revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2014 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 "chrome/browser/ui/autofill/popup_controller_common.h"
6
7#include "base/memory/scoped_ptr.h"
8#include "chrome/browser/ui/autofill/test_popup_controller_common.h"
9#include "chrome/test/base/chrome_render_view_host_test_harness.h"
10#include "ui/gfx/display.h"
11#include "ui/gfx/rect.h"
12
13namespace autofill {
14
15class PopupControllerBaseTest : public ChromeRenderViewHostTestHarness {
16 public:
17  PopupControllerBaseTest() {}
18  virtual ~PopupControllerBaseTest() {}
19
20 private:
21  DISALLOW_COPY_AND_ASSIGN(PopupControllerBaseTest);
22};
23
24TEST_F(PopupControllerBaseTest, GetPopupBoundsTest) {
25  int desired_width = 40;
26  int desired_height = 16;
27
28  // Set up the visible screen space.
29  gfx::Display display(0,
30                       gfx::Rect(0, 0, 2 * desired_width, 2 * desired_height));
31
32  // Store the possible element bounds and the popup bounds they should result
33  // in.
34  std::vector<gfx::RectF> element_bounds;
35  std::vector<gfx::Rect> expected_popup_bounds;
36
37  // The popup grows down and to the right.
38  element_bounds.push_back(gfx::RectF(0, 0, 0, 0));
39  expected_popup_bounds.push_back(
40      gfx::Rect(0, 0, desired_width, desired_height));
41
42  // The popup grows down and to the left.
43  element_bounds.push_back(gfx::RectF(2 * desired_width, 0, 0, 0));
44  expected_popup_bounds.push_back(
45      gfx::Rect(desired_width, 0, desired_width, desired_height));
46
47  // The popup grows up and to the right.
48  element_bounds.push_back(gfx::RectF(0, 2 * desired_height, 0, 0));
49  expected_popup_bounds.push_back(
50      gfx::Rect(0, desired_height, desired_width, desired_height));
51
52  // The popup grows up and to the left.
53  element_bounds.push_back(
54      gfx::RectF(2 * desired_width, 2 * desired_height, 0, 0));
55  expected_popup_bounds.push_back(
56      gfx::Rect(desired_width, desired_height, desired_width, desired_height));
57
58  // The popup would be partial off the top and left side of the screen.
59  element_bounds.push_back(
60      gfx::RectF(-desired_width / 2, -desired_height / 2, 0, 0));
61  expected_popup_bounds.push_back(
62      gfx::Rect(0, 0, desired_width, desired_height));
63
64  // The popup would be partially off the bottom and the right side of
65  // the screen.
66  element_bounds.push_back(
67      gfx::RectF(desired_width * 1.5, desired_height * 1.5, 0, 0));
68  expected_popup_bounds.push_back(
69      gfx::Rect((desired_width + 1) / 2, (desired_height + 1) / 2,
70                desired_width, desired_height));
71
72  for (size_t i = 0; i < element_bounds.size(); ++i) {
73    scoped_ptr<TestPopupControllerCommon> popup_controller(
74        new TestPopupControllerCommon(element_bounds[i]));
75    popup_controller->set_display(display);
76    gfx::Rect actual_popup_bounds =
77        popup_controller->GetPopupBounds(desired_width, desired_height);
78
79    EXPECT_EQ(expected_popup_bounds[i].ToString(),
80              actual_popup_bounds.ToString()) <<
81        "Popup bounds failed to match for test " << i;
82  }
83}
84
85}  // namespace autofill
86