FileDocCategorySizeDatePackage
ServicesTests.javaAPI DocAndroid 5.1 API3361Thu Mar 12 22:22:12 GMT 2015com.android.framework.multidexlegacytestservices.test

ServicesTests

public class ServicesTests extends android.test.InstrumentationTestCase
Run the tests with: adb shell am instrument -w com.android.framework.multidexlegacytestservices.test/android.test.InstrumentationTestRunner

Fields Summary
private static final String
SERVICE_BASE_ACTION
private static final int
MIN_SERVICE
private static final int
MAX_SERVICE
Constructors Summary
Methods Summary
private static booleanareAllServicesRunning(java.io.File tagetFilesDir)

        for (int i = MIN_SERVICE; i <= MAX_SERVICE; i++) {
            File completeFile = new File(tagetFilesDir, "Service" + i + ".complete");
            if (!completeFile.exists()) {
                return false;
            }
        }
        return true;

    
public voidtestStressConcurentFirstLaunch()



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