Changed Contracts to if-throw-else as requested

Changed use of nullable return types to throw exceptions instead of returning null
This commit is contained in:
mrnikbobjeff 2015-10-23 21:25:13 +02:00
parent e5e57c3020
commit 3544c34176
10 changed files with 91 additions and 73 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

View File

@ -9,4 +9,4 @@ namespace SpotifyAPI.Example
InitializeComponent();
}
}
}
}

View File

@ -1,10 +1,10 @@
using System;
using SpotifyAPI.Local;
using SpotifyAPI.Local.Enums;
using SpotifyAPI.Local.Models;
using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Forms;
using SpotifyAPI.Local;
using SpotifyAPI.Local.Enums;
using SpotifyAPI.Local.Models;
namespace SpotifyAPI.Example
{
@ -102,23 +102,23 @@ namespace SpotifyAPI.Example
isPlayingLabel.Text = playing.ToString();
}
void _spotify_OnVolumeChange(VolumeChangeEventArgs e)
private void _spotify_OnVolumeChange(VolumeChangeEventArgs e)
{
volumeLabel.Text = (e.NewVolume*100).ToString(CultureInfo.InvariantCulture);
volumeLabel.Text = (e.NewVolume * 100).ToString(CultureInfo.InvariantCulture);
}
void _spotify_OnTrackTimeChange(TrackTimeChangeEventArgs e)
private void _spotify_OnTrackTimeChange(TrackTimeChangeEventArgs e)
{
timeLabel.Text = FormatTime(e.TrackTime) + "/" + FormatTime(_currentTrack.Length);
timeProgressBar.Value = (int) e.TrackTime;
timeProgressBar.Value = (int)e.TrackTime;
}
void _spotify_OnTrackChange(TrackChangeEventArgs e)
private void _spotify_OnTrackChange(TrackChangeEventArgs e)
{
UpdateTrack(e.NewTrack);
}
void _spotify_OnPlayStateChange(PlayStateEventArgs e)
private void _spotify_OnPlayStateChange(PlayStateEventArgs e)
{
UpdatePlayingStatus(e.Playing);
}
@ -162,4 +162,4 @@ namespace SpotifyAPI.Example
return mins + ":" + secs;
}
}
}
}

View File

@ -1,22 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SpotifyAPI.Example
{
static class Program
internal static class Program
{
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main()
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new ExampleForm());
}
}
}
}

View File

@ -1,8 +1,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden
// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die mit einer Assembly verknüpft sind.
[assembly: AssemblyTitle("SpotifyAPI.Example")]
@ -14,8 +13,8 @@ using System.Runtime.InteropServices;
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest.
[assembly: ComVisible(false)]
@ -25,12 +24,12 @@ using System.Runtime.InteropServices;
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
//
// Hauptversion
// Nebenversion
// Nebenversion
// Buildnummer
// Revision
//
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -1,14 +1,13 @@
using System;
using SpotifyAPI.Web;
using SpotifyAPI.Web.Auth;
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows.Forms;
using SpotifyAPI.Web;
using SpotifyAPI.Web.Auth;
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
using Image = System.Drawing.Image;
namespace SpotifyAPI.Example
@ -21,7 +20,7 @@ namespace SpotifyAPI.Example
private PrivateProfile _profile;
private List<FullTrack> _savedTracks;
private List<SimplePlaylist> _playlists;
public WebControl()
{
InitializeComponent();
@ -37,7 +36,7 @@ namespace SpotifyAPI.Example
_auth.OnResponseReceivedEvent += _auth_OnResponseReceivedEvent;
}
void _auth_OnResponseReceivedEvent(Token token, string state)
private void _auth_OnResponseReceivedEvent(Token token, string state)
{
_auth.StopHttpServer();
@ -77,7 +76,7 @@ namespace SpotifyAPI.Example
_savedTracks.ForEach(track => savedTracksListView.Items.Add(new ListViewItem()
{
Text = track.Name,
SubItems = {string.Join(",", track.Artists.Select(source => source.Name)), track.Album.Name}
SubItems = { string.Join(",", track.Artists.Select(source => source.Name)), track.Album.Name }
}));
_playlists = GetPlaylists();
@ -134,4 +133,4 @@ namespace SpotifyAPI.Example
_auth.DoAuth();
}
}
}
}

View File

@ -1,8 +1,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden
// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die mit einer Assembly verknüpft sind.
[assembly: AssemblyTitle("SpotifyAPI.Tests")]
@ -14,8 +13,8 @@ using System.Runtime.InteropServices;
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest.
[assembly: ComVisible(false)]
@ -25,12 +24,12 @@ using System.Runtime.InteropServices;
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
//
// Hauptversion
// Nebenversion
// Nebenversion
// Buildnummer
// Revision
//
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -1,11 +1,11 @@
using System;
using System.IO;
using System.Linq;
using Moq;
using Moq;
using Newtonsoft.Json;
using NUnit.Framework;
using SpotifyAPI.Web;
using SpotifyAPI.Web.Models;
using System;
using System.IO;
using System.Linq;
namespace SpotifyAPI.Tests
{
@ -67,4 +67,4 @@ namespace SpotifyAPI.Tests
//Will add more tests once I decided if this is worth the effort (propably not?)
}
}
}

View File

@ -162,10 +162,12 @@ namespace SpotifyAPI.Local
public void Mute()
{
//Windows < Windows Vista Check
Contract.Requires(Environment.OSVersion.Version.Major >= 6);
if (Environment.OSVersion.Version.Major < 6)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
//Windows Vista Check
if (Environment.OSVersion.Version.Major == 6)
Contract.Requires(Environment.OSVersion.Version.Minor != 0);
if(Environment.OSVersion.Version.Minor == 0)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
VolumeMixerControl.MuteSpotify(true);
}
@ -175,10 +177,12 @@ namespace SpotifyAPI.Local
public void UnMute()
{
//Windows < Windows Vista Check
Contract.Requires(Environment.OSVersion.Version.Major >= 6);
if (Environment.OSVersion.Version.Major < 6)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
//Windows Vista Check
if (Environment.OSVersion.Version.Major == 6)
Contract.Requires(Environment.OSVersion.Version.Minor != 0);
if (Environment.OSVersion.Version.Minor == 0)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
VolumeMixerControl.MuteSpotify(false);
}
@ -186,13 +190,15 @@ namespace SpotifyAPI.Local
/// Checks whether Spotify is muted in the Volume Mixer control (required Windows 7 or newer)
/// </summary>
/// <returns>Null if an error occured, otherwise the muted state</returns>
public bool? IsSpotifyMuted()
public bool IsSpotifyMuted()
{
//Windows < Windows Vista Check
Contract.Requires(Environment.OSVersion.Version.Major >= 6);
if (Environment.OSVersion.Version.Major < 6)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
//Windows Vista Check
if (Environment.OSVersion.Version.Major == 6)
Contract.Requires(Environment.OSVersion.Version.Minor != 0);
if (Environment.OSVersion.Version.Minor == 0)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
return VolumeMixerControl.IsSpotifyMuted();
}
@ -202,12 +208,15 @@ namespace SpotifyAPI.Local
/// <param name="volume">A value between 0 and 100</param>
public void SetSpotifyVolume(float volume = 100)
{
Contract.Requires(0 <= volume && volume <= 100);
//Windows < Windows Vista Check
Contract.Requires(Environment.OSVersion.Version.Major >= 6);
if (Environment.OSVersion.Version.Major < 6)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
//Windows Vista Check
if (Environment.OSVersion.Version.Major == 6)
Contract.Requires(Environment.OSVersion.Version.Minor != 0);
if (Environment.OSVersion.Version.Minor == 0)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
if (volume < 0 || volume > 100)
throw new ArgumentOutOfRangeException("Volume parameter has to be a value between 0 and 100");
VolumeMixerControl.SetSpotifyVolume(volume);
}
@ -215,13 +224,15 @@ namespace SpotifyAPI.Local
/// Return the Volume Mixer volume of Spotify (requires Windows 7 or newer)
/// </summary>
/// <returns>Null if an error occured, otherwise a float between 0 and 100</returns>
public float? GetSpotifyVolume()
public float GetSpotifyVolume()
{
//Windows < Windows Vista Check
Contract.Requires(Environment.OSVersion.Version.Major >= 6);
if (Environment.OSVersion.Version.Major < 6)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
//Windows Vista Check
if (Environment.OSVersion.Version.Major == 6)
Contract.Requires(Environment.OSVersion.Version.Minor != 0);
if (Environment.OSVersion.Version.Minor == 0)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
return VolumeMixerControl.GetSpotifyVolume();
}

View File

@ -8,17 +8,20 @@ namespace SpotifyAPI.Local
{
private const String SPOTIFY_PROCESS_NAME = "spotify";
internal static float? GetSpotifyVolume()
internal static float GetSpotifyVolume()
{
Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME);
if (p.Length == 0)
return null;
throw new Exception("Spotify process is not running or was not found!");
int pid = p[0].Id;
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null)
return null;
{
Marshal.ReleaseComObject(volume);
throw new COMException("Volume object creation failed");
}
float level;
volume.GetMasterVolume(out level);
@ -26,17 +29,20 @@ namespace SpotifyAPI.Local
return level * 100;
}
internal static bool? IsSpotifyMuted()
internal static bool IsSpotifyMuted()
{
Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME);
if (p.Length == 0)
return null;
throw new Exception("Spotify process is not running or was not found!");
int pid = p[0].Id;
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null)
return null;
{
Marshal.ReleaseComObject(volume);
throw new COMException("Volume object creation failed");
}
bool mute;
volume.GetMute(out mute);
@ -48,13 +54,16 @@ namespace SpotifyAPI.Local
{
Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME);
if (p.Length == 0)
return;
throw new Exception("Spotify process is not running or was not found!");
int pid = p[0].Id;
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null)
return;
{
Marshal.ReleaseComObject(volume);
throw new COMException("Volume object creation failed");
}
Guid guid = Guid.Empty;
volume.SetMasterVolume(level / 100, ref guid);
@ -65,13 +74,16 @@ namespace SpotifyAPI.Local
{
Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME);
if (p.Length == 0)
return;
throw new Exception("Spotify process is not running or was not found!");
int pid = p[0].Id;
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null)
return;
{
Marshal.ReleaseComObject(volume);
throw new COMException("Volume object creation failed");
}
Guid guid = Guid.Empty;
volume.SetMute(mute, ref guid);
@ -125,6 +137,7 @@ namespace SpotifyAPI.Local
[Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]
private class MMDeviceEnumerator
{
}
private enum EDataFlow