test: added unit tests for the PowerUtils util class.

Signed-off-by: ardevd <edvard.holst@gmail.com>
This commit is contained in:
ardevd 2019-04-09 20:35:04 +02:00
parent b252c6440d
commit 81184d0266
No known key found for this signature in database
GPG key ID: C30729EE3C9A3CCE

View file

@ -0,0 +1,58 @@
package com.owncloud.android.utils;
import android.content.Context;
import android.os.Build;
import android.os.PowerManager;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
public class PowerUtilsTest {
@Mock
private Context mContext;
@Mock
private PowerManager mPowerManager;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
private static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}
@Test
public void isPowerSaveMode_assertCorrectlyReportsTrue() throws Exception {
setFinalStatic(Build.VERSION.class.getField("SDK_INT"), Build.VERSION_CODES.O);
when(mContext.getSystemService(Context.POWER_SERVICE)).thenReturn(mPowerManager);
when(mPowerManager.isPowerSaveMode()).thenReturn(true);
assertTrue("Incorrectly reported power saving mode on",
PowerUtils.isPowerSaveMode(mContext));
}
@Test
public void isPowerSaveMode_assertCorrectlyReportsFalse() throws Exception {
setFinalStatic(Build.VERSION.class.getField("SDK_INT"), Build.VERSION_CODES.O);
when(mContext.getSystemService(Context.POWER_SERVICE)).thenReturn(mPowerManager);
when(mPowerManager.isPowerSaveMode()).thenReturn(false);
assertFalse("Incorrectly reported power saving mode off",
PowerUtils.isPowerSaveMode(mContext));
}
}