summaryrefslogtreecommitdiffstats
path: root/chromeos/audio/audio_devices_pref_handler_impl_unittest.cc
blob: dea358f507d141001329178f86f545b5bfd1e604 (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
// Copyright 2014 The Chromium 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 "chromeos/audio/audio_devices_pref_handler_impl.h"

#include <stdint.h>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/prefs/testing_pref_service.h"
#include "chromeos/audio/audio_device.h"
#include "chromeos/audio/audio_devices_pref_handler.h"
#include "chromeos/chromeos_pref_names.h"
#include "chromeos/dbus/audio_node.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chromeos {

const uint64_t kInternalMicId = 10003;
const uint64_t kHeadphoneId = 10002;
const uint64_t kHDMIOutputId = 10006;
const uint64_t kOtherTypeOutputId = 90001;
const uint64_t kOtherTypeInputId = 90002;

const AudioDevice kInternalMic(AudioNode(true,
                                         kInternalMicId,
                                         kInternalMicId,
                                         "Fake Mic",
                                         "INTERNAL_MIC",
                                         "Internal Mic",
                                         false,
                                         0));

const AudioDevice kHeadphone(AudioNode(false,
                                       kHeadphoneId,
                                       kHeadphoneId,
                                       "Fake Headphone",
                                       "HEADPHONE",
                                       "Headphone",
                                       false,
                                       0));

const AudioDevice kHDMIOutput(AudioNode(false,
                                        kHDMIOutputId,
                                        kHDMIOutputId,
                                        "HDMI output",
                                        "HDMI",
                                        "HDMI output",
                                        false,
                                        0));

const AudioDevice kInputDeviceWithSpecialCharacters(
    AudioNode(true,
              kOtherTypeInputId,
              kOtherTypeInputId,
              "Fake ~!@#$%^&*()_+`-=<>?,./{}|[]\\\\Mic",
              "SOME_OTHER_TYPE",
              "Other Type Input Device",
              true,
              0));

const AudioDevice kOutputDeviceWithSpecialCharacters(
    AudioNode(false,
              kOtherTypeOutputId,
              kOtherTypeOutputId,
              "Fake ~!@#$%^&*()_+`-=<>?,./{}|[]\\\\Headphone",
              "SOME_OTHER_TYPE",
              "Other Type Output Device",
              false,
              0));

class AudioDevicesPrefHandlerTest : public testing::Test {
 public:
  AudioDevicesPrefHandlerTest() {}
  ~AudioDevicesPrefHandlerTest() override {}

  void SetUp() override {
    pref_service_.reset(new TestingPrefServiceSimple());
    AudioDevicesPrefHandlerImpl::RegisterPrefs(pref_service_->registry());
    audio_pref_handler_ = new AudioDevicesPrefHandlerImpl(pref_service_.get());
  }

  void TearDown() override { audio_pref_handler_ = NULL; }

 protected:
  scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler_;
  scoped_ptr<TestingPrefServiceSimple> pref_service_;

 private:
  DISALLOW_COPY_AND_ASSIGN(AudioDevicesPrefHandlerTest);
};

TEST_F(AudioDevicesPrefHandlerTest, TestDefaultValues) {
  // TODO(rkc): Once the bug with default preferences is fixed, fix this test
  // also. http://crbug.com/442489
  EXPECT_EQ(75.0, audio_pref_handler_->GetInputGainValue(&kInternalMic));
  EXPECT_EQ(75.0, audio_pref_handler_->GetOutputVolumeValue(&kHeadphone));
  EXPECT_EQ(75.0, audio_pref_handler_->GetOutputVolumeValue(&kHDMIOutput));
  EXPECT_EQ(AUDIO_STATE_NOT_AVAILABLE,
            audio_pref_handler_->GetDeviceState(kInternalMic));
  EXPECT_EQ(AUDIO_STATE_NOT_AVAILABLE,
            audio_pref_handler_->GetDeviceState(kHeadphone));
  EXPECT_EQ(AUDIO_STATE_NOT_AVAILABLE,
            audio_pref_handler_->GetDeviceState(kHDMIOutput));
}

TEST_F(AudioDevicesPrefHandlerTest, PrefsRegistered) {
  // The standard audio prefs are registered.
  EXPECT_TRUE(pref_service_->FindPreference(prefs::kAudioDevicesVolumePercent));
  EXPECT_TRUE(pref_service_->FindPreference(prefs::kAudioDevicesMute));
  EXPECT_TRUE(pref_service_->FindPreference(prefs::kAudioOutputAllowed));
  EXPECT_TRUE(pref_service_->FindPreference(prefs::kAudioVolumePercent));
  EXPECT_TRUE(pref_service_->FindPreference(prefs::kAudioMute));
  EXPECT_TRUE(pref_service_->FindPreference(prefs::kAudioDevicesState));
}

TEST_F(AudioDevicesPrefHandlerTest, TestBasicInputOutputDevices) {
  audio_pref_handler_->SetVolumeGainValue(kInternalMic, 13.37);
  EXPECT_EQ(13.37, audio_pref_handler_->GetInputGainValue(&kInternalMic));
  audio_pref_handler_->SetVolumeGainValue(kHeadphone, 47.28);
  EXPECT_EQ(47.28, audio_pref_handler_->GetOutputVolumeValue(&kHeadphone));
}

TEST_F(AudioDevicesPrefHandlerTest, TestSpecialCharactersInDeviceNames) {
  audio_pref_handler_->SetVolumeGainValue(kInputDeviceWithSpecialCharacters,
                                          73.31);
  audio_pref_handler_->SetVolumeGainValue(kOutputDeviceWithSpecialCharacters,
                                          85.92);

  EXPECT_EQ(73.31, audio_pref_handler_->GetInputGainValue(
      &kInputDeviceWithSpecialCharacters));
  EXPECT_EQ(85.92, audio_pref_handler_->GetOutputVolumeValue(
      &kOutputDeviceWithSpecialCharacters));
}

TEST_F(AudioDevicesPrefHandlerTest, TestDeviceStates) {
  audio_pref_handler_->SetDeviceState(kInternalMic, AUDIO_STATE_NOT_AVAILABLE);
  EXPECT_EQ(AUDIO_STATE_NOT_AVAILABLE,
            audio_pref_handler_->GetDeviceState(kInternalMic));
  audio_pref_handler_->SetDeviceState(kHeadphone, AUDIO_STATE_ACTIVE);
  EXPECT_EQ(AUDIO_STATE_ACTIVE,
            audio_pref_handler_->GetDeviceState(kHeadphone));
  audio_pref_handler_->SetDeviceState(kHDMIOutput, AUDIO_STATE_INACTIVE);
  EXPECT_EQ(AUDIO_STATE_INACTIVE,
            audio_pref_handler_->GetDeviceState(kHDMIOutput));
}

}  // namespace chromeos