mobile/android/tests/background/junit3/src/announcements/TestAnnouncementsBroadcastService.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/tests/background/junit3/src/announcements/TestAnnouncementsBroadcastService.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,99 @@
     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.announcements;
     1.8 +
     1.9 +import java.util.concurrent.BrokenBarrierException;
    1.10 +
    1.11 +import org.mozilla.gecko.background.common.GlobalConstants;
    1.12 +import org.mozilla.gecko.background.helpers.BackgroundServiceTestCase;
    1.13 +
    1.14 +import android.content.Intent;
    1.15 +import android.content.SharedPreferences;
    1.16 +
    1.17 +public class TestAnnouncementsBroadcastService
    1.18 +    extends BackgroundServiceTestCase<TestAnnouncementsBroadcastService.MockAnnouncementsBroadcastService> {
    1.19 +  public static class MockAnnouncementsBroadcastService extends AnnouncementsBroadcastService {
    1.20 +    @Override
    1.21 +    protected SharedPreferences getSharedPreferences() {
    1.22 +      return this.getSharedPreferences(sharedPrefsName,
    1.23 +          GlobalConstants.SHARED_PREFERENCES_MODE);
    1.24 +    }
    1.25 +
    1.26 +    @Override
    1.27 +    protected void onHandleIntent(Intent intent) {
    1.28 +      super.onHandleIntent(intent);
    1.29 +      try {
    1.30 +        barrier.await();
    1.31 +      } catch (InterruptedException e) {
    1.32 +        fail("Awaiting thread should not be interrupted.");
    1.33 +      } catch (BrokenBarrierException e) {
    1.34 +        // This will happen on timeout - do nothing.
    1.35 +      }
    1.36 +    }
    1.37 +  }
    1.38 +
    1.39 +  public TestAnnouncementsBroadcastService() {
    1.40 +    super(MockAnnouncementsBroadcastService.class);
    1.41 +  }
    1.42 +
    1.43 +  @Override
    1.44 +  public void setUp() throws Exception {
    1.45 +    super.setUp();
    1.46 +    // We can't mock AlarmManager since it has a package-private constructor, so instead we reset
    1.47 +    // the alarm by hand.
    1.48 +    cancelAlarm(getServiceIntent());
    1.49 +  }
    1.50 +
    1.51 +  @Override
    1.52 +  public void tearDown() throws Exception {
    1.53 +    cancelAlarm(getServiceIntent());
    1.54 +    AnnouncementsConstants.DISABLED = false;
    1.55 +    super.tearDown();
    1.56 +  }
    1.57 +
    1.58 +  protected Intent getServiceIntent() {
    1.59 +    final Intent intent = new Intent(getContext(), AnnouncementsService.class);
    1.60 +    return intent;
    1.61 +  }
    1.62 +
    1.63 +  public void testIgnoredServicePrefIntents() throws Exception {
    1.64 +    // Intent without "enabled" extra is ignored.
    1.65 +    intent.setAction(AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF);
    1.66 +    startService(intent);
    1.67 +    await();
    1.68 +
    1.69 +    assertFalse(isServiceAlarmSet(getServiceIntent()));
    1.70 +  }
    1.71 +
    1.72 +  public void testServicePrefIntentDisabled() throws Exception {
    1.73 +    intent.setAction(AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF)
    1.74 +        .putExtra("enabled", false);
    1.75 +    startService(intent);
    1.76 +    await();
    1.77 +    assertFalse(isServiceAlarmSet(getServiceIntent()));
    1.78 +  }
    1.79 +
    1.80 +  public void testServicePrefIntentEnabled() throws Exception {
    1.81 +    intent.setAction(AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF)
    1.82 +        .putExtra("enabled", true);
    1.83 +    startService(intent);
    1.84 +    await();
    1.85 +    assertTrue(isServiceAlarmSet(getServiceIntent()));
    1.86 +  }
    1.87 +
    1.88 +  public void testServicePrefCancelled() throws Exception {
    1.89 +    intent.setAction(AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF)
    1.90 +        .putExtra("enabled", true);
    1.91 +    startService(intent);
    1.92 +    await();
    1.93 +
    1.94 +    assertTrue(isServiceAlarmSet(getServiceIntent()));
    1.95 +    barrier.reset();
    1.96 +
    1.97 +    intent.putExtra("enabled", false);
    1.98 +    startService(intent);
    1.99 +    await();
   1.100 +    assertFalse(isServiceAlarmSet(getServiceIntent()));
   1.101 +  }
   1.102 +}

mercurial