summaryrefslogtreecommitdiff
path: root/chromium/ash/wm/dock/docked_window_resizer_unittest.cc
blob: 49aeca25318991c5c3d91a05028aab19931fa142 (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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
// Copyright (c) 2013 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 "ash/wm/dock/docked_window_resizer.h"

#include "ash/ash_switches.h"
#include "ash/launcher/launcher.h"
#include "ash/launcher/launcher_model.h"
#include "ash/root_window_controller.h"
#include "ash/screen_ash.h"
#include "ash/shelf/shelf_layout_manager.h"
#include "ash/shelf/shelf_types.h"
#include "ash/shelf/shelf_widget.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/cursor_manager_test_api.h"
#include "ash/test/shell_test_api.h"
#include "ash/test/test_launcher_delegate.h"
#include "ash/wm/dock/docked_window_layout_manager.h"
#include "ash/wm/drag_window_resizer.h"
#include "ash/wm/panels/panel_layout_manager.h"
#include "ash/wm/window_properties.h"
#include "ash/wm/window_util.h"
#include "base/command_line.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/root_window.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/base/hit_test.h"
#include "ui/base/ui_base_types.h"
#include "ui/views/widget/widget.h"

namespace ash {
namespace internal {

class DockedWindowResizerTest
    : public test::AshTestBase,
      public testing::WithParamInterface<aura::client::WindowType> {
 public:
  DockedWindowResizerTest() : model_(NULL), window_type_(GetParam()) {}
  virtual ~DockedWindowResizerTest() {}

  virtual void SetUp() OVERRIDE {
    CommandLine::ForCurrentProcess()->AppendSwitch(
        ash::switches::kAshEnableStickyEdges);
    CommandLine::ForCurrentProcess()->AppendSwitch(
        ash::switches::kAshEnableDockedWindows);
    AshTestBase::SetUp();
    UpdateDisplay("600x400");
    test::ShellTestApi test_api(Shell::GetInstance());
    model_ = test_api.launcher_model();
  }

  virtual void TearDown() OVERRIDE {
    AshTestBase::TearDown();
  }

 protected:
  enum DockedEdge {
    DOCKED_EDGE_NONE,
    DOCKED_EDGE_LEFT,
    DOCKED_EDGE_RIGHT,
  };

  aura::Window* CreateTestWindow(const gfx::Rect& bounds) {
    aura::Window* window = CreateTestWindowInShellWithDelegateAndType(
        &delegate_,
        window_type_,
        0,
        bounds);
    if (window_type_ == aura::client::WINDOW_TYPE_PANEL) {
      test::TestLauncherDelegate* launcher_delegate =
          test::TestLauncherDelegate::instance();
      launcher_delegate->AddLauncherItem(window);
      PanelLayoutManager* manager =
          static_cast<PanelLayoutManager*>(
              Shell::GetContainer(window->GetRootWindow(),
                                  internal::kShellWindowId_PanelContainer)->
                  layout_manager());
      manager->Relayout();
    }
    return window;
  }

  static WindowResizer* CreateSomeWindowResizer(
      aura::Window* window,
      const gfx::Point& point_in_parent,
      int window_component) {
    return CreateWindowResizer(
        window,
        point_in_parent,
        window_component,
        aura::client::WINDOW_MOVE_SOURCE_MOUSE).release();
  }

  void DragStart(aura::Window* window) {
    initial_location_in_parent_ = window->bounds().origin();
    resizer_.reset(CreateSomeWindowResizer(window,
                                           initial_location_in_parent_,
                                           HTCAPTION));
    ASSERT_TRUE(resizer_.get());
  }

  void DragStartAtOffsetFromwindowOrigin(aura::Window* window,
                                         int dx,
                                         int dy) {
    initial_location_in_parent_ =
        window->bounds().origin() + gfx::Vector2d(dx, dy);
    resizer_.reset(CreateSomeWindowResizer(window,
                                           initial_location_in_parent_,
                                           HTCAPTION));
    ASSERT_TRUE(resizer_.get());
  }

  void ResizeStartAtOffsetFromwindowOrigin(aura::Window* window,
                                           int dx,
                                           int dy,
                                           int window_component) {
    initial_location_in_parent_ =
        window->bounds().origin() + gfx::Vector2d(dx, dy);
    resizer_.reset(CreateSomeWindowResizer(window,
                                           initial_location_in_parent_,
                                           window_component));
    ASSERT_TRUE(resizer_.get());
  }

  void DragMove(int dx, int dy) {
    resizer_->Drag(initial_location_in_parent_ + gfx::Vector2d(dx, dy), 0);
  }

  void DragEnd() {
    resizer_->CompleteDrag(0);
    resizer_.reset();
  }

  void DragRevert() {
    resizer_->RevertDrag();
    resizer_.reset();
  }

  // Panels are parented by panel container during drags.
  // All other windows that are tested here are parented by dock container
  // during drags.
  int CorrectContainerIdDuringDrag() {
    if (window_type_ == aura::client::WINDOW_TYPE_PANEL)
      return internal::kShellWindowId_PanelContainer;
    return internal::kShellWindowId_DockedContainer;
  }

  // Test dragging the window vertically (to detach if it is a panel) and then
  // horizontally to the edge with an added offset from the edge of |dx|.
  void DragRelativeToEdge(DockedEdge edge,
                          aura::Window* window,
                          int dx) {
    DragVerticallyAndRelativeToEdge(
        edge,
        window,
        dx,
        window_type_ == aura::client::WINDOW_TYPE_PANEL ? -100 : 20);
  }

  void DragToVerticalPositionAndToEdge(DockedEdge edge,
                                       aura::Window* window,
                                       int y) {
    DragToVerticalPositionRelativeToEdge(edge, window, 0, y);
  }

  void DragToVerticalPositionRelativeToEdge(DockedEdge edge,
                                            aura::Window* window,
                                            int dx,
                                            int y) {
    gfx::Rect initial_bounds = window->GetBoundsInScreen();
    DragVerticallyAndRelativeToEdge(edge, window, dx, y - initial_bounds.y());
  }

  // Detach if our window is a panel, then drag it vertically by |dy| and
  // horizontally to the edge with an added offset from the edge of |dx|.
  void DragVerticallyAndRelativeToEdge(DockedEdge edge,
                                       aura::Window* window,
                                       int dx,
                                       int dy) {
    aura::RootWindow* root_window = window->GetRootWindow();
    gfx::Rect initial_bounds = window->GetBoundsInScreen();

    if (window_type_ == aura::client::WINDOW_TYPE_PANEL) {
      ASSERT_NO_FATAL_FAILURE(DragStart(window));
      EXPECT_TRUE(window->GetProperty(kPanelAttachedKey));

      // Drag enough to detach since our tests assume panels to be initially
      // detached.
      DragMove(0, dy);
      EXPECT_EQ(CorrectContainerIdDuringDrag(), window->parent()->id());
      EXPECT_EQ(initial_bounds.x(), window->GetBoundsInScreen().x());
      EXPECT_EQ(initial_bounds.y() + dy, window->GetBoundsInScreen().y());

      // The panel should be detached when the drag completes.
      DragEnd();

      EXPECT_FALSE(window->GetProperty(kPanelAttachedKey));
      EXPECT_EQ(internal::kShellWindowId_DefaultContainer,
                window->parent()->id());
      EXPECT_EQ(root_window, window->GetRootWindow());
    }

    // avoid snap by clicking away from the border
    ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromwindowOrigin(window, 25, 5));

    // Drag the window left or right to the edge (or almost to it).
    if (edge == DOCKED_EDGE_LEFT)
      dx += window->GetRootWindow()->bounds().x() - initial_bounds.x();
    else if (edge == DOCKED_EDGE_RIGHT)
      dx += window->GetRootWindow()->bounds().right() - initial_bounds.right();
    DragMove(dx, window_type_ == aura::client::WINDOW_TYPE_PANEL ? 0 : dy);
    EXPECT_EQ(CorrectContainerIdDuringDrag(), window->parent()->id());
    // Release the mouse and the panel should be attached to the dock.
    DragEnd();

    // x-coordinate can get adjusted by snapping or sticking.
    // y-coordinate could be changed by possible automatic layout if docked.
    if (window->parent()->id() != internal::kShellWindowId_DockedContainer)
      EXPECT_EQ(initial_bounds.y() + dy, window->GetBoundsInScreen().y());
  }

  bool test_panels() const {
    return window_type_ == aura::client::WINDOW_TYPE_PANEL;
  }

 private:
  scoped_ptr<WindowResizer> resizer_;
  LauncherModel* model_;
  aura::client::WindowType window_type_;
  aura::test::TestWindowDelegate delegate_;

  // Location at start of the drag in |window->parent()|'s coordinates.
  gfx::Point initial_location_in_parent_;

  DISALLOW_COPY_AND_ASSIGN(DockedWindowResizerTest);
};

// Verifies a window can be dragged and attached to the dock.
TEST_P(DockedWindowResizerTest, AttachRightPrecise) {
  if (!SupportsHostWindowResize())
    return;

  scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), 0);

  // The window should be attached and snapped to the right edge.
  EXPECT_EQ(window->GetRootWindow()->bounds().right(),
            window->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, window->parent()->id());
}

// Verifies a window can be dragged and attached to the dock
// even if we overshoot the screen edge by a few pixels (sticky edge)
TEST_P(DockedWindowResizerTest, AttachRightOvershoot) {
  if (!SupportsHostWindowResize())
    return;

  scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), +4);

  // The window should be attached and snapped to the right edge.
  EXPECT_EQ(window->GetRootWindow()->bounds().right(),
            window->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, window->parent()->id());
}

// Verifies a window can be dragged and then if not quite reaching the screen
// edge it does not get docked to a screen edge and stays in the desktop.
TEST_P(DockedWindowResizerTest, AttachRightUndershoot) {
  if (!SupportsHostWindowResize())
    return;

  scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), -1);

  // The window should not be attached to the dock.
  EXPECT_EQ(window->GetRootWindow()->bounds().right() - 1,
            window->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer,
            window->parent()->id());
}

// Verifies a window can be dragged and attached to the dock.
TEST_P(DockedWindowResizerTest, AttachLeftPrecise) {
  if (!SupportsHostWindowResize())
    return;

  scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  DragRelativeToEdge(DOCKED_EDGE_LEFT, window.get(), 0);

  // The window should be attached and snapped to the left dock.
  EXPECT_EQ(window->GetRootWindow()->bounds().x(),
            window->GetBoundsInScreen().x());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, window->parent()->id());
}

// Verifies a window can be dragged and attached to the dock
// even if we overshoot the screen edge by a few pixels (sticky edge)
TEST_P(DockedWindowResizerTest, AttachLeftOvershoot) {
  if (!SupportsHostWindowResize())
    return;

  scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  DragRelativeToEdge(DOCKED_EDGE_LEFT, window.get(), -4);

  // The window should be attached and snapped to the left dock.
  EXPECT_EQ(window->GetRootWindow()->bounds().x(),
            window->GetBoundsInScreen().x());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, window->parent()->id());
}

// Verifies a window can be dragged and then if not quite reaching the screen
// edge it does not get docked to a screen edge and stays in the desktop.
TEST_P(DockedWindowResizerTest, AttachLeftUndershoot) {
  if (!SupportsHostWindowResize())
    return;

  scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  DragRelativeToEdge(DOCKED_EDGE_LEFT, window.get(), 1);

  // The window should not be attached to the dock.
  EXPECT_EQ(window->GetRootWindow()->bounds().x() + 1,
            window->GetBoundsInScreen().x());
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer,
            window->parent()->id());
}

// Dock on the right side, change shelf alignment, check that windows move to
// the opposite side.
TEST_P(DockedWindowResizerTest, AttachRightChangeShelf) {
  if (!SupportsHostWindowResize())
    return;

  scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), 0);

  // The window should be attached and snapped to the right edge.
  EXPECT_EQ(window->GetRootWindow()->bounds().right(),
            window->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, window->parent()->id());

  // set launcher shelf to be aligned on the right
  ash::Shell* shell = ash::Shell::GetInstance();
  shell->SetShelfAlignment(SHELF_ALIGNMENT_RIGHT,
                           shell->GetPrimaryRootWindow());
  // The window should have moved and get attached to the left dock.
  EXPECT_EQ(window->GetRootWindow()->bounds().x(),
            window->GetBoundsInScreen().x());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, window->parent()->id());

  // set launcher shelf to be aligned on the left
  shell->SetShelfAlignment(SHELF_ALIGNMENT_LEFT,
                           shell->GetPrimaryRootWindow());
  // The window should have moved and get attached to the right edge.
  EXPECT_EQ(window->GetRootWindow()->bounds().right(),
            window->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, window->parent()->id());

  // set launcher shelf to be aligned at the bottom
  shell->SetShelfAlignment(SHELF_ALIGNMENT_BOTTOM,
                           shell->GetPrimaryRootWindow());
  // The window should stay in the right edge.
  EXPECT_EQ(window->GetRootWindow()->bounds().right(),
            window->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, window->parent()->id());
}

// Dock on the right side, try to undock, then drag more to really undock
TEST_P(DockedWindowResizerTest, AttachTryDetach) {
  if (!SupportsHostWindowResize())
    return;

  scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), 0);

  // The window should be attached and snapped to the right edge.
  EXPECT_EQ(window->GetRootWindow()->bounds().right(),
            window->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, window->parent()->id());

  // Try to detach by dragging left less than kSnapToDockDistance.
  // The window should stay docked.
  ASSERT_NO_FATAL_FAILURE(DragStart(window.get()));
  DragMove(-4, -10);
  // Release the mouse and the window should be still attached to the dock.
  DragEnd();

  // The window should be still attached to the right edge.
  EXPECT_EQ(window->GetRootWindow()->bounds().right(),
            window->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, window->parent()->id());

  // Try to detach by dragging left by kSnapToDockDistance or more.
  // The window should get undocked.
  ASSERT_NO_FATAL_FAILURE(DragStart(window.get()));
  DragMove(-32, -10);
  // Release the mouse and the window should be no longer attached to the dock.
  DragEnd();

  // The window should be floating on the desktop again.
  EXPECT_EQ(window->GetRootWindow()->bounds().right() - 32,
            window->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer,
            window->parent()->id());
}

// Minimize a docked window, then restore it and check that it is still docked.
TEST_P(DockedWindowResizerTest, AttachMinimizeRestore) {
  if (!SupportsHostWindowResize())
    return;

  scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), 0);

  // The window should be attached and snapped to the right edge.
  EXPECT_EQ(window->GetRootWindow()->bounds().right(),
            window->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, window->parent()->id());

  // Minimize the window, it should be hidden.
  window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
  RunAllPendingInMessageLoop();
  EXPECT_FALSE(window->IsVisible());
  // Restore the window; window should be visible.
  window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
  RunAllPendingInMessageLoop();
  EXPECT_TRUE(window->IsVisible());
}

// Dock two windows, undock one, check that the other one is still docked.
TEST_P(DockedWindowResizerTest, AttachTwoWindows)
{
  if (!SupportsHostWindowResize())
    return;

  scoped_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  scoped_ptr<aura::Window> w2(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
  DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w2.get(), 50);

  // Both windows should be attached and snapped to the right edge.
  EXPECT_EQ(w1->GetRootWindow()->bounds().right(),
            w1->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());

  EXPECT_EQ(w2->GetRootWindow()->bounds().right(),
            w2->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w2->parent()->id());

  // Detach by dragging left (should get undocked).
  ASSERT_NO_FATAL_FAILURE(DragStart(w2.get()));
  // Drag up as well to avoid attaching panels to launcher shelf.
  DragMove(-32, -100);
  // Release the mouse and the window should be no longer attached to the edge.
  DragEnd();

  // The first window should be still docked.
  EXPECT_EQ(w1->GetRootWindow()->bounds().right(),
            w1->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());

  // The second window should be floating on the desktop again.
  EXPECT_EQ(w2->GetRootWindow()->bounds().right() - 32,
            w2->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer,
            w2->parent()->id());
}

// Dock one window, try to dock another window on the opposite side (should not
// dock).
TEST_P(DockedWindowResizerTest, AttachOnTwoSides)
{
  if (!SupportsHostWindowResize())
    return;

  scoped_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  scoped_ptr<aura::Window> w2(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
  DragToVerticalPositionAndToEdge(DOCKED_EDGE_LEFT, w2.get(), 50);

  // The first window should be attached and snapped to the right edge.
  EXPECT_EQ(w1->GetRootWindow()->bounds().right(),
            w1->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());

  // The second window should be near the left edge but not snapped.
  EXPECT_EQ(w2->GetRootWindow()->bounds().x(), w2->GetBoundsInScreen().x());
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer, w2->parent()->id());
}

// Reverting drag
TEST_P(DockedWindowResizerTest, RevertDragRestoresAttachment) {
  if (!SupportsHostWindowResize())
    return;

  scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), 0);

  // The window should be attached and snapped to the right edge.
  EXPECT_EQ(window->GetRootWindow()->bounds().right(),
            window->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, window->parent()->id());

  // Drag the window out but revert the drag
  ASSERT_NO_FATAL_FAILURE(DragStart(window.get()));
  DragMove(-50, 0);
  DragRevert();
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, window->parent()->id());

  // Detach window.
  ASSERT_NO_FATAL_FAILURE(DragStart(window.get()));
  DragMove(-50, 0);
  DragEnd();
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer,
            window->parent()->id());
}

// Move a docked window to the second display
TEST_P(DockedWindowResizerTest, DragAcrossDisplays) {
  if (!SupportsMultipleDisplays())
    return;

  UpdateDisplay("800x800,800x800");
  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
  scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  gfx::Rect initial_bounds = window->GetBoundsInScreen();
  EXPECT_EQ(root_windows[0], window->GetRootWindow());

  DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), 0);
  // The window should be attached and snapped to the right edge.
  EXPECT_EQ(window->GetRootWindow()->bounds().right(),
            window->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, window->parent()->id());

  // Undock and move to the right - enough to get it peeking at the other screen
  // but not enough to land in the other screen
  ASSERT_NO_FATAL_FAILURE(DragStart(window.get()));
  DragMove(70, 0);
  EXPECT_EQ(CorrectContainerIdDuringDrag(), window->parent()->id());
  DragEnd();
  EXPECT_NE(window->GetRootWindow()->bounds().right(),
            window->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer,
            window->parent()->id());
  EXPECT_EQ(root_windows[0], window->GetRootWindow());

  // Move back left - should dock again.
  ASSERT_NO_FATAL_FAILURE(DragStart(window.get()));
  DragMove(-70, 0);
  EXPECT_EQ(CorrectContainerIdDuringDrag(), window->parent()->id());
  DragEnd();
  EXPECT_EQ(window->GetRootWindow()->bounds().right(),
            window->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer,
            window->parent()->id());
  EXPECT_EQ(root_windows[0], window->GetRootWindow());

  // Undock and move to the right - enough to get the mouse pointer past the
  // edge of the screen and into the second screen. The window should now be
  // in the second screen and not docked.
  ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromwindowOrigin(
      window.get(),
      window->bounds().width()/2 + 10,
      0));
  DragMove(window->bounds().width()/2 - 5, 0);
  EXPECT_EQ(CorrectContainerIdDuringDrag(), window->parent()->id());
  DragEnd();
  EXPECT_NE(window->GetRootWindow()->bounds().right(),
            window->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer,
            window->parent()->id());
  EXPECT_EQ(root_windows[1], window->GetRootWindow());

  // Keep dragging it to the right until it docks. The window should now be
  // in the second screen.
  ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromwindowOrigin(
      window.get(),
      window->bounds().width()/2 + 10,
      0));
  DragMove(window->GetRootWindow()->GetBoundsInScreen().x() -
           window->GetBoundsInScreen().x(),
           0);
  EXPECT_EQ(CorrectContainerIdDuringDrag(), window->parent()->id());
  DragEnd();
  EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().x(),
            window->GetBoundsInScreen().x());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, window->parent()->id());
  EXPECT_EQ(root_windows[1], window->GetRootWindow());
}

// Dock two windows, undock one.
// Test the docked windows area size and default container resizing.
TEST_P(DockedWindowResizerTest, AttachTwoWindowsDetachOne)
{
  if (!SupportsHostWindowResize())
    return;

  scoped_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  scoped_ptr<aura::Window> w2(CreateTestWindow(gfx::Rect(0, 0, 210, 201)));
  // Work area should cover the whole screen.
  EXPECT_EQ(ScreenAsh::GetDisplayBoundsInParent(w2.get()).width(),
            ScreenAsh::GetDisplayWorkAreaBoundsInParent(w2.get()).width());

  DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
  // A window should be attached and snapped to the right edge.
  EXPECT_EQ(w1->GetRootWindow()->bounds().right(),
            w1->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  DockedWindowLayoutManager* manager =
      static_cast<DockedWindowLayoutManager*>(w1->parent()->layout_manager());
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  EXPECT_EQ(w1->bounds().width(), manager->docked_width_);

  DragToVerticalPositionRelativeToEdge(DOCKED_EDGE_RIGHT, w2.get(), 0, 100);
  // Both windows should now be attached and snapped to the right edge.
  EXPECT_EQ(w2->GetRootWindow()->bounds().right(),
            w2->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w2->parent()->id());
  // Dock width should be set to a wider window.
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  EXPECT_EQ(std::max(w1->bounds().width(), w2->bounds().width()),
            manager->docked_width_);

  // Try to detach by dragging left a bit (should not get undocked).
  // This would normally detach a single docked window but since we have another
  // window and the mouse pointer does not leave the dock area the window
  // should stay docked.
  ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromwindowOrigin(w2.get(), 60, 0));
  // Drag up as well as left to avoid attaching panels to launcher shelf.
  DragMove(-40, -40);
  // Release the mouse and the window should be still attached to the edge.
  DragEnd();

  // The first window should be still docked.
  EXPECT_EQ(w1->GetRootWindow()->bounds().right(),
            w1->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());

  // The second window should be still docked.
  EXPECT_EQ(w2->GetRootWindow()->bounds().right(),
            w2->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w2->parent()->id());

  // Detach by dragging left more (should get undocked).
  ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromwindowOrigin(
      w2.get(),
      w2->bounds().width()/2 + 10,
      0));
  // Drag up as well to avoid attaching panels to launcher shelf.
  DragMove(-(w2->bounds().width()/2 + 20), -100);
  // Release the mouse and the window should be no longer attached to the edge.
  DragEnd();

  // The second window should be floating on the desktop again.
  EXPECT_EQ(w2->GetRootWindow()->bounds().right() -
            (w2->bounds().width()/2 + 20),
            w2->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer, w2->parent()->id());
  // Dock width should be set to remaining single docked window.
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  EXPECT_EQ(w1->bounds().width(), manager->docked_width_);
}

// Dock one windows. Maximize other testing desktop resizing.
TEST_P(DockedWindowResizerTest, AttachWindowMaximizeOther)
{
  if (!SupportsHostWindowResize())
    return;

  scoped_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  scoped_ptr<aura::Window> w2(CreateTestWindow(gfx::Rect(0, 0, 210, 201)));
  // Work area should cover the whole screen.
  EXPECT_EQ(ScreenAsh::GetDisplayBoundsInParent(w2.get()).width(),
            ScreenAsh::GetDisplayWorkAreaBoundsInParent(w2.get()).width());

  DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
  // A window should be attached and snapped to the right edge.
  EXPECT_EQ(w1->GetRootWindow()->bounds().right(),
            w1->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  DockedWindowLayoutManager* manager =
      static_cast<DockedWindowLayoutManager*>(w1->parent()->layout_manager());
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  EXPECT_EQ(w1->bounds().width(), manager->docked_width_);

  DragToVerticalPositionRelativeToEdge(DOCKED_EDGE_RIGHT,
                                       w2.get(),
                                       -(w2->bounds().width()/2 + 20),
                                       50);
  // The first window should be still docked.
  EXPECT_EQ(w1->GetRootWindow()->bounds().right(),
            w1->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());

  // The second window should be floating on the desktop.
  EXPECT_EQ(w2->GetRootWindow()->bounds().right() -
            (w2->bounds().width()/2 + 20),
            w2->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer, w2->parent()->id());
  // Dock width should be set to remaining single docked window.
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  EXPECT_EQ(w1->bounds().width(), manager->docked_width_);
  // Desktop work area should now shrink.
  EXPECT_EQ(ScreenAsh::GetDisplayBoundsInParent(w2.get()).width() -
            manager->docked_width_ - DockedWindowLayoutManager::kMinDockGap,
            ScreenAsh::GetDisplayWorkAreaBoundsInParent(w2.get()).width());

  // Maximize the second window - Maximized area should be shrunk.
  const gfx::Rect restored_bounds = w2->bounds();
  wm::MaximizeWindow(w2.get());
  EXPECT_EQ(ScreenAsh::GetDisplayBoundsInParent(w2.get()).width() -
            manager->docked_width_ - DockedWindowLayoutManager::kMinDockGap,
            w2->bounds().width());

  // Detach the first window (this should require very little drag).
  ASSERT_NO_FATAL_FAILURE(DragStart(w1.get()));
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  DragMove(-35, 10);
  // Alignment is set to "NONE" when drag starts.
  EXPECT_EQ(DOCKED_ALIGNMENT_NONE, manager->alignment_);
  // Release the mouse and the window should be no longer attached to the edge.
  DragEnd();
  EXPECT_EQ(DOCKED_ALIGNMENT_NONE, manager->alignment_);
  // Dock should get shrunk and desktop should get expanded.
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer, w1->parent()->id());
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer, w2->parent()->id());
  EXPECT_EQ(DOCKED_ALIGNMENT_NONE, manager->alignment_);
  EXPECT_EQ(0, manager->docked_width_);
  // The second window should now get resized and take up the whole screen.
  EXPECT_EQ(ScreenAsh::GetDisplayBoundsInParent(w2.get()).width(),
            w2->bounds().width());

  // Dock the first window to the left edge.
  // Click at an offset from origin to prevent snapping.
  ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromwindowOrigin(w1.get(), 10, 0));
  DragMove(-w1->bounds().x(), 0);
  // Alignment set to "NONE" during the drag of the window when none are docked.
  EXPECT_EQ(DOCKED_ALIGNMENT_NONE, manager->alignment_);
  // Release the mouse and the window should be now attached to the edge.
  DragEnd();
  // Dock should get expanded and desktop should get shrunk.
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, manager->alignment_);
  EXPECT_EQ(w1->bounds().width(), manager->docked_width_);
  // Second window should still be in the desktop.
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer, w2->parent()->id());
  // Maximized window should be shrunk.
  EXPECT_EQ(ScreenAsh::GetDisplayBoundsInParent(w2.get()).width() -
            manager->docked_width_ - DockedWindowLayoutManager::kMinDockGap,
            w2->bounds().width());

  // Unmaximize the second window.
  wm::RestoreWindow(w2.get());
  // Its bounds should get restored.
  EXPECT_EQ(restored_bounds, w2->bounds());
}

// Dock one window. Test the sticky behavior near screen or desktop edge.
TEST_P(DockedWindowResizerTest, AttachOneTestSticky)
{
  if (!SupportsHostWindowResize())
    return;

  scoped_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  scoped_ptr<aura::Window> w2(CreateTestWindow(gfx::Rect(0, 0, 210, 201)));
  // Work area should cover the whole screen.
  EXPECT_EQ(ScreenAsh::GetDisplayBoundsInParent(w2.get()).width(),
            ScreenAsh::GetDisplayWorkAreaBoundsInParent(w2.get()).width());

  DragToVerticalPositionAndToEdge(DOCKED_EDGE_LEFT, w1.get(), 20);
  // A window should be attached and snapped to the left edge.
  EXPECT_EQ(w1->GetRootWindow()->bounds().x(),
            w1->GetBoundsInScreen().x());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  DockedWindowLayoutManager* manager =
      static_cast<DockedWindowLayoutManager*>(w1->parent()->layout_manager());
  // The first window should be docked.
  EXPECT_EQ(w1->GetRootWindow()->bounds().x(),
            w1->GetBoundsInScreen().x());
  // Dock width should be set to that of a single docked window.
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, manager->alignment_);
  EXPECT_EQ(w1->bounds().width(), manager->docked_width_);

  // Position second window in the desktop just to the right of the docked w1.
  DragToVerticalPositionRelativeToEdge(DOCKED_EDGE_LEFT,
                                       w2.get(),
                                       w1->bounds().right() + 20,
                                       50);
  // The second window should be floating on the desktop.
  EXPECT_EQ(w2->GetRootWindow()->bounds().x() + (w1->bounds().right() + 20),
            w2->GetBoundsInScreen().x());
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer, w2->parent()->id());
  // Dock width should be set to that of a single docked window.
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, manager->alignment_);
  EXPECT_EQ(w1->bounds().width(), manager->docked_width_);

  // Drag w2 almost to the dock, the mouse pointer not quite reaching the dock.
  ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromwindowOrigin(w2.get(), 10, 0));
  DragMove(1 + manager->docked_width_ - w2->bounds().x(), 0);
  // Alignment set to "LEFT" during the drag because dock has a window in it.
  EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, manager->alignment_);
  // Release the mouse and the window should not be attached to the edge.
  DragEnd();
  // Dock should still have only one window in it.
  EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, manager->alignment_);
  EXPECT_EQ(w1->bounds().width(), manager->docked_width_);
  // The second window should still be in the desktop.
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer, w2->parent()->id());

  // Drag w2 by a bit more - it should resist the drag (stuck edges)
  int start_x = w2->bounds().x();
  ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromwindowOrigin(w2.get(), 100, 5));
  DragMove(-2, 0);
  // Window should not actually move.
  EXPECT_EQ(start_x, w2->bounds().x());
  // Alignment set to "LEFT" during the drag because dock has a window in it.
  EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, manager->alignment_);
  // Release the mouse and the window should not be attached to the edge.
  DragEnd();
  // Window should be still where it was before the last drag started.
  EXPECT_EQ(start_x, w2->bounds().x());
  // Dock should still have only one window in it
  EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, manager->alignment_);
  EXPECT_EQ(w1->bounds().width(), manager->docked_width_);
  // The second window should still be in the desktop
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer, w2->parent()->id());

  // Drag w2 by more than the stuck threshold and drop it into the dock.
  ASSERT_NO_FATAL_FAILURE(DragStart(w2.get()));
  DragMove(-100, 0);
  // Window should actually move.
  EXPECT_NE(start_x, w2->bounds().x());
  // Alignment set to "LEFT" during the drag because dock has a window in it.
  EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, manager->alignment_);
  // Release the mouse and the window should be attached to the edge.
  DragEnd();
  // Both windows are docked now.
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w2->parent()->id());
  // Dock should get expanded and desktop should get shrunk.
  EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, manager->alignment_);
  EXPECT_EQ(std::max(w1->bounds().width(), w2->bounds().width()),
            manager->docked_width_);
  // Desktop work area should now shrink by dock width.
  EXPECT_EQ(ScreenAsh::GetDisplayBoundsInParent(w2.get()).width() -
            manager->docked_width_ - DockedWindowLayoutManager::kMinDockGap,
            ScreenAsh::GetDisplayWorkAreaBoundsInParent(w2.get()).width());
}

// Dock two windows, resize one or both.
// Test the docked windows area size and remaining desktop resizing.
TEST_P(DockedWindowResizerTest, ResizeTwoWindows)
{
  if (!SupportsHostWindowResize())
    return;

  // Wider display to start since panels are limited to half the display width.
  UpdateDisplay("1000x400");
  scoped_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  scoped_ptr<aura::Window> w2(CreateTestWindow(gfx::Rect(0, 0, 210, 201)));
  // Work area should cover the whole screen.
  EXPECT_EQ(ScreenAsh::GetDisplayBoundsInParent(w2.get()).width(),
            ScreenAsh::GetDisplayWorkAreaBoundsInParent(w2.get()).width());

  DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
  // A window should be attached and snapped to the right edge.
  EXPECT_EQ(w1->GetRootWindow()->bounds().right(),
            w1->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  DockedWindowLayoutManager* manager =
      static_cast<DockedWindowLayoutManager*>(w1->parent()->layout_manager());
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  EXPECT_EQ(w1->bounds().width(), manager->docked_width_);

  DragToVerticalPositionRelativeToEdge(DOCKED_EDGE_RIGHT, w2.get(), 0, 100);
  // Both windows should now be attached and snapped to the right edge.
  EXPECT_EQ(w2->GetRootWindow()->bounds().right(),
            w2->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w2->parent()->id());
  // Dock width should be set to a wider window.
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  EXPECT_EQ(std::max(w1->bounds().width(), w2->bounds().width()),
            manager->docked_width_);

  // Resize the first window left by a bit and test that the dock expands.
  int previous_width = w1->bounds().width();
  const int kResizeSpan1 = 30;
  ASSERT_NO_FATAL_FAILURE(ResizeStartAtOffsetFromwindowOrigin(w1.get(),
                                                              0,
                                                              20,
                                                              HTLEFT));
  DragMove(-kResizeSpan1, 0);
  // Alignment set to "RIGHT" during the drag because dock has a window in it.
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  // Release the mouse and the window should be attached to the edge.
  DragEnd();
  // Dock should still have both windows in it.
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w2->parent()->id());
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  // w1 is now wider than w2 and the dock should expand and be as wide as w1.
  EXPECT_EQ(previous_width + kResizeSpan1, w1->bounds().width());
  EXPECT_GT(w1->bounds().width(), w2->bounds().width());
  EXPECT_EQ(w1->bounds().width(), manager->docked_width_);
  // Desktop work area should shrink.
  EXPECT_EQ(ScreenAsh::GetDisplayBoundsInParent(w2.get()).width() -
            manager->docked_width_ - DockedWindowLayoutManager::kMinDockGap,
            ScreenAsh::GetDisplayWorkAreaBoundsInParent(w2.get()).width());

  // Resize the first window left by more than the dock maximum width.
  // This should cause the window to overhang and the dock to shrink to w2.
  previous_width = w1->bounds().width();
  const int kResizeSpan2 = 250;
  ASSERT_NO_FATAL_FAILURE(ResizeStartAtOffsetFromwindowOrigin(w1.get(),
                                                              0,
                                                              20,
                                                              HTLEFT));
  DragMove(-kResizeSpan2, 0);
  // Alignment set to "RIGHT" during the drag because dock has a window in it.
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  // Release the mouse and the window should be attached to the edge.
  DragEnd();
  // Dock should still have both windows in it.
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w2->parent()->id());
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  // w1 is now wider than the maximum dock width and the dock should shrink to
  // the next widest window (w2).
  EXPECT_EQ(previous_width + kResizeSpan2, w1->bounds().width());
  EXPECT_GT(w1->bounds().width(), w2->bounds().width());
  EXPECT_EQ(w2->bounds().width(), manager->docked_width_);
  // Desktop work area should shrink.
  EXPECT_EQ(ScreenAsh::GetDisplayBoundsInParent(w2.get()).width() -
            manager->docked_width_ - DockedWindowLayoutManager::kMinDockGap,
            ScreenAsh::GetDisplayWorkAreaBoundsInParent(w2.get()).width());

  // Resize the first window right to get it completely inside the docked area.
  previous_width = w1->bounds().width();
  const int kResizeSpan3 = 100;
  ASSERT_NO_FATAL_FAILURE(ResizeStartAtOffsetFromwindowOrigin(w1.get(),
                                                              0,
                                                              20,
                                                              HTLEFT));
  DragMove(kResizeSpan3, 0);
  // Alignment set to "RIGHT" during the drag because dock has a window in it.
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  // Release the mouse and the window should be attached to the edge.
  DragEnd();
  // Dock should still have both windows in it.
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w2->parent()->id());
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  // w1 is still wider than w2 so the dock should expand and be as wide as w1.
  EXPECT_EQ(previous_width - kResizeSpan3, w1->bounds().width());
  EXPECT_GT(w1->bounds().width(), w2->bounds().width());
  EXPECT_EQ(w1->bounds().width(), manager->docked_width_);
  // Desktop work area should shrink.
  EXPECT_EQ(ScreenAsh::GetDisplayBoundsInParent(w2.get()).width() -
            manager->docked_width_ - DockedWindowLayoutManager::kMinDockGap,
            ScreenAsh::GetDisplayWorkAreaBoundsInParent(w2.get()).width());

  // Resize the first window left to be overhang again.
  previous_width = w1->bounds().width();
  ASSERT_NO_FATAL_FAILURE(ResizeStartAtOffsetFromwindowOrigin(w1.get(),
                                                              0,
                                                              20,
                                                              HTLEFT));
  DragMove(-kResizeSpan3, 0);
  DragEnd();
  EXPECT_EQ(previous_width + kResizeSpan3, w1->bounds().width());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  // Docked area should be as wide as the second window - the first is too wide.
  EXPECT_EQ(w2->bounds().width(), manager->docked_width_);

  // Undock the second window. Docked area should shrink to its minimum size.
  ASSERT_NO_FATAL_FAILURE(DragStart(w2.get()));
  // Drag up as well to avoid attaching panels to launcher shelf.
  DragMove(-40, -100);
  // Alignment set to "RIGHT" since we have another window docked.
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  // Release the mouse and the window should be no longer attached to the edge.
  DragEnd();
  EXPECT_EQ(internal::kShellWindowId_DefaultContainer, w2->parent()->id());
  // Dock should get shrunk to minimum size.
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  EXPECT_EQ(manager->kMinDockWidth, manager->docked_width_);
  // The first window should be still docked.
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  // Desktop work area should be inset.
  EXPECT_EQ(ScreenAsh::GetDisplayBoundsInParent(w2.get()).width() -
            manager->docked_width_ - DockedWindowLayoutManager::kMinDockGap,
            ScreenAsh::GetDisplayWorkAreaBoundsInParent(w2.get()).width());
}

TEST_P(DockedWindowResizerTest, DragToShelf)
{
  if (!SupportsHostWindowResize())
    return;

  scoped_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
  // Work area should cover the whole screen.
  EXPECT_EQ(ScreenAsh::GetDisplayBoundsInParent(w1.get()).width(),
            ScreenAsh::GetDisplayWorkAreaBoundsInParent(w1.get()).width());

  DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
  // A window should be attached and snapped to the right edge.
  EXPECT_EQ(w1->GetRootWindow()->bounds().right(),
            w1->GetBoundsInScreen().right());
  EXPECT_EQ(internal::kShellWindowId_DockedContainer, w1->parent()->id());
  DockedWindowLayoutManager* manager =
      static_cast<DockedWindowLayoutManager*>(w1->parent()->layout_manager());
  EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, manager->alignment_);
  EXPECT_EQ(w1->bounds().width(), manager->docked_width_);

  // Detach and drag down to shelf.
  ASSERT_NO_FATAL_FAILURE(DragStart(w1.get()));
  DragMove(-40, 0);
  // Alignment is set to "NONE" when drag starts.
  EXPECT_EQ(DOCKED_ALIGNMENT_NONE, manager->alignment_);
  // Release the mouse and the window should be no longer attached to the edge.
  DragEnd();
  EXPECT_EQ(DOCKED_ALIGNMENT_NONE, manager->alignment_);

  // Drag down almost to shelf. A panel will snap, a regular window won't.
  ShelfWidget* shelf = Launcher::ForPrimaryDisplay()->shelf_widget();
  const int shelf_y = shelf->GetWindowBoundsInScreen().y();
  const int kDistanceFromShelf = 10;
  ASSERT_NO_FATAL_FAILURE(DragStart(w1.get()));
  DragMove(0, -kDistanceFromShelf + shelf_y - w1->bounds().bottom());
  DragEnd();
  if (test_panels()) {
    // The panel should be touching the shelf and attached.
    EXPECT_EQ(shelf_y, w1->bounds().bottom());
    EXPECT_TRUE(w1->GetProperty(kPanelAttachedKey));
  } else {
    // The window should not be touching the shelf.
    EXPECT_EQ(shelf_y - kDistanceFromShelf, w1->bounds().bottom());
  }
}

// Tests run twice - on both panels and normal windows
INSTANTIATE_TEST_CASE_P(NormalOrPanel,
                        DockedWindowResizerTest,
                        testing::Values(aura::client::WINDOW_TYPE_NORMAL,
                                        aura::client::WINDOW_TYPE_PANEL));
}  // namespace internal
}  // namespace ash