1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/tests/background/junit3/src/helpers/BackgroundServiceTestCase.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +package org.mozilla.gecko.background.helpers; 1.8 + 1.9 +import android.app.AlarmManager; 1.10 +import android.app.PendingIntent; 1.11 +import android.app.Service; 1.12 +import android.content.Context; 1.13 +import android.content.Intent; 1.14 +import android.content.SharedPreferences; 1.15 +import android.test.ServiceTestCase; 1.16 +import java.util.concurrent.BrokenBarrierException; 1.17 +import java.util.concurrent.CyclicBarrier; 1.18 +import java.util.UUID; 1.19 + 1.20 +import org.mozilla.gecko.background.common.GlobalConstants; 1.21 + 1.22 +/** 1.23 + * An abstract test class for testing background services. Since we have to wait for background 1.24 + * services to finish before asserting the changed state, this class provides much of the 1.25 + * functionality to do this. Extending classes need still need to implement some of the components - 1.26 + * see {@link TestHealthReportBroadcastService} for an example. 1.27 + */ 1.28 +public abstract class BackgroundServiceTestCase<T extends Service> extends ServiceTestCase<T> { 1.29 + private static final String SHARED_PREFS_PREFIX = "BackgroundServiceTestCase-"; 1.30 + // Ideally, this would not be static so multiple test classes can be run in parallel. However, 1.31 + // mServiceClass can only retrieve this reference statically because mServiceClass cannot get a 1.32 + // reference to the Test* class as ServiceTestCase instantiates it via reflection and we can't 1.33 + // pass it as a constructor arg. 1.34 + protected static String sharedPrefsName; 1.35 + 1.36 + private final Class<T> mServiceClass; 1.37 + 1.38 + protected static CyclicBarrier barrier; 1.39 + protected Intent intent; 1.40 + 1.41 + public BackgroundServiceTestCase(Class<T> serviceClass) { 1.42 + super(serviceClass); 1.43 + mServiceClass = serviceClass; 1.44 + } 1.45 + 1.46 + @Override 1.47 + public void setUp() throws Exception { 1.48 + barrier = new CyclicBarrier(2); 1.49 + intent = new Intent(getContext(), mServiceClass); 1.50 + sharedPrefsName = SHARED_PREFS_PREFIX + mServiceClass.getName() + "-" + UUID.randomUUID(); 1.51 + } 1.52 + 1.53 + @Override 1.54 + public void tearDown() throws Exception { 1.55 + barrier = null; 1.56 + intent = null; 1.57 + clearSharedPrefs(); // Not necessary but reduces file system cruft. 1.58 + } 1.59 + 1.60 + protected SharedPreferences getSharedPreferences() { 1.61 + return getContext().getSharedPreferences(sharedPrefsName, 1.62 + GlobalConstants.SHARED_PREFERENCES_MODE); 1.63 + } 1.64 + 1.65 + protected void clearSharedPrefs() { 1.66 + getSharedPreferences().edit() 1.67 + .clear() 1.68 + .commit(); 1.69 + } 1.70 + 1.71 + protected void await() { 1.72 + try { 1.73 + barrier.await(); 1.74 + } catch (InterruptedException e) { 1.75 + fail("Test runner thread should not be interrupted."); 1.76 + } catch (BrokenBarrierException e) { 1.77 + fail("Background services should not timeout or be interrupted"); 1.78 + } 1.79 + } 1.80 + 1.81 + protected void cancelAlarm(Intent intent) { 1.82 + final AlarmManager am = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); 1.83 + final PendingIntent pi = PendingIntent.getService(getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 1.84 + am.cancel(pi); 1.85 + pi.cancel(); 1.86 + } 1.87 + 1.88 + protected boolean isServiceAlarmSet(Intent intent) { 1.89 + return PendingIntent.getService(getContext(), 0, intent, PendingIntent.FLAG_NO_CREATE) != null; 1.90 + } 1.91 +}