| 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 <memory> | 5 #include <memory> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/run_loop.h" | |
| 11 #include "base/task_runner_util.h" | |
| 12 #include "base/test/values_test_util.h" | 10 #include "base/test/values_test_util.h" |
| 13 #include "base/threading/sequenced_worker_pool.h" | |
| 14 #include "chrome/browser/ui/webui/print_preview/printer_capabilities.h" | 11 #include "chrome/browser/ui/webui/print_preview/printer_capabilities.h" |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "printing/backend/test_print_backend.h" | 13 #include "printing/backend/test_print_backend.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "ui/gfx/geometry/size.h" | 15 #include "ui/gfx/geometry/size.h" |
| 20 | 16 |
| 21 namespace { | 17 namespace printing { |
| 22 | |
| 23 void SettingsReply(std::unique_ptr<base::DictionaryValue>* out, | |
| 24 std::unique_ptr<base::DictionaryValue> reply) { | |
| 25 *out = std::move(reply); | |
| 26 } | |
| 27 | |
| 28 std::unique_ptr<base::DictionaryValue> GetSettingsSynchronous( | |
| 29 const std::string& printer_name, | |
| 30 const printing::PrinterBasicInfo& basic_info) { | |
| 31 std::unique_ptr<base::DictionaryValue> settings_dictionary; | |
| 32 | |
| 33 base::SequencedWorkerPool* worker_pool = | |
| 34 content::BrowserThread::GetBlockingPool(); | |
| 35 | |
| 36 base::PostTaskAndReplyWithResult( | |
| 37 worker_pool, FROM_HERE, base::Bind(&printing::GetSettingsOnBlockingPool, | |
| 38 printer_name, basic_info), | |
| 39 base::Bind(&SettingsReply, base::Unretained(&settings_dictionary))); | |
| 40 | |
| 41 worker_pool->FlushForTesting(); | |
| 42 base::RunLoop().RunUntilIdle(); | |
| 43 | |
| 44 return settings_dictionary; | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | 18 |
| 49 class PrinterCapabilitiesTest : public testing::Test { | 19 class PrinterCapabilitiesTest : public testing::Test { |
| 50 public: | 20 public: |
| 51 PrinterCapabilitiesTest() : test_browser_threads_() {} | 21 PrinterCapabilitiesTest() {} |
| 22 ~PrinterCapabilitiesTest() override {} |
| 52 | 23 |
| 53 protected: | 24 protected: |
| 54 void SetUp() override { | 25 void SetUp() override { |
| 55 test_backend_ = new printing::TestPrintBackend(); | 26 test_backend_ = new TestPrintBackend(); |
| 56 printing::PrintBackend::SetPrintBackendForTesting(test_backend_.get()); | 27 PrintBackend::SetPrintBackendForTesting(test_backend_.get()); |
| 57 } | 28 } |
| 58 | 29 |
| 59 void TearDown() override { test_backend_ = nullptr; } | 30 void TearDown() override { test_backend_ = nullptr; } |
| 60 | 31 |
| 61 printing::TestPrintBackend* print_backend() { return test_backend_.get(); } | 32 TestPrintBackend* print_backend() { return test_backend_.get(); } |
| 62 | 33 |
| 63 private: | 34 private: |
| 64 content::TestBrowserThreadBundle test_browser_threads_; | 35 content::TestBrowserThreadBundle test_browser_threads_; |
| 65 scoped_refptr<printing::TestPrintBackend> test_backend_; | 36 scoped_refptr<TestPrintBackend> test_backend_; |
| 66 }; | 37 }; |
| 67 | 38 |
| 68 // Verify that we don't crash for a missing printer and a nullptr is never | 39 // Verify that we don't crash for a missing printer and a nullptr is never |
| 69 // returned. | 40 // returned. |
| 70 TEST_F(PrinterCapabilitiesTest, NonNullForMissingPrinter) { | 41 TEST_F(PrinterCapabilitiesTest, NonNullForMissingPrinter) { |
| 71 printing::PrinterBasicInfo basic_info; | 42 PrinterBasicInfo basic_info; |
| 72 std::string printer_name = "missing_printer"; | 43 std::string printer_name = "missing_printer"; |
| 73 | 44 |
| 74 std::unique_ptr<base::DictionaryValue> settings_dictionary = | 45 std::unique_ptr<base::DictionaryValue> settings_dictionary = |
| 75 GetSettingsSynchronous(printer_name, basic_info); | 46 GetSettingsOnBlockingPool(printer_name, basic_info); |
| 76 | 47 |
| 77 ASSERT_TRUE(settings_dictionary); | 48 ASSERT_TRUE(settings_dictionary); |
| 78 } | 49 } |
| 79 | 50 |
| 80 TEST_F(PrinterCapabilitiesTest, ProvidedCapabilitiesUsed) { | 51 TEST_F(PrinterCapabilitiesTest, ProvidedCapabilitiesUsed) { |
| 81 std::string printer_name = "test_printer"; | 52 std::string printer_name = "test_printer"; |
| 82 printing::PrinterBasicInfo basic_info; | 53 PrinterBasicInfo basic_info; |
| 83 std::unique_ptr<printing::PrinterSemanticCapsAndDefaults> caps = | 54 auto caps = base::MakeUnique<PrinterSemanticCapsAndDefaults>(); |
| 84 base::MakeUnique<printing::PrinterSemanticCapsAndDefaults>(); | |
| 85 | 55 |
| 86 // set a capability | 56 // set a capability |
| 87 caps->dpis = {gfx::Size(600, 600)}; | 57 caps->dpis = {gfx::Size(600, 600)}; |
| 88 | 58 |
| 89 print_backend()->AddValidPrinter(printer_name, std::move(caps)); | 59 print_backend()->AddValidPrinter(printer_name, std::move(caps)); |
| 90 | 60 |
| 91 std::unique_ptr<base::DictionaryValue> settings_dictionary = | 61 std::unique_ptr<base::DictionaryValue> settings_dictionary = |
| 92 GetSettingsSynchronous(printer_name, basic_info); | 62 GetSettingsOnBlockingPool(printer_name, basic_info); |
| 93 | 63 |
| 94 // verify settings were created | 64 // verify settings were created |
| 95 ASSERT_TRUE(settings_dictionary); | 65 ASSERT_TRUE(settings_dictionary); |
| 96 | 66 |
| 97 // verify capabilities and have one entry | 67 // verify capabilities and have one entry |
| 98 base::DictionaryValue* cdd; | 68 base::DictionaryValue* cdd; |
| 99 ASSERT_TRUE(settings_dictionary->GetDictionary("capabilities", &cdd)); | 69 ASSERT_TRUE(settings_dictionary->GetDictionary("capabilities", &cdd)); |
| 100 | 70 |
| 101 // read the CDD for the dpi attribute. | 71 // read the CDD for the dpi attribute. |
| 102 base::DictionaryValue* caps_dict; | 72 base::DictionaryValue* caps_dict; |
| 103 ASSERT_TRUE(cdd->GetDictionary("printer", &caps_dict)); | 73 ASSERT_TRUE(cdd->GetDictionary("printer", &caps_dict)); |
| 104 EXPECT_TRUE(caps_dict->HasKey("dpi")); | 74 EXPECT_TRUE(caps_dict->HasKey("dpi")); |
| 105 } | 75 } |
| 106 | 76 |
| 107 // Ensure that the capabilities dictionary is present but empty if the backend | 77 // Ensure that the capabilities dictionary is present but empty if the backend |
| 108 // doesn't return capabilities. | 78 // doesn't return capabilities. |
| 109 TEST_F(PrinterCapabilitiesTest, NullCapabilitiesExcluded) { | 79 TEST_F(PrinterCapabilitiesTest, NullCapabilitiesExcluded) { |
| 110 std::string printer_name = "test_printer"; | 80 std::string printer_name = "test_printer"; |
| 111 printing::PrinterBasicInfo basic_info; | 81 PrinterBasicInfo basic_info; |
| 112 | 82 |
| 113 // return false when attempting to retrieve capabilities | 83 // return false when attempting to retrieve capabilities |
| 114 print_backend()->AddValidPrinter(printer_name, nullptr); | 84 print_backend()->AddValidPrinter(printer_name, nullptr); |
| 115 | 85 |
| 116 std::unique_ptr<base::DictionaryValue> settings_dictionary = | 86 std::unique_ptr<base::DictionaryValue> settings_dictionary = |
| 117 GetSettingsSynchronous(printer_name, basic_info); | 87 GetSettingsOnBlockingPool(printer_name, basic_info); |
| 118 | 88 |
| 119 // verify settings were created | 89 // verify settings were created |
| 120 ASSERT_TRUE(settings_dictionary); | 90 ASSERT_TRUE(settings_dictionary); |
| 121 | 91 |
| 122 // verify that capabilities is an empty dictionary | 92 // verify that capabilities is an empty dictionary |
| 123 base::DictionaryValue* caps_dict; | 93 base::DictionaryValue* caps_dict; |
| 124 ASSERT_TRUE(settings_dictionary->GetDictionary("capabilities", &caps_dict)); | 94 ASSERT_TRUE(settings_dictionary->GetDictionary("capabilities", &caps_dict)); |
| 125 EXPECT_TRUE(caps_dict->empty()); | 95 EXPECT_TRUE(caps_dict->empty()); |
| 126 } | 96 } |
| 97 |
| 98 } // namespace printing |
| OLD | NEW |