Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Unified Diff: extensions/browser/api/printer_provider/printer_provider_apitest.cc

Issue 1148383002: Add onGetUsbPrinterInfoRequested event to printerProvider API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: extensions/browser/api/printer_provider/printer_provider_apitest.cc
diff --git a/extensions/browser/api/printer_provider/printer_provider_apitest.cc b/extensions/browser/api/printer_provider/printer_provider_apitest.cc
index ccec1a643b9cb0b7f91a3c6de46f5ff7e55049ed..643499cf8b19ffdce3186e8a8247cea99963a599 100644
--- a/extensions/browser/api/printer_provider/printer_provider_apitest.cc
+++ b/extensions/browser/api/printer_provider/printer_provider_apitest.cc
@@ -13,9 +13,12 @@
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
+#include "device/usb/mock_usb_device.h"
+#include "device/usb/mock_usb_service.h"
#include "extensions/browser/api/printer_provider/printer_provider_api.h"
#include "extensions/browser/api/printer_provider/printer_provider_api_factory.h"
#include "extensions/browser/api/printer_provider/printer_provider_print_job.h"
+#include "extensions/browser/api/usb/usb_guid_map.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h"
#include "extensions/common/value_builder.h"
@@ -70,6 +73,16 @@ void RecordDictAndRunCallback(std::string* result,
callback.Run();
}
+// Callback for PrinterProvider::DispatchGrantUsbPrinterAccess calls.
+// It expects |value| to equal |expected_value| and runs |callback|.
+void ExpectValueAndRunCallback(const base::Value* expected_value,
+ const base::Closure& callback,
+ const base::DictionaryValue& value) {
+ EXPECT_TRUE(value.Equals(expected_value));
+ if (!callback.is_null())
+ callback.Run();
+}
+
// Tests for chrome.printerProvider API.
class PrinterProviderApiTest : public ShellApiTest {
public:
@@ -90,6 +103,15 @@ class PrinterProviderApiTest : public ShellApiTest {
->DispatchGetPrintersRequested(callback);
}
+ void StartGetUsbPrinterInfoRequest(
+ const std::string& extension_id,
+ scoped_refptr<device::UsbDevice> device,
+ const PrinterProviderAPI::GetPrinterInfoCallback& callback) {
+ PrinterProviderAPIFactory::GetInstance()
+ ->GetForBrowserContext(browser_context())
+ ->DispatchGetUsbPrinterInfoRequested(extension_id, device, callback);
+ }
+
void StartPrintRequestWithNoData(
const std::string& extension_id,
const PrinterProviderAPI::PrintCallback& callback) {
@@ -255,6 +277,32 @@ class PrinterProviderApiTest : public ShellApiTest {
EXPECT_EQ(expected_result, result);
}
+ // Run a test for the chrome.printerProvider.onGetUsbPrinterInfoRequested
+ // event.
+ // |test_param|: The test that should be run.
+ // |expected_result|: The printer info that the app is expected to report.
+ void RunUsbPrinterInfoRequestTest(const std::string& test_param) {
+ ResultCatcher catcher;
+ scoped_refptr<device::UsbDevice> device =
+ new device::MockUsbDevice(0, 0, "Google", "USB Printer", "");
+ usb_service_.AddDevice(device);
+
+ std::string extension_id;
+ InitializePrinterProviderTestApp("api_test/printer_provider/usb_printers",
+ test_param, &extension_id);
+ ASSERT_FALSE(extension_id.empty());
+
+ scoped_ptr<base::Value> expected_printer_info(new base::DictionaryValue());
+ base::RunLoop run_loop;
+ StartGetUsbPrinterInfoRequest(
+ extension_id, device,
+ base::Bind(&ExpectValueAndRunCallback, expected_printer_info.get(),
+ run_loop.QuitClosure()));
+ run_loop.Run();
+
+ ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
+ }
+
bool SimulateExtensionUnload(const std::string& extension_id) {
ExtensionRegistry* extension_registry =
ExtensionRegistry::Get(browser_context());
@@ -281,10 +329,13 @@ class PrinterProviderApiTest : public ShellApiTest {
ASSERT_EQ(expected_printers.size(), printers.GetSize());
for (const base::Value* printer_value : expected_printers) {
EXPECT_TRUE(printers.Find(*printer_value) != printers.end())
- << "Unabe to find " << *printer_value << " in " << printers;
+ << "Unable to find " << *printer_value << " in " << printers;
}
}
+ protected:
+ device::MockUsbService usb_service_;
+
private:
// Initializes |data_dir_| if needed and creates a file in it containing
// provided data.
@@ -758,6 +809,46 @@ IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetPrintersInvalidPrinterValue) {
EXPECT_TRUE(printers.empty());
}
+IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetUsbPrinterInfo) {
+ ResultCatcher catcher;
+ scoped_refptr<device::UsbDevice> device =
+ new device::MockUsbDevice(0, 0, "Google", "USB Printer", "");
+ usb_service_.AddDevice(device);
+
+ std::string extension_id;
+ InitializePrinterProviderTestApp("api_test/printer_provider/usb_printers",
+ "OK", &extension_id);
+ ASSERT_FALSE(extension_id.empty());
+
+ UsbGuidMap* guid_map = UsbGuidMap::Get(browser_context());
+ scoped_ptr<base::Value> expected_printer_info(
+ DictionaryBuilder()
+ .Set("description", "This printer is a USB device.")
+ .Set("extensionId", extension_id)
+ .Set("extensionName", "Test USB printer provider")
+ .Set("id",
+ base::StringPrintf("%s:usbDevice-%u", extension_id.c_str(),
+ guid_map->GetIdFromGuid(device->guid())))
+ .Set("name", "Test Printer")
+ .Build());
+ base::RunLoop run_loop;
+ StartGetUsbPrinterInfoRequest(
+ extension_id, device,
+ base::Bind(&ExpectValueAndRunCallback, expected_printer_info.get(),
+ run_loop.QuitClosure()));
+ run_loop.Run();
+
+ ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
+}
+
+IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetUsbPrinterInfoEmptyResponse) {
+ RunUsbPrinterInfoRequestTest("EMPTY_RESPONSE");
+}
+
+IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetUsbPrinterInfoNoListener) {
+ RunUsbPrinterInfoRequestTest("NO_LISTENER");
+}
+
} // namespace
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698