Commit affccb73 authored by Daniel Hosseinian's avatar Daniel Hosseinian Committed by Commit Bot

Refactor PrintPreviewHandlerTest with helper functions

The tests that validate the 'getPrinters' and 'getPrinterCapabilities'
Web UI calls contain lines of code that can be used again once more
tests are added.

Remove some deprecated base::ListValue from tests.

Bug: 487875
Change-Id: Ic49fc6b62ba44b3a548f026d68162446a852998e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1893974
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#711950}
parent 54b0ec8a
...@@ -46,6 +46,7 @@ namespace printing { ...@@ -46,6 +46,7 @@ namespace printing {
namespace { namespace {
const char kDummyInitiatorName[] = "TestInitiator"; const char kDummyInitiatorName[] = "TestInitiator";
const char kEmptyPrinterName[] = "EmptyPrinter";
const char kTestData[] = "abc"; const char kTestData[] = "abc";
// Array of all PrinterTypes. // Array of all PrinterTypes.
...@@ -56,6 +57,10 @@ constexpr PrinterType kAllTypes[] = {kPrivetPrinter, kExtensionPrinter, ...@@ -56,6 +57,10 @@ constexpr PrinterType kAllTypes[] = {kPrivetPrinter, kExtensionPrinter,
constexpr PrinterType kAllSupportedTypes[] = {kPrivetPrinter, kExtensionPrinter, constexpr PrinterType kAllSupportedTypes[] = {kPrivetPrinter, kExtensionPrinter,
kPdfPrinter, kLocalPrinter}; kPdfPrinter, kLocalPrinter};
// All three printer types that implement PrinterHandler::StartGetPrinters().
constexpr PrinterType kFetchableTypes[] = {kPrivetPrinter, kExtensionPrinter,
kLocalPrinter};
struct PrinterInfo { struct PrinterInfo {
std::string id; std::string id;
bool is_default; bool is_default;
...@@ -82,7 +87,7 @@ PrinterInfo GetSimplePrinterInfo(const std::string& name, bool is_default) { ...@@ -82,7 +87,7 @@ PrinterInfo GetSimplePrinterInfo(const std::string& name, bool is_default) {
PrinterInfo GetEmptyPrinterInfo() { PrinterInfo GetEmptyPrinterInfo() {
PrinterInfo empty_printer; PrinterInfo empty_printer;
empty_printer.id = "EmptyPrinter"; empty_printer.id = kEmptyPrinterName;
empty_printer.is_default = false; empty_printer.is_default = false;
empty_printer.basic_info.SetKey("printer_name", empty_printer.basic_info.SetKey("printer_name",
base::Value(empty_printer.id)); base::Value(empty_printer.id));
...@@ -417,6 +422,58 @@ class PrintPreviewHandlerTest : public testing::Test { ...@@ -417,6 +422,58 @@ class PrintPreviewHandlerTest : public testing::Test {
settings->FindKeyOfType("syncAvailable", base::Value::Type::BOOLEAN)); settings->FindKeyOfType("syncAvailable", base::Value::Type::BOOLEAN));
} }
// Simulates a 'getPrinters' Web UI message by constructing the arguments and
// making the call to the handler.
void SendGetPrinters(PrinterType type, const std::string& callback_id_in) {
base::Value args(base::Value::Type::LIST);
args.Append(callback_id_in);
args.Append(type);
handler()->HandleGetPrinters(&base::Value::AsListValue(args));
}
// Validates that the printers-added Web UI event has been fired for
// |expected-type| with 1 printer. This should be the second most recent call,
// as the resolution of the getPrinters() promise will be the most recent.
void ValidatePrinterTypeAdded(PrinterType expected_type) {
const size_t call_data_size = web_ui()->call_data().size();
ASSERT_GE(call_data_size, 2u);
const content::TestWebUI::CallData& add_data =
*web_ui()->call_data()[call_data_size - 2];
AssertWebUIEventFired(add_data, "printers-added");
const int type = add_data.arg2()->GetInt();
EXPECT_EQ(expected_type, type);
ASSERT_TRUE(add_data.arg3());
base::span<const base::Value> printer_list = add_data.arg3()->GetList();
ASSERT_EQ(printer_list.size(), 1u);
EXPECT_TRUE(printer_list[0].FindKeyOfType("printer_name",
base::Value::Type::STRING));
}
// Simulates a 'getPrinterCapabilities' Web UI message by constructing the
// arguments and making the call to the handler.
void SendGetPrinterCapabilities(PrinterType type,
const std::string& callback_id_in,
const std::string& printer_name) {
base::Value args(base::Value::Type::LIST);
args.Append(callback_id_in);
args.Append(printer_name);
args.Append(type);
handler()->HandleGetPrinterCapabilities(&base::Value::AsListValue(args));
}
// Validates that a printer capabilities promise was resolved/rejected.
void ValidatePrinterCapabilities(const std::string& callback_id_in,
bool expect_resolved) {
const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
CheckWebUIResponse(data, callback_id_in, expect_resolved);
if (expect_resolved) {
const base::Value* settings = data.arg3();
ASSERT_TRUE(settings);
EXPECT_TRUE(settings->FindKeyOfType(kSettingCapabilities,
base::Value::Type::DICTIONARY));
}
}
IPC::TestSink& initiator_sink() { IPC::TestSink& initiator_sink() {
content::RenderFrameHost* rfh = initiator_web_contents_->GetMainFrame(); content::RenderFrameHost* rfh = initiator_web_contents_->GetMainFrame();
auto* rph = static_cast<content::MockRenderProcessHost*>(rfh->GetProcess()); auto* rph = static_cast<content::MockRenderProcessHost*>(rfh->GetProcess());
...@@ -508,38 +565,20 @@ TEST_F(PrintPreviewHandlerTest, GetPrinters) { ...@@ -508,38 +565,20 @@ TEST_F(PrintPreviewHandlerTest, GetPrinters) {
Initialize(); Initialize();
// Check all three printer types that implement // Check all three printer types that implement
// PrinterHandler::StartGetPrinters(). for (size_t i = 0; i < base::size(kFetchableTypes); i++) {
const PrinterType types[] = {kPrivetPrinter, kExtensionPrinter, PrinterType type = kFetchableTypes[i];
kLocalPrinter};
for (size_t i = 0; i < base::size(types); i++) {
PrinterType type = types[i];
handler()->reset_calls();
base::Value args(base::Value::Type::LIST);
std::string callback_id_in = std::string callback_id_in =
"test-callback-id-" + base::NumberToString(i + 1); "test-callback-id-" + base::NumberToString(i + 1);
args.Append(callback_id_in); handler()->reset_calls();
args.Append(type); SendGetPrinters(type, callback_id_in);
std::unique_ptr<base::ListValue> list_args =
base::ListValue::From(base::Value::ToUniquePtrValue(std::move(args)));
handler()->HandleGetPrinters(list_args.get());
EXPECT_TRUE(handler()->CalledOnlyForType(type)); EXPECT_TRUE(handler()->CalledOnlyForType(type));
// Start with 1 call from initial settings, then add 2 more for each loop // Start with 1 call from initial settings, then add 2 more for each loop
// iteration (one for printers-added, and one for the response). // iteration (one for printers-added, and one for the response).
ASSERT_EQ(1u + 2 * (i + 1), web_ui()->call_data().size()); ASSERT_EQ(1u + 2 * (i + 1), web_ui()->call_data().size());
// Validate printers-added ValidatePrinterTypeAdded(type);
const content::TestWebUI::CallData& add_data =
*web_ui()->call_data()[web_ui()->call_data().size() - 2];
AssertWebUIEventFired(add_data, "printers-added");
int type_out;
ASSERT_TRUE(add_data.arg2()->GetAsInteger(&type_out));
EXPECT_EQ(type, type_out);
ASSERT_TRUE(add_data.arg3());
base::span<const base::Value> printer_list = add_data.arg3()->GetList();
ASSERT_EQ(printer_list.size(), 1u);
EXPECT_TRUE(printer_list[0].FindKeyOfType("printer_name",
base::Value::Type::STRING));
// Verify getPrinters promise was resolved successfully. // Verify getPrinters promise was resolved successfully.
const content::TestWebUI::CallData& data = *web_ui()->call_data().back(); const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
...@@ -559,46 +598,28 @@ TEST_F(PrintPreviewHandlerTest, GetPrinterCapabilities) { ...@@ -559,46 +598,28 @@ TEST_F(PrintPreviewHandlerTest, GetPrinterCapabilities) {
// PrinterHandler::StartGetCapability(). // PrinterHandler::StartGetCapability().
for (size_t i = 0; i < base::size(kAllSupportedTypes); i++) { for (size_t i = 0; i < base::size(kAllSupportedTypes); i++) {
PrinterType type = kAllSupportedTypes[i]; PrinterType type = kAllSupportedTypes[i];
handler()->reset_calls();
base::Value args(base::Value::Type::LIST);
std::string callback_id_in = std::string callback_id_in =
"test-callback-id-" + base::NumberToString(i + 1); "test-callback-id-" + base::NumberToString(i + 1);
args.Append(callback_id_in); handler()->reset_calls();
args.Append(kDummyPrinterName); SendGetPrinterCapabilities(type, callback_id_in, kDummyPrinterName);
args.Append(type);
std::unique_ptr<base::ListValue> list_args =
base::ListValue::From(base::Value::ToUniquePtrValue(std::move(args)));
handler()->HandleGetPrinterCapabilities(list_args.get());
EXPECT_TRUE(handler()->CalledOnlyForType(type)); EXPECT_TRUE(handler()->CalledOnlyForType(type));
// Start with 1 call from initial settings, then add 1 more for each loop // Start with 1 call from initial settings, then add 1 more for each loop
// iteration. // iteration.
ASSERT_EQ(1u + (i + 1), web_ui()->call_data().size()); ASSERT_EQ(1u + (i + 1), web_ui()->call_data().size());
// Verify that the printer capabilities promise was resolved correctly. ValidatePrinterCapabilities(callback_id_in, /*expect_resolved=*/true);
const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
CheckWebUIResponse(data, callback_id_in, true);
const base::Value* settings = data.arg3();
ASSERT_TRUE(settings);
EXPECT_TRUE(settings->FindKeyOfType(kSettingCapabilities,
base::Value::Type::DICTIONARY));
} }
// Run through the loop again, this time with a printer that has no // Run through the loop again, this time with a printer that has no
// capabilities. // capabilities.
for (size_t i = 0; i < base::size(kAllSupportedTypes); i++) { for (size_t i = 0; i < base::size(kAllSupportedTypes); i++) {
PrinterType type = kAllSupportedTypes[i]; PrinterType type = kAllSupportedTypes[i];
handler()->reset_calls();
base::Value args(base::Value::Type::LIST);
std::string callback_id_in = std::string callback_id_in =
"test-callback-id-" + "test-callback-id-" +
base::NumberToString(i + base::size(kAllSupportedTypes) + 1); base::NumberToString(i + base::size(kAllSupportedTypes) + 1);
args.Append(callback_id_in); handler()->reset_calls();
args.Append("EmptyPrinter"); SendGetPrinterCapabilities(type, callback_id_in, kEmptyPrinterName);
args.Append(type);
std::unique_ptr<base::ListValue> list_args =
base::ListValue::From(base::Value::ToUniquePtrValue(std::move(args)));
handler()->HandleGetPrinterCapabilities(list_args.get());
EXPECT_TRUE(handler()->CalledOnlyForType(type)); EXPECT_TRUE(handler()->CalledOnlyForType(type));
// Start with 1 call from initial settings plus // Start with 1 call from initial settings plus
...@@ -607,9 +628,7 @@ TEST_F(PrintPreviewHandlerTest, GetPrinterCapabilities) { ...@@ -607,9 +628,7 @@ TEST_F(PrintPreviewHandlerTest, GetPrinterCapabilities) {
ASSERT_EQ(1u + base::size(kAllSupportedTypes) + (i + 1), ASSERT_EQ(1u + base::size(kAllSupportedTypes) + (i + 1),
web_ui()->call_data().size()); web_ui()->call_data().size());
// Verify printer capabilities promise was rejected. ValidatePrinterCapabilities(callback_id_in, /*expect_resolved=*/false);
const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
CheckWebUIResponse(data, callback_id_in, false);
} }
} }
......
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