summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/info_bubble_unittest.cc
blob: 8d7f8ece886081cf07c8cb2b053f709be859a70a (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
226
227
228
229
230
231
232
233
234
235
236
237
// 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/browser/views/info_bubble.h"
#include "testing/gtest/include/gtest/gtest.h"

typedef testing::Test InfoBubbleTest;

class TestBorderContents : public BorderContents {
 public:
  TestBorderContents() {}

  void set_monitor_bounds(const gfx::Rect& bounds) {
    monitor_bounds_ = bounds;
  }

  BubbleBorder* bubble_border() const { return bubble_border_; }

 protected:
  virtual gfx::Rect GetMonitorBounds(const gfx::Rect& rect) {
    return monitor_bounds_;
  }

 private:
  gfx::Rect monitor_bounds_;

  DISALLOW_COPY_AND_ASSIGN(TestBorderContents);
};

// Tests that the arrow is moved appropriately when the info-bubble does not fit
// the screen.
TEST_F(InfoBubbleTest, BorderContentsSizeAndGetBounds) {
  TestBorderContents border_contents;
  border_contents.Init();

  // Test that the info bubble displays normally when it fits.
  gfx::Rect contents_bounds;
  gfx::Rect window_bounds;
  border_contents.set_monitor_bounds(gfx::Rect(0, 0, 1000, 1000));
  border_contents.SizeAndGetBounds(
      gfx::Rect(100, 100, 50, 50),  // |position_relative_to|
      BubbleBorder::TOP_LEFT,
      false,                        // |allow_bubble_offscreen|
      gfx::Size(500, 500),          // |contents_size|
      &contents_bounds, &window_bounds);
  // The arrow shouldn't have changed from TOP_LEFT.
  BubbleBorder::ArrowLocation arrow_location =
      border_contents.bubble_border()->arrow_location();
  EXPECT_TRUE(BubbleBorder::has_arrow(arrow_location));
  EXPECT_TRUE(BubbleBorder::is_arrow_on_top(arrow_location));
  EXPECT_TRUE(BubbleBorder::is_arrow_on_left(arrow_location));
  EXPECT_GT(window_bounds.x(), 100);
  EXPECT_GT(window_bounds.y(), 100 + 50 - 10);  // -10 to roughly compensate for
                                                // arrow overlap.

  // Test bubble not fitting on left.
  border_contents.SizeAndGetBounds(
      gfx::Rect(100, 100, 50, 50),  // |position_relative_to|
      BubbleBorder::TOP_RIGHT,
      false,                        // |allow_bubble_offscreen|
      gfx::Size(500, 500),          // |contents_size|
      &contents_bounds, &window_bounds);
  arrow_location = border_contents.bubble_border()->arrow_location();
  // The arrow should have changed to TOP_LEFT.
  EXPECT_TRUE(BubbleBorder::has_arrow(arrow_location));
  EXPECT_TRUE(BubbleBorder::is_arrow_on_top(arrow_location));
  EXPECT_TRUE(BubbleBorder::is_arrow_on_left(arrow_location));
  EXPECT_GT(window_bounds.x(), 100);
  EXPECT_GT(window_bounds.y(), 100 + 50 - 10);  // -10 to roughly compensate for
                                                // arrow overlap.

  // Test bubble not fitting on left or top.
  border_contents.SizeAndGetBounds(
      gfx::Rect(100, 100, 50, 50),  // |position_relative_to|
      BubbleBorder::BOTTOM_RIGHT,
      false,                        // |allow_bubble_offscreen|
      gfx::Size(500, 500),          // |contents_size|
      &contents_bounds, &window_bounds);
  arrow_location = border_contents.bubble_border()->arrow_location();
  // The arrow should have changed to TOP_LEFT.
  EXPECT_TRUE(BubbleBorder::has_arrow(arrow_location));
  EXPECT_TRUE(BubbleBorder::is_arrow_on_top(arrow_location));
  EXPECT_TRUE(BubbleBorder::is_arrow_on_left(arrow_location));
  EXPECT_GT(window_bounds.x(), 100);
  EXPECT_GT(window_bounds.y(), 100 + 50 - 10);  // -10 to roughly compensate for
                                                // arrow overlap.

  // Test bubble not fitting on top.
  border_contents.SizeAndGetBounds(
      gfx::Rect(100, 100, 50, 50),  // |position_relative_to|
      BubbleBorder::BOTTOM_LEFT,
      false,                        // |allow_bubble_offscreen|
      gfx::Size(500, 500),          // |contents_size|
      &contents_bounds, &window_bounds);
  arrow_location = border_contents.bubble_border()->arrow_location();
  // The arrow should have changed to TOP_LEFT.
  EXPECT_TRUE(BubbleBorder::has_arrow(arrow_location));
  EXPECT_TRUE(BubbleBorder::is_arrow_on_top(arrow_location));
  EXPECT_TRUE(BubbleBorder::is_arrow_on_left(arrow_location));
  EXPECT_GT(window_bounds.x(), 100);
  EXPECT_GT(window_bounds.y(), 100 + 50 - 10);  // -10 to roughly compensate for
                                                // arrow overlap.

  // Test bubble not fitting on top and right.
  border_contents.SizeAndGetBounds(
      gfx::Rect(900, 100, 50, 50),  // |position_relative_to|
      BubbleBorder::BOTTOM_LEFT,
      false,                        // |allow_bubble_offscreen|
      gfx::Size(500, 500),          // |contents_size|
      &contents_bounds, &window_bounds);
  arrow_location = border_contents.bubble_border()->arrow_location();
  // The arrow should have changed to TOP_RIGHT.
  EXPECT_TRUE(BubbleBorder::has_arrow(arrow_location));
  EXPECT_TRUE(BubbleBorder::is_arrow_on_top(arrow_location));
  EXPECT_FALSE(BubbleBorder::is_arrow_on_left(arrow_location));
  EXPECT_LT(window_bounds.x(), 900 + 50 - 500);
  EXPECT_GT(window_bounds.y(), 100 + 50 - 10);  // -10 to roughly compensate for
                                                // arrow overlap.

 // Test bubble not fitting on right.
  border_contents.SizeAndGetBounds(
      gfx::Rect(900, 100, 50, 50),  // |position_relative_to|
      BubbleBorder::TOP_LEFT,
      false,                        // |allow_bubble_offscreen|
      gfx::Size(500, 500),          // |contents_size|
      &contents_bounds, &window_bounds);
  arrow_location = border_contents.bubble_border()->arrow_location();
  // The arrow should have changed to TOP_RIGHT.
  EXPECT_TRUE(BubbleBorder::has_arrow(arrow_location));
  EXPECT_TRUE(BubbleBorder::is_arrow_on_top(arrow_location));
  EXPECT_FALSE(BubbleBorder::is_arrow_on_left(arrow_location));
  EXPECT_LT(window_bounds.x(), 900 + 50 - 500);
  EXPECT_GT(window_bounds.y(), 100 + 50 - 10);  // -10 to roughly compensate for
                                                // arrow overlap.

  // Test bubble not fitting on bottom and right.
  border_contents.SizeAndGetBounds(
      gfx::Rect(900, 900, 50, 50),  // |position_relative_to|
      BubbleBorder::TOP_LEFT,
      false,                        // |allow_bubble_offscreen|
      gfx::Size(500, 500),          // |contents_size|
      &contents_bounds, &window_bounds);
  arrow_location = border_contents.bubble_border()->arrow_location();
  // The arrow should have changed to BOTTOM_RIGHT.
  EXPECT_TRUE(BubbleBorder::has_arrow(arrow_location));
  EXPECT_FALSE(BubbleBorder::is_arrow_on_top(arrow_location));
  EXPECT_FALSE(BubbleBorder::is_arrow_on_left(arrow_location));
  EXPECT_LT(window_bounds.x(), 900 + 50 - 500);
  EXPECT_LT(window_bounds.y(), 900 - 500 - 15);  // -15 to roughly compensate
                                                 // for arrow height.

  // Test bubble not fitting at the bottom.
  border_contents.SizeAndGetBounds(
      gfx::Rect(100, 900, 50, 50),  // |position_relative_to|
      BubbleBorder::TOP_LEFT,
      false,                        // |allow_bubble_offscreen|
      gfx::Size(500, 500),          // |contents_size|
      &contents_bounds, &window_bounds);
  arrow_location = border_contents.bubble_border()->arrow_location();
  // The arrow should have changed to BOTTOM_LEFT.
  EXPECT_TRUE(BubbleBorder::has_arrow(arrow_location));
  EXPECT_FALSE(BubbleBorder::is_arrow_on_top(arrow_location));
  EXPECT_TRUE(BubbleBorder::is_arrow_on_left(arrow_location));
  // The window should be right aligned with the position_relative_to.
  EXPECT_LT(window_bounds.x(), 900 + 50 - 500);
  EXPECT_LT(window_bounds.y(), 900 - 500 - 15);  // -15 to roughly compensate
                                                 // for arrow height.

  // Test bubble not fitting at the bottom and left.
  border_contents.SizeAndGetBounds(
      gfx::Rect(100, 900, 50, 50),  // |position_relative_to|
      BubbleBorder::TOP_RIGHT,
      false,                        // |allow_bubble_offscreen|
      gfx::Size(500, 500),          // |contents_size|
      &contents_bounds, &window_bounds);
  arrow_location = border_contents.bubble_border()->arrow_location();
  // The arrow should have changed to BOTTOM_LEFT.
  EXPECT_TRUE(BubbleBorder::has_arrow(arrow_location));
  EXPECT_FALSE(BubbleBorder::is_arrow_on_top(arrow_location));
  EXPECT_TRUE(BubbleBorder::is_arrow_on_left(arrow_location));
  // The window should be right aligned with the position_relative_to.
  EXPECT_LT(window_bounds.x(), 900 + 50 - 500);
  EXPECT_LT(window_bounds.y(), 900 - 500 - 15);  // -15 to roughly compensate
                                                 // for arrow height.
}

// Tests that the arrow is not moved when the info-bubble does not fit the
// screen but moving it would make matter worse.
TEST_F(InfoBubbleTest, BorderContentsSizeAndGetBoundsDontMoveArrow) {
  TestBorderContents border_contents;
  border_contents.Init();
  gfx::Rect contents_bounds;
  gfx::Rect window_bounds;
  border_contents.set_monitor_bounds(gfx::Rect(0, 0, 1000, 1000));
  border_contents.SizeAndGetBounds(
      gfx::Rect(400, 100, 50, 50),  // |position_relative_to|
      BubbleBorder::TOP_LEFT,
      false,                        // |allow_bubble_offscreen|
      gfx::Size(500, 700),          // |contents_size|
      &contents_bounds, &window_bounds);

  // The arrow should not have changed, as it would make it the bubble even more
  // offscreen.
  BubbleBorder::ArrowLocation arrow_location =
      border_contents.bubble_border()->arrow_location();
  EXPECT_TRUE(BubbleBorder::has_arrow(arrow_location));
  EXPECT_TRUE(BubbleBorder::is_arrow_on_top(arrow_location));
  EXPECT_TRUE(BubbleBorder::is_arrow_on_left(arrow_location));
}

// Test that the 'allow offscreen' prevents the bubble from moving.
TEST_F(InfoBubbleTest, BorderContentsSizeAndGetBoundsAllowOffscreen) {
  TestBorderContents border_contents;
  border_contents.Init();
  gfx::Rect contents_bounds;
  gfx::Rect window_bounds;
  border_contents.set_monitor_bounds(gfx::Rect(0, 0, 1000, 1000));
  border_contents.SizeAndGetBounds(
      gfx::Rect(100, 900, 50, 50),  // |position_relative_to|
      BubbleBorder::TOP_RIGHT,
      true,                         // |allow_bubble_offscreen|
      gfx::Size(500, 500),          // |contents_size|
      &contents_bounds, &window_bounds);

  // The arrow should not have changed (eventhough the bubble does not fit).
  BubbleBorder::ArrowLocation arrow_location =
      border_contents.bubble_border()->arrow_location();
  EXPECT_TRUE(BubbleBorder::has_arrow(arrow_location));
  EXPECT_TRUE(BubbleBorder::is_arrow_on_top(arrow_location));
  EXPECT_FALSE(BubbleBorder::is_arrow_on_left(arrow_location));
  // The coordinates should be pointing to 'positive relative to' from
  // TOP_RIGHT.
  EXPECT_LT(window_bounds.x(), 100 + 50 - 500);
  EXPECT_GT(window_bounds.y(), 900 + 50 - 10);  // -10 to roughly compensate for
                                                // arrow overlap.
}