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

Unified Diff: extensions/browser/api/usb/usb_apitest.cc

Issue 891853002: Add a UsbService::Observer function for cleanup actions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 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/usb/usb_apitest.cc
diff --git a/extensions/browser/api/usb/usb_apitest.cc b/extensions/browser/api/usb/usb_apitest.cc
index 29060b2ab0ec3208fc41628d20fe666c2cbf98b4..f436f382f57710f383ad752ba6a015c8a856f63c 100644
--- a/extensions/browser/api/usb/usb_apitest.cc
+++ b/extensions/browser/api/usb/usb_apitest.cc
@@ -6,7 +6,9 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_utils.h"
#include "device/usb/usb_service.h"
+#include "extensions/browser/api/device_permissions_prompt.h"
#include "extensions/browser/api/usb/usb_api.h"
+#include "extensions/shell/browser/shell_extensions_api_client.h"
#include "extensions/shell/test/shell_apitest.h"
#include "extensions/test/extension_test_message_listener.h"
#include "net/base/io_buffer.h"
@@ -42,6 +44,38 @@ void RequestUsbAccess(int interface_id,
base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, true));
}
+class TestDevicePermissionsPrompt
+ : public DevicePermissionsPrompt,
+ public DevicePermissionsPrompt::Prompt::Observer {
+ public:
+ TestDevicePermissionsPrompt(content::WebContents* web_contents)
+ : DevicePermissionsPrompt(web_contents) {}
+
+ void ShowDialog() override { prompt()->SetObserver(this); }
+
+ void OnDevicesChanged() override {
+ std::vector<scoped_refptr<UsbDevice>> devices;
+ for (size_t i = 0; i < prompt()->GetDeviceCount(); ++i) {
+ prompt()->GrantDevicePermission(i);
+ devices.push_back(prompt()->GetDevice(i));
+ if (!prompt()->multiple()) {
+ break;
+ }
+ }
+ delegate()->OnUsbDevicesChosen(devices);
+ }
+};
+
+class TestExtensionsAPIClient : public ShellExtensionsAPIClient {
+ public:
+ TestExtensionsAPIClient() : ShellExtensionsAPIClient() {}
+
+ scoped_ptr<DevicePermissionsPrompt> CreateDevicePermissionsPrompt(
+ content::WebContents* web_contents) const override {
+ return make_scoped_ptr(new TestDevicePermissionsPrompt(web_contents));
+ }
+};
+
class MockUsbDeviceHandle : public UsbDeviceHandle {
public:
MockUsbDeviceHandle() : UsbDeviceHandle() {}
@@ -157,6 +191,10 @@ class UsbApiTest : public ShellApiTest {
ShellApiTest::SetUpOnMainThread();
mock_device_ = new MockUsbDevice(0, 0, 0);
+ EXPECT_CALL(*mock_device_.get(), GetManufacturer(_))
+ .WillRepeatedly(Return(false));
+ EXPECT_CALL(*mock_device_.get(), GetProduct(_))
+ .WillRepeatedly(Return(false));
EXPECT_CALL(*mock_device_.get(), GetSerialNumber(_))
.WillRepeatedly(Return(false));
@@ -298,7 +336,6 @@ IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceAdded) {
run_loop.Run();
ASSERT_TRUE(result_listener.WaitUntilSatisfied());
- ASSERT_EQ("success", result_listener.message());
}
IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) {
@@ -318,7 +355,28 @@ IN_PROC_BROWSER_TEST_F(UsbApiTest, OnDeviceRemoved) {
run_loop.Run();
ASSERT_TRUE(result_listener.WaitUntilSatisfied());
- ASSERT_EQ("success", result_listener.message());
+}
+
+IN_PROC_BROWSER_TEST_F(UsbApiTest, GetUserSelectedDevices) {
+ ExtensionTestMessageListener ready_listener("opened_device", false);
+ ExtensionTestMessageListener result_listener("success", false);
+ result_listener.set_failure_message("failure");
+
+ EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(1);
+
+ TestExtensionsAPIClient test_api_client;
+ ASSERT_TRUE(LoadApp("api_test/usb/get_user_selected_devices"));
+ ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
+
+ base::RunLoop run_loop;
+ BrowserThread::PostTaskAndReply(
+ BrowserThread::FILE, FROM_HERE,
+ base::Bind(&MockUsbService::NotifyDeviceRemoved,
+ base::Unretained(mock_service_), mock_device_),
+ run_loop.QuitClosure());
+ run_loop.Run();
+
+ ASSERT_TRUE(result_listener.WaitUntilSatisfied());
}
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698