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 "content/shell/browser/layout_test/layout_test_bluetooth_adapter_provid
er.h" | 5 #include "content/shell/browser/layout_test/layout_test_bluetooth_adapter_provid
er.h" |
6 | 6 |
7 #include "device/bluetooth/bluetooth_adapter.h" | 7 #include "device/bluetooth/bluetooth_adapter.h" |
8 #include "device/bluetooth/bluetooth_device.h" | 8 #include "device/bluetooth/bluetooth_device.h" |
9 #include "device/bluetooth/bluetooth_discovery_session.h" | 9 #include "device/bluetooth/bluetooth_discovery_session.h" |
10 #include "device/bluetooth/bluetooth_uuid.h" | 10 #include "device/bluetooth/bluetooth_uuid.h" |
11 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 11 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
12 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" | 12 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
14 | 14 |
15 using device::BluetoothAdapter; | 15 using device::BluetoothAdapter; |
16 using device::BluetoothAdapterFactory; | 16 using device::BluetoothAdapterFactory; |
17 using device::BluetoothDevice; | 17 using device::BluetoothDevice; |
18 using device::BluetoothDiscoverySession; | 18 using device::BluetoothDiscoverySession; |
19 using device::BluetoothUUID; | 19 using device::BluetoothUUID; |
20 using device::MockBluetoothAdapter; | 20 using device::MockBluetoothAdapter; |
21 using device::MockBluetoothDevice; | 21 using device::MockBluetoothDevice; |
22 using device::MockBluetoothDiscoverySession; | 22 using device::MockBluetoothDiscoverySession; |
23 using testing::Invoke; | 23 using testing::Invoke; |
24 using testing::Return; | 24 using testing::Return; |
25 using testing::NiceMock; | 25 using testing::NiceMock; |
26 using testing::_; | 26 using testing::_; |
27 | 27 |
| 28 namespace { |
| 29 // Invokes Run() on the k-th argument of the function with no arguments. |
| 30 ACTION_TEMPLATE(RunCallback, |
| 31 HAS_1_TEMPLATE_PARAMS(int, k), |
| 32 AND_0_VALUE_PARAMS()) { |
| 33 return ::testing::get<k>(args).Run(); |
| 34 } |
| 35 |
| 36 // Invokes Run() on the k-th argument of the function with the result |
| 37 // of |func| as an argument. |
| 38 ACTION_TEMPLATE(RunCallbackWithResult, |
| 39 HAS_1_TEMPLATE_PARAMS(int, k), |
| 40 AND_1_VALUE_PARAMS(func)) { |
| 41 return ::testing::get<k>(args).Run(func()); |
| 42 } |
| 43 } |
| 44 |
28 namespace content { | 45 namespace content { |
29 | 46 |
30 // static | 47 // static |
31 scoped_refptr<BluetoothAdapter> | 48 scoped_refptr<BluetoothAdapter> |
32 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( | 49 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( |
33 const std::string& fake_adapter_name) { | 50 const std::string& fake_adapter_name) { |
34 // TODO(ortuno): Remove RejectRequestDevice once LayoutTests are modified | 51 // TODO(ortuno): Remove RejectRequestDevice once LayoutTests are modified |
35 if (fake_adapter_name == "RejectRequestDevice_NotFoundError" || | 52 if (fake_adapter_name == "RejectRequestDevice_NotFoundError" || |
36 fake_adapter_name == "EmptyAdapter") { | 53 fake_adapter_name == "EmptyAdapter") { |
37 return GetEmptyAdapter(); | 54 return GetEmptyAdapter(); |
38 } | 55 } |
39 // TODO(ortuno): Remove "Single Empty Device" once LayoutTests are modified | 56 // TODO(ortuno): Remove "Single Empty Device" once LayoutTests are modified |
40 else if (fake_adapter_name == "Single Empty Device" || | 57 else if (fake_adapter_name == "Single Empty Device" || |
41 fake_adapter_name == "SingleEmptyDeviceAdapter") { | 58 fake_adapter_name == "SingleEmptyDeviceAdapter") { |
42 return GetSingleEmptyDeviceAdapter(); | 59 return GetSingleEmptyDeviceAdapter(); |
43 } else if (fake_adapter_name == "") { | 60 } else if (fake_adapter_name == "") { |
44 return NULL; | 61 return NULL; |
45 } | 62 } |
46 NOTREACHED(); | 63 NOTREACHED(); |
47 return NULL; | 64 return NULL; |
48 } | 65 } |
49 | 66 |
50 // static | 67 // static |
51 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 68 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
52 LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() { | 69 LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() { |
53 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( | 70 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( |
54 new NiceMock<MockBluetoothAdapter>()); | 71 new NiceMock<MockBluetoothAdapter>()); |
55 | 72 |
56 ON_CALL(*adapter, StartDiscoverySession(_, _)) | 73 ON_CALL(*adapter, StartDiscoverySession(_, _)) |
57 .WillByDefault(Invoke( | 74 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( |
58 &LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySession)); | 75 []() { return GetDiscoverySession(); })); |
59 | 76 |
| 77 // Using Invoke allows the adapter returned from this method to be futher |
| 78 // modified and have devices added to it. The call to ::GetDevices will |
| 79 // invoke ::GetConstMockDevices, returning all devices added up to that time. |
60 ON_CALL(*adapter, GetDevices()) | 80 ON_CALL(*adapter, GetDevices()) |
61 .WillByDefault(Return(adapter->GetConstMockDevices())); | 81 .WillByDefault( |
| 82 Invoke(adapter.get(), &MockBluetoothAdapter::GetConstMockDevices)); |
62 | 83 |
63 return adapter.Pass(); | 84 return adapter.Pass(); |
64 } | 85 } |
65 | 86 |
66 // static | 87 // static |
67 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 88 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
68 LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() { | 89 LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() { |
69 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( | 90 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); |
70 new NiceMock<MockBluetoothAdapter>()); | |
71 | |
72 ON_CALL(*adapter, StartDiscoverySession(_, _)) | |
73 .WillByDefault(Invoke( | |
74 &LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySession)); | |
75 | 91 |
76 adapter->AddMockDevice(GetEmptyDevice(adapter.get())); | 92 adapter->AddMockDevice(GetEmptyDevice(adapter.get())); |
77 | 93 |
78 ON_CALL(*adapter, GetDevices()) | |
79 .WillByDefault(Return(adapter->GetConstMockDevices())); | |
80 | |
81 return adapter.Pass(); | 94 return adapter.Pass(); |
82 } | 95 } |
83 | 96 |
84 // static | 97 // static |
85 scoped_ptr<NiceMock<MockBluetoothDevice>> | 98 scoped_ptr<NiceMock<MockBluetoothDevice>> |
86 LayoutTestBluetoothAdapterProvider::GetEmptyDevice( | 99 LayoutTestBluetoothAdapterProvider::GetEmptyDevice( |
87 MockBluetoothAdapter* adapter) { | 100 MockBluetoothAdapter* adapter) { |
88 scoped_ptr<NiceMock<MockBluetoothDevice>> empty_device( | 101 scoped_ptr<NiceMock<MockBluetoothDevice>> empty_device( |
89 new NiceMock<MockBluetoothDevice>( | 102 new NiceMock<MockBluetoothDevice>( |
90 adapter, 0x1F00 /* Bluetooth Class */, "Empty Mock Device name", | 103 adapter, 0x1F00 /* Bluetooth Class */, "Empty Mock Device name", |
91 "Empty Mock Device instanceID", true /* Paired */, | 104 "Empty Mock Device instanceID", true /* Paired */, |
92 true /* Connected */)); | 105 true /* Connected */)); |
93 | 106 |
94 ON_CALL(*empty_device, GetVendorIDSource()) | 107 ON_CALL(*empty_device, GetVendorIDSource()) |
95 .WillByDefault(Return(BluetoothDevice::VENDOR_ID_BLUETOOTH)); | 108 .WillByDefault(Return(BluetoothDevice::VENDOR_ID_BLUETOOTH)); |
96 ON_CALL(*empty_device, GetVendorID()).WillByDefault(Return(0xFFFF)); | 109 ON_CALL(*empty_device, GetVendorID()).WillByDefault(Return(0xFFFF)); |
97 ON_CALL(*empty_device, GetProductID()).WillByDefault(Return(1)); | 110 ON_CALL(*empty_device, GetProductID()).WillByDefault(Return(1)); |
98 ON_CALL(*empty_device, GetDeviceID()).WillByDefault(Return(2)); | 111 ON_CALL(*empty_device, GetDeviceID()).WillByDefault(Return(2)); |
99 | 112 |
100 BluetoothDevice::UUIDList list; | 113 BluetoothDevice::UUIDList list; |
101 list.push_back(BluetoothUUID("1800")); | 114 list.push_back(BluetoothUUID("1800")); |
102 list.push_back(BluetoothUUID("1801")); | 115 list.push_back(BluetoothUUID("1801")); |
103 ON_CALL(*empty_device, GetUUIDs()).WillByDefault(Return(list)); | 116 ON_CALL(*empty_device, GetUUIDs()).WillByDefault(Return(list)); |
104 return empty_device.Pass(); | 117 return empty_device.Pass(); |
105 } | 118 } |
106 | 119 |
107 // static | 120 // static |
108 void LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySession( | 121 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> |
109 const BluetoothAdapter::DiscoverySessionCallback& callback, | 122 LayoutTestBluetoothAdapterProvider::GetDiscoverySession() { |
110 const BluetoothAdapter::ErrorCallback& error_callback) { | |
111 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> discovery_session( | 123 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> discovery_session( |
112 new NiceMock<MockBluetoothDiscoverySession>()); | 124 new NiceMock<MockBluetoothDiscoverySession>()); |
113 | 125 |
114 ON_CALL(*discovery_session, Stop(_, _)) | 126 ON_CALL(*discovery_session, Stop(_, _)) |
115 .WillByDefault(Invoke( | 127 .WillByDefault(RunCallback<0 /* success_callback */>()); |
116 &LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySessionStop)); | |
117 | 128 |
118 callback.Run(discovery_session.Pass()); | 129 return discovery_session.Pass(); |
119 } | |
120 | |
121 // static | |
122 void LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySessionStop( | |
123 const base::Closure& callback, | |
124 const base::Closure& error_callback) { | |
125 callback.Run(); | |
126 } | 130 } |
127 | 131 |
128 } // namespace content | 132 } // namespace content |
OLD | NEW |