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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/print_preview/local_printer_handler_chromeos.h"
#include <algorithm>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/memory/ref_counted.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/values_test_util.h"
#include "base/values.h"
#include "chrome/test/chromeos/printing/fake_local_printer_chromeos.h"
#include "chromeos/crosapi/mojom/local_printer.mojom.h"
#include "content/public/test/browser_task_environment.h"
#include "printing/backend/print_backend.h"
#include "printing/print_job_constants.h"
#include "printing/print_settings_conversion_chromeos.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace printing {
namespace {
using ::crosapi::mojom::GetOAuthAccessTokenResult;
using ::crosapi::mojom::LocalPrinter;
using ::crosapi::mojom::OAuthNotNeeded;
using ::printing::mojom::IppClientInfo;
using ::printing::mojom::IppClientInfoPtr;
using ::testing::ElementsAre;
using ::testing::Invoke;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::WithArg;
constexpr auto kStatusTimestamp = base::Time::FromSecondsSinceUnixEpoch(1e9);
// A `LocalPrinter` implementation where all functions run callbacks with
// reasonable default values.
class TestLocalPrinter : public FakeLocalPrinter {
public:
void GetUsernamePerPolicy(GetUsernamePerPolicyCallback callback) override {
std::move(callback).Run(std::nullopt);
}
void GetOAuthAccessToken(const std::string& printer_id,
GetOAuthAccessTokenCallback callback) override {
std::move(callback).Run(
GetOAuthAccessTokenResult::NewNone(OAuthNotNeeded::New()));
}
void GetIppClientInfo(const std::string& printer_id,
GetIppClientInfoCallback callback) override {
std::move(callback).Run({});
}
};
class MockLocalPrinter : public TestLocalPrinter {
public:
MOCK_METHOD(void,
GetUsernamePerPolicy,
(GetUsernamePerPolicyCallback callback));
MOCK_METHOD(void,
GetOAuthAccessToken,
(const std::string& printer_id,
GetOAuthAccessTokenCallback callback));
MOCK_METHOD(void,
GetIppClientInfo,
(const std::string& printer_id,
GetIppClientInfoCallback callback));
void DelegateToBase() {
ON_CALL(*this, GetUsernamePerPolicy)
.WillByDefault([this](GetUsernamePerPolicyCallback cb) {
return TestLocalPrinter::GetUsernamePerPolicy(std::move(cb));
});
ON_CALL(*this, GetOAuthAccessToken)
.WillByDefault([this](const std::string& printer_id,
GetOAuthAccessTokenCallback cb) {
return TestLocalPrinter::GetOAuthAccessToken(printer_id,
std::move(cb));
});
ON_CALL(*this, GetIppClientInfo)
.WillByDefault([this](const std::string& printer_id,
GetIppClientInfoCallback cb) {
return TestLocalPrinter::GetIppClientInfo(printer_id, std::move(cb));
});
}
};
// Used as a callback to `StartGetPrinters()` in tests.
// Increases `call_count` and records values returned by `StartGetPrinters()`.
void RecordPrinterList(size_t& call_count,
base::Value::List& printers_out,
base::Value::List printers) {
++call_count;
printers_out = std::move(printers);
}
// Used as a callback to `StartGetPrinters` in tests.
// Records that the test is done.
void RecordPrintersDone(bool& is_done_out) {
is_done_out = true;
}
void RecordGetCapability(base::Value::Dict& capabilities_out,
base::Value::Dict capability) {
capabilities_out = std::move(capability);
}
void RecordGetEulaUrl(std::string& fetched_eula_url,
const std::string& eula_url) {
fetched_eula_url = eula_url;
}
void RecordAshJobSettings(base::Value::Dict& fetched_settings,
base::Value::Dict settings) {
fetched_settings = std::move(settings);
}
const base::Value::Dict kInitialJobSettings = base::test::ParseJsonDict(R"({
"key": "value"
})");
} // namespace
// Test that the printer handler runs callbacks with reasonable defaults when
// the mojo connection to ash cannot be established, which should never occur in
// production but may occur in unit/browser tests.
class LocalPrinterHandlerChromeosNoAshTest : public testing::Test {
public:
LocalPrinterHandlerChromeosNoAshTest() = default;
LocalPrinterHandlerChromeosNoAshTest(
const LocalPrinterHandlerChromeosNoAshTest&) = delete;
LocalPrinterHandlerChromeosNoAshTest& operator=(
const LocalPrinterHandlerChromeosNoAshTest&) = delete;
~LocalPrinterHandlerChromeosNoAshTest() override = default;
void SetUp() override {
local_printer_handler_ = LocalPrinterHandlerChromeos::CreateForTesting(
/*local_printer=*/nullptr);
}
LocalPrinterHandlerChromeos* local_printer_handler() {
return local_printer_handler_.get();
}
private:
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<LocalPrinterHandlerChromeos> local_printer_handler_;
};
// Test that the printer handler runs callbacks with the correct values received
// from a mocked mojo connection to ash.
class LocalPrinterHandlerChromeosWithAshTest : public testing::Test {
public:
LocalPrinterHandlerChromeosWithAshTest() = default;
LocalPrinterHandlerChromeosWithAshTest(
const LocalPrinterHandlerChromeosWithAshTest&) = delete;
LocalPrinterHandlerChromeosWithAshTest& operator=(
const LocalPrinterHandlerChromeosWithAshTest&) = delete;
~LocalPrinterHandlerChromeosWithAshTest() override = default;
void SetUp() override {
local_printer_handler_ =
LocalPrinterHandlerChromeos::CreateForTesting(&local_printer_);
}
LocalPrinterHandlerChromeos* local_printer_handler() {
return local_printer_handler_.get();
}
MockLocalPrinter& local_printer() { return local_printer_; }
private:
NiceMock<MockLocalPrinter> local_printer_;
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<LocalPrinterHandlerChromeos> local_printer_handler_;
};
TEST_F(LocalPrinterHandlerChromeosNoAshTest,
PrinterStatusRequest_ProvidesDefaultValue) {
std::optional<base::Value::Dict> printer_status = base::Value::Dict();
local_printer_handler()->StartPrinterStatusRequest(
"printer1",
base::BindLambdaForTesting([&](std::optional<base::Value::Dict> status) {
printer_status = std::move(status);
}));
EXPECT_EQ(std::nullopt, printer_status);
}
TEST_F(LocalPrinterHandlerChromeosNoAshTest, GetPrinters_ProvidesDefaultValue) {
size_t call_count = 0;
base::Value::List printers;
bool is_done = false;
local_printer_handler()->StartGetPrinters(
base::BindRepeating(&RecordPrinterList, std::ref(call_count),
std::ref(printers)),
base::BindOnce(&RecordPrintersDone, std::ref(is_done)));
// RecordPrinterList is called when printers are discovered. If no printers
// are discovered, the function is not called.
EXPECT_EQ(0u, call_count);
// RecordPrintersDone is called once printer discovery is finished, even if no
// printers have been discovered.
EXPECT_TRUE(is_done);
}
TEST_F(LocalPrinterHandlerChromeosNoAshTest,
GetDefaultPrinter_ProvidesDefaultValue) {
std::string default_printer = "unset";
local_printer_handler()->GetDefaultPrinter(base::BindLambdaForTesting(
[&](const std::string& printer) { default_printer = printer; }));
EXPECT_EQ("", default_printer);
}
TEST_F(LocalPrinterHandlerChromeosNoAshTest,
GetCapability_ProvidesDefaultValue) {
base::Value::Dict fetched_caps;
local_printer_handler()->StartGetCapability(
"printer1", base::BindOnce(&RecordGetCapability, std::ref(fetched_caps)));
EXPECT_TRUE(fetched_caps.empty());
}
TEST_F(LocalPrinterHandlerChromeosNoAshTest, GetEulaUrl_ProvidesDefaultValue) {
std::string fetched_eula_url = "unset";
local_printer_handler()->StartGetEulaUrl(
"printer1",
base::BindOnce(&RecordGetEulaUrl, std::ref(fetched_eula_url)));
EXPECT_EQ("", fetched_eula_url);
}
TEST_F(LocalPrinterHandlerChromeosNoAshTest, GetAshJobSettingsEmpty) {
base::Value::Dict fetched_settings;
local_printer_handler()->GetAshJobSettingsForTesting(
"printer1",
base::BindOnce(&RecordAshJobSettings, std::ref(fetched_settings)),
kInitialJobSettings.Clone());
EXPECT_EQ(fetched_settings, kInitialJobSettings);
}
TEST_F(LocalPrinterHandlerChromeosWithAshTest, GetAshJobSettingsEmpty) {
local_printer().DelegateToBase();
base::Value::Dict fetched_settings;
local_printer_handler()->GetAshJobSettingsForTesting(
"printer1",
base::BindOnce(&RecordAshJobSettings, std::ref(fetched_settings)),
kInitialJobSettings.Clone());
EXPECT_EQ(fetched_settings, kInitialJobSettings);
}
TEST_F(LocalPrinterHandlerChromeosWithAshTest, GetAshJobSettingsUsername) {
local_printer().DelegateToBase();
auto return_expected_username =
[](LocalPrinter::GetUsernamePerPolicyCallback cb) {
std::move(cb).Run("chronos");
};
EXPECT_CALL(local_printer(), GetUsernamePerPolicy)
.WillOnce(WithArg<0>(Invoke(return_expected_username)));
base::Value::Dict fetched_settings;
local_printer_handler()->GetAshJobSettingsForTesting(
"printer1",
base::BindOnce(&RecordAshJobSettings, std::ref(fetched_settings)),
kInitialJobSettings.Clone());
// Test that `username` and `sendUserInfo` are in job settings, together with
// the old settings.
const base::Value::Dict kExpectedValue = base::test::ParseJsonDict(R"({
"key": "value",
"username": "chronos",
"sendUserInfo": true
})");
EXPECT_EQ(fetched_settings, kExpectedValue);
}
TEST_F(LocalPrinterHandlerChromeosWithAshTest, GetAshJobSettingsOAuthToken) {
local_printer().DelegateToBase();
auto return_expected_oauth_token =
[](LocalPrinter::GetOAuthAccessTokenCallback cb) {
std::move(cb).Run(GetOAuthAccessTokenResult::NewToken(
crosapi::mojom::OAuthAccessToken::New("token")));
};
EXPECT_CALL(local_printer(), GetOAuthAccessToken)
.WillOnce(WithArg<1>(Invoke(return_expected_oauth_token)));
base::Value::Dict fetched_settings;
local_printer_handler()->GetAshJobSettingsForTesting(
"printer1",
base::BindOnce(&RecordAshJobSettings, std::ref(fetched_settings)),
kInitialJobSettings.Clone());
// Test that oauth token is in job settings, together with the old settings.
const base::Value::Dict kExpectedValue = base::test::ParseJsonDict(R"({
"key": "value",
"chromeos-access-oauth-token": "token"
})");
EXPECT_EQ(fetched_settings, kExpectedValue);
}
TEST_F(LocalPrinterHandlerChromeosWithAshTest,
GetAshJobSettingsClientInfoEmptyPrinterId) {
local_printer().DelegateToBase();
EXPECT_CALL(local_printer(), GetIppClientInfo).Times(0);
base::Value::Dict fetched_settings;
local_printer_handler()->GetAshJobSettingsForTesting(
"", base::BindOnce(&RecordAshJobSettings, std::ref(fetched_settings)),
kInitialJobSettings.Clone());
EXPECT_EQ(fetched_settings, kInitialJobSettings);
}
TEST_F(LocalPrinterHandlerChromeosWithAshTest, GetAshJobSettingsClientInfo) {
local_printer().DelegateToBase();
const std::vector<IppClientInfo> expected_client_info{
{IppClientInfo::ClientType::kOperatingSystem, "ChromeOS", "patch",
"str_version", "version"},
{IppClientInfo::ClientType::kOther, "chromebook-42", std::nullopt, "",
std::nullopt}};
auto return_expected_client_info =
[client_info =
expected_client_info](LocalPrinter::GetIppClientInfoCallback cb) {
std::vector<IppClientInfoPtr> client_infos_to_send;
client_infos_to_send.push_back(client_info[0].Clone());
client_infos_to_send.push_back(client_info[1].Clone());
std::move(cb).Run(std::move(client_infos_to_send));
};
EXPECT_CALL(local_printer(), GetIppClientInfo)
.WillOnce(WithArg<1>(Invoke(std::move(return_expected_client_info))));
base::Value::Dict fetched_settings;
local_printer_handler()->GetAshJobSettingsForTesting(
"printer1",
base::BindOnce(&RecordAshJobSettings, std::ref(fetched_settings)),
kInitialJobSettings.Clone());
// Test that oauth token is in job settings, together with the old settings.
const base::Value::Dict kExpectedValue = base::test::ParseJsonDict(R"({
"key": "value",
"ipp-client-info": [
{
"ipp-client-type": 4,
"ipp-client-name": "ChromeOS",
"ipp-client-patches": "patch",
"ipp-client-string-version": "str_version",
"ipp-client-version": "version"
},
{
"ipp-client-type": 6,
"ipp-client-name": "chromebook-42",
"ipp-client-string-version": "",
},
]
})");
EXPECT_EQ(fetched_settings, kExpectedValue);
}
TEST(LocalPrinterHandlerChromeos, PrinterToValue) {
// Printer status.
crosapi::mojom::PrinterStatusPtr status =
crosapi::mojom::PrinterStatus::New();
status->printer_id = "printer_id";
status->timestamp = kStatusTimestamp;
status->status_reasons.push_back(crosapi::mojom::StatusReason::New(
crosapi::mojom::StatusReason::Reason::kOutOfInk,
crosapi::mojom::StatusReason::Severity::kWarning));
// Managed print options.
crosapi::mojom::ManagedPrintOptionsPtr managed_print_options =
crosapi::mojom::ManagedPrintOptions::New();
managed_print_options->color = crosapi::mojom::BoolOption::New();
managed_print_options->color->default_value = false;
managed_print_options->color->allowed_values = {false, true};
crosapi::mojom::LocalDestinationInfo input(
"device_name", "printer_name", "printer_description", false, "",
std::move(status), std::move(managed_print_options));
const base::Value kExpectedValue = base::test::ParseJson(R"({
"cupsEnterprisePrinter": false,
"deviceName": "device_name",
"managedPrintOptions": {
"color": {
"defaultValue": false,
"allowedValues": [false, true],
},
},
"printerDescription": "printer_description",
"printerName": "printer_name",
"printerStatus": {
"printerId": "printer_id",
"statusReasons": [ {
"reason": 6,
"severity": 2
} ],
"timestamp": 1e+12
}
})");
EXPECT_EQ(kExpectedValue, LocalPrinterHandlerChromeos::PrinterToValue(input));
}
TEST(LocalPrinterHandlerChromeos, PrinterToValue_ConfiguredViaPolicy) {
crosapi::mojom::LocalDestinationInfo printer("device_name", "printer_name",
"printer_description", true);
const base::Value kExpectedValue = base::test::ParseJson(R"({
"cupsEnterprisePrinter": true,
"deviceName": "device_name",
"managedPrintOptions": {},
"printerDescription": "printer_description",
"printerName": "printer_name",
"printerStatus": {}
})");
EXPECT_EQ(kExpectedValue,
LocalPrinterHandlerChromeos::PrinterToValue(printer));
}
TEST(LocalPrinterHandlerChromeos, ManagedPrintOptionsToValue_MediaSize) {
crosapi::mojom::ManagedPrintOptions managed_print_options;
managed_print_options.media_size = crosapi::mojom::SizeOption::New();
managed_print_options.media_size->default_value = crosapi::mojom::Size::New();
managed_print_options.media_size->default_value->width = 5;
managed_print_options.media_size->default_value->height = 10;
std::vector<crosapi::mojom::SizePtr> allowed_values(2);
allowed_values[0] = crosapi::mojom::Size::New(5, 10);
allowed_values[1] = crosapi::mojom::Size::New(15, 20);
managed_print_options.media_size->allowed_values = std::move(allowed_values);
const base::Value::Dict managed_print_options_dict =
LocalPrinterHandlerChromeos::ManagedPrintOptionsToValue(
managed_print_options);
EXPECT_EQ(
*managed_print_options_dict.FindDict(kManagedPrintOptions_MediaSize)
->FindDict(kManagedPrintOptions_DefaultValue),
base::Value::Dict()
.Set(kManagedPrintOptions_SizeWidth, 5)
.Set(kManagedPrintOptions_SizeHeight, 10));
EXPECT_EQ(
*managed_print_options_dict.FindDict(kManagedPrintOptions_MediaSize)
->FindList(kManagedPrintOptions_AllowedValues),
base::Value::List()
.Append(base::Value::Dict()
.Set(kManagedPrintOptions_SizeWidth, 5)
.Set(kManagedPrintOptions_SizeHeight, 10))
.Append(base::Value::Dict()
.Set(kManagedPrintOptions_SizeWidth, 15)
.Set(kManagedPrintOptions_SizeHeight, 20)));
}
TEST(LocalPrinterHandlerChromeos, ManagedPrintOptionsToValue_MediaType) {
crosapi::mojom::ManagedPrintOptions managed_print_options;
managed_print_options.media_type = crosapi::mojom::StringOption::New();
managed_print_options.media_type->default_value = "paper";
managed_print_options.media_type->allowed_values = {"paper", "metal", "wood"};
const base::Value::Dict managed_print_options_dict =
LocalPrinterHandlerChromeos::ManagedPrintOptionsToValue(
managed_print_options);
EXPECT_EQ(
*managed_print_options_dict.FindDict(kManagedPrintOptions_MediaType)
->FindString(kManagedPrintOptions_DefaultValue),
"paper");
EXPECT_THAT(
*managed_print_options_dict.FindDict(kManagedPrintOptions_MediaType)
->FindList(kManagedPrintOptions_AllowedValues),
ElementsAre("paper", "metal", "wood"));
}
TEST(LocalPrinterHandlerChromeos, ManagedPrintOptionsToValue_Duplex) {
crosapi::mojom::ManagedPrintOptions managed_print_options;
managed_print_options.duplex = crosapi::mojom::DuplexOption::New();
managed_print_options.duplex->default_value =
crosapi::mojom::DuplexType::kOneSided;
managed_print_options.duplex->allowed_values = {
crosapi::mojom::DuplexType::kOneSided,
crosapi::mojom::DuplexType::kShortEdge};
const base::Value::Dict managed_print_options_dict =
LocalPrinterHandlerChromeos::ManagedPrintOptionsToValue(
managed_print_options);
EXPECT_EQ(*managed_print_options_dict.FindDict(kManagedPrintOptions_Duplex)
->FindInt(kManagedPrintOptions_DefaultValue),
static_cast<int>(crosapi::mojom::DuplexType::kOneSided));
EXPECT_THAT(
*managed_print_options_dict.FindDict(kManagedPrintOptions_Duplex)
->FindList(kManagedPrintOptions_AllowedValues),
ElementsAre(static_cast<int>(crosapi::mojom::DuplexType::kOneSided),
static_cast<int>(crosapi::mojom::DuplexType::kShortEdge)));
}
TEST(LocalPrinterHandlerChromeos, ManagedPrintOptionsToValue_Color) {
crosapi::mojom::ManagedPrintOptions managed_print_options;
managed_print_options.color = crosapi::mojom::BoolOption::New();
managed_print_options.color->default_value = false;
managed_print_options.color->allowed_values = {false, true};
const base::Value::Dict managed_print_options_dict =
LocalPrinterHandlerChromeos::ManagedPrintOptionsToValue(
managed_print_options);
EXPECT_EQ(*managed_print_options_dict.FindDict(kManagedPrintOptions_Color)
->FindBool(kManagedPrintOptions_DefaultValue),
false);
EXPECT_THAT(*managed_print_options_dict.FindDict(kManagedPrintOptions_Color)
->FindList(kManagedPrintOptions_AllowedValues),
ElementsAre(false, true));
}
TEST(LocalPrinterHandlerChromeos, ManagedPrintOptionsToValue_Dpi) {
crosapi::mojom::ManagedPrintOptions managed_print_options;
managed_print_options.dpi = crosapi::mojom::DpiOption::New();
managed_print_options.dpi->default_value = crosapi::mojom::Dpi::New();
managed_print_options.dpi->default_value->vertical = 1000;
managed_print_options.dpi->default_value->horizontal = 1500;
managed_print_options.dpi->allowed_values =
std::vector<crosapi::mojom::DpiPtr>();
const base::Value::Dict managed_print_options_dict =
LocalPrinterHandlerChromeos::ManagedPrintOptionsToValue(
managed_print_options);
EXPECT_EQ(*managed_print_options_dict.FindDict(kManagedPrintOptions_Dpi)
->FindDict(kManagedPrintOptions_DefaultValue),
base::Value::Dict()
.Set(kManagedPrintOptions_DpiHorizontal, 1500)
.Set(kManagedPrintOptions_DpiVertical, 1000));
EXPECT_EQ(*managed_print_options_dict.FindDict(kManagedPrintOptions_Dpi)
->FindList(kManagedPrintOptions_AllowedValues),
base::Value::List());
}
TEST(LocalPrinterHandlerChromeos, ManagedPrintOptionsToValue_Quality) {
crosapi::mojom::ManagedPrintOptions managed_print_options;
managed_print_options.quality = crosapi::mojom::QualityOption::New();
managed_print_options.quality->default_value =
crosapi::mojom::QualityType::kDraft;
managed_print_options.quality->allowed_values = {
crosapi::mojom::QualityType::kDraft, crosapi::mojom::QualityType::kHigh};
const base::Value::Dict managed_print_options_dict =
LocalPrinterHandlerChromeos::ManagedPrintOptionsToValue(
managed_print_options);
EXPECT_EQ(*managed_print_options_dict.FindDict(kManagedPrintOptions_Quality)
->FindInt(kManagedPrintOptions_DefaultValue),
static_cast<int>(crosapi::mojom::QualityType::kDraft));
EXPECT_THAT(
*managed_print_options_dict.FindDict(kManagedPrintOptions_Quality)
->FindList(kManagedPrintOptions_AllowedValues),
ElementsAre(static_cast<int>(crosapi::mojom::QualityType::kDraft),
static_cast<int>(crosapi::mojom::QualityType::kHigh)));
}
TEST(LocalPrinterHandlerChromeos, ManagedPrintOptionsToValue_PrintAsImage) {
crosapi::mojom::ManagedPrintOptions managed_print_options;
managed_print_options.print_as_image = crosapi::mojom::BoolOption::New();
managed_print_options.print_as_image->default_value = std::nullopt;
managed_print_options.print_as_image->allowed_values = {false, true};
const base::Value::Dict managed_print_options_dict =
LocalPrinterHandlerChromeos::ManagedPrintOptionsToValue(
managed_print_options);
EXPECT_EQ(
managed_print_options_dict.FindDict(kManagedPrintOptions_PrintAsImage)
->FindBool(kManagedPrintOptions_DefaultValue),
std::nullopt);
EXPECT_THAT(
*managed_print_options_dict.FindDict(kManagedPrintOptions_PrintAsImage)
->FindList(kManagedPrintOptions_AllowedValues),
ElementsAre(false, true));
}
TEST(LocalPrinterHandlerChromeos, CapabilityToValue) {
auto caps = crosapi::mojom::CapabilitiesResponse::New();
caps->basic_info = crosapi::mojom::LocalDestinationInfo::New(
"device_name", "printer_name", "printer_description", false);
const base::Value kExpectedValue = base::test::ParseJson(R"({
"printer": {
"cupsEnterprisePrinter": false,
"deviceName": "device_name",
"printerDescription": "printer_description",
"printerName": "printer_name",
"printerOptions": {}
}
})");
ASSERT_TRUE(kExpectedValue.is_dict());
EXPECT_EQ(kExpectedValue.GetDict(),
LocalPrinterHandlerChromeos::CapabilityToValue(std::move(caps)));
}
TEST(LocalPrinterHandlerChromeos, CapabilityToValue_ConfiguredViaPolicy) {
auto caps = crosapi::mojom::CapabilitiesResponse::New();
caps->basic_info = crosapi::mojom::LocalDestinationInfo::New(
"device_name", "printer_name", "printer_description", true);
const base::Value kExpectedValue = base::test::ParseJson(R"({
"printer": {
"cupsEnterprisePrinter": true,
"deviceName": "device_name",
"printerDescription": "printer_description",
"printerName": "printer_name",
"printerOptions": {}
}
})");
ASSERT_TRUE(kExpectedValue.is_dict());
EXPECT_EQ(kExpectedValue.GetDict(),
LocalPrinterHandlerChromeos::CapabilityToValue(std::move(caps)));
}
TEST(LocalPrinterHandlerChromeos, CapabilityToValue_EmptyInput) {
EXPECT_TRUE(LocalPrinterHandlerChromeos::CapabilityToValue(nullptr).empty());
}
TEST(LocalPrinterHandlerChromeos, StatusToValue) {
crosapi::mojom::PrinterStatus status;
status.printer_id = "printer_id";
status.timestamp = kStatusTimestamp;
status.status_reasons.push_back(crosapi::mojom::StatusReason::New(
crosapi::mojom::StatusReason::Reason::kOutOfInk,
crosapi::mojom::StatusReason::Severity::kWarning));
const base::Value kExpectedValue = base::test::ParseJson(R"({
"printerId": "printer_id",
"statusReasons": [ {
"reason": 6,
"severity": 2
} ],
"timestamp": 1e+12
})");
EXPECT_EQ(kExpectedValue, LocalPrinterHandlerChromeos::StatusToValue(status));
}
TEST(LocalPrinterHandlerChromeos, RecordDpi) {
base::HistogramTester histogram_tester;
printing::PrinterSemanticCapsAndDefaults printer_caps;
// Represent DPI values in hex to simplify cross checking the values with the
// metric output.
printer_caps.default_dpi = {0x00C8, 0x0190};
printer_caps.dpis = {
{0x0064, 0x0064}, {0x00C8, 0x0190}, {0x00C8, 0x01F4}, {0x03E8, 0x03E8}};
auto caps = crosapi::mojom::CapabilitiesResponse::New();
caps->basic_info = crosapi::mojom::LocalDestinationInfo::New(
"device_name", "printer_name", "printer_description", false);
caps->capabilities = printer_caps;
LocalPrinterHandlerChromeos::CapabilityToValue(std::move(caps));
histogram_tester.ExpectUniqueSample("Printing.CUPS.DPI.Count", /*sample=*/4,
/*expected_bucket_count=*/1);
histogram_tester.ExpectUniqueSample("Printing.CUPS.DPI.Default",
/*sample=*/0x00C80190,
/*expected_bucket_count=*/1);
histogram_tester.ExpectUniqueSample("Printing.CUPS.DPI.Min",
/*sample=*/0x00640064,
/*expected_bucket_count=*/1);
histogram_tester.ExpectUniqueSample("Printing.CUPS.DPI.Max",
/*sample=*/0x03E803E8,
/*expected_bucket_count=*/1);
histogram_tester.ExpectBucketCount("Printing.CUPS.DPI.AllValues",
/*sample=*/0x00640064,
/*expected_count=*/1);
histogram_tester.ExpectBucketCount("Printing.CUPS.DPI.AllValues",
/*sample=*/0x00C80190,
/*expected_count=*/1);
histogram_tester.ExpectBucketCount("Printing.CUPS.DPI.AllValues",
/*sample=*/0x00C801F4,
/*expected_count=*/1);
histogram_tester.ExpectBucketCount("Printing.CUPS.DPI.AllValues",
/*sample=*/0x03E803E8,
/*expected_count=*/1);
}
} // namespace printing
|