OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "apps/saved_devices_service.h" | |
6 #include "base/run_loop.h" | |
7 #include "base/strings/utf_string_conversions.h" | |
8 #include "base/test/values_test_util.h" | |
9 #include "chrome/browser/extensions/test_extension_environment.h" | |
10 #include "chrome/test/base/testing_profile.h" | |
11 #include "device/usb/usb_device.h" | |
12 #include "device/usb/usb_device_handle.h" | |
13 #include "extensions/common/extension.h" | |
14 #include "testing/gmock/include/gmock/gmock.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace apps { | |
18 | |
19 namespace { | |
20 | |
21 using device::UsbDevice; | |
22 using device::UsbDeviceHandle; | |
23 using device::UsbEndpointDirection; | |
24 using device::UsbTransferCallback; | |
25 using testing::Return; | |
26 | |
27 class MockUsbDeviceHandle : public UsbDeviceHandle { | |
28 public: | |
29 MockUsbDeviceHandle(const std::string& serial_number) | |
30 : UsbDeviceHandle(), serial_number_(serial_number) {} | |
31 | |
32 MOCK_CONST_METHOD0(GetDevice, scoped_refptr<UsbDevice>()); | |
33 MOCK_METHOD0(Close, void()); | |
34 | |
35 MOCK_METHOD10(ControlTransfer, | |
36 void(UsbEndpointDirection direction, | |
37 TransferRequestType request_type, | |
38 TransferRecipient recipient, | |
39 uint8 request, | |
40 uint16 value, | |
41 uint16 index, | |
42 net::IOBuffer* buffer, | |
43 size_t length, | |
44 unsigned int timeout, | |
45 const UsbTransferCallback& callback)); | |
46 | |
47 MOCK_METHOD6(BulkTransfer, | |
48 void(UsbEndpointDirection direction, | |
49 uint8 endpoint, | |
50 net::IOBuffer* buffer, | |
51 size_t length, | |
52 unsigned int timeout, | |
53 const UsbTransferCallback& callback)); | |
54 | |
55 MOCK_METHOD6(InterruptTransfer, | |
56 void(UsbEndpointDirection direction, | |
57 uint8 endpoint, | |
58 net::IOBuffer* buffer, | |
59 size_t length, | |
60 unsigned int timeout, | |
61 const UsbTransferCallback& callback)); | |
62 | |
63 MOCK_METHOD8(IsochronousTransfer, | |
64 void(UsbEndpointDirection direction, | |
65 uint8 endpoint, | |
66 net::IOBuffer* buffer, | |
67 size_t length, | |
68 unsigned int packets, | |
69 unsigned int packet_length, | |
70 unsigned int timeout, | |
71 const UsbTransferCallback& callback)); | |
72 | |
73 MOCK_METHOD0(ResetDevice, bool()); | |
74 MOCK_METHOD1(ClaimInterface, bool(const int interface_number)); | |
75 MOCK_METHOD1(ReleaseInterface, bool(const int interface_number)); | |
76 MOCK_METHOD2(SetInterfaceAlternateSetting, | |
77 bool(const int interface_number, const int alternate_setting)); | |
78 MOCK_METHOD1(GetManufacturer, bool(base::string16* manufacturer)); | |
79 MOCK_METHOD1(GetProduct, bool(base::string16* product)); | |
80 | |
81 bool GetSerial(base::string16* serial) OVERRIDE { | |
82 if (serial_number_.empty()) { | |
83 return false; | |
84 } | |
85 | |
86 *serial = base::UTF8ToUTF16(serial_number_); | |
87 return true; | |
88 } | |
89 | |
90 private: | |
91 virtual ~MockUsbDeviceHandle() {} | |
92 | |
93 const std::string serial_number_; | |
94 }; | |
95 | |
96 class MockUsbDevice : public UsbDevice { | |
97 public: | |
98 MockUsbDevice(const std::string& serial_number, uint32 unique_id) | |
99 : UsbDevice(0, 0, unique_id), serial_number_(serial_number) {} | |
100 | |
101 MOCK_METHOD1(Close, bool(scoped_refptr<UsbDeviceHandle>)); | |
102 #if defined(OS_CHROMEOS) | |
103 MOCK_METHOD2(RequestUsbAccess, void(int, const base::Callback<void(bool)>&)); | |
104 #endif | |
105 MOCK_METHOD0(GetConfiguration, const device::UsbConfigDescriptor&()); | |
106 | |
107 scoped_refptr<UsbDeviceHandle> Open() OVERRIDE { | |
108 return new MockUsbDeviceHandle(serial_number_); | |
109 } | |
110 | |
111 void NotifyDisconnect() { UsbDevice::NotifyDisconnect(); } | |
112 | |
113 private: | |
114 virtual ~MockUsbDevice() {} | |
115 | |
116 const std::string serial_number_; | |
117 }; | |
118 } | |
119 | |
120 class SavedDevicesServiceTest : public testing::Test { | |
121 protected: | |
122 virtual void SetUp() OVERRIDE { | |
123 testing::Test::SetUp(); | |
124 extension_ = env_.MakeExtension(*base::test::ParseJson( | |
125 "{" | |
126 " \"app\": {" | |
127 " \"background\": {" | |
128 " \"scripts\": [\"background.js\"]" | |
129 " }" | |
130 " }," | |
131 " \"permissions\": [" | |
132 " \"usb\"" | |
133 " ]" | |
134 "}")); | |
135 service_ = SavedDevicesService::Get(env_.profile()); | |
136 device0 = new MockUsbDevice("ABCDE", 0); | |
137 device1 = new MockUsbDevice("", 1); | |
138 device2 = new MockUsbDevice("12345", 2); | |
139 device3 = new MockUsbDevice("", 3); | |
140 } | |
141 | |
142 extensions::TestExtensionEnvironment env_; | |
143 const extensions::Extension* extension_; | |
144 SavedDevicesService* service_; | |
145 scoped_refptr<MockUsbDevice> device0; | |
146 scoped_refptr<MockUsbDevice> device1; | |
147 scoped_refptr<MockUsbDevice> device2; | |
148 scoped_refptr<MockUsbDevice> device3; | |
149 }; | |
150 | |
151 TEST_F(SavedDevicesServiceTest, RegisterDevices) { | |
152 SavedDevicesService::SavedDevices* saved_devices = | |
153 service_->GetOrInsert(extension_->id()); | |
154 | |
155 base::string16 serial_number(base::ASCIIToUTF16("ABCDE")); | |
156 saved_devices->RegisterDevice(device0, &serial_number); | |
157 saved_devices->RegisterDevice(device1, NULL); | |
158 | |
159 // This is necessary as writing out registered devices happens in a task on | |
160 // the UI thread. | |
161 base::RunLoop run_loop; | |
162 run_loop.RunUntilIdle(); | |
163 | |
164 ASSERT_TRUE(saved_devices->IsRegistered(device0)); | |
165 ASSERT_TRUE(saved_devices->IsRegistered(device1)); | |
166 ASSERT_FALSE(saved_devices->IsRegistered(device2)); | |
167 ASSERT_FALSE(saved_devices->IsRegistered(device3)); | |
168 | |
169 std::vector<SavedDeviceEntry> device_entries = | |
170 service_->GetAllDevices(extension_->id()); | |
171 ASSERT_EQ(1U, device_entries.size()); | |
172 ASSERT_EQ(base::ASCIIToUTF16("ABCDE"), device_entries[0].serial_number); | |
173 | |
174 device1->NotifyDisconnect(); | |
175 | |
176 ASSERT_TRUE(saved_devices->IsRegistered(device0)); | |
177 ASSERT_FALSE(saved_devices->IsRegistered(device1)); | |
178 ASSERT_FALSE(saved_devices->IsRegistered(device2)); | |
179 ASSERT_FALSE(saved_devices->IsRegistered(device3)); | |
180 | |
181 service_->Clear(extension_->id()); | |
182 | |
183 // App is normally restarted, clearing its reference to the SavedDevices. | |
184 saved_devices = service_->GetOrInsert(extension_->id()); | |
185 ASSERT_FALSE(saved_devices->IsRegistered(device0)); | |
186 device_entries = service_->GetAllDevices(extension_->id()); | |
187 ASSERT_EQ(0U, device_entries.size()); | |
188 } | |
scheib
2014/09/18 20:31:49
Consider testing the restoration of saved devices
Reilly Grant (use Gerrit)
2014/09/18 21:27:59
I'm not sure how to do that with TestExtensionEnvi
| |
189 | |
190 } // namespace apps | |
OLD | NEW |