diff options
author | tbarzic <tbarzic@chromium.org> | 2015-02-26 19:17:34 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-02-27 03:19:05 +0000 |
commit | 089e28561693b3287409b96048d0c3c02cd0b740 (patch) | |
tree | 2bce2686e9fed63a3b521bb40e681f6d161c0ab2 /chrome/browser/ui/webui/print_preview/extension_printer_handler.cc | |
parent | 715d4a3869c5fe16fa45a15a15e9583de4581527 (diff) | |
download | chromium_src-089e28561693b3287409b96048d0c3c02cd0b740.zip chromium_src-089e28561693b3287409b96048d0c3c02cd0b740.tar.gz chromium_src-089e28561693b3287409b96048d0c3c02cd0b740.tar.bz2 |
Add tests for ExtensionPrinterHandler
In process, changed ExtensionPrinterHandler construction so
pwg_raster_converter and task runner for reading converted PWG file
can be injected through constructor.
BUG=461114
TEST=unit_tests --gtest_filter=ExtensionPrinterHandler*
Review URL: https://codereview.chromium.org/954283003
Cr-Commit-Position: refs/heads/master@{#318390}
Diffstat (limited to 'chrome/browser/ui/webui/print_preview/extension_printer_handler.cc')
-rw-r--r-- | chrome/browser/ui/webui/print_preview/extension_printer_handler.cc | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/chrome/browser/ui/webui/print_preview/extension_printer_handler.cc b/chrome/browser/ui/webui/print_preview/extension_printer_handler.cc index aaeb3ff..e47a228 100644 --- a/chrome/browser/ui/webui/print_preview/extension_printer_handler.cc +++ b/chrome/browser/ui/webui/print_preview/extension_printer_handler.cc @@ -14,7 +14,6 @@ #include "base/memory/ref_counted.h" #include "base/memory/ref_counted_memory.h" #include "base/task_runner_util.h" -#include "base/threading/worker_pool.h" #include "chrome/browser/local_discovery/pwg_raster_converter.h" #include "components/cloud_devices/common/cloud_device_description.h" #include "components/cloud_devices/common/printer_description.h" @@ -33,6 +32,7 @@ const char kContentTypePWGRaster[] = "image/pwg-raster"; const char kContentTypeAll[] = "*/*"; const char kInvalidDataPrintError[] = "INVALID_DATA"; +const char kInvalidTicketPrintError[] = "INVALID_TICKET"; // Reads raster data from file path |raster_path| and returns it as // RefCountedMemory. Returns NULL on error. @@ -57,6 +57,7 @@ scoped_refptr<base::RefCountedMemory> ReadConvertedPWGRasterFileOnWorkerThread( // Posts a task to read a file containing converted PWG raster data to the // worker pool. void ReadConvertedPWGRasterFile( + const scoped_refptr<base::TaskRunner>& slow_task_runner, const ExtensionPrinterHandler::RefCountedMemoryCallback& callback, bool success, const base::FilePath& pwg_file_path) { @@ -66,7 +67,7 @@ void ReadConvertedPWGRasterFile( } base::PostTaskAndReplyWithResult( - base::WorkerPool::GetTaskRunner(true).get(), FROM_HERE, + slow_task_runner.get(), FROM_HERE, base::Bind(&ReadConvertedPWGRasterFileOnWorkerThread, pwg_file_path), callback); } @@ -74,8 +75,11 @@ void ReadConvertedPWGRasterFile( } // namespace ExtensionPrinterHandler::ExtensionPrinterHandler( - content::BrowserContext* browser_context) - : browser_context_(browser_context), weak_ptr_factory_(this) { + content::BrowserContext* browser_context, + const scoped_refptr<base::TaskRunner>& slow_task_runner) + : browser_context_(browser_context), + slow_task_runner_(slow_task_runner), + weak_ptr_factory_(this) { } ExtensionPrinterHandler::~ExtensionPrinterHandler() { @@ -134,25 +138,30 @@ void ExtensionPrinterHandler::StartPrint( return; } + cloud_devices::CloudDeviceDescription ticket; + if (!ticket.InitFromString(ticket_json)) { + WrapPrintCallback(callback, false, kInvalidTicketPrintError); + return; + } + print_job->content_type = kContentTypePWGRaster; - ConvertToPWGRaster(print_data, printer_description, ticket_json, page_size, + ConvertToPWGRaster(print_data, printer_description, ticket, page_size, base::Bind(&ExtensionPrinterHandler::DispatchPrintJob, weak_ptr_factory_.GetWeakPtr(), callback, base::Passed(&print_job))); } +void ExtensionPrinterHandler::SetPwgRasterConverterForTesting( + scoped_ptr<local_discovery::PWGRasterConverter> pwg_raster_converter) { + pwg_raster_converter_ = pwg_raster_converter.Pass(); +} + void ExtensionPrinterHandler::ConvertToPWGRaster( const scoped_refptr<base::RefCountedMemory>& data, const cloud_devices::CloudDeviceDescription& printer_description, - const std::string& ticket_json, + const cloud_devices::CloudDeviceDescription& ticket, const gfx::Size& page_size, const ExtensionPrinterHandler::RefCountedMemoryCallback& callback) { - cloud_devices::CloudDeviceDescription ticket; - if (!ticket.InitFromString(ticket_json)) { - callback.Run(scoped_refptr<base::RefCountedMemory>()); - return; - } - if (!pwg_raster_converter_) { pwg_raster_converter_ = PWGRasterConverter::CreateDefault(); } @@ -160,7 +169,7 @@ void ExtensionPrinterHandler::ConvertToPWGRaster( data.get(), PWGRasterConverter::GetConversionSettings(printer_description, page_size), PWGRasterConverter::GetBitmapSettings(printer_description, ticket), - base::Bind(&ReadConvertedPWGRasterFile, callback)); + base::Bind(&ReadConvertedPWGRasterFile, slow_task_runner_, callback)); } void ExtensionPrinterHandler::DispatchPrintJob( |