Commit dd23f06c authored by rbpotter's avatar rbpotter Committed by Commit Bot

PrintPreviewHandler: Add a GetPreview test

Bug: None
Change-Id: I3441a7a704df305fcb4379ee46cc00b5d3b3619d
Reviewed-on: https://chromium-review.googlesource.com/740704
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#512993}
parent 616f37ba
......@@ -123,6 +123,9 @@ class PrintPreviewHandler
// Protected so unit tests can override.
virtual PrinterHandler* GetPrinterHandler(printing::PrinterType printer_type);
// Gets the initiator for the print preview dialog.
virtual content::WebContents* GetInitiator() const;
// Register/unregister from notifications of changes done to the GAIA
// cookie. Protected so unit tests can override.
virtual void RegisterForGaiaCookieChanges();
......@@ -137,6 +140,7 @@ class PrintPreviewHandler
FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest, GetPrinters);
FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest, GetPrinterCapabilities);
FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest, Print);
FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest, GetPreview);
class AccessTokenService;
content::WebContents* preview_web_contents() const;
......@@ -247,9 +251,6 @@ class PrintPreviewHandler
void SendCloudPrintJob(const std::string& callback_id,
const base::RefCountedBytes* data);
// Gets the initiator for the print preview dialog.
content::WebContents* GetInitiator() const;
// Closes the preview dialog.
void ClosePreviewDialog();
......
......@@ -11,13 +11,19 @@
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/printing/print_view_manager.h"
#include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
#include "chrome/browser/ui/webui/print_preview/printer_handler.h"
#include "chrome/test/base/testing_profile.h"
#include "components/printing/common/print_messages.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui_controller.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/test_web_ui.h"
#include "ipc/ipc_test_sink.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace printing {
......@@ -80,52 +86,52 @@ base::Value GetPrintTicket(printing::PrinterType type, bool cloud) {
// Letter
base::Value media_size(base::Value::Type::DICTIONARY);
media_size.SetKey("is_default", base::Value(true));
media_size.SetKey("width_microns", base::Value(215900));
media_size.SetKey("height_microns", base::Value(279400));
ticket.SetKey("mediaSize", std::move(media_size));
ticket.SetKey("pageCount", base::Value(1));
ticket.SetKey("landscape", base::Value(false));
ticket.SetKey("color", base::Value(2)); // color printing
ticket.SetKey("headerFooterEnabled", base::Value(false));
ticket.SetKey("marginsType", base::Value(0)); // default margins
ticket.SetKey("duplex", base::Value(1)); // LONG_EDGE
ticket.SetKey("copies", base::Value(1));
ticket.SetKey("collate", base::Value(false));
ticket.SetKey("shouldPrintBackgrounds", base::Value(false));
ticket.SetKey("shouldPrintSelectionOnly", base::Value(false));
ticket.SetKey("previewModifiable", base::Value(false));
ticket.SetKey("printToPDF",
media_size.SetKey(kSettingMediaSizeIsDefault, base::Value(true));
media_size.SetKey(kSettingMediaSizeWidthMicrons, base::Value(215900));
media_size.SetKey(kSettingMediaSizeHeightMicrons, base::Value(279400));
ticket.SetKey(kSettingMediaSize, std::move(media_size));
ticket.SetKey(kSettingPreviewPageCount, base::Value(1));
ticket.SetKey(kSettingLandscape, base::Value(false));
ticket.SetKey(kSettingColor, base::Value(2)); // color printing
ticket.SetKey(kSettingHeaderFooterEnabled, base::Value(false));
ticket.SetKey(kSettingMarginsType, base::Value(0)); // default margins
ticket.SetKey(kSettingDuplexMode, base::Value(1)); // LONG_EDGE
ticket.SetKey(kSettingCopies, base::Value(1));
ticket.SetKey(kSettingCollate, base::Value(true));
ticket.SetKey(kSettingShouldPrintBackgrounds, base::Value(false));
ticket.SetKey(kSettingShouldPrintSelectionOnly, base::Value(false));
ticket.SetKey(kSettingPreviewModifiable, base::Value(false));
ticket.SetKey(kSettingPrintToPDF,
base::Value(!cloud && type == printing::kPdfPrinter));
ticket.SetKey("printWithCloudPrint", base::Value(cloud));
ticket.SetKey("printWithPrivet", base::Value(is_privet_printer));
ticket.SetKey("printWithExtension", base::Value(is_extension_printer));
ticket.SetKey("rasterizePDF", base::Value(false));
ticket.SetKey("scaleFactor", base::Value(100));
ticket.SetKey("dpiHorizontal", base::Value(600));
ticket.SetKey("dpiVertical", base::Value(600));
ticket.SetKey("deviceName", base::Value(kDummyPrinterName));
ticket.SetKey("fitToPageEnabled", base::Value(true));
ticket.SetKey("pageWidth", base::Value(215900));
ticket.SetKey("pageHeight", base::Value(279400));
ticket.SetKey("showSystemDialog", base::Value(false));
ticket.SetKey(kSettingCloudPrintDialog, base::Value(cloud));
ticket.SetKey(kSettingPrintWithPrivet, base::Value(is_privet_printer));
ticket.SetKey(kSettingPrintWithExtension, base::Value(is_extension_printer));
ticket.SetKey(kSettingRasterizePdf, base::Value(false));
ticket.SetKey(kSettingScaleFactor, base::Value(100));
ticket.SetKey(kSettingDpiHorizontal, base::Value(600));
ticket.SetKey(kSettingDpiVertical, base::Value(600));
ticket.SetKey(kSettingDeviceName, base::Value(kDummyPrinterName));
ticket.SetKey(kSettingFitToPageEnabled, base::Value(true));
ticket.SetKey(kSettingPageWidth, base::Value(215900));
ticket.SetKey(kSettingPageHeight, base::Value(279400));
ticket.SetKey(kSettingShowSystemDialog, base::Value(false));
if (cloud)
ticket.SetKey("cloudPrintID", base::Value(kDummyPrinterName));
ticket.SetKey(kSettingCloudPrintId, base::Value(kDummyPrinterName));
if (is_privet_printer || is_extension_printer) {
base::Value capabilities(base::Value::Type::DICTIONARY);
capabilities.SetKey("duplex", base::Value(true)); // non-empty
std::string caps_string;
base::JSONWriter::Write(capabilities, &caps_string);
ticket.SetKey("capabilities", base::Value(caps_string));
ticket.SetKey(kSettingCapabilities, base::Value(caps_string));
base::Value print_ticket(base::Value::Type::DICTIONARY);
print_ticket.SetKey("version", base::Value("1.0"));
print_ticket.SetKey("print", base::Value());
std::string ticket_string;
base::JSONWriter::Write(print_ticket, &ticket_string);
ticket.SetKey("ticket", base::Value(ticket_string));
ticket.SetKey(kSettingTicket, base::Value(ticket_string));
}
return ticket;
......@@ -222,9 +228,10 @@ class FakePrintPreviewUI : public PrintPreviewUI {
class TestPrintPreviewHandler : public PrintPreviewHandler {
public:
explicit TestPrintPreviewHandler(
std::unique_ptr<PrinterHandler> printer_handler)
: test_printer_handler_(std::move(printer_handler)) {}
TestPrintPreviewHandler(std::unique_ptr<PrinterHandler> printer_handler,
content::WebContents* initiator)
: test_printer_handler_(std::move(printer_handler)),
initiator_(initiator) {}
PrinterHandler* GetPrinterHandler(PrinterType printer_type) override {
called_for_type_.insert(printer_type);
......@@ -234,6 +241,8 @@ class TestPrintPreviewHandler : public PrintPreviewHandler {
void RegisterForGaiaCookieChanges() override {}
void UnregisterForGaiaCookieChanges() override {}
content::WebContents* GetInitiator() const override { return initiator_; }
bool CalledOnlyForType(PrinterType printer_type) {
return (called_for_type_.size() == 1 &&
*called_for_type_.begin() == printer_type);
......@@ -246,6 +255,7 @@ class TestPrintPreviewHandler : public PrintPreviewHandler {
private:
base::flat_set<PrinterType> called_for_type_;
std::unique_ptr<PrinterHandler> test_printer_handler_;
content::WebContents* const initiator_;
DISALLOW_COPY_AND_ASSIGN(TestPrintPreviewHandler);
};
......@@ -259,8 +269,14 @@ class PrintPreviewHandlerTest : public testing::Test {
PrintPreviewHandlerTest() {
TestingProfile::Builder builder;
profile_ = builder.Build();
initiator_web_contents_.reset(content::WebContents::Create(
content::WebContents::CreateParams(profile_.get())));
content::WebContents* initiator = initiator_web_contents_.get();
preview_web_contents_.reset(content::WebContents::Create(
content::WebContents::CreateParams(profile_.get())));
printing::PrintViewManager::CreateForWebContents(initiator);
printing::PrintViewManager::FromWebContents(initiator)->PrintPreviewNow(
initiator->GetMainFrame(), false);
web_ui_ = std::make_unique<content::TestWebUI>();
web_ui_->set_web_contents(preview_web_contents_.get());
......@@ -271,7 +287,7 @@ class PrintPreviewHandlerTest : public testing::Test {
printer_handler_ = printer_handler.get();
auto preview_handler = std::make_unique<printing::TestPrintPreviewHandler>(
std::move(printer_handler));
std::move(printer_handler), initiator);
preview_handler->set_web_ui(web_ui());
handler_ = preview_handler.get();
......@@ -282,12 +298,19 @@ class PrintPreviewHandlerTest : public testing::Test {
web_ui()->SetController(preview_ui.release());
}
~PrintPreviewHandlerTest() override {
printing::PrintViewManager::FromWebContents(initiator_web_contents_.get())
->PrintPreviewDone();
}
void Initialize() {
// Sending this message will enable javascript, so it must always be called
// before any other messages are sent.
base::ListValue list_args;
list_args.AppendString("test-callback-id-0");
handler()->HandleGetInitialSettings(&list_args);
base::Value args(base::Value::Type::LIST);
args.GetList().emplace_back("test-callback-id-0");
std::unique_ptr<base::ListValue> list_args =
base::ListValue::From(base::Value::ToUniquePtrValue(std::move(args)));
handler()->HandleGetInitialSettings(list_args.get());
// In response to get initial settings, the initial settings are sent back
// and a use-cloud-print event is dispatched.
......@@ -351,6 +374,18 @@ class PrintPreviewHandlerTest : public testing::Test {
EXPECT_EQ(default_printer_name, printer->GetString());
}
IPC::TestSink& initiator_sink() {
content::RenderFrameHost* rfh = initiator_web_contents_->GetMainFrame();
auto* rph = static_cast<content::MockRenderProcessHost*>(rfh->GetProcess());
return rph->sink();
}
IPC::TestSink& preview_sink() {
content::RenderFrameHost* rfh = preview_web_contents_->GetMainFrame();
auto* rph = static_cast<content::MockRenderProcessHost*>(rfh->GetProcess());
return rph->sink();
}
const Profile* profile() { return profile_.get(); }
content::TestWebUI* web_ui() { return web_ui_.get(); }
printing::TestPrintPreviewHandler* handler() { return handler_; }
......@@ -361,7 +396,9 @@ class PrintPreviewHandlerTest : public testing::Test {
content::TestBrowserThreadBundle thread_bundle_;
std::unique_ptr<TestingProfile> profile_;
std::unique_ptr<content::TestWebUI> web_ui_;
content::RenderViewHostTestEnabler rvh_test_enabler_;
std::unique_ptr<content::WebContents> preview_web_contents_;
std::unique_ptr<content::WebContents> initiator_web_contents_;
std::vector<printing::PrinterInfo> printers_;
printing::TestPrinterHandler* printer_handler_;
printing::TestPrintPreviewHandler* handler_;
......@@ -392,12 +429,14 @@ TEST_F(PrintPreviewHandlerTest, GetPrinters) {
for (size_t i = 0; i < arraysize(types); i++) {
printing::PrinterType type = types[i];
handler()->reset_calls();
base::ListValue args;
base::Value args(base::Value::Type::LIST);
std::string callback_id_in =
"test-callback-id-" + base::UintToString(i + 1);
args.AppendString(callback_id_in);
args.AppendInteger(type);
handler()->HandleGetPrinters(&args);
args.GetList().emplace_back(callback_id_in);
args.GetList().emplace_back(type);
std::unique_ptr<base::ListValue> list_args =
base::ListValue::From(base::Value::ToUniquePtrValue(std::move(args)));
handler()->HandleGetPrinters(list_args.get());
EXPECT_TRUE(handler()->CalledOnlyForType(type));
// Start with 2 calls from initial settings, then add 2 more for each loop
......@@ -436,13 +475,15 @@ TEST_F(PrintPreviewHandlerTest, GetPrinterCapabilities) {
for (size_t i = 0; i < arraysize(printing::kAllTypes); i++) {
printing::PrinterType type = printing::kAllTypes[i];
handler()->reset_calls();
base::ListValue args;
base::Value args(base::Value::Type::LIST);
std::string callback_id_in =
"test-callback-id-" + base::UintToString(i + 1);
args.AppendString(callback_id_in);
args.AppendString(printing::kDummyPrinterName);
args.AppendInteger(type);
handler()->HandleGetPrinterCapabilities(&args);
args.GetList().emplace_back(callback_id_in);
args.GetList().emplace_back(printing::kDummyPrinterName);
args.GetList().emplace_back(type);
std::unique_ptr<base::ListValue> list_args =
base::ListValue::From(base::Value::ToUniquePtrValue(std::move(args)));
handler()->HandleGetPrinterCapabilities(list_args.get());
EXPECT_TRUE(handler()->CalledOnlyForType(type));
// Start with 2 calls from initial settings, then add 1 more for each loop
......@@ -463,14 +504,16 @@ TEST_F(PrintPreviewHandlerTest, GetPrinterCapabilities) {
for (size_t i = 0; i < arraysize(printing::kAllTypes); i++) {
printing::PrinterType type = printing::kAllTypes[i];
handler()->reset_calls();
base::ListValue args;
base::Value args(base::Value::Type::LIST);
std::string callback_id_in =
"test-callback-id-" +
base::UintToString(i + arraysize(printing::kAllTypes) + 1);
args.AppendString(callback_id_in);
args.AppendString("EmptyPrinter");
args.AppendInteger(type);
handler()->HandleGetPrinterCapabilities(&args);
args.GetList().emplace_back(callback_id_in);
args.GetList().emplace_back("EmptyPrinter");
args.GetList().emplace_back(type);
std::unique_ptr<base::ListValue> list_args =
base::ListValue::From(base::Value::ToUniquePtrValue(std::move(args)));
handler()->HandleGetPrinterCapabilities(list_args.get());
EXPECT_TRUE(handler()->CalledOnlyForType(type));
// Start with 2 calls from initial settings plus
......@@ -495,15 +538,17 @@ TEST_F(PrintPreviewHandlerTest, Print) {
printing::PrinterType type =
cloud ? printing::kPrivetPrinter : printing::kAllTypes[i];
handler()->reset_calls();
base::ListValue args;
base::Value args(base::Value::Type::LIST);
std::string callback_id_in =
"test-callback-id-" + base::UintToString(i + 1);
args.AppendString(callback_id_in);
args.GetList().emplace_back(callback_id_in);
base::Value print_ticket = printing::GetPrintTicket(type, cloud);
std::string json;
base::JSONWriter::Write(print_ticket, &json);
args.AppendString(json);
handler()->HandlePrint(&args);
args.GetList().emplace_back(json);
std::unique_ptr<base::ListValue> list_args =
base::ListValue::From(base::Value::ToUniquePtrValue(std::move(args)));
handler()->HandlePrint(list_args.get());
// Verify correct PrinterHandler was called or that no handler was requested
// for local and cloud printers.
......@@ -517,6 +562,18 @@ TEST_F(PrintPreviewHandlerTest, Print) {
const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
CheckWebUIResponse(data, callback_id_in, true);
// For local printers, the Print Preview UI will respond to the resolution
// by sending a "hidePreview" message, which should prompt an IPC message
// to the renderer.
if (type == printing::kLocalPrinter) {
base::Value hide_args(base::Value::Type::LIST);
std::unique_ptr<base::ListValue> hide_args_ptr = base::ListValue::From(
base::Value::ToUniquePtrValue(std::move(hide_args)));
handler()->HandleHidePreview(hide_args_ptr.get());
EXPECT_TRUE(preview_sink().GetUniqueMessageMatching(
PrintMsg_PrintForPrintPreview::ID));
}
// For cloud print, should also get the encoded data back as a string.
if (cloud) {
std::string print_data;
......@@ -527,3 +584,55 @@ TEST_F(PrintPreviewHandlerTest, Print) {
}
}
}
TEST_F(PrintPreviewHandlerTest, GetPreview) {
Initialize();
base::Value args(base::Value::Type::LIST);
args.GetList().emplace_back("test-callback-id-1");
base::Value print_ticket =
printing::GetPrintTicket(printing::kLocalPrinter, false);
// Make some modifications to match a preview print ticket.
print_ticket.SetKey(printing::kSettingPageRange, base::Value());
print_ticket.SetKey(printing::kIsFirstRequest, base::Value(true));
print_ticket.SetKey(printing::kPreviewRequestID, base::Value(0));
print_ticket.SetKey(printing::kSettingPreviewModifiable, base::Value(true));
print_ticket.SetKey(printing::kSettingGenerateDraftData, base::Value(true));
print_ticket.RemoveKey(printing::kSettingPageWidth);
print_ticket.RemoveKey(printing::kSettingPageHeight);
print_ticket.RemoveKey(printing::kSettingShowSystemDialog);
std::string json;
base::JSONWriter::Write(print_ticket, &json);
args.GetList().emplace_back(json);
std::string page_count;
base::JSONWriter::Write(base::Value(-1), &page_count);
args.GetList().emplace_back(page_count);
std::unique_ptr<base::ListValue> list_args =
base::ListValue::From(base::Value::ToUniquePtrValue(std::move(args)));
handler()->HandleGetPreview(list_args.get());
// Verify that the preview was requested from the renderer with the
// appropriate settings.
EXPECT_TRUE(
initiator_sink().GetUniqueMessageMatching(PrintMsg_PrintPreview::ID));
const IPC::Message* msg =
initiator_sink().GetFirstMessageMatching(PrintMsg_PrintPreview::ID);
ASSERT_TRUE(msg);
PrintMsg_PrintPreview::Param param;
PrintMsg_PrintPreview::Read(msg, &param);
bool preview_id_found = false;
for (const auto& it : std::get<0>(param).DictItems()) {
if (it.first == printing::kPreviewUIID) { // This is added by the handler.
preview_id_found = true;
continue;
}
base::Value* value_in = print_ticket.FindKey(it.first);
ASSERT_TRUE(value_in);
EXPECT_EQ(*value_in, it.second);
}
EXPECT_TRUE(preview_id_found);
}
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