Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: device/bluetooth/bluetooth_gatt_chromeos_unittest.cc

Issue 619973002: Add unit testing for more org.bluez.Error.* (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: deconstruction Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chromeos/dbus/fake_bluetooth_gatt_characteristic_client.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "base/memory/scoped_vector.h" 5 #include "base/memory/scoped_vector.h"
6 #include "base/message_loop/message_loop.h" 6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "chromeos/dbus/dbus_thread_manager.h" 8 #include "chromeos/dbus/dbus_thread_manager.h"
9 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" 9 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
10 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h" 10 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 FakeBluetoothGattCharacteristicClient* 391 FakeBluetoothGattCharacteristicClient*
392 fake_bluetooth_gatt_characteristic_client_; 392 fake_bluetooth_gatt_characteristic_client_;
393 FakeBluetoothGattDescriptorClient* fake_bluetooth_gatt_descriptor_client_; 393 FakeBluetoothGattDescriptorClient* fake_bluetooth_gatt_descriptor_client_;
394 scoped_ptr<device::BluetoothGattConnection> gatt_conn_; 394 scoped_ptr<device::BluetoothGattConnection> gatt_conn_;
395 ScopedVector<BluetoothGattNotifySession> update_sessions_; 395 ScopedVector<BluetoothGattNotifySession> update_sessions_;
396 scoped_refptr<BluetoothAdapter> adapter_; 396 scoped_refptr<BluetoothAdapter> adapter_;
397 397
398 int success_callback_count_; 398 int success_callback_count_;
399 int error_callback_count_; 399 int error_callback_count_;
400 std::vector<uint8> last_read_value_; 400 std::vector<uint8> last_read_value_;
401 enum BluetoothGattService::GattErrorCode last_service_error_; 401 BluetoothGattService::GattErrorCode last_service_error_;
402 }; 402 };
403 403
404 TEST_F(BluetoothGattChromeOSTest, GattConnection) { 404 TEST_F(BluetoothGattChromeOSTest, GattConnection) {
405 fake_bluetooth_device_client_->CreateDevice( 405 fake_bluetooth_device_client_->CreateDevice(
406 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 406 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
407 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 407 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
408 BluetoothDevice* device = adapter_->GetDevice( 408 BluetoothDevice* device = adapter_->GetDevice(
409 FakeBluetoothDeviceClient::kLowEnergyAddress); 409 FakeBluetoothDeviceClient::kLowEnergyAddress);
410 ASSERT_TRUE(device); 410 ASSERT_TRUE(device);
411 ASSERT_FALSE(device->IsConnected()); 411 ASSERT_FALSE(device->IsConnected());
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after
845 characteristic->WriteRemoteCharacteristic( 845 characteristic->WriteRemoteCharacteristic(
846 write_value, 846 write_value,
847 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, 847 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
848 base::Unretained(this)), 848 base::Unretained(this)),
849 base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback, 849 base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback,
850 base::Unretained(this))); 850 base::Unretained(this)));
851 EXPECT_TRUE(observer.last_gatt_characteristic_id_.empty()); 851 EXPECT_TRUE(observer.last_gatt_characteristic_id_.empty());
852 EXPECT_FALSE(observer.last_gatt_characteristic_uuid_.IsValid()); 852 EXPECT_FALSE(observer.last_gatt_characteristic_uuid_.IsValid());
853 EXPECT_EQ(0, success_callback_count_); 853 EXPECT_EQ(0, success_callback_count_);
854 EXPECT_EQ(1, error_callback_count_); 854 EXPECT_EQ(1, error_callback_count_);
855 EXPECT_EQ(BluetoothGattService::GattErrorCode::GATT_ERROR_NOT_PERMITTED, 855 EXPECT_EQ(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED,
856 last_service_error_); 856 last_service_error_);
857 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); 857 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_);
858 858
859 characteristic = service->GetCharacteristic( 859 characteristic = service->GetCharacteristic(
860 fake_bluetooth_gatt_characteristic_client_-> 860 fake_bluetooth_gatt_characteristic_client_->
861 GetBodySensorLocationPath().value()); 861 GetBodySensorLocationPath().value());
862 ASSERT_TRUE(characteristic); 862 ASSERT_TRUE(characteristic);
863 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_-> 863 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
864 GetBodySensorLocationPath().value(), 864 GetBodySensorLocationPath().value(),
865 characteristic->GetIdentifier()); 865 characteristic->GetIdentifier());
866 EXPECT_EQ(kBodySensorLocationUUID, characteristic->GetUUID()); 866 EXPECT_EQ(kBodySensorLocationUUID, characteristic->GetUUID());
867 characteristic->WriteRemoteCharacteristic( 867 characteristic->WriteRemoteCharacteristic(
868 write_value, 868 write_value,
869 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, 869 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
870 base::Unretained(this)), 870 base::Unretained(this)),
871 base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback, 871 base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback,
872 base::Unretained(this))); 872 base::Unretained(this)));
873 EXPECT_TRUE(observer.last_gatt_characteristic_id_.empty()); 873 EXPECT_TRUE(observer.last_gatt_characteristic_id_.empty());
874 EXPECT_FALSE(observer.last_gatt_characteristic_uuid_.IsValid()); 874 EXPECT_FALSE(observer.last_gatt_characteristic_uuid_.IsValid());
875 EXPECT_EQ(0, success_callback_count_); 875 EXPECT_EQ(0, success_callback_count_);
876 EXPECT_EQ(2, error_callback_count_); 876 EXPECT_EQ(2, error_callback_count_);
877 EXPECT_EQ(BluetoothGattService::GattErrorCode::GATT_ERROR_NOT_PERMITTED, 877 EXPECT_EQ(BluetoothGattService::GATT_ERROR_NOT_PERMITTED,
878 last_service_error_); 878 last_service_error_);
879 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); 879 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_);
880 880
881 // Issue write request to writeable characteristic. The "Body Sensor Location" 881 // Issue write request to writeable characteristic. The "Body Sensor Location"
882 // characteristic does not send notifications and WriteValue does not result 882 // characteristic does not send notifications and WriteValue does not result
883 // in a CharacteristicValueChanged event, thus no such event should be 883 // in a CharacteristicValueChanged event, thus no such event should be
884 // received. 884 // received.
885 characteristic = service->GetCharacteristic( 885 characteristic = service->GetCharacteristic(
886 fake_bluetooth_gatt_characteristic_client_-> 886 fake_bluetooth_gatt_characteristic_client_->
887 GetHeartRateControlPointPath().value()); 887 GetHeartRateControlPointPath().value());
(...skipping 21 matching lines...) Expand all
909 invalid_write_length.push_back(0x01); 909 invalid_write_length.push_back(0x01);
910 invalid_write_length.push_back(0x00); 910 invalid_write_length.push_back(0x00);
911 characteristic->WriteRemoteCharacteristic( 911 characteristic->WriteRemoteCharacteristic(
912 invalid_write_length, 912 invalid_write_length,
913 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, 913 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
914 base::Unretained(this)), 914 base::Unretained(this)),
915 base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback, 915 base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback,
916 base::Unretained(this))); 916 base::Unretained(this)));
917 EXPECT_EQ(1, success_callback_count_); 917 EXPECT_EQ(1, success_callback_count_);
918 EXPECT_EQ(3, error_callback_count_); 918 EXPECT_EQ(3, error_callback_count_);
919 EXPECT_EQ(BluetoothGattService::GattErrorCode::GATT_ERROR_INVALID_LENGTH, 919 EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH,
920 last_service_error_); 920 last_service_error_);
921 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); 921 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_);
922 922
923 std::vector<uint8> invalid_write_value; 923 std::vector<uint8> invalid_write_value;
924 invalid_write_value.push_back(0x02); 924 invalid_write_value.push_back(0x02);
925 characteristic->WriteRemoteCharacteristic( 925 characteristic->WriteRemoteCharacteristic(
926 invalid_write_value, 926 invalid_write_value,
927 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, 927 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
928 base::Unretained(this)), 928 base::Unretained(this)),
929 base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback, 929 base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback,
930 base::Unretained(this))); 930 base::Unretained(this)));
931 EXPECT_EQ(1, success_callback_count_); 931 EXPECT_EQ(1, success_callback_count_);
932 EXPECT_EQ(4, error_callback_count_); 932 EXPECT_EQ(4, error_callback_count_);
933 EXPECT_EQ(BluetoothGattService::GattErrorCode::GATT_ERROR_FAILED, 933 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_service_error_);
934 last_service_error_);
935 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); 934 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_);
936 935
937 // Issue a read request. A successful read results in a 936 // Issue a read request. A successful read results in a
938 // CharacteristicValueChanged notification. 937 // CharacteristicValueChanged notification.
939 characteristic = service->GetCharacteristic( 938 characteristic = service->GetCharacteristic(
940 fake_bluetooth_gatt_characteristic_client_-> 939 fake_bluetooth_gatt_characteristic_client_->
941 GetBodySensorLocationPath().value()); 940 GetBodySensorLocationPath().value());
942 ASSERT_TRUE(characteristic); 941 ASSERT_TRUE(characteristic);
943 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_-> 942 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
944 GetBodySensorLocationPath().value(), 943 GetBodySensorLocationPath().value(),
945 characteristic->GetIdentifier()); 944 characteristic->GetIdentifier());
946 EXPECT_EQ(kBodySensorLocationUUID, characteristic->GetUUID()); 945 EXPECT_EQ(kBodySensorLocationUUID, characteristic->GetUUID());
947 characteristic->ReadRemoteCharacteristic( 946 characteristic->ReadRemoteCharacteristic(
948 base::Bind(&BluetoothGattChromeOSTest::ValueCallback, 947 base::Bind(&BluetoothGattChromeOSTest::ValueCallback,
949 base::Unretained(this)), 948 base::Unretained(this)),
950 base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback, 949 base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback,
951 base::Unretained(this))); 950 base::Unretained(this)));
952 EXPECT_EQ(2, success_callback_count_); 951 EXPECT_EQ(2, success_callback_count_);
953 EXPECT_EQ(4, error_callback_count_); 952 EXPECT_EQ(4, error_callback_count_);
954 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_); 953 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_);
955 EXPECT_TRUE(ValuesEqual(characteristic->GetValue(), last_read_value_)); 954 EXPECT_TRUE(ValuesEqual(characteristic->GetValue(), last_read_value_));
955
956 // Test long-running actions.
957 fake_bluetooth_gatt_characteristic_client_->SetExtraProcessing(1);
958 characteristic = service->GetCharacteristic(
959 fake_bluetooth_gatt_characteristic_client_->GetBodySensorLocationPath()
960 .value());
961 ASSERT_TRUE(characteristic);
962 EXPECT_EQ(
963 fake_bluetooth_gatt_characteristic_client_->GetBodySensorLocationPath()
964 .value(),
965 characteristic->GetIdentifier());
966 EXPECT_EQ(kBodySensorLocationUUID, characteristic->GetUUID());
967 characteristic->ReadRemoteCharacteristic(
968 base::Bind(&BluetoothGattChromeOSTest::ValueCallback,
969 base::Unretained(this)),
970 base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback,
971 base::Unretained(this)));
972
973 // Callback counts shouldn't change, this one will be delayed until after
974 // tne next one.
975 EXPECT_EQ(2, success_callback_count_);
976 EXPECT_EQ(4, error_callback_count_);
977 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_);
978
979 // Next read should error because IN_PROGRESS
980 characteristic->ReadRemoteCharacteristic(
981 base::Bind(&BluetoothGattChromeOSTest::ValueCallback,
982 base::Unretained(this)),
983 base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback,
984 base::Unretained(this)));
985 EXPECT_EQ(5, error_callback_count_);
986 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, last_service_error_);
987
988 // But previous call finished.
989 EXPECT_EQ(3, success_callback_count_);
990 EXPECT_EQ(2, observer.gatt_characteristic_value_changed_count_);
991 EXPECT_TRUE(ValuesEqual(characteristic->GetValue(), last_read_value_));
992 fake_bluetooth_gatt_characteristic_client_->SetExtraProcessing(0);
993
994 // Test unauthorized actions.
995 fake_bluetooth_gatt_characteristic_client_->SetAuthorized(false);
996 characteristic->ReadRemoteCharacteristic(
997 base::Bind(&BluetoothGattChromeOSTest::ValueCallback,
998 base::Unretained(this)),
999 base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback,
1000 base::Unretained(this)));
1001 EXPECT_EQ(3, success_callback_count_);
1002 EXPECT_EQ(6, error_callback_count_);
1003 EXPECT_EQ(BluetoothGattService::GATT_ERROR_NOT_AUTHORIZED,
1004 last_service_error_);
1005 EXPECT_EQ(2, observer.gatt_characteristic_value_changed_count_);
1006 fake_bluetooth_gatt_characteristic_client_->SetAuthorized(true);
1007
1008 // Test unauthenticated / needs login.
1009 fake_bluetooth_gatt_characteristic_client_->SetAuthenticated(false);
1010 characteristic->ReadRemoteCharacteristic(
1011 base::Bind(&BluetoothGattChromeOSTest::ValueCallback,
1012 base::Unretained(this)),
1013 base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback,
1014 base::Unretained(this)));
1015 EXPECT_EQ(3, success_callback_count_);
1016 EXPECT_EQ(7, error_callback_count_);
1017 EXPECT_EQ(BluetoothGattService::GATT_ERROR_NOT_PAIRED, last_service_error_);
1018 EXPECT_EQ(2, observer.gatt_characteristic_value_changed_count_);
1019 fake_bluetooth_gatt_characteristic_client_->SetAuthenticated(true);
956 } 1020 }
957 1021
958 TEST_F(BluetoothGattChromeOSTest, GattCharacteristicProperties) { 1022 TEST_F(BluetoothGattChromeOSTest, GattCharacteristicProperties) {
959 fake_bluetooth_device_client_->CreateDevice( 1023 fake_bluetooth_device_client_->CreateDevice(
960 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 1024 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
961 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 1025 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
962 BluetoothDevice* device = adapter_->GetDevice( 1026 BluetoothDevice* device = adapter_->GetDevice(
963 FakeBluetoothDeviceClient::kLowEnergyAddress); 1027 FakeBluetoothDeviceClient::kLowEnergyAddress);
964 ASSERT_TRUE(device); 1028 ASSERT_TRUE(device);
965 1029
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
1067 // Write value. Writes to this descriptor will fail. 1131 // Write value. Writes to this descriptor will fail.
1068 desc_value[0] = 0x03; 1132 desc_value[0] = 0x03;
1069 descriptor->WriteRemoteDescriptor( 1133 descriptor->WriteRemoteDescriptor(
1070 desc_value, 1134 desc_value,
1071 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, 1135 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
1072 base::Unretained(this)), 1136 base::Unretained(this)),
1073 base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback, 1137 base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback,
1074 base::Unretained(this))); 1138 base::Unretained(this)));
1075 EXPECT_EQ(1, success_callback_count_); 1139 EXPECT_EQ(1, success_callback_count_);
1076 EXPECT_EQ(1, error_callback_count_); 1140 EXPECT_EQ(1, error_callback_count_);
1077 EXPECT_EQ(BluetoothGattService::GattErrorCode::GATT_ERROR_NOT_PERMITTED, 1141 EXPECT_EQ(BluetoothGattService::GATT_ERROR_NOT_PERMITTED,
1078 last_service_error_); 1142 last_service_error_);
1079 EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue())); 1143 EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue()));
1080 EXPECT_FALSE(ValuesEqual(desc_value, descriptor->GetValue())); 1144 EXPECT_FALSE(ValuesEqual(desc_value, descriptor->GetValue()));
1081 EXPECT_EQ(0, observer.gatt_service_changed_count_); 1145 EXPECT_EQ(0, observer.gatt_service_changed_count_);
1082 EXPECT_EQ(1, observer.gatt_descriptor_value_changed_count_); 1146 EXPECT_EQ(1, observer.gatt_descriptor_value_changed_count_);
1083 1147
1084 // Read new value. 1148 // Read new value.
1085 descriptor->ReadRemoteDescriptor( 1149 descriptor->ReadRemoteDescriptor(
1086 base::Bind(&BluetoothGattChromeOSTest::ValueCallback, 1150 base::Bind(&BluetoothGattChromeOSTest::ValueCallback,
1087 base::Unretained(this)), 1151 base::Unretained(this)),
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
1355 1419
1356 EXPECT_EQ(1, success_callback_count_); 1420 EXPECT_EQ(1, success_callback_count_);
1357 EXPECT_EQ(0, error_callback_count_); 1421 EXPECT_EQ(0, error_callback_count_);
1358 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_); 1422 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_);
1359 EXPECT_TRUE(characteristic->IsNotifying()); 1423 EXPECT_TRUE(characteristic->IsNotifying());
1360 EXPECT_EQ(1U, update_sessions_.size()); 1424 EXPECT_EQ(1U, update_sessions_.size());
1361 EXPECT_TRUE(update_sessions_[0]->IsActive()); 1425 EXPECT_TRUE(update_sessions_[0]->IsActive());
1362 } 1426 }
1363 1427
1364 } // namespace chromeos 1428 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/fake_bluetooth_gatt_characteristic_client.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698