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

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: Changes properties from string to '.' form. Refactored test file. Fixed style issues. 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
« no previous file with comments | « no previous file | chrome/browser/resources/print_preview/native_layer.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 PrintPreviewObserver(WebContents* dialog, Browser* browser) :
70 WebContentsObserver(dialog), browser_(browser), count_(0) {
71 SetPrintPreviewSettings(false,
72 "",
73 false,
74 false,
75 printing::DEFAULT_MARGINS);
76 }
77 virtual ~PrintPreviewObserver() {
78 browser_ = NULL;
79 }
80
81 // Sets closure for the observer so that it can end the loop.
82 void set_quit_closure(const base::Closure &closure) {
83 closure_ = closure;
84 }
85 // Actually stops the message_loop so that the test can proceed.
86 void EndLoop() {
87 base::MessageLoop::current()->PostTask(FROM_HERE, closure_);
88 }
89 bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
90 IPC_BEGIN_MESSAGE_MAP(PrintPreviewObserver, message)
91 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount,
92 OnDidGetPreviewPageCount)
93 IPC_MESSAGE_UNHANDLED(return false;)
94 IPC_END_MESSAGE_MAP();
95 return false;
96 }
97 // Gets the PrintPreviewUI so that certain elements can be accessed.
98 PrintPreviewUI* GetUI() {
99 return ui_;
100 }
101 // Returns the web_contents of the print preview dialog.
102 WebContents* GetPreviewContents() {
103 return web_contents_;
104 }
105
106 // Calls a javascript function that will change the print preview settings,
107 // such as the layout, the margins, page numbers, etc.
108 void ManipulatePreviewSettings(State state) {
109 std::vector<const base::Value*> args;
110 script_argument_.reset(new base::DictionaryValue());
111 if (state == kWaitingToSendSaveAsPdf) {
112 script_argument_->SetBoolean("selectSaveAsPdfDestination", true);
113 } else if (state == kWaitingToSendLayoutSettings) {
114 script_argument_->SetBoolean("layoutSettings.portrait", is_portrait_);
115 } else if (state == kWaitingToSendPageNumbers) {
116 script_argument_->SetString("pageRange", page_numbers_);
117 } else if (state == kWaitingToSendHeadersAndFooters) {
118 script_argument_->SetBoolean("headersAndFooters", headers_and_footers_);
119 } else if (state == kWaitingToSendBackgroundColorsAndImages) {
120 script_argument_->SetBoolean("backgroundColorsAndImages",
121 background_colors_and_images_);
122 } else if (state == kWaitingToSendMargins) {
123 script_argument_->SetInteger("margins", margins_);
124 }
125 args.push_back(script_argument_.get());
126 DCHECK(!script_argument_->empty());
127 DCHECK(!args.empty());
128 ui_->web_ui()->CallJavascriptFunction("onManipulateSettingsForTest", args);
129 }
130
131 // Function to set the print preview settings and save them so they
132 // can be sent later. Currently only used in the constructor. Will be
133 // used when creating a test and take command line arguments.
134 // |page_numbers| is a comma separated page range.
135 // Example: "1-5,9" will print pages 1 through 5 and page 9.
136 // The pages specified must be less than or equal to the maximum
137 // page number. An empty string seems to be valid input, however
138 // further testing will be required to see if that is actually
139 // true.
140 void SetPrintPreviewSettings(bool is_portrait,
141 const std::string& page_numbers,
142 bool headers_and_footers,
143 bool background_colors_and_images,
144 printing::MarginType margins) {
145 is_portrait_ = is_portrait;
146 page_numbers_ = page_numbers;
147 headers_and_footers_ = headers_and_footers;
148 background_colors_and_images_ = background_colors_and_images;
149 margins_ = margins;
150 }
151
152 private:
153 // Called when the observer gets the IPC message stating that the
154 // page count is ready.
155 void OnDidGetPreviewPageCount(
ivandavid 2014/06/20 21:31:45 Needs to be separated from the definition because
Lei Zhang 2014/06/20 21:41:36 There's really no issue with separating the decl f
156 const PrintHostMsg_DidGetPreviewPageCount_Params &params);
157
158 void DidCloneToNewWebContents(WebContents* old_web_contents,
159 WebContents* new_web_contents)
160 OVERRIDE {
161 Observe(new_web_contents);
162 }
163
164 void WebContentsDestroyed() OVERRIDE {
165 EndLoop();
166 }
167
168 Browser* browser_;
169 base::Closure closure_;
170 WebContents* web_contents_;
171
172 PrintPreviewUI* ui_;
173
174 int count_;
175
176 scoped_ptr<PrintPreviewObserver> print_preview_observer_;
177 scoped_ptr<base::DictionaryValue> script_argument_;
178 bool is_portrait_;
179 std::string page_numbers_;
180 bool headers_and_footers_;
181 bool background_colors_and_images_;
182 printing::MarginType margins_;
183
184 DISALLOW_COPY_AND_ASSIGN(PrintPreviewObserver);
185 };
186
187 class UIDoneLoadingMessageHandler : public content::WebUIMessageHandler {
188 public:
189 explicit UIDoneLoadingMessageHandler(PrintPreviewObserver* observer) :
190 observer_(observer), state_(kWaitingToSendSaveAsPdf) {}
191 virtual ~UIDoneLoadingMessageHandler() {
192 observer_ = NULL;
193 }
194 void HandleDone(const base::ListValue* /* args */) {
195 if (state_ == kWaitingForFinalMessage) {
196 observer_->EndLoop();
197 } else {
198 observer_->ManipulatePreviewSettings(state_);
199 state_ = static_cast<State>(static_cast<int>(state_) + 1);
200 }
201 }
202 void HandleFailure(const base::ListValue* /* args */) {
203 FAIL();
204 }
205 void RegisterMessages() OVERRIDE {
206 web_ui()->RegisterMessageCallback(
207 "UILoadedForTest",
208 base::Bind(&UIDoneLoadingMessageHandler::HandleDone,
209 base::Unretained(this)));
210
211 web_ui()->RegisterMessageCallback(
212 "UIFailedLoadingForTest",
213 base::Bind(&UIDoneLoadingMessageHandler::HandleFailure,
214 base::Unretained(this)));
215 }
216 private:
217 PrintPreviewObserver* observer_;
218 State state_;
219
220 DISALLOW_COPY_AND_ASSIGN(UIDoneLoadingMessageHandler);
221 };
222
223 void PrintPreviewObserver::OnDidGetPreviewPageCount(
224 const PrintHostMsg_DidGetPreviewPageCount_Params &params) {
225
226 WebContents* tab = browser_->tab_strip_model()->GetActiveWebContents();
227 ASSERT_TRUE(tab);
228 printing::PrintPreviewDialogController* dialog_controller =
229 printing::PrintPreviewDialogController::GetInstance();
230 ASSERT_TRUE(dialog_controller);
231 web_contents_ = dialog_controller->GetPrintPreviewForContents(tab);
232 ASSERT_TRUE(web_contents_);
233 ASSERT_TRUE(printing::PrintPreviewDialogController::
234 IsPrintPreviewDialog(web_contents_));
235 ui_ = static_cast<PrintPreviewUI*>(web_contents_->GetWebUI()->
236 GetController());
237 ASSERT_TRUE(ui_);
238 ASSERT_TRUE(ui_->web_ui());
239 ui_->web_ui()->CallJavascriptFunction("onEnableManipulateSettingsForTest");
240 Observe(web_contents_);
241 UIDoneLoadingMessageHandler* handler = new UIDoneLoadingMessageHandler(this);
242 ui_->web_ui()->AddMessageHandler(handler);
243 }
244
245 class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
246 public:
247 PrintPreviewPdfGeneratedBrowserTest() {}
248 virtual ~PrintPreviewPdfGeneratedBrowserTest() {}
249
250 void NavigateAndPreview(std::string file_name) {
251 base::FilePath directory("printing");
252 base::FilePath file(file_name);
253 ui_test_utils::NavigateToURL(browser(),
254 ui_test_utils::GetTestUrl(directory, file));
255
256 base::RunLoop loop;
257 print_preview_observer_->set_quit_closure(loop.QuitClosure());
258 chrome::Print(browser());
259 loop.Run();
260 }
261
262 void Print() {
263 base::FilePath pdf_file_save_path;
264 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &pdf_file_save_path));
265 pdf_file_save_path = pdf_file_save_path.AppendASCII("printing");
266 pdf_file_save_path = pdf_file_save_path.AppendASCII("dummy.pdf");
267
268 base::RunLoop loop;
269 print_preview_observer_->set_quit_closure(loop.QuitClosure());
270 print_preview_observer_->GetUI()->handler_->
271 FileSelected(pdf_file_save_path, 0, NULL);
272 loop.Run();
273 }
274
275 private:
276 virtual void SetUpOnMainThread() OVERRIDE {
277 WebContents* tab =
278 browser()->tab_strip_model()->GetActiveWebContents();
279 ASSERT_TRUE(tab);
280
281 print_preview_observer_.reset(new PrintPreviewObserver(tab, browser()));
282 chrome::DuplicateTab(browser());
283
284 WebContents* initiator_ =
285 browser()->tab_strip_model()->GetActiveWebContents();
286 ASSERT_TRUE(initiator_);
287 ASSERT_NE(tab, initiator_);
288 }
289
290 virtual void CleanUpOnMainThread() OVERRIDE {
291 print_preview_observer_.reset();
292 }
293
294 scoped_ptr<PrintPreviewObserver> print_preview_observer_;
295
296 DISALLOW_COPY_AND_ASSIGN(PrintPreviewPdfGeneratedBrowserTest);
297 };
298
299 IN_PROC_BROWSER_TEST_F(PrintPreviewPdfGeneratedBrowserTest, DummyTest) {
300 NavigateAndPreview("test2.html");
301 Print();
302 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/print_preview/native_layer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698