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

Side by Side Diff: chrome/browser/printing/print_preview_dialog_controller_browsertest.cc

Issue 18422004: Print Preview: Correctly rewrite PrintPreviewDialogControllerBrowserTest. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: rebase? Created 7 years, 5 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/run_loop.h"
6 #include "chrome/browser/printing/print_preview_dialog_controller.h" 8 #include "chrome/browser/printing/print_preview_dialog_controller.h"
7 #include "chrome/browser/printing/print_view_manager.h"
8 #include "chrome/browser/ui/browser.h" 9 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/browser_commands.h" 10 #include "chrome/browser/ui/browser_commands.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h" 11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/common/chrome_switches.h" 12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/print_messages.h"
12 #include "chrome/common/url_constants.h" 14 #include "chrome/common/url_constants.h"
13 #include "chrome/test/base/in_process_browser_test.h" 15 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/ui_test_utils.h" 16 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/browser/notification_service.h" 17 #include "content/public/browser/notification_service.h"
16 #include "content/public/browser/notification_types.h" 18 #include "content/public/browser/notification_types.h"
17 #include "content/public/browser/web_contents_observer.h" 19 #include "content/public/browser/web_contents_observer.h"
18 #include "url/gurl.h" 20 #include "url/gurl.h"
21 #include "ipc/ipc_message_macros.h"
19 22
20 using content::WebContents; 23 using content::WebContents;
24 using content::WebContentsObserver;
25
26 class RequestPrintPreviewObserver : public WebContentsObserver {
27 public:
28 explicit RequestPrintPreviewObserver(WebContents* dialog)
29 : WebContentsObserver(dialog) {
30 }
31 virtual ~RequestPrintPreviewObserver() {}
32
33 void set_quit_closure(const base::Closure& quit_closure) {
34 quit_closure_ = quit_closure;
35 }
36
37 private:
38 // content::WebContentsObserver implementation.
39 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
40 IPC_BEGIN_MESSAGE_MAP(RequestPrintPreviewObserver, message)
41 IPC_MESSAGE_HANDLER(PrintHostMsg_RequestPrintPreview,
42 OnRequestPrintPreview)
43 IPC_MESSAGE_UNHANDLED(break;)
44 IPC_END_MESSAGE_MAP();
45 return false; // Report not handled so the real handler receives it.
46 }
47
48 void OnRequestPrintPreview(
49 const PrintHostMsg_RequestPrintPreview_Params& /* params */) {
50 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_);
51 }
52
53 base::Closure quit_closure_;
54
55 DISALLOW_COPY_AND_ASSIGN(RequestPrintPreviewObserver);
56 };
57
58 class PrintPreviewDialogClonedObserver : public WebContentsObserver {
59 public:
60 explicit PrintPreviewDialogClonedObserver(WebContents* dialog)
61 : WebContentsObserver(dialog) {
62 }
63 virtual ~PrintPreviewDialogClonedObserver() {}
64
65 RequestPrintPreviewObserver* request_preview_tab_observer() {
66 return request_preview_tab_observer_.get();
67 }
68
69 private:
70 // content::WebContentsObserver implementation.
71 virtual void DidCloneToNewWebContents(
72 WebContents* old_web_contents,
73 WebContents* new_web_contents) OVERRIDE {
74 request_preview_tab_observer_.reset(
75 new RequestPrintPreviewObserver(new_web_contents));
76 }
77
78 scoped_ptr<RequestPrintPreviewObserver> request_preview_tab_observer_;
79
80 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogClonedObserver);
81 };
82
83 class PrintPreviewDialogDestroyedObserver : public WebContentsObserver {
84 public:
85 explicit PrintPreviewDialogDestroyedObserver(WebContents* dialog)
86 : WebContentsObserver(dialog),
87 dialog_destroyed_(false) {
88 }
89 virtual ~PrintPreviewDialogDestroyedObserver() {}
90
91 bool dialog_destroyed() const { return dialog_destroyed_; }
92
93 private:
94 // content::WebContentsObserver implementation.
95 virtual void WebContentsDestroyed(WebContents* contents) OVERRIDE {
96 dialog_destroyed_ = true;
97 }
98
99 bool dialog_destroyed_;
100
101 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDestroyedObserver);
102 };
21 103
22 class PrintPreviewDialogControllerBrowserTest : public InProcessBrowserTest { 104 class PrintPreviewDialogControllerBrowserTest : public InProcessBrowserTest {
23 public: 105 public:
24 PrintPreviewDialogControllerBrowserTest() {} 106 PrintPreviewDialogControllerBrowserTest() : initiator_tab_(NULL) {}
25 virtual ~PrintPreviewDialogControllerBrowserTest() {} 107 virtual ~PrintPreviewDialogControllerBrowserTest() {}
26 108
109 WebContents* initiator_tab() {
110 return initiator_tab_;
111 }
112
113 void PrintPreview() {
114 base::RunLoop run_loop;
115 request_preview_tab_observer()->set_quit_closure(run_loop.QuitClosure());
116 chrome::Print(browser());
117 run_loop.Run();
118 }
119
120 WebContents* GetPrintPreviewDialog() {
121 printing::PrintPreviewDialogController* dialog_controller =
122 printing::PrintPreviewDialogController::GetInstance();
123 return dialog_controller->GetPrintPreviewForContents(initiator_tab_);
124 }
125
126 private:
27 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 127 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
28 #if !defined(GOOGLE_CHROME_BUILD) 128 #if !defined(GOOGLE_CHROME_BUILD)
29 command_line->AppendSwitch(switches::kEnablePrintPreview); 129 command_line->AppendSwitch(switches::kEnablePrintPreview);
30 #endif 130 #endif
31 } 131 }
32 };
33 132
34 class PrintPreviewDialogDestroyedObserver 133 virtual void SetUpOnMainThread() OVERRIDE {
35 : public content::WebContentsObserver { 134 WebContents* first_tab =
36 public: 135 browser()->tab_strip_model()->GetActiveWebContents();
37 explicit PrintPreviewDialogDestroyedObserver(WebContents* dialog) 136 ASSERT_TRUE(first_tab);
38 : content::WebContentsObserver(dialog),
39 dialog_destroyed_(false) {
40 }
41 virtual ~PrintPreviewDialogDestroyedObserver() {}
42 137
43 bool dialog_destroyed() { return dialog_destroyed_; } 138 // Open a new tab so |cloned_tab_observer_| can see it first and attach a
139 // RequestPrintPreviewObserver to it before the real
140 // PrintPreviewMessageHandler gets created. Thus enabling
141 // RequestPrintPreviewObserver to get messages first for the purposes of
142 // this test.
143 cloned_tab_observer_.reset(new PrintPreviewDialogClonedObserver(first_tab));
144 chrome::DuplicateTab(browser());
44 145
45 private: 146 initiator_tab_ = browser()->tab_strip_model()->GetActiveWebContents();
46 virtual void WebContentsDestroyed(WebContents* contents) OVERRIDE { 147 ASSERT_TRUE(initiator_tab_);
47 dialog_destroyed_ = true; 148 ASSERT_NE(first_tab, initiator_tab_);
48 } 149 }
49 150
50 bool dialog_destroyed_; 151 virtual void CleanUpOnMainThread() OVERRIDE {
152 cloned_tab_observer_.reset();
153 initiator_tab_ = NULL;
154 }
155
156 RequestPrintPreviewObserver* request_preview_tab_observer() {
157 return cloned_tab_observer_->request_preview_tab_observer();
158 }
159
160 scoped_ptr<PrintPreviewDialogClonedObserver> cloned_tab_observer_;
161 WebContents* initiator_tab_;
162
163 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogControllerBrowserTest);
51 }; 164 };
52 165
53 // Test to verify that when a initiator tab navigates, we can create a new 166 // Test to verify that when a initiator tab navigates, we can create a new
54 // preview dialog for the new tab contents. 167 // preview dialog for the new tab contents.
55 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest, 168 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest,
56 NavigateFromInitiatorTab) { 169 NavigateFromInitiatorTab) {
57 // Create a reference to initiator tab contents. 170 // print for the first time.
58 WebContents* initiator_tab = 171 PrintPreview();
59 browser()->tab_strip_model()->GetActiveWebContents();
60 ASSERT_TRUE(initiator_tab);
61
62 printing::PrintPreviewDialogController* dialog_controller =
63 printing::PrintPreviewDialogController::GetInstance();
64 ASSERT_TRUE(dialog_controller);
65 172
66 // Get the preview dialog for the initiator tab. 173 // Get the preview dialog for the initiator tab.
67 printing::PrintViewManager* print_view_manager = 174 WebContents* preview_dialog = GetPrintPreviewDialog();
68 printing::PrintViewManager::FromWebContents(initiator_tab);
69 print_view_manager->PrintPreviewNow(false);
70 WebContents* preview_dialog =
71 dialog_controller->GetOrCreatePreviewDialog(initiator_tab);
72 175
73 // Check a new print preview dialog got created. 176 // Check a new print preview dialog got created.
74 ASSERT_TRUE(preview_dialog); 177 ASSERT_TRUE(preview_dialog);
75 ASSERT_NE(initiator_tab, preview_dialog); 178 ASSERT_NE(initiator_tab(), preview_dialog);
76 179
77 // Navigate in the initiator tab. Make sure navigating destroys the print 180 // Navigate in the initiator tab. Make sure navigating destroys the print
78 // preview dialog. 181 // preview dialog.
79 PrintPreviewDialogDestroyedObserver observer(preview_dialog); 182 PrintPreviewDialogDestroyedObserver dialog_destroyed_observer(preview_dialog);
80 GURL url(chrome::kChromeUINewTabURL); 183 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
81 ui_test_utils::NavigateToURL(browser(), url); 184 ASSERT_TRUE(dialog_destroyed_observer.dialog_destroyed());
82 ASSERT_TRUE(observer.dialog_destroyed()); 185
186 // Try printing again.
187 PrintPreview();
83 188
84 // Get the print preview dialog for the initiator tab. 189 // Get the print preview dialog for the initiator tab.
85 print_view_manager->PrintPreviewNow(false); 190 WebContents* new_preview_dialog = GetPrintPreviewDialog();
86 WebContents* new_preview_dialog =
87 dialog_controller->GetOrCreatePreviewDialog(initiator_tab);
88 191
89 // Check a new preview dialog got created. 192 // Check a new preview dialog got created.
90 EXPECT_TRUE(new_preview_dialog); 193 EXPECT_TRUE(new_preview_dialog);
91 } 194 }
92 195
93 // Test to verify that after reloading the initiator tab, it creates a new 196 // Test to verify that after reloading the initiator tab, it creates a new
94 // print preview dialog. 197 // print preview dialog.
95 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest, 198 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest,
96 ReloadInitiatorTab) { 199 ReloadInitiatorTab) {
97 // Create a reference to initiator tab contents. 200 // print for the first time.
98 WebContents* initiator_tab = 201 PrintPreview();
99 browser()->tab_strip_model()->GetActiveWebContents();
100 ASSERT_TRUE(initiator_tab);
101 202
102 printing::PrintPreviewDialogController* dialog_controller = 203 WebContents* preview_dialog = GetPrintPreviewDialog();
103 printing::PrintPreviewDialogController::GetInstance();
104 ASSERT_TRUE(dialog_controller);
105
106 // Create a preview dialog for the initiator tab.
107 printing::PrintViewManager* print_view_manager =
108 printing::PrintViewManager::FromWebContents(initiator_tab);
109 print_view_manager->PrintPreviewNow(false);
110 WebContents* preview_dialog =
111 dialog_controller->GetOrCreatePreviewDialog(initiator_tab);
112 204
113 // Check a new print preview dialog got created. 205 // Check a new print preview dialog got created.
114 ASSERT_TRUE(preview_dialog); 206 ASSERT_TRUE(preview_dialog);
115 ASSERT_NE(initiator_tab, preview_dialog); 207 ASSERT_NE(initiator_tab(), preview_dialog);
116 208
117 // Reload the initiator tab. Make sure reloading destroys the print preview 209 // Reload the initiator tab. Make sure reloading destroys the print preview
118 // dialog. 210 // dialog.
119 PrintPreviewDialogDestroyedObserver dialog_destroyed_observer(preview_dialog); 211 PrintPreviewDialogDestroyedObserver dialog_destroyed_observer(preview_dialog);
120 content::WindowedNotificationObserver notification_observer( 212 content::WindowedNotificationObserver notification_observer(
121 content::NOTIFICATION_LOAD_STOP, 213 content::NOTIFICATION_LOAD_STOP,
122 content::NotificationService::AllSources()); 214 content::NotificationService::AllSources());
123 chrome::Reload(browser(), CURRENT_TAB); 215 chrome::Reload(browser(), CURRENT_TAB);
124 notification_observer.Wait(); 216 notification_observer.Wait();
125 ASSERT_TRUE(dialog_destroyed_observer.dialog_destroyed()); 217 ASSERT_TRUE(dialog_destroyed_observer.dialog_destroyed());
126 218
219 // Try printing again.
220 PrintPreview();
221
127 // Create a preview dialog for the initiator tab. 222 // Create a preview dialog for the initiator tab.
128 print_view_manager->PrintPreviewNow(false); 223 WebContents* new_preview_dialog = GetPrintPreviewDialog();
129 WebContents* new_preview_dialog =
130 dialog_controller->GetOrCreatePreviewDialog(initiator_tab);
131
132 EXPECT_TRUE(new_preview_dialog); 224 EXPECT_TRUE(new_preview_dialog);
133 } 225 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698