mirror of
https://github.com/nextcloud/android.git
synced 2024-11-22 13:15:35 +03:00
test: added unit tests for the PowerUtils util class.
Signed-off-by: ardevd <edvard.holst@gmail.com>
This commit is contained in:
parent
b252c6440d
commit
81184d0266
1 changed files with 58 additions and 0 deletions
58
src/test/java/com/owncloud/android/utils/PowerUtilsTest.java
Normal file
58
src/test/java/com/owncloud/android/utils/PowerUtilsTest.java
Normal 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));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue