OLD | NEW |
| (Empty) |
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 "chrome/test/ui/ui_test.h" | |
6 | |
7 #include "base/utf_string_conversions.h" | |
8 #include "chrome/browser/ui/browser.h" | |
9 #include "chrome/browser/ui/constrained_window_tab_helper.h" | |
10 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
11 #include "chrome/browser/ui/webui/constrained_html_ui.h" | |
12 #include "chrome/browser/ui/webui/test_html_dialog_ui_delegate.h" | |
13 #include "chrome/common/url_constants.h" | |
14 #include "chrome/test/base/in_process_browser_test.h" | |
15 #include "chrome/test/base/ui_test_utils.h" | |
16 #include "content/public/browser/web_contents.h" | |
17 #include "content/public/browser/web_contents_observer.h" | |
18 | |
19 using content::WebContents; | |
20 | |
21 namespace { | |
22 | |
23 class ConstrainedHtmlDialogBrowserTestObserver | |
24 : public content::WebContentsObserver { | |
25 public: | |
26 explicit ConstrainedHtmlDialogBrowserTestObserver(WebContents* contents) | |
27 : content::WebContentsObserver(contents), | |
28 tab_destroyed_(false) { | |
29 } | |
30 virtual ~ConstrainedHtmlDialogBrowserTestObserver() {} | |
31 | |
32 bool tab_destroyed() { return tab_destroyed_; } | |
33 | |
34 private: | |
35 virtual void WebContentsDestroyed(WebContents* tab) OVERRIDE { | |
36 tab_destroyed_ = true; | |
37 } | |
38 | |
39 bool tab_destroyed_; | |
40 }; | |
41 | |
42 } // namespace | |
43 | |
44 class ConstrainedHtmlDialogBrowserTest : public InProcessBrowserTest { | |
45 public: | |
46 ConstrainedHtmlDialogBrowserTest() {} | |
47 | |
48 protected: | |
49 size_t GetConstrainedWindowCount(TabContentsWrapper* wrapper) const { | |
50 return wrapper->constrained_window_tab_helper()->constrained_window_count(); | |
51 } | |
52 }; | |
53 | |
54 // Tests that opening/closing the constrained window won't crash it. | |
55 IN_PROC_BROWSER_TEST_F(ConstrainedHtmlDialogBrowserTest, BasicTest) { | |
56 // The delegate deletes itself. | |
57 HtmlDialogUIDelegate* delegate = new test::TestHtmlDialogUIDelegate( | |
58 GURL(chrome::kChromeUIConstrainedHTMLTestURL)); | |
59 TabContentsWrapper* wrapper = browser()->GetSelectedTabContentsWrapper(); | |
60 ASSERT_TRUE(wrapper); | |
61 | |
62 ConstrainedHtmlUIDelegate* html_ui_delegate = | |
63 ConstrainedHtmlUI::CreateConstrainedHtmlDialog(browser()->profile(), | |
64 delegate, | |
65 NULL, | |
66 wrapper); | |
67 ASSERT_TRUE(html_ui_delegate); | |
68 EXPECT_TRUE(html_ui_delegate->window()); | |
69 EXPECT_EQ(1U, GetConstrainedWindowCount(wrapper)); | |
70 } | |
71 | |
72 // Tests that ReleaseTabContentsOnDialogClose() works. | |
73 IN_PROC_BROWSER_TEST_F(ConstrainedHtmlDialogBrowserTest, | |
74 ReleaseTabContentsOnDialogClose) { | |
75 // The delegate deletes itself. | |
76 HtmlDialogUIDelegate* delegate = new test::TestHtmlDialogUIDelegate( | |
77 GURL(chrome::kChromeUIConstrainedHTMLTestURL)); | |
78 TabContentsWrapper* wrapper = browser()->GetSelectedTabContentsWrapper(); | |
79 ASSERT_TRUE(wrapper); | |
80 | |
81 ConstrainedHtmlUIDelegate* html_ui_delegate = | |
82 ConstrainedHtmlUI::CreateConstrainedHtmlDialog(browser()->profile(), | |
83 delegate, | |
84 NULL, | |
85 wrapper); | |
86 ASSERT_TRUE(html_ui_delegate); | |
87 scoped_ptr<TabContentsWrapper> new_tab(html_ui_delegate->tab()); | |
88 ASSERT_TRUE(new_tab.get()); | |
89 ASSERT_EQ(1U, GetConstrainedWindowCount(wrapper)); | |
90 | |
91 ConstrainedHtmlDialogBrowserTestObserver observer(new_tab->web_contents()); | |
92 html_ui_delegate->ReleaseTabContentsOnDialogClose(); | |
93 html_ui_delegate->OnDialogCloseFromWebUI(); | |
94 | |
95 ASSERT_FALSE(observer.tab_destroyed()); | |
96 EXPECT_EQ(0U, GetConstrainedWindowCount(wrapper)); | |
97 new_tab.reset(); | |
98 EXPECT_TRUE(observer.tab_destroyed()); | |
99 } | |
OLD | NEW |