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 "chrome/test/ui/ui_test.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
12 #include "chrome/common/url_constants.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "components/web_modal/web_contents_modal_dialog_manager.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "ui/web_dialogs/test/test_web_dialog_delegate.h"
20 using content::WebContents
;
21 using ui::WebDialogDelegate
;
22 using web_modal::WebContentsModalDialogManager
;
26 class ConstrainedWebDialogBrowserTestObserver
27 : public content::WebContentsObserver
{
29 explicit ConstrainedWebDialogBrowserTestObserver(WebContents
* contents
)
30 : content::WebContentsObserver(contents
),
31 contents_destroyed_(false) {
33 virtual ~ConstrainedWebDialogBrowserTestObserver() {}
35 bool contents_destroyed() { return contents_destroyed_
; }
38 virtual void WebContentsDestroyed(WebContents
* tab
) OVERRIDE
{
39 contents_destroyed_
= true;
42 bool contents_destroyed_
;
47 class ConstrainedWebDialogBrowserTest
: public InProcessBrowserTest
{
49 ConstrainedWebDialogBrowserTest() {}
52 bool IsShowingWebContentsModalDialog(WebContents
* web_contents
) const {
53 WebContentsModalDialogManager
* web_contents_modal_dialog_manager
=
54 WebContentsModalDialogManager::FromWebContents(web_contents
);
55 return web_contents_modal_dialog_manager
->IsDialogActive();
59 // Tests that opening/closing the constrained window won't crash it.
60 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest
, BasicTest
) {
61 // The delegate deletes itself.
62 WebDialogDelegate
* delegate
= new ui::test::TestWebDialogDelegate(
63 GURL(chrome::kChromeUIConstrainedHTMLTestURL
));
64 WebContents
* web_contents
=
65 browser()->tab_strip_model()->GetActiveWebContents();
66 ASSERT_TRUE(web_contents
);
68 ConstrainedWebDialogDelegate
* dialog_delegate
=
69 CreateConstrainedWebDialog(browser()->profile(),
73 ASSERT_TRUE(dialog_delegate
);
74 EXPECT_TRUE(dialog_delegate
->GetNativeDialog());
75 EXPECT_TRUE(IsShowingWebContentsModalDialog(web_contents
));
78 // Tests that ReleaseWebContentsOnDialogClose() works.
79 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest
,
80 ReleaseWebContentsOnDialogClose
) {
81 // The delegate deletes itself.
82 WebDialogDelegate
* delegate
= new ui::test::TestWebDialogDelegate(
83 GURL(chrome::kChromeUIConstrainedHTMLTestURL
));
84 WebContents
* web_contents
=
85 browser()->tab_strip_model()->GetActiveWebContents();
86 ASSERT_TRUE(web_contents
);
88 ConstrainedWebDialogDelegate
* dialog_delegate
=
89 CreateConstrainedWebDialog(browser()->profile(),
93 ASSERT_TRUE(dialog_delegate
);
94 scoped_ptr
<WebContents
> new_tab(dialog_delegate
->GetWebContents());
95 ASSERT_TRUE(new_tab
.get());
96 ASSERT_TRUE(IsShowingWebContentsModalDialog(web_contents
));
98 ConstrainedWebDialogBrowserTestObserver
observer(new_tab
.get());
99 dialog_delegate
->ReleaseWebContentsOnDialogClose();
100 dialog_delegate
->OnDialogCloseFromWebUI();
102 ASSERT_FALSE(observer
.contents_destroyed());
103 EXPECT_FALSE(IsShowingWebContentsModalDialog(web_contents
));
105 EXPECT_TRUE(observer
.contents_destroyed());