#3248 Add unit test

This commit is contained in:
Max Bechtold 2019-07-07 11:42:52 +02:00
parent cd7f0245a5
commit 03074168ce
4 changed files with 66 additions and 26 deletions

View File

@ -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;
});
}

View File

@ -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;
}
}

View File

@ -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<Integer, Integer> 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,24 +339,6 @@ 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
@ -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();
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);
}

View File

@ -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);
}
}