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

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

Issue 335583004: Added a test that currently is able to print to pdf. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed style issues and made the testing functionality optional Created 6 years, 6 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
OLDNEW
(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 <string>
6 #include <vector>
7
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/path_service.h"
12 #include "base/run_loop.h"
13 #include "base/strings/string_util.h"
14 #include "chrome/app/chrome_command_ids.h"
15 #include "chrome/browser/net/referrer.h"
16 #include "chrome/browser/printing/print_preview_dialog_controller.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/browser_commands.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/browser/ui/webui/print_preview/print_preview_handler.h"
22 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
23 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h"
24 #include "chrome/common/chrome_paths.h"
25 #include "chrome/common/print_messages.h"
26 #include "chrome/common/url_constants.h"
27 #include "chrome/test/base/in_process_browser_test.h"
28 #include "chrome/test/base/ui_test_utils.h"
29 #include "content/public/browser/web_contents.h"
30 #include "content/public/browser/web_ui_message_handler.h"
31 #include "content/public/common/page_transition_types.h"
32 #include "content/public/test/browser_test_utils.h"
33 #include "content/public/test/test_navigation_observer.h"
34 #include "content/public/test/test_utils.h"
35 #include "printing/print_job_constants.h"
36 #include "ui/events/keycodes/keyboard_codes.h"
37 #include "url/gurl.h"
38 #include "ipc/ipc_message_macros.h"
39
40 using content::WebContents;
41 using content::WebContentsObserver;
42
43 class PrintPreviewObserver;
44 class UIDoneLoadingMessageHandler;
45
46 // Message refers to the 'UILoaded' message from print_preview.js.
47 // It gets sent either from onPreviewGenerationDone or from
48 // onManipulateSettings_() in print_preview.js
49 enum State {
50 // Waiting for the first message so the program can select Save as PDF
51 kWaitingToSendSaveAsPdf = 0,
52 // Waiting for the second message so the test can set the layout
53 kWaitingToSendLayoutSettings = 1,
54 // Waiting for the third message so the test can set the page numbers
55 kWaitingToSendPageNumbers = 2,
56 // Waiting for the forth message so the test can set the headers checkbox
57 kWaitingToSendHeadersAndFooters = 3,
58 // Waiting for the fifth message so the test can set the background checkbox
59 kWaitingToSendBackgroundColorsAndImages = 4,
60 // Waiting for the sixth message so the test can set the margins combobox
61 kWaitingToSendMargins = 5,
62 // Waiting for the final message so the program can save to PDF.
63 kWaitingForFinalMessage = 6
64 };
65
66 class PrintPreviewObserver : public WebContentsObserver {
67 public:
68 explicit PrintPreviewObserver(WebContents* dialog, Browser* browser);
69 virtual ~PrintPreviewObserver();
70
71 void set_quit_closure(const base::Closure &closure);
72 void EndLoop();
73 bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
74 PrintPreviewUI* GetUI() {
75 return ui_;
76 }
77
78 WebContents* GetPreviewContents() {
79 return web_contents_;
80 }
81
82 // Calls a javascript function that will change the print preview settings,
83 // such as the layout, the margins, page numbers, etc.
84 void ManipulatePreviewSettings(enum State);
85
86 // Function to set the print preview settings and save them so they
87 // can be sent later. Currently only used in the constructor. Will be
88 // used when creating a test and take command line arguments.
89 void SetPrintPreviewSettings(const std::string& layout_settings,
90 const std::string& page_numbers,
91 bool headers_and_footers,
92 bool background_colors_and_images,
93 printing::MarginType margin_type);
94
95 private:
96 void OnDidGetPreviewPageCount(
97 const PrintHostMsg_DidGetPreviewPageCount_Params &params);
98
99 void DidCloneToNewWebContents(WebContents* old_web_contents,
100 WebContents* new_web_contents)
101 OVERRIDE;
102
103 void WebContentsDestroyed();
104
105 Browser* browser_;
106 base::Closure closure_;
107 WebContents* web_contents_;
108
109 PrintPreviewUI* ui_;
110
111 int count_;
112
113 scoped_ptr<PrintPreviewObserver> print_preview_observer_;
114 scoped_ptr<base::StringValue> message_name_;
115 scoped_ptr<base::StringValue> layout_settings_;
116 scoped_ptr<base::StringValue> page_numbers_;
117 scoped_ptr<base::FundamentalValue> headers_and_footers_;
118 scoped_ptr<base::FundamentalValue> background_colors_and_images_;
119 scoped_ptr<base::FundamentalValue> margins_;
120
121 DISALLOW_COPY_AND_ASSIGN(PrintPreviewObserver);
122 };
123
124 class UIDoneLoadingMessageHandler : public content::WebUIMessageHandler {
125 public:
126 explicit UIDoneLoadingMessageHandler(PrintPreviewObserver* observer);
127 virtual ~UIDoneLoadingMessageHandler();
128 void HandleDone(const base::ListValue* /* args */);
129 void RegisterMessages() OVERRIDE;
130 private:
131 PrintPreviewObserver* observer_;
132 enum State state_;
133
134 DISALLOW_COPY_AND_ASSIGN(UIDoneLoadingMessageHandler);
135 };
136
137 UIDoneLoadingMessageHandler::UIDoneLoadingMessageHandler(
138 PrintPreviewObserver* observer) :
139 observer_(observer), state_(kWaitingToSendSaveAsPdf) {}
140
141 UIDoneLoadingMessageHandler::~UIDoneLoadingMessageHandler() {
142 observer_ = NULL;
143 }
144
145 void UIDoneLoadingMessageHandler::RegisterMessages() OVERRIDE {
146 web_ui()->RegisterMessageCallback(
147 "UILoadedForTest",
148 base::Bind(&UIDoneLoadingMessageHandler::HandleDone,
149 base::Unretained(this)));
150 }
151
152 void UIDoneLoadingMessageHandler::HandleDone(
153 const base::ListValue* /* args */) {
154 if (state_ == kWaitingForFinalMessage) {
155 observer_->EndLoop();
156 } else {
157 observer_->ManipulatePreviewSettings(state_);
158 state_ = static_cast<enum State>(static_cast<int>(state_) + 1);
159 }
160 }
161
162 PrintPreviewObserver::PrintPreviewObserver(
163 WebContents* dialog,
164 Browser* browser) :
165 WebContentsObserver(dialog), browser_(browser), count_(0) {
166 SetPrintPreviewSettings("PORTRAIT",
167 "",
168 false,
169 false,
170 printing::DEFAULT_MARGINS);
171 }
172
173 PrintPreviewObserver::~PrintPreviewObserver() {
174 browser_ = NULL;
175 message_name_.reset();
176 layout_settings_.reset();
177 page_numbers_.reset();
178 headers_and_footers_.reset();
179 background_colors_and_images_.reset();
180 margins_.reset();
181 }
182
183 void PrintPreviewObserver::set_quit_closure(const base::Closure &closure) {
184 closure_ = closure;
185 }
186
187 void PrintPreviewObserver::EndLoop() {
188 base::MessageLoop::current()->PostTask(FROM_HERE, closure_);
189 }
190
191 bool PrintPreviewObserver::OnMessageReceived(const IPC::Message& message)
192 OVERRIDE {
193 IPC_BEGIN_MESSAGE_MAP(PrintPreviewObserver, message)
194 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount,
195 OnDidGetPreviewPageCount)
196 IPC_MESSAGE_UNHANDLED(break;)
197 IPC_END_MESSAGE_MAP();
198 return false;
199 }
200
201 void PrintPreviewObserver::OnDidGetPreviewPageCount(
202 const PrintHostMsg_DidGetPreviewPageCount_Params &params) {
203
204 WebContents* tab = browser_->tab_strip_model()->GetActiveWebContents();
205
206 ASSERT_TRUE(tab);
207
208 printing::PrintPreviewDialogController* dialog_controller =
209 printing::PrintPreviewDialogController::GetInstance();
210
211 ASSERT_TRUE(dialog_controller);
212
213 web_contents_ = dialog_controller->GetPrintPreviewForContents(tab);
214
215 ASSERT_TRUE(web_contents_ && printing::PrintPreviewDialogController::
216 IsPrintPreviewDialog(web_contents_));
217
218 ui_ = static_cast<PrintPreviewUI*>(web_contents_->GetWebUI()->
219 GetController());
220
221 ASSERT_TRUE(ui_);
222
223 ASSERT_TRUE(ui_->web_ui());
224
225 ui_->web_ui()->CallJavascriptFunction("onEnableManipulateSettings");
226
227 this->Observe(web_contents_);
228
229 UIDoneLoadingMessageHandler* handler = new UIDoneLoadingMessageHandler(this);
230
231 ui_->web_ui()->AddMessageHandler(handler);
232 }
233
234 void PrintPreviewObserver::SetPrintPreviewSettings(
235 const std::string& layout_settings,
Lei Zhang 2014/06/17 23:22:20 How about we use a bool here and rename it is_port
ivandavid 2014/06/18 00:25:59 Using bool now.
236 const std::string& page_numbers,
Lei Zhang 2014/06/17 23:22:20 Mention this is a comma separated page range and g
ivandavid 2014/06/18 00:25:59 Added comment.
237 bool headers_and_footers,
238 bool background_colors_and_images,
239 printing::MarginType margins) {
240 layout_settings_.reset(new base::StringValue(layout_settings));
241 page_numbers_.reset(new base::StringValue(page_numbers));
242 headers_and_footers_.reset(
243 new base::FundamentalValue(headers_and_footers));
244 background_colors_and_images_.reset(
245 new base::FundamentalValue(background_colors_and_images));
246 margins_.reset(new base::FundamentalValue(margins));
247 }
248
249
250 void PrintPreviewObserver::ManipulatePreviewSettings(enum State state) {
251 std::vector<const base::Value*> args;
252 if (state == kWaitingToSendSaveAsPdf) {
253 message_name_.reset(new base::StringValue("SAVE_AS_PDF"));
254 args.push_back(message_name_.get());
255 } else if (state == kWaitingToSendLayoutSettings) {
256 message_name_.reset(new base::StringValue("LAYOUT_SETTINGS"));
257 args.push_back(message_name_.get());
258 args.push_back(layout_settings_.get());
259 } else if (state == kWaitingToSendPageNumbers) {
260 message_name_.reset(new base::StringValue("PAGE_NUMBERS"));
261 args.push_back(message_name_.get());
262 args.push_back(page_numbers_.get());
263 } else if (state == kWaitingToSendHeadersAndFooters) {
264 message_name_.reset(new base::StringValue("HEADERS_AND_FOOTERS"));
265 args.push_back(message_name_.get());
266 args.push_back(headers_and_footers_.get());
267 } else if (state == kWaitingToSendBackgroundColorsAndImages) {
268 message_name_.reset(new base::StringValue("BACKGROUND_COLORS_AND_IMAGES"));
269 args.push_back(message_name_.get());
270 args.push_back(background_colors_and_images_.get());
271 } else if (state == kWaitingToSendMargins) {
272 message_name_.reset(new base::StringValue("MARGINS"));
273 args.push_back(message_name_.get());
274 args.push_back(margins_.get());
275 }
276 ui_->web_ui()->CallJavascriptFunction("onManipulateSettings", args);
Lei Zhang 2014/06/17 23:22:20 At least one of the if/if else cases should have m
277 }
278
279 void PrintPreviewObserver::DidCloneToNewWebContents(
280 WebContents* old_web_contents,
281 WebContents* new_web_contents)
282 OVERRIDE {
283 this->Observe(new_web_contents);
284 }
285
286 void PrintPreviewObserver::WebContentsDestroyed() {
287 this->EndLoop();
288 }
289
290 class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
291 public:
292 PrintPreviewPdfGeneratedBrowserTest() {}
293 virtual ~PrintPreviewPdfGeneratedBrowserTest() {}
294
295 void NavigateAndPreview(std::string file_name) {
296 base::FilePath directory("printing");
297 base::FilePath file(file_name);
298
299 ui_test_utils::NavigateToURL(browser(),
300 ui_test_utils::GetTestUrl(directory, file));
301
302 base::RunLoop loop;
303 print_preview_observer_->set_quit_closure(loop.QuitClosure());
304 chrome::Print(browser());
305 loop.Run();
306 }
307
308 void Print() {
309 base::FilePath pdf_file_save_path;
310 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &pdf_file_save_path));
311 pdf_file_save_path = pdf_file_save_path.AppendASCII("printing");
312 pdf_file_save_path = pdf_file_save_path.AppendASCII("dummy.pdf");
313
314 base::RunLoop loop;
315 print_preview_observer_->set_quit_closure(loop.QuitClosure());
316
317 print_preview_observer_->GetUI()->handler_->
318 FileSelected(pdf_file_save_path, 0, NULL);
319
320 loop.Run();
321 }
322
323 private:
324 virtual void SetUpOnMainThread() OVERRIDE {
325 WebContents* tab =
326 browser()->tab_strip_model()->GetActiveWebContents();
327 ASSERT_TRUE(tab);
328
329 print_preview_observer_.reset(new PrintPreviewObserver(tab, browser()));
330 chrome::DuplicateTab(browser());
331
332 WebContents* initiator_ =
333 browser()->tab_strip_model()->GetActiveWebContents();
334 ASSERT_TRUE(initiator_);
335 ASSERT_NE(tab, initiator_);
336 }
337
338 virtual void CleanUpOnMainThread() OVERRIDE {
339 print_preview_observer_.reset();
340 }
341
342 scoped_ptr<PrintPreviewObserver> print_preview_observer_;
343
344 DISALLOW_COPY_AND_ASSIGN(PrintPreviewPdfGeneratedBrowserTest);
345 };
346
347 IN_PROC_BROWSER_TEST_F(PrintPreviewPdfGeneratedBrowserTest, DummyTest) {
348 NavigateAndPreview("test2.html");
349 Print();
350 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698