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 "device/bluetooth/test/bluetooth_test.h" | 5 #include "device/bluetooth/test/bluetooth_test.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "device/bluetooth/bluetooth_adapter.h" | 10 #include "device/bluetooth/bluetooth_adapter.h" |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 | 27 |
| 28 BluetoothTestBase::BluetoothTestBase() : weak_factory_(this) {} | 28 BluetoothTestBase::BluetoothTestBase() : weak_factory_(this) {} |
| 29 | 29 |
| 30 BluetoothTestBase::~BluetoothTestBase() { | 30 BluetoothTestBase::~BluetoothTestBase() { |
| 31 } | 31 } |
| 32 | 32 |
| 33 void BluetoothTestBase::StartLowEnergyDiscoverySession() { | 33 void BluetoothTestBase::StartLowEnergyDiscoverySession() { |
| 34 adapter_->StartDiscoverySessionWithFilter( | 34 adapter_->StartDiscoverySessionWithFilter( |
| 35 make_scoped_ptr(new BluetoothDiscoveryFilter( | 35 make_scoped_ptr(new BluetoothDiscoveryFilter( |
| 36 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)), | 36 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)), |
| 37 GetDiscoverySessionCallback(), GetErrorCallback()); | 37 GetDiscoverySessionCallback(Call::EXPECTED), |
| 38 GetErrorCallback(Call::NOT_EXPECTED)); | |
| 38 base::RunLoop().RunUntilIdle(); | 39 base::RunLoop().RunUntilIdle(); |
| 39 } | 40 } |
| 40 | 41 |
| 42 void BluetoothTestBase::StartLowEnergyDiscoverySessionExpectedToFail() { | |
| 43 adapter_->StartDiscoverySessionWithFilter( | |
| 44 make_scoped_ptr(new BluetoothDiscoveryFilter( | |
| 45 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)), | |
| 46 GetDiscoverySessionCallback(Call::NOT_EXPECTED), | |
| 47 GetErrorCallback(Call::EXPECTED)); | |
| 48 base::RunLoop().RunUntilIdle(); | |
| 49 } | |
| 50 | |
| 51 void BluetoothTestBase::TearDown() { | |
|
scheib
2015/11/20 23:08:20
Let's also verify that the success callbacks made
ortuno
2015/11/21 01:12:49
Done.
| |
| 52 EXPECT_FALSE(unexpected_callback_); | |
| 53 } | |
| 54 | |
| 41 bool BluetoothTestBase::DenyPermission() { | 55 bool BluetoothTestBase::DenyPermission() { |
| 42 return false; | 56 return false; |
| 43 } | 57 } |
| 44 | 58 |
| 45 BluetoothDevice* BluetoothTestBase::DiscoverLowEnergyDevice( | 59 BluetoothDevice* BluetoothTestBase::DiscoverLowEnergyDevice( |
| 46 int device_ordinal) { | 60 int device_ordinal) { |
| 47 NOTIMPLEMENTED(); | 61 NOTIMPLEMENTED(); |
| 48 return nullptr; | 62 return nullptr; |
| 49 } | 63 } |
| 50 | 64 |
| 51 void BluetoothTestBase::DeleteDevice(BluetoothDevice* device) { | 65 void BluetoothTestBase::DeleteDevice(BluetoothDevice* device) { |
| 52 adapter_->DeleteDeviceForTesting(device->GetAddress()); | 66 adapter_->DeleteDeviceForTesting(device->GetAddress()); |
| 53 } | 67 } |
| 54 | 68 |
| 55 void BluetoothTestBase::Callback() { | 69 void BluetoothTestBase::Callback(Call expected) { |
| 70 if (expected == Call::NOT_EXPECTED) { | |
| 71 unexpected_callback_ = true; | |
| 72 return; | |
|
scheib
2015/11/20 23:08:20
I think we want the counters always accurate, even
ortuno
2015/11/21 01:12:49
Done.
| |
| 73 } | |
| 56 ++callback_count_; | 74 ++callback_count_; |
| 57 } | 75 } |
| 58 | 76 |
| 59 void BluetoothTestBase::DiscoverySessionCallback( | 77 void BluetoothTestBase::DiscoverySessionCallback( |
| 78 Call expected, | |
| 60 scoped_ptr<BluetoothDiscoverySession> discovery_session) { | 79 scoped_ptr<BluetoothDiscoverySession> discovery_session) { |
| 80 if (expected == Call::NOT_EXPECTED) { | |
| 81 unexpected_callback_ = true; | |
| 82 return; | |
| 83 } | |
| 61 ++callback_count_; | 84 ++callback_count_; |
| 62 discovery_sessions_.push_back(discovery_session.release()); | 85 discovery_sessions_.push_back(discovery_session.release()); |
| 63 } | 86 } |
| 64 | 87 |
| 65 void BluetoothTestBase::GattConnectionCallback( | 88 void BluetoothTestBase::GattConnectionCallback( |
| 89 Call expected, | |
| 66 scoped_ptr<BluetoothGattConnection> connection) { | 90 scoped_ptr<BluetoothGattConnection> connection) { |
| 91 if (expected == Call::NOT_EXPECTED) { | |
| 92 unexpected_callback_ = true; | |
| 93 return; | |
| 94 } | |
| 67 ++callback_count_; | 95 ++callback_count_; |
| 68 gatt_connections_.push_back(connection.release()); | 96 gatt_connections_.push_back(connection.release()); |
| 69 } | 97 } |
| 70 | 98 |
| 71 void BluetoothTestBase::NotifyCallback( | 99 void BluetoothTestBase::NotifyCallback( |
| 100 Call expected, | |
| 72 scoped_ptr<BluetoothGattNotifySession> notify_session) { | 101 scoped_ptr<BluetoothGattNotifySession> notify_session) { |
| 102 if (expected == Call::NOT_EXPECTED) { | |
| 103 unexpected_callback_ = true; | |
| 104 return; | |
| 105 } | |
| 73 ++callback_count_; | 106 ++callback_count_; |
| 74 notify_sessions_.push_back(notify_session.release()); | 107 notify_sessions_.push_back(notify_session.release()); |
| 75 } | 108 } |
| 76 | 109 |
| 77 void BluetoothTestBase::ReadValueCallback(const std::vector<uint8>& value) { | 110 void BluetoothTestBase::ReadValueCallback(Call expected, |
| 111 const std::vector<uint8>& value) { | |
| 112 if (expected == Call::NOT_EXPECTED) { | |
| 113 unexpected_callback_ = true; | |
| 114 return; | |
| 115 } | |
| 78 ++callback_count_; | 116 ++callback_count_; |
| 79 last_read_value_ = value; | 117 last_read_value_ = value; |
| 80 } | 118 } |
| 81 | 119 |
| 82 void BluetoothTestBase::ErrorCallback() { | 120 void BluetoothTestBase::ErrorCallback(Call expected) { |
| 121 if (expected == Call::NOT_EXPECTED) { | |
| 122 unexpected_callback_ = true; | |
| 123 return; | |
| 124 } | |
| 83 ++error_callback_count_; | 125 ++error_callback_count_; |
| 84 } | 126 } |
| 85 | 127 |
| 86 void BluetoothTestBase::ConnectErrorCallback( | 128 void BluetoothTestBase::ConnectErrorCallback( |
| 129 Call expected, | |
| 87 enum BluetoothDevice::ConnectErrorCode error_code) { | 130 enum BluetoothDevice::ConnectErrorCode error_code) { |
| 131 if (expected == Call::NOT_EXPECTED) { | |
| 132 unexpected_callback_ = true; | |
| 133 return; | |
| 134 } | |
| 88 ++error_callback_count_; | 135 ++error_callback_count_; |
| 89 last_connect_error_code_ = error_code; | 136 last_connect_error_code_ = error_code; |
| 90 } | 137 } |
| 91 | 138 |
| 92 void BluetoothTestBase::GattErrorCallback( | 139 void BluetoothTestBase::GattErrorCallback( |
| 140 Call expected, | |
| 93 BluetoothGattService::GattErrorCode error_code) { | 141 BluetoothGattService::GattErrorCode error_code) { |
| 142 if (expected == Call::NOT_EXPECTED) { | |
| 143 unexpected_callback_ = true; | |
| 144 return; | |
| 145 } | |
| 94 ++error_callback_count_; | 146 ++error_callback_count_; |
| 95 last_gatt_error_code_ = error_code; | 147 last_gatt_error_code_ = error_code; |
| 96 } | 148 } |
| 97 | 149 |
| 98 base::Closure BluetoothTestBase::GetCallback() { | 150 base::Closure BluetoothTestBase::GetCallback(Call expected) { |
| 99 return base::Bind(&BluetoothTestBase::Callback, weak_factory_.GetWeakPtr()); | 151 return base::Bind(&BluetoothTestBase::Callback, weak_factory_.GetWeakPtr(), |
| 152 expected); | |
| 100 } | 153 } |
| 101 | 154 |
| 102 BluetoothAdapter::DiscoverySessionCallback | 155 BluetoothAdapter::DiscoverySessionCallback |
| 103 BluetoothTestBase::GetDiscoverySessionCallback() { | 156 BluetoothTestBase::GetDiscoverySessionCallback(Call expected) { |
| 104 return base::Bind(&BluetoothTestBase::DiscoverySessionCallback, | 157 return base::Bind(&BluetoothTestBase::DiscoverySessionCallback, |
| 105 weak_factory_.GetWeakPtr()); | 158 weak_factory_.GetWeakPtr(), expected); |
| 106 } | 159 } |
| 107 | 160 |
| 108 BluetoothDevice::GattConnectionCallback | 161 BluetoothDevice::GattConnectionCallback |
| 109 BluetoothTestBase::GetGattConnectionCallback() { | 162 BluetoothTestBase::GetGattConnectionCallback(Call expected) { |
| 110 return base::Bind(&BluetoothTestBase::GattConnectionCallback, | 163 return base::Bind(&BluetoothTestBase::GattConnectionCallback, |
| 111 weak_factory_.GetWeakPtr()); | 164 weak_factory_.GetWeakPtr(), expected); |
| 112 } | 165 } |
| 113 | 166 |
| 114 BluetoothGattCharacteristic::NotifySessionCallback | 167 BluetoothGattCharacteristic::NotifySessionCallback |
| 115 BluetoothTestBase::GetNotifyCallback() { | 168 BluetoothTestBase::GetNotifyCallback(Call expected) { |
| 116 return base::Bind(&BluetoothTestBase::NotifyCallback, | 169 return base::Bind(&BluetoothTestBase::NotifyCallback, |
| 117 weak_factory_.GetWeakPtr()); | 170 weak_factory_.GetWeakPtr(), expected); |
| 118 } | 171 } |
| 119 | 172 |
| 120 BluetoothGattCharacteristic::ValueCallback | 173 BluetoothGattCharacteristic::ValueCallback |
| 121 BluetoothTestBase::GetReadValueCallback() { | 174 BluetoothTestBase::GetReadValueCallback(Call expected) { |
| 122 return base::Bind(&BluetoothTestBase::ReadValueCallback, | 175 return base::Bind(&BluetoothTestBase::ReadValueCallback, |
| 123 weak_factory_.GetWeakPtr()); | 176 weak_factory_.GetWeakPtr(), expected); |
| 124 } | 177 } |
| 125 | 178 |
| 126 BluetoothAdapter::ErrorCallback BluetoothTestBase::GetErrorCallback() { | 179 BluetoothAdapter::ErrorCallback BluetoothTestBase::GetErrorCallback( |
| 180 Call expected) { | |
| 127 return base::Bind(&BluetoothTestBase::ErrorCallback, | 181 return base::Bind(&BluetoothTestBase::ErrorCallback, |
| 128 weak_factory_.GetWeakPtr()); | 182 weak_factory_.GetWeakPtr(), expected); |
| 129 } | 183 } |
| 130 | 184 |
| 131 BluetoothDevice::ConnectErrorCallback | 185 BluetoothDevice::ConnectErrorCallback |
| 132 BluetoothTestBase::GetConnectErrorCallback() { | 186 BluetoothTestBase::GetConnectErrorCallback(Call expected) { |
| 133 return base::Bind(&BluetoothTestBase::ConnectErrorCallback, | 187 return base::Bind(&BluetoothTestBase::ConnectErrorCallback, |
| 134 weak_factory_.GetWeakPtr()); | 188 weak_factory_.GetWeakPtr(), expected); |
| 135 } | 189 } |
| 136 | 190 |
| 137 base::Callback<void(BluetoothGattService::GattErrorCode)> | 191 base::Callback<void(BluetoothGattService::GattErrorCode)> |
| 138 BluetoothTestBase::GetGattErrorCallback() { | 192 BluetoothTestBase::GetGattErrorCallback(Call expected) { |
| 139 return base::Bind(&BluetoothTestBase::GattErrorCallback, | 193 return base::Bind(&BluetoothTestBase::GattErrorCallback, |
| 140 weak_factory_.GetWeakPtr()); | 194 weak_factory_.GetWeakPtr(), expected); |
| 141 } | 195 } |
| 142 | 196 |
| 143 void BluetoothTestBase::ResetEventCounts() { | 197 void BluetoothTestBase::ResetEventCounts() { |
| 144 last_connect_error_code_ = BluetoothDevice::ERROR_UNKNOWN; | 198 last_connect_error_code_ = BluetoothDevice::ERROR_UNKNOWN; |
| 145 callback_count_ = 0; | 199 callback_count_ = 0; |
| 146 error_callback_count_ = 0; | 200 error_callback_count_ = 0; |
| 201 unexpected_callback_ = false; | |
| 147 gatt_connection_attempts_ = 0; | 202 gatt_connection_attempts_ = 0; |
| 148 gatt_disconnection_attempts_ = 0; | 203 gatt_disconnection_attempts_ = 0; |
| 149 gatt_discovery_attempts_ = 0; | 204 gatt_discovery_attempts_ = 0; |
| 150 gatt_notify_characteristic_attempts_ = 0; | 205 gatt_notify_characteristic_attempts_ = 0; |
| 151 gatt_read_characteristic_attempts_ = 0; | 206 gatt_read_characteristic_attempts_ = 0; |
| 152 gatt_write_characteristic_attempts_ = 0; | 207 gatt_write_characteristic_attempts_ = 0; |
| 153 } | 208 } |
| 154 | 209 |
| 155 } // namespace device | 210 } // namespace device |
| OLD | NEW |