diff options
author | Max Bechtold <max.bechtold@andrena.de> | 2019-08-17 15:46:08 +0200 |
---|---|---|
committer | Max Bechtold <max.bechtold@andrena.de> | 2019-08-18 13:13:37 +0200 |
commit | 759ce1b3574587b7045368c1ae476032b8b79942 (patch) | |
tree | 3de43a180eeb005793f527a84e996919718802a3 /core/src/test/java | |
parent | 7f0e642069ef3c6cf6844d1a8f0c50d0fbe925f2 (diff) | |
download | AntennaPod-759ce1b3574587b7045368c1ae476032b8b79942.zip |
#3248 Adapt volume for playing media if its volume reduction setting is changed
Diffstat (limited to 'core/src/test/java')
-rw-r--r-- | core/src/test/java/de/danoeh/antennapod/core/service/playback/PlaybackVolumeAdaptorTest.java | 230 |
1 files changed, 230 insertions, 0 deletions
diff --git a/core/src/test/java/de/danoeh/antennapod/core/service/playback/PlaybackVolumeAdaptorTest.java b/core/src/test/java/de/danoeh/antennapod/core/service/playback/PlaybackVolumeAdaptorTest.java new file mode 100644 index 000000000..9036624d8 --- /dev/null +++ b/core/src/test/java/de/danoeh/antennapod/core/service/playback/PlaybackVolumeAdaptorTest.java @@ -0,0 +1,230 @@ +package de.danoeh.antennapod.core.service.playback; + +import de.danoeh.antennapod.core.feed.Feed; +import de.danoeh.antennapod.core.feed.FeedItem; +import de.danoeh.antennapod.core.feed.FeedMedia; +import de.danoeh.antennapod.core.feed.FeedPreferences; +import de.danoeh.antennapod.core.feed.FeedPreferences.VolumeReductionSetting; +import de.danoeh.antennapod.core.util.playback.Playable; +import org.junit.Before; +import org.junit.Test; + +import static org.mockito.Mockito.anyBoolean; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class PlaybackVolumeAdaptorTest { + + private static final String FEED_ID = "feedId"; + + private PlaybackServiceMediaPlayer mediaPlayer; + + @Before + public void setUp() throws Exception { + mediaPlayer = mock(PlaybackServiceMediaPlayer.class); + } + + @Test + public void noChangeIfNoFeedMediaPlaying() { + PlaybackVolumeAdaptor playbackVolumeAdaptor = new PlaybackVolumeAdaptor(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.PAUSED); + + Playable noFeedMedia = mock(Playable.class); + when(mediaPlayer.getPlayable()).thenReturn(noFeedMedia); + + playbackVolumeAdaptor.adaptVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeReductionSetting.OFF); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void noChangeIfPlayerStatusIsError() { + PlaybackVolumeAdaptor playbackVolumeAdaptor = new PlaybackVolumeAdaptor(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.ERROR); + + FeedMedia feedMedia = mock(FeedMedia.class); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + + playbackVolumeAdaptor.adaptVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeReductionSetting.OFF); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void noChangeIfPlayerStatusIsIndeterminate() { + PlaybackVolumeAdaptor playbackVolumeAdaptor = new PlaybackVolumeAdaptor(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.INDETERMINATE); + + FeedMedia feedMedia = mock(FeedMedia.class); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + + playbackVolumeAdaptor.adaptVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeReductionSetting.OFF); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void noChangeIfPlayerStatusIsStopped() { + PlaybackVolumeAdaptor playbackVolumeAdaptor = new PlaybackVolumeAdaptor(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.STOPPED); + + FeedMedia feedMedia = mock(FeedMedia.class); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + + playbackVolumeAdaptor.adaptVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeReductionSetting.OFF); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void noChangeIfPlayableIsNoItemOfAffectedFeed() { + PlaybackVolumeAdaptor playbackVolumeAdaptor = new PlaybackVolumeAdaptor(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.PLAYING); + + FeedMedia feedMedia = mock(FeedMedia.class); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + Feed feed = mockFeed(feedMedia, FEED_ID); + when(feed.getIdentifyingValue()).thenReturn("wrongFeedId"); + + playbackVolumeAdaptor.adaptVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeReductionSetting.OFF); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void adaptsPreferencesForLoadedFeedMediaIfPlayerStatusIsPaused() { + PlaybackVolumeAdaptor playbackVolumeAdaptor = new PlaybackVolumeAdaptor(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.PAUSED); + + FeedMedia feedMedia = mock(FeedMedia.class); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + FeedPreferences feedPreferences = mockFeedPreferences(feedMedia, FEED_ID); + + playbackVolumeAdaptor.adaptVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeReductionSetting.LIGHT); + + verify(feedPreferences, times(1)).setVolumeReductionSetting(VolumeReductionSetting.LIGHT); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void adaptsPreferencesForLoadedFeedMediaIfPlayerStatusIsPrepared() { + PlaybackVolumeAdaptor playbackVolumeAdaptor = new PlaybackVolumeAdaptor(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.PREPARED); + + FeedMedia feedMedia = mock(FeedMedia.class); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + FeedPreferences feedPreferences = mockFeedPreferences(feedMedia, FEED_ID); + + playbackVolumeAdaptor.adaptVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeReductionSetting.LIGHT); + + verify(feedPreferences, times(1)).setVolumeReductionSetting(VolumeReductionSetting.LIGHT); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void adaptsPreferencesForLoadedFeedMediaIfPlayerStatusIsInitializing() { + PlaybackVolumeAdaptor playbackVolumeAdaptor = new PlaybackVolumeAdaptor(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.INITIALIZING); + + FeedMedia feedMedia = mock(FeedMedia.class); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + FeedPreferences feedPreferences = mockFeedPreferences(feedMedia, FEED_ID); + + playbackVolumeAdaptor.adaptVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeReductionSetting.LIGHT); + + verify(feedPreferences, times(1)).setVolumeReductionSetting(VolumeReductionSetting.LIGHT); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void adaptsPreferencesForLoadedFeedMediaIfPlayerStatusIsPreparing() { + PlaybackVolumeAdaptor playbackVolumeAdaptor = new PlaybackVolumeAdaptor(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.PREPARING); + + FeedMedia feedMedia = mock(FeedMedia.class); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + FeedPreferences feedPreferences = mockFeedPreferences(feedMedia, FEED_ID); + + playbackVolumeAdaptor.adaptVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeReductionSetting.LIGHT); + + verify(feedPreferences, times(1)).setVolumeReductionSetting(VolumeReductionSetting.LIGHT); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void adaptsPreferencesForLoadedFeedMediaIfPlayerStatusIsSeeking() { + PlaybackVolumeAdaptor playbackVolumeAdaptor = new PlaybackVolumeAdaptor(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.SEEKING); + + FeedMedia feedMedia = mock(FeedMedia.class); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + FeedPreferences feedPreferences = mockFeedPreferences(feedMedia, FEED_ID); + + playbackVolumeAdaptor.adaptVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeReductionSetting.LIGHT); + + verify(feedPreferences, times(1)).setVolumeReductionSetting(VolumeReductionSetting.LIGHT); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void adaptsPreferencesAndForcesVolumeChangeForLoadedFeedMediaIfPlayerStatusIsPlaying() { + PlaybackVolumeAdaptor playbackVolumeAdaptor = new PlaybackVolumeAdaptor(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.PLAYING); + + FeedMedia feedMedia = mock(FeedMedia.class); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + FeedPreferences feedPreferences = mockFeedPreferences(feedMedia, FEED_ID); + + playbackVolumeAdaptor.adaptVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeReductionSetting.HEAVY); + + verify(feedPreferences, times(1)).setVolumeReductionSetting(VolumeReductionSetting.HEAVY); + + verify(mediaPlayer, times(1)).pause(false, false); + verify(mediaPlayer, times(1)).resume(); + } + + private FeedPreferences mockFeedPreferences(FeedMedia feedMedia, String feedId) { + Feed feed = mockFeed(feedMedia, feedId); + FeedPreferences feedPreferences = mock(FeedPreferences.class); + when(feed.getPreferences()).thenReturn(feedPreferences); + return feedPreferences; + } + + private Feed mockFeed(FeedMedia feedMedia, String feedId) { + FeedItem feedItem = mock(FeedItem.class); + when(feedMedia.getItem()).thenReturn(feedItem); + Feed feed = mock(Feed.class); + when(feed.getIdentifyingValue()).thenReturn(feedId); + when(feedItem.getFeed()).thenReturn(feed); + return feed; + } +} |