chromium/printing/mojom/printing_context_mojom_traits_unittest.cc

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/values.h"
#include "build/build_config.h"
#include "mojo/public/cpp/test_support/test_utils.h"
#include "printing/mojom/print.mojom.h"
#include "printing/mojom/printing_context.mojom.h"
#include "printing/page_range.h"
#include "printing/page_setup.h"
#include "printing/print_settings.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/size.h"

namespace printing {

namespace {

// Values for PageMargins, just want non-zero and all different.
const PageMargins kPageMarginNonzero(/*header=*/1,
                                     /*footer=*/2,
                                     /*left=*/3,
                                     /*right=*/4,
                                     /*top=*/5,
                                     /*bottom=*/6);

constexpr int kPageSetupTextHeight =;
constexpr gfx::Size kPageSetupPhysicalSize =;
constexpr gfx::Rect kPageSetupPrintableArea =;
const PageMargins kPageSetupRequestedMargins(
    /*header=*/10,
    /*footer=*/20,
    /*left=*/5,
    /*right=*/15,
    /*top=*/10 + kPageSetupTextHeight,
    /*bottom=*/20 + kPageSetupTextHeight);

const PageSetup kPageSetupAsymmetricalMargins(kPageSetupPhysicalSize,
                                              kPageSetupPrintableArea,
                                              kPageSetupRequestedMargins,
                                              /*forced_margins=*/false,
                                              kPageSetupTextHeight);
const PageSetup kPageSetupForcedMargins(kPageSetupPhysicalSize,
                                        kPageSetupPrintableArea,
                                        kPageSetupRequestedMargins,
                                        /*forced_margins=*/true,
                                        kPageSetupTextHeight);

constexpr gfx::Size kRequestedMediaSize =;
const char kRequestedMediaVendorId[] =;

PrintSettings::RequestedMedia GenerateSampleRequestedMedia() {}

#if BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
base::Value::Dict GenerateSampleSystemPrintDialogData(
#if BUILDFLAG(IS_MAC)
    bool include_optional_data
#endif
) {}
#endif  // BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)

// Support two possible sample `PrintSettings`, to ensure that certain fields
// get definitively tested for coverage (e.g., booleans).  Note that not all
// fields need distinct values between the two.  A key difference between them
// is custom vs. default margins, as this has significant implications
// internally on printable area.
const PageRanges kPrintSettingsPageRanges{};
constexpr char16_t kPrintSettingsTitle[] =;
constexpr char16_t kPrintSettingsUrl[] =;
constexpr int kPrintSettingsCopies =;
constexpr char16_t kPrintSettingsDeviceName[] =;
const PrintSettings::RequestedMedia kPrintSettingsRequestedMedia{};
const PageMargins kPrintSettingsCustomMarginsInPoints(/*header=*/10,
                                                      /*footer=*/15,
                                                      /*left=*/20,
                                                      /*right=*/25,
                                                      /*top=*/30,
                                                      /*bottom=*/35);

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
PrintSettings::AdvancedSettings GenerateSampleAdvancedSettings() {}

const PrintSettings::AdvancedSettings kPrintSettingsAdvancedSettings =;
#endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(IS_CHROMEOS)
constexpr char kPrintSettingsUsername[] = "username";
constexpr char kPrintSettingsPinValue[] = "pin-value";
#endif  // BUILDFLAG(IS_CHROMEOS)

constexpr bool kPrintSettingsSetSelection1 =;
constexpr bool kPrintSettingsSetSelection2 =;

constexpr mojom::MarginType kPrintSettingsMarginType1 =;
constexpr mojom::MarginType kPrintSettingsMarginType2 =;

constexpr bool kPrintSettingsDisplayHeaderFooter1 =;
constexpr bool kPrintSettingsDisplayHeaderFooter2 =;

constexpr bool kPrintSettingsShouldPrintBackgrounds1 =;
constexpr bool kPrintSettingsShouldPrintBackgrounds2 =;

constexpr bool kPrintSettingsCollate1 =;
constexpr bool kPrintSettingsCollate2 =;

constexpr mojom::ColorModel kPrintSettingsColorModel1 =;
constexpr mojom::ColorModel kPrintSettingsColorModel2 =;

constexpr mojom::DuplexMode kPrintSettingsDuplexMode1 =;
constexpr mojom::DuplexMode kPrintSettingsDuplexMode2 =;

constexpr gfx::Size kPrintSettingsDpi1(600, 600);
constexpr gfx::Size kPrintSettingsDpi2(1200, 600);

const char kPrintSettingsMediaTypeEmpty[] =;
const char kPrintSettingsMediaTypePlain[] =;

constexpr bool kPrintSettingsBorderless1 =;
constexpr bool kPrintSettingsBorderless2 =;

constexpr double kPrintSettingsScaleFactor1 =;
constexpr double kPrintSettingsScaleFactor2 =;

constexpr bool kPrintSettingsRasterizePdf1 =;
constexpr bool kPrintSettingsRasterizePdf2 =;

constexpr bool kPrintSettingsLandscape1 =;
constexpr bool kPrintSettingsLandscape2 =;

#if BUILDFLAG(IS_WIN)
constexpr mojom::PrinterLanguageType kPrintSettingsPrinterLanguageType1 =
    mojom::PrinterLanguageType::kTextOnly;
constexpr mojom::PrinterLanguageType kPrintSettingsPrinterLanguageType2 =
    mojom::PrinterLanguageType::kXps;
#endif  // BUILDFLAG(IS_WIN)

constexpr bool kPrintSettingsModifiable1 =;
constexpr bool kPrintSettingsModifiable2 =;

constexpr int kPrintSettingsPagesPerSheet1 =;
constexpr int kPrintSettingsPagesPerSheet2 =;

#if BUILDFLAG(IS_CHROMEOS)
constexpr bool kPrintSettingsSendUserInfo1 = true;
constexpr bool kPrintSettingsSendUserInfo2 = false;
#endif

struct GenerationParams {};

PrintSettings GenerateSamplePrintSettingsCommon() {}

PrintSettings GenerateSamplePrintSettingsDefaultMargins() {}

PrintSettings GenerateSamplePrintSettingsCustomMarginsWithParams(
    GenerationParams params) {}

PrintSettings GenerateSamplePrintSettingsCustomMargins() {}

bool RequestedMediasEqual(const PrintSettings::RequestedMedia& lhs,
                          const PrintSettings::RequestedMedia& rhs) {}

bool PageMarginsEqual(const PageMargins& lhs, const PageMargins& rhs) {}

}  // namespace

TEST(PrintingContextMojomTraitsTest, TestSerializeAndDeserializePageMargins) {}

// Test that no margins is valid.
TEST(PrintingContextMojomTraitsTest,
     TestSerializeAndDeserializePageMarginsNoMargins) {}

// Test that negative margin values are allowed.
TEST(PrintingContextMojomTraitsTest,
     TestSerializeAndDeserializePageMarginsNegativeValues) {}

TEST(PrintingContextMojomTraitsTest, TestSerializeAndDeserializePageSetup) {}

TEST(PrintingContextMojomTraitsTest,
     TestSerializeAndDeserializePageSetupForcedMargins) {}

TEST(PrintingContextMojomTraitsTest,
     TestSerializeAndDeserializePageRangeMultiPage) {}

TEST(PrintingContextMojomTraitsTest,
     TestSerializeAndDeserializePageRangeSinglePage) {}

TEST(PrintingContextMojomTraitsTest,
     TestSerializeAndDeserializePageRangeReverseRange) {}

TEST(PrintingContextMojomTraitsTest,
     TestSerializeAndDeserializeRequestedMedia) {}

TEST(PrintingContextMojomTraitsTest,
     TestSerializeAndDeserializeRequestedMediaEmpty) {}

TEST(PrintingContextMojomTraitsTest,
     TestSerializeAndDeserializePrintSettingsDefaultMargins) {}

TEST(PrintingContextMojomTraitsTest,
     TestSerializeAndDeserializePrintSettingsCustomMargins) {}

TEST(PrintingContextMojomTraitsTest,
     TestSerializeAndDeserializePrintSettingsEmptyPageRanges) {}

TEST(PrintingContextMojomTraitsTest,
     TestSerializeAndDeserializePrintSettingsEmptyPrintableArea) {}

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
TEST(PrintingContextMojomTraitsTest,
     TestSerializeAndDeserializePrintSettingsEmptyAdvancedSettings) {}
#endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
TEST(PrintingContextMojomTraitsTest,
     TestSerializeAndDeserializePrintSettingsSystemPrintDialogData) {}

TEST(PrintingContextMojomTraitsTest,
     TestSerializeAndDeserializePrintSettingsSystemPrintDialogDataInvalid) {}

TEST(PrintingContextMojomTraitsTest,
     TestSerializeAndDeserializePrintSettingsSystemPrintDialogDataExtraKey) {}

#if BUILDFLAG(IS_MAC)
TEST(
    PrintingContextMojomTraitsTest,
    TestSerializeAndDeserializePrintSettingsSystemPrintDialogDataDestTypeOutOfRange) {
  PrintSettings input = GenerateSamplePrintSettingsDefaultMargins();

  // Generate some system print dialog data.
  base::Value::Dict data =
      GenerateSampleSystemPrintDialogData(/*include_optional_data=*/false);

  // Override with out-of-range destination type.
  data.Set(kMacSystemPrintDialogDataDestinationType, 0x10000);
  input.set_system_print_dialog_data(std::move(data));

  PrintSettings output;
  EXPECT_FALSE(
      mojo::test::SerializeAndDeserialize<mojom::PrintSettings>(input, output));
}

TEST(
    PrintingContextMojomTraitsTest,
    TestSerializeAndDeserializePrintSettingsSystemPrintDialogDataDestTypeInvalidDataType) {
  PrintSettings input = GenerateSamplePrintSettingsDefaultMargins();

  // Generate some system print dialog data.
  base::Value::Dict data =
      GenerateSampleSystemPrintDialogData(/*include_optional_data=*/false);

  // Override with invalid data type for destination type.
  data.Set(kMacSystemPrintDialogDataDestinationType, "supposed to be an int");
  input.set_system_print_dialog_data(std::move(data));

  PrintSettings output;
  EXPECT_FALSE(
      mojo::test::SerializeAndDeserialize<mojom::PrintSettings>(input, output));
}

TEST(
    PrintingContextMojomTraitsTest,
    TestSerializeAndDeserializePrintSettingsSystemPrintDialogPageFormatInvalidDataType) {
  PrintSettings input = GenerateSamplePrintSettingsDefaultMargins();

  // Generate some system print dialog data.
  base::Value::Dict data =
      GenerateSampleSystemPrintDialogData(/*include_optional_data=*/false);

  // Override with invalid data type for page format.
  data.Set(kMacSystemPrintDialogDataPageFormat, "supposed to be a BlobStorage");
  input.set_system_print_dialog_data(std::move(data));

  PrintSettings output;
  EXPECT_FALSE(
      mojo::test::SerializeAndDeserialize<mojom::PrintSettings>(input, output));
}

TEST(
    PrintingContextMojomTraitsTest,
    TestSerializeAndDeserializePrintSettingsSystemPrintDialogPrintSettingsInvalidDataType) {
  PrintSettings input = GenerateSamplePrintSettingsDefaultMargins();

  // Generate some system print dialog data.
  base::Value::Dict data =
      GenerateSampleSystemPrintDialogData(/*include_optional_data=*/false);

  // Override with invalid data type for print settings.
  data.Set(kMacSystemPrintDialogDataPrintSettings,
           "supposed to be a BlobStorage");
  input.set_system_print_dialog_data(std::move(data));

  PrintSettings output;
  EXPECT_FALSE(
      mojo::test::SerializeAndDeserialize<mojom::PrintSettings>(input, output));
}

TEST(
    PrintingContextMojomTraitsTest,
    TestSerializeAndDeserializePrintSettingsSystemPrintDialogDestinationFormatInvalidDataType) {
  PrintSettings input = GenerateSamplePrintSettingsDefaultMargins();

  // Generate some system print dialog data.
  base::Value::Dict data =
      GenerateSampleSystemPrintDialogData(/*include_optional_data=*/false);

  // Override with invalid data type for destination format.
  data.Set(kMacSystemPrintDialogDataPageFormat, 0xBAD);
  input.set_system_print_dialog_data(std::move(data));

  PrintSettings output;
  EXPECT_FALSE(
      mojo::test::SerializeAndDeserialize<mojom::PrintSettings>(input, output));
}

TEST(
    PrintingContextMojomTraitsTest,
    TestSerializeAndDeserializePrintSettingsSystemPrintDialogDestinationLocationInvalidDataType) {
  PrintSettings input = GenerateSamplePrintSettingsDefaultMargins();

  // Generate some system print dialog data.
  base::Value::Dict data =
      GenerateSampleSystemPrintDialogData(/*include_optional_data=*/false);

  // Override with invalid data type for destination location.
  data.Set(kMacSystemPrintDialogDataDestinationLocation, 0xBAD);
  input.set_system_print_dialog_data(std::move(data));

  PrintSettings output;
  EXPECT_FALSE(
      mojo::test::SerializeAndDeserialize<mojom::PrintSettings>(input, output));
}
#endif  // BUILDFLAG(IS_MAC)

#if BUILDFLAG(IS_LINUX)
TEST(
    PrintingContextMojomTraitsTest,
    TestSerializeAndDeserializePrintSettingsSystemPrintDialogPrinterInvalidDataType) {}

TEST(
    PrintingContextMojomTraitsTest,
    TestSerializeAndDeserializePrintSettingsSystemPrintDialogPrintSettingsDataType) {}

TEST(
    PrintingContextMojomTraitsTest,
    TestSerializeAndDeserializePrintSettingsSystemPrintDialogPageSetupDataType) {}
#endif  // BUILDFLAG(IS_LINUX)

#endif  // BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)

}  // namespace printing