forked from jensstein/oandbackup
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
219 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
src/main/assets | ||
build | ||
local.properties | ||
sonarqube-env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/androidTest/java/dk/jens/backup/schedules/HandleAlarmsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package dk.jens.backup.schedules; | ||
|
||
import android.app.AlarmManager; | ||
import android.app.PendingIntent; | ||
import android.content.Intent; | ||
import android.support.test.rule.ActivityTestRule; | ||
import android.support.test.runner.AndroidJUnit4; | ||
import dk.jens.backup.AbstractInstrumentationTest; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class HandleAlarmsTest extends AbstractInstrumentationTest { | ||
@Mock | ||
private AlarmManager alarmManager = mock(AlarmManager.class); | ||
|
||
@Mock | ||
private HandleAlarms.DeviceIdleChecker deviceIdleChecker = mock(HandleAlarms.DeviceIdleChecker.class); | ||
|
||
@Rule | ||
public ActivityTestRule<Scheduler> schedulerActivityTestRule = | ||
new ActivityTestRule<>(Scheduler.class, false, true); | ||
|
||
@Test | ||
public void test_setAlarm_ignoringOptimizations() { | ||
final HandleAlarms handleAlarms = new HandleAlarms( | ||
schedulerActivityTestRule.getActivity()); | ||
handleAlarms.alarmManager = alarmManager; | ||
handleAlarms.deviceIdleChecker = deviceIdleChecker; | ||
|
||
when(deviceIdleChecker.isIdleModeSupported()).thenReturn(true); | ||
when(deviceIdleChecker.isIgnoringBatteryOptimizations()) | ||
.thenReturn(true); | ||
|
||
handleAlarms.setAlarm(2, 1020); | ||
final Intent intent = new Intent( | ||
schedulerActivityTestRule.getActivity(), AlarmReceiver.class); | ||
intent.putExtra("id", 2); | ||
final PendingIntent pendingIntent = | ||
PendingIntent.getBroadcast(schedulerActivityTestRule.getActivity(), | ||
2, intent, 0); | ||
verify(alarmManager).setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, | ||
1020L, pendingIntent); | ||
} | ||
|
||
@Test | ||
public void test_setAlarm_hasBatteryOptimizations() { | ||
final HandleAlarms handleAlarms = new HandleAlarms( | ||
schedulerActivityTestRule.getActivity()); | ||
handleAlarms.alarmManager = alarmManager; | ||
handleAlarms.deviceIdleChecker = deviceIdleChecker; | ||
|
||
when(deviceIdleChecker.isIdleModeSupported()).thenReturn(true); | ||
when(deviceIdleChecker.isIgnoringBatteryOptimizations()) | ||
.thenReturn(false); | ||
|
||
handleAlarms.setAlarm(2, 1020); | ||
final Intent intent = new Intent( | ||
schedulerActivityTestRule.getActivity(), AlarmReceiver.class); | ||
intent.putExtra("id", 2); | ||
final PendingIntent pendingIntent = | ||
PendingIntent.getBroadcast(schedulerActivityTestRule.getActivity(), | ||
2, intent, 0); | ||
verify(alarmManager).set(AlarmManager.RTC_WAKEUP, | ||
1020L, pendingIntent); | ||
} | ||
|
||
@Test | ||
public void test_setAlarm_idleModeNotSupported() { | ||
final HandleAlarms handleAlarms = new HandleAlarms( | ||
schedulerActivityTestRule.getActivity()); | ||
handleAlarms.alarmManager = alarmManager; | ||
handleAlarms.deviceIdleChecker = deviceIdleChecker; | ||
|
||
when(deviceIdleChecker.isIdleModeSupported()).thenReturn(false); | ||
when(deviceIdleChecker.isIgnoringBatteryOptimizations()) | ||
.thenReturn(false); | ||
|
||
handleAlarms.setAlarm(2, 1020); | ||
final Intent intent = new Intent( | ||
schedulerActivityTestRule.getActivity(), AlarmReceiver.class); | ||
intent.putExtra("id", 2); | ||
final PendingIntent pendingIntent = | ||
PendingIntent.getBroadcast(schedulerActivityTestRule.getActivity(), | ||
2, intent, 0); | ||
verify(alarmManager).set(AlarmManager.RTC_WAKEUP, | ||
1020L, pendingIntent); | ||
} | ||
|
||
@Test | ||
public void test_timeUntilNextEvent() { | ||
// 1561791600000 => 2019-06-29T09:00:00 | ||
final long now = 1561791600000L; | ||
final long nextEvent = HandleAlarms.timeUntilNextEvent(1, 10, now, | ||
now); | ||
// 90000000 => 25h | ||
assertThat(nextEvent, is(90000000L)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -xe | ||
|
||
FLAVOUR=arm64 | ||
|
||
function start_test() { | ||
test_c= | ||
if test ! -z $1; then | ||
test_c="-e class $1" | ||
fi | ||
./gradlew test${FLAVOUR^}DebugUnitTest assemble${FLAVOUR^}Debug assemble${FLAVOUR^}DebugAndroidTest | ||
adb push build/outputs/apk/${FLAVOUR}/debug/oandbackup-${FLAVOUR}-debug.apk /data/local/tmp/dk.jens.backup | ||
adb shell pm install -t -r "/data/local/tmp/dk.jens.backup" | ||
adb push build/outputs/apk/androidTest/${FLAVOUR}/debug/oandbackup-${FLAVOUR}-debug-androidTest.apk /data/local/tmp/dk.jens.backup.test | ||
adb shell pm install -t -r "/data/local/tmp/dk.jens.backup.test" | ||
|
||
adb shell am instrument -w -r -e debug false ${test_c} dk.jens.backup.test/android.support.test.runner.AndroidJUnitRunner | ||
} | ||
|
||
function init() { | ||
adb uninstall dk.jens.backup || true | ||
start_test "dk.jens.backup.TestHelper" | ||
} | ||
|
||
function sonarqube() { | ||
if test -e sonarqube-env; then | ||
source sonarqube-env | ||
fi | ||
./gradlew create${FLAVOUR^}DebugCoverageReport sonarqube \ | ||
-Dsonar.host.url=$SONARQUBE_HOST \ | ||
-Dsonar.login=$SONARQUBE_TOKEN | ||
} | ||
|
||
action= | ||
while [ $# -gt 0 ]; do | ||
case "$1" in | ||
"--test-class") | ||
test_class="$2" | ||
shift | ||
;; | ||
"--flavour") | ||
FLAVOUR="$2" | ||
shift | ||
;; | ||
"--clean") | ||
./gradlew clean | ||
;; | ||
"--sonarqube") | ||
action="sonarqube" | ||
;; | ||
"-h"|"--help") | ||
printf "usage: $0 [-h] [--test-class CLASS] [--flavour FLAVOUR] [--clean] [--sonarqube]" | ||
exit 0 | ||
;; | ||
*) | ||
printf "unknown option $1\n" $1 | ||
exit 1 | ||
;; | ||
esac | ||
shift | ||
done | ||
|
||
init | ||
|
||
if test ! -z $action && test $action = "sonarqube"; then | ||
sonarqube | ||
else | ||
start_test "$test_class" | ||
fi |