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..18126f77fd01bf151021ef0c8f161ecabcb90780 |
| --- /dev/null |
| +++ b/chrome/browser/printing/print_preview_pdf_generated_browsertest.cc |
| @@ -0,0 +1,251 @@ |
| +// 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 "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/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 |
| + */ |
| + |
| +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_; |
| + } |
| + |
| + 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_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PrintPreviewObserver); |
| +}; |
| + |
| +class UIDoneLoadingMessageHandler : public content::WebUIMessageHandler { |
| + public: |
| + UIDoneLoadingMessageHandler(PrintPreviewObserver * observer); |
| + virtual ~UIDoneLoadingMessageHandler(); |
| + void HandleDone(const base::ListValue * /* args */); |
| + void RegisterMessages() OVERRIDE; |
| + private: |
| + PrintPreviewObserver * observer_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(UIDoneLoadingMessageHandler); |
| +}; |
| + |
| +UIDoneLoadingMessageHandler::UIDoneLoadingMessageHandler( |
| + PrintPreviewObserver * observer) : |
| + observer_(observer) {} |
| + |
| +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 */) { |
| + observer_->EndLoop(); |
| +} |
| + |
| +PrintPreviewObserver::PrintPreviewObserver( |
| + WebContents * dialog, |
| + Browser * browser) : |
| + WebContentsObserver(dialog), browser_(browser), count_(0) {} |
| + |
| +PrintPreviewObserver::~PrintPreviewObserver() { |
| + browser_ = NULL; |
| +} |
| + |
| +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::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_.get()->set_quit_closure(loop.QuitClosure()); |
| + chrome::Print(browser()); |
| + loop.Run(); |
| + } |
| + |
| + void Print() { |
| + base::FilePath test_dir; |
| + ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &test_dir)); |
|
Lei Zhang
2014/06/13 22:20:11
You probably want chrome::DIR_TEST_DATA and not ba
|
| + test_dir = test_dir.AppendASCII("printing"); |
| + test_dir = test_dir.AppendASCII("dummy.pdf"); |
| + |
| + base::RunLoop loop; |
| + print_preview_observer_.get()->set_quit_closure(loop.QuitClosure()); |
| + |
| + print_preview_observer_.get()->GetUI()->handler_->print_to_pdf_path_ = |
|
Lei Zhang
2014/06/13 22:20:11
Maybe just call PrintPreviewHandler::FileSelected(
|
| + test_dir; |
| + |
| + print_preview_observer_.get()->GetUI()->handler_->PrintToPdf(); |
| + |
| + 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()); |
|
Lei Zhang
2014/06/13 22:20:10
Why duplicate the tab?
|
| + |
| + 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); |
| +}; |
| + |
| +/* when we get the 'UILoaded' message, we should send a message to the UI |
| + to actually press stuff. Then after stuff has been pressed, another message |
| + should come that tells the program that everything has been pressed and |
| + the pdf has been saved somewhere, and comparisons can start */ |
| + |
| +IN_PROC_BROWSER_TEST_F(PrintPreviewPdfGeneratedBrowserTest, DummyTest) { |
| + NavigateAndPreview("test2.html"); |
| + Print(); |
| +} |