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

Commit 3a9f0171 authored by Łukasz Rymanowski's avatar Łukasz Rymanowski
Browse files

leaudio: Improve some unit tests

This patch fixes restrictions configuration unit tests which maked it
hard to extend.

Bug: 230107540
Test: atest BluetoothInstrumentationTests
Tag: #feature
Change-Id: I276108e690b76deab4bfbcab05e359381d4c2654
parent f03a14d5
Loading
Loading
Loading
Loading
+68 −49
Original line number Diff line number Diff line
@@ -388,8 +388,8 @@ struct TestGroupAseConfigurationData {
  uint8_t audio_channel_counts_snk;
  uint8_t audio_channel_counts_src;

  uint8_t active_channel_num_snk;
  uint8_t active_channel_num_src;
  uint8_t expected_active_channel_num_snk;
  uint8_t expected_active_channel_num_src;
};

class LeAudioAseConfigurationTest : public Test {
@@ -458,9 +458,9 @@ class LeAudioAseConfigurationTest : public Test {
    uint8_t active_channel_num_snk = 0;
    uint8_t active_channel_num_src = 0;

    bool have_active_ase =
        data.active_channel_num_snk + data.active_channel_num_src;
    ASSERT_EQ(have_active_ase, data.device->HaveActiveAse());
    bool expected_active_ase = data.expected_active_channel_num_snk +
                               data.expected_active_channel_num_src;
    ASSERT_EQ(expected_active_ase, data.device->HaveActiveAse());

    for (ase* ase = data.device->GetFirstActiveAse(); ase;
         ase = data.device->GetNextActiveAse(ase)) {
@@ -472,8 +472,8 @@ class LeAudioAseConfigurationTest : public Test {
            GetAudioChannelCounts(*ase->codec_config.audio_channel_allocation);
    }

    ASSERT_EQ(data.active_channel_num_snk, active_channel_num_snk);
    ASSERT_EQ(data.active_channel_num_src, active_channel_num_src);
    ASSERT_EQ(data.expected_active_channel_num_snk, active_channel_num_snk);
    ASSERT_EQ(data.expected_active_channel_num_src, active_channel_num_src);
  }

  void SetCisInformationToActiveAse(void) {
@@ -497,8 +497,8 @@ class LeAudioAseConfigurationTest : public Test {
    // the configuration should fail if there are no active ases expected
    bool success_expected = data_size > 0;
    for (int i = 0; i < data_size; i++) {
      success_expected &=
          (data[i].active_channel_num_snk + data[i].active_channel_num_src) > 0;
      success_expected &= (data[i].expected_active_channel_num_snk +
                           data[i].expected_active_channel_num_src) > 0;

      /* Prepare PAC's */
      PublishedAudioCapabilitiesBuilder snk_pac_builder, src_pac_builder;
@@ -523,71 +523,90 @@ class LeAudioAseConfigurationTest : public Test {
    }
  }

  int getNumOfAses(LeAudioDevice* device, uint8_t direction) {
    return std::count_if(
        device->ases_.begin(), device->ases_.end(),
        [direction](auto& a) { return a.direction == direction; });
  }

  void TestGroupAseConfiguration(LeAudioContextType context_type,
                                 TestGroupAseConfigurationData* data,
                                 uint8_t data_size) {
    const auto* configurations =
        ::le_audio::AudioSetConfigurationProvider::Get()->GetConfigurations(
            context_type);

    int num_of_matching_configurations = 0;
    bool success_expected = data_size > 0;
    for (const auto& audio_set_conf : *configurations) {
      bool interesting_configuration = true;
      // the configuration should fail if there are no active ases expected
      bool success_expected = data_size > 0;
      bool not_matching_scenario = false;
      uint8_t snk_ases_cnt = 0;
      uint8_t src_ases_cnt = 0;
      PublishedAudioCapabilitiesBuilder snk_pac_builder, src_pac_builder;
      snk_pac_builder.Reset();
      src_pac_builder.Reset();

      /* Let's go thru devices in the group and configure them*/
      for (int i = 0; i < data_size; i++) {
        success_expected &= (data[i].active_channel_num_snk +
                             data[i].active_channel_num_src) > 0;
        success_expected &= (data[i].expected_active_channel_num_snk +
                             data[i].expected_active_channel_num_src) > 0;
        int num_of_ase_snk_per_dev = 0;
        int num_of_ase_src_per_dev = 0;

        /* Prepare PAC's */
        /* Note this test requires that reach TwoStereoChan configuration
         * version has similar version for OneStereoChan (both SingleDev,
         * DualDev). This is just how the test is created and this limitation
         * should be removed b/230107540
         */
        /* Prepare PAC's for each device. Also make sure configuration is in our
         * interest to test */
        for (const auto& entry : (*audio_set_conf).confs) {
          /* Configuration requires more devices than are supplied */
          if (entry.device_cnt > data_size) {
            not_matching_scenario = true;
            break;
          /* We are interested in the configurations which contains exact number
           * of devices and number of ases is same the number of expected ases
           * to active
           */
          if (entry.device_cnt != data_size) {
            interesting_configuration = false;
          }

          if (entry.direction == kLeAudioDirectionSink) {
            snk_ases_cnt += entry.ase_cnt;
            num_of_ase_snk_per_dev = entry.ase_cnt / data_size;
            snk_pac_builder.Add(entry.codec, data[i].audio_channel_counts_snk);
          } else {
            num_of_ase_src_per_dev = entry.ase_cnt / data_size;
            src_pac_builder.Add(entry.codec, data[i].audio_channel_counts_src);
          }
        }

        /* Scenario requires more ASEs than defined requirement */
        if (snk_ases_cnt < data[i].audio_channel_counts_snk ||
            src_ases_cnt < data[i].audio_channel_counts_src) {
          not_matching_scenario = true;
        }

        if (not_matching_scenario) break;

          data[i].device->snk_pacs_ = snk_pac_builder.Get();
          data[i].device->src_pacs_ = src_pac_builder.Get();
        }

      if (not_matching_scenario) continue;
        /* Make sure configuration can satisfy number of expected active ASEs*/
        if (num_of_ase_snk_per_dev != data[i].expected_active_channel_num_snk) {
          interesting_configuration = false;
        }

        if (num_of_ase_src_per_dev != data[i].expected_active_channel_num_src) {
          interesting_configuration = false;
        }
      }
      /* Stimulate update of active context map */
      group_->UpdateActiveContextsMap(static_cast<uint16_t>(context_type));
      ASSERT_EQ(success_expected, group_->Configure(context_type));

      auto configuration_result = group_->Configure(context_type);

      /* In case of configuration #ase is same as the one we expected to be
       * activated verify, ASEs are actually active */
      if (interesting_configuration) {
        ASSERT_TRUE(configuration_result);
        num_of_matching_configurations++;
        /* Check if each of the devices has activated ASEs as expected */
        for (int i = 0; i < data_size; i++) {
          TestGroupAseConfigurationVerdict(data[i]);
        }

      }
      group_->Deactivate();
      TestAsesInactive();
    }

    if (success_expected) {
      ASSERT_TRUE((num_of_matching_configurations > 0));
    } else {
      ASSERT_TRUE(num_of_matching_configurations == 0);
    }
  }

  void TestAsesActive(LeAudioCodecId codec_id, uint8_t sampling_frequency,
@@ -703,7 +722,7 @@ TEST_F(LeAudioAseConfigurationTest, test_mono_speaker_ringtone) {
  LeAudioDevice* mono_speaker = AddTestDevice(1, 1);
  TestGroupAseConfigurationData data(
      {mono_speaker, kLeAudioCodecLC3ChannelCountSingleChannel,
       kLeAudioCodecLC3ChannelCountSingleChannel, 1, 0});
       kLeAudioCodecLC3ChannelCountSingleChannel, 1, 1});

  TestGroupAseConfiguration(LeAudioContextType::RINGTONE, &data, 1);
}
@@ -730,7 +749,7 @@ TEST_F(LeAudioAseConfigurationTest, test_bounded_headphones_ringtone) {
  LeAudioDevice* bounded_headphones = AddTestDevice(2, 1);
  TestGroupAseConfigurationData data(
      {bounded_headphones, kLeAudioCodecLC3ChannelCountTwoChannel,
       kLeAudioCodecLC3ChannelCountSingleChannel, 2, 0});
       kLeAudioCodecLC3ChannelCountSingleChannel, 2, 1});

  TestGroupAseConfiguration(LeAudioContextType::RINGTONE, &data, 1);
}
@@ -757,7 +776,7 @@ TEST_F(LeAudioAseConfigurationTest, test_bounded_headset_ringtone) {
  LeAudioDevice* bounded_headset = AddTestDevice(2, 1);
  TestGroupAseConfigurationData data(
      {bounded_headset, kLeAudioCodecLC3ChannelCountTwoChannel,
       kLeAudioCodecLC3ChannelCountSingleChannel, 2, 0});
       kLeAudioCodecLC3ChannelCountSingleChannel, 2, 1});

  TestGroupAseConfiguration(LeAudioContextType::RINGTONE, &data, 1);
}
@@ -785,9 +804,9 @@ TEST_F(LeAudioAseConfigurationTest, test_earbuds_ringtone) {
  LeAudioDevice* right = AddTestDevice(1, 1);
  TestGroupAseConfigurationData data[] = {
      {left, kLeAudioCodecLC3ChannelCountSingleChannel,
       kLeAudioCodecLC3ChannelCountSingleChannel, 1, 0},
       kLeAudioCodecLC3ChannelCountSingleChannel, 1, 1},
      {right, kLeAudioCodecLC3ChannelCountSingleChannel,
       kLeAudioCodecLC3ChannelCountSingleChannel, 1, 0}};
       kLeAudioCodecLC3ChannelCountSingleChannel, 1, 1}};

  TestGroupAseConfiguration(LeAudioContextType::RINGTONE, data, 2);
}
@@ -820,7 +839,7 @@ TEST_F(LeAudioAseConfigurationTest, test_handsfree_ringtone) {
  LeAudioDevice* handsfree = AddTestDevice(1, 1);
  TestGroupAseConfigurationData data(
      {handsfree, kLeAudioCodecLC3ChannelCountSingleChannel,
       kLeAudioCodecLC3ChannelCountSingleChannel, 1, 0});
       kLeAudioCodecLC3ChannelCountSingleChannel, 1, 1});

  TestGroupAseConfiguration(LeAudioContextType::RINGTONE, &data, 1);
}