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

Unified Diff: device/usb/usb_device_handle_unittest.cc

Issue 980023002: Move device/usb classes from the FILE thread to UI thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add more thread assertions. Created 5 years, 8 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
« no previous file with comments | « device/usb/usb_device_handle_impl.cc ('k') | device/usb/usb_device_impl.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: device/usb/usb_device_handle_unittest.cc
diff --git a/device/usb/usb_device_handle_unittest.cc b/device/usb/usb_device_handle_unittest.cc
index 4cb4dff3e778a5a148f32007f20952aca58416c0..4d5586e00cc6f0c10173163c496217cd99412269 100644
--- a/device/usb/usb_device_handle_unittest.cc
+++ b/device/usb/usb_device_handle_unittest.cc
@@ -6,6 +6,7 @@
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
+#include "base/test/test_io_thread.h"
#include "device/test/usb_test_gadget.h"
#include "device/usb/usb_device.h"
#include "device/usb/usb_device_handle.h"
@@ -18,35 +19,63 @@ namespace {
class UsbDeviceHandleTest : public ::testing::Test {
public:
void SetUp() override {
- if (!UsbTestGadget::IsTestEnabled()) {
- return;
- }
+ message_loop_.reset(new base::MessageLoopForUI);
+ io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart));
+ }
- message_loop_.reset(new base::MessageLoopForIO);
+ protected:
+ scoped_ptr<base::TestIOThread> io_thread_;
- gadget_ = UsbTestGadget::Claim();
- ASSERT_TRUE(gadget_.get());
+ private:
+ scoped_ptr<base::MessageLoop> message_loop_;
+};
- ASSERT_TRUE(gadget_->SetType(UsbTestGadget::ECHO));
+class TestOpenCallback {
+ public:
+ TestOpenCallback()
+ : callback_(
+ base::Bind(&TestOpenCallback::SetResult, base::Unretained(this))) {}
- handle_ = gadget_->GetDevice()->Open();
- ASSERT_TRUE(handle_.get());
+ scoped_refptr<UsbDeviceHandle> WaitForResult() {
+ run_loop_.Run();
+ return device_handle_;
}
- void TearDown() override {
- if (handle_.get()) {
- handle_->Close();
- }
- gadget_.reset(NULL);
- message_loop_.reset(NULL);
+ const UsbDevice::OpenCallback& callback() const { return callback_; }
+
+ private:
+ void SetResult(scoped_refptr<UsbDeviceHandle> device_handle) {
+ device_handle_ = device_handle;
+ run_loop_.Quit();
}
- protected:
- scoped_refptr<UsbDeviceHandle> handle_;
+ const UsbDevice::OpenCallback callback_;
+ base::RunLoop run_loop_;
+ scoped_refptr<UsbDeviceHandle> device_handle_;
+};
+
+class TestResultCallback {
+ public:
+ TestResultCallback()
+ : callback_(base::Bind(&TestResultCallback::SetResult,
+ base::Unretained(this))) {}
+
+ bool WaitForResult() {
+ run_loop_.Run();
+ return success_;
+ }
+
+ const UsbDeviceHandle::ResultCallback& callback() const { return callback_; }
private:
- scoped_ptr<UsbTestGadget> gadget_;
- scoped_ptr<base::MessageLoop> message_loop_;
+ void SetResult(bool success) {
+ success_ = success;
+ run_loop_.Quit();
+ }
+
+ const UsbDeviceHandle::ResultCallback callback_;
+ base::RunLoop run_loop_;
+ bool success_;
};
class TestCompletionCallback {
@@ -55,6 +84,15 @@ class TestCompletionCallback {
: callback_(base::Bind(&TestCompletionCallback::SetResult,
base::Unretained(this))) {}
+ void WaitForResult() { run_loop_.Run(); }
+
+ const UsbDeviceHandle::TransferCallback& callback() const {
+ return callback_;
+ }
+ UsbTransferStatus status() const { return status_; }
+ size_t transferred() const { return transferred_; }
+
+ private:
void SetResult(UsbTransferStatus status,
scoped_refptr<net::IOBuffer> buffer,
size_t transferred) {
@@ -63,34 +101,37 @@ class TestCompletionCallback {
run_loop_.Quit();
}
- void WaitForResult() { run_loop_.Run(); }
-
- const UsbTransferCallback& callback() const { return callback_; }
- UsbTransferStatus status() const { return status_; }
- size_t transferred() const { return transferred_; }
-
- private:
- const UsbTransferCallback callback_;
+ const UsbDeviceHandle::TransferCallback callback_;
base::RunLoop run_loop_;
UsbTransferStatus status_;
size_t transferred_;
};
TEST_F(UsbDeviceHandleTest, InterruptTransfer) {
- if (!handle_.get()) {
+ if (!UsbTestGadget::IsTestEnabled()) {
return;
}
- ASSERT_TRUE(handle_->ClaimInterface(0));
+ scoped_ptr<UsbTestGadget> gadget =
+ UsbTestGadget::Claim(io_thread_->task_runner());
+ ASSERT_TRUE(gadget.get());
+ ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO));
+
+ TestOpenCallback open_device;
+ gadget->GetDevice()->Open(open_device.callback());
+ scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult();
+ ASSERT_TRUE(handle.get());
+
+ TestResultCallback claim_interface;
+ handle->ClaimInterface(0, claim_interface.callback());
+ ASSERT_TRUE(claim_interface.WaitForResult());
scoped_refptr<net::IOBufferWithSize> in_buffer(new net::IOBufferWithSize(64));
TestCompletionCallback in_completion;
- handle_->InterruptTransfer(USB_DIRECTION_INBOUND,
- 0x81,
- in_buffer.get(),
- in_buffer->size(),
- 5000, // 5 second timeout
- in_completion.callback());
+ handle->InterruptTransfer(USB_DIRECTION_INBOUND, 0x81, in_buffer.get(),
+ in_buffer->size(),
+ 5000, // 5 second timeout
+ in_completion.callback());
scoped_refptr<net::IOBufferWithSize> out_buffer(
new net::IOBufferWithSize(in_buffer->size()));
@@ -99,12 +140,10 @@ TEST_F(UsbDeviceHandleTest, InterruptTransfer) {
out_buffer->data()[i] = i;
}
- handle_->InterruptTransfer(USB_DIRECTION_OUTBOUND,
- 0x01,
- out_buffer.get(),
- out_buffer->size(),
- 5000, // 5 second timeout
- out_completion.callback());
+ handle->InterruptTransfer(USB_DIRECTION_OUTBOUND, 0x01, out_buffer.get(),
+ out_buffer->size(),
+ 5000, // 5 second timeout
+ out_completion.callback());
out_completion.WaitForResult();
ASSERT_EQ(USB_TRANSFER_COMPLETED, out_completion.status());
EXPECT_EQ(static_cast<size_t>(out_buffer->size()),
@@ -117,22 +156,36 @@ TEST_F(UsbDeviceHandleTest, InterruptTransfer) {
for (size_t i = 0; i < in_completion.transferred(); ++i) {
EXPECT_EQ(out_buffer->data()[i], in_buffer->data()[i]);
}
+
+ handle->Close();
}
TEST_F(UsbDeviceHandleTest, BulkTransfer) {
- if (!handle_.get()) {
+ if (!UsbTestGadget::IsTestEnabled()) {
return;
}
- ASSERT_TRUE(handle_->ClaimInterface(1));
+ scoped_ptr<UsbTestGadget> gadget =
+ UsbTestGadget::Claim(io_thread_->task_runner());
+ ASSERT_TRUE(gadget.get());
+ ASSERT_TRUE(gadget->SetType(UsbTestGadget::ECHO));
+
+ TestOpenCallback open_device;
+ gadget->GetDevice()->Open(open_device.callback());
+ scoped_refptr<UsbDeviceHandle> handle = open_device.WaitForResult();
+ ASSERT_TRUE(handle.get());
+
+ TestResultCallback claim_interface;
+ handle->ClaimInterface(1, claim_interface.callback());
+ ASSERT_TRUE(claim_interface.WaitForResult());
scoped_refptr<net::IOBufferWithSize> in_buffer(
new net::IOBufferWithSize(512));
TestCompletionCallback in_completion;
- handle_->BulkTransfer(USB_DIRECTION_INBOUND, 0x82, in_buffer.get(),
- in_buffer->size(),
- 5000, // 5 second timeout
- in_completion.callback());
+ handle->BulkTransfer(USB_DIRECTION_INBOUND, 0x82, in_buffer.get(),
+ in_buffer->size(),
+ 5000, // 5 second timeout
+ in_completion.callback());
scoped_refptr<net::IOBufferWithSize> out_buffer(
new net::IOBufferWithSize(in_buffer->size()));
@@ -141,10 +194,10 @@ TEST_F(UsbDeviceHandleTest, BulkTransfer) {
out_buffer->data()[i] = i;
}
- handle_->BulkTransfer(USB_DIRECTION_OUTBOUND, 0x02, out_buffer.get(),
- out_buffer->size(),
- 5000, // 5 second timeout
- out_completion.callback());
+ handle->BulkTransfer(USB_DIRECTION_OUTBOUND, 0x02, out_buffer.get(),
+ out_buffer->size(),
+ 5000, // 5 second timeout
+ out_completion.callback());
out_completion.WaitForResult();
ASSERT_EQ(USB_TRANSFER_COMPLETED, out_completion.status());
EXPECT_EQ(static_cast<size_t>(out_buffer->size()),
@@ -157,6 +210,8 @@ TEST_F(UsbDeviceHandleTest, BulkTransfer) {
for (size_t i = 0; i < in_completion.transferred(); ++i) {
EXPECT_EQ(out_buffer->data()[i], in_buffer->data()[i]);
}
+
+ handle->Close();
}
} // namespace
« no previous file with comments | « device/usb/usb_device_handle_impl.cc ('k') | device/usb/usb_device_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698