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