| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <utility> | 5 #include <utility> |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/test/values_test_util.h" | 8 #include "base/test/values_test_util.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chromeos/printing/printer_configuration.h" | 10 #include "chromeos/printing/printer_configuration.h" |
| 11 #include "chromeos/printing/printer_translator.h" | 11 #include "chromeos/printing/printer_translator.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace chromeos { | 14 namespace chromeos { |
| 15 namespace printing { | 15 namespace printing { |
| 16 | 16 |
| 17 // Printer test data | 17 // Printer test data |
| 18 const char kGUID[] = "GUID-GUID-GUID"; | 18 const char kHash[] = "ABCDEF123456"; |
| 19 const char kName[] = "Chrome Super Printer"; | 19 const char kName[] = "Chrome Super Printer"; |
| 20 const char kDescription[] = "first star on the left"; | 20 const char kDescription[] = "first star on the left"; |
| 21 const char kMake[] = "Chrome"; | 21 const char kMake[] = "Chrome"; |
| 22 const char kModel[] = "Inktastic Laser Magic"; | 22 const char kModel[] = "Inktastic Laser Magic"; |
| 23 const char kUri[] = "ipp://printy.domain.co:555/ipp/print"; | 23 const char kUri[] = "ipp://printy.domain.co:555/ipp/print"; |
| 24 const char kUUID[] = "UUID-UUID-UUID"; | 24 const char kUUID[] = "UUID-UUID-UUID"; |
| 25 | 25 |
| 26 // PpdReference test data | 26 // PpdReference test data |
| 27 const char kUserSuppliedPpdUrl[] = "/some/path/to/user.url"; | 27 const char kEffectiveMakeAndModel[] = "PrintBlaster LazerInker 2000"; |
| 28 const char kEffectiveMakeAndModel[] = "PrintBlaster 2000"; | |
| 29 | 28 |
| 30 TEST(PrinterTranslatorTest, PrefToPrinterMissingId) { | 29 TEST(PrinterTranslatorTest, RecommendedPrinterToPrinterMissingId) { |
| 31 base::DictionaryValue value; | 30 base::DictionaryValue value; |
| 32 std::unique_ptr<Printer> printer = PrefToPrinter(value); | 31 std::unique_ptr<Printer> printer = RecommendedPrinterToPrinter(value); |
| 33 | 32 |
| 34 EXPECT_FALSE(printer); | 33 EXPECT_FALSE(printer); |
| 35 } | 34 } |
| 36 | 35 |
| 37 TEST(PrinterTranslatorTest, PrefToPrinter) { | 36 TEST(PrinterTranslatorTest, MissingDisplayNameFails) { |
| 38 base::DictionaryValue preference; | 37 base::DictionaryValue preference; |
| 39 preference.SetString("id", kGUID); | 38 preference.SetString("id", kHash); |
| 39 // display name omitted |
| 40 preference.SetString("uri", kUri); |
| 41 preference.SetString("ppd_resource.effective_model", kEffectiveMakeAndModel); |
| 42 |
| 43 std::unique_ptr<Printer> printer = RecommendedPrinterToPrinter(preference); |
| 44 EXPECT_FALSE(printer); |
| 45 } |
| 46 |
| 47 TEST(PrinterTranslatorTest, MissingUriFails) { |
| 48 base::DictionaryValue preference; |
| 49 preference.SetString("id", kHash); |
| 50 preference.SetString("display_name", kName); |
| 51 // uri omitted |
| 52 preference.SetString("ppd_resource.effective_model", kEffectiveMakeAndModel); |
| 53 |
| 54 std::unique_ptr<Printer> printer = RecommendedPrinterToPrinter(preference); |
| 55 EXPECT_FALSE(printer); |
| 56 } |
| 57 |
| 58 TEST(PrinterTranslatorTest, MissingPpdResourceFails) { |
| 59 base::DictionaryValue preference; |
| 60 preference.SetString("id", kHash); |
| 61 preference.SetString("display_name", kName); |
| 62 preference.SetString("uri", kUri); |
| 63 // ppd resource omitted |
| 64 |
| 65 std::unique_ptr<Printer> printer = RecommendedPrinterToPrinter(preference); |
| 66 EXPECT_FALSE(printer); |
| 67 } |
| 68 |
| 69 TEST(PrinterTranslatorTest, MissingEffectiveMakeModelFails) { |
| 70 base::DictionaryValue preference; |
| 71 preference.SetString("id", kHash); |
| 72 preference.SetString("display_name", kName); |
| 73 preference.SetString("uri", kUri); |
| 74 preference.SetString("ppd_resource.foobarwrongfield", "gibberish"); |
| 75 |
| 76 std::unique_ptr<Printer> printer = RecommendedPrinterToPrinter(preference); |
| 77 EXPECT_FALSE(printer); |
| 78 } |
| 79 |
| 80 TEST(PrinterTranslatorTest, RecommendedPrinterMinimalSetup) { |
| 81 base::DictionaryValue preference; |
| 82 preference.SetString("id", kHash); |
| 83 preference.SetString("display_name", kName); |
| 84 preference.SetString("uri", kUri); |
| 85 preference.SetString("ppd_resource.effective_model", kEffectiveMakeAndModel); |
| 86 |
| 87 std::unique_ptr<Printer> printer = RecommendedPrinterToPrinter(preference); |
| 88 EXPECT_TRUE(printer); |
| 89 } |
| 90 |
| 91 TEST(PrinterTranslatorTest, RecommendedPrinterToPrinter) { |
| 92 base::DictionaryValue preference; |
| 93 preference.SetString("id", kHash); |
| 40 preference.SetString("display_name", kName); | 94 preference.SetString("display_name", kName); |
| 41 preference.SetString("description", kDescription); | 95 preference.SetString("description", kDescription); |
| 42 preference.SetString("manufacturer", kMake); | 96 preference.SetString("manufacturer", kMake); |
| 43 preference.SetString("model", kModel); | |
| 44 preference.SetString("uri", kUri); | |
| 45 preference.SetString("uuid", kUUID); | |
| 46 | |
| 47 std::unique_ptr<Printer> printer = PrefToPrinter(preference); | |
| 48 EXPECT_TRUE(printer); | |
| 49 | |
| 50 EXPECT_EQ(kGUID, printer->id()); | |
| 51 EXPECT_EQ(kName, printer->display_name()); | |
| 52 EXPECT_EQ(kDescription, printer->description()); | |
| 53 EXPECT_EQ(kMake, printer->manufacturer()); | |
| 54 EXPECT_EQ(kModel, printer->model()); | |
| 55 EXPECT_EQ(kUri, printer->uri()); | |
| 56 EXPECT_EQ(kUUID, printer->uuid()); | |
| 57 } | |
| 58 | |
| 59 TEST(PrinterTranslatorTest, PrinterToPref) { | |
| 60 Printer printer("GLOBALLY_UNIQUE_ID"); | |
| 61 printer.set_display_name(kName); | |
| 62 printer.set_description(kDescription); | |
| 63 printer.set_manufacturer(kMake); | |
| 64 printer.set_model(kModel); | |
| 65 printer.set_uri(kUri); | |
| 66 printer.set_uuid(kUUID); | |
| 67 | |
| 68 std::unique_ptr<base::DictionaryValue> pref = PrinterToPref(printer); | |
| 69 | |
| 70 base::ExpectDictStringValue("GLOBALLY_UNIQUE_ID", *pref, "id"); | |
| 71 base::ExpectDictStringValue(kName, *pref, "display_name"); | |
| 72 base::ExpectDictStringValue(kDescription, *pref, "description"); | |
| 73 base::ExpectDictStringValue(kMake, *pref, "manufacturer"); | |
| 74 base::ExpectDictStringValue(kModel, *pref, "model"); | |
| 75 base::ExpectDictStringValue(kUri, *pref, "uri"); | |
| 76 base::ExpectDictStringValue(kUUID, *pref, "uuid"); | |
| 77 } | |
| 78 | |
| 79 TEST(PrinterTranslatorTest, PrinterToPrefPpdReference) { | |
| 80 Printer printer("UNIQUE_ID"); | |
| 81 auto* ppd = printer.mutable_ppd_reference(); | |
| 82 ppd->user_supplied_ppd_url = kUserSuppliedPpdUrl; | |
| 83 ppd->effective_make_and_model = kEffectiveMakeAndModel; | |
| 84 | |
| 85 std::unique_ptr<base::DictionaryValue> actual = PrinterToPref(printer); | |
| 86 | |
| 87 base::ExpectDictStringValue(kUserSuppliedPpdUrl, *actual, | |
| 88 "ppd_reference.user_supplied_ppd_url"); | |
| 89 base::ExpectDictStringValue(kEffectiveMakeAndModel, *actual, | |
| 90 "ppd_reference.effective_model"); | |
| 91 } | |
| 92 | |
| 93 // Make sure we don't serialize empty fields. | |
| 94 TEST(PrinterTranslatorTest, PrinterToPrefPpdReferenceLazy) { | |
| 95 Printer printer("UNIQUE_ID"); | |
| 96 std::unique_ptr<base::DictionaryValue> actual = PrinterToPref(printer); | |
| 97 | |
| 98 EXPECT_FALSE(actual->HasKey("ppd_reference.user_supplied_ppd_url")); | |
| 99 EXPECT_FALSE(actual->HasKey("ppd_reference.ppd_server_key")); | |
| 100 } | |
| 101 | |
| 102 TEST(PrinterTranslatorTest, PrefToPrinterRoundTrip) { | |
| 103 base::DictionaryValue preference; | |
| 104 preference.SetString("id", kGUID); | |
| 105 preference.SetString("display_name", kName); | |
| 106 preference.SetString("description", kDescription); | |
| 107 preference.SetString("manufacturer", kMake); | |
| 108 preference.SetString("model", kModel); | 97 preference.SetString("model", kModel); |
| 109 preference.SetString("uri", kUri); | 98 preference.SetString("uri", kUri); |
| 110 preference.SetString("uuid", kUUID); | 99 preference.SetString("uuid", kUUID); |
| 111 | 100 |
| 112 preference.SetString("ppd_reference.user_supplied_ppd_url", | 101 preference.SetString("ppd_resource.effective_model", kEffectiveMakeAndModel); |
| 113 kUserSuppliedPpdUrl); | |
| 114 preference.SetString("ppd_reference.effective_model", kEffectiveMakeAndModel); | |
| 115 | 102 |
| 116 std::unique_ptr<Printer> printer = PrefToPrinter(preference); | 103 std::unique_ptr<Printer> printer = RecommendedPrinterToPrinter(preference); |
| 117 std::unique_ptr<base::DictionaryValue> pref_copy = PrinterToPref(*printer); | 104 EXPECT_TRUE(printer); |
| 118 | 105 |
| 119 EXPECT_TRUE(preference.Equals(pref_copy.get())); | 106 EXPECT_EQ(kHash, printer->id()); |
| 107 EXPECT_EQ(kName, printer->display_name()); |
| 108 EXPECT_EQ(kDescription, printer->description()); |
| 109 EXPECT_EQ(kMake, printer->manufacturer()); |
| 110 EXPECT_EQ(kModel, printer->model()); |
| 111 EXPECT_EQ(kUri, printer->uri()); |
| 112 EXPECT_EQ(kUUID, printer->uuid()); |
| 113 |
| 114 EXPECT_EQ(kEffectiveMakeAndModel, |
| 115 printer->ppd_reference().effective_make_and_model); |
| 120 } | 116 } |
| 121 | 117 |
| 122 } // namespace printing | 118 } // namespace printing |
| 123 } // namespace chromeos | 119 } // namespace chromeos |
| OLD | NEW |