summaryrefslogtreecommitdiffstats
path: root/ash/wm/window_modality_controller_unittest.cc
blob: 5ec300543bda13ec6fee702cbe150d04e2e7d920 (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
// 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 "ash/wm/window_modality_controller.h"

#include "ash/test/ash_test_base.h"
#include "ash/wm/window_util.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/test/event_generator.h"
#include "ui/aura/test/test_windows.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/window.h"
#include "ui/base/ui_base_types.h"

namespace ash {
namespace internal {

typedef test::AshTestBase WindowModalityControllerTest;

namespace {

bool ValidateStacking(aura::Window* parent, int ids[], int count) {
  for (int i = 0; i < count; ++i) {
    if (parent->children().at(i)->id() != ids[i])
      return false;
  }
  return true;
}

}

// Creates three windows, w1, w11, and w12. w11 is a non-modal transient, w12 is
// a modal transient.
// Validates:
// - it should be possible to activate w12 even when w11 is open.
// - activating w1 activates w12 and updates stacking order appropriately.
// - closing a window passes focus up the stack.
TEST_F(WindowModalityControllerTest, BasicActivation) {
  aura::test::TestWindowDelegate d;
  scoped_ptr<aura::Window> w1(
      aura::test::CreateTestWindowWithDelegate(&d, -1, gfx::Rect(), NULL));
  scoped_ptr<aura::Window> w11(
      aura::test::CreateTestWindowWithDelegate(&d, -11, gfx::Rect(), NULL));
  scoped_ptr<aura::Window> w12(
      aura::test::CreateTestWindowWithDelegate(&d, -12, gfx::Rect(), NULL));

  w1->AddTransientChild(w11.get());
  ActivateWindow(w1.get());
  EXPECT_TRUE(IsActiveWindow(w1.get()));
  ActivateWindow(w11.get());
  EXPECT_TRUE(IsActiveWindow(w11.get()));

  w12->SetProperty(aura::client::kModalKey, ui::MODAL_TYPE_WINDOW);
  w1->AddTransientChild(w12.get());
  ActivateWindow(w12.get());
  EXPECT_TRUE(IsActiveWindow(w12.get()));

  ActivateWindow(w11.get());
  EXPECT_TRUE(IsActiveWindow(w11.get()));

  int check1[] = { -1, -12, -11 };
  EXPECT_TRUE(ValidateStacking(w1->parent(), check1, arraysize(check1)));

  ActivateWindow(w1.get());
  EXPECT_TRUE(IsActiveWindow(w12.get()));
  // Transient children are always stacked above their transient parent, which
  // is why this order is not -11, -1, -12.
  int check2[] = { -1, -11, -12 };
  EXPECT_TRUE(ValidateStacking(w1->parent(), check2, arraysize(check2)));

  w12.reset();
  EXPECT_TRUE(IsActiveWindow(w11.get()));
  w11.reset();
  EXPECT_TRUE(IsActiveWindow(w1.get()));
}

// Create two toplevel windows w1 and w2, and nest two modals w11 and w111 below
// w1.
// Validates:
// - activating w1 while w11/w111 is showing always activates most deeply nested
//   descendant.
// - closing a window passes focus up the stack.
TEST_F(WindowModalityControllerTest, NestedModals) {
  aura::test::TestWindowDelegate d;
  scoped_ptr<aura::Window> w1(
      aura::test::CreateTestWindowWithDelegate(&d, -1, gfx::Rect(), NULL));
  scoped_ptr<aura::Window> w11(
      aura::test::CreateTestWindowWithDelegate(&d, -11, gfx::Rect(), NULL));
  scoped_ptr<aura::Window> w111(
      aura::test::CreateTestWindowWithDelegate(&d, -111, gfx::Rect(), NULL));
  scoped_ptr<aura::Window> w2(
      aura::test::CreateTestWindowWithDelegate(&d, -2, gfx::Rect(), NULL));

  w1->AddTransientChild(w11.get());
  w11->AddTransientChild(w111.get());

  ActivateWindow(w1.get());
  EXPECT_TRUE(IsActiveWindow(w1.get()));
  ActivateWindow(w2.get());
  EXPECT_TRUE(IsActiveWindow(w2.get()));

  // Set up modality.
  w11->SetProperty(aura::client::kModalKey, ui::MODAL_TYPE_WINDOW);
  w111->SetProperty(aura::client::kModalKey, ui::MODAL_TYPE_WINDOW);

  ActivateWindow(w1.get());
  EXPECT_TRUE(IsActiveWindow(w111.get()));
  int check1[] = { -2, -1, -11, -111 };
  EXPECT_TRUE(ValidateStacking(w1->parent(), check1, arraysize(check1)));

  ActivateWindow(w11.get());
  EXPECT_TRUE(IsActiveWindow(w111.get()));
  EXPECT_TRUE(ValidateStacking(w1->parent(), check1, arraysize(check1)));

  ActivateWindow(w111.get());
  EXPECT_TRUE(IsActiveWindow(w111.get()));
  EXPECT_TRUE(ValidateStacking(w1->parent(), check1, arraysize(check1)));

  ActivateWindow(w2.get());
  EXPECT_TRUE(IsActiveWindow(w2.get()));
  int check2[] = { -1, -11, -111, -2 };
  EXPECT_TRUE(ValidateStacking(w1->parent(), check2, arraysize(check2)));

  w2.reset();
  EXPECT_TRUE(IsActiveWindow(w111.get()));
  w111.reset();
  EXPECT_TRUE(IsActiveWindow(w11.get()));
  w11.reset();
  EXPECT_TRUE(IsActiveWindow(w1.get()));
}

// Create two toplevel windows w1 and w2, and nest two modals w11 and w111 below
// w1.
// Validates:
// - destroying w11 while w111 is focused activates w1.
TEST_F(WindowModalityControllerTest, NestedModalsOuterClosed) {
  aura::test::TestWindowDelegate d;
  scoped_ptr<aura::Window> w1(
      aura::test::CreateTestWindowWithDelegate(&d, -1, gfx::Rect(), NULL));
  scoped_ptr<aura::Window> w11(
      aura::test::CreateTestWindowWithDelegate(&d, -11, gfx::Rect(), NULL));
  // |w111| will be owned and deleted by |w11|.
  aura::Window* w111 =
      aura::test::CreateTestWindowWithDelegate(&d, -111, gfx::Rect(), NULL);
  scoped_ptr<aura::Window> w2(
      aura::test::CreateTestWindowWithDelegate(&d, -2, gfx::Rect(), NULL));

  w1->AddTransientChild(w11.get());
  w11->AddTransientChild(w111);

  ActivateWindow(w1.get());
  EXPECT_TRUE(IsActiveWindow(w1.get()));
  ActivateWindow(w2.get());
  EXPECT_TRUE(IsActiveWindow(w2.get()));

  // Set up modality.
  w11->SetProperty(aura::client::kModalKey, ui::MODAL_TYPE_WINDOW);
  w111->SetProperty(aura::client::kModalKey, ui::MODAL_TYPE_WINDOW);

  ActivateWindow(w1.get());
  EXPECT_TRUE(IsActiveWindow(w111));

  w111->Hide();
  EXPECT_TRUE(IsActiveWindow(w11.get()));

  // TODO(oshima): Re-showing doesn't set the focus back to
  // modal window. There is no such use case right now, but it
  // probably should.

  w11.reset();
  EXPECT_TRUE(IsActiveWindow(w1.get()));
}

// Modality also prevents events from being passed to the transient parent.
TEST_F(WindowModalityControllerTest, Events) {
  aura::test::TestWindowDelegate d;
  scoped_ptr<aura::Window> w1(aura::test::CreateTestWindowWithDelegate(&d, -1,
      gfx::Rect(0, 0, 100, 100), NULL));
  scoped_ptr<aura::Window> w11(aura::test::CreateTestWindowWithDelegate(&d, -11,
      gfx::Rect(20, 20, 50, 50), NULL));

  w1->AddTransientChild(w11.get());

  {
    // Clicking a point within w1 should activate that window.
    aura::test::EventGenerator generator(gfx::Point(10, 10));
    generator.ClickLeftButton();
    EXPECT_TRUE(IsActiveWindow(w1.get()));
  }

  w11->SetProperty(aura::client::kModalKey, ui::MODAL_TYPE_WINDOW);

  {
    // Clicking a point within w1 should activate w11.
    aura::test::EventGenerator generator(gfx::Point(10, 10));
    generator.ClickLeftButton();
    EXPECT_TRUE(IsActiveWindow(w11.get()));
  }
}


}  // namespace internal
}  // namespace ash