summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/ui/webui/print_preview/local_printer_handler_chromeos_unittest.cc
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2019-05-24 11:40:17 +0200
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2019-05-24 12:42:11 +0000
commit5d87695f37678f96492b258bbab36486c59866b4 (patch)
treebe9783bbaf04fb930c4d74ca9c00b5e7954c8bc6 /chromium/chrome/browser/ui/webui/print_preview/local_printer_handler_chromeos_unittest.cc
parent6c11fb357ec39bf087b8b632e2b1e375aef1b38b (diff)
BASELINE: Update Chromium to 75.0.3770.56
Change-Id: I86d2007fd27a45d5797eee06f4c9369b8b50ac4f Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
Diffstat (limited to 'chromium/chrome/browser/ui/webui/print_preview/local_printer_handler_chromeos_unittest.cc')
-rw-r--r--chromium/chrome/browser/ui/webui/print_preview/local_printer_handler_chromeos_unittest.cc323
1 files changed, 323 insertions, 0 deletions
diff --git a/chromium/chrome/browser/ui/webui/print_preview/local_printer_handler_chromeos_unittest.cc b/chromium/chrome/browser/ui/webui/print_preview/local_printer_handler_chromeos_unittest.cc
new file mode 100644
index 00000000000..a079503537a
--- /dev/null
+++ b/chromium/chrome/browser/ui/webui/print_preview/local_printer_handler_chromeos_unittest.cc
@@ -0,0 +1,323 @@
+// Copyright 2019 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 "chrome/browser/ui/webui/print_preview/local_printer_handler_chromeos.cc"
+
+#include <algorithm>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "base/bind.h"
+#include "base/containers/flat_set.h"
+#include "base/json/json_string_value_serializer.h"
+#include "base/memory/ref_counted.h"
+#include "base/values.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/test/base/testing_profile.h"
+#include "components/sync_preferences/testing_pref_service_syncable.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "printing/backend/print_backend.h"
+#include "printing/backend/test_print_backend.h"
+#include "printing/print_job_constants.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace printing {
+
+namespace {
+
+using chromeos::CupsPrintersManager;
+using chromeos::Printer;
+using chromeos::PrinterConfigurer;
+using chromeos::PrinterSetupCallback;
+using chromeos::PrinterSetupResult;
+
+// Used as a callback to StartGetPrinters in tests.
+// Increases |*call_count| and records values returned by StartGetPrinters.
+void RecordPrinterList(size_t* call_count,
+ std::unique_ptr<base::ListValue>* printers_out,
+ const base::ListValue& printers) {
+ ++(*call_count);
+ printers_out->reset(printers.DeepCopy());
+}
+
+// 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(std::unique_ptr<base::Value>* capabilities_out,
+ base::Value capability) {
+ capabilities_out->reset(capability.DeepCopy());
+}
+
+Printer CreateTestPrinter(const std::string& id, const std::string& name) {
+ Printer printer;
+ printer.set_id(id);
+ printer.set_display_name(name);
+ return printer;
+}
+
+Printer CreateEnterprisePrinter(const std::string& id,
+ const std::string& name) {
+ Printer printer = CreateTestPrinter(id, name);
+ printer.set_source(Printer::SRC_POLICY);
+ return printer;
+}
+
+class FakeCupsPrintersManager : public CupsPrintersManager {
+ public:
+ FakeCupsPrintersManager() : printers_(kNumPrinterClasses) {}
+
+ std::vector<Printer> GetPrinters(PrinterClass printer_class) const override {
+ return printers_[printer_class];
+ }
+
+ void RemoveUnavailablePrinters(std::vector<Printer>*) const override {}
+ void UpdateSavedPrinter(const Printer& printer) override {}
+ void RemoveSavedPrinter(const std::string& printer_id) override {}
+ void AddObserver(CupsPrintersManager::Observer* observer) override {}
+ void RemoveObserver(CupsPrintersManager::Observer* observer) override {}
+ void PrinterInstalled(const Printer& printer, bool is_automatic) override {}
+ void RecordSetupAbandoned(const Printer& printer) override {}
+
+ bool IsPrinterInstalled(const Printer& printer) const override {
+ return installed_.contains(printer.id());
+ }
+
+ base::Optional<Printer> GetPrinter(const std::string& id) const override {
+ // Search through each class of printers and find a printer with a
+ // matching id.
+ for (const std::vector<Printer>& v : printers_) {
+ auto iter = std::find_if(
+ v.begin(), v.end(), [&id](const Printer& p) { return p.id() == id; });
+ if (iter != v.end()) {
+ return *iter;
+ }
+ }
+ return base::nullopt;
+ }
+
+ // Add |printer| to the corresponding list in |printers_| bases on the given
+ // |printer_class|.
+ void AddPrinter(const Printer& printer, PrinterClass printer_class) {
+ ASSERT_LT(printer_class, printers_.size());
+ printers_[printer_class].push_back(printer);
+ }
+
+ void InstallPrinter(const std::string& id) { installed_.insert(id); }
+
+ private:
+ std::vector<std::vector<Printer>> printers_;
+ base::flat_set<std::string> installed_;
+};
+
+class FakePrinterConfigurer : public PrinterConfigurer {
+ public:
+ void SetUpPrinter(const Printer& printer,
+ PrinterSetupCallback callback) override {
+ std::move(callback).Run(PrinterSetupResult::kSuccess);
+ }
+};
+
+// Converts JSON string to base::ListValue object.
+// On failure, returns NULL and fills |*error| string.
+std::unique_ptr<base::ListValue> GetJSONAsListValue(const std::string& json,
+ std::string* error) {
+ auto ret = base::ListValue::From(
+ JSONStringValueDeserializer(json).Deserialize(nullptr, error));
+ if (!ret)
+ *error = "Value is not a list.";
+ return ret;
+}
+
+} // namespace
+
+class LocalPrinterHandlerChromeosTest : public testing::Test {
+ public:
+ LocalPrinterHandlerChromeosTest() = default;
+ ~LocalPrinterHandlerChromeosTest() override = default;
+
+ void SetUp() override {
+ test_backend_ = base::MakeRefCounted<TestPrintBackend>();
+ PrintBackend::SetPrintBackendForTesting(test_backend_.get());
+ local_printer_handler_ = LocalPrinterHandlerChromeos::CreateForTesting(
+ &profile_, nullptr, &printers_manager_,
+ std::make_unique<FakePrinterConfigurer>());
+ }
+
+ protected:
+ // Must outlive |profile_|.
+ content::TestBrowserThreadBundle thread_bundle_;
+ // Must outlive |printers_manager_|.
+ TestingProfile profile_;
+ scoped_refptr<TestPrintBackend> test_backend_;
+ FakeCupsPrintersManager printers_manager_;
+ std::unique_ptr<LocalPrinterHandlerChromeos> local_printer_handler_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(LocalPrinterHandlerChromeosTest);
+};
+
+TEST_F(LocalPrinterHandlerChromeosTest, GetPrinters) {
+ size_t call_count = 0;
+ std::unique_ptr<base::ListValue> printers;
+ bool is_done = false;
+
+ Printer saved_printer = CreateTestPrinter("printer1", "saved");
+ Printer enterprise_printer =
+ CreateEnterprisePrinter("printer2", "enterprise");
+ Printer automatic_printer = CreateTestPrinter("printer3", "automatic");
+
+ printers_manager_.AddPrinter(saved_printer, CupsPrintersManager::kSaved);
+ printers_manager_.AddPrinter(enterprise_printer,
+ CupsPrintersManager::kEnterprise);
+ printers_manager_.AddPrinter(automatic_printer,
+ CupsPrintersManager::kAutomatic);
+
+ local_printer_handler_->StartGetPrinters(
+ base::BindRepeating(&RecordPrinterList, &call_count, &printers),
+ base::BindOnce(&RecordPrintersDone, &is_done));
+
+ EXPECT_EQ(call_count, 1u);
+ EXPECT_TRUE(is_done);
+ ASSERT_TRUE(printers);
+
+ const std::string expected_list = R"(
+ [
+ {
+ "cupsEnterprisePrinter": false,
+ "deviceName": "printer1",
+ "printerDescription": "",
+ "printerName": "saved",
+ "printerOptions": {
+ "cupsEnterprisePrinter": "false",
+ "system_driverinfo": ""
+ }
+ },
+ {
+ "cupsEnterprisePrinter": true,
+ "deviceName": "printer2",
+ "printerDescription": "",
+ "printerName": "enterprise",
+ "printerOptions": {
+ "cupsEnterprisePrinter": "true",
+ "system_driverinfo": ""
+ }
+ },
+ {
+ "cupsEnterprisePrinter": false,
+ "deviceName": "printer3",
+ "printerDescription": "",
+ "printerName": "automatic",
+ "printerOptions": {
+ "cupsEnterprisePrinter": "false",
+ "system_driverinfo": ""
+ }
+ }
+ ]
+ )";
+ std::string error;
+ std::unique_ptr<base::ListValue> expected_printers(
+ GetJSONAsListValue(expected_list, &error));
+ ASSERT_TRUE(expected_printers) << "Error deserializing printers: " << error;
+ EXPECT_EQ(*printers, *expected_printers);
+}
+
+// Tests that fetching capabilities for an existing installed printer is
+// successful.
+TEST_F(LocalPrinterHandlerChromeosTest, StartGetCapabilityValidPrinter) {
+ Printer saved_printer = CreateTestPrinter("printer1", "saved");
+ printers_manager_.AddPrinter(saved_printer, CupsPrintersManager::kSaved);
+ printers_manager_.InstallPrinter("printer1");
+
+ // Add printer capabilities to |test_backend_|.
+ PrinterSemanticCapsAndDefaults caps;
+ test_backend_->AddValidPrinter(
+ "printer1", std::make_unique<PrinterSemanticCapsAndDefaults>(caps));
+
+ std::unique_ptr<base::Value> fetched_caps;
+ local_printer_handler_->StartGetCapability(
+ "printer1", base::BindOnce(&RecordGetCapability, &fetched_caps));
+
+ thread_bundle_.RunUntilIdle();
+
+ ASSERT_TRUE(fetched_caps);
+ base::DictionaryValue* dict;
+ ASSERT_TRUE(fetched_caps->GetAsDictionary(&dict));
+ ASSERT_TRUE(dict->HasKey(kSettingCapabilities));
+ ASSERT_TRUE(dict->HasKey(kPrinter));
+}
+
+// Test that printers which have not yet been installed are installed with
+// SetUpPrinter before their capabilities are fetched.
+TEST_F(LocalPrinterHandlerChromeosTest, StartGetCapabilityPrinterNotInstalled) {
+ Printer discovered_printer = CreateTestPrinter("printer1", "discovered");
+ // NOTE: The printer |discovered_printer| is not installed using
+ // InstallPrinter.
+ printers_manager_.AddPrinter(discovered_printer,
+ CupsPrintersManager::kDiscovered);
+
+ // Add printer capabilities to |test_backend_|.
+ PrinterSemanticCapsAndDefaults caps;
+ test_backend_->AddValidPrinter(
+ "printer1", std::make_unique<PrinterSemanticCapsAndDefaults>(caps));
+
+ std::unique_ptr<base::Value> fetched_caps;
+ local_printer_handler_->StartGetCapability(
+ "printer1", base::BindOnce(&RecordGetCapability, &fetched_caps));
+
+ thread_bundle_.RunUntilIdle();
+
+ ASSERT_TRUE(fetched_caps);
+ base::DictionaryValue* dict;
+ ASSERT_TRUE(fetched_caps->GetAsDictionary(&dict));
+ ASSERT_TRUE(dict->HasKey(kSettingCapabilities));
+ ASSERT_TRUE(dict->HasKey(kPrinter));
+}
+
+// In this test we expect the StartGetCapability to bail early because the
+// provided printer can't be found in the CupsPrintersManager.
+TEST_F(LocalPrinterHandlerChromeosTest, StartGetCapabilityInvalidPrinter) {
+ std::unique_ptr<base::Value> fetched_caps;
+ local_printer_handler_->StartGetCapability(
+ "invalid printer", base::BindOnce(&RecordGetCapability, &fetched_caps));
+
+ thread_bundle_.RunUntilIdle();
+
+ ASSERT_TRUE(fetched_caps);
+ EXPECT_TRUE(fetched_caps->is_none());
+}
+
+TEST_F(LocalPrinterHandlerChromeosTest, GetNativePrinterPolicies) {
+ sync_preferences::TestingPrefServiceSyncable* prefs =
+ profile_.GetTestingPrefService();
+
+ prefs->SetUserPref(prefs::kPrintingAllowedColorModes,
+ std::make_unique<base::Value>(1));
+ prefs->SetUserPref(prefs::kPrintingAllowedDuplexModes,
+ std::make_unique<base::Value>(0));
+ prefs->SetUserPref(prefs::kPrintingAllowedPinModes,
+ std::make_unique<base::Value>(1));
+ prefs->SetUserPref(prefs::kPrintingColorDefault,
+ std::make_unique<base::Value>(2));
+ prefs->SetUserPref(prefs::kPrintingDuplexDefault,
+ std::make_unique<base::Value>(4));
+ prefs->SetUserPref(prefs::kPrintingPinDefault,
+ std::make_unique<base::Value>(0));
+
+ base::Value expected_policies(base::Value::Type::DICTIONARY);
+ expected_policies.SetKey(kAllowedColorModes, base::Value(1));
+ expected_policies.SetKey(kAllowedDuplexModes, base::Value(0));
+ expected_policies.SetKey(kAllowedPinModes, base::Value(1));
+ expected_policies.SetKey(kDefaultColorMode, base::Value(2));
+ expected_policies.SetKey(kDefaultDuplexMode, base::Value(4));
+ expected_policies.SetKey(kDefaultPinMode, base::Value(0));
+
+ EXPECT_EQ(expected_policies,
+ local_printer_handler_->GetNativePrinterPolicies());
+}
+
+} // namespace printing