File: audio_device_id_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (119 lines) | stat: -rw-r--r-- 4,208 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/audio/audio_device_id.h"

#include <cstdint>

#include "chromeos/ash/components/audio/audio_device.h"
#include "chromeos/ash/components/audio/audio_device_selection_test_base.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {

namespace {

const uint32_t kInputMaxSupportedChannels = 1;
const uint32_t kOutputMaxSupportedChannels = 2;
const uint32_t kInputAudioEffect = 1;
const uint32_t kOutputAudioEffect = 0;
const int32_t kInputNumberOfVolumeSteps = 0;
const int32_t kOutputNumberOfVolumeSteps = 25;

const uint64_t kHeadphoneId = 10002;
const uint64_t kInternalMicId = 10003;
const uint64_t kUsbMicId = 10005;

struct AudioNodeInfo {
  bool is_input;
  uint64_t id;
  const char* const device_name;
  const char* const type;
  const char* const name;
};

const AudioNodeInfo kInternalMic = {true, kInternalMicId, "Fake Mic",
                                    "INTERNAL_MIC", "Internal Mic"};

const AudioNodeInfo kHeadphone = {false, kHeadphoneId, "Fake Headphone",
                                  "HEADPHONE", "Headphone"};

const AudioNodeInfo kUSBMic = {true, kUsbMicId, "Fake USB Mic", "USB",
                               "USB Microphone"};

AudioDevice CreateAudioDevice(const AudioNodeInfo& info, int version) {
  return AudioDevice(AudioNode(
      info.is_input, info.id, /*has_v2_stable_device_id=*/version == 2,
      /*stable_device_id_v1=*/info.id,
      /*stable_device_id_v2=*/version == 1 ? 0 : info.id ^ 0xFF,
      info.device_name, info.type, info.name, false, 0,
      info.is_input ? kInputMaxSupportedChannels : kOutputMaxSupportedChannels,
      info.is_input ? kInputAudioEffect : kOutputAudioEffect,
      info.is_input ? kInputNumberOfVolumeSteps : kOutputNumberOfVolumeSteps));
}

}  // namespace

class AudioDeviceIdTest : public AudioDeviceSelectionTestBase {};

TEST_F(AudioDeviceIdTest, GetDeviceIdString) {
  struct {
    const AudioNodeInfo audio_node_info;
    int version;
    const std::string expected_id;
  } items[] = {
      {kInternalMic, 1, "10003 : 1"}, {kInternalMic, 2, "2 : 10220 : 1"},
      {kHeadphone, 1, "10002 : 0"},   {kHeadphone, 2, "2 : 10221 : 0"},
      {kUSBMic, 1, "10005 : 1"},      {kUSBMic, 2, "2 : 10218 : 1"}};

  for (const auto& item : items) {
    EXPECT_EQ(GetDeviceIdString(
                  CreateAudioDevice(item.audio_node_info, item.version)),
              item.expected_id);
  }
}

// Verify GetDeviceSetIdString() can convert AudioDeviceList
// to string containing comma separated set of versioned device IDs.
TEST_F(AudioDeviceIdTest, GetDeviceSetIdString) {
  struct {
    const AudioDeviceList audio_device_list;
    const std::string expected_id;
  } items[] = {
      {{}, ""},
      {{CreateAudioDevice(kInternalMic, 1)}, "10003 : 1"},
      {{CreateAudioDevice(kInternalMic, 1), CreateAudioDevice(kInternalMic, 1)},
       "10003 : 1"},
      {{CreateAudioDevice(kInternalMic, 1), CreateAudioDevice(kHeadphone, 1)},
       "10002 : 0,10003 : 1"},
      {{CreateAudioDevice(kInternalMic, 1), CreateAudioDevice(kHeadphone, 1),
        CreateAudioDevice(kUSBMic, 1)},
       "10002 : 0,10003 : 1,10005 : 1"},
      {{CreateAudioDevice(kHeadphone, 1), CreateAudioDevice(kInternalMic, 1),
        CreateAudioDevice(kUSBMic, 1)},
       "10002 : 0,10003 : 1,10005 : 1"},
      {{CreateAudioDevice(kUSBMic, 1), CreateAudioDevice(kHeadphone, 1),
        CreateAudioDevice(kInternalMic, 1)},
       "10002 : 0,10003 : 1,10005 : 1"}};

  for (const auto& item : items) {
    EXPECT_EQ(GetDeviceSetIdString(item.audio_device_list), item.expected_id);
  }
}

TEST_F(AudioDeviceIdTest, ParseDeviceId) {
  struct {
    const std::string id_string;
    const std::optional<uint64_t> expected_id;
  } items[] = {
      {"", std::nullopt},           {"2", 2},         {"10003 : 1", 10003},
      {"2 : 10220 : 1", 10220},     {"10220", 10220}, {"a120000", std::nullopt},
      {"2 : a120000", std::nullopt}};

  for (const auto& item : items) {
    EXPECT_EQ(ParseDeviceId(item.id_string), item.expected_id);
  }
}

}  // namespace ash