Index: headless/lib/browser/headless_printing_unittest.cc |
diff --git a/headless/lib/browser/headless_printing_unittest.cc b/headless/lib/browser/headless_printing_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f059b46b0d0fd60cbc52077f1dc39e82e36a3821 |
--- /dev/null |
+++ b/headless/lib/browser/headless_printing_unittest.cc |
@@ -0,0 +1,192 @@ |
+// Copyright 2017 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 "base/memory/ptr_util.h" |
+#include "base/values.h" |
+#include "headless/lib/browser/headless_devtools_manager_delegate.h" |
+#include "headless/lib/browser/headless_print_manager.h" |
+#include "printing/features/features.h" |
+#include "printing/units.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace headless { |
+ |
+TEST(ParsePrintSettingsTest, Landscape) { |
+ printing::HeadlessPrintSettings settings; |
+ EXPECT_FALSE(settings.landscape); |
+ |
+ auto params = base::MakeUnique<base::DictionaryValue>(); |
+ params->SetBoolean("landscape", true); |
+ std::unique_ptr<base::DictionaryValue> response = |
+ ParsePrintSettings(0, params.get(), &settings); |
+ EXPECT_TRUE(settings.landscape); |
+ EXPECT_EQ(nullptr, response); |
+} |
+ |
+TEST(ParsePrintSettingsTest, HeaderFooter) { |
+ printing::HeadlessPrintSettings settings; |
+ EXPECT_FALSE(settings.display_header_footer); |
+ |
+ auto params = base::MakeUnique<base::DictionaryValue>(); |
+ params->SetBoolean("displayHeaderFooter", true); |
+ std::unique_ptr<base::DictionaryValue> response = |
+ ParsePrintSettings(0, params.get(), &settings); |
+ EXPECT_TRUE(settings.display_header_footer); |
+ EXPECT_EQ(nullptr, response); |
+} |
+ |
+TEST(ParsePrintSettingsTest, PrintBackground) { |
+ printing::HeadlessPrintSettings settings; |
+ EXPECT_FALSE(settings.should_print_backgrounds); |
+ |
+ auto params = base::MakeUnique<base::DictionaryValue>(); |
+ params->SetBoolean("printBackground", true); |
+ std::unique_ptr<base::DictionaryValue> response = |
+ ParsePrintSettings(0, params.get(), &settings); |
+ EXPECT_TRUE(settings.should_print_backgrounds); |
+ EXPECT_EQ(nullptr, response); |
+} |
+ |
+TEST(ParsePrintSettingsTest, Scale) { |
+ printing::HeadlessPrintSettings settings; |
+ EXPECT_DOUBLE_EQ(1, settings.scale); |
+ |
+ auto params = base::MakeUnique<base::DictionaryValue>(); |
+ params->SetDouble("scale", 0.5); |
+ std::unique_ptr<base::DictionaryValue> response = |
+ ParsePrintSettings(0, params.get(), &settings); |
+ EXPECT_DOUBLE_EQ(0.5, settings.scale); |
+ EXPECT_EQ(nullptr, response); |
+ |
+ params->SetDouble("scale", -1); |
+ response = ParsePrintSettings(0, params.get(), &settings); |
+ EXPECT_NE(nullptr, response); |
+ |
+ params->SetDouble("scale", 10); |
+ response = ParsePrintSettings(0, params.get(), &settings); |
+ EXPECT_NE(nullptr, response); |
+} |
+ |
+TEST(ParsePrintSettingsTest, PageRanges) { |
+ printing::HeadlessPrintSettings settings; |
+ EXPECT_EQ("", settings.page_ranges); |
+ |
+ auto params = base::MakeUnique<base::DictionaryValue>(); |
+ params->SetString("pageRanges", "abcd"); |
+ std::unique_ptr<base::DictionaryValue> response = |
+ ParsePrintSettings(0, params.get(), &settings); |
+ // Pass pageRanges text to settings directly and return no error, even if |
+ // it is invalid. The validation is deferred to HeadlessPrintMangager once the |
+ // total page count is available. See the PageRangeTextToPagesTest. |
+ EXPECT_EQ("abcd", settings.page_ranges); |
+ EXPECT_EQ(nullptr, response); |
+} |
+ |
+TEST(ParsePrintSettingsTest, Paper) { |
+ printing::HeadlessPrintSettings settings; |
+ |
+ auto params = base::MakeUnique<base::DictionaryValue>(); |
+ std::unique_ptr<base::DictionaryValue> response = |
+ ParsePrintSettings(0, params.get(), &settings); |
+ EXPECT_EQ(printing::kLetterWidthInch * printing::kPointsPerInch, |
+ settings.paper_size_in_points.width()); |
+ EXPECT_EQ(printing::kLetterHeightInch * printing::kPointsPerInch, |
+ settings.paper_size_in_points.height()); |
+ EXPECT_EQ(nullptr, response); |
+ |
+ params->SetDouble("paperWidth", 1); |
+ params->SetDouble("paperHeight", 2); |
+ response = ParsePrintSettings(0, params.get(), &settings); |
+ EXPECT_EQ(1 * printing::kPointsPerInch, |
+ settings.paper_size_in_points.width()); |
+ EXPECT_EQ(2 * printing::kPointsPerInch, |
+ settings.paper_size_in_points.height()); |
+ EXPECT_EQ(nullptr, response); |
+ |
+ params->SetDouble("paperWidth", -1); |
+ params->SetDouble("paperHeight", 2); |
+ response = ParsePrintSettings(0, params.get(), &settings); |
+ EXPECT_NE(nullptr, response); |
+ |
+ params->SetDouble("paperWidth", 1); |
+ params->SetDouble("paperHeight", -2); |
+ response = ParsePrintSettings(0, params.get(), &settings); |
+ EXPECT_NE(nullptr, response); |
+} |
+ |
+TEST(ParsePrintSettingsTest, Margin) { |
+ printing::HeadlessPrintSettings settings; |
+ |
+ auto params = base::MakeUnique<base::DictionaryValue>(); |
+ std::unique_ptr<base::DictionaryValue> response = |
+ ParsePrintSettings(0, params.get(), &settings); |
+ int default_margin = |
+ 1000 * printing::kPointsPerInch / printing::kHundrethsMMPerInch; |
+ EXPECT_DOUBLE_EQ(default_margin, settings.margins_in_points.top); |
+ EXPECT_DOUBLE_EQ(default_margin, settings.margins_in_points.bottom); |
+ EXPECT_DOUBLE_EQ(default_margin, settings.margins_in_points.left); |
+ EXPECT_DOUBLE_EQ(default_margin, settings.margins_in_points.right); |
+ EXPECT_EQ(nullptr, response); |
+ |
+ params->SetDouble("marginTop", 1); |
+ params->SetDouble("marginBottom", 2); |
+ params->SetDouble("marginLeft", 3); |
+ params->SetDouble("marginRight", 4); |
+ response = ParsePrintSettings(0, params.get(), &settings); |
+ EXPECT_DOUBLE_EQ(1 * printing::kPointsPerInch, |
+ settings.margins_in_points.top); |
+ EXPECT_DOUBLE_EQ(2 * printing::kPointsPerInch, |
+ settings.margins_in_points.bottom); |
+ EXPECT_DOUBLE_EQ(3 * printing::kPointsPerInch, |
+ settings.margins_in_points.left); |
+ EXPECT_DOUBLE_EQ(4 * printing::kPointsPerInch, |
+ settings.margins_in_points.right); |
+ EXPECT_EQ(nullptr, response); |
+ |
+ params->SetDouble("marginTop", -1); |
+ response = ParsePrintSettings(0, params.get(), &settings); |
+ EXPECT_NE(nullptr, response); |
+} |
+ |
+TEST(PageRangeTextToPagesTest, General) { |
+ using PM = printing::HeadlessPrintManager; |
+ std::vector<int> pages; |
+ std::vector<int> expected_pages; |
+ |
+ PM::PageRangeStatus status = PM::PageRangeTextToPages("-", 10, &pages); |
+ expected_pages = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
+ EXPECT_EQ(expected_pages, pages); |
+ EXPECT_EQ(PM::NO_ERROR, status); |
+ |
+ status = PM::PageRangeTextToPages("-5", 10, &pages); |
+ expected_pages = {0, 1, 2, 3, 4}; |
+ EXPECT_EQ(expected_pages, pages); |
+ EXPECT_EQ(PM::NO_ERROR, status); |
+ |
+ status = PM::PageRangeTextToPages("5-", 10, &pages); |
+ expected_pages = {4, 5, 6, 7, 8, 9}; |
+ EXPECT_EQ(expected_pages, pages); |
+ EXPECT_EQ(PM::NO_ERROR, status); |
+ |
+ status = PM::PageRangeTextToPages("1-3,9-10,4-6", 10, &pages); |
+ expected_pages = {0, 1, 2, 3, 4, 5, 8, 9}; |
+ EXPECT_EQ(expected_pages, pages); |
+ EXPECT_EQ(PM::NO_ERROR, status); |
+ |
+ status = PM::PageRangeTextToPages("1- 3, 9-10,4 -6", 10, &pages); |
+ expected_pages = {0, 1, 2, 3, 4, 5, 8, 9}; |
+ EXPECT_EQ(expected_pages, pages); |
+ EXPECT_EQ(PM::NO_ERROR, status); |
+ |
+ status = PM::PageRangeTextToPages("1-3,9-10,4-6", 5, &pages); |
+ EXPECT_EQ(PM::LIMIT_ERROR, status); |
+ |
+ status = PM::PageRangeTextToPages("abcd", 10, &pages); |
+ EXPECT_EQ(PM::SYNTAX_ERROR, status); |
+ |
+ status = PM::PageRangeTextToPages("1-3,9-a10,4-6", 10, &pages); |
+ EXPECT_EQ(PM::SYNTAX_ERROR, status); |
+} |
+ |
+} // namespace headless |