Commit c39cb22d authored by Julie Jeongeun Kim's avatar Julie Jeongeun Kim Committed by Chromium LUCI CQ

[printing] Update printing unit tests without OnMessageReceived

This CL refactors printing unit tests not to use OnMessageReceived
since the printing legacy IPCs will be converted to mojo.
It is a precursor of converting PrintHostMsg_DidStartPreview
and PrintHostMsg_DidGetDefaultPageLayout.

Bug: 1008939
Change-Id: I220076282c95e361d9ab3b8264dc0c754e4017db
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2615736Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatarKevin McNee <mcnee@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Commit-Queue: Julie Kim <jkim@igalia.com>
Cr-Commit-Position: refs/heads/master@{#844033}
parent af641502
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/test/test_timeouts.h" #include "base/test/test_timeouts.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "build/buildflag.h"
#include "build/chromeos_buildflags.h" #include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/extension_apitest.h" #include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
...@@ -46,6 +47,7 @@ ...@@ -46,6 +47,7 @@
#include "net/dns/mock_host_resolver.h" #include "net/dns/mock_host_resolver.h"
#include "net/http/http_response_headers.h" #include "net/http/http_response_headers.h"
#include "net/test/embedded_test_server/embedded_test_server.h" #include "net/test/embedded_test_server/embedded_test_server.h"
#include "printing/buildflags/buildflags.h"
#include "ui/views/accessibility/view_accessibility.h" #include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/webview/webview.h" #include "ui/views/controls/webview/webview.h"
#include "ui/views/view.h" #include "ui/views/view.h"
...@@ -54,6 +56,9 @@ ...@@ -54,6 +56,9 @@
#if defined(USE_AURA) #if defined(USE_AURA)
#include "ui/aura/window.h" #include "ui/aura/window.h"
#endif #endif
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
#include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
#endif
using extensions::ExtensionsAPIClient; using extensions::ExtensionsAPIClient;
using extensions::MimeHandlerViewGuest; using extensions::MimeHandlerViewGuest;
...@@ -184,6 +189,42 @@ class StubDevToolsAgentHostClient : public content::DevToolsAgentHostClient { ...@@ -184,6 +189,42 @@ class StubDevToolsAgentHostClient : public content::DevToolsAgentHostClient {
base::span<const uint8_t> message) override {} base::span<const uint8_t> message) override {}
}; };
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
class PrintPreviewDelegate : printing::PrintPreviewUI::TestDelegate {
public:
PrintPreviewDelegate() {
printing::PrintPreviewUI::SetDelegateForTesting(this);
}
PrintPreviewDelegate(const PrintPreviewDelegate&) = delete;
PrintPreviewDelegate& operator=(const PrintPreviewDelegate&) = delete;
~PrintPreviewDelegate() override {
printing::PrintPreviewUI::SetDelegateForTesting(nullptr);
}
void WaitUntilPreviewIsReady() {
if (total_page_count_ > 0)
return;
base::RunLoop run_loop;
quit_callback_ = run_loop.QuitClosure();
run_loop.Run();
}
private:
// PrintPreviewUI::TestDelegate:
void DidGetPreviewPageCount(uint32_t page_count) override {
EXPECT_GE(page_count, 1u);
total_page_count_ = page_count;
if (quit_callback_)
std::move(quit_callback_).Run();
}
void DidRenderPreviewPage(content::WebContents* preview_dialog) override {}
uint32_t total_page_count_ = 0;
base::OnceClosure quit_callback_;
};
#endif
} // namespace } // namespace
// Flaky (https://crbug.com/1033009) // Flaky (https://crbug.com/1033009)
...@@ -229,3 +270,23 @@ IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, ...@@ -229,3 +270,23 @@ IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest,
ui_test_utils::NavigateToURL(browser(), data_url); ui_test_utils::NavigateToURL(browser(), data_url);
ASSERT_TRUE(GetGuestViewManager()->WaitForSingleGuestCreated()); ASSERT_TRUE(GetGuestViewManager()->WaitForSingleGuestCreated());
} }
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, EmbeddedThenPrint) {
PrintPreviewDelegate print_preview_delegate;
InitializeTestPage(embedded_test_server()->GetURL("/test_embedded.html"));
ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL));
auto* gv_manager = GetGuestViewManager();
gv_manager->WaitForAllGuestsDeleted();
EXPECT_EQ(1U, gv_manager->num_guests_created());
// Verify that print dialog comes up.
auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
auto* main_frame = web_contents->GetMainFrame();
// Use setTimeout() to prevent ExecuteScript() from blocking on the print
// dialog.
ASSERT_TRUE(content::ExecuteScript(
main_frame, "setTimeout(function() { window.print(); }, 0)"));
print_preview_delegate.WaitUntilPreviewIsReady();
}
#endif
...@@ -64,7 +64,6 @@ ...@@ -64,7 +64,6 @@
#endif #endif
using content::WebContents; using content::WebContents;
using content::WebContentsObserver;
namespace printing { namespace printing {
...@@ -117,21 +116,25 @@ struct PrintPreviewSettings { ...@@ -117,21 +116,25 @@ struct PrintPreviewSettings {
bool source_is_pdf; bool source_is_pdf;
}; };
// Observes the print preview webpage. Once it observes the PreviewPageCount // Implements PrintPreviewUI::TestDelegate. Once DidGetPreviewPageCount() is
// message, will send a sequence of commands to the print preview dialog and // called, will send a sequence of commands to the print preview dialog and
// change the settings of the preview dialog. // change the settings of the preview dialog.
class PrintPreviewObserver : public WebContentsObserver { class PrintPreviewDelegate : printing::PrintPreviewUI::TestDelegate {
public: public:
PrintPreviewObserver(Browser* browser, PrintPreviewDelegate(Browser* browser,
WebContents* dialog, WebContents* dialog,
const base::FilePath& pdf_file_save_path) const base::FilePath& pdf_file_save_path)
: WebContentsObserver(dialog), : browser_(browser),
browser_(browser),
state_(kWaitingToSendSaveAsPdf), state_(kWaitingToSendSaveAsPdf),
failed_setting_("None"), failed_setting_("None"),
pdf_file_save_path_(pdf_file_save_path) {} pdf_file_save_path_(pdf_file_save_path) {
printing::PrintPreviewUI::SetDelegateForTesting(this);
~PrintPreviewObserver() override {} }
PrintPreviewDelegate(const PrintPreviewDelegate&) = delete;
PrintPreviewDelegate& operator=(const PrintPreviewDelegate&) = delete;
~PrintPreviewDelegate() override {
printing::PrintPreviewUI::SetDelegateForTesting(nullptr);
}
// Sets closure for the observer so that it can end the loop. // Sets closure for the observer so that it can end the loop.
void set_quit_closure(base::OnceClosure closure) { void set_quit_closure(base::OnceClosure closure) {
...@@ -144,13 +147,6 @@ class PrintPreviewObserver : public WebContentsObserver { ...@@ -144,13 +147,6 @@ class PrintPreviewObserver : public WebContentsObserver {
std::move(quit_closure_)); std::move(quit_closure_));
} }
bool OnMessageReceived(const IPC::Message& message) override {
IPC_BEGIN_MESSAGE_MAP(PrintPreviewObserver, message)
IPC_MESSAGE_HANDLER(PrintHostMsg_DidStartPreview, OnDidStartPreview)
IPC_END_MESSAGE_MAP()
return false;
}
// Gets the web contents for the print preview dialog so that the UI and // Gets the web contents for the print preview dialog so that the UI and
// other elements can be accessed. // other elements can be accessed.
WebContents* GetDialog() { WebContents* GetDialog() {
...@@ -206,7 +202,7 @@ class PrintPreviewObserver : public WebContentsObserver { ...@@ -206,7 +202,7 @@ class PrintPreviewObserver : public WebContentsObserver {
// Called by |GetUI()->handler_|, it is a callback function that call // Called by |GetUI()->handler_|, it is a callback function that call
// |EndLoop| when an attempt to save the PDF has been made. // |EndLoop| when an attempt to save the PDF has been made.
GetUI()->SetPdfSavedClosureForTesting(base::BindOnce( GetUI()->SetPdfSavedClosureForTesting(base::BindOnce(
&PrintPreviewObserver::EndLoop, base::Unretained(this))); &PrintPreviewDelegate::EndLoop, base::Unretained(this)));
ASSERT_FALSE(pdf_file_save_path_.empty()); ASSERT_FALSE(pdf_file_save_path_.empty());
GetUI()->SetSelectedFileForTesting(pdf_file_save_path_); GetUI()->SetSelectedFileForTesting(pdf_file_save_path_);
return; return;
...@@ -231,21 +227,21 @@ class PrintPreviewObserver : public WebContentsObserver { ...@@ -231,21 +227,21 @@ class PrintPreviewObserver : public WebContentsObserver {
// listens for 'UILoadedForTest' and 'UIFailedLoadingForTest.' // listens for 'UILoadedForTest' and 'UIFailedLoadingForTest.'
class UIDoneLoadingMessageHandler : public content::WebUIMessageHandler { class UIDoneLoadingMessageHandler : public content::WebUIMessageHandler {
public: public:
explicit UIDoneLoadingMessageHandler(PrintPreviewObserver* observer) explicit UIDoneLoadingMessageHandler(PrintPreviewDelegate* delegate)
: observer_(observer) {} : delegate_(delegate) {}
~UIDoneLoadingMessageHandler() override {} ~UIDoneLoadingMessageHandler() override {}
// When a setting has been set succesfully, this is called and the observer // When a setting has been set successfully, this is called and the delegate
// is told to send the next setting to be set. // is told to send the next setting to be set.
void HandleDone(const base::ListValue* /* args */) { void HandleDone(const base::ListValue* /* args */) {
observer_->ManipulatePreviewSettings(); delegate_->ManipulatePreviewSettings();
} }
// Ends the test because a setting was not set successfully. Called when // Ends the test because a setting was not set successfully. Called when
// this class hears 'UIFailedLoadingForTest.' // this class hears 'UIFailedLoadingForTest.'
void HandleFailure(const base::ListValue* /* args */) { void HandleFailure(const base::ListValue* /* args */) {
FAIL() << "Failed to set: " << observer_->GetFailedSetting(); FAIL() << "Failed to set: " << delegate_->GetFailedSetting();
} }
// Allows this class to listen for the 'UILoadedForTest' and // Allows this class to listen for the 'UILoadedForTest' and
...@@ -267,19 +263,11 @@ class PrintPreviewObserver : public WebContentsObserver { ...@@ -267,19 +263,11 @@ class PrintPreviewObserver : public WebContentsObserver {
} }
private: private:
PrintPreviewObserver* const observer_; PrintPreviewDelegate* const delegate_;
DISALLOW_COPY_AND_ASSIGN(UIDoneLoadingMessageHandler);
}; };
// Called when the observer gets the IPC message with the preview document's // PrintPreviewUI::TestDelegate:
// properties. void DidGetPreviewPageCount(uint32_t page_count) override {
void OnDidStartPreview(const mojom::DidStartPreviewParams& params,
const printing::mojom::PreviewIds& ids) {
WebContents* web_contents = GetDialog();
ASSERT_TRUE(web_contents);
Observe(web_contents);
PrintPreviewUI* ui = GetUI(); PrintPreviewUI* ui = GetUI();
ASSERT_TRUE(ui); ASSERT_TRUE(ui);
ASSERT_TRUE(ui->web_ui()); ASSERT_TRUE(ui->web_ui());
...@@ -288,24 +276,18 @@ class PrintPreviewObserver : public WebContentsObserver { ...@@ -288,24 +276,18 @@ class PrintPreviewObserver : public WebContentsObserver {
std::make_unique<UIDoneLoadingMessageHandler>(this)); std::make_unique<UIDoneLoadingMessageHandler>(this));
ui->SendEnableManipulateSettingsForTest(); ui->SendEnableManipulateSettingsForTest();
} }
void DidRenderPreviewPage(content::WebContents* preview_dialog) override {}
void DidCloneToNewWebContents(WebContents* old_web_contents,
WebContents* new_web_contents) override {
Observe(new_web_contents);
}
Browser* browser_; Browser* browser_;
base::OnceClosure quit_closure_; base::OnceClosure quit_closure_;
std::unique_ptr<PrintPreviewSettings> settings_; std::unique_ptr<PrintPreviewSettings> settings_;
// State of the observer. The state indicates what message to send // |state_| that indicates what message to send next. The state advances
// next. The state advances whenever the message handler calls // whenever the message handler calls ManipulatePreviewSettings() on the
// ManipulatePreviewSettings() on the observer. // delegate.
State state_; State state_;
std::string failed_setting_; std::string failed_setting_;
const base::FilePath pdf_file_save_path_; const base::FilePath pdf_file_save_path_;
DISALLOW_COPY_AND_ASSIGN(PrintPreviewObserver);
}; };
class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
...@@ -317,14 +299,14 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { ...@@ -317,14 +299,14 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
// for all the settings to be set, then save the preview to PDF. // for all the settings to be set, then save the preview to PDF.
void NavigateAndPrint(const base::FilePath::StringType& file_name, void NavigateAndPrint(const base::FilePath::StringType& file_name,
const PrintPreviewSettings& settings) { const PrintPreviewSettings& settings) {
print_preview_observer_->SetPrintPreviewSettings(settings); print_preview_delegate_->SetPrintPreviewSettings(settings);
base::FilePath path(file_name); base::FilePath path(file_name);
GURL gurl = net::FilePathToFileURL(base::MakeAbsoluteFilePath(path)); GURL gurl = net::FilePathToFileURL(base::MakeAbsoluteFilePath(path));
ui_test_utils::NavigateToURL(browser(), gurl); ui_test_utils::NavigateToURL(browser(), gurl);
base::RunLoop loop; base::RunLoop loop;
print_preview_observer_->set_quit_closure(loop.QuitClosure()); print_preview_delegate_->set_quit_closure(loop.QuitClosure());
chrome::Print(browser()); chrome::Print(browser());
loop.Run(); loop.Run();
...@@ -461,31 +443,17 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { ...@@ -461,31 +443,17 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
std::cerr.flush(); std::cerr.flush();
} }
// Duplicates the tab that was created when the browser opened. This is done void CreatePreviewDelegate() {
// so that the observer can listen to the duplicated tab as soon as possible
// and start listening for messages related to print preview.
void DuplicateTab() {
WebContents* tab = WebContents* tab =
browser()->tab_strip_model()->GetActiveWebContents(); browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(tab); ASSERT_TRUE(tab);
print_preview_delegate_ = std::make_unique<PrintPreviewDelegate>(
print_preview_observer_ = std::make_unique<PrintPreviewObserver>(
browser(), tab, pdf_file_save_path_); browser(), tab, pdf_file_save_path_);
chrome::DuplicateTab(browser());
WebContents* initiator =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(initiator);
ASSERT_NE(tab, initiator);
} }
// Resets the test so that another web page can be printed. It also deletes // Resets the test so that another web page can be printed.
// the duplicated tab as it isn't needed anymore.
void Reset() { void Reset() {
png_output_.clear(); png_output_.clear();
ASSERT_EQ(2, browser()->tab_strip_model()->count());
chrome::CloseTab(browser());
ASSERT_EQ(1, browser()->tab_strip_model()->count());
} }
// Creates a temporary directory to store a text file that will be used for // Creates a temporary directory to store a text file that will be used for
...@@ -554,7 +522,7 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { ...@@ -554,7 +522,7 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
&png_output_)); &png_output_));
} }
std::unique_ptr<PrintPreviewObserver> print_preview_observer_; std::unique_ptr<PrintPreviewDelegate> print_preview_delegate_;
base::FilePath pdf_file_save_path_; base::FilePath pdf_file_save_path_;
// Vector for storing the PNG to be sent to the layout test framework. // Vector for storing the PNG to be sent to the layout test framework.
...@@ -615,7 +583,7 @@ IN_PROC_BROWSER_TEST_F(PrintPreviewPdfGeneratedBrowserTest, ...@@ -615,7 +583,7 @@ IN_PROC_BROWSER_TEST_F(PrintPreviewPdfGeneratedBrowserTest,
cmd = base::UTF8ToWide(input); cmd = base::UTF8ToWide(input);
#endif #endif
DuplicateTab(); CreatePreviewDelegate();
PrintPreviewSettings settings( PrintPreviewSettings settings(
true, "", false, false, mojom::MarginType::kDefaultMargins, true, "", false, false, mojom::MarginType::kDefaultMargins,
cmd.find(file_extension) != base::FilePath::StringType::npos); cmd.find(file_extension) != base::FilePath::StringType::npos);
......
...@@ -305,7 +305,6 @@ source_set("chrome_extensions_browsertests") { ...@@ -305,7 +305,6 @@ source_set("chrome_extensions_browsertests") {
"//components/dom_distiller/core:test_support", "//components/dom_distiller/core:test_support",
"//components/guest_view/browser:test_support", "//components/guest_view/browser:test_support",
"//components/javascript_dialogs", "//components/javascript_dialogs",
"//components/printing/common",
"//components/resources", "//components/resources",
"//components/strings", "//components/strings",
"//components/sync", "//components/sync",
......
...@@ -41,9 +41,6 @@ specific_include_rules = { ...@@ -41,9 +41,6 @@ specific_include_rules = {
".*(test|test_util)\.(cc|h)$": [ ".*(test|test_util)\.(cc|h)$": [
"+content/public/test", "+content/public/test",
], ],
"mime_handler_view_browsertest.cc": [
"+components/printing/common",
],
"mime_handler_view_interactive_uitest.cc": [ "mime_handler_view_interactive_uitest.cc": [
"+chrome/browser/ui/exclusive_access/exclusive_access_test.h", "+chrome/browser/ui/exclusive_access/exclusive_access_test.h",
"+chrome/test/base/interactive_test_utils.h", "+chrome/test/base/interactive_test_utils.h",
......
...@@ -21,8 +21,6 @@ ...@@ -21,8 +21,6 @@
#include "components/guest_view/browser/test_guest_view_manager.h" #include "components/guest_view/browser/test_guest_view_manager.h"
#include "components/javascript_dialogs/app_modal_dialog_controller.h" #include "components/javascript_dialogs/app_modal_dialog_controller.h"
#include "components/javascript_dialogs/app_modal_dialog_view.h" #include "components/javascript_dialogs/app_modal_dialog_view.h"
#include "components/printing/common/print.mojom.h"
#include "components/printing/common/print_messages.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_handle.h" #include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_observer.h"
...@@ -182,60 +180,6 @@ IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, Embedded) { ...@@ -182,60 +180,6 @@ IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, Embedded) {
EXPECT_EQ(1U, gv_manager->num_guests_created()); EXPECT_EQ(1U, gv_manager->num_guests_created());
} }
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
class PrintPreviewWaiter : public content::BrowserMessageFilter {
public:
PrintPreviewWaiter() : BrowserMessageFilter(PrintMsgStart) {}
bool OnMessageReceived(const IPC::Message& message) override {
IPC_BEGIN_MESSAGE_MAP(PrintPreviewWaiter, message)
IPC_MESSAGE_HANDLER(PrintHostMsg_DidStartPreview, OnDidStartPreview)
IPC_END_MESSAGE_MAP()
return false;
}
void OnDidStartPreview(const printing::mojom::DidStartPreviewParams& params,
const printing::mojom::PreviewIds& ids) {
// Expect that there is at least one page.
did_load_ = true;
run_loop_.Quit();
EXPECT_TRUE(params.page_count >= 1);
}
void Wait() {
if (!did_load_)
run_loop_.Run();
}
private:
~PrintPreviewWaiter() override = default;
bool did_load_ = false;
base::RunLoop run_loop_;
};
IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, EmbeddedThenPrint) {
RunTest("test_embedded.html");
ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL));
auto* gv_manager = GetGuestViewManager();
gv_manager->WaitForAllGuestsDeleted();
EXPECT_EQ(1U, gv_manager->num_guests_created());
// Verify that print dialog comes up.
auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
auto* main_frame = web_contents->GetMainFrame();
auto print_preview_waiter = base::MakeRefCounted<PrintPreviewWaiter>();
web_contents->GetMainFrame()->GetProcess()->AddFilter(
print_preview_waiter.get());
// Use setTimeout() to prevent ExecuteScript() from blocking on the print
// dialog.
ASSERT_TRUE(content::ExecuteScript(
main_frame, "setTimeout(function() { window.print(); }, 0)"));
print_preview_waiter->Wait();
}
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
// This test start with an <object> that has a content frame. Then the content // This test start with an <object> that has a content frame. Then the content
// frame (plugin frame) is navigated to a cross-origin target page. After the // frame (plugin frame) is navigated to a cross-origin target page. After the
// navigation is completed, the <object> is set to render MimeHandlerView by // navigation is completed, the <object> is set to render MimeHandlerView by
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment