Chromium Code Reviews| Index: chrome/browser/printing/print_preview_pdf_generated_browsertest.cc |
| diff --git a/chrome/browser/printing/print_preview_pdf_generated_browsertest.cc b/chrome/browser/printing/print_preview_pdf_generated_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..643bd3f772270b272d773991cc12434c44381094 |
| --- /dev/null |
| +++ b/chrome/browser/printing/print_preview_pdf_generated_browsertest.cc |
| @@ -0,0 +1,304 @@ |
| +// 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.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/files/file_path.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/path_service.h" |
| +#include "base/run_loop.h" |
| +#include "base/strings/string_util.h" |
| +#include "chrome/app/chrome_command_ids.h" |
| +#include "chrome/browser/net/referrer.h" |
| +#include "chrome/browser/printing/print_preview_dialog_controller.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_commands.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "chrome/browser/ui/webui/print_preview/print_preview_handler.h" |
| +#include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
| +#include "chrome/browser/ui/webui/print_preview/sticky_settings.h" |
| +#include "chrome/common/chrome_paths.h" |
| +#include "chrome/common/print_messages.h" |
| +#include "chrome/common/url_constants.h" |
| +#include "chrome/test/base/in_process_browser_test.h" |
| +#include "chrome/test/base/ui_test_utils.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/browser/web_ui_message_handler.h" |
| +#include "content/public/common/page_transition_types.h" |
| +#include "content/public/test/browser_test_utils.h" |
| +#include "content/public/test/test_navigation_observer.h" |
| +#include "content/public/test/test_utils.h" |
| +#include "printing/print_job_constants.h" |
| +#include "ui/events/keycodes/keyboard_codes.h" |
| +#include "url/gurl.h" |
| +#include "ipc/ipc_message_macros.h" |
| + |
| +using content::WebContents; |
| +using content::WebContentsObserver; |
| + |
| +class PrintPreviewObserver; |
| +class UIDoneLoadingMessageHandler; |
| + |
| +// Message refers to the 'UILoaded' message from print_preview.js. |
| +// It gets sent either from onPreviewGenerationDone or from |
| +// onManipulateSettings_() in print_preview.js |
| +enum State { |
| + // Waiting for the first message so the program can select Save as PDF |
| + kWaitingToSendSaveAsPdf = 0, |
| + // Waiting for the second message so the test can set the layout |
| + kWaitingToSendLayoutSettings = 1, |
| + // Waiting for the third message so the test can set the page numbers |
| + kWaitingToSendPageNumbers = 2, |
| + // Waiting for the forth message so the test can set the headers checkbox |
| + kWaitingToSendHeadersAndFooters = 3, |
| + // Waiting for the fifth message so the test can set the background checkbox |
| + kWaitingToSendBackgroundColorsAndImages = 4, |
| + // Waiting for the sixth message so the test can set the margins combobox |
| + kWaitingToSendMargins = 5, |
| + // Waiting for the final message so the program can save to PDF. |
| + 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.
|
| +}; |
| + |
| +class PrintPreviewObserver : public WebContentsObserver { |
| + public: |
| + //explicit PrintPreviewObserver(WebContents* dialog, Browser* browser); |
|
Lei Zhang
2014/06/20 21:41:36
delete?
ivandavid
2014/06/24 18:49:52
Done.
|
| + 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.
|
| + WebContentsObserver(dialog), browser_(browser), count_(0) { |
| + 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.
|
| + "", |
| + false, |
| + false, |
| + printing::DEFAULT_MARGINS); |
| + } |
|
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.
|
| + virtual ~PrintPreviewObserver() { |
| + browser_ = NULL; |
|
Lei Zhang
2014/06/20 21:41:36
Do we need this?
ivandavid
2014/06/24 18:49:52
Done.
|
| + } |
| + |
| + // Sets closure for the observer so that it can end the loop. |
| + void set_quit_closure(const base::Closure &closure) { |
| + closure_ = closure; |
| + } |
| + // Actually stops the message_loop so that the test can proceed. |
| + void EndLoop() { |
| + base::MessageLoop::current()->PostTask(FROM_HERE, closure_); |
| + } |
| + 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.
|
| + IPC_BEGIN_MESSAGE_MAP(PrintPreviewObserver, message) |
| + IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, |
| + OnDidGetPreviewPageCount) |
| + IPC_MESSAGE_UNHANDLED(return false;) |
| + IPC_END_MESSAGE_MAP(); |
| + return false; |
| + } |
| + // Gets the PrintPreviewUI so that certain elements can be accessed. |
| + PrintPreviewUI* GetUI() { |
| + return ui_; |
| + } |
| + // Returns the web_contents of the print preview dialog. |
| + WebContents* GetPreviewContents() { |
| + return web_contents_; |
| + } |
| + |
| + // Calls a javascript function that will change the print preview settings, |
| + // such as the layout, the margins, page numbers, etc. |
| + void ManipulatePreviewSettings(State state) { |
| + std::vector<const base::Value*> args; |
| + script_argument_.reset(new base::DictionaryValue()); |
| + if (state == kWaitingToSendSaveAsPdf) |
| + script_argument_->SetBoolean("selectSaveAsPdfDestination", true); |
| + else if (state == kWaitingToSendLayoutSettings) |
| + script_argument_->SetBoolean("layoutSettings.portrait", is_portrait_); |
| + else if (state == kWaitingToSendPageNumbers) |
| + script_argument_->SetString("pageRange", page_numbers_); |
| + else if (state == kWaitingToSendHeadersAndFooters) |
| + script_argument_->SetBoolean("headersAndFooters", headers_and_footers_); |
| + else if (state == kWaitingToSendBackgroundColorsAndImages) |
| + 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.
|
| + background_colors_and_images_); |
| + else if (state == kWaitingToSendMargins) |
| + script_argument_->SetInteger("margins", margins_); |
| + |
| + args.push_back(script_argument_.get()); |
| + DCHECK(!script_argument_->empty()); |
| + DCHECK(!args.empty()); |
| + ui_->web_ui()->CallJavascriptFunction("onManipulateSettingsForTest", args); |
| + } |
| + |
| + // Function to set the print preview settings and save them so they |
| + // can be sent later. Currently only used in the constructor. Will be |
| + // used when creating a test and take command line arguments. |
| + // |page_numbers| is a comma separated page range. |
| + // Example: "1-5,9" will print pages 1 through 5 and page 9. |
| + // The pages specified must be less than or equal to the maximum |
| + // page number. An empty string seems to be valid input, however |
| + // further testing will be required to see if that is actually |
| + // true. |
| + void SetPrintPreviewSettings(bool is_portrait, |
| + const std::string& page_numbers, |
| + bool headers_and_footers, |
| + bool background_colors_and_images, |
| + printing::MarginType margins) { |
| + is_portrait_ = is_portrait; |
| + page_numbers_ = page_numbers; |
| + headers_and_footers_ = headers_and_footers; |
| + background_colors_and_images_ = background_colors_and_images; |
| + margins_ = margins; |
| + } |
| + |
| + private: |
| + // Called when the observer gets the IPC message stating that the |
| + // page count is ready. |
| + void OnDidGetPreviewPageCount( |
| + const PrintHostMsg_DidGetPreviewPageCount_Params ¶ms); |
| + |
| + void DidCloneToNewWebContents(WebContents* old_web_contents, |
| + WebContents* new_web_contents) |
| + OVERRIDE { |
| + Observe(new_web_contents); |
|
Lei Zhang
2014/06/20 21:41:36
You'd actually only want to do this once right? Th
|
| + } |
| + |
| + void WebContentsDestroyed() OVERRIDE { |
| + EndLoop(); |
| + } |
| + |
| + Browser* browser_; |
| + base::Closure closure_; |
| + WebContents* web_contents_; |
| + |
| + PrintPreviewUI* ui_; |
| + |
| + int count_; |
|
Lei Zhang
2014/06/20 21:41:36
not used?
ivandavid
2014/06/24 18:49:51
Done.
|
| + |
| + scoped_ptr<PrintPreviewObserver> print_preview_observer_; |
| + scoped_ptr<base::DictionaryValue> script_argument_; |
| + bool is_portrait_; |
| + std::string page_numbers_; |
| + bool headers_and_footers_; |
| + bool background_colors_and_images_; |
| + 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
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(PrintPreviewObserver); |
| +}; |
| + |
| +class UIDoneLoadingMessageHandler : public content::WebUIMessageHandler { |
| + public: |
| + explicit UIDoneLoadingMessageHandler(PrintPreviewObserver* observer) : |
| + observer_(observer), state_(kWaitingToSendSaveAsPdf) {} |
| + virtual ~UIDoneLoadingMessageHandler() { |
| + observer_ = NULL; |
| + } |
| + void HandleDone(const base::ListValue* /* args */) { |
| + if (state_ == kWaitingForFinalMessage) { |
| + observer_->EndLoop(); |
| + } else { |
| + observer_->ManipulatePreviewSettings(state_); |
| + state_ = static_cast<State>(static_cast<int>(state_) + 1); |
| + } |
| + } |
| + void HandleFailure(const base::ListValue* /* args */) { |
| + FAIL(); |
| + } |
| + void RegisterMessages() OVERRIDE { |
| + web_ui()->RegisterMessageCallback( |
| + "UILoadedForTest", |
| + base::Bind(&UIDoneLoadingMessageHandler::HandleDone, |
| + base::Unretained(this))); |
| + |
| + web_ui()->RegisterMessageCallback( |
| + "UIFailedLoadingForTest", |
| + base::Bind(&UIDoneLoadingMessageHandler::HandleFailure, |
| + base::Unretained(this))); |
| + } |
|
Dan Beam
2014/06/21 02:44:11
like Lei mentioned, this code would be easier to r
|
| + private: |
| + PrintPreviewObserver* observer_; |
| + State state_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(UIDoneLoadingMessageHandler); |
| +}; |
| + |
| +void PrintPreviewObserver::OnDidGetPreviewPageCount( |
| + const PrintHostMsg_DidGetPreviewPageCount_Params ¶ms) { |
| + |
| + WebContents* tab = browser_->tab_strip_model()->GetActiveWebContents(); |
| + ASSERT_TRUE(tab); |
| + printing::PrintPreviewDialogController* dialog_controller = |
| + printing::PrintPreviewDialogController::GetInstance(); |
| + ASSERT_TRUE(dialog_controller); |
| + web_contents_ = dialog_controller->GetPrintPreviewForContents(tab); |
| + ASSERT_TRUE(web_contents_); |
| + ASSERT_TRUE(printing::PrintPreviewDialogController:: |
| + IsPrintPreviewDialog(web_contents_)); |
| + ui_ = static_cast<PrintPreviewUI*>(web_contents_->GetWebUI()-> |
| + GetController()); |
| + ASSERT_TRUE(ui_); |
| + ASSERT_TRUE(ui_->web_ui()); |
| + ui_->web_ui()->CallJavascriptFunction("onEnableManipulateSettingsForTest"); |
| + Observe(web_contents_); |
| + UIDoneLoadingMessageHandler* handler = new UIDoneLoadingMessageHandler(this); |
| + ui_->web_ui()->AddMessageHandler(handler); |
| +} |
| + |
| +class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { |
| + public: |
| + PrintPreviewPdfGeneratedBrowserTest() {} |
| + virtual ~PrintPreviewPdfGeneratedBrowserTest() {} |
| + |
| + void NavigateAndPreview(std::string file_name) { |
| + base::FilePath directory(FILE_PATH_LITERAL("printing")); |
| + 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
|
| + ui_test_utils::NavigateToURL(browser(), |
| + ui_test_utils::GetTestUrl(directory, file)); |
| + |
| + base::RunLoop loop; |
| + print_preview_observer_->set_quit_closure(loop.QuitClosure()); |
| + chrome::Print(browser()); |
| + loop.Run(); |
| + } |
| + |
| + void Print() { |
| + base::FilePath pdf_file_save_path; |
| + ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &pdf_file_save_path)); |
| + pdf_file_save_path = pdf_file_save_path.AppendASCII( |
| + 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.
|
| + pdf_file_save_path = pdf_file_save_path.AppendASCII( |
| + FILE_PATH_LITERAL("dummy.pdf")); |
| + |
| + base::RunLoop loop; |
| + print_preview_observer_->set_quit_closure(loop.QuitClosure()); |
| + print_preview_observer_->GetUI()->handler_-> |
| + FileSelected(pdf_file_save_path, 0, NULL); |
| + loop.Run(); |
| + } |
| + |
| + private: |
| + virtual void SetUpOnMainThread() OVERRIDE { |
| + WebContents* tab = |
| + browser()->tab_strip_model()->GetActiveWebContents(); |
| + ASSERT_TRUE(tab); |
| + |
| + print_preview_observer_.reset(new PrintPreviewObserver(tab, browser())); |
| + chrome::DuplicateTab(browser()); |
| + |
| + WebContents* initiator_ = |
| + browser()->tab_strip_model()->GetActiveWebContents(); |
| + ASSERT_TRUE(initiator_); |
| + ASSERT_NE(tab, initiator_); |
| + } |
| + |
| + virtual void CleanUpOnMainThread() OVERRIDE { |
| + print_preview_observer_.reset(); |
| + } |
| + |
| + scoped_ptr<PrintPreviewObserver> print_preview_observer_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PrintPreviewPdfGeneratedBrowserTest); |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(PrintPreviewPdfGeneratedBrowserTest, DummyTest) { |
| + NavigateAndPreview("test2.html"); |
| + Print(); |
| +} |