summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/panels/panel_and_desktop_notification_browsertest.cc
blob: 44128bf04dcbe6f7270ece85cc1c5536646cad14 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright (c) 2012 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/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/notifications/balloon.h"
#include "chrome/browser/notifications/balloon_collection_impl.h"
#include "chrome/browser/notifications/desktop_notification_service.h"
#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/notifications/notification_ui_manager.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/panels/base_panel_browser_test.h"
#include "chrome/browser/ui/panels/panel.h"
#include "chrome/browser/ui/panels/panel_manager.h"
#include "chrome/browser/ui/panels/test_panel_mouse_watcher.h"
#include "chrome/common/pref_names.h"
#include "content/public/common/show_desktop_notification_params.h"
#include "ui/gfx/screen.h"

// Desktop notification code subscribes to various panel change notifications
// so that it knows when to adjusts balloon positions. In order to give
// desktop notification code a chance to process the change notifications,
// we call MessageLoopForUI::current()->RunAllPending() after any panel change
// has been made.
class PanelAndDesktopNotificationTest : public BasePanelBrowserTest {
 public:
  PanelAndDesktopNotificationTest() : BasePanelBrowserTest() {
  }

  virtual ~PanelAndDesktopNotificationTest() {
  }

  virtual void SetUpOnMainThread() OVERRIDE {
    // Do not use our own testing work area since desktop notification code
    // does not have the hook up for testing work area.
    disable_display_settings_mock();

    BasePanelBrowserTest::SetUpOnMainThread();

    g_browser_process->local_state()->SetInteger(
        prefs::kDesktopNotificationPosition, BalloonCollection::LOWER_RIGHT);
    balloons_ = new BalloonCollectionImpl();
    ui_manager_.reset(NotificationUIManager::Create(
        g_browser_process->local_state(), balloons_));
    service_.reset(new DesktopNotificationService(browser()->profile(),
                   ui_manager_.get()));
  }

  virtual void CleanUpOnMainThread() OVERRIDE {
    balloons_->RemoveAll();
    MessageLoopForUI::current()->RunAllPending();

    service_.reset();
    ui_manager_.reset();

    BasePanelBrowserTest::CleanUpOnMainThread();
  }

  content::ShowDesktopNotificationHostMsgParams StandardTestNotification() {
    content::ShowDesktopNotificationHostMsgParams params;
    params.notification_id = 0;
    params.origin = GURL("http://www.google.com");
    params.is_html = false;
    params.icon_url = GURL("/icon.png");
    params.title = ASCIIToUTF16("Title");
    params.body = ASCIIToUTF16("Text");
    params.direction = WebKit::WebTextDirectionDefault;
    return params;
  }

  Balloon* CreateBalloon() {
    content::ShowDesktopNotificationHostMsgParams params =
        StandardTestNotification();
    EXPECT_TRUE(service()->ShowDesktopNotification(
          params, 0, 0, DesktopNotificationService::PageNotification));
    MessageLoopForUI::current()->RunAllPending();
    return balloons().front();
  }

  static int GetBalloonBottomPosition(Balloon* balloon) {
#if defined(OS_MACOSX)
    // The position returned by the notification balloon is based on Mac's
    // vertically inverted orientation. We need to flip it so that it can
    // be compared against the position returned by the panel.
    gfx::Size screen_size = gfx::Screen::GetPrimaryMonitor().size();
    return screen_size.height() - balloon->GetPosition().y();
#else
    return balloon->GetPosition().y() + balloon->GetViewSize().height();
#endif
  }

  static void DragPanelToMouseLocation(Panel* panel,
                                       const gfx::Point& new_mouse_location) {
    PanelManager* panel_manager = PanelManager::GetInstance();
    panel_manager->StartDragging(panel, panel->GetBounds().origin());
    panel_manager->Drag(new_mouse_location);
    panel_manager->EndDragging(false);
  }

  static void ResizePanelByMouseWithDelta(Panel* panel,
                                          panel::ResizingSides side,
                                          const gfx::Point& delta) {
    PanelManager* panel_manager = PanelManager::GetInstance();
    gfx::Point mouse_location = panel->GetBounds().origin();
    panel_manager->StartResizingByMouse(panel, mouse_location, side);
    panel_manager->ResizeByMouse(mouse_location.Add(delta));
    panel_manager->EndResizingByMouse(false);
  }

  DesktopNotificationService* service() const { return service_.get(); }
  const BalloonCollection::Balloons& balloons() const {
    return balloons_->GetActiveBalloons();
  }

 private:
  BalloonCollectionImpl* balloons_;  // Owned by NotificationUIManager.
  scoped_ptr<NotificationUIManager> ui_manager_;
  scoped_ptr<DesktopNotificationService> service_;
};

IN_PROC_BROWSER_TEST_F(PanelAndDesktopNotificationTest, AddAndClosePanel) {
  Balloon* balloon = CreateBalloon();
  int original_balloon_bottom = GetBalloonBottomPosition(balloon);

  // Create a docked panel. Expect that the notification balloon moves up to be
  // above the panel.
  Panel* panel = CreateDockedPanel("1", gfx::Rect(0, 0, 200, 200));
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom = GetBalloonBottomPosition(balloon);
  EXPECT_LT(balloon_bottom, panel->GetBounds().y());
  EXPECT_LT(balloon_bottom, original_balloon_bottom);

  // Close the panel. Expect the notification balloon moves back to its original
  // position.
  panel->Close();
  MessageLoopForUI::current()->RunAllPending();
  EXPECT_EQ(original_balloon_bottom, GetBalloonBottomPosition(balloon));
}

IN_PROC_BROWSER_TEST_F(PanelAndDesktopNotificationTest,
                       ExpandAndCollapsePanel) {
  // Disable mouse watcher since we don't want mouse movements to affect panel
  // testing for title-only state.
  PanelManager* panel_manager = PanelManager::GetInstance();
  PanelMouseWatcher* mouse_watcher = new TestPanelMouseWatcher();
  panel_manager->SetMouseWatcherForTesting(mouse_watcher);

  Balloon* balloon = CreateBalloon();

  // Create a docked panel. Expect that the notification balloon moves up to be
  // above the panel.
  Panel* panel = CreateDockedPanel("1", gfx::Rect(0, 0, 200, 200));
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom_on_expanded = GetBalloonBottomPosition(balloon);
  EXPECT_LT(balloon_bottom_on_expanded, panel->GetBounds().y());

  // Minimize the panel. Expect that the notification balloon moves down, but
  // still above the minimized panel.
  panel->Minimize();
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom_on_minimized = GetBalloonBottomPosition(balloon);
  EXPECT_LT(balloon_bottom_on_minimized, panel->GetBounds().y());
  EXPECT_LT(balloon_bottom_on_expanded, balloon_bottom_on_minimized);

  // Bring up the title-bar for the panel by drawing attention. Expect that the
  // notification balloon moves up a little bit to be still above the title-only
  // panel.
  panel->FlashFrame(true);
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom_on_title_only = GetBalloonBottomPosition(balloon);
  EXPECT_LT(balloon_bottom_on_title_only, panel->GetBounds().y());
  EXPECT_LT(balloon_bottom_on_title_only, balloon_bottom_on_minimized);
  EXPECT_LT(balloon_bottom_on_expanded, balloon_bottom_on_title_only);

  // Expand the panel. Expect that the notification balloon moves up to go back
  // to the same position when the panel is expanded.
  panel->Restore();
  MessageLoopForUI::current()->RunAllPending();
  EXPECT_EQ(balloon_bottom_on_expanded, GetBalloonBottomPosition(balloon));

  PanelManager::GetInstance()->CloseAll();
}

IN_PROC_BROWSER_TEST_F(PanelAndDesktopNotificationTest, DragNarrowPanel) {
  Balloon* balloon = CreateBalloon();

  // Let the panel width be smaller than the balloon width.
  int panel_width = balloon->GetViewSize().width() - 50;

  // Create 2 docked panels. Expect that the notification balloon moves up to be
  // above the tall panel.
  Panel* tall_panel = CreateDockedPanel("1", gfx::Rect(0, 0, panel_width, 300));
  Panel* short_panel = CreateDockedPanel(
      "2", gfx::Rect(0, 0, panel_width, 200));
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom = GetBalloonBottomPosition(balloon);
  EXPECT_LT(balloon_bottom, tall_panel->GetBounds().y());

  // Swap 2 docked panels by dragging. Expect that the notificaition balloon
  // remains at the same position.
  DragPanelToMouseLocation(tall_panel, short_panel->GetBounds().origin());
  MessageLoopForUI::current()->RunAllPending();
  EXPECT_EQ(balloon_bottom, GetBalloonBottomPosition(balloon));

  PanelManager::GetInstance()->CloseAll();
}

IN_PROC_BROWSER_TEST_F(PanelAndDesktopNotificationTest, DragWidePanel) {
  Balloon* balloon = CreateBalloon();

  // Let the panel width be greater than the balloon width.
  int panel_width = balloon->GetViewSize().width() + 50;

  // Create 2 docked panels. Expect that the notification balloon moves up to be
  // above the tall panel.
  Panel* tall_panel = CreateDockedPanel("1", gfx::Rect(0, 0, panel_width, 300));
  Panel* short_panel = CreateDockedPanel(
      "2", gfx::Rect(0, 0, panel_width, 200));
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom_before_drag = GetBalloonBottomPosition(balloon);
  EXPECT_LT(balloon_bottom_before_drag, tall_panel->GetBounds().y());

  // Swap 2 docked panels by dragging. Expect that the notificaiton balloon
  // moves down to be just above the short panel.
  DragPanelToMouseLocation(tall_panel, short_panel->GetBounds().origin());
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom_after_drag = GetBalloonBottomPosition(balloon);
  EXPECT_LT(balloon_bottom_after_drag, short_panel->GetBounds().y());
  EXPECT_LT(balloon_bottom_before_drag, balloon_bottom_after_drag);

  PanelManager::GetInstance()->CloseAll();
}

IN_PROC_BROWSER_TEST_F(PanelAndDesktopNotificationTest, DetachAndAttachPanel) {
  PanelManager* panel_manager = PanelManager::GetInstance();
  Balloon* balloon = CreateBalloon();
  int original_balloon_bottom = GetBalloonBottomPosition(balloon);

  // Create a docked panel. Expect that the notification balloon moves up to be
  // above the panel.
  Panel* panel = CreateDockedPanel("1", gfx::Rect(0, 0, 200, 200));
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom_after_panel_created = GetBalloonBottomPosition(balloon);
  EXPECT_LT(balloon_bottom_after_panel_created, panel->GetBounds().y());
  EXPECT_LT(balloon_bottom_after_panel_created, original_balloon_bottom);

  // Detach the panel. Expect that the notification balloon moves down to its
  // original position.
  panel_manager->MovePanelToStrip(
      panel, PanelStrip::DETACHED, PanelStrip::DEFAULT_POSITION);
  MessageLoopForUI::current()->RunAllPending();
  EXPECT_EQ(PanelStrip::DETACHED, panel->panel_strip()->type());
  EXPECT_EQ(original_balloon_bottom, GetBalloonBottomPosition(balloon));

  // Reattach the panel. Expect that the notification balloon moves above the
  // panel.
  panel_manager->MovePanelToStrip(
      panel, PanelStrip::DOCKED, PanelStrip::DEFAULT_POSITION);
  MessageLoopForUI::current()->RunAllPending();
  EXPECT_EQ(PanelStrip::DOCKED, panel->panel_strip()->type());
  EXPECT_EQ(balloon_bottom_after_panel_created,
            GetBalloonBottomPosition(balloon));

  panel_manager->CloseAll();
}

IN_PROC_BROWSER_TEST_F(PanelAndDesktopNotificationTest, ResizePanel) {
  PanelManager* panel_manager = PanelManager::GetInstance();
  Balloon* balloon = CreateBalloon();

  // Create a docked panel. Expect that the notification balloon moves up to be
  // above the panel.
  Panel* panel = CreateDockedPanel("1", gfx::Rect(0, 0, 200, 200));
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom = GetBalloonBottomPosition(balloon);
  gfx::Rect original_bounds = panel->GetBounds();
  EXPECT_LT(balloon_bottom, original_bounds.y());

  // Resize the panel to make it taller. Expect that the notification balloon
  // moves further up by the amount of enlarge offset.
  gfx::Point resize_delta(50, 100);
  gfx::Rect new_bounds = original_bounds;
  new_bounds.set_width(new_bounds.width() + resize_delta.x());
  new_bounds.set_height(new_bounds.height() + resize_delta.y());
  panel->SetBounds(new_bounds);
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom2 = GetBalloonBottomPosition(balloon);
  EXPECT_EQ(balloon_bottom - resize_delta.y(), balloon_bottom2);

  // Resize the panel to make it shorter. Expect that the notification balloon
  // moves down by the amount of shrink offset.
  resize_delta = gfx::Point(0, -60);
  new_bounds.set_width(new_bounds.width() + resize_delta.x());
  new_bounds.set_height(new_bounds.height() + resize_delta.y());
  panel->SetBounds(new_bounds);
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom3 = GetBalloonBottomPosition(balloon);
  EXPECT_EQ(balloon_bottom2 - resize_delta.y(), balloon_bottom3);

  panel_manager->CloseAll();
}

IN_PROC_BROWSER_TEST_F(PanelAndDesktopNotificationTest, ResizePanelByMouse) {
  Balloon* balloon = CreateBalloon();

  // Create a docked panel. Expect that the notification balloon moves up to be
  // above the panel.
  Panel* panel = CreateDockedPanel("1", gfx::Rect(0, 0, 200, 200));
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom = GetBalloonBottomPosition(balloon);
  EXPECT_LT(balloon_bottom, panel->GetBounds().y());

  // Resize the panel to make it taller. Expect that the notification balloon
  // moves further up by the amount of enlarge offset.
  gfx::Point drag_delta(-50, -100);
  ResizePanelByMouseWithDelta(panel, panel::RESIZE_TOP_LEFT, drag_delta);
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom2 = GetBalloonBottomPosition(balloon);
  EXPECT_EQ(balloon_bottom + drag_delta.y(), balloon_bottom2);

  // Resize the panel to make it shorter. Expect that the notification balloon
  // moves down by the amount of shrink offset.
  drag_delta = gfx::Point(0, 60);
  ResizePanelByMouseWithDelta(panel, panel::RESIZE_TOP, drag_delta);
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom3 = GetBalloonBottomPosition(balloon);
  EXPECT_EQ(balloon_bottom2 + drag_delta.y(), balloon_bottom3);

  PanelManager::GetInstance()->CloseAll();
}

IN_PROC_BROWSER_TEST_F(PanelAndDesktopNotificationTest, InteractWithTwoPanels) {
  Balloon* balloon = CreateBalloon();
  int original_balloon_bottom = GetBalloonBottomPosition(balloon);

  // Let the panel width be smaller than the balloon width.
  int panel_width = balloon->GetViewSize().width() - 50;

  // Create a short panel. Expect that the notification balloon moves up to be
  // above the short panel.
  Panel* short_panel = CreateDockedPanel(
      "1", gfx::Rect(0, 0, panel_width, 150));
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom_after_short_panel_created =
      GetBalloonBottomPosition(balloon);
  EXPECT_LT(balloon_bottom_after_short_panel_created,
            short_panel->GetBounds().y());
  EXPECT_LT(balloon_bottom_after_short_panel_created, original_balloon_bottom);

  // Create a tall panel. Expect that the notification balloon moves further up
  // to be above the tall panel.
  Panel* tall_panel = CreateDockedPanel("2", gfx::Rect(0, 0, panel_width, 200));
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom_after_tall_panel_created =
      GetBalloonBottomPosition(balloon);
  EXPECT_LT(balloon_bottom_after_tall_panel_created,
            tall_panel->GetBounds().y());
  EXPECT_LT(balloon_bottom_after_tall_panel_created,
            balloon_bottom_after_short_panel_created);

  // Minimize tall panel. Expect that the notification balloon moves down to the
  // same position when short panel is first created.
  tall_panel->Minimize();
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom_after_tall_panel_minimized =
      GetBalloonBottomPosition(balloon);
  EXPECT_EQ(balloon_bottom_after_short_panel_created,
            balloon_bottom_after_tall_panel_minimized);

  // Minimize short panel. Expect that the notification balloon moves further
  // down.
  short_panel->Minimize();
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom_after_both_panels_minimized =
      GetBalloonBottomPosition(balloon);
  EXPECT_LT(balloon_bottom_after_both_panels_minimized,
            short_panel->GetBounds().y());
  EXPECT_LT(balloon_bottom_after_both_panels_minimized,
            tall_panel->GetBounds().y());
  EXPECT_LT(balloon_bottom_after_short_panel_created,
            balloon_bottom_after_both_panels_minimized);
  EXPECT_LT(balloon_bottom_after_both_panels_minimized,
            original_balloon_bottom);

  // Expand short panel. Expect that the notification balloon moves further up
  // to the same position when short panel is first created.
  short_panel->Restore();
  MessageLoopForUI::current()->RunAllPending();
  int balloon_bottom_after_short_panel_expanded =
      GetBalloonBottomPosition(balloon);
  EXPECT_EQ(balloon_bottom_after_short_panel_created,
            balloon_bottom_after_short_panel_expanded);

  // Close tall panel. Expect that the notification balloon moves down to the
  // same position when short panel is first created.
  tall_panel->Close();
  MessageLoopForUI::current()->RunAllPending();
  EXPECT_EQ(balloon_bottom_after_short_panel_created,
            GetBalloonBottomPosition(balloon));

  // Close short panel. Expect that the notification balloo moves back to its
  // original position.
  short_panel->Close();
  MessageLoopForUI::current()->RunAllPending();
  EXPECT_EQ(original_balloon_bottom, GetBalloonBottomPosition(balloon));
}