Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <fstream> | 6 #include <fstream> |
| 7 #include <iostream> | 7 #include <iostream> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <utility> | 11 #include <utility> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/bind.h" | |
| 15 #include "base/callback.h" | |
| 14 #include "base/file_util.h" | 16 #include "base/file_util.h" |
| 15 #include "base/files/file.h" | 17 #include "base/files/file.h" |
| 16 #include "base/files/file_path.h" | 18 #include "base/files/file_path.h" |
| 17 #include "base/files/scoped_temp_dir.h" | 19 #include "base/files/scoped_temp_dir.h" |
| 18 #include "base/logging.h" | 20 #include "base/logging.h" |
| 19 #include "base/md5.h" | 21 #include "base/md5.h" |
| 20 #include "base/memory/scoped_ptr.h" | 22 #include "base/memory/scoped_ptr.h" |
| 21 #include "base/path_service.h" | 23 #include "base/path_service.h" |
| 22 #include "base/run_loop.h" | 24 #include "base/run_loop.h" |
| 23 #include "base/scoped_native_library.h" | 25 #include "base/scoped_native_library.h" |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 } else if (state_ == kWaitingToSendBackgroundColorsAndImages) { | 179 } else if (state_ == kWaitingToSendBackgroundColorsAndImages) { |
| 178 script_argument.SetBoolean("backgroundColorsAndImages", | 180 script_argument.SetBoolean("backgroundColorsAndImages", |
| 179 settings_->background_colors_and_images); | 181 settings_->background_colors_and_images); |
| 180 state_ = kWaitingToSendMargins; | 182 state_ = kWaitingToSendMargins; |
| 181 failed_setting_ = "Background Colors and Images"; | 183 failed_setting_ = "Background Colors and Images"; |
| 182 } else if (state_ == kWaitingToSendMargins) { | 184 } else if (state_ == kWaitingToSendMargins) { |
| 183 script_argument.SetInteger("margins", settings_->margins); | 185 script_argument.SetInteger("margins", settings_->margins); |
| 184 state_ = kWaitingForFinalMessage; | 186 state_ = kWaitingForFinalMessage; |
| 185 failed_setting_ = "Margins"; | 187 failed_setting_ = "Margins"; |
| 186 } else if (state_ == kWaitingForFinalMessage) { | 188 } else if (state_ == kWaitingForFinalMessage) { |
| 187 EndLoop(); | 189 EndLoop(); |
|
Lei Zhang
2014/07/22 00:11:30
Actually, do you need this EndLoop() call? It seem
ivandavid
2014/07/22 02:40:26
Oooo. I didn't really think about this. I removed
ivandavid
2014/07/22 02:40:26
Done.
| |
| 188 return; | 190 return; |
| 189 } | 191 } |
| 190 | 192 |
| 191 ASSERT_FALSE(script_argument.empty()); | 193 ASSERT_FALSE(script_argument.empty()); |
| 192 GetUI()->web_ui()->CallJavascriptFunction( | 194 GetUI()->web_ui()->CallJavascriptFunction( |
| 193 "onManipulateSettingsForTest", script_argument); | 195 "onManipulateSettingsForTest", script_argument); |
| 194 } | 196 } |
| 195 | 197 |
| 196 // Saves the print preview settings to be sent to the print preview dialog. | 198 // Saves the print preview settings to be sent to the print preview dialog. |
| 197 void SetPrintPreviewSettings(const PrintPreviewSettings& settings) { | 199 void SetPrintPreviewSettings(const PrintPreviewSettings& settings) { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 265 ui->web_ui()->AddMessageHandler(new UIDoneLoadingMessageHandler(this)); | 267 ui->web_ui()->AddMessageHandler(new UIDoneLoadingMessageHandler(this)); |
| 266 ui->web_ui()->CallJavascriptFunction("onEnableManipulateSettingsForTest"); | 268 ui->web_ui()->CallJavascriptFunction("onEnableManipulateSettingsForTest"); |
| 267 } | 269 } |
| 268 | 270 |
| 269 virtual void DidCloneToNewWebContents( | 271 virtual void DidCloneToNewWebContents( |
| 270 WebContents* old_web_contents, | 272 WebContents* old_web_contents, |
| 271 WebContents* new_web_contents) OVERRIDE { | 273 WebContents* new_web_contents) OVERRIDE { |
| 272 Observe(new_web_contents); | 274 Observe(new_web_contents); |
| 273 } | 275 } |
| 274 | 276 |
| 275 virtual void WebContentsDestroyed() OVERRIDE { | |
| 276 EndLoop(); | |
| 277 } | |
| 278 | |
| 279 Browser* browser_; | 277 Browser* browser_; |
| 280 base::Closure quit_closure_; | 278 base::Closure quit_closure_; |
| 281 scoped_ptr<PrintPreviewSettings> settings_; | 279 scoped_ptr<PrintPreviewSettings> settings_; |
| 282 | 280 |
| 283 // State of the observer. The state indicates what message to send | 281 // State of the observer. The state indicates what message to send |
| 284 // next. The state advances whenever the message handler calls | 282 // next. The state advances whenever the message handler calls |
| 285 // ManipulatePreviewSettings() on the observer. | 283 // ManipulatePreviewSettings() on the observer. |
| 286 State state_; | 284 State state_; |
| 287 std::string failed_setting_; | 285 std::string failed_setting_; |
| 288 | 286 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 309 chrome::Print(browser()); | 307 chrome::Print(browser()); |
| 310 loop.Run(); | 308 loop.Run(); |
| 311 } | 309 } |
| 312 | 310 |
| 313 // Prints the web page to a PDF. NavigateAndPreview must be called first. | 311 // Prints the web page to a PDF. NavigateAndPreview must be called first. |
| 314 void Print() { | 312 void Print() { |
| 315 ASSERT_FALSE(pdf_file_save_path_.empty()); | 313 ASSERT_FALSE(pdf_file_save_path_.empty()); |
| 316 | 314 |
| 317 base::RunLoop loop; | 315 base::RunLoop loop; |
| 318 print_preview_observer_->set_quit_closure(loop.QuitClosure()); | 316 print_preview_observer_->set_quit_closure(loop.QuitClosure()); |
| 317 | |
| 318 // Callback that is called by PrintPreviewHandler and ends | |
| 319 // |print_preview_observer|'s message loop when the file has been saved. | |
| 320 base::Closure callback = | |
| 321 base::Bind(&PrintPreviewObserver::EndLoop, | |
| 322 base::Unretained(print_preview_observer_.get())); | |
| 323 print_preview_observer_->GetUI()->SetCallbackForTesting(&callback); | |
| 324 | |
| 319 print_preview_observer_->GetUI()->SetSelectedFileForTesting( | 325 print_preview_observer_->GetUI()->SetSelectedFileForTesting( |
| 320 pdf_file_save_path_); | 326 pdf_file_save_path_); |
| 321 loop.Run(); | 327 loop.Run(); |
| 322 | |
| 323 // Checks to see if the file exists and is readable. If the file doesn't | |
| 324 // exist, the test will fail. If it exists, but isn't readable, the test | |
| 325 // will keep polling until the file becomes readable. This is due to a | |
| 326 // problem on Windows where the file exists, but isn't readable, causing | |
| 327 // other ASSERT statements in this test to fail. | |
| 328 // TODO(ivandavid): Come up with a better way to do this. | |
| 329 ASSERT_TRUE(base::PathExists(pdf_file_save_path_)); | |
| 330 while (true) { | |
| 331 base::File pdf_file( | |
| 332 pdf_file_save_path_, base::File::FLAG_OPEN | base::File::FLAG_READ); | |
| 333 if (pdf_file.IsValid()) | |
| 334 break; | |
| 335 } | |
| 336 } | 328 } |
| 337 | 329 |
| 338 // Initializes function pointers from the PDF library. Called once when the | 330 // Initializes function pointers from the PDF library. Called once when the |
| 339 // test starts. The library is closed when the browser test ends. | 331 // test starts. The library is closed when the browser test ends. |
| 340 void InitPdfFunctions() { | 332 void InitPdfFunctions() { |
| 341 base::FilePath pdf_module_path; | 333 base::FilePath pdf_module_path; |
| 342 | 334 |
| 343 ASSERT_TRUE(PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_module_path)); | 335 ASSERT_TRUE(PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_module_path)); |
| 344 ASSERT_TRUE(base::PathExists(pdf_module_path)); | 336 ASSERT_TRUE(base::PathExists(pdf_module_path)); |
| 345 pdf_lib_.Reset(base::LoadNativeLibrary(pdf_module_path, NULL)); | 337 pdf_lib_.Reset(base::LoadNativeLibrary(pdf_module_path, NULL)); |
| (...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 709 // waiting for this message and start waiting for the image data. | 701 // waiting for this message and start waiting for the image data. |
| 710 std::cout << "#EOF\n"; | 702 std::cout << "#EOF\n"; |
| 711 std::cout.flush(); | 703 std::cout.flush(); |
| 712 | 704 |
| 713 SendPng(); | 705 SendPng(); |
| 714 Reset(); | 706 Reset(); |
| 715 } | 707 } |
| 716 } | 708 } |
| 717 | 709 |
| 718 } // namespace printing | 710 } // namespace printing |
| OLD | NEW |