summaryrefslogtreecommitdiff
path: root/cras/src/tests/card_config_unittest.cc
blob: 4616515be53cf15eda3637bbe4b6aabbe00391cb (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
// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <gtest/gtest.h>
#include <stdio.h>

extern "C" {
#include "cras_card_config.h"
#include "cras_types.h"
}

namespace {

static unsigned int cras_volume_curve_create_default_called;
static struct cras_volume_curve* cras_volume_curve_create_default_return;
static unsigned int cras_volume_curve_create_simple_step_called;
static long cras_volume_curve_create_simple_step_max_volume;
static long cras_volume_curve_create_simple_step_volume_step;
static struct cras_volume_curve* cras_volume_curve_create_simple_step_return;
static unsigned int cras_volume_curve_create_explicit_called;
static long cras_explicit_curve[101];
static struct cras_volume_curve* cras_volume_curve_create_explicit_return;

static const char CONFIG_PATH[] = CRAS_UT_TMPDIR;

void CreateConfigFile(const char* name, const char* config_text) {
  FILE* f;
  char card_path[128];

  snprintf(card_path, sizeof(card_path), "%s/%s", CONFIG_PATH, name);
  f = fopen(card_path, "w");
  if (f == NULL)
    return;

  fprintf(f, "%s", config_text);

  fclose(f);
}

class CardConfigTestSuite : public testing::Test {
 protected:
  virtual void SetUp() {
    cras_volume_curve_create_default_called = 0;
    cras_volume_curve_create_default_return =
        reinterpret_cast<struct cras_volume_curve*>(0x55);
    cras_volume_curve_create_simple_step_return =
        reinterpret_cast<struct cras_volume_curve*>(0x56);
    cras_volume_curve_create_explicit_return =
        reinterpret_cast<struct cras_volume_curve*>(0x57);
    cras_volume_curve_create_simple_step_called = 0;
    cras_volume_curve_create_simple_step_return = NULL;
    cras_volume_curve_create_explicit_called = 0;
    cras_volume_curve_create_explicit_return = NULL;
  }
};

// Test that no config is returned if the file doesn't exist.
TEST_F(CardConfigTestSuite, NoConfigFound) {
  struct cras_card_config* config;

  config = cras_card_config_create(CONFIG_PATH, "no_effing_way_this_exists");
  EXPECT_EQ(NULL, config);
}

// Test an empty config file, should return a null volume curve.
TEST_F(CardConfigTestSuite, EmptyConfigFileReturnsNullVolumeCurve) {
  static const char empty_config_text[] = "";
  static const char empty_config_name[] = "EmptyConfigCard";
  struct cras_card_config* config;
  struct cras_volume_curve* curve;

  CreateConfigFile(empty_config_name, empty_config_text);

  config = cras_card_config_create(CONFIG_PATH, empty_config_name);
  EXPECT_NE(static_cast<struct cras_card_config*>(NULL), config);

  curve = cras_card_config_get_volume_curve_for_control(config, "asdf");
  EXPECT_EQ(0, cras_volume_curve_create_default_called);
  EXPECT_EQ(static_cast<struct cras_volume_curve*>(NULL), curve);

  cras_card_config_destroy(config);
}

// Getting a curve from a null config should always return null volume curve.
TEST_F(CardConfigTestSuite, NullConfigGivesDefaultVolumeCurve) {
  struct cras_volume_curve* curve;

  curve = cras_card_config_get_volume_curve_for_control(NULL, "asdf");
  EXPECT_EQ(0, cras_volume_curve_create_default_called);
  EXPECT_EQ(static_cast<struct cras_volume_curve*>(NULL), curve);
}

// Test getting a curve from a simple_step configuration.
TEST_F(CardConfigTestSuite, SimpleStepConfig) {
  static const char simple_config_name[] = "simple";
  static const char simple_config_text[] =
      "[Card1]\n"
      "volume_curve = simple_step\n"
      "volume_step = 75\n"
      "max_volume = -600\n";
  struct cras_card_config* config;
  struct cras_volume_curve* curve;

  CreateConfigFile(simple_config_name, simple_config_text);

  config = cras_card_config_create(CONFIG_PATH, simple_config_name);
  EXPECT_NE(static_cast<struct cras_card_config*>(NULL), config);

  // Unknown config should return default curve.
  curve = cras_card_config_get_volume_curve_for_control(NULL, "asdf");
  EXPECT_EQ(0, cras_volume_curve_create_default_called);
  EXPECT_EQ(static_cast<struct cras_volume_curve*>(NULL), curve);

  // Test a config that specifies simple_step.
  curve = cras_card_config_get_volume_curve_for_control(config, "Card1");
  EXPECT_EQ(cras_volume_curve_create_simple_step_return, curve);
  EXPECT_EQ(0, cras_volume_curve_create_default_called);
  EXPECT_EQ(1, cras_volume_curve_create_simple_step_called);
  EXPECT_EQ(-600, cras_volume_curve_create_simple_step_max_volume);
  EXPECT_EQ(75, cras_volume_curve_create_simple_step_volume_step);

  cras_card_config_destroy(config);
}

// Test getting a curve from an explicit configuration.
TEST_F(CardConfigTestSuite, ExplicitCurveConfig) {
  static const char explicit_config_name[] = "explicit";
  static const char explicit_config_text[] =
      "[Card1]\n"
      "volume_curve = explicit\n"
      "dB_at_0 = -9950\n"
      "dB_at_1 = -9850\n"
      "dB_at_2 = -9750\n"
      "dB_at_3 = -9650\n"
      "dB_at_4 = -9550\n"
      "dB_at_5 = -9450\n"
      "dB_at_6 = -9350\n"
      "dB_at_7 = -9250\n"
      "dB_at_8 = -9150\n"
      "dB_at_9 = -9050\n"
      "dB_at_10 = -8950\n"
      "dB_at_11 = -8850\n"
      "dB_at_12 = -8750\n"
      "dB_at_13 = -8650\n"
      "dB_at_14 = -8550\n"
      "dB_at_15 = -8450\n"
      "dB_at_16 = -8350\n"
      "dB_at_17 = -8250\n"
      "dB_at_18 = -8150\n"
      "dB_at_19 = -8050\n"
      "dB_at_20 = -7950\n"
      "dB_at_21 = -7850\n"
      "dB_at_22 = -7750\n"
      "dB_at_23 = -7650\n"
      "dB_at_24 = -7550\n"
      "dB_at_25 = -7450\n"
      "dB_at_26 = -7350\n"
      "dB_at_27 = -7250\n"
      "dB_at_28 = -7150\n"
      "dB_at_29 = -7050\n"
      "dB_at_30 = -6950\n"
      "dB_at_31 = -6850\n"
      "dB_at_32 = -6750\n"
      "dB_at_33 = -6650\n"
      "dB_at_34 = -6550\n"
      "dB_at_35 = -6450\n"
      "dB_at_36 = -6350\n"
      "dB_at_37 = -6250\n"
      "dB_at_38 = -6150\n"
      "dB_at_39 = -6050\n"
      "dB_at_40 = -5950\n"
      "dB_at_41 = -5850\n"
      "dB_at_42 = -5750\n"
      "dB_at_43 = -5650\n"
      "dB_at_44 = -5550\n"
      "dB_at_45 = -5450\n"
      "dB_at_46 = -5350\n"
      "dB_at_47 = -5250\n"
      "dB_at_48 = -5150\n"
      "dB_at_49 = -5050\n"
      "dB_at_50 = -4950\n"
      "dB_at_51 = -4850\n"
      "dB_at_52 = -4750\n"
      "dB_at_53 = -4650\n"
      "dB_at_54 = -4550\n"
      "dB_at_55 = -4450\n"
      "dB_at_56 = -4350\n"
      "dB_at_57 = -4250\n"
      "dB_at_58 = -4150\n"
      "dB_at_59 = -4050\n"
      "dB_at_60 = -3950\n"
      "dB_at_61 = -3850\n"
      "dB_at_62 = -3750\n"
      "dB_at_63 = -3650\n"
      "dB_at_64 = -3550\n"
      "dB_at_65 = -3450\n"
      "dB_at_66 = -3350\n"
      "dB_at_67 = -3250\n"
      "dB_at_68 = -3150\n"
      "dB_at_69 = -3050\n"
      "dB_at_70 = -2950\n"
      "dB_at_71 = -2850\n"
      "dB_at_72 = -2750\n"
      "dB_at_73 = -2650\n"
      "dB_at_74 = -2550\n"
      "dB_at_75 = -2450\n"
      "dB_at_76 = -2350\n"
      "dB_at_77 = -2250\n"
      "dB_at_78 = -2150\n"
      "dB_at_79 = -2050\n"
      "dB_at_80 = -1950\n"
      "dB_at_81 = -1850\n"
      "dB_at_82 = -1750\n"
      "dB_at_83 = -1650\n"
      "dB_at_84 = -1550\n"
      "dB_at_85 = -1450\n"
      "dB_at_86 = -1350\n"
      "dB_at_87 = -1250\n"
      "dB_at_88 = -1150\n"
      "dB_at_89 = -1050\n"
      "dB_at_90 = -950\n"
      "dB_at_91 = -850\n"
      "dB_at_92 = -750\n"
      "dB_at_93 = -650\n"
      "dB_at_94 = -550\n"
      "dB_at_95 = -450\n"
      "dB_at_96 = -350\n"
      "dB_at_97 = -250\n"
      "dB_at_98 = -150\n"
      "dB_at_99 = -50\n"
      "dB_at_100 = 50\n";
  struct cras_card_config* config;
  struct cras_volume_curve* curve;

  CreateConfigFile(explicit_config_name, explicit_config_text);

  config = cras_card_config_create(CONFIG_PATH, explicit_config_name);
  EXPECT_NE(static_cast<struct cras_card_config*>(NULL), config);

  // Test a explicit curve config.
  curve = cras_card_config_get_volume_curve_for_control(config, "Card1");
  EXPECT_EQ(0, cras_volume_curve_create_default_called);
  EXPECT_EQ(0, cras_volume_curve_create_simple_step_called);
  EXPECT_EQ(1, cras_volume_curve_create_explicit_called);
  EXPECT_EQ(cras_volume_curve_create_explicit_return, curve);
  for (unsigned int i = 0; i < 101; i++) {
    EXPECT_EQ((static_cast<long>(i) - 100) * 100 + 50, cras_explicit_curve[i]);
  }

  cras_card_config_destroy(config);
}

// Stubs.
extern "C" {

struct cras_volume_curve* cras_volume_curve_create_default() {
  cras_volume_curve_create_default_called++;
  return cras_volume_curve_create_default_return;
}

struct cras_volume_curve* cras_volume_curve_create_simple_step(
    long max_volume,
    long volume_step) {
  cras_volume_curve_create_simple_step_called++;
  cras_volume_curve_create_simple_step_max_volume = max_volume;
  cras_volume_curve_create_simple_step_volume_step = volume_step;
  return cras_volume_curve_create_simple_step_return;
}

struct cras_volume_curve* cras_volume_curve_create_explicit(long dB_vals[101]) {
  cras_volume_curve_create_explicit_called++;
  memcpy(cras_explicit_curve, dB_vals, sizeof(cras_explicit_curve));
  return cras_volume_curve_create_explicit_return;
}
}

}  //  namespace

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}