Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / constrained_web_dialog_ui_browsertest.cc
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.
4
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"
17
18 using content::WebContents;
19 using ui::WebDialogDelegate;
20 using web_modal::WebContentsModalDialogManager;
21
22 namespace {
23
24 class ConstrainedWebDialogBrowserTestObserver
25     : public content::WebContentsObserver {
26  public:
27   explicit ConstrainedWebDialogBrowserTestObserver(WebContents* contents)
28       : content::WebContentsObserver(contents),
29         contents_destroyed_(false) {
30   }
31   virtual ~ConstrainedWebDialogBrowserTestObserver() {}
32
33   bool contents_destroyed() { return contents_destroyed_; }
34
35  private:
36   virtual void WebContentsDestroyed() OVERRIDE {
37     contents_destroyed_ = true;
38   }
39
40   bool contents_destroyed_;
41 };
42
43 }  // namespace
44
45 class ConstrainedWebDialogBrowserTest : public InProcessBrowserTest {
46  public:
47   ConstrainedWebDialogBrowserTest() {}
48
49  protected:
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();
54   }
55 };
56
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);
65
66   ConstrainedWebDialogDelegate* dialog_delegate =
67       CreateConstrainedWebDialog(browser()->profile(),
68                                  delegate,
69                                  NULL,
70                                  web_contents);
71   ASSERT_TRUE(dialog_delegate);
72   EXPECT_TRUE(dialog_delegate->GetNativeDialog());
73   EXPECT_TRUE(IsShowingWebContentsModalDialog(web_contents));
74 }
75
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);
85
86   ConstrainedWebDialogDelegate* dialog_delegate =
87       CreateConstrainedWebDialog(browser()->profile(),
88                                  delegate,
89                                  NULL,
90                                  web_contents);
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));
95
96   ConstrainedWebDialogBrowserTestObserver observer(new_tab.get());
97   dialog_delegate->ReleaseWebContentsOnDialogClose();
98   dialog_delegate->OnDialogCloseFromWebUI();
99
100   ASSERT_FALSE(observer.contents_destroyed());
101   EXPECT_FALSE(IsShowingWebContentsModalDialog(web_contents));
102   new_tab.reset();
103   EXPECT_TRUE(observer.contents_destroyed());
104 }