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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
// Copyright 2013 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/printing_utils.h"
#include <stddef.h>
#include <limits>
#include <string>
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "printing/buildflags/buildflags.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/size.h"
#if BUILDFLAG(IS_WIN)
#include "base/win/scoped_hdc.h"
#include "printing/backend/printing_info_win.h"
#include "printing/backend/win_helper.h"
#include "printing/printing_test.h"
#include "ui/gfx/geometry/rect.h"
#endif
namespace printing {
namespace {
constexpr size_t kTestLength = 8;
#if BUILDFLAG(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
constexpr gfx::Size kIsoA4Microns(210000, 297000);
constexpr gfx::Size kNaLetterMicrons(216000, 279000);
#endif
std::string Simplify(const std::string& title) {
return base::UTF16ToUTF8(
SimplifyDocumentTitleWithLength(base::UTF8ToUTF16(title), kTestLength));
}
std::string Format(const std::string& owner, const std::string& title) {
return base::UTF16ToUTF8(FormatDocumentTitleWithOwnerAndLength(
base::UTF8ToUTF16(owner), base::UTF8ToUTF16(title), kTestLength));
}
#if BUILDFLAG(IS_WIN)
// This test is automatically disabled if no printer is available.
class PrintingUtilsWinTest : public PrintingTest<testing::Test> {};
#endif // BUILDFLAG(IS_WIN)
} // namespace
TEST(PrintingUtilsTest, SimplifyDocumentTitle) {
EXPECT_EQ("", Simplify(""));
EXPECT_EQ("abcdefgh", Simplify("abcdefgh"));
EXPECT_EQ("abc...ij", Simplify("abcdefghij"));
EXPECT_EQ("Controls", Simplify("C\ron\nt\15rols"));
EXPECT_EQ("C__foo_", Simplify("C:\\foo\\"));
EXPECT_EQ("C__foo_", Simplify("C:/foo/"));
EXPECT_EQ("a_b_c", Simplify("a<b\"c"));
EXPECT_EQ("d_e_f_", Simplify("d*e?f~"));
EXPECT_EQ("", Simplify("\n\r\n\r\t\r"));
}
TEST(PrintingUtilsTest, FormatDocumentTitleWithOwner) {
EXPECT_EQ(": ", Format("", ""));
EXPECT_EQ("abc: ", Format("abc", ""));
EXPECT_EQ(": 123", Format("", "123"));
EXPECT_EQ("abc: 123", Format("abc", "123"));
EXPECT_EQ("abc: 0.9", Format("abc", "0123456789"));
EXPECT_EQ("ab...j: ", Format("abcdefghij", "123"));
EXPECT_EQ("xyz: _.o", Format("xyz", "\\f\\oo"));
EXPECT_EQ("ab...j: ", Format("abcdefghij", "0123456789"));
}
#if BUILDFLAG(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
TEST(PrintingUtilsTest, GetDefaultPaperSizeFromLocaleMicrons) {
// Valid locales
EXPECT_EQ(kNaLetterMicrons, GetDefaultPaperSizeFromLocaleMicrons("en-US"));
EXPECT_EQ(kNaLetterMicrons, GetDefaultPaperSizeFromLocaleMicrons("en_US"));
EXPECT_EQ(kNaLetterMicrons, GetDefaultPaperSizeFromLocaleMicrons("fr-CA"));
EXPECT_EQ(kNaLetterMicrons, GetDefaultPaperSizeFromLocaleMicrons("es-CL"));
EXPECT_EQ(kIsoA4Microns, GetDefaultPaperSizeFromLocaleMicrons("en_UK"));
EXPECT_EQ(kIsoA4Microns, GetDefaultPaperSizeFromLocaleMicrons("fa-IR"));
// Empty locale
EXPECT_EQ(kIsoA4Microns, GetDefaultPaperSizeFromLocaleMicrons(""));
// Non-existing locale
EXPECT_EQ(kIsoA4Microns,
GetDefaultPaperSizeFromLocaleMicrons("locale-does-not-exist"));
}
TEST(PrintingUtilsTest, SizesEqualWithinEpsilon) {
constexpr int kMaxInt = std::numeric_limits<int>::max();
// Large sizes
EXPECT_TRUE(SizesEqualWithinEpsilon(gfx::Size(kMaxInt, kMaxInt),
gfx::Size(kMaxInt - 1, kMaxInt - 1), 1));
EXPECT_FALSE(SizesEqualWithinEpsilon(gfx::Size(kMaxInt, kMaxInt),
gfx::Size(kMaxInt - 1, kMaxInt - 2), 1));
EXPECT_TRUE(SizesEqualWithinEpsilon(gfx::Size(kMaxInt, kMaxInt),
gfx::Size(0, 0), kMaxInt));
EXPECT_FALSE(SizesEqualWithinEpsilon(gfx::Size(kMaxInt, kMaxInt),
gfx::Size(0, 0), kMaxInt - 1));
// Empty sizes
EXPECT_TRUE(SizesEqualWithinEpsilon(gfx::Size(0, 0), gfx::Size(0, 0), 0));
EXPECT_TRUE(SizesEqualWithinEpsilon(gfx::Size(1, 0), gfx::Size(0, 2), 0));
EXPECT_TRUE(SizesEqualWithinEpsilon(gfx::Size(1, -2), gfx::Size(-1, 2), 0));
// Common paper sizes
EXPECT_FALSE(SizesEqualWithinEpsilon(kNaLetterMicrons, kIsoA4Microns, 1000));
EXPECT_TRUE(SizesEqualWithinEpsilon(kNaLetterMicrons,
gfx::Size(215900, 279400), 500));
EXPECT_TRUE(
SizesEqualWithinEpsilon(kIsoA4Microns, gfx::Size(210500, 296500), 500));
}
#endif // BUILDFLAG(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
#if BUILDFLAG(IS_WIN)
TEST(PrintingUtilsTest, GetCenteredPageContentRect) {
gfx::Rect page_content;
// No centering.
gfx::Size page_size = gfx::Size(1200, 1200);
gfx::Rect page_content_rect = gfx::Rect(0, 0, 400, 1100);
page_content = GetCenteredPageContentRect(gfx::Size(1000, 1000), page_size,
page_content_rect);
EXPECT_EQ(0, page_content.x());
EXPECT_EQ(0, page_content.y());
EXPECT_EQ(400, page_content.width());
EXPECT_EQ(1100, page_content.height());
// X centered.
page_size = gfx::Size(500, 1200);
page_content = GetCenteredPageContentRect(gfx::Size(1000, 1000), page_size,
page_content_rect);
EXPECT_EQ(250, page_content.x());
EXPECT_EQ(0, page_content.y());
EXPECT_EQ(400, page_content.width());
EXPECT_EQ(1100, page_content.height());
// Y centered.
page_size = gfx::Size(1200, 500);
page_content = GetCenteredPageContentRect(gfx::Size(1000, 1000), page_size,
page_content_rect);
EXPECT_EQ(0, page_content.x());
EXPECT_EQ(250, page_content.y());
EXPECT_EQ(400, page_content.width());
EXPECT_EQ(1100, page_content.height());
// Both X and Y centered.
page_size = gfx::Size(500, 500),
page_content = GetCenteredPageContentRect(gfx::Size(1000, 1000), page_size,
page_content_rect);
EXPECT_EQ(250, page_content.x());
EXPECT_EQ(250, page_content.y());
EXPECT_EQ(400, page_content.width());
EXPECT_EQ(1100, page_content.height());
}
// Disabled - see crbug.com/1231528 for context.
TEST_F(PrintingUtilsWinTest, DISABLED_GetPrintableAreaDeviceUnits) {
if (IsTestCaseDisabled()) {
return;
}
std::wstring printer_name = GetDefaultPrinter();
ScopedPrinterHandle printer;
ASSERT_TRUE(printer.OpenPrinterWithName(printer_name.c_str()));
const DEVMODE* dev_mode = nullptr;
PrinterInfo2 info_2;
if (info_2.Init(printer.Get())) {
dev_mode = info_2.get()->pDevMode;
}
ASSERT_TRUE(dev_mode);
base::win::ScopedCreateDC hdc(
CreateDC(L"WINSPOOL", printer_name.c_str(), nullptr, dev_mode));
ASSERT_TRUE(hdc.Get());
// Check that getting printable area is successful and the resulting area is
// non-empty.
gfx::Rect output = GetPrintableAreaDeviceUnits(hdc.Get());
EXPECT_FALSE(output.IsEmpty());
}
#endif // BUILDFLAG(IS_WIN)
} // namespace printing
|