OLD | NEW |
(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(State 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 // |page_numbers| is a comma separated page range. |
| 90 // Example: "1-5,9" will print pages 1 through 5 and page 9. |
| 91 // The pages specified must be less than or equal to the maximum |
| 92 // page number. |
| 93 void SetPrintPreviewSettings(bool is_portrait, |
| 94 const std::string& page_numbers, |
| 95 bool headers_and_footers, |
| 96 bool background_colors_and_images, |
| 97 printing::MarginType margin_type); |
| 98 |
| 99 private: |
| 100 void OnDidGetPreviewPageCount( |
| 101 const PrintHostMsg_DidGetPreviewPageCount_Params ¶ms); |
| 102 |
| 103 void DidCloneToNewWebContents(WebContents* old_web_contents, |
| 104 WebContents* new_web_contents) |
| 105 OVERRIDE; |
| 106 |
| 107 void WebContentsDestroyed(); |
| 108 |
| 109 Browser* browser_; |
| 110 base::Closure closure_; |
| 111 WebContents* web_contents_; |
| 112 |
| 113 PrintPreviewUI* ui_; |
| 114 |
| 115 int count_; |
| 116 |
| 117 scoped_ptr<PrintPreviewObserver> print_preview_observer_; |
| 118 scoped_ptr<base::StringValue> message_name_; |
| 119 scoped_ptr<base::FundamentalValue> is_portrait_; |
| 120 scoped_ptr<base::StringValue> page_numbers_; |
| 121 scoped_ptr<base::FundamentalValue> headers_and_footers_; |
| 122 scoped_ptr<base::FundamentalValue> background_colors_and_images_; |
| 123 scoped_ptr<base::FundamentalValue> margins_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(PrintPreviewObserver); |
| 126 }; |
| 127 |
| 128 class UIDoneLoadingMessageHandler : public content::WebUIMessageHandler { |
| 129 public: |
| 130 explicit UIDoneLoadingMessageHandler(PrintPreviewObserver* observer); |
| 131 virtual ~UIDoneLoadingMessageHandler(); |
| 132 void HandleDone(const base::ListValue* /* args */); |
| 133 void RegisterMessages() OVERRIDE; |
| 134 private: |
| 135 PrintPreviewObserver* observer_; |
| 136 State state_; |
| 137 |
| 138 DISALLOW_COPY_AND_ASSIGN(UIDoneLoadingMessageHandler); |
| 139 }; |
| 140 |
| 141 UIDoneLoadingMessageHandler::UIDoneLoadingMessageHandler( |
| 142 PrintPreviewObserver* observer) : |
| 143 observer_(observer), state_(kWaitingToSendSaveAsPdf) {} |
| 144 |
| 145 UIDoneLoadingMessageHandler::~UIDoneLoadingMessageHandler() { |
| 146 observer_ = NULL; |
| 147 } |
| 148 |
| 149 void UIDoneLoadingMessageHandler::RegisterMessages() OVERRIDE { |
| 150 web_ui()->RegisterMessageCallback( |
| 151 "UILoadedForTest", |
| 152 base::Bind(&UIDoneLoadingMessageHandler::HandleDone, |
| 153 base::Unretained(this))); |
| 154 } |
| 155 |
| 156 void UIDoneLoadingMessageHandler::HandleDone( |
| 157 const base::ListValue* /* args */) { |
| 158 if (state_ == kWaitingForFinalMessage) { |
| 159 observer_->EndLoop(); |
| 160 } else { |
| 161 observer_->ManipulatePreviewSettings(state_); |
| 162 state_ = static_cast<State>(static_cast<int>(state_) + 1); |
| 163 } |
| 164 } |
| 165 |
| 166 PrintPreviewObserver::PrintPreviewObserver( |
| 167 WebContents* dialog, |
| 168 Browser* browser) : |
| 169 WebContentsObserver(dialog), browser_(browser), count_(0) { |
| 170 SetPrintPreviewSettings("PORTRAIT", |
| 171 "", |
| 172 false, |
| 173 false, |
| 174 printing::DEFAULT_MARGINS); |
| 175 } |
| 176 |
| 177 PrintPreviewObserver::~PrintPreviewObserver() { |
| 178 browser_ = NULL; |
| 179 message_name_.reset(); |
| 180 is_portrait_.reset(); |
| 181 page_numbers_.reset(); |
| 182 headers_and_footers_.reset(); |
| 183 background_colors_and_images_.reset(); |
| 184 margins_.reset(); |
| 185 } |
| 186 |
| 187 void PrintPreviewObserver::set_quit_closure(const base::Closure &closure) { |
| 188 closure_ = closure; |
| 189 } |
| 190 |
| 191 void PrintPreviewObserver::EndLoop() { |
| 192 base::MessageLoop::current()->PostTask(FROM_HERE, closure_); |
| 193 } |
| 194 |
| 195 bool PrintPreviewObserver::OnMessageReceived(const IPC::Message& message) |
| 196 OVERRIDE { |
| 197 IPC_BEGIN_MESSAGE_MAP(PrintPreviewObserver, message) |
| 198 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, |
| 199 OnDidGetPreviewPageCount) |
| 200 IPC_MESSAGE_UNHANDLED(break;) |
| 201 IPC_END_MESSAGE_MAP(); |
| 202 return false; |
| 203 } |
| 204 |
| 205 void PrintPreviewObserver::OnDidGetPreviewPageCount( |
| 206 const PrintHostMsg_DidGetPreviewPageCount_Params ¶ms) { |
| 207 |
| 208 WebContents* tab = browser_->tab_strip_model()->GetActiveWebContents(); |
| 209 |
| 210 ASSERT_TRUE(tab); |
| 211 |
| 212 printing::PrintPreviewDialogController* dialog_controller = |
| 213 printing::PrintPreviewDialogController::GetInstance(); |
| 214 |
| 215 ASSERT_TRUE(dialog_controller); |
| 216 |
| 217 web_contents_ = dialog_controller->GetPrintPreviewForContents(tab); |
| 218 |
| 219 ASSERT_TRUE(web_contents_ && printing::PrintPreviewDialogController:: |
| 220 IsPrintPreviewDialog(web_contents_)); |
| 221 |
| 222 ui_ = static_cast<PrintPreviewUI*>(web_contents_->GetWebUI()-> |
| 223 GetController()); |
| 224 |
| 225 ASSERT_TRUE(ui_); |
| 226 |
| 227 ASSERT_TRUE(ui_->web_ui()); |
| 228 |
| 229 ui_->web_ui()->CallJavascriptFunction("onEnableManipulateSettingsForTest"); |
| 230 |
| 231 this->Observe(web_contents_); |
| 232 |
| 233 UIDoneLoadingMessageHandler* handler = new UIDoneLoadingMessageHandler(this); |
| 234 |
| 235 ui_->web_ui()->AddMessageHandler(handler); |
| 236 } |
| 237 |
| 238 void PrintPreviewObserver::SetPrintPreviewSettings( |
| 239 bool is_portrait, |
| 240 const std::string& page_numbers, |
| 241 bool headers_and_footers, |
| 242 bool background_colors_and_images, |
| 243 printing::MarginType margins) { |
| 244 is_portrait_.reset(new base::FundamentalValue(is_portrait)); |
| 245 page_numbers_.reset(new base::StringValue(page_numbers)); |
| 246 headers_and_footers_.reset( |
| 247 new base::FundamentalValue(headers_and_footers)); |
| 248 background_colors_and_images_.reset( |
| 249 new base::FundamentalValue(background_colors_and_images)); |
| 250 margins_.reset(new base::FundamentalValue(margins)); |
| 251 } |
| 252 |
| 253 |
| 254 void PrintPreviewObserver::ManipulatePreviewSettings(State state) { |
| 255 std::vector<const base::Value*> args; |
| 256 if (state == kWaitingToSendSaveAsPdf) { |
| 257 message_name_.reset(new base::StringValue("SAVE_AS_PDF")); |
| 258 args.push_back(message_name_.get()); |
| 259 } else if (state == kWaitingToSendLayoutSettings) { |
| 260 message_name_.reset(new base::StringValue("LAYOUT_SETTINGS")); |
| 261 args.push_back(message_name_.get()); |
| 262 args.push_back(is_portrait_.get()); |
| 263 } else if (state == kWaitingToSendPageNumbers) { |
| 264 message_name_.reset(new base::StringValue("PAGE_NUMBERS")); |
| 265 args.push_back(message_name_.get()); |
| 266 args.push_back(page_numbers_.get()); |
| 267 } else if (state == kWaitingToSendHeadersAndFooters) { |
| 268 message_name_.reset(new base::StringValue("HEADERS_AND_FOOTERS")); |
| 269 args.push_back(message_name_.get()); |
| 270 args.push_back(headers_and_footers_.get()); |
| 271 } else if (state == kWaitingToSendBackgroundColorsAndImages) { |
| 272 message_name_.reset(new base::StringValue("BACKGROUND_COLORS_AND_IMAGES")); |
| 273 args.push_back(message_name_.get()); |
| 274 args.push_back(background_colors_and_images_.get()); |
| 275 } else if (state == kWaitingToSendMargins) { |
| 276 message_name_.reset(new base::StringValue("MARGINS")); |
| 277 args.push_back(message_name_.get()); |
| 278 args.push_back(margins_.get()); |
| 279 } |
| 280 DCHECK(!args.empty()); |
| 281 ui_->web_ui()->CallJavascriptFunction("onManipulateSettingsForTest", args); |
| 282 } |
| 283 |
| 284 void PrintPreviewObserver::DidCloneToNewWebContents( |
| 285 WebContents* old_web_contents, |
| 286 WebContents* new_web_contents) |
| 287 OVERRIDE { |
| 288 this->Observe(new_web_contents); |
| 289 } |
| 290 |
| 291 void PrintPreviewObserver::WebContentsDestroyed() { |
| 292 this->EndLoop(); |
| 293 } |
| 294 |
| 295 class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { |
| 296 public: |
| 297 PrintPreviewPdfGeneratedBrowserTest() {} |
| 298 virtual ~PrintPreviewPdfGeneratedBrowserTest() {} |
| 299 |
| 300 void NavigateAndPreview(std::string file_name) { |
| 301 base::FilePath directory("printing"); |
| 302 base::FilePath file(file_name); |
| 303 |
| 304 ui_test_utils::NavigateToURL(browser(), |
| 305 ui_test_utils::GetTestUrl(directory, file)); |
| 306 |
| 307 base::RunLoop loop; |
| 308 print_preview_observer_->set_quit_closure(loop.QuitClosure()); |
| 309 chrome::Print(browser()); |
| 310 loop.Run(); |
| 311 } |
| 312 |
| 313 void Print() { |
| 314 base::FilePath pdf_file_save_path; |
| 315 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &pdf_file_save_path)); |
| 316 pdf_file_save_path = pdf_file_save_path.AppendASCII("printing"); |
| 317 pdf_file_save_path = pdf_file_save_path.AppendASCII("dummy.pdf"); |
| 318 |
| 319 base::RunLoop loop; |
| 320 print_preview_observer_->set_quit_closure(loop.QuitClosure()); |
| 321 |
| 322 print_preview_observer_->GetUI()->handler_-> |
| 323 FileSelected(pdf_file_save_path, 0, NULL); |
| 324 |
| 325 loop.Run(); |
| 326 } |
| 327 |
| 328 private: |
| 329 virtual void SetUpOnMainThread() OVERRIDE { |
| 330 WebContents* tab = |
| 331 browser()->tab_strip_model()->GetActiveWebContents(); |
| 332 ASSERT_TRUE(tab); |
| 333 |
| 334 print_preview_observer_.reset(new PrintPreviewObserver(tab, browser())); |
| 335 chrome::DuplicateTab(browser()); |
| 336 |
| 337 WebContents* initiator_ = |
| 338 browser()->tab_strip_model()->GetActiveWebContents(); |
| 339 ASSERT_TRUE(initiator_); |
| 340 ASSERT_NE(tab, initiator_); |
| 341 } |
| 342 |
| 343 virtual void CleanUpOnMainThread() OVERRIDE { |
| 344 print_preview_observer_.reset(); |
| 345 } |
| 346 |
| 347 scoped_ptr<PrintPreviewObserver> print_preview_observer_; |
| 348 |
| 349 DISALLOW_COPY_AND_ASSIGN(PrintPreviewPdfGeneratedBrowserTest); |
| 350 }; |
| 351 |
| 352 IN_PROC_BROWSER_TEST_F(PrintPreviewPdfGeneratedBrowserTest, DummyTest) { |
| 353 NavigateAndPreview("test2.html"); |
| 354 Print(); |
| 355 } |
OLD | NEW |