summaryrefslogtreecommitdiffstats
path: root/athena/wm/window_manager_unittest.cc
blob: 9bcb5b8906a199207f9035e2f95fdbdb5d531a52 (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
// Copyright 2014 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 "athena/wm/public/window_manager.h"

#include "athena/screen/public/screen_manager.h"
#include "athena/test/athena_test_base.h"
#include "athena/wm/public/window_list_provider.h"
#include "athena/wm/split_view_controller.h"
#include "athena/wm/test/window_manager_impl_test_api.h"
#include "athena/wm/window_manager_impl.h"
#include "ui/aura/client/window_tree_client.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/window.h"
#include "ui/base/hit_test.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/display.h"
#include "ui/gfx/screen.h"
#include "ui/wm/core/window_util.h"

namespace athena {

class WindowManagerTest : public test::AthenaTestBase {
 public:
  WindowManagerTest() {}
  virtual ~WindowManagerTest() {}

  scoped_ptr<aura::Window> CreateAndShowWindow(aura::WindowDelegate* delegate) {
    scoped_ptr<aura::Window> window(CreateTestWindow(delegate, gfx::Rect()));
    window->Show();
    return window.Pass();
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(WindowManagerTest);
};

TEST_F(WindowManagerTest, OverviewModeBasics) {
  aura::test::TestWindowDelegate delegate;
  scoped_ptr<aura::Window> first(CreateAndShowWindow(&delegate));
  scoped_ptr<aura::Window> second(CreateAndShowWindow(&delegate));

  test::WindowManagerImplTestApi wm_api;
  wm::ActivateWindow(second.get());

  ASSERT_FALSE(WindowManager::GetInstance()->IsOverviewModeActive());
  EXPECT_EQ(first->bounds().ToString(), second->bounds().ToString());
  EXPECT_EQ(gfx::Screen::GetNativeScreen()
                ->GetPrimaryDisplay()
                .work_area()
                .size()
                .ToString(),
            first->bounds().size().ToString());
  EXPECT_FALSE(WindowManager::GetInstance()->IsOverviewModeActive());

  // Tests that going into overview mode does not change the window bounds.
  WindowManager::GetInstance()->ToggleOverview();
  ASSERT_TRUE(WindowManager::GetInstance()->IsOverviewModeActive());
  EXPECT_EQ(first->bounds().ToString(), second->bounds().ToString());
  EXPECT_EQ(gfx::Screen::GetNativeScreen()
                ->GetPrimaryDisplay()
                .work_area()
                .size()
                .ToString(),
            first->bounds().size().ToString());
  EXPECT_TRUE(first->IsVisible());
  EXPECT_TRUE(second->IsVisible());

  // Terminate overview mode. |first| should be hidden, since it's not visible
  // to the user anymore.
  WindowManager::GetInstance()->ToggleOverview();
  ASSERT_FALSE(WindowManager::GetInstance()->IsOverviewModeActive());
  EXPECT_FALSE(first->IsVisible());
  EXPECT_TRUE(second->IsVisible());
}

TEST_F(WindowManagerTest, OverviewToSplitViewMode) {
  test::WindowManagerImplTestApi wm_api;

  aura::test::TestWindowDelegate delegate;
  scoped_ptr<aura::Window> w1(CreateAndShowWindow(&delegate));
  scoped_ptr<aura::Window> w2(CreateAndShowWindow(&delegate));
  scoped_ptr<aura::Window> w3(CreateAndShowWindow(&delegate));
  wm::ActivateWindow(w3.get());

  WindowManager::GetInstance()->ToggleOverview();
  EXPECT_TRUE(w1->IsVisible());
  EXPECT_TRUE(w2->IsVisible());
  EXPECT_TRUE(w3->IsVisible());

  // Go into split-view mode.
  WindowOverviewModeDelegate* overview_delegate = wm_api.wm();
  overview_delegate->OnSelectSplitViewWindow(w3.get(), NULL, w3.get());
  EXPECT_TRUE(w3->IsVisible());
  EXPECT_TRUE(w2->IsVisible());
  EXPECT_FALSE(w1->IsVisible());
}

TEST_F(WindowManagerTest, NewWindowFromOverview) {
  aura::test::TestWindowDelegate delegate;
  scoped_ptr<aura::Window> w1(CreateAndShowWindow(&delegate));
  scoped_ptr<aura::Window> w2(CreateAndShowWindow(&delegate));

  WindowManager::GetInstance()->ToggleOverview();
  EXPECT_TRUE(w1->IsVisible());
  EXPECT_TRUE(w2->IsVisible());

  // Test that opening a new window exits overview mode. The new window could
  // have been opened by JavaScript or by the home card.
  scoped_ptr<aura::Window> w3(CreateAndShowWindow(&delegate));

  ASSERT_FALSE(WindowManager::GetInstance()->IsOverviewModeActive());
  EXPECT_TRUE(w3->IsVisible());
  EXPECT_TRUE(wm::IsActiveWindow(w3.get()));
  EXPECT_FALSE(w1->IsVisible());
  EXPECT_FALSE(w2->IsVisible());
}

TEST_F(WindowManagerTest, BezelGestureToSplitViewMode) {
  aura::test::TestWindowDelegate delegate;
  scoped_ptr<aura::Window> first(CreateAndShowWindow(&delegate));
  scoped_ptr<aura::Window> second(CreateAndShowWindow(&delegate));
  scoped_ptr<aura::Window> third(CreateAndShowWindow(&delegate));

  test::WindowManagerImplTestApi wm_api;

  // Test that going into split-view mode with two-finger gesture selects the
  // correct windows on left and right splits.
  ui::test::EventGenerator generator(root_window());
  const gfx::Point start_points[2] = {
      gfx::Point(2, 10), gfx::Point(4, 20),
  };
  const int kEventTimeSepration = 16;
  int x_middle = root_window()->bounds().width() / 2;
  generator.GestureMultiFingerScroll(
      2, start_points, kEventTimeSepration, 1, x_middle, 0);
  ASSERT_TRUE(wm_api.GetSplitViewController()->IsSplitViewModeActive());
  EXPECT_EQ(second.get(), wm_api.GetSplitViewController()->left_window());
  EXPECT_EQ(third.get(), wm_api.GetSplitViewController()->right_window());
  EXPECT_EQ(second->bounds().size().ToString(),
            third->bounds().size().ToString());
}

TEST_F(WindowManagerTest, BezelGestureToSwitchBetweenWindows) {
  aura::test::TestWindowDelegate delegate;
  scoped_ptr<aura::Window> first(CreateAndShowWindow(&delegate));
  scoped_ptr<aura::Window> second(CreateAndShowWindow(&delegate));
  scoped_ptr<aura::Window> third(CreateAndShowWindow(&delegate));
  first->Hide();
  second->Hide();

  test::WindowManagerImplTestApi wm_api;

  EXPECT_EQ(third.get(),
            wm_api.GetWindowListProvider()->GetWindowList().back());

  // Do a two-finger swipe from the left bezel.
  ui::test::EventGenerator generator(root_window());
  const gfx::Point left_bezel_points[2] = {
      gfx::Point(2, 10), gfx::Point(4, 20),
  };
  const int kEventTimeSepration = 16;
  int width = root_window()->bounds().width();
  generator.GestureMultiFingerScroll(
      2, left_bezel_points, kEventTimeSepration, 1, width, 0);
  EXPECT_TRUE(wm::IsActiveWindow(second.get()));
  EXPECT_EQ(second.get(),
            wm_api.GetWindowListProvider()->GetWindowList().back());
}

TEST_F(WindowManagerTest, TitleDragSwitchBetweenWindows) {
  aura::test::TestWindowDelegate delegate;
  delegate.set_window_component(HTCAPTION);
  scoped_ptr<aura::Window> first(CreateAndShowWindow(&delegate));
  scoped_ptr<aura::Window> second(CreateAndShowWindow(&delegate));
  scoped_ptr<aura::Window> third(CreateAndShowWindow(&delegate));

  test::WindowManagerImplTestApi wm_api;

  EXPECT_EQ(third.get(),
            wm_api.GetWindowListProvider()->GetWindowList().back());

  // Do a title-swipe from the top to switch to the previous window.
  ui::test::EventGenerator generator(root_window());
  generator.GestureScrollSequence(gfx::Point(20, 10),
                                  gfx::Point(20, 400),
                                  base::TimeDelta::FromMilliseconds(20),
                                  5);
  EXPECT_TRUE(wm::IsActiveWindow(second.get()));
  EXPECT_EQ(second.get(),
            wm_api.GetWindowListProvider()->GetWindowList().back());
  EXPECT_TRUE(second->IsVisible());
  EXPECT_FALSE(third->IsVisible());

  // Performing the same gesture again will switch back to |third|.
  generator.GestureScrollSequence(gfx::Point(20, 10),
                                  gfx::Point(20, 400),
                                  base::TimeDelta::FromMilliseconds(20),
                                  5);
  EXPECT_TRUE(wm::IsActiveWindow(third.get()));
  EXPECT_EQ(third.get(),
            wm_api.GetWindowListProvider()->GetWindowList().back());
  EXPECT_FALSE(second->IsVisible());
  EXPECT_TRUE(third->IsVisible());

  // Perform a swipe that doesn't go enough to perform the window switch.
  generator.GestureScrollSequence(gfx::Point(20, 10),
                                  gfx::Point(20, 90),
                                  base::TimeDelta::FromMilliseconds(20),
                                  5);
  EXPECT_TRUE(wm::IsActiveWindow(third.get()));
  EXPECT_EQ(third.get(),
            wm_api.GetWindowListProvider()->GetWindowList().back());
  EXPECT_FALSE(second->IsVisible());
  EXPECT_TRUE(third->IsVisible());
}

TEST_F(WindowManagerTest, TitleDragSwitchBetweenWindowsInSplitViewMode) {
  aura::test::TestWindowDelegate delegate;
  delegate.set_window_component(HTCAPTION);
  scoped_ptr<aura::Window> first(CreateAndShowWindow(&delegate));
  scoped_ptr<aura::Window> second(CreateAndShowWindow(&delegate));
  scoped_ptr<aura::Window> third(CreateAndShowWindow(&delegate));
  scoped_ptr<aura::Window> fourth(CreateAndShowWindow(&delegate));

  test::WindowManagerImplTestApi wm_api;

  // Test that going into split-view mode with two-finger gesture selects the
  // correct windows on left and right splits.
  ui::test::EventGenerator generator(root_window());
  const gfx::Point start_points[2] = {
      gfx::Point(2, 10), gfx::Point(4, 20),
  };
  const int kEventTimeSepration = 16;
  int x_middle = root_window()->bounds().width() / 2;
  generator.GestureMultiFingerScroll(
      2, start_points, kEventTimeSepration, 1, x_middle, 0);
  ASSERT_TRUE(wm_api.GetSplitViewController()->IsSplitViewModeActive());
  EXPECT_EQ(third.get(), wm_api.GetSplitViewController()->left_window());
  EXPECT_EQ(fourth.get(), wm_api.GetSplitViewController()->right_window());

  // Swipe the title of the left window. It should switch to |second|.
  generator.GestureScrollSequence(gfx::Point(20, 10),
                                  gfx::Point(20, 400),
                                  base::TimeDelta::FromMilliseconds(20),
                                  5);
  EXPECT_EQ(second.get(), wm_api.GetSplitViewController()->left_window());
  EXPECT_EQ(fourth.get(), wm_api.GetSplitViewController()->right_window());
  aura::Window::Windows windows =
      wm_api.GetWindowListProvider()->GetWindowList();
  ASSERT_EQ(4u, windows.size());
  EXPECT_EQ(second.get(), windows[3]);
  EXPECT_EQ(fourth.get(), windows[2]);

  // Swipe the title of the right window now. It should switch to |third|.
  generator.GestureScrollSequence(gfx::Point(x_middle + 20, 10),
                                  gfx::Point(x_middle + 20, 400),
                                  base::TimeDelta::FromMilliseconds(20),
                                  5);
  EXPECT_EQ(second.get(), wm_api.GetSplitViewController()->left_window());
  EXPECT_EQ(third.get(), wm_api.GetSplitViewController()->right_window());
  windows = wm_api.GetWindowListProvider()->GetWindowList();
  ASSERT_EQ(4u, windows.size());
  EXPECT_EQ(second.get(), windows[3]);
  EXPECT_EQ(third.get(), windows[2]);
}

TEST_F(WindowManagerTest, NewWindowBounds) {
  aura::test::TestWindowDelegate delegate;
  scoped_ptr<aura::Window> first(CreateAndShowWindow(&delegate));

  test::WindowManagerImplTestApi wm_api;
  // The window should have the same size as the container.
  const gfx::Size work_area =
      gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().work_area().size();
  EXPECT_EQ(work_area.ToString(),
            first->bounds().size().ToString());
  EXPECT_TRUE(first->bounds().origin().IsOrigin());

  // A second window should have the same bounds as the first one.
  scoped_ptr<aura::Window> second(CreateAndShowWindow(&delegate));
  EXPECT_EQ(first->bounds().ToString(), second->bounds().ToString());

  // Get into split view.
  wm_api.GetSplitViewController()->ActivateSplitMode(NULL, NULL);
  const gfx::Rect left_bounds =
      wm_api.GetSplitViewController()->left_window()->bounds();
  EXPECT_NE(work_area.ToString(),
            left_bounds.size().ToString());

  // A new window should replace the left window when in split view.
  scoped_ptr<aura::Window> third(CreateAndShowWindow(&delegate));
  EXPECT_EQ(wm_api.GetSplitViewController()->left_window(), third.get());
  EXPECT_EQ(left_bounds.ToString(), third->bounds().ToString());
}

TEST_F(WindowManagerTest, SplitModeActivationByShortcut) {
  test::WindowManagerImplTestApi wm_api;

  aura::test::TestWindowDelegate delegate;
  scoped_ptr<aura::Window> w1(CreateTestWindow(&delegate, gfx::Rect()));
  w1->Show();

  ui::test::EventGenerator generator(root_window());

  // Splitview mode needs at least two windows.
  generator.PressKey(ui::VKEY_F6, ui::EF_CONTROL_DOWN);
  generator.ReleaseKey(ui::VKEY_F6, ui::EF_CONTROL_DOWN);
  EXPECT_FALSE(wm_api.GetSplitViewController()->IsSplitViewModeActive());

  scoped_ptr<aura::Window> w2(CreateAndShowWindow(&delegate));
  w2->Show();

  generator.PressKey(ui::VKEY_F6, ui::EF_CONTROL_DOWN);
  generator.ReleaseKey(ui::VKEY_F6, ui::EF_CONTROL_DOWN);
  EXPECT_TRUE(wm_api.GetSplitViewController()->IsSplitViewModeActive());
  int width =
      gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().work_area().width();

  EXPECT_EQ(width / 2, w1->bounds().width());
  EXPECT_EQ(width / 2, w2->bounds().width());

  // Toggle back to normal mode.
  generator.PressKey(ui::VKEY_F6, ui::EF_CONTROL_DOWN);
  generator.ReleaseKey(ui::VKEY_F6, ui::EF_CONTROL_DOWN);
  EXPECT_FALSE(wm_api.GetSplitViewController()->IsSplitViewModeActive());

  EXPECT_EQ(width, w1->bounds().width());
  EXPECT_EQ(width, w2->bounds().width());
}

TEST_F(WindowManagerTest, OverviewModeFromSplitMode) {
  test::WindowManagerImplTestApi wm_api;

  aura::test::TestWindowDelegate delegate;
  scoped_ptr<aura::Window> w1(CreateAndShowWindow(&delegate));
  scoped_ptr<aura::Window> w2(CreateAndShowWindow(&delegate));
  scoped_ptr<aura::Window> w3(CreateAndShowWindow(&delegate));

  // Get into split-view mode, and then turn on overview mode.
  wm_api.GetSplitViewController()->ActivateSplitMode(NULL, NULL);
  WindowManager::GetInstance()->ToggleOverview();
  EXPECT_TRUE(wm_api.GetSplitViewController()->IsSplitViewModeActive());
  EXPECT_EQ(w3.get(), wm_api.GetSplitViewController()->left_window());
  EXPECT_EQ(w2.get(), wm_api.GetSplitViewController()->right_window());

  WindowOverviewModeDelegate* overview_delegate = wm_api.wm();
  overview_delegate->OnSelectWindow(w1.get());
  EXPECT_FALSE(wm_api.GetSplitViewController()->IsSplitViewModeActive());
  EXPECT_TRUE(w1->IsVisible());
  // Make sure the windows that were in split-view mode are hidden.
  EXPECT_FALSE(w2->IsVisible());
  EXPECT_FALSE(w3->IsVisible());
}

}  // namespace athena