Chromium Code Reviews| 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 ::std::tr1::get<k>(args).Run(); | |
|
scheib
2015/05/20 04:17:06
First time I've seen "::std::tr1". Is that better
ortuno
2015/05/20 16:44:04
Done.
| |
| 34 } | |
| 35 | |
| 36 // Invokes Run() on the k-th argument of the function with the result | |
| 37 // of |func| as an argument. This is done to get around the lack of support | |
|
Jeffrey Yasskin
2015/05/20 00:27:28
I think you can remove the "This is done" explanat
ortuno
2015/05/20 16:44:04
Done.
| |
| 38 // for scoped_ptr in gmock. | |
| 39 ACTION_TEMPLATE(RunCallbackWithResult, | |
| 40 HAS_1_TEMPLATE_PARAMS(int, k), | |
| 41 AND_1_VALUE_PARAMS(func)) { | |
| 42 return ::std::tr1::get<k>(args).Run(func()); | |
| 43 } | |
| 44 } | |
| 45 | |
| 28 namespace content { | 46 namespace content { |
| 29 | 47 |
| 30 // static | 48 // static |
| 31 scoped_refptr<BluetoothAdapter> | 49 scoped_refptr<BluetoothAdapter> |
| 32 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( | 50 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( |
| 33 const std::string& fake_adapter_name) { | 51 const std::string& fake_adapter_name) { |
| 34 // TODO(ortuno): Remove RejectRequestDevice once LayoutTests are modified | 52 // TODO(ortuno): Remove RejectRequestDevice once LayoutTests are modified |
| 35 if (fake_adapter_name == "RejectRequestDevice_NotFoundError" || | 53 if (fake_adapter_name == "RejectRequestDevice_NotFoundError" || |
| 36 fake_adapter_name == "EmptyAdapter") { | 54 fake_adapter_name == "EmptyAdapter") { |
| 37 return GetEmptyAdapter(); | 55 return GetEmptyAdapter(); |
| 38 } | 56 } |
| 39 // TODO(ortuno): Remove "Single Empty Device" once LayoutTests are modified | 57 // TODO(ortuno): Remove "Single Empty Device" once LayoutTests are modified |
| 40 else if (fake_adapter_name == "Single Empty Device" || | 58 else if (fake_adapter_name == "Single Empty Device" || |
| 41 fake_adapter_name == "SingleEmptyDeviceAdapter") { | 59 fake_adapter_name == "SingleEmptyDeviceAdapter") { |
| 42 return GetSingleEmptyDeviceAdapter(); | 60 return GetSingleEmptyDeviceAdapter(); |
| 43 } else if (fake_adapter_name == "") { | 61 } else if (fake_adapter_name == "") { |
| 44 return NULL; | 62 return NULL; |
| 45 } | 63 } |
| 46 NOTREACHED(); | 64 NOTREACHED(); |
| 47 return NULL; | 65 return NULL; |
| 48 } | 66 } |
| 49 | 67 |
| 50 // static | 68 // static |
| 51 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 69 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 52 LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() { | 70 LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() { |
| 53 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( | 71 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( |
| 54 new NiceMock<MockBluetoothAdapter>()); | 72 new NiceMock<MockBluetoothAdapter>()); |
| 55 | 73 |
| 74 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> discovery_session( | |
|
scheib
2015/05/20 04:17:05
Is 'discovery_session' used? The anon function bel
ortuno
2015/05/20 16:44:04
Done.
| |
| 75 GetDiscoverySession()); | |
| 76 | |
| 56 ON_CALL(*adapter, StartDiscoverySession(_, _)) | 77 ON_CALL(*adapter, StartDiscoverySession(_, _)) |
| 57 .WillByDefault(Invoke( | 78 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>([]() { |
| 58 &LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySession)); | 79 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> discovery_session( |
| 80 GetDiscoverySession()); | |
| 81 return discovery_session.Pass(); | |
|
Jeffrey Yasskin
2015/05/20 00:27:28
In this particular function, you don't even need t
ortuno
2015/05/20 16:44:04
Done.
| |
| 82 })); | |
| 59 | 83 |
| 60 ON_CALL(*adapter, GetDevices()) | 84 ON_CALL(*adapter, GetDevices()) |
| 61 .WillByDefault(Return(adapter->GetMockDevices())); | 85 .WillByDefault( |
|
scheib
2015/05/20 04:17:05
Explain in comment here why. E.g.
// Using Invoke
ortuno
2015/05/20 16:44:04
Done.
scheib
2015/05/20 16:57:49
Not as done as I think you think I thought you wou
| |
| 86 Invoke(adapter.get(), &MockBluetoothAdapter::GetMockDevices)); | |
| 62 | 87 |
| 63 return adapter.Pass(); | 88 return adapter.Pass(); |
| 64 } | 89 } |
| 65 | 90 |
| 66 // static | 91 // static |
| 67 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 92 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 68 LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() { | 93 LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() { |
| 69 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( | 94 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); |
| 70 new NiceMock<MockBluetoothAdapter>()); | |
| 71 | |
| 72 ON_CALL(*adapter, StartDiscoverySession(_, _)) | |
| 73 .WillByDefault(Invoke( | |
| 74 &LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySession)); | |
| 75 | 95 |
| 76 adapter->AddMockDevice(GetEmptyDevice(adapter.get())); | 96 adapter->AddMockDevice(GetEmptyDevice(adapter.get())); |
| 77 | 97 |
| 78 ON_CALL(*adapter, GetDevices()) | |
| 79 .WillByDefault(Return(adapter->GetMockDevices())); | |
| 80 | |
| 81 return adapter.Pass(); | 98 return adapter.Pass(); |
| 82 } | 99 } |
| 83 | 100 |
| 84 // static | 101 // static |
| 85 scoped_ptr<NiceMock<MockBluetoothDevice>> | 102 scoped_ptr<NiceMock<MockBluetoothDevice>> |
| 86 LayoutTestBluetoothAdapterProvider::GetEmptyDevice( | 103 LayoutTestBluetoothAdapterProvider::GetEmptyDevice( |
| 87 MockBluetoothAdapter* adapter) { | 104 MockBluetoothAdapter* adapter) { |
| 88 scoped_ptr<NiceMock<MockBluetoothDevice>> empty_device( | 105 scoped_ptr<NiceMock<MockBluetoothDevice>> empty_device( |
| 89 new NiceMock<MockBluetoothDevice>( | 106 new NiceMock<MockBluetoothDevice>( |
| 90 adapter, 0x1F00 /* Bluetooth Class */, "Empty Mock Device name", | 107 adapter, 0x1F00 /* Bluetooth Class */, "Empty Mock Device name", |
| 91 "Empty Mock Device instanceID", true /* Paired */, | 108 "Empty Mock Device instanceID", true /* Paired */, |
| 92 true /* Connected */)); | 109 true /* Connected */)); |
| 93 | 110 |
| 94 ON_CALL(*empty_device, GetVendorIDSource()) | 111 ON_CALL(*empty_device, GetVendorIDSource()) |
| 95 .WillByDefault(Return(BluetoothDevice::VENDOR_ID_BLUETOOTH)); | 112 .WillByDefault(Return(BluetoothDevice::VENDOR_ID_BLUETOOTH)); |
| 96 ON_CALL(*empty_device, GetVendorID()).WillByDefault(Return(0xFFFF)); | 113 ON_CALL(*empty_device, GetVendorID()).WillByDefault(Return(0xFFFF)); |
| 97 ON_CALL(*empty_device, GetProductID()).WillByDefault(Return(1)); | 114 ON_CALL(*empty_device, GetProductID()).WillByDefault(Return(1)); |
| 98 ON_CALL(*empty_device, GetDeviceID()).WillByDefault(Return(2)); | 115 ON_CALL(*empty_device, GetDeviceID()).WillByDefault(Return(2)); |
| 99 | 116 |
| 100 BluetoothDevice::UUIDList list; | 117 BluetoothDevice::UUIDList list; |
| 101 list.push_back(BluetoothUUID("1800")); | 118 list.push_back(BluetoothUUID("1800")); |
| 102 list.push_back(BluetoothUUID("1801")); | 119 list.push_back(BluetoothUUID("1801")); |
| 103 ON_CALL(*empty_device, GetUUIDs()).WillByDefault(Return(list)); | 120 ON_CALL(*empty_device, GetUUIDs()).WillByDefault(Return(list)); |
| 104 return empty_device.Pass(); | 121 return empty_device.Pass(); |
| 105 } | 122 } |
| 106 | 123 |
| 107 // static | 124 // static |
| 108 void LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySession( | 125 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> |
| 109 const BluetoothAdapter::DiscoverySessionCallback& callback, | 126 LayoutTestBluetoothAdapterProvider::GetDiscoverySession() { |
| 110 const BluetoothAdapter::ErrorCallback& error_callback) { | |
| 111 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> discovery_session( | 127 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> discovery_session( |
| 112 new NiceMock<MockBluetoothDiscoverySession>()); | 128 new NiceMock<MockBluetoothDiscoverySession>()); |
| 113 | 129 |
| 114 ON_CALL(*discovery_session, Stop(_, _)) | 130 ON_CALL(*discovery_session, Stop(_, _)) |
| 115 .WillByDefault(Invoke( | 131 .WillByDefault(RunCallback<0 /* success_callback */>()); |
| 116 &LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySessionStop)); | |
| 117 | 132 |
| 118 callback.Run(discovery_session.Pass()); | 133 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 } | 134 } |
| 127 | 135 |
| 128 } // namespace content | 136 } // namespace content |
| OLD | NEW |