summaryrefslogtreecommitdiff
path: root/chromium/printing/printing_utils_unittest.cc
blob: 935099d3684a003eef159e9e3a9c80487087ac25 (plain)
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
// Copyright 2013 The Chromium Authors. All rights reserved.
// 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 "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/size.h"

namespace printing {

namespace {

constexpr size_t kTestLength = 8;
constexpr gfx::Size kIsoA4Microns(210000, 297000);
constexpr gfx::Size kNaLetterMicrons(216000, 279000);

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));
}

}  // 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"));
}

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));
}

TEST(PrintingUtilsTest, ParsePaper) {
  PrinterSemanticCapsAndDefaults::Paper paper_mm =
      ParsePaper("iso_a4_210x297mm");
  EXPECT_EQ(gfx::Size(210000, 297000), paper_mm.size_um);
  EXPECT_EQ("iso_a4_210x297mm", paper_mm.vendor_id);
  EXPECT_EQ("iso a4", paper_mm.display_name);

  PrinterSemanticCapsAndDefaults::Paper paper_in =
      ParsePaper("na_letter_8.5x11in");
  EXPECT_EQ(gfx::Size(215900, 279400), paper_in.size_um);
  EXPECT_EQ("na_letter_8.5x11in", paper_in.vendor_id);
  EXPECT_EQ("na letter", paper_in.display_name);
}

}  // namespace printing