| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- // -----------------------------------------
- // SoundScribe (TM) and related software.
- //
- // Copyright (C) 2007-2011 Vannatech
- // http://www.vannatech.com
- // All rights reserved.
- //
- // This source code is subject to the MIT License.
- // http://www.opensource.org/licenses/mit-license.php
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- // -----------------------------------------
- using System;
- using CoreAudioTests.Common;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using Vannatech.CoreAudio.Interfaces;
- using Vannatech.CoreAudio.Constants;
- using Vannatech.CoreAudio.Structures;
- namespace CoreAudioTests.EndpointVolumeApi
- {
- /// <summary>
- /// Tests all methods of the IAudioEndpointVolume interface.
- /// </summary>
- [TestClass]
- public class IAudioEndpointVolumeTest : TestClass<IAudioEndpointVolume>
- {
- /// <summary>
- /// Tests that the channel count can be received for each available endpoint, and an HRESULT of S_OK is returned.
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_GetChannelCount()
- {
- ExecuteDeviceActivationTest(activation =>
- {
- var count = UInt32.MaxValue;
- var result = activation.GetChannelCount(out count);
- AssertCoreAudio.IsHResultOk(result);
- Assert.AreNotEqual(uint.MaxValue, count, "The channel count value was not received.");
- });
- }
- /// <summary>
- /// Tests that the volume level can be obtained for each available endpoint channel.
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_GetChannelVolumeLevel()
- {
- var tested = false;
- ExecuteDeviceActivationTest(activation =>
- {
- var count = UInt32.MaxValue;
- activation.GetChannelCount(out count);
- for (uint i = 0; i < count; i++)
- {
- float level = 123.456f;
- var result = activation.GetChannelVolumeLevel(i, out level);
- AssertCoreAudio.IsHResultOk(result);
- Assert.AreNotEqual(123.456f, level, "The level value was not received.");
- tested = true;
- }
- });
- if (!tested) Assert.Inconclusive("No channels were available to test against.");
- }
- /// <summary>
- /// Tests that the scalar volume level can be obtained for each available endpoint channel.
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_GetChannelVolumeLevelScalar()
- {
- var tested = false;
- ExecuteDeviceActivationTest(activation =>
- {
- var count = UInt32.MaxValue;
- activation.GetChannelCount(out count);
- for (uint i = 0; i < count; i++)
- {
- float level = 123.456f;
- var result = activation.GetChannelVolumeLevelScalar(i, out level);
- AssertCoreAudio.IsHResultOk(result);
- Assert.AreNotEqual(123.456f, level, "The level value was not received.");
- tested = true;
- }
- });
- if (!tested) Assert.Inconclusive("No channels were available to test against.");
- }
- /// <summary>
- /// Tests that the master volume level can be obtained for each available endpoint.
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_GetMasterVolumeLevel()
- {
- ExecuteDeviceActivationTest(activation =>
- {
- float level = 123.456f;
- var result = activation.GetMasterVolumeLevel(out level);
- AssertCoreAudio.IsHResultOk(result);
- Assert.AreNotEqual(123.456f, level, "The level value was not received.");
- });
- }
- /// <summary>
- /// Tests that the master scalar volume level can be obtained for each available endpoint.
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_GetMasterVolumeLevelScalar()
- {
- ExecuteDeviceActivationTest(activation =>
- {
- float level = 123.456f;
- var result = activation.GetMasterVolumeLevelScalar(out level);
- AssertCoreAudio.IsHResultOk(result);
- Assert.AreNotEqual(123.456f, level, "The level value was not received.");
- });
- }
- /// <summary>
- /// Tests that the muting state can be received for each available endpoint, with an HRESULT of S_OK returned.
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_GetMute()
- {
- ExecuteDeviceActivationTest(activation =>
- {
- bool valOne = true, valTwo = false;
-
- var result = activation.GetMute(out valOne);
- AssertCoreAudio.IsHResultOk(result);
- result = activation.GetMute(out valTwo);
- AssertCoreAudio.IsHResultOk(result);
- Assert.AreEqual(valOne, valTwo, "The mute state was not received.");
- });
- }
- /// <summary>
- /// Tests that the volume ranges for each available endpoint can be received.
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_GetVolumeRange()
- {
- ExecuteDeviceActivationTest(activation =>
- {
- float volumeMin = 123.456f, volumeMax = 123.456f, volumeStep = 123.456f;
- var result = activation.GetVolumeRange(out volumeMin, out volumeMax, out volumeStep);
- AssertCoreAudio.IsHResultOk(result);
- Assert.AreNotEqual(123.456f, volumeMin, "The min volume value was not received.");
- Assert.AreNotEqual(123.456f, volumeMax, "The max volume value was not received.");
- Assert.AreNotEqual(123.456f, volumeStep, "The volume step value was not received.");
- });
- }
-
- /// <summary>
- /// Tests that the volume step info for each available endpoint can be received.
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_GetVolumeStepInfo()
- {
- ExecuteDeviceActivationTest(activation =>
- {
- var step = UInt32.MaxValue;
- var stepCount = UInt32.MaxValue;
- var result = activation.GetVolumeStepInfo(out step, out stepCount);
- AssertCoreAudio.IsHResultOk(result);
- Assert.AreNotEqual(UInt32.MaxValue, step, "The step value was not received.");
- Assert.AreNotEqual(UInt32.MaxValue, stepCount, "The step count value was not received.");
- });
- }
- /// <summary>
- /// Tests that the hardware support flags for each available endpoint can be received, and are within a valid range.
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_QueryHardwareSupport()
- {
- ExecuteDeviceActivationTest(activation =>
- {
- var mask = UInt32.MaxValue;
- var result = activation.QueryHardwareSupport(out mask);
- AssertCoreAudio.IsHResultOk(result);
- Assert.IsTrue((mask >= 0) && (mask <= 7), "The hardware mask is not in the valid range.");
- });
- }
- /// <summary>
- ///
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_RegisterControlChangeNotify()
- {
- ExecuteDeviceActivationTest(activation =>
- {
- var client = new AudioEndpointVolumeCallback();
- var result = activation.RegisterControlChangeNotify(client);
- AssertCoreAudio.IsHResultOk(result);
- });
- }
- /// <summary>
- /// Tests that the volume level can be set for each channel on each available endpoint.
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_SetChannelVolumeLevel()
- {
- var tested = false;
- ExecuteDeviceActivationTest(activation =>
- {
- UInt32 count;
- activation.GetChannelCount(out count);
- for (uint i = 0; i < count; i++)
- {
- // determine valid range.
- float volumeMin, volumeMax, volumeStep;
- activation.GetVolumeRange(out volumeMin, out volumeMax, out volumeStep);
- // get the original level.
- float levelOrig, levelNew, levelCheck;
- activation.GetChannelVolumeLevel(i, out levelOrig);
- // create a random valid level
- var rand = new Random();
- levelNew = (float)rand.Next((int)volumeMin, (int)volumeMax);
- // set the new level.
- Guid context = Guid.NewGuid();
- var result = activation.SetChannelVolumeLevel(i, levelNew, context);
- AssertCoreAudio.IsHResultOk(result);
- // check that the level was set.
- activation.GetChannelVolumeLevel(i, out levelCheck);
- Assert.AreEqual(levelNew, levelCheck, volumeStep, "The new volume level was not set properly.");
- // reset the level to the original.
- result = activation.SetChannelVolumeLevel(i, levelOrig, context);
- AssertCoreAudio.IsHResultOk(result);
- tested = true;
- }
- });
-
- if(!tested) Assert.Inconclusive("No channels were available to test against.");
- }
- /// <summary>
- /// Tests that the scalar volume level can be set for each channel on each available endpoint.
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_SetChannelVolumeLevelScalar()
- {
- var tested = false;
- ExecuteDeviceActivationTest(activation =>
- {
- UInt32 count;
- activation.GetChannelCount(out count);
- for (uint i = 0; i < count; i++)
- {
- // get the original level.
- float levelOrig, levelNew, levelCheck;
- activation.GetChannelVolumeLevelScalar(i, out levelOrig);
- // create a random valid level
- var rand = new Random();
- levelNew = (float)rand.NextDouble();
- // set the new level.
- Guid context = Guid.NewGuid();
- var result = activation.SetChannelVolumeLevelScalar(i, levelNew, context);
- AssertCoreAudio.IsHResultOk(result);
- // check that the level was set.
- activation.GetChannelVolumeLevelScalar(i, out levelCheck);
- Assert.AreEqual(levelNew, levelCheck, 0.001f, "The new volume level was not set properly.");
- // reset the level to the original.
- result = activation.SetChannelVolumeLevelScalar(i, levelOrig, context);
- AssertCoreAudio.IsHResultOk(result);
- tested = true;
- }
- });
- if (!tested) Assert.Inconclusive("No channels were available to test against.");
- }
- /// <summary>
- /// Tests that the master volume level can be set for each available endpoint.
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_SetMasterVolumeLevel()
- {
- ExecuteDeviceActivationTest(activation =>
- {
- // determine valid range.
- float volumeMin, volumeMax, volumeStep;
- activation.GetVolumeRange(out volumeMin, out volumeMax, out volumeStep);
- // get the original level.
- float levelOrig, levelNew, levelCheck;
- activation.GetMasterVolumeLevel(out levelOrig);
- // create a random valid level
- var rand = new Random();
- levelNew = (float)rand.Next((int)volumeMin, (int)volumeMax);
- // set the new level.
- Guid context = Guid.NewGuid();
- var result = activation.SetMasterVolumeLevel(levelNew, context);
- AssertCoreAudio.IsHResultOk(result);
- // check that the level was set.
- activation.GetMasterVolumeLevel(out levelCheck);
- Assert.AreEqual(levelNew, levelCheck, volumeStep, "The new volume level was not set properly.");
- // reset the level to the original.
- result = activation.SetMasterVolumeLevel(levelOrig, context);
- });
- }
- /// <summary>
- /// Tests that the master scalar volume level can be set for each available endpoint.
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_SetMasterVolumeLevelScalar()
- {
- ExecuteDeviceActivationTest(activation =>
- {
- // get the original level.
- float levelOrig, levelNew, levelCheck;
- activation.GetMasterVolumeLevelScalar(out levelOrig);
- // create a random valid level
- var rand = new Random();
- levelNew = (float)rand.NextDouble();
- // set the new level.
- Guid context = Guid.NewGuid();
- var result = activation.SetMasterVolumeLevelScalar(levelNew, context);
- AssertCoreAudio.IsHResultOk(result);
- // check that the level was set.
- activation.GetMasterVolumeLevelScalar(out levelCheck);
- Assert.AreEqual(levelNew, levelCheck, 0.001f, "The new volume level was not set properly.");
- // reset the level to the original.
- result = activation.SetMasterVolumeLevelScalar(levelOrig, context);
- });
- }
- /// <summary>
- /// Tests that the muting state can be set for each available endpoint.
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_SetMute()
- {
- ExecuteDeviceActivationTest(activation =>
- {
- // get the original mute state.
- bool muteOrig, muteNew, muteCheck;
- activation.GetMute(out muteOrig);
- muteNew = !muteOrig;
- // change it to the opposite.
- Guid context = Guid.NewGuid();
- var result = activation.SetMute(muteNew, context);
- AssertCoreAudio.IsHResultOk(result);
- // check that the value changed.
- activation.GetMute(out muteCheck);
- Assert.AreEqual(muteNew, muteCheck, "The muting state was not set properly.");
- // return to original state.
- result = activation.SetMute(muteOrig, context);
- AssertCoreAudio.IsHResultOk(result);
- });
- }
- /// <summary>
- ///
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_UnregisterControlChangeNotify()
- {
- ExecuteDeviceActivationTest(activation =>
- {
- var client = new AudioEndpointVolumeCallback();
- activation.RegisterControlChangeNotify(client);
- var result = activation.UnregisterControlChangeNotify(client);
- AssertCoreAudio.IsHResultOk(result);
- });
- }
- /// <summary>
- /// Tests that the volume step down decrements the step by one, for each available endpoint.
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_VolumeStepDown()
- {
- ExecuteDeviceActivationTest(activation =>
- {
- // get the original value.
- float levelOrig;
- activation.GetMasterVolumeLevelScalar(out levelOrig);
- // set to a volume in the middle.
- Guid context = Guid.NewGuid();
- activation.SetMasterVolumeLevelScalar(0.5f, context);
- // get the initial step info
- UInt32 step, stepCount;
- activation.GetVolumeStepInfo(out step, out stepCount);
- // determine valid number of steps to take.
- uint stepsToTake = (uint)Math.Floor(stepCount / 2.0f);
- while (stepsToTake > 0)
- {
- // step down the volume.
- var result = activation.VolumeStepDown(context);
- AssertCoreAudio.IsHResultOk(result);
- // check that the volume step decremented one.
- var previousStep = step;
- activation.GetVolumeStepInfo(out step, out stepCount);
- Assert.AreEqual(previousStep - 1, step, "The volume step did not decrement.");
- stepsToTake--;
- }
- // reset the volume level to original value.
- activation.SetMasterVolumeLevelScalar(levelOrig, context);
- });
- }
- /// <summary>
- /// Tests that the volume step up increments the step by one, for each available endpoint.
- /// </summary>
- [TestMethod]
- public void IAudioEndpointVolume_VolumeStepUp()
- {
- ExecuteDeviceActivationTest(activation =>
- {
- // get the original value.
- float levelOrig;
- activation.GetMasterVolumeLevelScalar(out levelOrig);
- // set to a volume in the middle.
- Guid context = Guid.NewGuid();
- activation.SetMasterVolumeLevelScalar(0.5f, context);
- // get the initial step info
- UInt32 step, stepCount;
- activation.GetVolumeStepInfo(out step, out stepCount);
- // determine valid number of steps to take.
- uint stepsToTake = (uint)Math.Floor(stepCount / 2.0f);
- while (stepsToTake > 0)
- {
- // step up the volume.
- var result = activation.VolumeStepUp(context);
- AssertCoreAudio.IsHResultOk(result);
- // check that the volume step decremented one.
- uint previousStep = step;
- activation.GetVolumeStepInfo(out step, out stepCount);
- Assert.AreEqual(previousStep + 1, step, "The volume step did not increment.");
- stepsToTake--;
- }
- // reset the volume level to original value.
- activation.SetMasterVolumeLevelScalar(levelOrig, context);
- });
- }
- }
- }
|