From 03074168ce7432d8c439e87241d151e6dbbc9bd6 Mon Sep 17 00:00:00 2001 From: Max Bechtold Date: Sun, 7 Jul 2019 11:42:52 +0200 Subject: [PATCH] #3248 Add unit test --- .../fragment/FeedSettingsFragment.java | 2 +- .../service/playback/FeedVolumeReduction.java | 16 ++++++++ .../core/service/playback/LocalPSMP.java | 39 +++++++------------ .../playback/FeedVolumeReductionTest.java | 35 +++++++++++++++++ 4 files changed, 66 insertions(+), 26 deletions(-) create mode 100644 core/src/main/java/de/danoeh/antennapod/core/service/playback/FeedVolumeReduction.java create mode 100644 core/src/test/java/de/danoeh/antennapod/core/service/playback/FeedVolumeReductionTest.java diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java index e08020d9c..d9c350720 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java @@ -132,7 +132,7 @@ public class FeedSettingsFragment extends PreferenceFragmentCompat { } feed.savePreferences(); updateVolumeReductionSummary(); - // TODO maxbechtold Check if we can call setVolume for the PlaybackService, if running + // TODO maxbechtold Check if we can call setVolume for the PlaybackService, if running. Else, show toast? return false; }); } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/FeedVolumeReduction.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/FeedVolumeReduction.java new file mode 100644 index 000000000..c9e10efc3 --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/FeedVolumeReduction.java @@ -0,0 +1,16 @@ +package de.danoeh.antennapod.core.service.playback; + +import de.danoeh.antennapod.core.feed.FeedPreferences; + +public class FeedVolumeReduction { + float getReductionFactor(FeedPreferences preferences) { + FeedPreferences.VolumeReductionSetting volumeReductionSetting = preferences.getVolumeReductionSetting(); + // TODO maxbechtold These numbers should be tested + if (volumeReductionSetting == FeedPreferences.VolumeReductionSetting.LIGHT) { + return 0.5f; + } else if (volumeReductionSetting == FeedPreferences.VolumeReductionSetting.HEAVY) { + return 0.2f; + } + return 1.0f; + } +} diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java index b4cd4b0d9..5153dcddb 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java @@ -12,6 +12,8 @@ import android.util.Log; import android.util.Pair; import android.view.SurfaceHolder; +import de.danoeh.antennapod.core.feed.FeedMedia; +import de.danoeh.antennapod.core.feed.FeedPreferences; import org.antennapod.audio.MediaPlayer; import java.io.File; @@ -25,8 +27,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.ReentrantLock; -import de.danoeh.antennapod.core.feed.FeedMedia; -import de.danoeh.antennapod.core.feed.FeedPreferences; import de.danoeh.antennapod.core.feed.MediaType; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.util.RewindAfterPauseUtils; @@ -53,6 +53,8 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { private volatile boolean pausedBecauseOfTransientAudiofocusLoss; private volatile Pair videoSize; + private final FeedVolumeReduction feedVolumeReduction; + /** * Some asynchronous calls might change the state of the MediaPlayer object. Therefore calls in other threads * have to wait until these operations have finished. @@ -138,6 +140,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { super(context, callback); this.audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); this.playerLock = new PlayerLock(); + this.feedVolumeReduction = new FeedVolumeReduction(); this.startWhenPrepared = new AtomicBoolean(false); executor = new PlayerExecutor(); @@ -336,26 +339,8 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { } } - private float getVolumeReductionFactor() { - Playable playable = getPlayable(); - if (playable instanceof FeedMedia) { - FeedMedia feedMedia = (FeedMedia) playable; - FeedPreferences preferences = feedMedia.getItem().getFeed().getPreferences(); - FeedPreferences.VolumeReductionSetting volumeReductionSetting = preferences.getVolumeReductionSetting(); - // TODO maxbechtold Move this logic out of this class - // TODO maxbechtold These numbers should be tested - if (volumeReductionSetting == FeedPreferences.VolumeReductionSetting.LIGHT) { - return 0.5f; - } else if (volumeReductionSetting == FeedPreferences.VolumeReductionSetting.HEAVY) { - return 0.2f; - } - } - return 1.0f; - } - - - /** + /** * Saves the current position and pauses playback. Note that, if audiofocus * is abandoned, the lockscreen controls will also disapear. *

@@ -691,10 +676,14 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { private void setVolumeSync(float volumeLeft, float volumeRight) { playerLock.lock(); if (media != null && media.getMediaType() == MediaType.AUDIO) { - // TODO maxbechtold does not apply to currently playing episode - float reductionFactor = getVolumeReductionFactor(); - volumeLeft *= reductionFactor; - volumeRight *= reductionFactor; + Playable playable = getPlayable(); + if (playable instanceof FeedMedia) { + FeedMedia feedMedia = (FeedMedia) playable; + FeedPreferences preferences = feedMedia.getItem().getFeed().getPreferences(); + float reductionFactor = feedVolumeReduction.getReductionFactor(preferences); + volumeLeft *= reductionFactor; + volumeRight *= reductionFactor; + } mediaPlayer.setVolume(volumeLeft, volumeRight); Log.d(TAG, "Media player volume was set to " + volumeLeft + " " + volumeRight); } diff --git a/core/src/test/java/de/danoeh/antennapod/core/service/playback/FeedVolumeReductionTest.java b/core/src/test/java/de/danoeh/antennapod/core/service/playback/FeedVolumeReductionTest.java new file mode 100644 index 000000000..e8a2af413 --- /dev/null +++ b/core/src/test/java/de/danoeh/antennapod/core/service/playback/FeedVolumeReductionTest.java @@ -0,0 +1,35 @@ +package de.danoeh.antennapod.core.service.playback; + +import de.danoeh.antennapod.core.feed.FeedPreferences; +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class FeedVolumeReductionTest { + + @Test + public void noReductionIfTurnedOff() { + FeedPreferences feedPreferences = mock(FeedPreferences.class); + when(feedPreferences.getVolumeReductionSetting()).thenReturn(FeedPreferences.VolumeReductionSetting.OFF); + + FeedVolumeReduction feedVolumeReduction = new FeedVolumeReduction(); + float reductionFactor = feedVolumeReduction.getReductionFactor(feedPreferences); + assertEquals(1.0f, reductionFactor, 0.01f ); + } + + @Test + public void lightReductionYieldsHigherValueThanHeavyReduction() { + FeedPreferences feedPreferences = mock(FeedPreferences.class); + FeedVolumeReduction feedVolumeReduction = new FeedVolumeReduction(); + + when(feedPreferences.getVolumeReductionSetting()).thenReturn(FeedPreferences.VolumeReductionSetting.LIGHT); + float lightReductionFactor = feedVolumeReduction.getReductionFactor(feedPreferences); + + when(feedPreferences.getVolumeReductionSetting()).thenReturn(FeedPreferences.VolumeReductionSetting.HEAVY); + float heavyReductionFactor = feedVolumeReduction.getReductionFactor(feedPreferences); + + assertTrue("Light reduction must have higher factor than heavy reduction", lightReductionFactor > heavyReductionFactor); + } +} \ No newline at end of file