bluetooth_advertisement_unittest.cc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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
  3. // found in the LICENSE file.
  4. #include "device/bluetooth/bluetooth_advertisement.h"
  5. #include <stdint.h>
  6. #include <memory>
  7. #include "base/memory/ptr_util.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace device {
  10. namespace {
  11. TEST(BluetoothAdvertisementTest, DataMembersAreAssignedCorrectly) {
  12. // Sample manufacturer data.
  13. BluetoothAdvertisement::ManufacturerData manufacturer_data;
  14. std::vector<uint8_t> sample_data(5, 0);
  15. manufacturer_data[0] = std::vector<uint8_t>(5, 0);
  16. // Sample UUID List.
  17. const BluetoothAdvertisement::UUIDList uuids(1, "1234");
  18. // Sample Service Data.
  19. BluetoothAdvertisement::ServiceData service_data;
  20. service_data["1234"] = std::vector<uint8_t>(5, 0);
  21. // Sample Scan Response Data.
  22. BluetoothAdvertisement::ScanResponseData scan_response_data;
  23. scan_response_data[0x16] = std::vector<uint8_t>(5, 0);
  24. BluetoothAdvertisement::Data data(
  25. BluetoothAdvertisement::ADVERTISEMENT_TYPE_BROADCAST);
  26. ASSERT_EQ(data.type(), BluetoothAdvertisement::ADVERTISEMENT_TYPE_BROADCAST);
  27. // Try without assiging Service UUID.
  28. ASSERT_FALSE(data.service_uuids().get());
  29. // Assign Service UUID.
  30. data.set_service_uuids(
  31. std::make_unique<BluetoothAdvertisement::UUIDList>(uuids));
  32. // Retrieve Service UUID.
  33. ASSERT_EQ(*data.service_uuids(), uuids);
  34. // Retrieve again.
  35. ASSERT_FALSE(data.service_uuids().get());
  36. // Try without assigning Manufacturer Data.
  37. ASSERT_FALSE(data.manufacturer_data().get());
  38. // Assign Manufacturer Data.
  39. data.set_manufacturer_data(
  40. std::make_unique<BluetoothAdvertisement::ManufacturerData>(
  41. manufacturer_data));
  42. // Retrieve Manufacturer Data.
  43. ASSERT_EQ(*data.manufacturer_data(), manufacturer_data);
  44. // Retrieve again.
  45. ASSERT_FALSE(data.manufacturer_data().get());
  46. // Try without assigning Solicit UUIDs.
  47. ASSERT_FALSE(data.solicit_uuids().get());
  48. // Assign Solicit UUIDs.
  49. data.set_solicit_uuids(
  50. std::make_unique<BluetoothAdvertisement::UUIDList>(uuids));
  51. // Retrieve Solicit UUIDs.
  52. ASSERT_EQ(*data.solicit_uuids(), uuids);
  53. // Retieve again.
  54. ASSERT_FALSE(data.solicit_uuids().get());
  55. // Try without assigning Service Data.
  56. ASSERT_FALSE(data.service_data().get());
  57. // Assign Service Data.
  58. data.set_service_data(
  59. std::make_unique<BluetoothAdvertisement::ServiceData>(service_data));
  60. // Retrieve Service Data.
  61. ASSERT_EQ(*data.service_data(), service_data);
  62. // Retrieve again.
  63. ASSERT_FALSE(data.service_data().get());
  64. // Try without assigning Scan Response Data.
  65. ASSERT_FALSE(data.scan_response_data().get());
  66. // Assign Service Data.
  67. data.set_scan_response_data(
  68. std::make_unique<BluetoothAdvertisement::ScanResponseData>(
  69. scan_response_data));
  70. // Retrieve Service Data.
  71. ASSERT_EQ(*data.scan_response_data(), scan_response_data);
  72. // Retrieve again.
  73. ASSERT_FALSE(data.scan_response_data().get());
  74. }
  75. } // namespace
  76. } // namespace device