File targetFilesDir = getInstrumentation().getTargetContext().getFilesDir();
for (int i = MIN_SERVICE; i <= MAX_SERVICE; i++) {
File resultFile = new File(targetFilesDir, "Service" + i);
resultFile.delete();
assertFalse("Failed to delete result file '" + resultFile.getAbsolutePath() + "'.",
resultFile.exists());
File completeFile = new File(targetFilesDir, "Service" + i + ".complete");
completeFile.delete();
assertFalse("Failed to delete completion file '" + completeFile.getAbsolutePath() +
"'.", completeFile.exists());
}
for (int i = MIN_SERVICE; i <= MAX_SERVICE; i++) {
getInstrumentation().getContext().startService(new Intent(SERVICE_BASE_ACTION + i));
try {
Thread.sleep((i - 1) * (1 << (i / 5)));
} catch (InterruptedException e) {
}
}
Log.i("ServicesTests", "start sleeping");
int attempt = 0;
int maxAttempt = 50; // 10 is enough for a nexus S
do {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
attempt ++;
if (attempt >= maxAttempt) {
fail();
}
} while (!areAllServicesRunning(targetFilesDir));
for (int i = MIN_SERVICE; i <= MAX_SERVICE; i++) {
File resultFile = new File(targetFilesDir, "Service" + i);
assertTrue("Service" + i + " never completed.", resultFile.isFile());
assertEquals("Service" + i + " was restarted.", 8L, resultFile.length());
}