Home | History | Annotate | Download | only in win
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <string>
      6 
      7 #include "base/bind.h"
      8 #include "base/bind_helpers.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/strings/utf_string_conversions.h"
     11 #include "base/win/scoped_com_initializer.h"
     12 #include "media/audio/audio_manager.h"
     13 #include "media/audio/win/audio_device_listener_win.h"
     14 #include "media/audio/win/core_audio_util_win.h"
     15 #include "testing/gmock/include/gmock/gmock.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 
     18 using base::win::ScopedCOMInitializer;
     19 
     20 namespace media {
     21 
     22 static const char kNoDevice[] = "";
     23 static const char kFirstTestDevice[] = "test_device_0";
     24 static const char kSecondTestDevice[] = "test_device_1";
     25 
     26 class AudioDeviceListenerWinTest : public testing::Test {
     27  public:
     28   AudioDeviceListenerWinTest()
     29       : com_init_(ScopedCOMInitializer::kMTA) {
     30   }
     31 
     32   virtual void SetUp() {
     33     if (!CoreAudioUtil::IsSupported())
     34       return;
     35 
     36     output_device_listener_.reset(new AudioDeviceListenerWin(base::Bind(
     37         &AudioDeviceListenerWinTest::OnDeviceChange, base::Unretained(this))));
     38   }
     39 
     40   // Simulate a device change where no output devices are available.
     41   bool SimulateNullDefaultOutputDeviceChange() {
     42     return output_device_listener_->OnDefaultDeviceChanged(
     43         static_cast<EDataFlow>(eConsole), static_cast<ERole>(eRender),
     44         NULL) == S_OK;
     45   }
     46 
     47   bool SimulateDefaultOutputDeviceChange(const char* new_device_id) {
     48     return output_device_listener_->OnDefaultDeviceChanged(
     49         static_cast<EDataFlow>(eConsole), static_cast<ERole>(eRender),
     50         base::ASCIIToWide(new_device_id).c_str()) == S_OK;
     51   }
     52 
     53   void SetOutputDeviceId(std::string new_device_id) {
     54     output_device_listener_->default_render_device_id_ = new_device_id;
     55   }
     56 
     57   MOCK_METHOD0(OnDeviceChange, void());
     58 
     59  private:
     60   ScopedCOMInitializer com_init_;
     61   scoped_ptr<AudioDeviceListenerWin> output_device_listener_;
     62 
     63   DISALLOW_COPY_AND_ASSIGN(AudioDeviceListenerWinTest);
     64 };
     65 
     66 // Simulate a device change events and ensure we get the right callbacks.
     67 TEST_F(AudioDeviceListenerWinTest, OutputDeviceChange) {
     68   if (!CoreAudioUtil::IsSupported())
     69     return;
     70 
     71   SetOutputDeviceId(kNoDevice);
     72   EXPECT_CALL(*this, OnDeviceChange()).Times(1);
     73   ASSERT_TRUE(SimulateDefaultOutputDeviceChange(kFirstTestDevice));
     74 
     75   testing::Mock::VerifyAndClear(this);
     76   EXPECT_CALL(*this, OnDeviceChange()).Times(1);
     77   ASSERT_TRUE(SimulateDefaultOutputDeviceChange(kSecondTestDevice));
     78 
     79   // The second device event should be ignored since the device id has not
     80   // changed.
     81   ASSERT_TRUE(SimulateDefaultOutputDeviceChange(kSecondTestDevice));
     82 }
     83 
     84 // Ensure that null output device changes don't crash.  Simulates the situation
     85 // where we have no output devices.
     86 TEST_F(AudioDeviceListenerWinTest, NullOutputDeviceChange) {
     87   if (!CoreAudioUtil::IsSupported())
     88     return;
     89 
     90   SetOutputDeviceId(kNoDevice);
     91   EXPECT_CALL(*this, OnDeviceChange()).Times(0);
     92   ASSERT_TRUE(SimulateNullDefaultOutputDeviceChange());
     93 
     94   testing::Mock::VerifyAndClear(this);
     95   EXPECT_CALL(*this, OnDeviceChange()).Times(1);
     96   ASSERT_TRUE(SimulateDefaultOutputDeviceChange(kFirstTestDevice));
     97 
     98   testing::Mock::VerifyAndClear(this);
     99   EXPECT_CALL(*this, OnDeviceChange()).Times(1);
    100   ASSERT_TRUE(SimulateNullDefaultOutputDeviceChange());
    101 }
    102 
    103 }  // namespace media
    104