1 // Copyright 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15
16 // Tests for audio volume handler.
17
18 #include "audio_volume_handler_mock.h"
19
20 #include <memory>
21 #include <string>
22
23 #include <base/files/file_path.h>
24 #include <base/files/file_util.h>
25 #include <base/files/scoped_temp_dir.h>
26 #include <brillo/key_value_store.h>
27 #include <brillo/strings/string_utils.h>
28 #include <gmock/gmock.h>
29 #include <gtest/gtest.h>
30
31 #include "audio_device_handler.h"
32
33 using base::FilePath;
34 using base::PathExists;
35 using base::ScopedTempDir;
36 using brillo::string_utils::ToString;
37 using std::stoi;
38 using testing::_;
39
40 namespace brillo {
41
42 class AudioVolumeHandlerTest : public testing::Test {
43 public:
SetUp()44 void SetUp() override {
45 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
46 volume_file_path_ = temp_dir_.path().Append("vol_file");
47 handler_.SetVolumeFilePathForTesting(volume_file_path_);
48 }
49
SetupHandlerVolumeFile()50 void SetupHandlerVolumeFile() {
51 handler_.kv_store_ = std::unique_ptr<KeyValueStore>(new KeyValueStore);
52 handler_.GenerateVolumeFile();
53 }
54
55 AudioVolumeHandlerMock handler_;
56 FilePath volume_file_path_;
57
58 private:
59 ScopedTempDir temp_dir_;
60 };
61
62 // Test that the volume file is formatted correctly.
TEST_F(AudioVolumeHandlerTest,FileGeneration)63 TEST_F(AudioVolumeHandlerTest, FileGeneration) {
64 SetupHandlerVolumeFile();
65 KeyValueStore kv_store;
66 kv_store.Load(volume_file_path_);
67 for (auto stream : handler_.kSupportedStreams_) {
68 std::string value;
69 ASSERT_EQ(handler_.kMinIndex_, 0);
70 ASSERT_EQ(handler_.kMaxIndex_, 100);
71 for (auto device : AudioDeviceHandler::kSupportedOutputDevices_) {
72 ASSERT_TRUE(kv_store.GetString(handler_.kCurrentIndexKey_ + "." +
73 ToString(stream) + "." +
74 ToString(device),
75 &value));
76 ASSERT_EQ(handler_.kDefaultCurrentIndex_, stoi(value));
77 }
78 }
79 }
80
81 // Test GetVolumeCurrentIndex.
TEST_F(AudioVolumeHandlerTest,GetVolumeForStreamDeviceTuple)82 TEST_F(AudioVolumeHandlerTest, GetVolumeForStreamDeviceTuple) {
83 handler_.kv_store_ = std::unique_ptr<KeyValueStore>(new KeyValueStore);
84 handler_.kv_store_->SetString(handler_.kCurrentIndexKey_ + ".1.2", "100");
85 ASSERT_EQ(
86 handler_.GetVolumeCurrentIndex(static_cast<audio_stream_type_t>(1), 2),
87 100);
88 }
89
90 // Test SetVolumeCurrentIndex.
TEST_F(AudioVolumeHandlerTest,SetVolumeForStreamDeviceTuple)91 TEST_F(AudioVolumeHandlerTest, SetVolumeForStreamDeviceTuple) {
92 handler_.kv_store_ = std::unique_ptr<KeyValueStore>(new KeyValueStore);
93 handler_.PersistVolumeConfiguration(
94 static_cast<audio_stream_type_t>(1), 2, 100);
95 std::string value;
96 auto key = handler_.kCurrentIndexKey_ + ".1.2";
97 handler_.kv_store_->GetString(key, &value);
98 ASSERT_EQ(stoi(value), 100);
99 }
100
101 // Test that a new volume file is generated if it doesn't exist.
TEST_F(AudioVolumeHandlerTest,InitNoFile)102 TEST_F(AudioVolumeHandlerTest, InitNoFile) {
103 EXPECT_CALL(handler_, InitAPSAllStreams());
104 handler_.Init(nullptr);
105 EXPECT_TRUE(PathExists(volume_file_path_));
106 }
107
108 // Test that a new volume file isn't generated it already exists.
TEST_F(AudioVolumeHandlerTest,InitFilePresent)109 TEST_F(AudioVolumeHandlerTest, InitFilePresent) {
110 KeyValueStore kv_store;
111 kv_store.SetString("foo", "100");
112 kv_store.Save(volume_file_path_);
113 EXPECT_CALL(handler_, InitAPSAllStreams());
114 handler_.Init(nullptr);
115 EXPECT_TRUE(PathExists(volume_file_path_));
116 std::string value;
117 handler_.kv_store_->GetString("foo", &value);
118 EXPECT_EQ(stoi(value), 100);
119 }
120
TEST_F(AudioVolumeHandlerTest,ProcessEventEmpty)121 TEST_F(AudioVolumeHandlerTest, ProcessEventEmpty) {
122 struct input_event event;
123 event.type = 0;
124 event.code = 0;
125 event.value = 0;
126 EXPECT_CALL(handler_, AdjustVolumeActiveStreams(_)).Times(0);
127 handler_.ProcessEvent(event);
128 }
129
TEST_F(AudioVolumeHandlerTest,ProcessEventKeyUp)130 TEST_F(AudioVolumeHandlerTest, ProcessEventKeyUp) {
131 struct input_event event;
132 event.type = EV_KEY;
133 event.code = KEY_VOLUMEUP;
134 event.value = 1;
135 EXPECT_CALL(handler_, AdjustVolumeActiveStreams(1));
136 handler_.ProcessEvent(event);
137 }
138
TEST_F(AudioVolumeHandlerTest,ProcessEventKeyDown)139 TEST_F(AudioVolumeHandlerTest, ProcessEventKeyDown) {
140 struct input_event event;
141 event.type = EV_KEY;
142 event.code = KEY_VOLUMEDOWN;
143 event.value = 1;
144 EXPECT_CALL(handler_, AdjustVolumeActiveStreams(-1));
145 handler_.ProcessEvent(event);
146 }
147
TEST_F(AudioVolumeHandlerTest,SelectStream)148 TEST_F(AudioVolumeHandlerTest, SelectStream) {
149 EXPECT_EQ(handler_.GetVolumeControlStream(), AUDIO_STREAM_DEFAULT);
150 handler_.SetVolumeControlStream(AUDIO_STREAM_MUSIC);
151 EXPECT_EQ(handler_.GetVolumeControlStream(), AUDIO_STREAM_MUSIC);
152 }
153
TEST_F(AudioVolumeHandlerTest,ComputeNewVolume)154 TEST_F(AudioVolumeHandlerTest, ComputeNewVolume) {
155 EXPECT_EQ(handler_.GetNewVolumeIndex(50, 1, AUDIO_STREAM_MUSIC), 51);
156 EXPECT_EQ(handler_.GetNewVolumeIndex(50, -1, AUDIO_STREAM_MUSIC), 49);
157 handler_.step_sizes_[AUDIO_STREAM_MUSIC] = 10;
158 EXPECT_EQ(handler_.GetNewVolumeIndex(50, 1, AUDIO_STREAM_MUSIC), 60);
159 EXPECT_EQ(handler_.GetNewVolumeIndex(50, -1, AUDIO_STREAM_MUSIC), 40);
160 SetupHandlerVolumeFile();
161 EXPECT_EQ(handler_.GetNewVolumeIndex(100, 1, AUDIO_STREAM_MUSIC), 100);
162 EXPECT_EQ(handler_.GetNewVolumeIndex(0, -1, AUDIO_STREAM_MUSIC), 0);
163 }
164
TEST_F(AudioVolumeHandlerTest,GetSetMaxSteps)165 TEST_F(AudioVolumeHandlerTest, GetSetMaxSteps) {
166 EXPECT_EQ(handler_.GetVolumeMaxSteps(AUDIO_STREAM_MUSIC), 100);
167 EXPECT_EQ(handler_.SetVolumeMaxSteps(AUDIO_STREAM_MUSIC, 0), EINVAL);
168 EXPECT_EQ(handler_.GetVolumeMaxSteps(AUDIO_STREAM_MUSIC), 100);
169 EXPECT_EQ(handler_.SetVolumeMaxSteps(AUDIO_STREAM_MUSIC, 100), 0);
170 EXPECT_EQ(handler_.GetVolumeMaxSteps(AUDIO_STREAM_MUSIC), 100);
171 EXPECT_EQ(handler_.SetVolumeMaxSteps(AUDIO_STREAM_MUSIC, -1), EINVAL);
172 EXPECT_EQ(handler_.SetVolumeMaxSteps(AUDIO_STREAM_MUSIC, 101), EINVAL);
173 }
174
TEST_F(AudioVolumeHandlerTest,GetSetVolumeIndex)175 TEST_F(AudioVolumeHandlerTest, GetSetVolumeIndex) {
176 SetupHandlerVolumeFile();
177 EXPECT_CALL(handler_, TriggerCallback(AUDIO_STREAM_MUSIC, _, 0));
178 EXPECT_EQ(handler_.SetVolumeIndex(
179 AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADSET, 0),
180 0);
181 EXPECT_CALL(handler_, TriggerCallback(AUDIO_STREAM_MUSIC, 0, 50));
182 EXPECT_EQ(handler_.SetVolumeIndex(
183 AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADSET, 50),
184 0);
185 EXPECT_CALL(handler_, TriggerCallback(AUDIO_STREAM_MUSIC, 50, 100));
186 EXPECT_EQ(handler_.SetVolumeIndex(
187 AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADSET, 100),
188 0);
189 EXPECT_EQ(handler_.SetVolumeIndex(
190 AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADSET, -1),
191 EINVAL);
192 EXPECT_EQ(handler_.SetVolumeIndex(
193 AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADSET, 101),
194 EINVAL);
195 EXPECT_EQ(handler_.SetVolumeMaxSteps(AUDIO_STREAM_MUSIC, 10), 0);
196 EXPECT_EQ(handler_.GetVolumeIndex(AUDIO_STREAM_MUSIC,
197 AUDIO_DEVICE_OUT_WIRED_HEADSET),
198 10);
199 EXPECT_EQ(handler_.SetVolumeIndex(
200 AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADSET, 11),
201 EINVAL);
202 EXPECT_CALL(handler_, TriggerCallback(AUDIO_STREAM_MUSIC, 100, 50));
203 EXPECT_EQ(handler_.SetVolumeIndex(
204 AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADSET, 5),
205 0);
206 EXPECT_EQ(handler_.SetVolumeMaxSteps(AUDIO_STREAM_MUSIC, 20), 0);
207 EXPECT_EQ(handler_.GetVolumeIndex(AUDIO_STREAM_MUSIC,
208 AUDIO_DEVICE_OUT_WIRED_HEADSET),
209 10);
210 }
211
212 } // namespace brillo
213