mobile/android/tests/background/junit3/src/helpers/BackgroundServiceTestCase.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 package org.mozilla.gecko.background.helpers;
michael@0 5
michael@0 6 import android.app.AlarmManager;
michael@0 7 import android.app.PendingIntent;
michael@0 8 import android.app.Service;
michael@0 9 import android.content.Context;
michael@0 10 import android.content.Intent;
michael@0 11 import android.content.SharedPreferences;
michael@0 12 import android.test.ServiceTestCase;
michael@0 13 import java.util.concurrent.BrokenBarrierException;
michael@0 14 import java.util.concurrent.CyclicBarrier;
michael@0 15 import java.util.UUID;
michael@0 16
michael@0 17 import org.mozilla.gecko.background.common.GlobalConstants;
michael@0 18
michael@0 19 /**
michael@0 20 * An abstract test class for testing background services. Since we have to wait for background
michael@0 21 * services to finish before asserting the changed state, this class provides much of the
michael@0 22 * functionality to do this. Extending classes need still need to implement some of the components -
michael@0 23 * see {@link TestHealthReportBroadcastService} for an example.
michael@0 24 */
michael@0 25 public abstract class BackgroundServiceTestCase<T extends Service> extends ServiceTestCase<T> {
michael@0 26 private static final String SHARED_PREFS_PREFIX = "BackgroundServiceTestCase-";
michael@0 27 // Ideally, this would not be static so multiple test classes can be run in parallel. However,
michael@0 28 // mServiceClass can only retrieve this reference statically because mServiceClass cannot get a
michael@0 29 // reference to the Test* class as ServiceTestCase instantiates it via reflection and we can't
michael@0 30 // pass it as a constructor arg.
michael@0 31 protected static String sharedPrefsName;
michael@0 32
michael@0 33 private final Class<T> mServiceClass;
michael@0 34
michael@0 35 protected static CyclicBarrier barrier;
michael@0 36 protected Intent intent;
michael@0 37
michael@0 38 public BackgroundServiceTestCase(Class<T> serviceClass) {
michael@0 39 super(serviceClass);
michael@0 40 mServiceClass = serviceClass;
michael@0 41 }
michael@0 42
michael@0 43 @Override
michael@0 44 public void setUp() throws Exception {
michael@0 45 barrier = new CyclicBarrier(2);
michael@0 46 intent = new Intent(getContext(), mServiceClass);
michael@0 47 sharedPrefsName = SHARED_PREFS_PREFIX + mServiceClass.getName() + "-" + UUID.randomUUID();
michael@0 48 }
michael@0 49
michael@0 50 @Override
michael@0 51 public void tearDown() throws Exception {
michael@0 52 barrier = null;
michael@0 53 intent = null;
michael@0 54 clearSharedPrefs(); // Not necessary but reduces file system cruft.
michael@0 55 }
michael@0 56
michael@0 57 protected SharedPreferences getSharedPreferences() {
michael@0 58 return getContext().getSharedPreferences(sharedPrefsName,
michael@0 59 GlobalConstants.SHARED_PREFERENCES_MODE);
michael@0 60 }
michael@0 61
michael@0 62 protected void clearSharedPrefs() {
michael@0 63 getSharedPreferences().edit()
michael@0 64 .clear()
michael@0 65 .commit();
michael@0 66 }
michael@0 67
michael@0 68 protected void await() {
michael@0 69 try {
michael@0 70 barrier.await();
michael@0 71 } catch (InterruptedException e) {
michael@0 72 fail("Test runner thread should not be interrupted.");
michael@0 73 } catch (BrokenBarrierException e) {
michael@0 74 fail("Background services should not timeout or be interrupted");
michael@0 75 }
michael@0 76 }
michael@0 77
michael@0 78 protected void cancelAlarm(Intent intent) {
michael@0 79 final AlarmManager am = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
michael@0 80 final PendingIntent pi = PendingIntent.getService(getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
michael@0 81 am.cancel(pi);
michael@0 82 pi.cancel();
michael@0 83 }
michael@0 84
michael@0 85 protected boolean isServiceAlarmSet(Intent intent) {
michael@0 86 return PendingIntent.getService(getContext(), 0, intent, PendingIntent.FLAG_NO_CREATE) != null;
michael@0 87 }
michael@0 88 }

mercurial