OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <set> | 5 #include <set> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
14 #include "device/core/device_client.h" | 14 #include "device/core/device_client.h" |
15 #include "device/devices_app/usb/device_impl.h" | 15 #include "device/devices_app/usb/device_impl.h" |
16 #include "device/devices_app/usb/device_manager_impl.h" | 16 #include "device/devices_app/usb/device_manager_impl.h" |
| 17 #include "device/devices_app/usb/fake_permission_provider.h" |
17 #include "device/usb/mock_usb_device.h" | 18 #include "device/usb/mock_usb_device.h" |
18 #include "device/usb/mock_usb_device_handle.h" | 19 #include "device/usb/mock_usb_device_handle.h" |
19 #include "device/usb/mock_usb_service.h" | 20 #include "device/usb/mock_usb_service.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" | |
22 | 22 |
23 using ::testing::Invoke; | 23 using ::testing::Invoke; |
24 using ::testing::_; | 24 using ::testing::_; |
25 | 25 |
26 namespace device { | 26 namespace device { |
27 namespace usb { | 27 namespace usb { |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 class TestPermissionProvider : public PermissionProvider { | |
32 public: | |
33 TestPermissionProvider(mojo::InterfaceRequest<PermissionProvider> request) | |
34 : binding_(this, request.Pass()) {} | |
35 ~TestPermissionProvider() override {} | |
36 | |
37 void HasDevicePermission( | |
38 mojo::Array<DeviceInfoPtr> requested_devices, | |
39 const HasDevicePermissionCallback& callback) override { | |
40 // Permission to access all devices granted. | |
41 mojo::Array<mojo::String> allowed_guids(requested_devices.size()); | |
42 for (size_t i = 0; i < requested_devices.size(); ++i) | |
43 allowed_guids[i] = requested_devices[i]->guid; | |
44 callback.Run(allowed_guids.Pass()); | |
45 } | |
46 | |
47 private: | |
48 mojo::StrongBinding<PermissionProvider> binding_; | |
49 }; | |
50 | |
51 class TestDeviceClient : public DeviceClient { | 31 class TestDeviceClient : public DeviceClient { |
52 public: | 32 public: |
53 TestDeviceClient() {} | 33 TestDeviceClient() {} |
54 ~TestDeviceClient() override {} | 34 ~TestDeviceClient() override {} |
55 | 35 |
56 MockUsbService& mock_usb_service() { return mock_usb_service_; } | 36 MockUsbService& mock_usb_service() { return mock_usb_service_; } |
57 | 37 |
58 private: | 38 private: |
59 // DeviceClient implementation: | 39 // DeviceClient implementation: |
60 UsbService* GetUsbService() override { return &mock_usb_service_; } | 40 UsbService* GetUsbService() override { return &mock_usb_service_; } |
61 | 41 |
62 MockUsbService mock_usb_service_; | 42 MockUsbService mock_usb_service_; |
63 }; | 43 }; |
64 | 44 |
65 class USBDeviceManagerImplTest : public testing::Test { | 45 class USBDeviceManagerImplTest : public testing::Test { |
66 public: | 46 public: |
67 USBDeviceManagerImplTest() | 47 USBDeviceManagerImplTest() |
68 : device_client_(new TestDeviceClient), | 48 : device_client_(new TestDeviceClient), |
69 message_loop_(new base::MessageLoop) {} | 49 message_loop_(new base::MessageLoop) {} |
70 ~USBDeviceManagerImplTest() override {} | 50 ~USBDeviceManagerImplTest() override {} |
71 | 51 |
72 protected: | 52 protected: |
73 MockUsbService& mock_usb_service() { | 53 MockUsbService& mock_usb_service() { |
74 return device_client_->mock_usb_service(); | 54 return device_client_->mock_usb_service(); |
75 } | 55 } |
76 | 56 |
77 DeviceManagerPtr ConnectToDeviceManager() { | 57 DeviceManagerPtr ConnectToDeviceManager() { |
78 PermissionProviderPtr permission_provider; | 58 PermissionProviderPtr permission_provider; |
79 new TestPermissionProvider(mojo::GetProxy(&permission_provider)); | 59 permission_provider_.Bind(mojo::GetProxy(&permission_provider)); |
80 DeviceManagerPtr device_manager; | 60 DeviceManagerPtr device_manager; |
81 new DeviceManagerImpl(mojo::GetProxy(&device_manager), | 61 new DeviceManagerImpl(mojo::GetProxy(&device_manager), |
82 permission_provider.Pass(), | 62 permission_provider.Pass(), |
83 base::ThreadTaskRunnerHandle::Get()); | 63 base::ThreadTaskRunnerHandle::Get()); |
84 return device_manager.Pass(); | 64 return device_manager.Pass(); |
85 } | 65 } |
86 | 66 |
87 private: | 67 private: |
| 68 FakePermissionProvider permission_provider_; |
88 scoped_ptr<TestDeviceClient> device_client_; | 69 scoped_ptr<TestDeviceClient> device_client_; |
89 scoped_ptr<base::MessageLoop> message_loop_; | 70 scoped_ptr<base::MessageLoop> message_loop_; |
90 }; | 71 }; |
91 | 72 |
92 void ExpectDevicesAndThen(const std::set<std::string>& expected_guids, | 73 void ExpectDevicesAndThen(const std::set<std::string>& expected_guids, |
93 const base::Closure& continuation, | 74 const base::Closure& continuation, |
94 mojo::Array<DeviceInfoPtr> results) { | 75 mojo::Array<DeviceInfoPtr> results) { |
95 EXPECT_EQ(expected_guids.size(), results.size()); | 76 EXPECT_EQ(expected_guids.size(), results.size()); |
96 std::set<std::string> actual_guids; | 77 std::set<std::string> actual_guids; |
97 for (size_t i = 0; i < results.size(); ++i) | 78 for (size_t i = 0; i < results.size(); ++i) |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 base::RunLoop loop; | 226 base::RunLoop loop; |
246 device_manager->GetDeviceChanges(base::Bind(&ExpectDeviceChangesAndThen, | 227 device_manager->GetDeviceChanges(base::Bind(&ExpectDeviceChangesAndThen, |
247 added_guids, removed_guids, | 228 added_guids, removed_guids, |
248 loop.QuitClosure())); | 229 loop.QuitClosure())); |
249 loop.Run(); | 230 loop.Run(); |
250 } | 231 } |
251 } | 232 } |
252 | 233 |
253 } // namespace usb | 234 } // namespace usb |
254 } // namespace device | 235 } // namespace device |
OLD | NEW |