1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/message_loop/message_loop.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "components/usb_service/usb_device.h"
8 #include "components/usb_service/usb_device_handle.h"
9 #include "device/test/usb_test_gadget.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 using ::usb_service::UsbDeviceHandle
;
18 class UsbServiceTest
: public ::testing::Test
{
20 virtual void SetUp() {
21 message_loop_
.reset(new base::MessageLoopForIO
);
25 scoped_ptr
<base::MessageLoop
> message_loop_
;
28 TEST_F(UsbServiceTest
, ClaimGadget
) {
29 if (!UsbTestGadget::IsTestEnabled()) return;
31 scoped_ptr
<UsbTestGadget
> gadget
= UsbTestGadget::Claim();
32 ASSERT_TRUE(gadget
.get());
34 scoped_refptr
<UsbDeviceHandle
> handle
= gadget
->GetDevice()->Open();
37 ASSERT_TRUE(handle
->GetManufacturer(&utf16
));
38 ASSERT_EQ("Google Inc.", base::UTF16ToUTF8(utf16
));
39 // Check again to make sure string descriptor caching works.
40 ASSERT_EQ("Google Inc.", base::UTF16ToUTF8(utf16
));
42 ASSERT_TRUE(handle
->GetProduct(&utf16
));
43 ASSERT_EQ("Test Gadget (default state)", base::UTF16ToUTF8(utf16
));
44 // Check again to make sure string descriptor caching works.
45 ASSERT_EQ("Test Gadget (default state)", base::UTF16ToUTF8(utf16
));
47 ASSERT_TRUE(handle
->GetSerial(&utf16
));
48 ASSERT_EQ(gadget
->GetSerial(), base::UTF16ToUTF8(utf16
));
49 // Check again to make sure string descriptor caching works.
50 ASSERT_EQ(gadget
->GetSerial(), base::UTF16ToUTF8(utf16
));
53 TEST_F(UsbServiceTest
, DisconnectAndReconnect
) {
54 if (!UsbTestGadget::IsTestEnabled()) return;
56 scoped_ptr
<UsbTestGadget
> gadget
= UsbTestGadget::Claim();
57 ASSERT_TRUE(gadget
.get());
58 ASSERT_TRUE(gadget
->Disconnect());
59 ASSERT_TRUE(gadget
->Reconnect());