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

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: Only include the permission granting part in this patch. 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..755a7075e6c179e1a84863298b3a2726c9380364 100644
--- a/extensions/browser/api/printer_provider/printer_provider_apitest.cc
+++ b/extensions/browser/api/printer_provider/printer_provider_apitest.cc
@@ -13,6 +13,8 @@
#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"
@@ -24,6 +26,9 @@
#include "extensions/test/result_catcher.h"
#include "testing/gtest/include/gtest/gtest.h"
+using device::MockUsbDevice;
+using device::MockUsbService;
+
namespace extensions {
namespace {
@@ -70,6 +75,17 @@ 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 +106,15 @@ class PrinterProviderApiTest : public ShellApiTest {
->DispatchGetPrintersRequested(callback);
}
+ void StartGrantUsbPrinterAccess(
+ const std::string& extension_id,
+ int device_id,
+ const PrinterProviderAPI::UsbAccessGrantedCallback& callback) {
+ PrinterProviderAPIFactory::GetInstance()
+ ->GetForBrowserContext(browser_context())
+ ->DispatchGrantUsbPrinterAccess(extension_id, device_id, callback);
+ }
+
void StartPrintRequestWithNoData(
const std::string& extension_id,
const PrinterProviderAPI::PrintCallback& callback) {
@@ -281,10 +306,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:
+ MockUsbService usb_service_;
+
private:
// Initializes |data_dir_| if needed and creates a file in it containing
// provided data.
@@ -758,6 +786,64 @@ IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetPrintersInvalidPrinterValue) {
EXPECT_TRUE(printers.empty());
}
+IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GrantUsbPrinterAccess) {
+ ResultCatcher catcher;
+ usb_service_.AddDevice(new MockUsbDevice(0, 0, "Google", "USB Printer", ""));
+
+ std::string extension_id;
+ InitializePrinterProviderTestApp("api_test/printer_provider/usb_printers",
+ "OK", &extension_id);
+ ASSERT_FALSE(extension_id.empty());
+
+ base::ListValue printers;
+ {
+ base::RunLoop run_loop;
+ StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
+ &printers, run_loop.QuitClosure()));
+ run_loop.Run();
+ }
+
+ ScopedVector<base::Value> expected_printers;
+ ValidatePrinterListValue(printers, expected_printers);
+
+ scoped_ptr<base::Value> expected_printer_info(
+ DictionaryBuilder()
+ .Set("description", "This printer is a USB device.")
+ .Set("id", "granted-0")
+ .Set("name", "Test Printer")
+ .Build());
+ {
+ base::RunLoop run_loop;
+ StartGrantUsbPrinterAccess(
+ extension_id, 0,
+ base::Bind(&ExpectValueAndRunCallback, expected_printer_info.get(),
+ run_loop.QuitClosure()));
+ run_loop.Run();
+ }
+
+ printers.Clear();
+ {
+ base::RunLoop run_loop;
+ StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
+ &printers, run_loop.QuitClosure()));
+ run_loop.Run();
+ }
+
+ expected_printers.push_back(
+ DictionaryBuilder()
+ .Set("description", "This printer is a USB device.")
+ .Set("extensionId", extension_id)
+ .Set("extensionName", "Test USB printer provider")
+ .Set("id",
+ base::StringPrintf("%s:enumerated-0", extension_id.c_str()))
+ .Set("name", "Test Printer")
+ .Build());
+
+ ValidatePrinterListValue(printers, expected_printers);
+
+ ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
+}
tbarzic 2015/05/27 00:11:03 can you add a test case for when the extension doe
Reilly Grant (use Gerrit) 2015/05/28 21:04:10 Done.
+
} // namespace
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698