[M120 Migration][VD] Remove accessing oom_score_adj in zygote process
[platform/framework/web/chromium-efl.git] / printing / printing_utils_unittest.cc
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "printing/printing_utils.h"
6
7 #include <stddef.h>
8
9 #include <limits>
10 #include <string>
11
12 #include "base/strings/utf_string_conversions.h"
13 #include "build/build_config.h"
14 #include "build/chromeos_buildflags.h"
15 #include "printing/buildflags/buildflags.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/gfx/geometry/size.h"
18
19 #if BUILDFLAG(IS_WIN)
20 #include "base/win/scoped_hdc.h"
21 #include "printing/backend/printing_info_win.h"
22 #include "printing/backend/win_helper.h"
23 #include "printing/printing_test.h"
24 #include "ui/gfx/geometry/rect.h"
25 #endif
26
27 namespace printing {
28
29 namespace {
30
31 constexpr size_t kTestLength = 8;
32
33 #if BUILDFLAG(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
34 constexpr gfx::Size kIsoA4Microns(210000, 297000);
35 constexpr gfx::Size kNaLetterMicrons(216000, 279000);
36 #endif
37
38 std::string Simplify(const std::string& title) {
39   return base::UTF16ToUTF8(
40       SimplifyDocumentTitleWithLength(base::UTF8ToUTF16(title), kTestLength));
41 }
42
43 std::string Format(const std::string& owner, const std::string& title) {
44   return base::UTF16ToUTF8(FormatDocumentTitleWithOwnerAndLength(
45       base::UTF8ToUTF16(owner), base::UTF8ToUTF16(title), kTestLength));
46 }
47
48 #if BUILDFLAG(IS_WIN)
49 // This test is automatically disabled if no printer is available.
50 class PrintingUtilsWinTest : public PrintingTest<testing::Test> {};
51 #endif  // BUILDFLAG(IS_WIN)
52
53 }  // namespace
54
55 TEST(PrintingUtilsTest, SimplifyDocumentTitle) {
56   EXPECT_EQ("", Simplify(""));
57   EXPECT_EQ("abcdefgh", Simplify("abcdefgh"));
58   EXPECT_EQ("abc...ij", Simplify("abcdefghij"));
59   EXPECT_EQ("Controls", Simplify("C\ron\nt\15rols"));
60   EXPECT_EQ("C__foo_", Simplify("C:\\foo\\"));
61   EXPECT_EQ("C__foo_", Simplify("C:/foo/"));
62   EXPECT_EQ("a_b_c", Simplify("a<b\"c"));
63   EXPECT_EQ("d_e_f_", Simplify("d*e?f~"));
64   EXPECT_EQ("", Simplify("\n\r\n\r\t\r"));
65 }
66
67 TEST(PrintingUtilsTest, FormatDocumentTitleWithOwner) {
68   EXPECT_EQ(": ", Format("", ""));
69   EXPECT_EQ("abc: ", Format("abc", ""));
70   EXPECT_EQ(": 123", Format("", "123"));
71   EXPECT_EQ("abc: 123", Format("abc", "123"));
72   EXPECT_EQ("abc: 0.9", Format("abc", "0123456789"));
73   EXPECT_EQ("ab...j: ", Format("abcdefghij", "123"));
74   EXPECT_EQ("xyz: _.o", Format("xyz", "\\f\\oo"));
75   EXPECT_EQ("ab...j: ", Format("abcdefghij", "0123456789"));
76 }
77
78 #if BUILDFLAG(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
79 TEST(PrintingUtilsTest, GetDefaultPaperSizeFromLocaleMicrons) {
80   // Valid locales
81   EXPECT_EQ(kNaLetterMicrons, GetDefaultPaperSizeFromLocaleMicrons("en-US"));
82   EXPECT_EQ(kNaLetterMicrons, GetDefaultPaperSizeFromLocaleMicrons("en_US"));
83   EXPECT_EQ(kNaLetterMicrons, GetDefaultPaperSizeFromLocaleMicrons("fr-CA"));
84   EXPECT_EQ(kNaLetterMicrons, GetDefaultPaperSizeFromLocaleMicrons("es-CL"));
85   EXPECT_EQ(kIsoA4Microns, GetDefaultPaperSizeFromLocaleMicrons("en_UK"));
86   EXPECT_EQ(kIsoA4Microns, GetDefaultPaperSizeFromLocaleMicrons("fa-IR"));
87
88   // Empty locale
89   EXPECT_EQ(kIsoA4Microns, GetDefaultPaperSizeFromLocaleMicrons(""));
90
91   // Non-existing locale
92   EXPECT_EQ(kIsoA4Microns,
93             GetDefaultPaperSizeFromLocaleMicrons("locale-does-not-exist"));
94 }
95
96 TEST(PrintingUtilsTest, SizesEqualWithinEpsilon) {
97   constexpr int kMaxInt = std::numeric_limits<int>::max();
98
99   // Large sizes
100   EXPECT_TRUE(SizesEqualWithinEpsilon(gfx::Size(kMaxInt, kMaxInt),
101                                       gfx::Size(kMaxInt - 1, kMaxInt - 1), 1));
102   EXPECT_FALSE(SizesEqualWithinEpsilon(gfx::Size(kMaxInt, kMaxInt),
103                                        gfx::Size(kMaxInt - 1, kMaxInt - 2), 1));
104   EXPECT_TRUE(SizesEqualWithinEpsilon(gfx::Size(kMaxInt, kMaxInt),
105                                       gfx::Size(0, 0), kMaxInt));
106   EXPECT_FALSE(SizesEqualWithinEpsilon(gfx::Size(kMaxInt, kMaxInt),
107                                        gfx::Size(0, 0), kMaxInt - 1));
108
109   // Empty sizes
110   EXPECT_TRUE(SizesEqualWithinEpsilon(gfx::Size(0, 0), gfx::Size(0, 0), 0));
111   EXPECT_TRUE(SizesEqualWithinEpsilon(gfx::Size(1, 0), gfx::Size(0, 2), 0));
112   EXPECT_TRUE(SizesEqualWithinEpsilon(gfx::Size(1, -2), gfx::Size(-1, 2), 0));
113
114   // Common paper sizes
115   EXPECT_FALSE(SizesEqualWithinEpsilon(kNaLetterMicrons, kIsoA4Microns, 1000));
116   EXPECT_TRUE(SizesEqualWithinEpsilon(kNaLetterMicrons,
117                                       gfx::Size(215900, 279400), 500));
118   EXPECT_TRUE(
119       SizesEqualWithinEpsilon(kIsoA4Microns, gfx::Size(210500, 296500), 500));
120 }
121 #endif  // BUILDFLAG(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
122
123 #if BUILDFLAG(IS_WIN)
124 TEST(PrintingUtilsTest, GetCenteredPageContentRect) {
125   gfx::Rect page_content;
126
127   // No centering.
128   gfx::Size page_size = gfx::Size(1200, 1200);
129   gfx::Rect page_content_rect = gfx::Rect(0, 0, 400, 1100);
130   page_content = GetCenteredPageContentRect(gfx::Size(1000, 1000), page_size,
131                                             page_content_rect);
132   EXPECT_EQ(0, page_content.x());
133   EXPECT_EQ(0, page_content.y());
134   EXPECT_EQ(400, page_content.width());
135   EXPECT_EQ(1100, page_content.height());
136
137   // X centered.
138   page_size = gfx::Size(500, 1200);
139   page_content = GetCenteredPageContentRect(gfx::Size(1000, 1000), page_size,
140                                             page_content_rect);
141   EXPECT_EQ(250, page_content.x());
142   EXPECT_EQ(0, page_content.y());
143   EXPECT_EQ(400, page_content.width());
144   EXPECT_EQ(1100, page_content.height());
145
146   // Y centered.
147   page_size = gfx::Size(1200, 500);
148   page_content = GetCenteredPageContentRect(gfx::Size(1000, 1000), page_size,
149                                             page_content_rect);
150   EXPECT_EQ(0, page_content.x());
151   EXPECT_EQ(250, page_content.y());
152   EXPECT_EQ(400, page_content.width());
153   EXPECT_EQ(1100, page_content.height());
154
155   // Both X and Y centered.
156   page_size = gfx::Size(500, 500),
157   page_content = GetCenteredPageContentRect(gfx::Size(1000, 1000), page_size,
158                                             page_content_rect);
159   EXPECT_EQ(250, page_content.x());
160   EXPECT_EQ(250, page_content.y());
161   EXPECT_EQ(400, page_content.width());
162   EXPECT_EQ(1100, page_content.height());
163 }
164
165 // Disabled - see crbug.com/1231528 for context.
166 TEST_F(PrintingUtilsWinTest, DISABLED_GetPrintableAreaDeviceUnits) {
167   if (IsTestCaseDisabled()) {
168     return;
169   }
170
171   std::wstring printer_name = GetDefaultPrinter();
172   ScopedPrinterHandle printer;
173   ASSERT_TRUE(printer.OpenPrinterWithName(printer_name.c_str()));
174
175   const DEVMODE* dev_mode = nullptr;
176   PrinterInfo2 info_2;
177   if (info_2.Init(printer.Get())) {
178     dev_mode = info_2.get()->pDevMode;
179   }
180   ASSERT_TRUE(dev_mode);
181
182   base::win::ScopedCreateDC hdc(
183       CreateDC(L"WINSPOOL", printer_name.c_str(), nullptr, dev_mode));
184   ASSERT_TRUE(hdc.Get());
185
186   // Check that getting printable area is successful and the resulting area is
187   // non-empty.
188   gfx::Rect output = GetPrintableAreaDeviceUnits(hdc.Get());
189   EXPECT_FALSE(output.IsEmpty());
190 }
191 #endif  // BUILDFLAG(IS_WIN)
192
193 }  // namespace printing