Skip to content

Commit

Permalink
Add test for Model and Recording Devices. See #19
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Aflalo committed Sep 2, 2015
1 parent 7ca5f45 commit 656de9f
Show file tree
Hide file tree
Showing 6 changed files with 423 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

using System;
using System.Collections.Generic;
using System.Reflection;
using AudioEndPointControllerWrapper;
using Moq;
using NUnit.Framework;
Expand All @@ -24,25 +23,19 @@
namespace SoundSwitch.Tests
{
[TestFixture]
public class ModelUnitTest
public class ModelPlaybackDeviceTests
{
private static void SetConfigurationMoq(IMock<ISoundSwitchConfiguration> configurationMoq)
{
var fieldInfo = typeof (AppConfigs).GetRuntimeProperty("Configuration");
fieldInfo.SetValue(null, configurationMoq.Object);
}

[Test]
public void TestAddingPlaybackDeviceToSelected()
{
var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"};
var audioMoqI = new Mock<IAudioDevice> { Name = "first audio dev" };
var audioMoqI = new Mock<IAudioDevice> {Name = "first audio dev"};
audioMoqI.SetupGet(a => a.FriendlyName).Returns("Speakers (Test device)");
audioMoqI.SetupGet(a => a.Type).Returns(AudioDeviceType.Playback);

//Setup
configurationMoq.Setup(c => c.SelectedPlaybackDeviceList).Returns(new HashSet<string>());
SetConfigurationMoq(configurationMoq);
TestHelpers.SetConfigurationMoq(configurationMoq);

//Action
var eventCalled = false;
Expand All @@ -63,13 +56,14 @@ public void TestAddingPlaybackDeviceToSelected()
public void TestAddingAlreadyPresentPlaybackDeviceToSelected()
{
var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"};
var audioMoqI = new Mock<IAudioDevice> { Name = "first audio dev" };
var audioMoqI = new Mock<IAudioDevice> {Name = "first audio dev"};
audioMoqI.SetupGet(a => a.FriendlyName).Returns("Speakers (Test device)");
audioMoqI.SetupGet(a => a.Type).Returns(AudioDeviceType.Playback);

//Setup
configurationMoq.Setup(c => c.SelectedPlaybackDeviceList).Returns(new HashSet<string> { "Speakers (Test device)" });
SetConfigurationMoq(configurationMoq);
configurationMoq.Setup(c => c.SelectedPlaybackDeviceList)
.Returns(new HashSet<string> {"Speakers (Test device)"});
TestHelpers.SetConfigurationMoq(configurationMoq);

//Action
var eventCalled = false;
Expand All @@ -88,13 +82,14 @@ public void TestAddingAlreadyPresentPlaybackDeviceToSelected()
public void TestRemovePresentPlaybackDevice()
{
var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"};
var audioMoqI = new Mock<IAudioDevice> { Name = "first audio dev" };
var audioMoqI = new Mock<IAudioDevice> {Name = "first audio dev"};
audioMoqI.SetupGet(a => a.FriendlyName).Returns("Speakers (Test device)");
audioMoqI.SetupGet(a => a.Type).Returns(AudioDeviceType.Playback);

//Setup
configurationMoq.Setup(c => c.SelectedPlaybackDeviceList).Returns(new HashSet<string> { "Speakers (Test device)" });
SetConfigurationMoq(configurationMoq);
configurationMoq.Setup(c => c.SelectedPlaybackDeviceList)
.Returns(new HashSet<string> {"Speakers (Test device)"});
TestHelpers.SetConfigurationMoq(configurationMoq);

//Action
var eventCalled = false;
Expand All @@ -114,13 +109,13 @@ public void TestRemovePresentPlaybackDevice()
public void TestRemoveNotPresentPlaybackDevice()
{
var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"};
var audioMoqI = new Mock<IAudioDevice> { Name = "first audio dev" };
var audioMoqI = new Mock<IAudioDevice> {Name = "first audio dev"};
audioMoqI.SetupGet(a => a.FriendlyName).Returns("Speakers (Test device)");
audioMoqI.SetupGet(a => a.Type).Returns(AudioDeviceType.Playback);

//Setup
configurationMoq.Setup(c => c.SelectedPlaybackDeviceList).Returns(new HashSet<string>());
SetConfigurationMoq(configurationMoq);
TestHelpers.SetConfigurationMoq(configurationMoq);

//Action
var eventCalled = false;
Expand All @@ -135,7 +130,7 @@ public void TestRemoveNotPresentPlaybackDevice()
}

[Test]
public void TestUnionSelectedDeviceWithActiveDevice()
public void TestUnionSelectedPlaybackDeviceWithActiveDevice()
{
var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"};

Expand All @@ -149,8 +144,9 @@ public void TestUnionSelectedDeviceWithActiveDevice()
.Returns(new List<IAudioDevice> {audioMoqI.Object, audioMoqII.Object});

//Setup
configurationMoq.Setup(c => c.SelectedPlaybackDeviceList).Returns(new HashSet<string> { "Speakers (Test device)" });
SetConfigurationMoq(configurationMoq);
configurationMoq.Setup(c => c.SelectedPlaybackDeviceList)
.Returns(new HashSet<string> {"Speakers (Test device)"});
TestHelpers.SetConfigurationMoq(configurationMoq);
AppModel.Instance.ActiveAudioDeviceLister = listerMoq.Object;

//Action
Expand All @@ -167,28 +163,29 @@ public void TestUnionSelectedDeviceWithActiveDevice()
[Test]
public void TestCycleConsoleAudioDevice()
{
var configurationMoq = new Mock<ISoundSwitchConfiguration> { Name = "Configuration mock" };
var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"};
configurationMoq.SetupGet(conf => conf.LastPlaybackActive).Returns("");

var audioMoqI = new Mock<IAudioDevice> { Name = "first audio dev" };
var audioMoqI = new Mock<IAudioDevice> {Name = "first audio dev"};
audioMoqI.SetupGet(a => a.FriendlyName).Returns("Speakers (Test device)");
audioMoqI.Setup(a => a.IsDefault(It.Is<Role>(role => role == Role.Console))).Returns(true).Verifiable();
var audioMoqII = new Mock<IAudioDevice> { Name = "secound audio dev" };
var audioMoqII = new Mock<IAudioDevice> {Name = "secound audio dev"};
audioMoqII.SetupGet(a => a.FriendlyName).Returns("Headphones (Test device)");

var listerMoq = new Mock<IAudioDeviceLister> { Name = "Lister" };
var listerMoq = new Mock<IAudioDeviceLister> {Name = "Lister"};
listerMoq.Setup(lister => lister.GetPlaybackDevices())
.Returns(new List<IAudioDevice> { audioMoqI.Object, audioMoqII.Object });
.Returns(new List<IAudioDevice> {audioMoqI.Object, audioMoqII.Object});

//Setup
configurationMoq.Setup(c => c.SelectedPlaybackDeviceList).Returns(new HashSet<string> { "Speakers (Test device)", "Headphones (Test device)" });
SetConfigurationMoq(configurationMoq);
configurationMoq.Setup(c => c.SelectedPlaybackDeviceList)
.Returns(new HashSet<string> {"Speakers (Test device)", "Headphones (Test device)"});
TestHelpers.SetConfigurationMoq(configurationMoq);
AppModel.Instance.ActiveAudioDeviceLister = listerMoq.Object;
IAudioDevice audioDevice = null;
AppModel.Instance.DefaultDeviceChanged += (sender, @event) => audioDevice = @event.AudioDevice;

//Action
Assert.That(AppModel.Instance.CycleActiveDevice());
Assert.That(AppModel.Instance.CycleActiveDevice(AudioDeviceType.Playback));

//Asserts
configurationMoq.VerifyGet(c => c.SelectedPlaybackDeviceList);
Expand All @@ -204,30 +201,33 @@ public void TestCycleConsoleAudioDevice()
[Test]
public void TestCycleCommunicationsAudioDevice()
{
var configurationMoq = new Mock<ISoundSwitchConfiguration> { Name = "Configuration mock" };
var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"};
configurationMoq.SetupGet(conf => conf.LastPlaybackActive).Returns("");
configurationMoq.SetupGet(conf => conf.ChangeCommunications).Returns(true);

var audioMoqI = new Mock<IAudioDevice> { Name = "first audio dev" };
var audioMoqI = new Mock<IAudioDevice> {Name = "first audio dev"};
audioMoqI.SetupGet(a => a.FriendlyName).Returns("Speakers (Test device)");
audioMoqI.Setup(a => a.IsDefault(It.Is<Role>(role => role == Role.Console))).Returns(true).Verifiable();
audioMoqI.Setup(a => a.IsDefault(It.Is<Role>(role => role == Role.Communications))).Returns(true).Verifiable();
var audioMoqII = new Mock<IAudioDevice> { Name = "secound audio dev" };
audioMoqI.Setup(a => a.IsDefault(It.Is<Role>(role => role == Role.Communications)))
.Returns(true)
.Verifiable();
var audioMoqII = new Mock<IAudioDevice> {Name = "secound audio dev"};
audioMoqII.SetupGet(a => a.FriendlyName).Returns("Headphones (Test device)");

var listerMoq = new Mock<IAudioDeviceLister> { Name = "Lister" };
var listerMoq = new Mock<IAudioDeviceLister> {Name = "Lister"};
listerMoq.Setup(lister => lister.GetPlaybackDevices())
.Returns(new List<IAudioDevice> { audioMoqI.Object, audioMoqII.Object });
.Returns(new List<IAudioDevice> {audioMoqI.Object, audioMoqII.Object});

//Setup
configurationMoq.Setup(c => c.SelectedPlaybackDeviceList).Returns(new HashSet<string> { "Speakers (Test device)", "Headphones (Test device)" });
SetConfigurationMoq(configurationMoq);
configurationMoq.Setup(c => c.SelectedPlaybackDeviceList)
.Returns(new HashSet<string> {"Speakers (Test device)", "Headphones (Test device)"});
TestHelpers.SetConfigurationMoq(configurationMoq);
AppModel.Instance.ActiveAudioDeviceLister = listerMoq.Object;
IAudioDevice audioDevice = null;
AppModel.Instance.DefaultDeviceChanged += (sender, @event) => audioDevice = @event.AudioDevice;

//Action
Assert.That(AppModel.Instance.CycleActiveDevice());
Assert.That(AppModel.Instance.CycleActiveDevice(AudioDeviceType.Playback));

//Asserts
configurationMoq.VerifyGet(c => c.SelectedPlaybackDeviceList);
Expand All @@ -244,28 +244,29 @@ public void TestCycleCommunicationsAudioDevice()
[Test]
public void TestCycleAudioDeviceIsACycleThatReturnToFirstWhenReachEnd()
{
var configurationMoq = new Mock<ISoundSwitchConfiguration> { Name = "Configuration mock" };
var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"};
configurationMoq.SetupGet(conf => conf.LastPlaybackActive).Returns("");

var audioMoqI = new Mock<IAudioDevice> { Name = "first audio dev" };
var audioMoqI = new Mock<IAudioDevice> {Name = "first audio dev"};
audioMoqI.SetupGet(a => a.FriendlyName).Returns("Speakers (Test device)");
var audioMoqII = new Mock<IAudioDevice> { Name = "secound audio dev" };
var audioMoqII = new Mock<IAudioDevice> {Name = "secound audio dev"};
audioMoqII.SetupGet(a => a.FriendlyName).Returns("Headphones (Test device)");
audioMoqII.Setup(a => a.IsDefault(It.Is<Role>(role => role == Role.Console))).Returns(true).Verifiable();

var listerMoq = new Mock<IAudioDeviceLister> { Name = "Lister" };
var listerMoq = new Mock<IAudioDeviceLister> {Name = "Lister"};
listerMoq.Setup(lister => lister.GetPlaybackDevices())
.Returns(new List<IAudioDevice> { audioMoqI.Object, audioMoqII.Object });
.Returns(new List<IAudioDevice> {audioMoqI.Object, audioMoqII.Object});

//Setup
configurationMoq.Setup(c => c.SelectedPlaybackDeviceList).Returns(new HashSet<string> { "Speakers (Test device)", "Headphones (Test device)" });
SetConfigurationMoq(configurationMoq);
configurationMoq.Setup(c => c.SelectedPlaybackDeviceList)
.Returns(new HashSet<string> {"Speakers (Test device)", "Headphones (Test device)"});
TestHelpers.SetConfigurationMoq(configurationMoq);
AppModel.Instance.ActiveAudioDeviceLister = listerMoq.Object;
IAudioDevice audioDevice = null;
AppModel.Instance.DefaultDeviceChanged += (sender, @event) => audioDevice = @event.AudioDevice;

//Action
Assert.That(AppModel.Instance.CycleActiveDevice());
Assert.That(AppModel.Instance.CycleActiveDevice(AudioDeviceType.Playback));

//Asserts
configurationMoq.VerifyGet(c => c.SelectedPlaybackDeviceList);
Expand All @@ -286,7 +287,7 @@ public void TestSetActiveDeviceNull()


//Setup
SetConfigurationMoq(configurationMoq);
TestHelpers.SetConfigurationMoq(configurationMoq);
bool deviceChangeEventCalled = false;
bool errorTriggeredEventCalled = false;
AppModel.Instance.DefaultDeviceChanged += (sender, @event) => deviceChangeEventCalled = true;
Expand All @@ -304,14 +305,15 @@ public void TestSetActiveDeviceNull()
[Test]
public void TestSetAudioDeviceSetAudioDeviceAsDefault()
{
var configurationMoq = new Mock<ISoundSwitchConfiguration> { Name = "Configuration mock" };
var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"};
configurationMoq.SetupGet(conf => conf.LastPlaybackActive).Returns("");

var audioMoqI = new Mock<IAudioDevice> { Name = "first audio dev" };
var audioMoqI = new Mock<IAudioDevice> {Name = "first audio dev"};
audioMoqI.SetupGet(a => a.FriendlyName).Returns("Speakers (Test device)");
audioMoqI.SetupGet(a => a.Type).Returns(AudioDeviceType.Playback);

//Setup
SetConfigurationMoq(configurationMoq);
TestHelpers.SetConfigurationMoq(configurationMoq);
IAudioDevice audioDevice = null;
AppModel.Instance.DefaultDeviceChanged += (sender, @event) => audioDevice = @event.AudioDevice;

Expand Down
Loading

0 comments on commit 656de9f

Please sign in to comment.