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..3070eeadd573d1f0c41cca6d7e9e83e1f872c9c6 |
| --- /dev/null |
| +++ b/chrome/browser/printing/print_preview_pdf_generated_browsertest.cc |
| @@ -0,0 +1,353 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// 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 "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; |
| + |
| +// 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.
|
| + |
| +// 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 { |
|
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
|
| + // Waiting for the first message so the program can select Save as PDF |
| + WAITING_TO_SEND_SAVE_AS_PDF = 0, |
| + // Waiting for the second message so the test can set the layout |
| + WAITING_TO_SEND_LAYOUT_SETTINGS = 1, |
| + // Waiting for the third message so the test can set the page numbers |
| + WAITING_TO_SEND_PAGE_NUMBERS = 2, |
| + // Waiting for the forth message so the test can set the headers checkbox |
| + WAITING_TO_SEND_HEADERS_AND_FOOTERS = 3, |
| + // Waiting for the fifth message so the test can set the background checkbox |
| + WAITING_TO_SEND_BACKGROUND_COLORS_AND_IMAGES = 4, |
| + // Waiting for the sixth message so the test can set the margins combobox |
| + WAITING_TO_SEND_MARGINS = 5, |
| + // Waiting for the final message so the program can save to PDF. |
| + WAITING_FOR_FINAL_MESSAGE = 6 |
| +}; |
| + |
| +class PrintPreviewObserver : public WebContentsObserver { |
| + public: |
| + explicit PrintPreviewObserver(WebContents* dialog, Browser* browser); |
| + virtual ~PrintPreviewObserver(); |
| + |
| + void set_quit_closure(const base::Closure &closure); |
| + void EndLoop(); |
| + bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| + PrintPreviewUI* GetUI() { |
| + return ui_; |
| + } |
| + |
| + 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(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
|
| + |
| + // 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. |
| + |
| + // 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.
|
| + // custom. Don't use custom. |
| + 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.
|
| + std::string page_numbers = "", |
| + bool headers_and_footers = true, |
| + bool background_colors_and_images = false, |
| + int margins = 0); |
| + |
| + private: |
| + void OnDidGetPreviewPageCount( |
| + const PrintHostMsg_DidGetPreviewPageCount_Params ¶ms); |
| + |
| + void DidCloneToNewWebContents(WebContents* old_web_contents, |
| + WebContents* new_web_contents) |
| + OVERRIDE; |
| + |
| + void WebContentsDestroyed(); |
| + |
| + Browser* browser_; |
| + base::Closure closure_; |
| + WebContents* web_contents_; |
| + |
| + PrintPreviewUI* ui_; |
| + |
| + int count_; |
| + |
| + scoped_ptr<PrintPreviewObserver> print_preview_observer_; |
| + scoped_ptr<base::StringValue> message_name_; |
| + scoped_ptr<base::StringValue> layout_settings_; |
| + scoped_ptr<base::StringValue> page_numbers_; |
| + scoped_ptr<base::FundamentalValue> headers_and_footers_; |
| + scoped_ptr<base::FundamentalValue> background_colors_and_images_; |
| + scoped_ptr<base::FundamentalValue> margins_; |
| + |
|
ivandavid
2014/06/17 21:38:18
I save all the settings in the class, because I wa
|
| + DISALLOW_COPY_AND_ASSIGN(PrintPreviewObserver); |
| +}; |
| + |
| +class UIDoneLoadingMessageHandler : public content::WebUIMessageHandler { |
| + public: |
| + explicit UIDoneLoadingMessageHandler(PrintPreviewObserver* observer); |
| + virtual ~UIDoneLoadingMessageHandler(); |
| + void HandleDone(const base::ListValue* /* args */); |
| + void RegisterMessages() OVERRIDE; |
| + private: |
| + PrintPreviewObserver* observer_; |
| + state state_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(UIDoneLoadingMessageHandler); |
| +}; |
| + |
| +UIDoneLoadingMessageHandler::UIDoneLoadingMessageHandler( |
| + PrintPreviewObserver* observer) : |
| + observer_(observer), state_(WAITING_TO_SEND_SAVE_AS_PDF) {} |
| + |
| +UIDoneLoadingMessageHandler::~UIDoneLoadingMessageHandler() { |
| + observer_ = NULL; |
| +} |
| + |
| +void UIDoneLoadingMessageHandler::RegisterMessages() OVERRIDE { |
| + web_ui()->RegisterMessageCallback( |
| + "UILoaded", |
| + base::Bind(&UIDoneLoadingMessageHandler::HandleDone, |
| + base::Unretained(this))); |
| +} |
| + |
| +void UIDoneLoadingMessageHandler::HandleDone( |
| + const base::ListValue* /* args */) { |
| + if (state_ == WAITING_FOR_FINAL_MESSAGE) { |
| + observer_->EndLoop(); |
| + } else { |
| + observer_->ManipulatePreviewSettings(state_); |
| + 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
|
| + } |
| +} |
| + |
| +PrintPreviewObserver::PrintPreviewObserver( |
| + WebContents* dialog, |
| + Browser* browser) : |
| + WebContentsObserver(dialog), browser_(browser), count_(0) { |
| + SetPrintPreviewSettings("LANDSCAPE", "", false, false, 2); |
| +} |
|
ivandavid
2014/06/17 21:38:18
I change most of the settings to show that it will
|
| + |
| +PrintPreviewObserver::~PrintPreviewObserver() { |
| + browser_ = NULL; |
| + message_name_.reset(); |
| + layout_settings_.reset(); |
| + page_numbers_.reset(); |
| + headers_and_footers_.reset(); |
| + background_colors_and_images_.reset(); |
| + margins_.reset(); |
| +} |
| + |
| +void PrintPreviewObserver::set_quit_closure(const base::Closure &closure) { |
| + closure_ = closure; |
| +} |
| + |
| +void PrintPreviewObserver::EndLoop() { |
| + base::MessageLoop::current()->PostTask(FROM_HERE, closure_); |
| +} |
| + |
| +bool PrintPreviewObserver::OnMessageReceived(const IPC::Message& message) |
| + OVERRIDE { |
| + IPC_BEGIN_MESSAGE_MAP(PrintPreviewObserver, message) |
| + IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, |
| + OnDidGetPreviewPageCount) |
| + IPC_MESSAGE_UNHANDLED(break;) |
| + IPC_END_MESSAGE_MAP(); |
| + return false; |
| +} |
| + |
| +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_ && printing::PrintPreviewDialogController:: |
| + IsPrintPreviewDialog(web_contents_)); |
| + |
| + ui_ = static_cast<PrintPreviewUI*>(web_contents_->GetWebUI()-> |
| + GetController()); |
| + |
| + ASSERT_TRUE(ui_); |
| + |
| + ASSERT_TRUE(ui_->web_ui()); |
| + |
| + this->Observe(web_contents_); |
| + |
| + UIDoneLoadingMessageHandler* handler = new UIDoneLoadingMessageHandler(this); |
| + |
| + ui_->web_ui()->AddMessageHandler(handler); |
| +} |
| + |
| +void PrintPreviewObserver:: |
|
Lei Zhang
2014/06/17 21:57:32
style:
void PrintPreviewObserver::SetPrintPreview
|
| + 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.
|
| + std::string page_numbers, |
| + bool headers_and_footers, |
| + bool background_colors_and_images, |
| + int margins) { |
| + layout_settings_.reset(new base::StringValue(layout_settings)); |
|
Lei Zhang
2014/06/17 21:57:33
style: indentation
|
| + page_numbers_.reset(new base::StringValue(page_numbers)); |
| + headers_and_footers_.reset( |
| + new base::FundamentalValue(headers_and_footers)); |
| + background_colors_and_images_.reset( |
| + new base::FundamentalValue(background_colors_and_images)); |
| + margins_.reset(new base::FundamentalValue(margins)); |
| +} |
| + |
| + |
| +void PrintPreviewObserver::ManipulatePreviewSettings(enum state state) { |
| + std::vector<const base::Value*> args; |
| + if (state == WAITING_TO_SEND_SAVE_AS_PDF) { |
| + message_name_.reset(new base::StringValue("SAVE_AS_PDF")); |
| + args.push_back(message_name_.get()); |
| + 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.
|
| + } else if (state == WAITING_TO_SEND_LAYOUT_SETTINGS) { |
| + message_name_.reset(new base::StringValue("LAYOUT_SETTINGS")); |
| + args.push_back(message_name_.get()); |
| + args.push_back(layout_settings_.get()); |
| + ui_->web_ui()->CallJavascriptFunction("onManipulateSettings", args); |
| + } else if (state == WAITING_TO_SEND_PAGE_NUMBERS) { |
| + message_name_.reset(new base::StringValue("PAGE_NUMBERS")); |
| + args.push_back(message_name_.get()); |
| + args.push_back(page_numbers_.get()); |
| + ui_->web_ui()->CallJavascriptFunction("onManipulateSettings", args); |
| + } else if (state == WAITING_TO_SEND_HEADERS_AND_FOOTERS) { |
| + message_name_.reset(new base::StringValue("HEADERS_AND_FOOTERS")); |
| + args.push_back(message_name_.get()); |
| + args.push_back(headers_and_footers_.get()); |
| + ui_->web_ui()->CallJavascriptFunction("onManipulateSettings", args); |
| + } else if (state == WAITING_TO_SEND_BACKGROUND_COLORS_AND_IMAGES) { |
| + message_name_.reset(new base::StringValue("BACKGROUND_COLORS_AND_IMAGES")); |
| + args.push_back(message_name_.get()); |
| + args.push_back(background_colors_and_images_.get()); |
| + ui_->web_ui()->CallJavascriptFunction("onManipulateSettings", args); |
| + } else if (state == WAITING_TO_SEND_MARGINS) { |
| + message_name_.reset(new base::StringValue("MARGINS")); |
| + args.push_back(message_name_.get()); |
| + args.push_back(margins_.get()); |
| + ui_->web_ui()->CallJavascriptFunction("onManipulateSettings", args); |
| + } |
| +} |
| + |
| +void PrintPreviewObserver::DidCloneToNewWebContents( |
| + WebContents* old_web_contents, |
| + WebContents* new_web_contents) |
| + OVERRIDE { |
| + this->Observe(new_web_contents); |
| +} |
| + |
| +void PrintPreviewObserver::WebContentsDestroyed() { |
| + this->EndLoop(); |
| +} |
| + |
| +class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { |
| + public: |
| + PrintPreviewPdfGeneratedBrowserTest() {} |
| + virtual ~PrintPreviewPdfGeneratedBrowserTest() {} |
| + |
| + void NavigateAndPreview(std::string file_name) { |
| + base::FilePath directory("printing"); |
| + base::FilePath file(file_name); |
| + |
| + 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; |
|
ivandavid
2014/06/17 21:38:18
Changed the variable to your suggestion.
|
| + ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &pdf_file_save_path)); |
| + pdf_file_save_path = pdf_file_save_path.AppendASCII("printing"); |
| + pdf_file_save_path = pdf_file_save_path.AppendASCII("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(); |
| +} |