OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/aura_shell/modal_container_layout_manager.h" | |
6 | |
7 #include "base/compiler_specific.h" | |
8 #include "ui/aura/root_window.h" | |
9 #include "ui/aura/test/event_generator.h" | |
10 #include "ui/aura/window.h" | |
11 #include "ui/aura_shell/shell.h" | |
12 #include "ui/aura_shell/shell_window_ids.h" | |
13 #include "ui/aura_shell/test/aura_shell_test_base.h" | |
14 #include "ui/aura_shell/window_util.h" | |
15 #include "ui/views/widget/widget.h" | |
16 #include "ui/views/widget/widget_delegate.h" | |
17 | |
18 namespace aura_shell { | |
19 namespace test { | |
20 | |
21 namespace { | |
22 | |
23 aura::Window* GetModalContainer() { | |
24 return Shell::GetInstance()->GetContainer( | |
25 aura_shell::internal::kShellWindowId_ModalContainer); | |
26 } | |
27 | |
28 aura::Window* GetDefaultContainer() { | |
29 return Shell::GetInstance()->GetContainer( | |
30 aura_shell::internal::kShellWindowId_DefaultContainer); | |
31 } | |
32 | |
33 class TestWindow : public views::WidgetDelegateView { | |
34 public: | |
35 explicit TestWindow(bool modal) : modal_(modal) {} | |
36 virtual ~TestWindow() {} | |
37 | |
38 static aura::Window* OpenTestWindow(aura::Window* parent, bool modal) { | |
39 DCHECK(!modal || (modal && parent)); | |
40 views::Widget* widget = | |
41 views::Widget::CreateWindowWithParent(new TestWindow(modal), parent); | |
42 widget->Show(); | |
43 return widget->GetNativeView(); | |
44 } | |
45 | |
46 // Overridden from views::View: | |
47 virtual gfx::Size GetPreferredSize() OVERRIDE { | |
48 return gfx::Size(50, 50); | |
49 } | |
50 | |
51 // Overridden from views::WidgetDelegate: | |
52 virtual views::View* GetContentsView() OVERRIDE { | |
53 return this; | |
54 } | |
55 virtual bool IsModal() const OVERRIDE { | |
56 return modal_; | |
57 } | |
58 | |
59 private: | |
60 bool modal_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(TestWindow); | |
63 }; | |
64 | |
65 class TransientWindowObserver : public aura::WindowObserver { | |
66 public: | |
67 TransientWindowObserver() : destroyed_(false) {} | |
68 virtual ~TransientWindowObserver() {} | |
69 | |
70 bool destroyed() const { return destroyed_; } | |
71 | |
72 // Overridden from aura::WindowObserver: | |
73 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE { | |
74 destroyed_ = true; | |
75 } | |
76 | |
77 private: | |
78 bool destroyed_; | |
79 | |
80 DISALLOW_COPY_AND_ASSIGN(TransientWindowObserver); | |
81 }; | |
82 | |
83 } // namespace | |
84 | |
85 typedef AuraShellTestBase ModalContainerLayoutManagerTest; | |
86 | |
87 TEST_F(ModalContainerLayoutManagerTest, NonModalTransient) { | |
88 scoped_ptr<aura::Window> parent(TestWindow::OpenTestWindow(NULL, false)); | |
89 aura::Window* transient = TestWindow::OpenTestWindow(parent.get(), false); | |
90 TransientWindowObserver destruction_observer; | |
91 transient->AddObserver(&destruction_observer); | |
92 | |
93 EXPECT_EQ(parent.get(), transient->transient_parent()); | |
94 EXPECT_EQ(GetDefaultContainer(), transient->parent()); | |
95 | |
96 // The transient should be destroyed with its parent. | |
97 parent.reset(); | |
98 EXPECT_TRUE(destruction_observer.destroyed()); | |
99 } | |
100 | |
101 TEST_F(ModalContainerLayoutManagerTest, ModalTransient) { | |
102 scoped_ptr<aura::Window> parent(TestWindow::OpenTestWindow(NULL, false)); | |
103 // parent should be active. | |
104 EXPECT_TRUE(IsActiveWindow(parent.get())); | |
105 | |
106 aura::Window* t1 = TestWindow::OpenTestWindow(parent.get(), true); | |
107 TransientWindowObserver do1; | |
108 t1->AddObserver(&do1); | |
109 | |
110 EXPECT_EQ(parent.get(), t1->transient_parent()); | |
111 EXPECT_EQ(GetModalContainer(), t1->parent()); | |
112 | |
113 // t1 should now be active. | |
114 EXPECT_TRUE(IsActiveWindow(t1)); | |
115 | |
116 // Attempting to click the parent should result in no activation change. | |
117 aura::test::EventGenerator e1(parent.get()); | |
118 e1.ClickLeftButton(); | |
119 EXPECT_TRUE(IsActiveWindow(t1)); | |
120 | |
121 // Now open another modal transient parented to the original modal transient. | |
122 aura::Window* t2 = TestWindow::OpenTestWindow(t1, true); | |
123 TransientWindowObserver do2; | |
124 t2->AddObserver(&do2); | |
125 | |
126 EXPECT_TRUE(IsActiveWindow(t2)); | |
127 | |
128 EXPECT_EQ(t1, t2->transient_parent()); | |
129 EXPECT_EQ(GetModalContainer(), t2->parent()); | |
130 | |
131 // t2 should still be active, even after clicking on t1. | |
132 aura::test::EventGenerator e2(t1); | |
133 e2.ClickLeftButton(); | |
134 EXPECT_TRUE(IsActiveWindow(t2)); | |
135 | |
136 // Both transients should be destroyed with parent. | |
137 parent.reset(); | |
138 EXPECT_TRUE(do1.destroyed()); | |
139 EXPECT_TRUE(do2.destroyed()); | |
140 } | |
141 | |
142 // Tests that we can activate an unrelated window after a modal window is closed | |
143 // for a window. | |
144 TEST_F(ModalContainerLayoutManagerTest, CanActivateAfterEndModalSession) { | |
145 scoped_ptr<aura::Window> unrelated(TestWindow::OpenTestWindow(NULL, false)); | |
146 unrelated->SetBounds(gfx::Rect(100, 100, 50, 50)); | |
147 scoped_ptr<aura::Window> parent(TestWindow::OpenTestWindow(NULL, false)); | |
148 // parent should be active. | |
149 EXPECT_TRUE(IsActiveWindow(parent.get())); | |
150 | |
151 scoped_ptr<aura::Window> transient( | |
152 TestWindow::OpenTestWindow(parent.get(), true)); | |
153 // t1 should now be active. | |
154 EXPECT_TRUE(IsActiveWindow(transient.get())); | |
155 | |
156 // Attempting to click the parent should result in no activation change. | |
157 aura::test::EventGenerator e1(parent.get()); | |
158 e1.ClickLeftButton(); | |
159 EXPECT_TRUE(IsActiveWindow(transient.get())); | |
160 | |
161 // Now close the transient. | |
162 transient.reset(); | |
163 | |
164 // parent should now be active again. | |
165 EXPECT_TRUE(IsActiveWindow(parent.get())); | |
166 | |
167 // Attempting to click unrelated should activate it. | |
168 aura::test::EventGenerator e2(unrelated.get()); | |
169 e2.ClickLeftButton(); | |
170 EXPECT_TRUE(IsActiveWindow(unrelated.get())); | |
171 } | |
172 | |
173 } // namespace test | |
174 } // namespace aura_shell | |
OLD | NEW |