OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 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 "base/memory/ptr_util.h" |
| 6 #include "base/values.h" |
| 7 #include "headless/lib/browser/headless_devtools_manager_delegate.h" |
| 8 #include "headless/lib/browser/headless_print_manager.h" |
| 9 #include "printing/features/features.h" |
| 10 #include "printing/units.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace headless { |
| 14 |
| 15 TEST(ParsePrintSettingsTest, Landscape) { |
| 16 printing::HeadlessPrintSettings settings; |
| 17 EXPECT_FALSE(settings.landscape); |
| 18 |
| 19 auto params = base::MakeUnique<base::DictionaryValue>(); |
| 20 params->SetBoolean("landscape", true); |
| 21 std::unique_ptr<base::DictionaryValue> response = |
| 22 ParsePrintSettings(0, params.get(), &settings); |
| 23 EXPECT_TRUE(settings.landscape); |
| 24 EXPECT_EQ(nullptr, response); |
| 25 } |
| 26 |
| 27 TEST(ParsePrintSettingsTest, HeaderFooter) { |
| 28 printing::HeadlessPrintSettings settings; |
| 29 EXPECT_FALSE(settings.display_header_footer); |
| 30 |
| 31 auto params = base::MakeUnique<base::DictionaryValue>(); |
| 32 params->SetBoolean("displayHeaderFooter", true); |
| 33 std::unique_ptr<base::DictionaryValue> response = |
| 34 ParsePrintSettings(0, params.get(), &settings); |
| 35 EXPECT_TRUE(settings.display_header_footer); |
| 36 EXPECT_EQ(nullptr, response); |
| 37 } |
| 38 |
| 39 TEST(ParsePrintSettingsTest, PrintBackground) { |
| 40 printing::HeadlessPrintSettings settings; |
| 41 EXPECT_FALSE(settings.should_print_backgrounds); |
| 42 |
| 43 auto params = base::MakeUnique<base::DictionaryValue>(); |
| 44 params->SetBoolean("printBackground", true); |
| 45 std::unique_ptr<base::DictionaryValue> response = |
| 46 ParsePrintSettings(0, params.get(), &settings); |
| 47 EXPECT_TRUE(settings.should_print_backgrounds); |
| 48 EXPECT_EQ(nullptr, response); |
| 49 } |
| 50 |
| 51 TEST(ParsePrintSettingsTest, Scale) { |
| 52 printing::HeadlessPrintSettings settings; |
| 53 EXPECT_DOUBLE_EQ(1, settings.scale); |
| 54 |
| 55 auto params = base::MakeUnique<base::DictionaryValue>(); |
| 56 params->SetDouble("scale", 0.5); |
| 57 std::unique_ptr<base::DictionaryValue> response = |
| 58 ParsePrintSettings(0, params.get(), &settings); |
| 59 EXPECT_DOUBLE_EQ(0.5, settings.scale); |
| 60 EXPECT_EQ(nullptr, response); |
| 61 |
| 62 params->SetDouble("scale", -1); |
| 63 response = ParsePrintSettings(0, params.get(), &settings); |
| 64 EXPECT_NE(nullptr, response); |
| 65 |
| 66 params->SetDouble("scale", 10); |
| 67 response = ParsePrintSettings(0, params.get(), &settings); |
| 68 EXPECT_NE(nullptr, response); |
| 69 } |
| 70 |
| 71 TEST(ParsePrintSettingsTest, PageRanges) { |
| 72 printing::HeadlessPrintSettings settings; |
| 73 EXPECT_EQ("", settings.page_ranges); |
| 74 |
| 75 auto params = base::MakeUnique<base::DictionaryValue>(); |
| 76 params->SetString("pageRanges", "abcd"); |
| 77 std::unique_ptr<base::DictionaryValue> response = |
| 78 ParsePrintSettings(0, params.get(), &settings); |
| 79 // Pass pageRanges text to settings directly and return no error, even if |
| 80 // it is invalid. The validation is deferred to HeadlessPrintMangager once the |
| 81 // total page count is available. See the PageRangeTextToPagesTest. |
| 82 EXPECT_EQ("abcd", settings.page_ranges); |
| 83 EXPECT_EQ(nullptr, response); |
| 84 } |
| 85 |
| 86 TEST(ParsePrintSettingsTest, Paper) { |
| 87 printing::HeadlessPrintSettings settings; |
| 88 |
| 89 auto params = base::MakeUnique<base::DictionaryValue>(); |
| 90 std::unique_ptr<base::DictionaryValue> response = |
| 91 ParsePrintSettings(0, params.get(), &settings); |
| 92 EXPECT_EQ(printing::kLetterWidthInch * printing::kPointsPerInch, |
| 93 settings.paper_size_in_points.width()); |
| 94 EXPECT_EQ(printing::kLetterHeightInch * printing::kPointsPerInch, |
| 95 settings.paper_size_in_points.height()); |
| 96 EXPECT_EQ(nullptr, response); |
| 97 |
| 98 params->SetDouble("paperWidth", 1); |
| 99 params->SetDouble("paperHeight", 2); |
| 100 response = ParsePrintSettings(0, params.get(), &settings); |
| 101 EXPECT_EQ(1 * printing::kPointsPerInch, |
| 102 settings.paper_size_in_points.width()); |
| 103 EXPECT_EQ(2 * printing::kPointsPerInch, |
| 104 settings.paper_size_in_points.height()); |
| 105 EXPECT_EQ(nullptr, response); |
| 106 |
| 107 params->SetDouble("paperWidth", -1); |
| 108 params->SetDouble("paperHeight", 2); |
| 109 response = ParsePrintSettings(0, params.get(), &settings); |
| 110 EXPECT_NE(nullptr, response); |
| 111 |
| 112 params->SetDouble("paperWidth", 1); |
| 113 params->SetDouble("paperHeight", -2); |
| 114 response = ParsePrintSettings(0, params.get(), &settings); |
| 115 EXPECT_NE(nullptr, response); |
| 116 } |
| 117 |
| 118 TEST(ParsePrintSettingsTest, Margin) { |
| 119 printing::HeadlessPrintSettings settings; |
| 120 |
| 121 auto params = base::MakeUnique<base::DictionaryValue>(); |
| 122 std::unique_ptr<base::DictionaryValue> response = |
| 123 ParsePrintSettings(0, params.get(), &settings); |
| 124 int default_margin = |
| 125 1000 * printing::kPointsPerInch / printing::kHundrethsMMPerInch; |
| 126 EXPECT_DOUBLE_EQ(default_margin, settings.margins_in_points.top); |
| 127 EXPECT_DOUBLE_EQ(default_margin, settings.margins_in_points.bottom); |
| 128 EXPECT_DOUBLE_EQ(default_margin, settings.margins_in_points.left); |
| 129 EXPECT_DOUBLE_EQ(default_margin, settings.margins_in_points.right); |
| 130 EXPECT_EQ(nullptr, response); |
| 131 |
| 132 params->SetDouble("marginTop", 1); |
| 133 params->SetDouble("marginBottom", 2); |
| 134 params->SetDouble("marginLeft", 3); |
| 135 params->SetDouble("marginRight", 4); |
| 136 response = ParsePrintSettings(0, params.get(), &settings); |
| 137 EXPECT_DOUBLE_EQ(1 * printing::kPointsPerInch, |
| 138 settings.margins_in_points.top); |
| 139 EXPECT_DOUBLE_EQ(2 * printing::kPointsPerInch, |
| 140 settings.margins_in_points.bottom); |
| 141 EXPECT_DOUBLE_EQ(3 * printing::kPointsPerInch, |
| 142 settings.margins_in_points.left); |
| 143 EXPECT_DOUBLE_EQ(4 * printing::kPointsPerInch, |
| 144 settings.margins_in_points.right); |
| 145 EXPECT_EQ(nullptr, response); |
| 146 |
| 147 params->SetDouble("marginTop", -1); |
| 148 response = ParsePrintSettings(0, params.get(), &settings); |
| 149 EXPECT_NE(nullptr, response); |
| 150 } |
| 151 |
| 152 TEST(PageRangeTextToPagesTest, General) { |
| 153 using PM = printing::HeadlessPrintManager; |
| 154 std::vector<int> pages; |
| 155 std::vector<int> expected_pages; |
| 156 |
| 157 PM::PageRangeStatus status = PM::PageRangeTextToPages("-", 10, &pages); |
| 158 expected_pages = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 159 EXPECT_EQ(expected_pages, pages); |
| 160 EXPECT_EQ(PM::NO_ERROR, status); |
| 161 |
| 162 status = PM::PageRangeTextToPages("-5", 10, &pages); |
| 163 expected_pages = {0, 1, 2, 3, 4}; |
| 164 EXPECT_EQ(expected_pages, pages); |
| 165 EXPECT_EQ(PM::NO_ERROR, status); |
| 166 |
| 167 status = PM::PageRangeTextToPages("5-", 10, &pages); |
| 168 expected_pages = {4, 5, 6, 7, 8, 9}; |
| 169 EXPECT_EQ(expected_pages, pages); |
| 170 EXPECT_EQ(PM::NO_ERROR, status); |
| 171 |
| 172 status = PM::PageRangeTextToPages("1-3,9-10,4-6", 10, &pages); |
| 173 expected_pages = {0, 1, 2, 3, 4, 5, 8, 9}; |
| 174 EXPECT_EQ(expected_pages, pages); |
| 175 EXPECT_EQ(PM::NO_ERROR, status); |
| 176 |
| 177 status = PM::PageRangeTextToPages("1- 3, 9-10,4 -6", 10, &pages); |
| 178 expected_pages = {0, 1, 2, 3, 4, 5, 8, 9}; |
| 179 EXPECT_EQ(expected_pages, pages); |
| 180 EXPECT_EQ(PM::NO_ERROR, status); |
| 181 |
| 182 status = PM::PageRangeTextToPages("1-3,9-10,4-6", 5, &pages); |
| 183 EXPECT_EQ(PM::LIMIT_ERROR, status); |
| 184 |
| 185 status = PM::PageRangeTextToPages("abcd", 10, &pages); |
| 186 EXPECT_EQ(PM::SYNTAX_ERROR, status); |
| 187 |
| 188 status = PM::PageRangeTextToPages("1-3,9-a10,4-6", 10, &pages); |
| 189 EXPECT_EQ(PM::SYNTAX_ERROR, status); |
| 190 } |
| 191 |
| 192 } // namespace headless |
OLD | NEW |