aboutsummaryrefslogtreecommitdiff
path: root/tests/src/mixer_test.cc
blob: cbce3ecd2f87a0e9bdc9b977b9fa43c6b3791a6f (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* mixer_test.c
**
** Copyright 2020, The Android Open Source Project
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
**     * Redistributions of source code must retain the above copyright
**       notice, this list of conditions and the following disclaimer.
**     * Redistributions in binary form must reproduce the above copyright
**       notice, this list of conditions and the following disclaimer in the
**       documentation and/or other materials provided with the distribution.
**     * Neither the name of The Android Open Source Project nor the names of
**       its contributors may be used to endorse or promote products derived
**       from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
** DAMAGE.
*/
#include "pcm_test_device.h"

#include <limits>
#include <string_view>
#include <string>
#include <thread>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>

#include <gtest/gtest.h>

#include "tinyalsa/mixer.h"

namespace tinyalsa {
namespace testing {

#ifndef MAX_CARD_INDEX
#define MAX_CARD_INDEX 2
#endif

static constexpr unsigned int kMaxCardIndex = MAX_CARD_INDEX;

static constexpr int k100Percent = 100;
static constexpr int k0Percent = 0;

TEST(MixerTest, OpenAndClose) {
    // assume card 0 is always probed.
    mixer *mixer_object = mixer_open(0);
    EXPECT_NE(mixer_object, nullptr);
    mixer_close(mixer_object);
}

TEST(MixerTest, NullParametersCheck) {
    EXPECT_EQ(mixer_open(1000), nullptr);
    mixer_close(nullptr);
    EXPECT_EQ(mixer_add_new_ctls(nullptr), 0);
    EXPECT_EQ(mixer_get_name(nullptr), nullptr);
    EXPECT_EQ(mixer_get_num_ctls(nullptr), 0);
    EXPECT_EQ(mixer_get_num_ctls_by_name(nullptr, ""), 0);
    EXPECT_EQ(mixer_get_num_ctls_by_name(reinterpret_cast<const mixer *>(1), nullptr), 0);
    EXPECT_EQ(mixer_get_ctl_const(nullptr, 0), nullptr);
    EXPECT_EQ(mixer_get_ctl(nullptr, 0), nullptr);
    EXPECT_EQ(mixer_get_ctl_by_name(nullptr, ""), nullptr);
    EXPECT_EQ(mixer_get_ctl_by_name(reinterpret_cast<mixer *>(1), nullptr), nullptr);
    EXPECT_EQ(mixer_get_ctl_by_name_and_index(nullptr, "", 0), nullptr);
    EXPECT_EQ(
            mixer_get_ctl_by_name_and_index(reinterpret_cast<mixer *>(1), nullptr, 0),
            nullptr);
    EXPECT_NE(mixer_subscribe_events(nullptr, 0), 0);
    EXPECT_LT(mixer_wait_event(nullptr, 0), 0);
    EXPECT_EQ(mixer_ctl_get_id(nullptr), std::numeric_limits<unsigned int>::max());
    EXPECT_EQ(mixer_ctl_get_name(nullptr), nullptr);
    EXPECT_EQ(mixer_ctl_get_type(nullptr), MIXER_CTL_TYPE_UNKNOWN);
    EXPECT_STREQ(mixer_ctl_get_type_string(nullptr), "");
    EXPECT_EQ(mixer_ctl_get_num_values(nullptr), 0);
    EXPECT_EQ(mixer_ctl_get_num_enums(nullptr), 0);
    EXPECT_EQ(mixer_ctl_get_enum_string(nullptr, 0), nullptr);
    mixer_ctl_update(nullptr);
    EXPECT_EQ(mixer_ctl_is_access_tlv_rw(nullptr), 0);
    EXPECT_EQ(mixer_ctl_get_percent(nullptr, 0), -EINVAL);
    EXPECT_EQ(mixer_ctl_set_percent(nullptr, 0, 0), -EINVAL);
    EXPECT_EQ(mixer_ctl_get_value(nullptr, 0), -EINVAL);
    EXPECT_EQ(mixer_ctl_get_array(nullptr, reinterpret_cast<void *>(1), 1), -EINVAL);
    EXPECT_EQ(mixer_ctl_get_array(reinterpret_cast<mixer_ctl *>(1), nullptr, 1), -EINVAL);
    EXPECT_EQ(
            mixer_ctl_get_array(
                reinterpret_cast<mixer_ctl *>(1), reinterpret_cast<void *>(1), 0), -EINVAL);
    EXPECT_EQ(mixer_ctl_set_value(nullptr, 0, 0), -EINVAL);
    EXPECT_EQ(mixer_ctl_set_array(nullptr, reinterpret_cast<const void *>(1), 1), -EINVAL);
    EXPECT_EQ(mixer_ctl_set_array(reinterpret_cast<mixer_ctl *>(1), nullptr, 1), -EINVAL);
    EXPECT_EQ(
            mixer_ctl_set_array(
                reinterpret_cast<mixer_ctl *>(1), reinterpret_cast<const void *>(1), 0), -EINVAL);
    EXPECT_EQ(mixer_ctl_set_enum_by_string(nullptr, reinterpret_cast<const char *>(1)), -EINVAL);
    EXPECT_EQ(mixer_ctl_set_enum_by_string(reinterpret_cast<mixer_ctl *>(1), nullptr), -EINVAL);
    EXPECT_EQ(mixer_ctl_get_range_min(nullptr), -EINVAL);
    EXPECT_EQ(mixer_ctl_get_range_max(nullptr), -EINVAL);
    EXPECT_EQ(mixer_read_event(nullptr, reinterpret_cast<mixer_ctl_event *>(1)), -EINVAL);
    EXPECT_EQ(mixer_read_event(reinterpret_cast<mixer *>(1), nullptr), -EINVAL);
    EXPECT_EQ(mixer_consume_event(nullptr), -EINVAL);
}

class MixerTest : public ::testing::TestWithParam<unsigned int> {
  protected:
    MixerTest() : mixer_object(nullptr) {}
    virtual ~MixerTest() = default;

    virtual void SetUp() override {
        unsigned int card = GetParam();
        mixer_object = mixer_open(card);
        ASSERT_NE(mixer_object, nullptr);
    }

    virtual void TearDown() override {
        mixer_close(mixer_object);
    }

    mixer *mixer_object;
};

TEST_P(MixerTest, AddNewControls) {
    ASSERT_EQ(mixer_add_new_ctls(mixer_object), 0);
}

TEST_P(MixerTest, GetName) {
    const char *name = mixer_get_name(mixer_object);
    std::cout << name << std::endl;
    ASSERT_STRNE(name, "");
}

TEST_P(MixerTest, GetNumberOfControls) {
    unsigned int nums = mixer_get_num_ctls(mixer_object);
    std::cout << nums << std::endl;
    ASSERT_GT(nums, 0);
}

class MixerControlsTest : public MixerTest {
  protected:
    MixerControlsTest() : number_of_controls(0), controls(nullptr) {}
    virtual ~MixerControlsTest() = default;

    virtual void SetUp() override {
        MixerTest::SetUp();

        number_of_controls = mixer_get_num_ctls(mixer_object);
        ASSERT_GT(number_of_controls, 0);

        controls = std::make_unique<const mixer_ctl *[]>(number_of_controls);
        ASSERT_NE(controls, nullptr);

        for (unsigned int i = 0; i < number_of_controls; i++) {
            controls[i] = mixer_get_ctl_const(mixer_object, i);
            EXPECT_EQ(mixer_ctl_get_id(controls[i]), i);
            EXPECT_STRNE(mixer_ctl_get_name(controls[i]), "");
            EXPECT_NE(controls[i], nullptr);
        }
    }

    virtual void TearDown() override {
        controls = nullptr;
        MixerTest::TearDown();
    }

    unsigned int number_of_controls;
    std::unique_ptr<const mixer_ctl *[]> controls;
};

TEST_P(MixerControlsTest, GetNumberOfControlsByName) {
    for (unsigned int i = 0; i < number_of_controls; ++i) {
        const char *name = mixer_ctl_get_name(controls[i]);
        ASSERT_GE(mixer_get_num_ctls_by_name(mixer_object, name), 1);
    }

    std::string name{mixer_ctl_get_name(controls[0])};
    name += "1";
    ASSERT_EQ(mixer_get_num_ctls_by_name(mixer_object, name.c_str()), 0);
}

TEST_P(MixerControlsTest, GetControlById) {
    for (unsigned int i = 0; i < number_of_controls; ++i) {
        ASSERT_EQ(mixer_get_ctl(mixer_object, i), controls[i]);
    }

    ASSERT_EQ(mixer_get_ctl(mixer_object, number_of_controls), nullptr);
}

TEST_P(MixerControlsTest, GetControlByName) {
    std::unordered_set<std::string> visited_names_set;
    for (unsigned int i = 0; i < number_of_controls; ++i) {
        std::string name{mixer_ctl_get_name(controls[i])};
        if (visited_names_set.find(name) == visited_names_set.end()) {
            ASSERT_EQ(mixer_get_ctl_by_name(mixer_object, name.c_str()), controls[i]);
            visited_names_set.insert(name);
        }
    }
}

TEST_P(MixerControlsTest, GetControlByNameAndIndex) {
    std::unordered_map<std::string, int32_t> visited_names_and_count_map;
    for (unsigned int i = 0; i < number_of_controls; ++i) {
        std::string name{mixer_ctl_get_name(controls[i])};
        if (visited_names_and_count_map.find(name) == visited_names_and_count_map.end()) {
            visited_names_and_count_map[name] = 0;
        }
        ASSERT_EQ(
                mixer_get_ctl_by_name_and_index(mixer_object,
                                                name.c_str(),
                                                visited_names_and_count_map[name]),
                controls[i]);
        visited_names_and_count_map[name] = visited_names_and_count_map[name] + 1;
    }
}

static inline bool IsValidTypeString(std::string& type) {
    return type == "BOOL" || type == "INT" || type == "ENUM" || type == "BYTE" ||
            type == "IEC958" || type == "INT64";
}

TEST_P(MixerControlsTest, GetControlTypeString) {
    ASSERT_STREQ(mixer_ctl_get_type_string(nullptr), "");

    for (unsigned int i = 0; i < number_of_controls; ++i) {
        std::string type{mixer_ctl_get_type_string(controls[i])};
        ASSERT_TRUE(IsValidTypeString(type));
    }
}

TEST_P(MixerControlsTest, GetNumberOfValues) {
    ASSERT_EQ(mixer_ctl_get_num_values(nullptr), 0);
}

TEST_P(MixerControlsTest, GetNumberOfEnumsAndEnumString) {
    for (unsigned int i = 0; i < number_of_controls; ++i) {
        const mixer_ctl *control = controls[i];
        if (mixer_ctl_get_type(control) == MIXER_CTL_TYPE_ENUM) {
            unsigned int number_of_enums = mixer_ctl_get_num_enums(control);
            ASSERT_GT(number_of_enums, 0);
            for (unsigned int enum_id = 0; enum_id < number_of_enums; ++enum_id) {
                const char *enum_name = mixer_ctl_get_enum_string(
                        const_cast<mixer_ctl *>(control),
                        enum_id);
                ASSERT_STRNE(enum_name, "");
            }
        }
    }
}

TEST_P(MixerControlsTest, UpdateControl) {
    for (unsigned int i = 0; i < number_of_controls; ++i) {
        mixer_ctl_update(const_cast<mixer_ctl *>(controls[i]));
    }
}

TEST_P(MixerControlsTest, GetPercent) {
    for (unsigned int i = 0; i < number_of_controls; ++i) {
        const mixer_ctl *control = controls[i];
        if (mixer_ctl_get_type(control) == MIXER_CTL_TYPE_INT) {
            unsigned int number_of_values = mixer_ctl_get_num_values(controls[i]);
            std::unique_ptr<long []> values = std::make_unique<long []>(number_of_values);
            mixer_ctl_get_array(control, values.get(), number_of_values);
            for (unsigned int value_id = 0; value_id < number_of_values; ++value_id) {
                int max = mixer_ctl_get_range_max(control);
                int min = mixer_ctl_get_range_min(control);
                int percent = mixer_ctl_get_percent(control, value_id);
                ASSERT_GE(percent, k0Percent);
                ASSERT_LE(percent, k100Percent);
                int range = max - min;
                ASSERT_EQ(percent, (values[value_id] - min) * k100Percent / range);
            }
        } else {
            ASSERT_EQ(mixer_ctl_get_percent(control, 0), -EINVAL);
        }
    }
}

TEST_P(MixerControlsTest, SetPercent) {
    for (unsigned int i = 0; i < number_of_controls; ++i) {
        const mixer_ctl *control = controls[i];
        if (mixer_ctl_get_type(control) == MIXER_CTL_TYPE_INT) {
            unsigned int number_of_values = mixer_ctl_get_num_values(controls[i]);
            std::unique_ptr<long []> values = std::make_unique<long []>(number_of_values);
            mixer_ctl_get_array(control, values.get(), number_of_values);
            for (unsigned int value_id = 0; value_id < number_of_values; ++value_id) {
                int max = mixer_ctl_get_range_max(control);
                int min = mixer_ctl_get_range_min(control);
                int value = values[value_id];
                int percent = mixer_ctl_get_percent(control, value_id);
                if (mixer_ctl_set_percent(
                        const_cast<mixer_ctl *>(control), value_id, k100Percent) == 0) {
                    // note: some controls are able to be written, but their values might not be
                    //   changed.
                    mixer_ctl_get_array(control, values.get(), number_of_values);
                    int new_value = values[value_id];
                    ASSERT_TRUE(new_value == value || new_value == max);
                }
                if (mixer_ctl_set_percent(
                        const_cast<mixer_ctl *>(control), value_id, k0Percent) == 0) {
                    mixer_ctl_get_array(control, values.get(), number_of_values);
                    int new_value = values[value_id];
                    ASSERT_TRUE(new_value == value || new_value == min);
                }
                mixer_ctl_set_percent(const_cast<mixer_ctl *>(control), value_id, percent);
            }
        } else {
            ASSERT_EQ(mixer_ctl_get_percent(control, 0), -EINVAL);
        }
    }
}

TEST_P(MixerControlsTest, Event) {
    ASSERT_EQ(mixer_subscribe_events(mixer_object, 1), 0);
    const mixer_ctl *control = nullptr;
    for (unsigned int i = 0; i < number_of_controls; ++i) {
        std::string_view name{mixer_ctl_get_name(controls[i])};

        if (name.find("Volume") != std::string_view::npos) {
            control = controls[i];
        }
    }

    if (control == nullptr) {
        GTEST_SKIP() << "No volume control was found in the controls list.";
    }

    auto *local_mixer_object = mixer_object;
    int percent = mixer_ctl_get_percent(control, 0);
    std::thread thread([local_mixer_object, control, percent] () {
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
        mixer_ctl_set_percent(
                const_cast<mixer_ctl *>(control), 0,
                percent == k100Percent ? k0Percent : k100Percent);
    });

    EXPECT_EQ(mixer_wait_event(mixer_object, 1000), 1);

    EXPECT_EQ(mixer_consume_event(mixer_object), 1);

    thread.join();
    ASSERT_EQ(mixer_subscribe_events(mixer_object, 0), 0);

    mixer_ctl_set_percent(const_cast<mixer_ctl *>(control), 0, percent);
}

INSTANTIATE_TEST_SUITE_P(
    MixerTest,
    MixerTest,
    ::testing::Range<unsigned int>(
        0,
        kMaxCardIndex + 1
    ));

INSTANTIATE_TEST_SUITE_P(
    MixerControlsTest,
    MixerControlsTest,
    ::testing::Range<unsigned int>(
        0,
        kMaxCardIndex + 1
    ));

} // namespace testing
} // namespace tinyalsa