File: print_settings_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (145 lines) | stat: -rw-r--r-- 5,597 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "printing/print_settings.h"

#include "base/test/gtest_util.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "printing/buildflags/buildflags.h"
#include "printing/mojom/print.mojom.h"
#include "printing/units.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace printing {

TEST(PrintSettingsTest, ColorModeToColorModel) {
  for (int mode = static_cast<int>(mojom::ColorModel::kUnknownColorModel);
       mode <= static_cast<int>(mojom::ColorModel::kMaxValue); ++mode) {
    EXPECT_EQ(ColorModeToColorModel(mode),
              static_cast<mojom::ColorModel>(mode));
  }

  // Check edge cases.
  EXPECT_EQ(ColorModeToColorModel(
                static_cast<int>(mojom::ColorModel::kUnknownColorModel) - 1),
            mojom::ColorModel::kUnknownColorModel);
  EXPECT_EQ(
      ColorModeToColorModel(static_cast<int>(mojom::ColorModel::kMaxValue) + 1),
      mojom::ColorModel::kUnknownColorModel);
}

TEST(PrintSettingsTest, IsColorModelSelected) {
  for (int model = static_cast<int>(mojom::ColorModel::kUnknownColorModel) + 1;
       model <= static_cast<int>(mojom::ColorModel::kMaxValue); ++model) {
    EXPECT_TRUE(IsColorModelSelected(static_cast<mojom::ColorModel>(model))
                    .has_value());
  }
}

TEST(PrintSettingsDeathTest, IsColorModelSelectedEdges) {
  GTEST_FLAG_SET(death_test_style, "threadsafe");
  EXPECT_NOTREACHED_DEATH(
      IsColorModelSelected(mojom::ColorModel::kUnknownColorModel));
}
#if BUILDFLAG(USE_CUPS)
TEST(PrintSettingsTest, GetColorModelForModel) {
  std::string color_setting_name;
  std::string color_value;
  for (int model = static_cast<int>(mojom::ColorModel::kUnknownColorModel);
       model <= static_cast<int>(mojom::ColorModel::kMaxValue); ++model) {
    GetColorModelForModel(static_cast<mojom::ColorModel>(model),
                          &color_setting_name, &color_value);
    EXPECT_FALSE(color_setting_name.empty());
    EXPECT_FALSE(color_value.empty());
    color_setting_name.clear();
    color_value.clear();
  }
}
#endif  // BUILDFLAG(USE_CUPS)

#if BUILDFLAG(USE_CUPS_IPP)
TEST(PrintSettingsTest, GetIppColorModelForModel) {
  for (int model = static_cast<int>(mojom::ColorModel::kUnknownColorModel);
       model <= static_cast<int>(mojom::ColorModel::kMaxValue); ++model) {
    EXPECT_FALSE(GetIppColorModelForModel(static_cast<mojom::ColorModel>(model))
                     .empty());
  }
}
#endif  // BUILDFLAG(USE_CUPS_IPP)

TEST(PrintSettingsTest, SetPrinterPrintableArea) {
  static constexpr gfx::Size kPhysicalSizeDeviceUnits(600, 800);
  static constexpr gfx::Rect kPrintableAreaDeviceUnits(50, 50, 500, 700);

  struct TestCase {
    int dpi;
    PageMargins margins;
    mojom::MarginType expected_margin_type;
  } static const kTestCases[] = {
      {
          300,
          PageMargins(),
          mojom::MarginType::kDefaultMargins,
      },
      {250, PageMargins(0, 0, 10000, 10000, 5000, 5000),
       mojom::MarginType::kCustomMargins},
      {426, PageMargins(0, 0, 20000, 20000, 25400, 25400),
       mojom::MarginType::kCustomMargins},
      {300, PageMargins(0, 0, 10000, 20000, 5000, 15000),
       mojom::MarginType::kCustomMargins}};

  for (const auto& test_case : kTestCases) {
    PrintSettings settings;
    settings.set_dpi(test_case.dpi);

    if (test_case.expected_margin_type == mojom::MarginType::kCustomMargins) {
      settings.SetCustomMargins(test_case.margins);
    }

    settings.SetPrinterPrintableArea(kPhysicalSizeDeviceUnits,
                                     kPrintableAreaDeviceUnits,
                                     /*landscape_needs_flip=*/false);

    EXPECT_EQ(test_case.expected_margin_type, settings.margin_type());
    EXPECT_EQ(test_case.margins.top,
              settings.requested_custom_margins_in_microns().top);
    EXPECT_EQ(test_case.margins.bottom,
              settings.requested_custom_margins_in_microns().bottom);
    EXPECT_EQ(test_case.margins.left,
              settings.requested_custom_margins_in_microns().left);
    EXPECT_EQ(test_case.margins.right,
              settings.requested_custom_margins_in_microns().right);

    const PageSetup& page_setup = settings.page_setup_device_units();

    if (test_case.expected_margin_type == mojom::MarginType::kDefaultMargins) {
#if BUILDFLAG(IS_MAC)
      EXPECT_EQ(PageMargins(50, 50, 50, 50, 50, 50),
                page_setup.effective_margins());
#else
      EXPECT_EQ(PageMargins(50, 50, 118, 118, 118, 118),
                page_setup.effective_margins());
#endif
    } else if (test_case.expected_margin_type ==
               mojom::MarginType::kCustomMargins) {
      const int device_units_per_inch = settings.device_units_per_inch();
      PageMargins expected_custom_margins;
      expected_custom_margins.header = 0;
      expected_custom_margins.footer = 0;
      expected_custom_margins.top = ConvertUnit(
          test_case.margins.top, kMicronsPerInch, device_units_per_inch);
      expected_custom_margins.bottom = ConvertUnit(
          test_case.margins.bottom, kMicronsPerInch, device_units_per_inch);
      expected_custom_margins.left = ConvertUnit(
          test_case.margins.left, kMicronsPerInch, device_units_per_inch);
      expected_custom_margins.right = ConvertUnit(
          test_case.margins.right, kMicronsPerInch, device_units_per_inch);

      EXPECT_EQ(expected_custom_margins, page_setup.effective_margins());
    }
  }
}

}  // namespace printing