Skip to content

Commit

Permalink
Added PlaybackControllerProvider, so that the MediaButtonPlaybackCont…
Browse files Browse the repository at this point in the history
…roller always has the "current" instance of the playback controller (in the case where the PlaybackController property is reassigned on the IMediaManager).
  • Loading branch information
myrobotzone committed Jul 31, 2017
1 parent f622928 commit 0c1e448
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<DependentUpon>UnitTestApp.xaml</DependentUpon>
</Compile>
<Compile Include="Unit\MediaButtonPlaybackControllerTest.cs" />
<Compile Include="Unit\PlaybackControllerProviderMock.cs" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="UnitTestApp.xaml">
Expand Down
21 changes: 13 additions & 8 deletions MediaManager.UWP.Tests/Unit/MediaButtonPlaybackControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void ctor_MediaButtonsAreEnabled()
{
var controlsMock = new Mock<ISystemMediaTransportControlsWrapper>();

new MediaButtonPlaybackController(controlsMock.Object, new Mock<IPlaybackController>().Object);
new MediaButtonPlaybackController(controlsMock.Object, new PlaybackControllerProviderMock().Object);

controlsMock.VerifySet(m => m.IsNextEnabled = true);
controlsMock.VerifySet(m => m.IsPreviousEnabled = true);
Expand All @@ -28,7 +28,7 @@ public void SubscribeToNotifications_SubscribeToMediaButtonEventsIsCalled()
{
var controlsMock = new Mock<ISystemMediaTransportControlsWrapper>();

var controller = new MediaButtonPlaybackController(controlsMock.Object, new Mock<IPlaybackController>().Object);
var controller = new MediaButtonPlaybackController(controlsMock.Object, new PlaybackControllerProviderMock().Object);
controller.SubscribeToNotifications();

controlsMock.Verify(m => m.SubscribeToMediaButtonEvents());
Expand All @@ -39,7 +39,7 @@ public void UnsubscribeFromNotifications_CallsUnsubscribeFromMediaButtonEvents()
{
var controlsMock = new Mock<ISystemMediaTransportControlsWrapper>();

var controller = new MediaButtonPlaybackController(controlsMock.Object, new Mock<IPlaybackController>().Object);
var controller = new MediaButtonPlaybackController(controlsMock.Object, new PlaybackControllerProviderMock().Object);
controller.UnsubscribeFromNotifications();

controlsMock.Verify(m => m.UnsubscribeFromMediaButtonEvents());
Expand All @@ -50,8 +50,9 @@ public void NextButtonPressed_PlayNextIsCalled()
{
var controlsMock = new Mock<ISystemMediaTransportControlsWrapper>();
var playbackControllerMock = new Mock<IPlaybackController>();
var playbackControllerProviderMock = new PlaybackControllerProviderMock().SetupGetPlayBackController(playbackControllerMock.Object);

var controller = new MediaButtonPlaybackController(controlsMock.Object, playbackControllerMock.Object);
var controller = new MediaButtonPlaybackController(controlsMock.Object, playbackControllerProviderMock.Object);
controller.SubscribeToNotifications();

var args = new Mock<ISystemMediaTransportControlsButtonPressedEventArgsWrapper>();
Expand All @@ -66,8 +67,9 @@ public void PreviousButtonPressed_PlayPreviousOrSeekToStartIsCalled()
{
var controlsMock = new Mock<ISystemMediaTransportControlsWrapper>();
var playbackControllerMock = new Mock<IPlaybackController>();
var playbackControllerProviderMock = new PlaybackControllerProviderMock().SetupGetPlayBackController(playbackControllerMock.Object);

var controller = new MediaButtonPlaybackController(controlsMock.Object, playbackControllerMock.Object);
var controller = new MediaButtonPlaybackController(controlsMock.Object, playbackControllerProviderMock.Object);
controller.SubscribeToNotifications();

var args = new Mock<ISystemMediaTransportControlsButtonPressedEventArgsWrapper>();
Expand All @@ -82,8 +84,9 @@ public void PlayButtonPressed_PlayIsCalled()
{
var controlsMock = new Mock<ISystemMediaTransportControlsWrapper>();
var playbackControllerMock = new Mock<IPlaybackController>();
var playbackControllerProviderMock = new PlaybackControllerProviderMock().SetupGetPlayBackController(playbackControllerMock.Object);

var controller = new MediaButtonPlaybackController(controlsMock.Object, playbackControllerMock.Object);
var controller = new MediaButtonPlaybackController(controlsMock.Object, playbackControllerProviderMock.Object);
controller.SubscribeToNotifications();

var args = new Mock<ISystemMediaTransportControlsButtonPressedEventArgsWrapper>();
Expand All @@ -98,8 +101,9 @@ public void PauseButtonPressed_PauseIsCalled()
{
var controlsMock = new Mock<ISystemMediaTransportControlsWrapper>();
var playbackControllerMock = new Mock<IPlaybackController>();
var playbackControllerProviderMock = new PlaybackControllerProviderMock().SetupGetPlayBackController(playbackControllerMock.Object);

var controller = new MediaButtonPlaybackController(controlsMock.Object, playbackControllerMock.Object);
var controller = new MediaButtonPlaybackController(controlsMock.Object, playbackControllerProviderMock.Object);
controller.SubscribeToNotifications();

var args = new Mock<ISystemMediaTransportControlsButtonPressedEventArgsWrapper>();
Expand All @@ -114,8 +118,9 @@ public void StopButtonPressed_StopIsCalled()
{
var controlsMock = new Mock<ISystemMediaTransportControlsWrapper>();
var playbackControllerMock = new Mock<IPlaybackController>();
var playbackControllerProviderMock = new PlaybackControllerProviderMock().SetupGetPlayBackController(playbackControllerMock.Object);

var controller = new MediaButtonPlaybackController(controlsMock.Object, playbackControllerMock.Object);
var controller = new MediaButtonPlaybackController(controlsMock.Object, playbackControllerProviderMock.Object);
controller.SubscribeToNotifications();

var args = new Mock<ISystemMediaTransportControlsButtonPressedEventArgsWrapper>();
Expand Down
14 changes: 14 additions & 0 deletions MediaManager.UWP.Tests/Unit/PlaybackControllerProviderMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Moq;
using Plugin.MediaManager.Abstractions;

namespace Plugin.MediaManager.UWP.Tests.Unit
{
internal class PlaybackControllerProviderMock : Mock<IPlaybackControllerProvider>
{
public PlaybackControllerProviderMock SetupGetPlayBackController(IPlaybackController playbackController)
{
Setup(mock => mock.PlaybackController).Returns(playbackController);
return this;
}
}
}
9 changes: 9 additions & 0 deletions MediaManager.UWP/IPlaybackControllerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Plugin.MediaManager.Abstractions;

namespace Plugin.MediaManager
{
internal interface IPlaybackControllerProvider
{
IPlaybackController PlaybackController { get; }
}
}
16 changes: 8 additions & 8 deletions MediaManager.UWP/MediaButtonPlaybackController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace Plugin.MediaManager
internal class MediaButtonPlaybackController
{
private readonly ISystemMediaTransportControlsWrapper _systemMediaTransportControlsWrapper;
private readonly IPlaybackController _playbackController;
private readonly IPlaybackControllerProvider _playbackControllerProvider;

public MediaButtonPlaybackController(ISystemMediaTransportControlsWrapper systemMediaTransportControlsWrapper, IPlaybackController playbackController)
public MediaButtonPlaybackController(ISystemMediaTransportControlsWrapper systemMediaTransportControlsWrapper, IPlaybackControllerProvider playbackControllerProvider)
{
_systemMediaTransportControlsWrapper = systemMediaTransportControlsWrapper;
_playbackController = playbackController;
_playbackControllerProvider = playbackControllerProvider;

_systemMediaTransportControlsWrapper.IsNextEnabled = true;
_systemMediaTransportControlsWrapper.IsPreviousEnabled = true;
Expand All @@ -38,19 +38,19 @@ private async void MediaControls_ButtonPressed(SystemMediaTransportControls send
switch (args.Button)
{
case SystemMediaTransportControlsButton.Next:
await _playbackController.PlayNext();
await _playbackControllerProvider.PlaybackController.PlayNext();
break;
case SystemMediaTransportControlsButton.Previous:
await _playbackController.PlayPreviousOrSeekToStart();
await _playbackControllerProvider.PlaybackController.PlayPreviousOrSeekToStart();
break;
case SystemMediaTransportControlsButton.Play:
await _playbackController.Play();
await _playbackControllerProvider.PlaybackController.Play();
break;
case SystemMediaTransportControlsButton.Pause:
await _playbackController.Pause();
await _playbackControllerProvider.PlaybackController.Pause();
break;
case SystemMediaTransportControlsButton.Stop:
await _playbackController.Stop();
await _playbackControllerProvider.PlaybackController.Stop();
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions MediaManager.UWP/MediaManagerImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Plugin.MediaManager
/// <summary>
/// Implementation for Feature
/// </summary>
public class MediaManagerImplementation : MediaManagerBase
public class MediaManagerImplementation : MediaManagerBase, IPlaybackControllerProvider
{
private IAudioPlayer _audioPlayer;
private IVideoPlayer _videoPlayer;
Expand All @@ -17,7 +17,7 @@ public class MediaManagerImplementation : MediaManagerBase
public MediaManagerImplementation()
{
var systemMediaTransportControlsWrapper = new SystemMediaTransportControlsWrapper(SystemMediaTransportControls.GetForCurrentView());
_mediaButtonPlaybackController = new MediaButtonPlaybackController(systemMediaTransportControlsWrapper, PlaybackController);
_mediaButtonPlaybackController = new MediaButtonPlaybackController(systemMediaTransportControlsWrapper, this);
_mediaButtonPlaybackController.SubscribeToNotifications();
}

Expand Down
1 change: 1 addition & 0 deletions MediaManager.UWP/Plugin.MediaManager.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<Link>CrossMediaManager.cs</Link>
</Compile>
<Compile Include="AudioPlayerImplementation.cs" />
<Compile Include="IPlaybackControllerProvider.cs" />
<Compile Include="SystemWrappers\ISystemMediaTransportControlsWrapper.cs" />
<Compile Include="SystemWrappers\ISystemMediaTransportControlsButtonPressedEventArgsWrapper.cs" />
<Compile Include="MediaExtractorImplementation.cs" />
Expand Down

0 comments on commit 0c1e448

Please sign in to comment.