Commit 0f7cafd6 authored by Daniel Hosseinian's avatar Daniel Hosseinian Committed by Commit Bot

Move and test CUPS IPP color model selector

The version of GetColorModelForMode() that ChromeOS uses will be used
by macOS. Rename it to GetIppColorModelForMode() and move it to
print_settings.h. Meanwhile, add some simple tests.

Bug: 1106437
Change-Id: Ic01d1cf242c6e77774d247253f6cd912c2566f5b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2303855
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#789717}
parent e312d44e
...@@ -10,6 +10,10 @@ ...@@ -10,6 +10,10 @@
#include "printing/print_job_constants.h" #include "printing/print_job_constants.h"
#include "printing/units.h" #include "printing/units.h"
#if defined(USE_CUPS) && (defined(OS_MACOSX) || defined(OS_CHROMEOS))
#include <cups/cups.h>
#endif
namespace printing { namespace printing {
namespace { namespace {
...@@ -183,6 +187,23 @@ void GetColorModelForMode(int color_mode, ...@@ -183,6 +187,23 @@ void GetColorModelForMode(int color_mode,
// The default case is excluded from the above switch statement to ensure that // The default case is excluded from the above switch statement to ensure that
// all ColorModel values are determinantly handled. // all ColorModel values are determinantly handled.
} }
#if defined(OS_MACOSX) || defined(OS_CHROMEOS)
std::string GetIppColorModelForMode(int color_mode) {
// Accept |UNKNOWN_COLOR_MODEL| for consistency with GetColorModelForMode().
if (color_mode == UNKNOWN_COLOR_MODEL)
return CUPS_PRINT_COLOR_MODE_MONOCHROME;
base::Optional<bool> is_color = IsColorModelSelected(color_mode);
if (!is_color.has_value()) {
NOTREACHED();
return std::string();
}
return is_color.value() ? CUPS_PRINT_COLOR_MODE_COLOR
: CUPS_PRINT_COLOR_MODE_MONOCHROME;
}
#endif // defined(OS_MACOSX) || defined(OS_CHROMEOS)
#endif // defined(USE_CUPS) #endif // defined(USE_CUPS)
base::Optional<bool> IsColorModelSelected(int color_mode) { base::Optional<bool> IsColorModelSelected(int color_mode) {
......
...@@ -37,7 +37,12 @@ PRINTING_EXPORT base::Optional<bool> IsColorModelSelected(int color_mode); ...@@ -37,7 +37,12 @@ PRINTING_EXPORT base::Optional<bool> IsColorModelSelected(int color_mode);
PRINTING_EXPORT void GetColorModelForMode(int color_mode, PRINTING_EXPORT void GetColorModelForMode(int color_mode,
std::string* color_setting_name, std::string* color_setting_name,
std::string* color_value); std::string* color_value);
#if defined(OS_MACOSX) || defined(OS_CHROMEOS)
// Convert from |color_mode| to a print-color-mode value from PWG 5100.13.
PRINTING_EXPORT std::string GetIppColorModelForMode(int color_mode);
#endif #endif
#endif // defined(USE_CUPS)
// Inform the printing system that it may embed this user-agent string // Inform the printing system that it may embed this user-agent string
// in its output's metadata. // in its output's metadata.
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "printing/print_settings.h" #include "printing/print_settings.h"
#include "base/test/gtest_util.h" #include "base/test/gtest_util.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace printing { namespace printing {
...@@ -43,6 +44,19 @@ TEST(PrintSettingsDeathTest, GetColorModelForModeEdges) { ...@@ -43,6 +44,19 @@ TEST(PrintSettingsDeathTest, GetColorModelForModeEdges) {
EXPECT_DCHECK_DEATH(GetColorModelForMode(COLOR_MODEL_LAST + 1, EXPECT_DCHECK_DEATH(GetColorModelForMode(COLOR_MODEL_LAST + 1,
&color_setting_name, &color_value)); &color_setting_name, &color_value));
} }
#if defined(OS_MACOSX) || defined(OS_CHROMEOS)
TEST(PrintSettingsTest, GetIppColorModelForMode) {
for (int model = UNKNOWN_COLOR_MODEL; model <= COLOR_MODEL_LAST; ++model)
EXPECT_FALSE(GetIppColorModelForMode(model).empty());
}
TEST(PrintSettingsDeathTest, GetIppColorModelForModeEdges) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
EXPECT_DCHECK_DEATH(GetIppColorModelForMode(UNKNOWN_COLOR_MODEL - 1));
EXPECT_DCHECK_DEATH(GetIppColorModelForMode(COLOR_MODEL_LAST + 1));
}
#endif // defined(OS_MACOSX) || defined(OS_CHROMEOS)
#endif // defined(USE_CUPS) #endif // defined(USE_CUPS)
} // namespace printing } // namespace printing
...@@ -36,20 +36,6 @@ namespace printing { ...@@ -36,20 +36,6 @@ namespace printing {
namespace { namespace {
// Convert from a ColorMode setting to a print-color-mode value from PWG 5100.13
const char* GetColorModelForMode(int color_mode) {
const char* mode_string;
base::Optional<bool> is_color = IsColorModelSelected(color_mode);
if (is_color.has_value()) {
mode_string = is_color.value() ? CUPS_PRINT_COLOR_MODE_COLOR
: CUPS_PRINT_COLOR_MODE_MONOCHROME;
} else {
mode_string = nullptr;
}
return mode_string;
}
// Returns a new char buffer which is a null-terminated copy of |value|. The // Returns a new char buffer which is a null-terminated copy of |value|. The
// caller owns the returned string. // caller owns the returned string.
char* DuplicateString(const base::StringPiece value) { char* DuplicateString(const base::StringPiece value) {
...@@ -138,7 +124,7 @@ std::vector<ScopedCupsOption> SettingsToCupsOptions( ...@@ -138,7 +124,7 @@ std::vector<ScopedCupsOption> SettingsToCupsOptions(
std::vector<ScopedCupsOption> options; std::vector<ScopedCupsOption> options;
options.push_back( options.push_back(
ConstructOption(kIppColor, ConstructOption(kIppColor,
GetColorModelForMode(settings.color()))); // color GetIppColorModelForMode(settings.color()))); // color
options.push_back(ConstructOption(kIppDuplex, sides)); // duplexing options.push_back(ConstructOption(kIppDuplex, sides)); // duplexing
options.push_back( options.push_back(
ConstructOption(kIppMedia, ConstructOption(kIppMedia,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment