mobile/android/tests/background/junit3/src/healthreport/TestHealthReportBroadcastService.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/healthreport/TestHealthReportBroadcastService.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,145 @@
     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.healthreport;
     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.healthreport.prune.HealthReportPruneService;
    1.13 +import org.mozilla.gecko.background.healthreport.upload.HealthReportUploadService;
    1.14 +import org.mozilla.gecko.background.helpers.BackgroundServiceTestCase;
    1.15 +
    1.16 +import android.content.Intent;
    1.17 +import android.content.SharedPreferences;
    1.18 +
    1.19 +public class TestHealthReportBroadcastService
    1.20 +    extends BackgroundServiceTestCase<TestHealthReportBroadcastService.MockHealthReportBroadcastService> {
    1.21 +  public static class MockHealthReportBroadcastService extends HealthReportBroadcastService {
    1.22 +    @Override
    1.23 +    protected SharedPreferences getSharedPreferences() {
    1.24 +      return this.getSharedPreferences(sharedPrefsName, GlobalConstants.SHARED_PREFERENCES_MODE);
    1.25 +    }
    1.26 +
    1.27 +    @Override
    1.28 +    protected void onHandleIntent(Intent intent) {
    1.29 +      super.onHandleIntent(intent);
    1.30 +      try {
    1.31 +        barrier.await();
    1.32 +      } catch (InterruptedException e) {
    1.33 +        fail("Awaiting Service thread should not be interrupted.");
    1.34 +      } catch (BrokenBarrierException e) {
    1.35 +        // This will happen on timeout - do nothing.
    1.36 +      }
    1.37 +    }
    1.38 +  }
    1.39 +
    1.40 +  public TestHealthReportBroadcastService() {
    1.41 +    super(MockHealthReportBroadcastService.class);
    1.42 +  }
    1.43 +
    1.44 +  @Override
    1.45 +  public void setUp() throws Exception {
    1.46 +    super.setUp();
    1.47 +    // We can't mock AlarmManager since it has a package-private constructor, so instead we reset
    1.48 +    // the alarm by hand.
    1.49 +    cancelAlarm(getUploadIntent());
    1.50 +  }
    1.51 +
    1.52 +  @Override
    1.53 +  public void tearDown() throws Exception {
    1.54 +    cancelAlarm(getUploadIntent());
    1.55 +    super.tearDown();
    1.56 +  }
    1.57 +
    1.58 +  protected Intent getUploadIntent() {
    1.59 +    final Intent intent = new Intent(getContext(), HealthReportUploadService.class);
    1.60 +    intent.setAction("upload");
    1.61 +    return intent;
    1.62 +  }
    1.63 +
    1.64 +  protected Intent getPruneIntent() {
    1.65 +    final Intent intent = new Intent(getContext(), HealthReportPruneService.class);
    1.66 +    intent.setAction("prune");
    1.67 +    return intent;
    1.68 +  }
    1.69 +
    1.70 +  public void testIgnoredUploadPrefIntents() throws Exception {
    1.71 +    // Intent without "upload" extra is ignored.
    1.72 +    intent.setAction(HealthReportConstants.ACTION_HEALTHREPORT_UPLOAD_PREF)
    1.73 +        .putExtra("profileName", "profileName")
    1.74 +        .putExtra("profilePath", "profilePath");
    1.75 +    startService(intent);
    1.76 +    await();
    1.77 +
    1.78 +    assertFalse(isServiceAlarmSet(getUploadIntent()));
    1.79 +    barrier.reset();
    1.80 +
    1.81 +    // No "profileName" extra.
    1.82 +    intent.putExtra("enabled", true)
    1.83 +        .removeExtra("profileName");
    1.84 +    startService(intent);
    1.85 +    await();
    1.86 +
    1.87 +    assertFalse(isServiceAlarmSet(getUploadIntent()));
    1.88 +    barrier.reset();
    1.89 +
    1.90 +    // No "profilePath" extra.
    1.91 +    intent.putExtra("profileName", "profileName")
    1.92 +        .removeExtra("profilePath");
    1.93 +    startService(intent);
    1.94 +    await();
    1.95 +
    1.96 +    assertFalse(isServiceAlarmSet(getUploadIntent()));
    1.97 +  }
    1.98 +
    1.99 +  public void testUploadPrefIntentDisabled() throws Exception {
   1.100 +    intent.setAction(HealthReportConstants.ACTION_HEALTHREPORT_UPLOAD_PREF)
   1.101 +        .putExtra("enabled", false)
   1.102 +        .putExtra("profileName", "profileName")
   1.103 +        .putExtra("profilePath", "profilePath");
   1.104 +    startService(intent);
   1.105 +    await();
   1.106 +
   1.107 +    assertFalse(isServiceAlarmSet(getUploadIntent()));
   1.108 +  }
   1.109 +
   1.110 +  public void testUploadPrefIntentEnabled() throws Exception {
   1.111 +    intent.setAction(HealthReportConstants.ACTION_HEALTHREPORT_UPLOAD_PREF)
   1.112 +        .putExtra("enabled", true)
   1.113 +        .putExtra("profileName", "profileName")
   1.114 +        .putExtra("profilePath", "profilePath");
   1.115 +    startService(intent);
   1.116 +    await();
   1.117 +
   1.118 +    assertTrue(isServiceAlarmSet(getUploadIntent()));
   1.119 +  }
   1.120 +
   1.121 +  public void testUploadServiceCancelled() throws Exception {
   1.122 +    intent.setAction(HealthReportConstants.ACTION_HEALTHREPORT_UPLOAD_PREF)
   1.123 +        .putExtra("enabled", true)
   1.124 +        .putExtra("profileName", "profileName")
   1.125 +        .putExtra("profilePath", "profilePath");
   1.126 +    startService(intent);
   1.127 +    await();
   1.128 +
   1.129 +    assertTrue(isServiceAlarmSet(getUploadIntent()));
   1.130 +    barrier.reset();
   1.131 +
   1.132 +    intent.putExtra("enabled", false);
   1.133 +    startService(intent);
   1.134 +    await();
   1.135 +
   1.136 +    assertFalse(isServiceAlarmSet(getUploadIntent()));
   1.137 +  }
   1.138 +
   1.139 +  public void testPruneService() throws Exception {
   1.140 +    intent.setAction(HealthReportConstants.ACTION_HEALTHREPORT_PRUNE)
   1.141 +        .putExtra("profileName", "profileName")
   1.142 +        .putExtra("profilePath", "profilePath");
   1.143 +    startService(intent);
   1.144 +    await();
   1.145 +    assertTrue(isServiceAlarmSet(getPruneIntent()));
   1.146 +    barrier.reset();
   1.147 +  }
   1.148 +}

mercurial