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

Commit c38ca682 authored by Łukasz Rymanowski's avatar Łukasz Rymanowski
Browse files

has_client: Fix unit tests

This patch allows to use osi_property_set_bool correctly
for the test purposes.
Since  stub/osi.cc stubs also alarm_new/free, has unit tests
will make use of it.

This is patch is needed for the new testcase in following patch
Bug: 262309077
Test: atest bluetooth_has_test
Test: atest BluetoothInstrumentationTests
Tag: #feature

Merged-In: I79037935730b47dbb463fd5614f4e878d226e88b
Change-Id: I79037935730b47dbb463fd5614f4e878d226e88b
(cherry picked from commit d4058f84aa29617bf2471d4e280cd8478291c7c8)
parent ab3914dc
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -889,6 +889,7 @@ cc_test {
        "test/common/btm_api_mock.cc",
        "test/common/mock_controller.cc",
        "test/common/mock_csis_client.cc",
        ":TestStubOsi",
    ],
    shared_libs: [
        "libprotobuf-cpp-lite",
+14 −28
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <osi/include/alarm.h>
#include <osi/test/alarm_mock.h>
#include <sys/socket.h>

#include <variant>
@@ -39,19 +38,10 @@
#include "mock_controller.h"
#include "mock_csis_client.h"

static std::map<const char*, bool> fake_osi_bool_props;

bool osi_property_get_bool(const char* key, bool default_value) {
  if (fake_osi_bool_props.count(key)) return fake_osi_bool_props.at(key);

  return default_value;
}

void osi_property_set_bool(const char* key, bool value) {
  fake_osi_bool_props.insert_or_assign(key, value);
}

bool gatt_profile_get_eatt_support(const RawAddress& addr) { return true; }
void osi_property_set_bool(const char* key, bool value);

std::map<std::string, int> mock_function_count_map;

namespace bluetooth {
namespace has {
@@ -655,8 +645,7 @@ class HasClientTestBase : public ::testing::Test {
  }

  void SetUp(void) override {
    fake_osi_bool_props.clear();

    mock_function_count_map.clear();
    controller::SetMockControllerInterface(&controller_interface_);
    bluetooth::manager::SetMockBtmInterface(&btm_interface);
    bluetooth::storage::SetMockBtifStorageInterface(&btif_storage_interface_);
@@ -2939,7 +2928,7 @@ TEST_F(HasClientTest, test_dumpsys) {

class HasTypesTest : public ::testing::Test {
 protected:
  void SetUp(void) override { fake_osi_bool_props.clear(); }
  void SetUp(void) override { mock_function_count_map.clear(); }

  void TearDown(void) override {}
};  // namespace
@@ -3076,15 +3065,16 @@ TEST_F(HasTypesTest, test_group_op_coordinator_init) {
  auto address1 = GetTestAddress(1);
  auto address2 = GetTestAddress(2);

  EXPECT_CALL(*AlarmMock::Get(), AlarmNew(_)).Times(1);
  HasCtpGroupOpCoordinator wrapper(
      {address1, address2},
      HasCtpOp(0x01, ::le_audio::has::PresetCtpOpcode::READ_PRESETS, 6));
  ASSERT_EQ(2u, wrapper.ref_cnt);

  EXPECT_CALL(*AlarmMock::Get(), AlarmFree(_)).Times(1);
  HasCtpGroupOpCoordinator::Cleanup();
  ASSERT_EQ(0u, wrapper.ref_cnt);

  ASSERT_EQ(1, mock_function_count_map["alarm_free"]);
  ASSERT_EQ(1, mock_function_count_map["alarm_new"]);
}

TEST_F(HasTypesTest, test_group_op_coordinator_copy) {
@@ -3095,7 +3085,6 @@ TEST_F(HasTypesTest, test_group_op_coordinator_copy) {
  auto address1 = GetTestAddress(1);
  auto address2 = GetTestAddress(2);

  EXPECT_CALL(*AlarmMock::Get(), AlarmNew(_)).Times(1);
  HasCtpGroupOpCoordinator wrapper(
      {address1, address2},
      HasCtpOp(0x01, ::le_audio::has::PresetCtpOpcode::READ_PRESETS, 6));
@@ -3111,9 +3100,11 @@ TEST_F(HasTypesTest, test_group_op_coordinator_copy) {
  delete wrapper4;
  ASSERT_EQ(4u, wrapper.ref_cnt);

  EXPECT_CALL(*AlarmMock::Get(), AlarmFree(_)).Times(1);
  HasCtpGroupOpCoordinator::Cleanup();
  ASSERT_EQ(0u, wrapper.ref_cnt);

  ASSERT_EQ(1, mock_function_count_map["alarm_free"]);
  ASSERT_EQ(1, mock_function_count_map["alarm_new"]);
}

TEST_F(HasTypesTest, test_group_op_coordinator_completion) {
@@ -3126,7 +3117,6 @@ TEST_F(HasTypesTest, test_group_op_coordinator_completion) {
  auto address2 = GetTestAddress(2);
  auto address3 = GetTestAddress(3);

  EXPECT_CALL(*AlarmMock::Get(), AlarmNew(_)).Times(1);
  HasCtpGroupOpCoordinator wrapper(
      {address1, address3},
      HasCtpOp(0x01, ::le_audio::has::PresetCtpOpcode::READ_PRESETS, 6));
@@ -3135,7 +3125,6 @@ TEST_F(HasTypesTest, test_group_op_coordinator_completion) {
      HasCtpOp(0x01, ::le_audio::has::PresetCtpOpcode::READ_PRESETS, 6));
  ASSERT_EQ(3u, wrapper.ref_cnt);

  EXPECT_CALL(*AlarmMock::Get(), AlarmFree(_)).Times(0);
  ASSERT_FALSE(wrapper.IsFullyCompleted());

  wrapper.SetCompleted(address1);
@@ -3144,23 +3133,20 @@ TEST_F(HasTypesTest, test_group_op_coordinator_completion) {
  wrapper.SetCompleted(address3);
  ASSERT_EQ(1u, wrapper.ref_cnt);
  ASSERT_FALSE(wrapper.IsFullyCompleted());
  Mock::VerifyAndClearExpectations(&*AlarmMock::Get());

  /* Non existing address completion */
  EXPECT_CALL(*AlarmMock::Get(), AlarmFree(_)).Times(0);
  wrapper.SetCompleted(address2);
  Mock::VerifyAndClearExpectations(&*AlarmMock::Get());
  ASSERT_EQ(1u, wrapper.ref_cnt);

  /* Last device address completion */
  EXPECT_CALL(*AlarmMock::Get(), AlarmFree(_)).Times(1);
  wrapper2.SetCompleted(address2);
  Mock::VerifyAndClearExpectations(&*AlarmMock::Get());
  ASSERT_TRUE(wrapper.IsFullyCompleted());
  ASSERT_EQ(0u, wrapper.ref_cnt);

  EXPECT_CALL(*AlarmMock::Get(), AlarmFree(_)).Times(0);
  HasCtpGroupOpCoordinator::Cleanup();

  ASSERT_EQ(1, mock_function_count_map["alarm_free"]);
  ASSERT_EQ(1, mock_function_count_map["alarm_new"]);
}

}  // namespace
+6 −2
Original line number Diff line number Diff line
@@ -381,7 +381,7 @@ void* fixed_queue_try_remove_from_queue(fixed_queue_t* queue, void* data) {

alarm_t* alarm_new(const char* name) {
  mock_function_count_map[__func__]++;
  return nullptr;
  return (alarm_t*)new uint8_t[30];
}
alarm_t* alarm_new_periodic(const char* name) {
  mock_function_count_map[__func__]++;
@@ -404,7 +404,11 @@ void alarm_cancel(alarm_t* alarm) {
}
void alarm_cleanup(void) { mock_function_count_map[__func__]++; }
void alarm_debug_dump(int fd) { mock_function_count_map[__func__]++; }
void alarm_free(alarm_t* alarm) { mock_function_count_map[__func__]++; }
void alarm_free(alarm_t* alarm) {
  uint8_t* ptr = (uint8_t*)alarm;
  delete[] ptr;
  mock_function_count_map[__func__]++;
}
void alarm_set(alarm_t* alarm, uint64_t interval_ms, alarm_callback_t cb,
               void* data) {
  mock_function_count_map[__func__]++;