#include "chrome/browser/ui/webui/print_preview/pdf_printer_handler.h"
#include <optional>
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/values_test_util.h"
#include "build/build_config.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/scoped_browser_locale.h"
#include "components/url_formatter/url_formatter.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#if BUILDFLAG(IS_MAC)
#include "chrome/common/printing/printer_capabilities_mac.h"
#include "printing/backend/print_backend.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#endif
#define FPL(x) …
namespace printing {
namespace {
const char kPdfDeviceName[] = …;
const char kPdfPrinterCapability[] = …;
void RecordCapability(base::OnceClosure done_closure,
base::Value::Dict* capability_out,
base::Value::Dict capability) { … }
#if BUILDFLAG(IS_MAC)
base::Value::Dict GetValueFromCustomPaper(
const PrinterSemanticCapsAndDefaults::Paper& paper) {
base::Value::Dict paper_value;
paper_value.Set("custom_display_name", paper.display_name());
paper_value.Set("height_microns", paper.size_um().height());
paper_value.Set("width_microns", paper.size_um().width());
int imageable_area_left_microns = paper.printable_area_um().x();
int imageable_area_bottom_microns = paper.printable_area_um().y();
int imageable_area_right_microns =
paper.printable_area_um().x() + paper.printable_area_um().width();
int imageable_area_top_microns =
paper.printable_area_um().y() + paper.printable_area_um().height();
paper_value.Set("imageable_area_left_microns", imageable_area_left_microns);
paper_value.Set("imageable_area_bottom_microns",
imageable_area_bottom_microns);
paper_value.Set("imageable_area_right_microns", imageable_area_right_microns);
paper_value.Set("imageable_area_top_microns", imageable_area_top_microns);
return paper_value;
}
#endif
}
PdfPrinterHandlerTest;
class PdfPrinterHandlerGetCapabilityTest : public BrowserWithTestWindowTest { … };
TEST_F(PdfPrinterHandlerTest, GetFileNameForPrintJobTitle) { … }
TEST_F(PdfPrinterHandlerTest, GetFileNameForPrintJobURL) { … }
TEST_F(PdfPrinterHandlerTest, GetFileName) { … }
TEST_F(PdfPrinterHandlerGetCapabilityTest, GetCapability) { … }
#if BUILDFLAG(IS_MAC)
TEST_F(PdfPrinterHandlerGetCapabilityTest,
GetMacCustomPaperSizesInCapabilities) {
constexpr char kPaperOptionPath[] = "capabilities.printer.media_size.option";
static const PrinterSemanticCapsAndDefaults::Papers kTestPapers = {
{"printer1", "", gfx::Size(101600, 127000),
gfx::Rect(120, 120, 101480, 126880)},
{"printer2", "", gfx::Size(76200, 152400),
gfx::Rect(0, 0, 76200, 152400)},
{"printer3", "", gfx::Size(330200, 863600),
gfx::Rect(0, 0, 330200, 863600)},
{"printer4", "", gfx::Size(101600, 50800),
gfx::Rect(0, 0, 101600, 50800)},
};
base::Value expected_capability =
base::test::ParseJson(kPdfPrinterCapability);
ASSERT_TRUE(expected_capability.is_dict());
base::Value::List* expected_paper_options =
expected_capability.GetDict().FindListByDottedPath(kPaperOptionPath);
ASSERT_TRUE(expected_paper_options);
for (const PrinterSemanticCapsAndDefaults::Paper& paper : kTestPapers)
expected_paper_options->Append(GetValueFromCustomPaper(paper));
SetMacCustomPaperSizesForTesting(kTestPapers);
base::Value::Dict capability = StartGetCapabilityAndWaitForResults();
const base::Value::List* paper_options =
capability.FindListByDottedPath(kPaperOptionPath);
ASSERT_TRUE(paper_options);
EXPECT_EQ(*expected_paper_options, *paper_options);
}
#endif
}