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 "base/strings/utf_string_conversions.h"
6 #include "chrome/browser/profiles/profile.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/tabs/tab_strip_model.h"
9 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
10 #include "chrome/common/url_constants.h"
11 #include "chrome/test/base/in_process_browser_test.h"
12 #include "chrome/test/base/ui_test_utils.h"
13 #include "components/web_modal/web_contents_modal_dialog_manager.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "ui/web_dialogs/test/test_web_dialog_delegate.h"
18 using content::WebContents
;
19 using ui::WebDialogDelegate
;
20 using web_modal::WebContentsModalDialogManager
;
24 class ConstrainedWebDialogBrowserTestObserver
25 : public content::WebContentsObserver
{
27 explicit ConstrainedWebDialogBrowserTestObserver(WebContents
* contents
)
28 : content::WebContentsObserver(contents
),
29 contents_destroyed_(false) {
31 virtual ~ConstrainedWebDialogBrowserTestObserver() {}
33 bool contents_destroyed() { return contents_destroyed_
; }
36 virtual void WebContentsDestroyed(WebContents
* tab
) OVERRIDE
{
37 contents_destroyed_
= true;
40 bool contents_destroyed_
;
45 class ConstrainedWebDialogBrowserTest
: public InProcessBrowserTest
{
47 ConstrainedWebDialogBrowserTest() {}
50 bool IsShowingWebContentsModalDialog(WebContents
* web_contents
) const {
51 WebContentsModalDialogManager
* web_contents_modal_dialog_manager
=
52 WebContentsModalDialogManager::FromWebContents(web_contents
);
53 return web_contents_modal_dialog_manager
->IsDialogActive();
57 // Tests that opening/closing the constrained window won't crash it.
58 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest
, BasicTest
) {
59 // The delegate deletes itself.
60 WebDialogDelegate
* delegate
= new ui::test::TestWebDialogDelegate(
61 GURL(chrome::kChromeUIConstrainedHTMLTestURL
));
62 WebContents
* web_contents
=
63 browser()->tab_strip_model()->GetActiveWebContents();
64 ASSERT_TRUE(web_contents
);
66 ConstrainedWebDialogDelegate
* dialog_delegate
=
67 CreateConstrainedWebDialog(browser()->profile(),
71 ASSERT_TRUE(dialog_delegate
);
72 EXPECT_TRUE(dialog_delegate
->GetNativeDialog());
73 EXPECT_TRUE(IsShowingWebContentsModalDialog(web_contents
));
76 // Tests that ReleaseWebContentsOnDialogClose() works.
77 IN_PROC_BROWSER_TEST_F(ConstrainedWebDialogBrowserTest
,
78 ReleaseWebContentsOnDialogClose
) {
79 // The delegate deletes itself.
80 WebDialogDelegate
* delegate
= new ui::test::TestWebDialogDelegate(
81 GURL(chrome::kChromeUIConstrainedHTMLTestURL
));
82 WebContents
* web_contents
=
83 browser()->tab_strip_model()->GetActiveWebContents();
84 ASSERT_TRUE(web_contents
);
86 ConstrainedWebDialogDelegate
* dialog_delegate
=
87 CreateConstrainedWebDialog(browser()->profile(),
91 ASSERT_TRUE(dialog_delegate
);
92 scoped_ptr
<WebContents
> new_tab(dialog_delegate
->GetWebContents());
93 ASSERT_TRUE(new_tab
.get());
94 ASSERT_TRUE(IsShowingWebContentsModalDialog(web_contents
));
96 ConstrainedWebDialogBrowserTestObserver
observer(new_tab
.get());
97 dialog_delegate
->ReleaseWebContentsOnDialogClose();
98 dialog_delegate
->OnDialogCloseFromWebUI();
100 ASSERT_FALSE(observer
.contents_destroyed());
101 EXPECT_FALSE(IsShowingWebContentsModalDialog(web_contents
));
103 EXPECT_TRUE(observer
.contents_destroyed());