Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 995ebbe7 authored by Henri Chataing's avatar Henri Chataing Committed by Gerrit Code Review
Browse files

Merge "net_test_avrcp: fix unit tests" into main

parents 61430f6d 15e529ad
Loading
Loading
Loading
Loading
+106 −82
Original line number Diff line number Diff line
@@ -15,9 +15,12 @@
 */

#include <base/functional/bind.h>
#include <com_android_bluetooth_flags.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <memory>

#include "avrcp_internal.h"
#include "avrcp_test_helper.h"
#include "connection_handler.h"
@@ -41,11 +44,15 @@ bool btif_av_both_enable(void) { return false; }
namespace bluetooth {
namespace avrcp {

using device_ptr = std::shared_ptr<Device>;

class AvrcpConnectionHandlerTest : public testing::Test {
public:
  void SetUp() override { ON_CALL(mock_avrcp_, Close(_)).WillByDefault(Return(0)); }
  void SetUp() override {
    bound_connection_cb_ = base::BindRepeating(
            &AvrcpConnectionHandlerTest::DeviceConnectionCallback, weak_factory_.GetWeakPtr());
    ON_CALL(mock_avrcp_, Close(_)).WillByDefault(Return(0));
  }

  void TearDown() override { current_device_.reset(); }

  void SetUpSdp(tAVRC_FIND_CBACK* sdp_cb, bool browsing, bool absolute_volume) {
    EXPECT_CALL(mock_avrcp_, FindService(_, _, _, _))
@@ -82,16 +89,28 @@ public:
            .WillRepeatedly(DoAll(SetArgPointee<2>(AVRC_REV_1_6), Return(true)));
  }

  void DeviceConnectionCallback(std::shared_ptr<Device> new_device) {
    ASSERT_TRUE(new_device != nullptr);
    new_device->RegisterInterfaces(&mock_media_, &mock_a2dp_, &mock_volume_,
                                   &mock_player_settings_);
    current_device_ = new_device;
  }

protected:
  ConnectionHandler* connection_handler_ = nullptr;
  std::shared_ptr<Device> current_device_ = nullptr;
  base::RepeatingCallback<void(std::shared_ptr<Device>)> bound_connection_cb_;

  // We use NiceMock's here because each function of this code does quite a few
  // operations. This way it is much easier to write a higher number of smaller
  // tests without having a large amount of warnings.
  NiceMock<MockFunction<void(device_ptr)>> device_cb;
  NiceMock<MockAvrcpInterface> mock_avrcp_;
  NiceMock<MockSdpInterface> mock_sdp_;
  NiceMock<MockVolumeInterface> mock_volume_;
  NiceMock<MockMediaInterface> mock_media_;
  NiceMock<MockA2dpInterface> mock_a2dp_;
  NiceMock<MockPlayerSettingsInterface> mock_player_settings_;
  base::WeakPtrFactory<AvrcpConnectionHandlerTest> weak_factory_{this};
};

TEST_F(AvrcpConnectionHandlerTest, initializeTest) {
@@ -102,10 +121,9 @@ TEST_F(AvrcpConnectionHandlerTest, initializeTest) {
          .Times(1)
          .WillOnce(DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)));

  auto bound_callback =
          base::BindRepeating(&MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
  ASSERT_TRUE(
          ConnectionHandler::Initialize(bound_callback, &mock_avrcp_, &mock_sdp_, &mock_volume_));
  // Initialize the interface
  ASSERT_TRUE(ConnectionHandler::Initialize(bound_connection_cb_, &mock_avrcp_, &mock_sdp_,
                                            &mock_volume_));
  connection_handler_ = ConnectionHandler::Get();

  // Check that the callback was sent with us as the acceptor
@@ -125,10 +143,8 @@ TEST_F(AvrcpConnectionHandlerTest, notConnectedDisconnectTest) {
          .WillOnce(DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)));

  // Initialize the interface
  auto bound_callback =
          base::BindRepeating(&MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
  ASSERT_TRUE(
          ConnectionHandler::Initialize(bound_callback, &mock_avrcp_, &mock_sdp_, &mock_volume_));
  ASSERT_TRUE(ConnectionHandler::Initialize(bound_connection_cb_, &mock_avrcp_, &mock_sdp_,
                                            &mock_volume_));
  connection_handler_ = ConnectionHandler::Get();

  // Call the callback with a message saying the connection has closed
@@ -148,10 +164,8 @@ TEST_F(AvrcpConnectionHandlerTest, disconnectAfterCleanupTest) {
          .WillOnce(DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)));

  // Initialize the interface
  auto bound_callback =
          base::BindRepeating(&MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
  ASSERT_TRUE(
          ConnectionHandler::Initialize(bound_callback, &mock_avrcp_, &mock_sdp_, &mock_volume_));
  ASSERT_TRUE(ConnectionHandler::Initialize(bound_connection_cb_, &mock_avrcp_, &mock_sdp_,
                                            &mock_volume_));
  connection_handler_ = ConnectionHandler::Get();

  connection_handler_ = nullptr;
@@ -175,10 +189,8 @@ TEST_F(AvrcpConnectionHandlerTest, remoteDeviceConnectionTest) {
          .WillOnce(DoAll(SetArgPointee<0>(2), SaveArgPointee<1>(&conn_cb), Return(0)));

  // Initialize the interface
  auto bound_callback =
          base::BindRepeating(&MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
  ASSERT_TRUE(
          ConnectionHandler::Initialize(bound_callback, &mock_avrcp_, &mock_sdp_, &mock_volume_));
  ASSERT_TRUE(ConnectionHandler::Initialize(bound_connection_cb_, &mock_avrcp_, &mock_sdp_,
                                            &mock_volume_));
  connection_handler_ = ConnectionHandler::Get();

  // Check that the callback was sent with us as the acceptor
@@ -188,19 +200,24 @@ TEST_F(AvrcpConnectionHandlerTest, remoteDeviceConnectionTest) {
  tAVRC_FIND_CBACK sdp_cb;
  SetUpSdp(&sdp_cb, false, false);

  // Set an expectation that a device will be created
  EXPECT_CALL(device_cb, Call(_)).Times(1);

  // Set an Expectation that OpenBrowse will be called in acceptor mode when the
  // device connects.
  EXPECT_CALL(mock_avrcp_, OpenBrowse(1, AVCT_ACP)).Times(1);

  if (com::android::bluetooth::flags::avrcp_connect_a2dp_delayed()) {
    // Set an expectation that SDP for audio will be performed
    EXPECT_CALL(mock_a2dp_, find_audio_sink_service(_, _)).Times(1);
  }

  // Call the callback with a message saying that a remote device has connected
  conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kAny);

  // Run the SDP callback with status success
  sdp_cb.Run(0);

  // Check that a device was created
  ASSERT_TRUE(current_device_ != nullptr);

  connection_handler_ = nullptr;
  ConnectionHandler::CleanUp();
}
@@ -219,16 +236,19 @@ TEST_F(AvrcpConnectionHandlerTest, noAbsoluteVolumeTest) {
          .WillOnce(DoAll(SetArgPointee<0>(2), SaveArgPointee<1>(&conn_cb), Return(0)));

  // Initialize the interface
  auto bound_callback =
          base::BindRepeating(&MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
  ASSERT_TRUE(
          ConnectionHandler::Initialize(bound_callback, &mock_avrcp_, &mock_sdp_, &mock_volume_));
  ASSERT_TRUE(ConnectionHandler::Initialize(bound_connection_cb_, &mock_avrcp_, &mock_sdp_,
                                            &mock_volume_));
  connection_handler_ = ConnectionHandler::Get();

  // Set an Expectations that SDP will be performed
  tAVRC_FIND_CBACK sdp_cb;
  SetUpSdp(&sdp_cb, false, false);

  if (com::android::bluetooth::flags::avrcp_connect_a2dp_delayed()) {
    // Set an expectation that SDP for audio will be performed
    EXPECT_CALL(mock_a2dp_, find_audio_sink_service(_, _)).Times(1);
  }

  EXPECT_CALL(mock_volume_, DeviceConnected(RawAddress::kAny)).Times(1);

  // Call the callback with a message saying that a remote device has connected
@@ -237,6 +257,9 @@ TEST_F(AvrcpConnectionHandlerTest, noAbsoluteVolumeTest) {
  // Run the SDP callback with status success
  sdp_cb.Run(0);

  // Check that a device was created
  ASSERT_TRUE(current_device_ != nullptr);

  connection_handler_ = nullptr;
  ConnectionHandler::CleanUp();
}
@@ -255,12 +278,9 @@ TEST_F(AvrcpConnectionHandlerTest, absoluteVolumeTest) {
          .WillOnce(DoAll(SetArgPointee<0>(2), SaveArgPointee<1>(&conn_cb), Return(0)));

  // Initialize the interface
  auto bound_callback =
          base::BindRepeating(&MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));

  StrictMock<MockVolumeInterface> strict_volume;
  ASSERT_TRUE(
          ConnectionHandler::Initialize(bound_callback, &mock_avrcp_, &mock_sdp_, &strict_volume));
  ASSERT_TRUE(ConnectionHandler::Initialize(bound_connection_cb_, &mock_avrcp_, &mock_sdp_,
                                            &strict_volume));
  connection_handler_ = ConnectionHandler::Get();

  // Set an Expectations that SDP will be performed with absolute volume
@@ -268,12 +288,20 @@ TEST_F(AvrcpConnectionHandlerTest, absoluteVolumeTest) {
  tAVRC_FIND_CBACK sdp_cb;
  SetUpSdp(&sdp_cb, false, true);

  if (com::android::bluetooth::flags::avrcp_connect_a2dp_delayed()) {
    // Set an expectation that SDP for audio will be performed
    EXPECT_CALL(mock_a2dp_, find_audio_sink_service(_, _)).Times(1);
  }

  // Call the callback with a message saying that a remote device has connected
  conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kAny);

  // Run the SDP callback with status success
  sdp_cb.Run(0);

  // Check that a device was created
  ASSERT_TRUE(current_device_ != nullptr);

  connection_handler_ = nullptr;
  ConnectionHandler::CleanUp();
}
@@ -288,15 +316,21 @@ TEST_F(AvrcpConnectionHandlerTest, disconnectTest) {
          .WillOnce(DoAll(SetArgPointee<0>(2), Return(0)));

  // Initialize the interface
  auto bound_callback =
          base::BindRepeating(&MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
  ASSERT_TRUE(
          ConnectionHandler::Initialize(bound_callback, &mock_avrcp_, &mock_sdp_, &mock_volume_));
  ASSERT_TRUE(ConnectionHandler::Initialize(bound_connection_cb_, &mock_avrcp_, &mock_sdp_,
                                            &mock_volume_));
  connection_handler_ = ConnectionHandler::Get();

  if (com::android::bluetooth::flags::avrcp_connect_a2dp_delayed()) {
    // Set an expectation that SDP for audio will be performed
    EXPECT_CALL(mock_a2dp_, find_audio_sink_service(_, _)).Times(1);
  }

  // Call the callback with a message saying that a remote device has connected
  conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kAny);

  // Check that a device was created
  ASSERT_TRUE(current_device_ != nullptr);

  // Set up the expectation that Close will be called
  EXPECT_CALL(mock_avrcp_, Close(1)).Times(1);

@@ -323,10 +357,8 @@ TEST_F(AvrcpConnectionHandlerTest, multipleRemoteDeviceConnectionTest) {
          .WillOnce(DoAll(SetArgPointee<0>(3), SaveArgPointee<1>(&conn_cb), Return(0)));

  // Initialize the interface
  auto bound_callback =
          base::BindRepeating(&MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
  ASSERT_TRUE(
          ConnectionHandler::Initialize(bound_callback, &mock_avrcp_, &mock_sdp_, &mock_volume_));
  ASSERT_TRUE(ConnectionHandler::Initialize(bound_connection_cb_, &mock_avrcp_, &mock_sdp_,
                                            &mock_volume_));
  connection_handler_ = ConnectionHandler::Get();

  // Check that the callback was sent with us as the acceptor
@@ -336,9 +368,6 @@ TEST_F(AvrcpConnectionHandlerTest, multipleRemoteDeviceConnectionTest) {
  tAVRC_FIND_CBACK sdp_cb;
  SetUpSdp(&sdp_cb, false, false);

  // Set an expectation that a device will be created
  EXPECT_CALL(device_cb, Call(_)).Times(1);

  // Set an Expectation that OpenBrowse will be called in acceptor mode when the
  // device connects on handle 1
  EXPECT_CALL(mock_avrcp_, OpenBrowse(1, AVCT_ACP)).Times(1);
@@ -346,15 +375,16 @@ TEST_F(AvrcpConnectionHandlerTest, multipleRemoteDeviceConnectionTest) {
  // Call the callback with a message saying that a remote device has connected
  conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kAny);

  // Check that a first device was created
  ASSERT_TRUE(current_device_ != nullptr);
  current_device_.reset();

  // Run the SDP callback with status success
  sdp_cb.Run(0);

  // Set an Expectations that SDP will be performed again
  SetUpSdp(&sdp_cb, false, false);

  // Set an expectation that a device will be created again
  EXPECT_CALL(device_cb, Call(_)).Times(1);

  // Set an Expectation that OpenBrowse will be called in acceptor mode when the
  // device connects on handle 2
  EXPECT_CALL(mock_avrcp_, OpenBrowse(2, AVCT_ACP)).Times(1);
@@ -363,6 +393,9 @@ TEST_F(AvrcpConnectionHandlerTest, multipleRemoteDeviceConnectionTest) {
  // with a different address
  conn_cb.ctrl_cback.Run(2, AVRC_OPEN_IND_EVT, 0, &RawAddress::kEmpty);

  // Check that a second device was created
  ASSERT_TRUE(current_device_ != nullptr);

  // Run the SDP callback with status success
  sdp_cb.Run(0);

@@ -379,10 +412,8 @@ TEST_F(AvrcpConnectionHandlerTest, cleanupTest) {
          .WillOnce(DoAll(SetArgPointee<0>(3), SaveArgPointee<1>(&conn_cb), Return(0)));

  // Initialize the interface
  auto bound_callback =
          base::BindRepeating(&MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
  ASSERT_TRUE(
          ConnectionHandler::Initialize(bound_callback, &mock_avrcp_, &mock_sdp_, &mock_volume_));
  ASSERT_TRUE(ConnectionHandler::Initialize(bound_connection_cb_, &mock_avrcp_, &mock_sdp_,
                                            &mock_volume_));
  connection_handler_ = ConnectionHandler::Get();

  // Call the callback twice with a message saying that a remote device has
@@ -401,10 +432,8 @@ TEST_F(AvrcpConnectionHandlerTest, cleanupTest) {

TEST_F(AvrcpConnectionHandlerTest, connectToRemoteDeviceTest) {
  // Initialize the interface
  auto bound_callback =
          base::BindRepeating(&MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
  ASSERT_TRUE(
          ConnectionHandler::Initialize(bound_callback, &mock_avrcp_, &mock_sdp_, &mock_volume_));
  ASSERT_TRUE(ConnectionHandler::Initialize(bound_connection_cb_, &mock_avrcp_, &mock_sdp_,
                                            &mock_volume_));
  connection_handler_ = ConnectionHandler::Get();

  // Set an Expectation that SDP will be performed
@@ -427,9 +456,6 @@ TEST_F(AvrcpConnectionHandlerTest, connectToRemoteDeviceTest) {
  // Check that the callback was sent with us as the initiator
  ASSERT_EQ(conn_cb.conn, 0);

  // Set an expectation that a device will be created
  EXPECT_CALL(device_cb, Call(_)).Times(1);

  // Set an Expectation that OpenBrowse will NOT be called since the SDP entry
  // didn't list browsing as a feature
  EXPECT_CALL(mock_avrcp_, OpenBrowse(_, _)).Times(0);
@@ -438,6 +464,9 @@ TEST_F(AvrcpConnectionHandlerTest, connectToRemoteDeviceTest) {
  // with a different address
  conn_cb.ctrl_cback.Run(2, AVRC_OPEN_IND_EVT, 0, &RawAddress::kEmpty);

  // Check that a device was created
  ASSERT_TRUE(current_device_ != nullptr);

  // Cleanup the object causing all open connections to be closed
  connection_handler_ = nullptr;
  ConnectionHandler::CleanUp();
@@ -445,10 +474,8 @@ TEST_F(AvrcpConnectionHandlerTest, connectToRemoteDeviceTest) {

TEST_F(AvrcpConnectionHandlerTest, connectToBrowsableRemoteDeviceTest) {
  // Initialize the interface
  auto bound_callback =
          base::BindRepeating(&MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
  ASSERT_TRUE(
          ConnectionHandler::Initialize(bound_callback, &mock_avrcp_, &mock_sdp_, &mock_volume_));
  ASSERT_TRUE(ConnectionHandler::Initialize(bound_connection_cb_, &mock_avrcp_, &mock_sdp_,
                                            &mock_volume_));
  connection_handler_ = ConnectionHandler::Get();

  // Set an Expectation that SDP will be performed
@@ -471,9 +498,6 @@ TEST_F(AvrcpConnectionHandlerTest, connectToBrowsableRemoteDeviceTest) {
  // Check that the callback was sent with us as the initiator
  ASSERT_EQ(conn_cb.conn, 0);

  // Set an expectation that a device will be created
  EXPECT_CALL(device_cb, Call(_)).Times(1);

  // Set an Expectation that OpenBrowse will be called since browsing is listed
  // as supported in SDP
  EXPECT_CALL(mock_avrcp_, OpenBrowse(1, AVCT_INT)).Times(1);
@@ -482,6 +506,9 @@ TEST_F(AvrcpConnectionHandlerTest, connectToBrowsableRemoteDeviceTest) {
  // with a different address
  conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kEmpty);

  // Check that a device was created
  ASSERT_TRUE(current_device_ != nullptr);

  // Cleanup the object causing all open connections to be closed
  connection_handler_ = nullptr;
  ConnectionHandler::CleanUp();
@@ -497,10 +524,8 @@ TEST_F(AvrcpConnectionHandlerTest, disconnectWhileDoingSdpTest) {
          .WillOnce(DoAll(SetArgPointee<0>(2), Return(0)));

  // Initialize the interface
  auto bound_callback =
          base::BindRepeating(&MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
  ASSERT_TRUE(
          ConnectionHandler::Initialize(bound_callback, &mock_avrcp_, &mock_sdp_, &mock_volume_));
  ASSERT_TRUE(ConnectionHandler::Initialize(bound_connection_cb_, &mock_avrcp_, &mock_sdp_,
                                            &mock_volume_));
  connection_handler_ = ConnectionHandler::Get();

  // Set an Expectation that SDP will be performed
@@ -510,6 +535,9 @@ TEST_F(AvrcpConnectionHandlerTest, disconnectWhileDoingSdpTest) {
  // Call the callback with a message saying that a remote device has connected
  conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kAny);

  // Check that a device was created
  ASSERT_TRUE(current_device_ != nullptr);

  // Call the callback with a message saying that a remote device has
  // disconnected
  conn_cb.ctrl_cback.Run(1, AVRC_CLOSE_IND_EVT, 0, &RawAddress::kAny);
@@ -533,10 +561,8 @@ TEST_F(AvrcpConnectionHandlerTest, connectionCollisionTest) {
          .WillOnce(DoAll(SetArgPointee<0>(2), SaveArgPointee<1>(&conn_cb), Return(0)));

  // Initialize the interface
  auto bound_callback =
          base::BindRepeating(&MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
  ASSERT_TRUE(
          ConnectionHandler::Initialize(bound_callback, &mock_avrcp_, &mock_sdp_, &mock_volume_));
  ASSERT_TRUE(ConnectionHandler::Initialize(bound_connection_cb_, &mock_avrcp_, &mock_sdp_,
                                            &mock_volume_));
  connection_handler_ = ConnectionHandler::Get();

  // Check that the callback was sent with us as the acceptor
@@ -548,9 +574,6 @@ TEST_F(AvrcpConnectionHandlerTest, connectionCollisionTest) {

  connection_handler_->ConnectDevice(RawAddress::kAny);

  // Set an expectation that a device will be created
  EXPECT_CALL(device_cb, Call(_)).Times(1);

  // Set an Expectations that SDP search will be performed again but will fail
  EXPECT_CALL(mock_avrcp_, FindService(_, _, _, _))
          .Times(1)
@@ -562,6 +585,9 @@ TEST_F(AvrcpConnectionHandlerTest, connectionCollisionTest) {
  // Call the callback with a message saying that a remote device has connected
  conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kAny);

  // Check that a device was created
  ASSERT_TRUE(current_device_ != nullptr);

  // Set an expectation that cleanup will close the last connection
  EXPECT_CALL(mock_avrcp_, Close(_));

@@ -586,18 +612,13 @@ TEST_F(AvrcpConnectionHandlerTest, acceptorSdpSearchFailTest) {
          .WillOnce(DoAll(SetArgPointee<0>(2), SaveArgPointee<1>(&conn_cb), Return(0)));

  // Initialize the interface
  auto bound_callback =
          base::BindRepeating(&MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
  ASSERT_TRUE(
          ConnectionHandler::Initialize(bound_callback, &mock_avrcp_, &mock_sdp_, &mock_volume_));
  ASSERT_TRUE(ConnectionHandler::Initialize(bound_connection_cb_, &mock_avrcp_, &mock_sdp_,
                                            &mock_volume_));
  connection_handler_ = ConnectionHandler::Get();

  // Check that the callback was sent with us as the acceptor
  ASSERT_EQ(conn_cb.conn, 1);

  // Set an expectation that a device will be created
  EXPECT_CALL(device_cb, Call(_)).Times(1);

  // Set an expectation that SDP search will be performed but will fail
  tAVRC_FIND_CBACK sdp_cb;
  EXPECT_CALL(mock_avrcp_, FindService(_, _, _, _))
@@ -610,6 +631,9 @@ TEST_F(AvrcpConnectionHandlerTest, acceptorSdpSearchFailTest) {
  // Call the callback with a message saying that a remote device has connected
  conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kAny);

  // Check that a device was created
  ASSERT_TRUE(current_device_ != nullptr);

  // Set an expectation that cleanup will close the last connection
  EXPECT_CALL(mock_avrcp_, Close(_));