chromium/components/printing/common/cloud_print_cdd_conversion_unittest.cc

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

#include "components/printing/common/cloud_print_cdd_conversion.h"

#include "base/test/values_test_util.h"
#include "base/values.h"
#include "printing/backend/print_backend.h"
#include "printing/backend/print_backend_test_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"

namespace cloud_print {

namespace {

constexpr char kKeyPrinter[] =;
constexpr char kKeyVersion[] =;
constexpr char kValueVersion[] =;

constexpr char kExpectedCollateDefaultTrue[] =;

constexpr char kExpectedCollateDefaultFalse[] =;

constexpr char kExpectedColor[] =;

constexpr char kExpectedCopies[] =;

constexpr char kExpectedDpi[] =;

constexpr char kExpectedDuplex[] =;

constexpr char kExpectedMediaSize[] =;

constexpr char kExpectedPageOrientation[] =;

constexpr char kExpectedSupportedContentType[] =;

constexpr char kExpectedMediaSizeWithWiderPaper[] =;

constexpr char kExpectedMediaType[] =;

#if BUILDFLAG(IS_CHROMEOS)
constexpr char kExpectedPinSupportedTrue[] = R"json({
  "supported": true
})json";

constexpr char kExpectedPinSupportedFalse[] = R"json({
  "supported": false
})json";

constexpr char kExpectedAdvancedCapabilities[] = R"json([
  {
    "display_name": "Advanced Capability #1 (bool)",
    "id": "advanced_cap_bool",
    "type": "TYPED_VALUE",
    "typed_value_cap": {
      "value_type": "BOOLEAN"
    }
  }, {
    "display_name": "Advanced Capability #2 (double)",
    "id": "advanced_cap_double",
    "select_cap": {
      "option": [ {
        "display_name": "Advanced Capability #1",
        "value": "adv_cap_val_1"
      }, {
        "display_name": "Advanced Capability #2",
        "value": "adv_cap_val_2"
      }, {
        "display_name": "Advanced Capability #3",
        "value": "adv_cap_val_3"
      } ]
    },
    "type": "SELECT"
  }
])json";
#endif  // BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(IS_WIN)
constexpr char kExpectedPageOutputQuality[] = R"json([
  {
    "display_name": "Page output quality",
    "id": "page_output_quality",
    "select_cap": {
      "option": [ {
        "display_name": "Normal",
        "value": "ns000:Normal"
      }, {
        "display_name": "Draft",
        "value": "ns000:Draft",
        "is_default": true
      }, {
        "display_name": "Advance",
        "value": "ns000:Advance"
      } ]
    },
    "type": "SELECT"
  }
])json";

constexpr char kExpectedPageOutputQualityNullDefault[] = R"json([
  {
    "display_name": "Page output quality",
    "id": "page_output_quality",
    "select_cap": {
      "option": [ {
        "display_name": "Normal",
        "value": "ns000:Normal"
      }, {
        "display_name": "Draft",
        "value": "ns000:Draft"
      }, {
        "display_name": "Advance",
        "value": "ns000:Advance"
      } ]
    },
    "type": "SELECT"
  }
])json";
#endif  // BUILDFLAG(IS_WIN)

const base::Value::Dict* GetPrinterDict(const base::Value& caps_value) {}

}  // namespace

TEST(CloudPrintCddConversionTest, ValidCloudPrintCddConversion) {}

TEST(CloudPrintCddConversionTest, MissingEntry) {}

TEST(CloudPrintCddConversionTest, CollateDefaultIsFalse) {}

TEST(CloudPrintCddConversionTest, WiderPaper) {}

#if BUILDFLAG(IS_CHROMEOS)
TEST(CloudPrintCddConversionTest, MediaTypeOnlyOne) {
  printing::PrinterSemanticCapsAndDefaults input =
      printing::GenerateSamplePrinterSemanticCapsAndDefaults({});
  input.media_types = {input.media_types[0]};
  const base::Value output = PrinterSemanticCapsAndDefaultsToCdd(input);
  const base::Value::Dict* printer_dict = GetPrinterDict(output);

  // The media type list should only be included when more than one media type
  // is supported.
  ASSERT_TRUE(printer_dict);
  EXPECT_FALSE(printer_dict->contains("media_type"));
}

TEST(CloudPrintCddConversionTest, PinAndAdvancedCapabilities) {
  printing::PrinterSemanticCapsAndDefaults input =
      printing::GenerateSamplePrinterSemanticCapsAndDefaults(
          printing::SampleWithPinAndAdvancedCapabilities());
  const base::Value output = PrinterSemanticCapsAndDefaultsToCdd(input);
  const base::Value::Dict* printer_dict = GetPrinterDict(output);

  ASSERT_TRUE(printer_dict);
  ASSERT_EQ(11u, printer_dict->size());
  base::ExpectDictValue(base::test::ParseJson(kExpectedPinSupportedTrue),
                        *printer_dict, "pin");
  base::ExpectDictValue(base::test::ParseJson(kExpectedAdvancedCapabilities),
                        *printer_dict, "vendor_capability");
}
#endif  // BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(IS_WIN)
TEST(CloudPrintCddConversionTest, PageOutputQualityWithDefaultQuality) {
  printing::PrinterSemanticCapsAndDefaults input =
      printing::GenerateSamplePrinterSemanticCapsAndDefaults(
          printing::SampleWithPageOutputQuality());
  input.page_output_quality->default_quality = printing::kDefaultQuality;
  const base::Value output = PrinterSemanticCapsAndDefaultsToCdd(input);
  const base::Value::Dict* printer_dict = GetPrinterDict(output);

  ASSERT_TRUE(printer_dict);
  ASSERT_EQ(10u, printer_dict->size());
  base::ExpectDictValue(base::test::ParseJson(kExpectedPageOutputQuality),
                        *printer_dict, "vendor_capability");
}

TEST(CloudPrintCddConversionTest, PageOutputQualityNullDefaultQuality) {
  printing::PrinterSemanticCapsAndDefaults input =
      printing::GenerateSamplePrinterSemanticCapsAndDefaults(
          printing::SampleWithPageOutputQuality());
  const base::Value output = PrinterSemanticCapsAndDefaultsToCdd(input);
  const base::Value::Dict* printer_dict = GetPrinterDict(output);

  ASSERT_TRUE(printer_dict);
  ASSERT_EQ(10u, printer_dict->size());
  base::ExpectDictValue(
      base::test::ParseJson(kExpectedPageOutputQualityNullDefault),
      *printer_dict, "vendor_capability");
}
#endif  // BUILDFLAG(IS_WIN)

}  // namespace cloud_print