diff --git a/SoundSwitch.Tests/AppModelTests.cs b/SoundSwitch.Tests/PlaybackDevices.cs similarity index 80% rename from SoundSwitch.Tests/AppModelTests.cs rename to SoundSwitch.Tests/PlaybackDevices.cs index 841c0f7077..84f6f71495 100644 --- a/SoundSwitch.Tests/AppModelTests.cs +++ b/SoundSwitch.Tests/PlaybackDevices.cs @@ -14,7 +14,6 @@ using System; using System.Collections.Generic; -using System.Reflection; using AudioEndPointControllerWrapper; using Moq; using NUnit.Framework; @@ -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; @@ -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; @@ -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; @@ -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; @@ -135,7 +130,7 @@ public void TestRemoveNotPresentPlaybackDevice() } [Test] - public void TestUnionSelectedDeviceWithActiveDevice() + public void TestUnionSelectedPlaybackDeviceWithActiveDevice() { var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"}; @@ -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 @@ -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); @@ -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); @@ -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); @@ -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; @@ -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; diff --git a/SoundSwitch.Tests/RecordingDevices.cs b/SoundSwitch.Tests/RecordingDevices.cs new file mode 100644 index 0000000000..6cdb73e748 --- /dev/null +++ b/SoundSwitch.Tests/RecordingDevices.cs @@ -0,0 +1,336 @@ +/******************************************************************** +* Copyright (C) 2015 Antoine Aflalo +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +********************************************************************/ + +using System; +using System.Collections.Generic; +using AudioEndPointControllerWrapper; +using Moq; +using NUnit.Framework; +using SoundSwitch.Framework.Configuration; +using SoundSwitch.Model; + +namespace SoundSwitch.Tests +{ + [TestFixture] + public class ModelRecordingDeviceTests + { + [Test] + public void TestAddingRecordingDeviceToSelected() + { + var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"}; + 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.Recording); + + //Setup + configurationMoq.Setup(c => c.SelectedRecordingDeviceList).Returns(new HashSet<string>()); + TestHelpers.SetConfigurationMoq(configurationMoq); + + //Action + var eventCalled = false; + AppModel.Instance.SelectedDeviceChanged += (sender, changed) => eventCalled = true; + Assert.True(AppModel.Instance.SelectDevice(audioMoqI.Object)); + + //Asserts + configurationMoq.VerifyGet(c => c.SelectedRecordingDeviceList); + configurationMoq.Verify(c => c.Save()); + audioMoqI.VerifyGet(a => a.FriendlyName); + audioMoqI.VerifyGet(a => a.Type); + Assert.That(configurationMoq.Object.SelectedRecordingDeviceList.Count == 1); + Assert.That(configurationMoq.Object.SelectedRecordingDeviceList.Contains("Speakers (Test device)")); + Assert.That(eventCalled, "SelectedDeviceChanged not called"); + } + + [Test] + public void TestAddingAlreadyPresentRecordingDeviceToSelected() + { + var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"}; + 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.Recording); + + //Setup + configurationMoq.Setup(c => c.SelectedRecordingDeviceList) + .Returns(new HashSet<string> {"Speakers (Test device)"}); + TestHelpers.SetConfigurationMoq(configurationMoq); + + //Action + var eventCalled = false; + AppModel.Instance.SelectedDeviceChanged += (sender, changed) => eventCalled = true; + Assert.False(AppModel.Instance.SelectDevice(audioMoqI.Object)); + + //Asserts + configurationMoq.VerifyGet(c => c.SelectedRecordingDeviceList); + audioMoqI.VerifyGet(a => a.FriendlyName); + audioMoqI.VerifyGet(a => a.Type); + Assert.That(configurationMoq.Object.SelectedRecordingDeviceList.Count == 1); + Assert.That(!eventCalled, "SelectedDeviceChanged called"); + } + + [Test] + public void TestRemovePresentRecordingDevice() + { + var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"}; + 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.Recording); + + //Setup + configurationMoq.Setup(c => c.SelectedRecordingDeviceList) + .Returns(new HashSet<string> {"Speakers (Test device)"}); + TestHelpers.SetConfigurationMoq(configurationMoq); + + //Action + var eventCalled = false; + AppModel.Instance.SelectedDeviceChanged += (sender, changed) => eventCalled = true; + Assert.True(AppModel.Instance.UnselectDevice(audioMoqI.Object)); + + //Asserts + configurationMoq.VerifyGet(c => c.SelectedRecordingDeviceList); + configurationMoq.Verify(c => c.Save()); + audioMoqI.VerifyGet(a => a.FriendlyName); + audioMoqI.VerifyGet(a => a.Type); + Assert.That(configurationMoq.Object.SelectedRecordingDeviceList.Count == 0); + Assert.That(eventCalled, "SelectedDeviceChanged not called"); + } + + [Test] + public void TestRemoveNotPresentRecordingDevice() + { + var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"}; + 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.Recording); + + //Setup + configurationMoq.Setup(c => c.SelectedRecordingDeviceList).Returns(new HashSet<string>()); + TestHelpers.SetConfigurationMoq(configurationMoq); + + //Action + var eventCalled = false; + AppModel.Instance.SelectedDeviceChanged += (sender, changed) => eventCalled = true; + Assert.False(AppModel.Instance.UnselectDevice(audioMoqI.Object)); + + //Asserts + configurationMoq.VerifyGet(c => c.SelectedRecordingDeviceList); + audioMoqI.VerifyGet(a => a.FriendlyName); + audioMoqI.VerifyGet(a => a.Type); + Assert.That(!eventCalled, "SelectedDeviceChanged called"); + } + + [Test] + public void TestUnionSelectedRecordingDeviceWithActiveDevice() + { + var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"}; + + 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"}; + audioMoqII.SetupGet(a => a.FriendlyName).Returns("Headphones (Test device)"); + + var listerMoq = new Mock<IAudioDeviceLister> {Name = "Lister"}; + listerMoq.Setup(lister => lister.GetRecordingDevices()) + .Returns(new List<IAudioDevice> {audioMoqI.Object, audioMoqII.Object}); + + //Setup + configurationMoq.Setup(c => c.SelectedRecordingDeviceList) + .Returns(new HashSet<string> {"Speakers (Test device)"}); + TestHelpers.SetConfigurationMoq(configurationMoq); + AppModel.Instance.ActiveAudioDeviceLister = listerMoq.Object; + + //Action + Assert.That(AppModel.Instance.AvailableRecordingDevices.Count == 1); + Assert.That(AppModel.Instance.AvailableRecordingDevices.Contains(audioMoqI.Object)); + + //Asserts + configurationMoq.VerifyGet(c => c.SelectedRecordingDeviceList); + audioMoqI.VerifyGet(a => a.FriendlyName); + audioMoqII.VerifyGet(a => a.FriendlyName); + listerMoq.Verify(l => l.GetRecordingDevices()); + } + + [Test] + public void TestCycleConsoleAudioDevice() + { + var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"}; + configurationMoq.SetupGet(conf => conf.LastRecordingActive).Returns(""); + + 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"}; + audioMoqII.SetupGet(a => a.FriendlyName).Returns("Headphones (Test device)"); + audioMoqII.SetupGet(a => a.Type).Returns(AudioDeviceType.Recording); + + var listerMoq = new Mock<IAudioDeviceLister> {Name = "Lister"}; + listerMoq.Setup(lister => lister.GetRecordingDevices()) + .Returns(new List<IAudioDevice> {audioMoqI.Object, audioMoqII.Object}); + + //Setup + configurationMoq.Setup(c => c.SelectedRecordingDeviceList) + .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(AudioDeviceType.Recording)); + + //Asserts + configurationMoq.VerifyGet(c => c.SelectedRecordingDeviceList); + audioMoqI.VerifyGet(a => a.FriendlyName); + audioMoqII.VerifyGet(a => a.FriendlyName); + listerMoq.Verify(l => l.GetRecordingDevices()); + configurationMoq.VerifyGet(config => config.LastRecordingActive); + configurationMoq.VerifySet(config => config.LastRecordingActive = "Headphones (Test device)"); + audioMoqII.Verify(a => a.SetAsDefault(It.Is<Role>(role => role == Role.Console))); + Assert.That(audioMoqII.Object.Equals(audioDevice)); + } + + [Test] + public void TestCycleCommunicationsAudioDevice() + { + var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"}; + configurationMoq.SetupGet(conf => conf.LastRecordingActive).Returns(""); + configurationMoq.SetupGet(conf => conf.ChangeCommunications).Returns(true); + + 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(); + audioMoqI.SetupGet(a => a.Type).Returns(AudioDeviceType.Recording); + var audioMoqII = new Mock<IAudioDevice> {Name = "secound audio dev"}; + audioMoqII.SetupGet(a => a.FriendlyName).Returns("Headphones (Test device)"); + audioMoqII.SetupGet(a => a.Type).Returns(AudioDeviceType.Recording); + + + var listerMoq = new Mock<IAudioDeviceLister> {Name = "Lister"}; + listerMoq.Setup(lister => lister.GetRecordingDevices()) + .Returns(new List<IAudioDevice> {audioMoqI.Object, audioMoqII.Object}); + + //Setup + configurationMoq.Setup(c => c.SelectedRecordingDeviceList) + .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(AudioDeviceType.Recording)); + + //Asserts + configurationMoq.VerifyGet(c => c.SelectedRecordingDeviceList); + audioMoqI.VerifyGet(a => a.FriendlyName); + audioMoqII.VerifyGet(a => a.FriendlyName); + listerMoq.Verify(l => l.GetRecordingDevices()); + configurationMoq.VerifyGet(config => config.LastRecordingActive); + configurationMoq.VerifySet(config => config.LastRecordingActive = "Headphones (Test device)"); + audioMoqII.Verify(a => a.SetAsDefault(It.Is<Role>(role => role == Role.Console))); + audioMoqII.Verify(a => a.SetAsDefault(It.Is<Role>(role => role == Role.Communications))); + Assert.That(audioMoqII.Object.Equals(audioDevice)); + } + + [Test] + public void TestCycleAudioDeviceIsACycleThatReturnToFirstWhenReachEnd() + { + var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"}; + configurationMoq.SetupGet(conf => conf.LastRecordingActive).Returns(""); + + 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"}; + audioMoqII.SetupGet(a => a.FriendlyName).Returns("Headphones (Test device)"); + audioMoqII.Setup(a => a.IsDefault(It.Is<Role>(role => role == Role.Console))).Returns(true).Verifiable(); + audioMoqI.SetupGet(a => a.Type).Returns(AudioDeviceType.Recording); + audioMoqII.SetupGet(a => a.Type).Returns(AudioDeviceType.Recording); + + var listerMoq = new Mock<IAudioDeviceLister> {Name = "Lister"}; + listerMoq.Setup(lister => lister.GetRecordingDevices()) + .Returns(new List<IAudioDevice> {audioMoqI.Object, audioMoqII.Object}); + + //Setup + configurationMoq.Setup(c => c.SelectedRecordingDeviceList) + .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(AudioDeviceType.Recording)); + + //Asserts + configurationMoq.VerifyGet(c => c.SelectedRecordingDeviceList); + audioMoqI.VerifyGet(a => a.FriendlyName); + audioMoqII.VerifyGet(a => a.FriendlyName); + listerMoq.Verify(l => l.GetRecordingDevices()); + configurationMoq.VerifyGet(config => config.LastRecordingActive); + configurationMoq.VerifySet(config => config.LastRecordingActive = "Speakers (Test device)"); + audioMoqI.Verify(a => a.SetAsDefault(It.Is<Role>(role => role == Role.Console))); + Assert.That(audioMoqI.Object.Equals(audioDevice)); + } + + [Test] + public void TestSetActiveDeviceNull() + { + var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"}; + configurationMoq.SetupGet(conf => conf.LastRecordingActive).Returns(""); + + + //Setup + TestHelpers.SetConfigurationMoq(configurationMoq); + bool deviceChangeEventCalled = false; + bool errorTriggeredEventCalled = false; + AppModel.Instance.DefaultDeviceChanged += (sender, @event) => deviceChangeEventCalled = true; + AppModel.Instance.ErrorTriggered += + (sender, @event) => errorTriggeredEventCalled = @event.Exception is NullReferenceException; + + //Action + AppModel.Instance.SetActiveDevice(null); + + //Asserts + Assert.That(!deviceChangeEventCalled); + Assert.That(errorTriggeredEventCalled); + } + + [Test] + public void TestSetAudioDeviceSetAudioDeviceAsDefault() + { + var configurationMoq = new Mock<ISoundSwitchConfiguration> {Name = "Configuration mock"}; + configurationMoq.SetupGet(conf => conf.LastRecordingActive).Returns(""); + + 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.Recording); + + //Setup + TestHelpers.SetConfigurationMoq(configurationMoq); + IAudioDevice audioDevice = null; + AppModel.Instance.DefaultDeviceChanged += (sender, @event) => audioDevice = @event.AudioDevice; + + //Action + AppModel.Instance.SetActiveDevice(audioMoqI.Object); + + //Asserts + configurationMoq.VerifySet(config => config.LastRecordingActive = "Speakers (Test device)"); + audioMoqI.VerifyGet(a => a.Type); + audioMoqI.Verify(a => a.SetAsDefault(It.Is<Role>(role => role == Role.Console))); + Assert.That(audioMoqI.Object.Equals(audioDevice)); + } + } +} \ No newline at end of file diff --git a/SoundSwitch.Tests/SoundSwitch.Tests.csproj b/SoundSwitch.Tests/SoundSwitch.Tests.csproj index 8383036827..1a8b5404b9 100644 --- a/SoundSwitch.Tests/SoundSwitch.Tests.csproj +++ b/SoundSwitch.Tests/SoundSwitch.Tests.csproj @@ -125,8 +125,10 @@ </Otherwise> </Choose> <ItemGroup> - <Compile Include="AppModelTests.cs" /> + <Compile Include="RecordingDevices.cs" /> + <Compile Include="PlaybackDevices.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="TestHelpers.cs" /> </ItemGroup> <ItemGroup> <None Include="packages.config" /> diff --git a/SoundSwitch.Tests/TestHelpers.cs b/SoundSwitch.Tests/TestHelpers.cs new file mode 100644 index 0000000000..6604afab4b --- /dev/null +++ b/SoundSwitch.Tests/TestHelpers.cs @@ -0,0 +1,29 @@ +/******************************************************************** +* Copyright (C) 2015 Antoine Aflalo +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +********************************************************************/ + +using System.Reflection; +using Moq; +using SoundSwitch.Framework.Configuration; + +namespace SoundSwitch.Tests +{ + static class TestHelpers + { + public static void SetConfigurationMoq(IMock<ISoundSwitchConfiguration> configurationMoq) + { + var fieldInfo = typeof (AppConfigs).GetRuntimeProperty("Configuration"); + fieldInfo.SetValue(null, configurationMoq.Object); + } + } +} diff --git a/SoundSwitch/Model/AppModel.cs b/SoundSwitch/Model/AppModel.cs index 97ebc24219..7a1a94cf42 100644 --- a/SoundSwitch/Model/AppModel.cs +++ b/SoundSwitch/Model/AppModel.cs @@ -347,7 +347,6 @@ public bool SetActiveDevice(IAudioDevice device) AppLogger.Log.Info("Set Default Communication device", device); device.SetAsDefault(Role.Communications); } - DefaultDeviceChanged?.Invoke(this, new AudioChangeEvent(device)); switch (device.Type) { case AudioDeviceType.Playback: @@ -359,7 +358,7 @@ public bool SetActiveDevice(IAudioDevice device) default: throw new ArgumentOutOfRangeException(); } - + DefaultDeviceChanged?.Invoke(this, new AudioChangeEvent(device)); AppConfigs.Configuration.Save(); return true; } @@ -376,7 +375,7 @@ public bool SetActiveDevice(IAudioDevice device) /// as far as we can tell), returns false if could not successfully switch. Throws NoDevicesException /// if there are no devices configured. /// </summary> - public bool CycleActiveDevice(AudioDeviceType type = AudioDeviceType.Playback) + public bool CycleActiveDevice(AudioDeviceType type) { using (AppLogger.Log.InfoCall()) { diff --git a/SoundSwitch/Model/IAppModel.cs b/SoundSwitch/Model/IAppModel.cs index e506d5e2ca..fc51b77309 100644 --- a/SoundSwitch/Model/IAppModel.cs +++ b/SoundSwitch/Model/IAppModel.cs @@ -143,7 +143,7 @@ public interface IAppModel /// as far as we can tell), returns false if could not successfully switch. Throws NoDevicesException /// if there are no devices configured. /// </summary> - bool CycleActiveDevice(AudioDeviceType type = AudioDeviceType.Playback); + bool CycleActiveDevice(AudioDeviceType type); #endregion }