Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
|
Lei Zhang
2014/06/20 21:41:36
It's 2014.
ivandavid
2014/06/24 18:49:52
Done.
| |
| 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 | |
|
Lei Zhang
2014/06/20 21:41:37
Unlike JSON, you can put a comma after the 6, so i
ivandavid
2014/06/24 18:49:51
Done.
| |
| 64 }; | |
| 65 | |
| 66 class PrintPreviewObserver : public WebContentsObserver { | |
| 67 public: | |
| 68 //explicit PrintPreviewObserver(WebContents* dialog, Browser* browser); | |
|
Lei Zhang
2014/06/20 21:41:36
delete?
ivandavid
2014/06/24 18:49:52
Done.
| |
| 69 PrintPreviewObserver(WebContents* dialog, Browser* browser) : | |
|
Lei Zhang
2014/06/20 21:41:36
nit put the colon on the next line with 4 space in
ivandavid
2014/06/24 18:49:51
Done.
| |
| 70 WebContentsObserver(dialog), browser_(browser), count_(0) { | |
| 71 SetPrintPreviewSettings(false, | |
|
Lei Zhang
2014/06/20 21:57:57
Rather than doing this here, you probably want to
ivandavid
2014/06/24 18:49:51
Done.
| |
| 72 "", | |
| 73 false, | |
| 74 false, | |
| 75 printing::DEFAULT_MARGINS); | |
| 76 } | |
|
Lei Zhang
2014/06/20 21:41:36
Add a blank line after the end of a method. Ditto
ivandavid
2014/06/24 18:49:51
Done.
| |
| 77 virtual ~PrintPreviewObserver() { | |
| 78 browser_ = NULL; | |
|
Lei Zhang
2014/06/20 21:41:36
Do we need this?
ivandavid
2014/06/24 18:49:52
Done.
| |
| 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 { | |
|
Dan Beam
2014/06/21 02:44:11
fix indent
ivandavid
2014/06/24 18:49:51
Done.
| |
| 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", | |
|
Lei Zhang
2014/06/20 21:41:37
The moment you have an if block that's multiple li
ivandavid
2014/06/24 18:49:51
Done.
| |
| 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( | |
| 156 const PrintHostMsg_DidGetPreviewPageCount_Params ¶ms); | |
| 157 | |
| 158 void DidCloneToNewWebContents(WebContents* old_web_contents, | |
| 159 WebContents* new_web_contents) | |
| 160 OVERRIDE { | |
| 161 Observe(new_web_contents); | |
|
Lei Zhang
2014/06/20 21:41:36
You'd actually only want to do this once right? Th
| |
| 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_; | |
|
Lei Zhang
2014/06/20 21:41:36
not used?
ivandavid
2014/06/24 18:49:51
Done.
| |
| 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_; | |
|
Dan Beam
2014/06/21 02:44:11
please make sure all of these plain-old-data (e.g.
ivandavid
2014/06/24 18:49:51
Done.
Dan Beam
2014/06/24 22:55:15
not done
ivandavid
2014/06/25 00:02:21
It was done in patch 17. Should have mentioned tha
| |
| 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 } | |
|
Dan Beam
2014/06/21 02:44:11
like Lei mentioned, this code would be easier to r
| |
| 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 ¶ms) { | |
| 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(FILE_PATH_LITERAL("printing")); | |
| 252 base::FilePath file(FILE_PATH_LITERAL(file_name.c_str())); | |
|
Lei Zhang
2014/06/20 21:57:57
FILE_PATH_LITERAL only works for literal strings.
ivandavid
2014/06/24 18:49:51
Did you mean base::FilePath::StringType? Because t
| |
| 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( | |
| 266 FILE_PATH_LITERAL("printing")); | |
|
Lei Zhang
2014/06/20 21:41:36
FILE_PATH_LITERAL is only used with Append(). Appe
ivandavid
2014/06/24 18:49:51
Done.
| |
| 267 pdf_file_save_path = pdf_file_save_path.AppendASCII( | |
| 268 FILE_PATH_LITERAL("dummy.pdf")); | |
| 269 | |
| 270 base::RunLoop loop; | |
| 271 print_preview_observer_->set_quit_closure(loop.QuitClosure()); | |
| 272 print_preview_observer_->GetUI()->handler_-> | |
| 273 FileSelected(pdf_file_save_path, 0, NULL); | |
| 274 loop.Run(); | |
| 275 } | |
| 276 | |
| 277 private: | |
| 278 virtual void SetUpOnMainThread() OVERRIDE { | |
| 279 WebContents* tab = | |
| 280 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 281 ASSERT_TRUE(tab); | |
| 282 | |
| 283 print_preview_observer_.reset(new PrintPreviewObserver(tab, browser())); | |
| 284 chrome::DuplicateTab(browser()); | |
| 285 | |
| 286 WebContents* initiator_ = | |
| 287 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 288 ASSERT_TRUE(initiator_); | |
| 289 ASSERT_NE(tab, initiator_); | |
| 290 } | |
| 291 | |
| 292 virtual void CleanUpOnMainThread() OVERRIDE { | |
| 293 print_preview_observer_.reset(); | |
| 294 } | |
| 295 | |
| 296 scoped_ptr<PrintPreviewObserver> print_preview_observer_; | |
| 297 | |
| 298 DISALLOW_COPY_AND_ASSIGN(PrintPreviewPdfGeneratedBrowserTest); | |
| 299 }; | |
| 300 | |
| 301 IN_PROC_BROWSER_TEST_F(PrintPreviewPdfGeneratedBrowserTest, DummyTest) { | |
| 302 NavigateAndPreview("test2.html"); | |
| 303 Print(); | |
| 304 } | |
| OLD | NEW |