OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/ui/ui_test.h" | 5 #include "chrome/test/ui/ui_test.h" |
6 | 6 |
7 #include "base/file_path.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
11 #include "chrome/browser/ui/constrained_window_tab_helper.h" | 9 #include "chrome/browser/ui/constrained_window_tab_helper.h" |
12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 10 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
13 #include "chrome/browser/ui/webui/constrained_html_ui.h" | 11 #include "chrome/browser/ui/webui/constrained_html_ui.h" |
14 #include "chrome/browser/ui/webui/html_dialog_ui.h" | 12 #include "chrome/browser/ui/webui/html_dialog_ui.h" |
15 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
16 #include "chrome/test/base/in_process_browser_test.h" | 14 #include "chrome/test/base/in_process_browser_test.h" |
17 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
18 #include "content/browser/renderer_host/render_widget_host_view.h" | |
19 #include "content/browser/tab_contents/tab_contents.h" | 16 #include "content/browser/tab_contents/tab_contents.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "content/browser/tab_contents/tab_contents_observer.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 | |
23 using testing::Eq; | |
24 | 18 |
25 namespace { | 19 namespace { |
26 | 20 |
27 class TestHtmlDialogUIDelegate : public HtmlDialogUIDelegate { | 21 class TestHtmlDialogUIDelegate : public HtmlDialogUIDelegate { |
28 public: | 22 public: |
29 TestHtmlDialogUIDelegate() {} | 23 TestHtmlDialogUIDelegate() {} |
30 virtual ~TestHtmlDialogUIDelegate() {} | 24 virtual ~TestHtmlDialogUIDelegate() {} |
31 | 25 |
32 // HTMLDialogUIDelegate implementation: | 26 // HTMLDialogUIDelegate implementation: |
33 virtual bool IsDialogModal() const OVERRIDE { | 27 virtual bool IsDialogModal() const OVERRIDE { |
(...skipping 16 matching lines...) Expand all Loading... |
50 } | 44 } |
51 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { } | 45 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { } |
52 virtual void OnCloseContents(TabContents* source, bool* out_close_dialog) | 46 virtual void OnCloseContents(TabContents* source, bool* out_close_dialog) |
53 OVERRIDE { | 47 OVERRIDE { |
54 if (out_close_dialog) | 48 if (out_close_dialog) |
55 *out_close_dialog = true; | 49 *out_close_dialog = true; |
56 } | 50 } |
57 virtual bool ShouldShowDialogTitle() const OVERRIDE { return true; } | 51 virtual bool ShouldShowDialogTitle() const OVERRIDE { return true; } |
58 }; | 52 }; |
59 | 53 |
| 54 class ConstrainedHtmlDialogBrowserTestObserver : public TabContentsObserver { |
| 55 public: |
| 56 explicit ConstrainedHtmlDialogBrowserTestObserver(TabContents* contents) |
| 57 : TabContentsObserver(contents), |
| 58 tab_destroyed_(false) { |
| 59 } |
| 60 virtual ~ConstrainedHtmlDialogBrowserTestObserver() {} |
| 61 |
| 62 bool tab_destroyed() { return tab_destroyed_; } |
| 63 |
| 64 private: |
| 65 virtual void TabContentsDestroyed(TabContents* tab) { |
| 66 tab_destroyed_ = true; |
| 67 } |
| 68 |
| 69 bool tab_destroyed_; |
| 70 }; |
| 71 |
60 } // namespace | 72 } // namespace |
61 | 73 |
62 class ConstrainedHtmlDialogBrowserTest : public InProcessBrowserTest { | 74 class ConstrainedHtmlDialogBrowserTest : public InProcessBrowserTest { |
63 public: | 75 public: |
64 ConstrainedHtmlDialogBrowserTest() {} | 76 ConstrainedHtmlDialogBrowserTest() {} |
| 77 |
| 78 protected: |
| 79 size_t GetConstrainedWindowCount(TabContentsWrapper* wrapper) const { |
| 80 return wrapper->constrained_window_tab_helper()->constrained_window_count(); |
| 81 } |
65 }; | 82 }; |
66 | 83 |
67 // Tests that opening/closing the constrained window won't crash it. | 84 // Tests that opening/closing the constrained window won't crash it. |
68 IN_PROC_BROWSER_TEST_F(ConstrainedHtmlDialogBrowserTest, BasicTest) { | 85 IN_PROC_BROWSER_TEST_F(ConstrainedHtmlDialogBrowserTest, BasicTest) { |
69 // The delegate deletes itself. | 86 // The delegate deletes itself. |
70 HtmlDialogUIDelegate* delegate = new TestHtmlDialogUIDelegate(); | 87 HtmlDialogUIDelegate* delegate = new TestHtmlDialogUIDelegate(); |
71 TabContentsWrapper* wrapper = browser()->GetSelectedTabContentsWrapper(); | 88 TabContentsWrapper* wrapper = browser()->GetSelectedTabContentsWrapper(); |
72 ASSERT_TRUE(wrapper != NULL); | 89 ASSERT_TRUE(wrapper); |
73 | 90 |
74 ConstrainedHtmlUI::CreateConstrainedHtmlDialog(browser()->profile(), | 91 ConstrainedHtmlUIDelegate* html_ui_delegate = |
75 delegate, | 92 ConstrainedHtmlUI::CreateConstrainedHtmlDialog(browser()->profile(), |
76 wrapper); | 93 delegate, |
| 94 wrapper); |
| 95 ASSERT_TRUE(html_ui_delegate); |
| 96 EXPECT_TRUE(html_ui_delegate->window()); |
| 97 EXPECT_EQ(1U, GetConstrainedWindowCount(wrapper)); |
| 98 } |
77 | 99 |
78 ASSERT_EQ(1U, wrapper->constrained_window_tab_helper()-> | 100 // Tests that ReleaseTabContentsOnDialogClose() works. |
79 constrained_window_count()); | 101 IN_PROC_BROWSER_TEST_F(ConstrainedHtmlDialogBrowserTest, |
| 102 ReleaseTabContentsOnDialogClose) { |
| 103 // The delegate deletes itself. |
| 104 TestHtmlDialogUIDelegate* delegate = new TestHtmlDialogUIDelegate(); |
| 105 TabContentsWrapper* wrapper = browser()->GetSelectedTabContentsWrapper(); |
| 106 ASSERT_TRUE(wrapper); |
| 107 |
| 108 ConstrainedHtmlUIDelegate* html_ui_delegate = |
| 109 ConstrainedHtmlUI::CreateConstrainedHtmlDialog(browser()->profile(), |
| 110 delegate, |
| 111 wrapper); |
| 112 ASSERT_TRUE(html_ui_delegate); |
| 113 scoped_ptr<TabContentsWrapper> new_tab(html_ui_delegate->tab()); |
| 114 ASSERT_TRUE(new_tab.get()); |
| 115 ASSERT_EQ(1U, GetConstrainedWindowCount(wrapper)); |
| 116 |
| 117 ConstrainedHtmlDialogBrowserTestObserver observer(new_tab->tab_contents()); |
| 118 html_ui_delegate->ReleaseTabContentsOnDialogClose(); |
| 119 html_ui_delegate->OnDialogCloseFromWebUI(); |
| 120 |
| 121 ASSERT_FALSE(observer.tab_destroyed()); |
| 122 EXPECT_EQ(0U, GetConstrainedWindowCount(wrapper)); |
| 123 new_tab.reset(); |
| 124 EXPECT_TRUE(observer.tab_destroyed()); |
80 } | 125 } |
OLD | NEW |