Remove NativeWebContentsModalDialog and NativePopup typedefs.
[chromium-blink-merge.git] / components / web_modal / web_contents_modal_dialog_manager_unittest.cc
blobea07abf9baff7946bc36fd9d029315d076d5532c
1 // Copyright (c) 2012 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.
5 #include "components/web_modal/web_contents_modal_dialog_manager.h"
7 #include <map>
9 #include "base/memory/scoped_ptr.h"
10 #include "components/web_modal/single_web_contents_dialog_manager.h"
11 #include "components/web_modal/test_web_contents_modal_dialog_manager_delegate.h"
12 #include "content/public/test/test_renderer_host.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace web_modal {
17 // Tracks persistent state changes of the native WC-modal dialog manager.
18 class NativeManagerTracker {
19 public:
20 enum DialogState {
21 UNKNOWN,
22 NOT_SHOWN,
23 SHOWN,
24 HIDDEN,
25 CLOSED
28 NativeManagerTracker() : state_(UNKNOWN), was_shown_(false) {}
30 void SetState(DialogState state) {
31 state_ = state;
32 if (state_ == SHOWN)
33 was_shown_ = true;
36 DialogState state_;
37 bool was_shown_;
40 NativeManagerTracker unused_tracker;
42 class TestNativeWebContentsModalDialogManager
43 : public SingleWebContentsDialogManager {
44 public:
45 TestNativeWebContentsModalDialogManager(
46 gfx::NativeWindow dialog,
47 SingleWebContentsDialogManagerDelegate* delegate,
48 NativeManagerTracker* tracker)
49 : delegate_(delegate),
50 dialog_(dialog),
51 tracker_(tracker) {
52 if (tracker_)
53 tracker_->SetState(NativeManagerTracker::NOT_SHOWN);
56 void Show() override {
57 if (tracker_)
58 tracker_->SetState(NativeManagerTracker::SHOWN);
60 void Hide() override {
61 if (tracker_)
62 tracker_->SetState(NativeManagerTracker::HIDDEN);
64 void Close() override {
65 if (tracker_)
66 tracker_->SetState(NativeManagerTracker::CLOSED);
67 delegate_->WillClose(dialog_);
69 void Focus() override {}
70 void Pulse() override {}
71 void HostChanged(WebContentsModalDialogHost* new_host) override {}
72 gfx::NativeWindow dialog() override { return dialog_; }
74 void StopTracking() {
75 tracker_ = NULL;
78 private:
79 SingleWebContentsDialogManagerDelegate* delegate_;
80 gfx::NativeWindow dialog_;
81 NativeManagerTracker* tracker_;
83 DISALLOW_COPY_AND_ASSIGN(TestNativeWebContentsModalDialogManager);
86 class WebContentsModalDialogManagerTest
87 : public content::RenderViewHostTestHarness {
88 public:
89 WebContentsModalDialogManagerTest()
90 : next_dialog_id(1),
91 manager(NULL) {
94 void SetUp() override {
95 content::RenderViewHostTestHarness::SetUp();
97 delegate.reset(new TestWebContentsModalDialogManagerDelegate);
98 WebContentsModalDialogManager::CreateForWebContents(web_contents());
99 manager = WebContentsModalDialogManager::FromWebContents(web_contents());
100 manager->SetDelegate(delegate.get());
101 test_api.reset(new WebContentsModalDialogManager::TestApi(manager));
104 void TearDown() override {
105 test_api.reset();
106 content::RenderViewHostTestHarness::TearDown();
109 protected:
110 gfx::NativeWindow MakeFakeDialog() {
111 // WebContentsModalDialogManager treats the dialog window as an opaque
112 // type, so creating fake dialog windows using reinterpret_cast is valid.
113 return reinterpret_cast<gfx::NativeWindow>(next_dialog_id++);
116 int next_dialog_id;
117 scoped_ptr<TestWebContentsModalDialogManagerDelegate> delegate;
118 WebContentsModalDialogManager* manager;
119 scoped_ptr<WebContentsModalDialogManager::TestApi> test_api;
121 DISALLOW_COPY_AND_ASSIGN(WebContentsModalDialogManagerTest);
124 SingleWebContentsDialogManager*
125 WebContentsModalDialogManager::CreateNativeWebModalManager(
126 gfx::NativeWindow dialog,
127 SingleWebContentsDialogManagerDelegate* native_delegate) {
128 NOTREACHED();
129 return new TestNativeWebContentsModalDialogManager(
130 dialog,
131 native_delegate,
132 &unused_tracker);
135 // Test that the dialog is shown immediately when the delegate indicates the web
136 // contents is visible.
137 TEST_F(WebContentsModalDialogManagerTest, WebContentsVisible) {
138 // Dialog should be shown while WebContents is visible.
139 const gfx::NativeWindow dialog = MakeFakeDialog();
141 NativeManagerTracker tracker;
142 TestNativeWebContentsModalDialogManager* native_manager =
143 new TestNativeWebContentsModalDialogManager(dialog, manager, &tracker);
144 manager->ShowDialogWithManager(dialog,
145 scoped_ptr<SingleWebContentsDialogManager>(native_manager).Pass());
147 EXPECT_EQ(NativeManagerTracker::SHOWN, tracker.state_);
148 EXPECT_TRUE(manager->IsDialogActive());
149 EXPECT_TRUE(delegate->web_contents_blocked());
150 EXPECT_TRUE(tracker.was_shown_);
152 native_manager->StopTracking();
155 // Test that the dialog is not shown immediately when the delegate indicates the
156 // web contents is not visible.
157 TEST_F(WebContentsModalDialogManagerTest, WebContentsNotVisible) {
158 // Dialog should not be shown while WebContents is not visible.
159 delegate->set_web_contents_visible(false);
161 const gfx::NativeWindow dialog = MakeFakeDialog();
163 NativeManagerTracker tracker;
164 TestNativeWebContentsModalDialogManager* native_manager =
165 new TestNativeWebContentsModalDialogManager(dialog, manager, &tracker);
166 manager->ShowDialogWithManager(dialog,
167 scoped_ptr<SingleWebContentsDialogManager>(native_manager).Pass());
169 EXPECT_EQ(NativeManagerTracker::NOT_SHOWN, tracker.state_);
170 EXPECT_TRUE(manager->IsDialogActive());
171 EXPECT_TRUE(delegate->web_contents_blocked());
172 EXPECT_FALSE(tracker.was_shown_);
174 native_manager->StopTracking();
177 // Test that only the first of multiple dialogs is shown.
178 TEST_F(WebContentsModalDialogManagerTest, ShowDialogs) {
179 const gfx::NativeWindow dialog1 = MakeFakeDialog();
180 const gfx::NativeWindow dialog2 = MakeFakeDialog();
181 const gfx::NativeWindow dialog3 = MakeFakeDialog();
183 NativeManagerTracker tracker1;
184 NativeManagerTracker tracker2;
185 NativeManagerTracker tracker3;
186 TestNativeWebContentsModalDialogManager* native_manager1 =
187 new TestNativeWebContentsModalDialogManager(dialog1, manager, &tracker1);
188 TestNativeWebContentsModalDialogManager* native_manager2 =
189 new TestNativeWebContentsModalDialogManager(dialog2, manager, &tracker2);
190 TestNativeWebContentsModalDialogManager* native_manager3 =
191 new TestNativeWebContentsModalDialogManager(dialog3, manager, &tracker3);
192 manager->ShowDialogWithManager(dialog1,
193 scoped_ptr<SingleWebContentsDialogManager>(native_manager1).Pass());
194 manager->ShowDialogWithManager(dialog2,
195 scoped_ptr<SingleWebContentsDialogManager>(native_manager2).Pass());
196 manager->ShowDialogWithManager(dialog3,
197 scoped_ptr<SingleWebContentsDialogManager>(native_manager3).Pass());
199 EXPECT_TRUE(delegate->web_contents_blocked());
200 EXPECT_EQ(NativeManagerTracker::SHOWN, tracker1.state_);
201 EXPECT_EQ(NativeManagerTracker::NOT_SHOWN, tracker2.state_);
202 EXPECT_EQ(NativeManagerTracker::NOT_SHOWN, tracker3.state_);
204 native_manager1->StopTracking();
205 native_manager2->StopTracking();
206 native_manager3->StopTracking();
209 // Test that the dialog is shown/hidden when the WebContents is shown/hidden.
210 TEST_F(WebContentsModalDialogManagerTest, VisibilityObservation) {
211 const gfx::NativeWindow dialog = MakeFakeDialog();
213 NativeManagerTracker tracker;
214 TestNativeWebContentsModalDialogManager* native_manager =
215 new TestNativeWebContentsModalDialogManager(dialog, manager, &tracker);
216 manager->ShowDialogWithManager(dialog,
217 scoped_ptr<SingleWebContentsDialogManager>(native_manager).Pass());
219 EXPECT_TRUE(manager->IsDialogActive());
220 EXPECT_TRUE(delegate->web_contents_blocked());
221 EXPECT_EQ(NativeManagerTracker::SHOWN, tracker.state_);
223 test_api->WebContentsWasHidden();
225 EXPECT_TRUE(manager->IsDialogActive());
226 EXPECT_TRUE(delegate->web_contents_blocked());
227 EXPECT_EQ(NativeManagerTracker::HIDDEN, tracker.state_);
229 test_api->WebContentsWasShown();
231 EXPECT_TRUE(manager->IsDialogActive());
232 EXPECT_TRUE(delegate->web_contents_blocked());
233 EXPECT_EQ(NativeManagerTracker::SHOWN, tracker.state_);
235 native_manager->StopTracking();
238 // Test that attaching an interstitial page closes all dialogs.
239 TEST_F(WebContentsModalDialogManagerTest, InterstitialPage) {
240 const gfx::NativeWindow dialog1 = MakeFakeDialog();
241 const gfx::NativeWindow dialog2 = MakeFakeDialog();
243 NativeManagerTracker tracker1;
244 NativeManagerTracker tracker2;
245 TestNativeWebContentsModalDialogManager* native_manager1 =
246 new TestNativeWebContentsModalDialogManager(dialog1, manager, &tracker1);
247 TestNativeWebContentsModalDialogManager* native_manager2 =
248 new TestNativeWebContentsModalDialogManager(dialog2, manager, &tracker2);
249 manager->ShowDialogWithManager(dialog1,
250 scoped_ptr<SingleWebContentsDialogManager>(native_manager1).Pass());
251 manager->ShowDialogWithManager(dialog2,
252 scoped_ptr<SingleWebContentsDialogManager>(native_manager2).Pass());
254 test_api->DidAttachInterstitialPage();
256 #if defined(USE_AURA)
257 EXPECT_EQ(NativeManagerTracker::CLOSED, tracker1.state_);
258 EXPECT_EQ(NativeManagerTracker::CLOSED, tracker2.state_);
259 #else
260 EXPECT_EQ(NativeManagerTracker::SHOWN, tracker1.state_);
261 EXPECT_EQ(NativeManagerTracker::NOT_SHOWN, tracker2.state_);
262 #endif
264 EXPECT_TRUE(tracker1.was_shown_);
265 EXPECT_FALSE(tracker2.was_shown_);
267 #if !defined(USE_AURA)
268 native_manager1->StopTracking();
269 native_manager2->StopTracking();
270 #endif
274 // Test that the first dialog is always shown, regardless of the order in which
275 // dialogs are closed.
276 TEST_F(WebContentsModalDialogManagerTest, CloseDialogs) {
277 // The front dialog is always shown regardless of dialog close order.
278 const gfx::NativeWindow dialog1 = MakeFakeDialog();
279 const gfx::NativeWindow dialog2 = MakeFakeDialog();
280 const gfx::NativeWindow dialog3 = MakeFakeDialog();
281 const gfx::NativeWindow dialog4 = MakeFakeDialog();
283 NativeManagerTracker tracker1;
284 NativeManagerTracker tracker2;
285 NativeManagerTracker tracker3;
286 NativeManagerTracker tracker4;
287 TestNativeWebContentsModalDialogManager* native_manager1 =
288 new TestNativeWebContentsModalDialogManager(dialog1, manager, &tracker1);
289 TestNativeWebContentsModalDialogManager* native_manager2 =
290 new TestNativeWebContentsModalDialogManager(dialog2, manager, &tracker2);
291 TestNativeWebContentsModalDialogManager* native_manager3 =
292 new TestNativeWebContentsModalDialogManager(dialog3, manager, &tracker3);
293 TestNativeWebContentsModalDialogManager* native_manager4 =
294 new TestNativeWebContentsModalDialogManager(dialog4, manager, &tracker4);
295 manager->ShowDialogWithManager(dialog1,
296 scoped_ptr<SingleWebContentsDialogManager>(native_manager1).Pass());
297 manager->ShowDialogWithManager(dialog2,
298 scoped_ptr<SingleWebContentsDialogManager>(native_manager2).Pass());
299 manager->ShowDialogWithManager(dialog3,
300 scoped_ptr<SingleWebContentsDialogManager>(native_manager3).Pass());
301 manager->ShowDialogWithManager(dialog4,
302 scoped_ptr<SingleWebContentsDialogManager>(native_manager4).Pass());
304 native_manager1->Close();
306 EXPECT_TRUE(manager->IsDialogActive());
307 EXPECT_TRUE(delegate->web_contents_blocked());
308 EXPECT_EQ(NativeManagerTracker::CLOSED, tracker1.state_);
309 EXPECT_EQ(NativeManagerTracker::SHOWN, tracker2.state_);
310 EXPECT_EQ(NativeManagerTracker::NOT_SHOWN, tracker3.state_);
311 EXPECT_EQ(NativeManagerTracker::NOT_SHOWN, tracker4.state_);
313 native_manager3->Close();
315 EXPECT_TRUE(manager->IsDialogActive());
316 EXPECT_TRUE(delegate->web_contents_blocked());
317 EXPECT_EQ(NativeManagerTracker::CLOSED, tracker1.state_);
318 EXPECT_EQ(NativeManagerTracker::SHOWN, tracker2.state_);
319 EXPECT_EQ(NativeManagerTracker::CLOSED, tracker3.state_);
320 EXPECT_EQ(NativeManagerTracker::NOT_SHOWN, tracker4.state_);
321 EXPECT_FALSE(tracker3.was_shown_);
323 native_manager2->Close();
325 EXPECT_TRUE(manager->IsDialogActive());
326 EXPECT_TRUE(delegate->web_contents_blocked());
327 EXPECT_EQ(NativeManagerTracker::CLOSED, tracker1.state_);
328 EXPECT_EQ(NativeManagerTracker::CLOSED, tracker2.state_);
329 EXPECT_EQ(NativeManagerTracker::CLOSED, tracker3.state_);
330 EXPECT_EQ(NativeManagerTracker::SHOWN, tracker4.state_);
331 EXPECT_FALSE(tracker3.was_shown_);
333 native_manager4->Close();
335 EXPECT_FALSE(manager->IsDialogActive());
336 EXPECT_FALSE(delegate->web_contents_blocked());
337 EXPECT_EQ(NativeManagerTracker::CLOSED, tracker1.state_);
338 EXPECT_EQ(NativeManagerTracker::CLOSED, tracker2.state_);
339 EXPECT_EQ(NativeManagerTracker::CLOSED, tracker3.state_);
340 EXPECT_EQ(NativeManagerTracker::CLOSED, tracker4.state_);
341 EXPECT_TRUE(tracker1.was_shown_);
342 EXPECT_TRUE(tracker2.was_shown_);
343 EXPECT_FALSE(tracker3.was_shown_);
344 EXPECT_TRUE(tracker4.was_shown_);
347 // Test that CloseAllDialogs does what it says.
348 TEST_F(WebContentsModalDialogManagerTest, CloseAllDialogs) {
349 const int kWindowCount = 4;
350 NativeManagerTracker trackers[kWindowCount];
351 TestNativeWebContentsModalDialogManager* native_managers[kWindowCount];
352 for (int i = 0; i < kWindowCount; i++) {
353 const gfx::NativeWindow dialog = MakeFakeDialog();
354 native_managers[i] =
355 new TestNativeWebContentsModalDialogManager(
356 dialog, manager, &(trackers[i]));
357 manager->ShowDialogWithManager(dialog,
358 scoped_ptr<SingleWebContentsDialogManager>(
359 native_managers[i]).Pass());
362 for (int i = 0; i < kWindowCount; i++)
363 EXPECT_NE(NativeManagerTracker::CLOSED, trackers[i].state_);
365 test_api->CloseAllDialogs();
367 EXPECT_FALSE(delegate->web_contents_blocked());
368 EXPECT_FALSE(manager->IsDialogActive());
369 for (int i = 0; i < kWindowCount; i++)
370 EXPECT_EQ(NativeManagerTracker::CLOSED, trackers[i].state_);
373 } // namespace web_modal