Chromium Code Reviews| 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 "ui/events/keycodes/keyboard_codes.h" | |
| 36 #include "url/gurl.h" | |
| 37 #include "ipc/ipc_message_macros.h" | |
| 38 | |
| 39 using content::WebContents; | |
| 40 using content::WebContentsObserver; | |
| 41 | |
| 42 class PrintPreviewObserver; | |
| 43 class UIDoneLoadingMessageHandler; | |
| 44 | |
| 45 // Need to split declarations & definitions due to forward declaration problem | |
|
Lei Zhang
2014/06/17 21:57:32
You don't need to state this. It's fine to split t
ivandavid
2014/06/17 22:59:15
I removed the comment.
| |
| 46 | |
| 47 // Message refers to the 'UILoaded' message from print_preview.js. | |
| 48 // It gets sent either from onPreviewGenerationDone or from | |
| 49 // onManipulateSettings_() in print_preview.js | |
| 50 enum state { | |
|
Lei Zhang
2014/06/17 21:57:32
style: State, http://google-styleguide.googlecode.
ivandavid
2014/06/17 22:59:16
Made the enums look like constants rather than mac
Lei Zhang
2014/06/17 23:22:20
Oh, the previous MACRO_NAME style is actually pref
| |
| 51 // Waiting for the first message so the program can select Save as PDF | |
| 52 WAITING_TO_SEND_SAVE_AS_PDF = 0, | |
| 53 // Waiting for the second message so the test can set the layout | |
| 54 WAITING_TO_SEND_LAYOUT_SETTINGS = 1, | |
| 55 // Waiting for the third message so the test can set the page numbers | |
| 56 WAITING_TO_SEND_PAGE_NUMBERS = 2, | |
| 57 // Waiting for the forth message so the test can set the headers checkbox | |
| 58 WAITING_TO_SEND_HEADERS_AND_FOOTERS = 3, | |
| 59 // Waiting for the fifth message so the test can set the background checkbox | |
| 60 WAITING_TO_SEND_BACKGROUND_COLORS_AND_IMAGES = 4, | |
| 61 // Waiting for the sixth message so the test can set the margins combobox | |
| 62 WAITING_TO_SEND_MARGINS = 5, | |
| 63 // Waiting for the final message so the program can save to PDF. | |
| 64 WAITING_FOR_FINAL_MESSAGE = 6 | |
| 65 }; | |
| 66 | |
| 67 class PrintPreviewObserver : public WebContentsObserver { | |
| 68 public: | |
| 69 explicit PrintPreviewObserver(WebContents* dialog, Browser* browser); | |
| 70 virtual ~PrintPreviewObserver(); | |
| 71 | |
| 72 void set_quit_closure(const base::Closure &closure); | |
| 73 void EndLoop(); | |
| 74 bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
| 75 PrintPreviewUI* GetUI() { | |
| 76 return ui_; | |
| 77 } | |
| 78 | |
| 79 WebContents* GetPreviewContents() { | |
| 80 return web_contents_; | |
| 81 } | |
| 82 | |
| 83 // Calls a javascript function that will change the print preview settings, | |
| 84 // such as the layout, the margins, page numbers, etc. | |
| 85 void ManipulatePreviewSettings(enum state); | |
|
Lei Zhang
2014/06/17 21:57:33
enum state -> State state
ivandavid
2014/06/17 22:59:15
Changed everywhere.
Lei Zhang
2014/06/17 23:22:20
You didn't actually change this one. You still hav
| |
| 86 | |
| 87 // Function to set the print preview settings and save them so they | |
| 88 // can be sent later. Currently only used in the constructor. Will be | |
| 89 // used when creating a test and take command line arguments. | |
| 90 | |
| 91 // For margins, 0 is default, 1 is none, 2 is minimum, and 3 is | |
|
Lei Zhang
2014/06/17 21:57:32
There's an enum MarginType in printing/print_job_c
ivandavid
2014/06/17 22:59:16
Change the type to printing::MarginType.
| |
| 92 // custom. Don't use custom. | |
| 93 void SetPrintPreviewSettings(std::string layout_settings = "", | |
|
Lei Zhang
2014/06/17 21:57:33
No default arguments please. http://google-stylegu
ivandavid
2014/06/17 22:59:15
I removed them.
| |
| 94 std::string page_numbers = "", | |
| 95 bool headers_and_footers = true, | |
| 96 bool background_colors_and_images = false, | |
| 97 int margins = 0); | |
| 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::StringValue> layout_settings_; | |
| 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 | |
|
ivandavid
2014/06/17 21:38:18
I save all the settings in the class, because I wa
| |
| 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_(WAITING_TO_SEND_SAVE_AS_PDF) {} | |
| 144 | |
| 145 UIDoneLoadingMessageHandler::~UIDoneLoadingMessageHandler() { | |
| 146 observer_ = NULL; | |
| 147 } | |
| 148 | |
| 149 void UIDoneLoadingMessageHandler::RegisterMessages() OVERRIDE { | |
| 150 web_ui()->RegisterMessageCallback( | |
| 151 "UILoaded", | |
| 152 base::Bind(&UIDoneLoadingMessageHandler::HandleDone, | |
| 153 base::Unretained(this))); | |
| 154 } | |
| 155 | |
| 156 void UIDoneLoadingMessageHandler::HandleDone( | |
| 157 const base::ListValue* /* args */) { | |
| 158 if (state_ == WAITING_FOR_FINAL_MESSAGE) { | |
| 159 observer_->EndLoop(); | |
| 160 } else { | |
| 161 observer_->ManipulatePreviewSettings(state_); | |
| 162 state_ = static_cast<enum state>(static_cast<int>(state_) + 1); | |
|
ivandavid
2014/06/17 21:38:18
I wasn't sure if there was a cleaner way to do thi
| |
| 163 } | |
| 164 } | |
| 165 | |
| 166 PrintPreviewObserver::PrintPreviewObserver( | |
| 167 WebContents* dialog, | |
| 168 Browser* browser) : | |
| 169 WebContentsObserver(dialog), browser_(browser), count_(0) { | |
| 170 SetPrintPreviewSettings("LANDSCAPE", "", false, false, 2); | |
| 171 } | |
|
ivandavid
2014/06/17 21:38:18
I change most of the settings to show that it will
| |
| 172 | |
| 173 PrintPreviewObserver::~PrintPreviewObserver() { | |
| 174 browser_ = NULL; | |
| 175 message_name_.reset(); | |
| 176 layout_settings_.reset(); | |
| 177 page_numbers_.reset(); | |
| 178 headers_and_footers_.reset(); | |
| 179 background_colors_and_images_.reset(); | |
| 180 margins_.reset(); | |
| 181 } | |
| 182 | |
| 183 void PrintPreviewObserver::set_quit_closure(const base::Closure &closure) { | |
| 184 closure_ = closure; | |
| 185 } | |
| 186 | |
| 187 void PrintPreviewObserver::EndLoop() { | |
| 188 base::MessageLoop::current()->PostTask(FROM_HERE, closure_); | |
| 189 } | |
| 190 | |
| 191 bool PrintPreviewObserver::OnMessageReceived(const IPC::Message& message) | |
| 192 OVERRIDE { | |
| 193 IPC_BEGIN_MESSAGE_MAP(PrintPreviewObserver, message) | |
| 194 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, | |
| 195 OnDidGetPreviewPageCount) | |
| 196 IPC_MESSAGE_UNHANDLED(break;) | |
| 197 IPC_END_MESSAGE_MAP(); | |
| 198 return false; | |
| 199 } | |
| 200 | |
| 201 void PrintPreviewObserver::OnDidGetPreviewPageCount( | |
| 202 const PrintHostMsg_DidGetPreviewPageCount_Params ¶ms) { | |
| 203 | |
| 204 WebContents* tab = browser_->tab_strip_model()->GetActiveWebContents(); | |
| 205 | |
| 206 ASSERT_TRUE(tab); | |
| 207 | |
| 208 printing::PrintPreviewDialogController* dialog_controller = | |
| 209 printing::PrintPreviewDialogController::GetInstance(); | |
| 210 | |
| 211 ASSERT_TRUE(dialog_controller); | |
| 212 | |
| 213 web_contents_ = dialog_controller->GetPrintPreviewForContents(tab); | |
| 214 | |
| 215 ASSERT_TRUE(web_contents_ && printing::PrintPreviewDialogController:: | |
| 216 IsPrintPreviewDialog(web_contents_)); | |
| 217 | |
| 218 ui_ = static_cast<PrintPreviewUI*>(web_contents_->GetWebUI()-> | |
| 219 GetController()); | |
| 220 | |
| 221 ASSERT_TRUE(ui_); | |
| 222 | |
| 223 ASSERT_TRUE(ui_->web_ui()); | |
| 224 | |
| 225 this->Observe(web_contents_); | |
| 226 | |
| 227 UIDoneLoadingMessageHandler* handler = new UIDoneLoadingMessageHandler(this); | |
| 228 | |
| 229 ui_->web_ui()->AddMessageHandler(handler); | |
| 230 } | |
| 231 | |
| 232 void PrintPreviewObserver:: | |
|
Lei Zhang
2014/06/17 21:57:32
style:
void PrintPreviewObserver::SetPrintPreview
| |
| 233 SetPrintPreviewSettings(std::string layout_settings, | |
|
Lei Zhang
2014/06/17 21:57:33
non-POD types should be passed by const reference,
ivandavid
2014/06/17 22:59:16
I think I fixed it.
| |
| 234 std::string page_numbers, | |
| 235 bool headers_and_footers, | |
| 236 bool background_colors_and_images, | |
| 237 int margins) { | |
| 238 layout_settings_.reset(new base::StringValue(layout_settings)); | |
|
Lei Zhang
2014/06/17 21:57:33
style: indentation
| |
| 239 page_numbers_.reset(new base::StringValue(page_numbers)); | |
| 240 headers_and_footers_.reset( | |
| 241 new base::FundamentalValue(headers_and_footers)); | |
| 242 background_colors_and_images_.reset( | |
| 243 new base::FundamentalValue(background_colors_and_images)); | |
| 244 margins_.reset(new base::FundamentalValue(margins)); | |
| 245 } | |
| 246 | |
| 247 | |
| 248 void PrintPreviewObserver::ManipulatePreviewSettings(enum state state) { | |
| 249 std::vector<const base::Value*> args; | |
| 250 if (state == WAITING_TO_SEND_SAVE_AS_PDF) { | |
| 251 message_name_.reset(new base::StringValue("SAVE_AS_PDF")); | |
| 252 args.push_back(message_name_.get()); | |
| 253 ui_->web_ui()->CallJavascriptFunction("onManipulateSettings", args); | |
|
Lei Zhang
2014/06/17 21:57:32
Since you are doing the same thing at the end of e
ivandavid
2014/06/17 22:59:16
Done.
| |
| 254 } else if (state == WAITING_TO_SEND_LAYOUT_SETTINGS) { | |
| 255 message_name_.reset(new base::StringValue("LAYOUT_SETTINGS")); | |
| 256 args.push_back(message_name_.get()); | |
| 257 args.push_back(layout_settings_.get()); | |
| 258 ui_->web_ui()->CallJavascriptFunction("onManipulateSettings", args); | |
| 259 } else if (state == WAITING_TO_SEND_PAGE_NUMBERS) { | |
| 260 message_name_.reset(new base::StringValue("PAGE_NUMBERS")); | |
| 261 args.push_back(message_name_.get()); | |
| 262 args.push_back(page_numbers_.get()); | |
| 263 ui_->web_ui()->CallJavascriptFunction("onManipulateSettings", args); | |
| 264 } else if (state == WAITING_TO_SEND_HEADERS_AND_FOOTERS) { | |
| 265 message_name_.reset(new base::StringValue("HEADERS_AND_FOOTERS")); | |
| 266 args.push_back(message_name_.get()); | |
| 267 args.push_back(headers_and_footers_.get()); | |
| 268 ui_->web_ui()->CallJavascriptFunction("onManipulateSettings", args); | |
| 269 } else if (state == WAITING_TO_SEND_BACKGROUND_COLORS_AND_IMAGES) { | |
| 270 message_name_.reset(new base::StringValue("BACKGROUND_COLORS_AND_IMAGES")); | |
| 271 args.push_back(message_name_.get()); | |
| 272 args.push_back(background_colors_and_images_.get()); | |
| 273 ui_->web_ui()->CallJavascriptFunction("onManipulateSettings", args); | |
| 274 } else if (state == WAITING_TO_SEND_MARGINS) { | |
| 275 message_name_.reset(new base::StringValue("MARGINS")); | |
| 276 args.push_back(message_name_.get()); | |
| 277 args.push_back(margins_.get()); | |
| 278 ui_->web_ui()->CallJavascriptFunction("onManipulateSettings", args); | |
| 279 } | |
| 280 } | |
| 281 | |
| 282 void PrintPreviewObserver::DidCloneToNewWebContents( | |
| 283 WebContents* old_web_contents, | |
| 284 WebContents* new_web_contents) | |
| 285 OVERRIDE { | |
| 286 this->Observe(new_web_contents); | |
| 287 } | |
| 288 | |
| 289 void PrintPreviewObserver::WebContentsDestroyed() { | |
| 290 this->EndLoop(); | |
| 291 } | |
| 292 | |
| 293 class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { | |
| 294 public: | |
| 295 PrintPreviewPdfGeneratedBrowserTest() {} | |
| 296 virtual ~PrintPreviewPdfGeneratedBrowserTest() {} | |
| 297 | |
| 298 void NavigateAndPreview(std::string file_name) { | |
| 299 base::FilePath directory("printing"); | |
| 300 base::FilePath file(file_name); | |
| 301 | |
| 302 ui_test_utils::NavigateToURL(browser(), | |
| 303 ui_test_utils::GetTestUrl(directory, file)); | |
| 304 | |
| 305 base::RunLoop loop; | |
| 306 print_preview_observer_->set_quit_closure(loop.QuitClosure()); | |
| 307 chrome::Print(browser()); | |
| 308 loop.Run(); | |
| 309 } | |
| 310 | |
| 311 void Print() { | |
| 312 base::FilePath pdf_file_save_path; | |
|
ivandavid
2014/06/17 21:38:18
Changed the variable to your suggestion.
| |
| 313 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &pdf_file_save_path)); | |
| 314 pdf_file_save_path = pdf_file_save_path.AppendASCII("printing"); | |
| 315 pdf_file_save_path = pdf_file_save_path.AppendASCII("dummy.pdf"); | |
| 316 | |
| 317 base::RunLoop loop; | |
| 318 print_preview_observer_->set_quit_closure(loop.QuitClosure()); | |
| 319 | |
| 320 print_preview_observer_->GetUI()->handler_-> | |
| 321 FileSelected(pdf_file_save_path, 0, NULL); | |
| 322 | |
| 323 loop.Run(); | |
| 324 } | |
| 325 | |
| 326 private: | |
| 327 virtual void SetUpOnMainThread() OVERRIDE { | |
| 328 WebContents* tab = | |
| 329 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 330 ASSERT_TRUE(tab); | |
| 331 | |
| 332 print_preview_observer_.reset(new PrintPreviewObserver(tab, browser())); | |
| 333 chrome::DuplicateTab(browser()); | |
| 334 | |
| 335 WebContents* initiator_ = | |
| 336 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 337 ASSERT_TRUE(initiator_); | |
| 338 ASSERT_NE(tab, initiator_); | |
| 339 } | |
| 340 | |
| 341 virtual void CleanUpOnMainThread() OVERRIDE { | |
| 342 print_preview_observer_.reset(); | |
| 343 } | |
| 344 | |
| 345 scoped_ptr<PrintPreviewObserver> print_preview_observer_; | |
| 346 | |
| 347 DISALLOW_COPY_AND_ASSIGN(PrintPreviewPdfGeneratedBrowserTest); | |
| 348 }; | |
| 349 | |
| 350 IN_PROC_BROWSER_TEST_F(PrintPreviewPdfGeneratedBrowserTest, DummyTest) { | |
| 351 NavigateAndPreview("test2.html"); | |
| 352 Print(); | |
| 353 } | |
| OLD | NEW |