Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.framework.multidexlegacytestservices.test;
     18 
     19 import android.content.Intent;
     20 import android.test.InstrumentationTestCase;
     21 import android.util.Log;
     22 
     23 import java.io.File;
     24 
     25 /**
     26  * Run the tests with: <code>adb shell am instrument -w
     27  com.android.framework.multidexlegacytestservices.test/android.test.InstrumentationTestRunner
     28 </code>
     29  */
     30 public class ServicesTests extends InstrumentationTestCase {
     31     private static final String SERVICE_BASE_ACTION =
     32             "com.android.framework.multidexlegacytestservices.action.Service";
     33     private static final int MIN_SERVICE = 1;
     34     private static final int MAX_SERVICE = 19;
     35 
     36 
     37     public void testStressConcurentFirstLaunch() {
     38         File targetFilesDir = getInstrumentation().getTargetContext().getFilesDir();
     39         for (int i = MIN_SERVICE; i <= MAX_SERVICE; i++) {
     40             File resultFile = new File(targetFilesDir, "Service" + i);
     41             resultFile.delete();
     42             assertFalse("Failed to delete result file '" + resultFile.getAbsolutePath() + "'.",
     43                     resultFile.exists());
     44             File completeFile = new File(targetFilesDir, "Service" + i + ".complete");
     45             completeFile.delete();
     46             assertFalse("Failed to delete completion file '" + completeFile.getAbsolutePath() +
     47                     "'.", completeFile.exists());
     48        }
     49         for (int i = MIN_SERVICE; i <= MAX_SERVICE; i++) {
     50             getInstrumentation().getContext().startService(new Intent(SERVICE_BASE_ACTION + i));
     51             try {
     52                 Thread.sleep((i - 1) * (1 << (i / 5)));
     53             } catch (InterruptedException e) {
     54             }
     55        }
     56 
     57 
     58         Log.i("ServicesTests", "start sleeping");
     59         int attempt = 0;
     60         int maxAttempt = 50; // 10 is enough for a nexus S
     61         do {
     62             try {
     63                 Thread.sleep(5000);
     64             } catch (InterruptedException e) {
     65             }
     66             attempt ++;
     67             if (attempt >= maxAttempt) {
     68                 fail();
     69             }
     70         } while (!areAllServicesRunning(targetFilesDir));
     71 
     72         for (int i = MIN_SERVICE; i <= MAX_SERVICE; i++) {
     73             File resultFile = new File(targetFilesDir, "Service" + i);
     74             assertTrue("Service" + i + " never completed.", resultFile.isFile());
     75             assertEquals("Service" + i + " was restarted.", 8L, resultFile.length());
     76         }
     77     }
     78 
     79     private static boolean areAllServicesRunning(File tagetFilesDir) {
     80         for (int i = MIN_SERVICE; i <= MAX_SERVICE; i++) {
     81             File completeFile = new File(tagetFilesDir, "Service" + i + ".complete");
     82             if (!completeFile.exists()) {
     83                 return false;
     84             }
     85         }
     86         return true;
     87 
     88     }
     89 }
     90