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() { |
| 52 EXPECT_EQ(expected_success_callback_calls_, actual_success_callback_calls_); |
| 53 EXPECT_EQ(expected_error_callback_calls_, actual_error_callback_calls_); |
| 54 EXPECT_FALSE(unexpected_success_callback_); |
| 55 EXPECT_FALSE(unexpected_error_callback_); |
| 56 } |
| 57 |
41 bool BluetoothTestBase::DenyPermission() { | 58 bool BluetoothTestBase::DenyPermission() { |
42 return false; | 59 return false; |
43 } | 60 } |
44 | 61 |
45 BluetoothDevice* BluetoothTestBase::DiscoverLowEnergyDevice( | 62 BluetoothDevice* BluetoothTestBase::DiscoverLowEnergyDevice( |
46 int device_ordinal) { | 63 int device_ordinal) { |
47 NOTIMPLEMENTED(); | 64 NOTIMPLEMENTED(); |
48 return nullptr; | 65 return nullptr; |
49 } | 66 } |
50 | 67 |
51 void BluetoothTestBase::DeleteDevice(BluetoothDevice* device) { | 68 void BluetoothTestBase::DeleteDevice(BluetoothDevice* device) { |
52 adapter_->DeleteDeviceForTesting(device->GetAddress()); | 69 adapter_->DeleteDeviceForTesting(device->GetAddress()); |
53 } | 70 } |
54 | 71 |
55 void BluetoothTestBase::Callback() { | 72 void BluetoothTestBase::Callback(Call expected) { |
56 ++callback_count_; | 73 ++callback_count_; |
| 74 |
| 75 if (expected == Call::EXPECTED) |
| 76 ++actual_success_callback_calls_; |
| 77 else |
| 78 unexpected_success_callback_ = true; |
57 } | 79 } |
58 | 80 |
59 void BluetoothTestBase::DiscoverySessionCallback( | 81 void BluetoothTestBase::DiscoverySessionCallback( |
| 82 Call expected, |
60 scoped_ptr<BluetoothDiscoverySession> discovery_session) { | 83 scoped_ptr<BluetoothDiscoverySession> discovery_session) { |
61 ++callback_count_; | 84 ++callback_count_; |
62 discovery_sessions_.push_back(discovery_session.release()); | 85 discovery_sessions_.push_back(discovery_session.release()); |
| 86 |
| 87 if (expected == Call::EXPECTED) |
| 88 ++actual_success_callback_calls_; |
| 89 else |
| 90 unexpected_success_callback_ = true; |
63 } | 91 } |
64 | 92 |
65 void BluetoothTestBase::GattConnectionCallback( | 93 void BluetoothTestBase::GattConnectionCallback( |
| 94 Call expected, |
66 scoped_ptr<BluetoothGattConnection> connection) { | 95 scoped_ptr<BluetoothGattConnection> connection) { |
67 ++callback_count_; | 96 ++callback_count_; |
68 gatt_connections_.push_back(connection.release()); | 97 gatt_connections_.push_back(connection.release()); |
| 98 |
| 99 if (expected == Call::EXPECTED) |
| 100 ++actual_success_callback_calls_; |
| 101 else |
| 102 unexpected_success_callback_ = true; |
69 } | 103 } |
70 | 104 |
71 void BluetoothTestBase::NotifyCallback( | 105 void BluetoothTestBase::NotifyCallback( |
| 106 Call expected, |
72 scoped_ptr<BluetoothGattNotifySession> notify_session) { | 107 scoped_ptr<BluetoothGattNotifySession> notify_session) { |
73 ++callback_count_; | 108 ++callback_count_; |
74 notify_sessions_.push_back(notify_session.release()); | 109 notify_sessions_.push_back(notify_session.release()); |
| 110 |
| 111 if (expected == Call::EXPECTED) |
| 112 ++actual_success_callback_calls_; |
| 113 else |
| 114 unexpected_success_callback_ = true; |
75 } | 115 } |
76 | 116 |
77 void BluetoothTestBase::ReadValueCallback(const std::vector<uint8>& value) { | 117 void BluetoothTestBase::ReadValueCallback(Call expected, |
| 118 const std::vector<uint8>& value) { |
78 ++callback_count_; | 119 ++callback_count_; |
79 last_read_value_ = value; | 120 last_read_value_ = value; |
| 121 |
| 122 if (expected == Call::EXPECTED) |
| 123 ++actual_success_callback_calls_; |
| 124 else |
| 125 unexpected_success_callback_ = true; |
80 } | 126 } |
81 | 127 |
82 void BluetoothTestBase::ErrorCallback() { | 128 void BluetoothTestBase::ErrorCallback(Call expected) { |
83 ++error_callback_count_; | 129 ++error_callback_count_; |
| 130 |
| 131 if (expected == Call::EXPECTED) |
| 132 ++actual_error_callback_calls_; |
| 133 else |
| 134 unexpected_error_callback_ = true; |
84 } | 135 } |
85 | 136 |
86 void BluetoothTestBase::ConnectErrorCallback( | 137 void BluetoothTestBase::ConnectErrorCallback( |
| 138 Call expected, |
87 enum BluetoothDevice::ConnectErrorCode error_code) { | 139 enum BluetoothDevice::ConnectErrorCode error_code) { |
88 ++error_callback_count_; | 140 ++error_callback_count_; |
89 last_connect_error_code_ = error_code; | 141 last_connect_error_code_ = error_code; |
| 142 |
| 143 if (expected == Call::EXPECTED) |
| 144 ++actual_error_callback_calls_; |
| 145 else |
| 146 unexpected_error_callback_ = true; |
90 } | 147 } |
91 | 148 |
92 void BluetoothTestBase::GattErrorCallback( | 149 void BluetoothTestBase::GattErrorCallback( |
| 150 Call expected, |
93 BluetoothGattService::GattErrorCode error_code) { | 151 BluetoothGattService::GattErrorCode error_code) { |
94 ++error_callback_count_; | 152 ++error_callback_count_; |
95 last_gatt_error_code_ = error_code; | 153 last_gatt_error_code_ = error_code; |
| 154 |
| 155 if (expected == Call::EXPECTED) |
| 156 ++actual_error_callback_calls_; |
| 157 else |
| 158 unexpected_error_callback_ = true; |
96 } | 159 } |
97 | 160 |
98 base::Closure BluetoothTestBase::GetCallback() { | 161 base::Closure BluetoothTestBase::GetCallback(Call expected) { |
99 return base::Bind(&BluetoothTestBase::Callback, weak_factory_.GetWeakPtr()); | 162 if (expected == Call::EXPECTED) |
| 163 ++expected_success_callback_calls_; |
| 164 return base::Bind(&BluetoothTestBase::Callback, weak_factory_.GetWeakPtr(), |
| 165 expected); |
100 } | 166 } |
101 | 167 |
102 BluetoothAdapter::DiscoverySessionCallback | 168 BluetoothAdapter::DiscoverySessionCallback |
103 BluetoothTestBase::GetDiscoverySessionCallback() { | 169 BluetoothTestBase::GetDiscoverySessionCallback(Call expected) { |
| 170 if (expected == Call::EXPECTED) |
| 171 ++expected_success_callback_calls_; |
104 return base::Bind(&BluetoothTestBase::DiscoverySessionCallback, | 172 return base::Bind(&BluetoothTestBase::DiscoverySessionCallback, |
105 weak_factory_.GetWeakPtr()); | 173 weak_factory_.GetWeakPtr(), expected); |
106 } | 174 } |
107 | 175 |
108 BluetoothDevice::GattConnectionCallback | 176 BluetoothDevice::GattConnectionCallback |
109 BluetoothTestBase::GetGattConnectionCallback() { | 177 BluetoothTestBase::GetGattConnectionCallback(Call expected) { |
| 178 if (expected == Call::EXPECTED) |
| 179 ++expected_success_callback_calls_; |
110 return base::Bind(&BluetoothTestBase::GattConnectionCallback, | 180 return base::Bind(&BluetoothTestBase::GattConnectionCallback, |
111 weak_factory_.GetWeakPtr()); | 181 weak_factory_.GetWeakPtr(), expected); |
112 } | 182 } |
113 | 183 |
114 BluetoothGattCharacteristic::NotifySessionCallback | 184 BluetoothGattCharacteristic::NotifySessionCallback |
115 BluetoothTestBase::GetNotifyCallback() { | 185 BluetoothTestBase::GetNotifyCallback(Call expected) { |
| 186 if (expected == Call::EXPECTED) |
| 187 ++expected_success_callback_calls_; |
116 return base::Bind(&BluetoothTestBase::NotifyCallback, | 188 return base::Bind(&BluetoothTestBase::NotifyCallback, |
117 weak_factory_.GetWeakPtr()); | 189 weak_factory_.GetWeakPtr(), expected); |
118 } | 190 } |
119 | 191 |
120 BluetoothGattCharacteristic::ValueCallback | 192 BluetoothGattCharacteristic::ValueCallback |
121 BluetoothTestBase::GetReadValueCallback() { | 193 BluetoothTestBase::GetReadValueCallback(Call expected) { |
| 194 if (expected == Call::EXPECTED) |
| 195 ++expected_success_callback_calls_; |
122 return base::Bind(&BluetoothTestBase::ReadValueCallback, | 196 return base::Bind(&BluetoothTestBase::ReadValueCallback, |
123 weak_factory_.GetWeakPtr()); | 197 weak_factory_.GetWeakPtr(), expected); |
124 } | 198 } |
125 | 199 |
126 BluetoothAdapter::ErrorCallback BluetoothTestBase::GetErrorCallback() { | 200 BluetoothAdapter::ErrorCallback BluetoothTestBase::GetErrorCallback( |
| 201 Call expected) { |
| 202 if (expected == Call::EXPECTED) |
| 203 ++expected_error_callback_calls_; |
127 return base::Bind(&BluetoothTestBase::ErrorCallback, | 204 return base::Bind(&BluetoothTestBase::ErrorCallback, |
128 weak_factory_.GetWeakPtr()); | 205 weak_factory_.GetWeakPtr(), expected); |
129 } | 206 } |
130 | 207 |
131 BluetoothDevice::ConnectErrorCallback | 208 BluetoothDevice::ConnectErrorCallback |
132 BluetoothTestBase::GetConnectErrorCallback() { | 209 BluetoothTestBase::GetConnectErrorCallback(Call expected) { |
| 210 if (expected == Call::EXPECTED) |
| 211 ++expected_error_callback_calls_; |
133 return base::Bind(&BluetoothTestBase::ConnectErrorCallback, | 212 return base::Bind(&BluetoothTestBase::ConnectErrorCallback, |
134 weak_factory_.GetWeakPtr()); | 213 weak_factory_.GetWeakPtr(), expected); |
135 } | 214 } |
136 | 215 |
137 base::Callback<void(BluetoothGattService::GattErrorCode)> | 216 base::Callback<void(BluetoothGattService::GattErrorCode)> |
138 BluetoothTestBase::GetGattErrorCallback() { | 217 BluetoothTestBase::GetGattErrorCallback(Call expected) { |
| 218 if (expected == Call::EXPECTED) |
| 219 ++expected_error_callback_calls_; |
139 return base::Bind(&BluetoothTestBase::GattErrorCallback, | 220 return base::Bind(&BluetoothTestBase::GattErrorCallback, |
140 weak_factory_.GetWeakPtr()); | 221 weak_factory_.GetWeakPtr(), expected); |
141 } | 222 } |
142 | 223 |
143 void BluetoothTestBase::ResetEventCounts() { | 224 void BluetoothTestBase::ResetEventCounts() { |
144 last_connect_error_code_ = BluetoothDevice::ERROR_UNKNOWN; | 225 last_connect_error_code_ = BluetoothDevice::ERROR_UNKNOWN; |
145 callback_count_ = 0; | 226 callback_count_ = 0; |
146 error_callback_count_ = 0; | 227 error_callback_count_ = 0; |
147 gatt_connection_attempts_ = 0; | 228 gatt_connection_attempts_ = 0; |
148 gatt_disconnection_attempts_ = 0; | 229 gatt_disconnection_attempts_ = 0; |
149 gatt_discovery_attempts_ = 0; | 230 gatt_discovery_attempts_ = 0; |
150 gatt_notify_characteristic_attempts_ = 0; | 231 gatt_notify_characteristic_attempts_ = 0; |
151 gatt_read_characteristic_attempts_ = 0; | 232 gatt_read_characteristic_attempts_ = 0; |
152 gatt_write_characteristic_attempts_ = 0; | 233 gatt_write_characteristic_attempts_ = 0; |
153 } | 234 } |
154 | 235 |
155 } // namespace device | 236 } // namespace device |
OLD | NEW |