Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(353)

Side by Side Diff: chrome/browser/ui/webui/constrained_html_ui_browsertest.cc

Issue 8220026: Add new methods to create constrained windows for print preview and release the internal TabConte... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
21 class ConstrainedHtmlDialogBrowserTestHandler : public WebUIMessageHandler {
22 public:
23 ConstrainedHtmlDialogBrowserTestHandler() {}
24 virtual ~ConstrainedHtmlDialogBrowserTestHandler() {}
25
26 ConstrainedHtmlUI* constrained_ui() {
27 return static_cast<ConstrainedHtmlUI*>(web_ui());
28 }
29 private:
30 virtual void RegisterMessages() {}
31 };
32
27 class TestHtmlDialogUIDelegate : public HtmlDialogUIDelegate { 33 class TestHtmlDialogUIDelegate : public HtmlDialogUIDelegate {
28 public: 34 public:
29 TestHtmlDialogUIDelegate() {} 35 TestHtmlDialogUIDelegate() {
36 test_handler_.reset(new ConstrainedHtmlDialogBrowserTestHandler);
37 }
30 virtual ~TestHtmlDialogUIDelegate() {} 38 virtual ~TestHtmlDialogUIDelegate() {}
31 39
32 // HTMLDialogUIDelegate implementation: 40 // HTMLDialogUIDelegate implementation:
33 virtual bool IsDialogModal() const OVERRIDE { 41 virtual bool IsDialogModal() const OVERRIDE {
34 return true; 42 return true;
35 } 43 }
36 virtual string16 GetDialogTitle() const OVERRIDE { 44 virtual string16 GetDialogTitle() const OVERRIDE {
37 return UTF8ToUTF16("Test"); 45 return UTF8ToUTF16("Test");
38 } 46 }
39 virtual GURL GetDialogContentURL() const OVERRIDE { 47 virtual GURL GetDialogContentURL() const OVERRIDE {
40 return GURL(chrome::kChromeUIConstrainedHTMLTestURL); 48 return GURL(chrome::kChromeUIConstrainedHTMLTestURL);
41 } 49 }
42 virtual void GetWebUIMessageHandlers( 50 virtual void GetWebUIMessageHandlers(
43 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE {} 51 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE {
52 handlers->push_back(test_handler_.get());
53 }
44 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE { 54 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE {
45 size->set_width(400); 55 size->set_width(400);
46 size->set_height(400); 56 size->set_height(400);
47 } 57 }
48 virtual std::string GetDialogArgs() const OVERRIDE { 58 virtual std::string GetDialogArgs() const OVERRIDE {
49 return std::string(); 59 return std::string();
50 } 60 }
51 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { } 61 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { }
52 virtual void OnCloseContents(TabContents* source, bool* out_close_dialog) 62 virtual void OnCloseContents(TabContents* source, bool* out_close_dialog)
53 OVERRIDE { 63 OVERRIDE {
54 if (out_close_dialog) 64 if (out_close_dialog)
55 *out_close_dialog = true; 65 *out_close_dialog = true;
56 } 66 }
57 virtual bool ShouldShowDialogTitle() const OVERRIDE { return true; } 67 virtual bool ShouldShowDialogTitle() const OVERRIDE { return true; }
68
69 ConstrainedHtmlDialogBrowserTestHandler* test_handler() {
70 return test_handler_.get();
71 }
72
73 private:
74 scoped_ptr<ConstrainedHtmlDialogBrowserTestHandler> test_handler_;
75 };
76
77 class ConstrainedHtmlDialogBrowserTestObserver : public TabContentsObserver {
78 public:
79 explicit ConstrainedHtmlDialogBrowserTestObserver(TabContents* contents)
80 : TabContentsObserver(contents),
81 tab_destroyed_(false) {
82 }
83 virtual ~ConstrainedHtmlDialogBrowserTestObserver() {}
84
85 bool tab_destroyed() { return tab_destroyed_; }
86
87 private:
88 virtual void TabContentsDestroyed(TabContents* tab) {
89 tab_destroyed_ = true;
90 }
91
92 bool tab_destroyed_;
58 }; 93 };
59 94
60 } // namespace 95 } // namespace
61 96
62 class ConstrainedHtmlDialogBrowserTest : public InProcessBrowserTest { 97 class ConstrainedHtmlDialogBrowserTest : public InProcessBrowserTest {
63 public: 98 public:
64 ConstrainedHtmlDialogBrowserTest() {} 99 ConstrainedHtmlDialogBrowserTest() {}
65 }; 100 };
66 101
67 // Tests that opening/closing the constrained window won't crash it. 102 // Tests that opening/closing the constrained window won't crash it.
68 IN_PROC_BROWSER_TEST_F(ConstrainedHtmlDialogBrowserTest, BasicTest) { 103 IN_PROC_BROWSER_TEST_F(ConstrainedHtmlDialogBrowserTest, BasicTest) {
69 // The delegate deletes itself. 104 // The delegate deletes itself.
70 HtmlDialogUIDelegate* delegate = new TestHtmlDialogUIDelegate(); 105 HtmlDialogUIDelegate* delegate = new TestHtmlDialogUIDelegate();
71 TabContentsWrapper* wrapper = browser()->GetSelectedTabContentsWrapper(); 106 TabContentsWrapper* wrapper = browser()->GetSelectedTabContentsWrapper();
72 ASSERT_TRUE(wrapper != NULL); 107 ASSERT_TRUE(wrapper);
73 108
74 ConstrainedHtmlUI::CreateConstrainedHtmlDialog(browser()->profile(), 109 ConstrainedWindow* window =
75 delegate, 110 ConstrainedHtmlUI::CreateConstrainedHtmlDialog(browser()->profile(),
76 wrapper); 111 delegate,
112 wrapper);
113 EXPECT_TRUE(window);
114 EXPECT_EQ(1U, wrapper->constrained_window_tab_helper()->
115 constrained_window_count());
116 }
77 117
118 // Tests that ReleaseTabContentsOnDialogClose() works.
119 IN_PROC_BROWSER_TEST_F(ConstrainedHtmlDialogBrowserTest,
120 ReleaseTabContentsOnDialogClose) {
121 // The delegate deletes itself.
122 TestHtmlDialogUIDelegate* delegate = new TestHtmlDialogUIDelegate();
123 TabContentsWrapper* wrapper = browser()->GetSelectedTabContentsWrapper();
124 ASSERT_TRUE(wrapper);
125
126 TabContentsWrapper* new_tab =
127 ConstrainedHtmlUI::CreateConstrainedPrintPreviewHtmlUI(
128 browser()->profile(),
129 delegate,
130 wrapper);
131 ASSERT_TRUE(new_tab);
78 ASSERT_EQ(1U, wrapper->constrained_window_tab_helper()-> 132 ASSERT_EQ(1U, wrapper->constrained_window_tab_helper()->
79 constrained_window_count()); 133 constrained_window_count());
134
135 ConstrainedHtmlDialogBrowserTestObserver observer(new_tab->tab_contents());
136 ConstrainedHtmlDialogBrowserTestHandler* handler = delegate->test_handler();
137 ConstrainedHtmlUI* constrained_ui = handler->constrained_ui();
138 ASSERT_TRUE(constrained_ui);
139 ConstrainedHtmlUIDelegate* constrained_delegate =
140 constrained_ui->GetConstrainedDelegate();
141 ASSERT_TRUE(constrained_delegate);
142 constrained_delegate->ReleaseTabContentsOnDialogClose();
Evan Stade 2011/10/11 02:49:17 so doesn't this leak?
Lei Zhang 2011/10/11 03:15:58 In this test, yes we leak |new_tab|. In its intend
Evan Stade 2011/10/11 22:54:59 i see that you have plugged this leak
143 constrained_delegate->OnDialogCloseFromWebUI();
144
145 EXPECT_FALSE(observer.tab_destroyed());
146 EXPECT_EQ(0U, wrapper->constrained_window_tab_helper()->
147 constrained_window_count());
Evan Stade 2011/10/11 02:49:17 4 more indent
Lei Zhang 2011/10/11 03:15:58 This is just copy + pasted. I'll change the other
80 } 148 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698