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

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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.announcements;
michael@0 5
michael@0 6 import java.util.concurrent.BrokenBarrierException;
michael@0 7
michael@0 8 import org.mozilla.gecko.background.common.GlobalConstants;
michael@0 9 import org.mozilla.gecko.background.helpers.BackgroundServiceTestCase;
michael@0 10
michael@0 11 import android.content.Intent;
michael@0 12 import android.content.SharedPreferences;
michael@0 13
michael@0 14 public class TestAnnouncementsBroadcastService
michael@0 15 extends BackgroundServiceTestCase<TestAnnouncementsBroadcastService.MockAnnouncementsBroadcastService> {
michael@0 16 public static class MockAnnouncementsBroadcastService extends AnnouncementsBroadcastService {
michael@0 17 @Override
michael@0 18 protected SharedPreferences getSharedPreferences() {
michael@0 19 return this.getSharedPreferences(sharedPrefsName,
michael@0 20 GlobalConstants.SHARED_PREFERENCES_MODE);
michael@0 21 }
michael@0 22
michael@0 23 @Override
michael@0 24 protected void onHandleIntent(Intent intent) {
michael@0 25 super.onHandleIntent(intent);
michael@0 26 try {
michael@0 27 barrier.await();
michael@0 28 } catch (InterruptedException e) {
michael@0 29 fail("Awaiting thread should not be interrupted.");
michael@0 30 } catch (BrokenBarrierException e) {
michael@0 31 // This will happen on timeout - do nothing.
michael@0 32 }
michael@0 33 }
michael@0 34 }
michael@0 35
michael@0 36 public TestAnnouncementsBroadcastService() {
michael@0 37 super(MockAnnouncementsBroadcastService.class);
michael@0 38 }
michael@0 39
michael@0 40 @Override
michael@0 41 public void setUp() throws Exception {
michael@0 42 super.setUp();
michael@0 43 // We can't mock AlarmManager since it has a package-private constructor, so instead we reset
michael@0 44 // the alarm by hand.
michael@0 45 cancelAlarm(getServiceIntent());
michael@0 46 }
michael@0 47
michael@0 48 @Override
michael@0 49 public void tearDown() throws Exception {
michael@0 50 cancelAlarm(getServiceIntent());
michael@0 51 AnnouncementsConstants.DISABLED = false;
michael@0 52 super.tearDown();
michael@0 53 }
michael@0 54
michael@0 55 protected Intent getServiceIntent() {
michael@0 56 final Intent intent = new Intent(getContext(), AnnouncementsService.class);
michael@0 57 return intent;
michael@0 58 }
michael@0 59
michael@0 60 public void testIgnoredServicePrefIntents() throws Exception {
michael@0 61 // Intent without "enabled" extra is ignored.
michael@0 62 intent.setAction(AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF);
michael@0 63 startService(intent);
michael@0 64 await();
michael@0 65
michael@0 66 assertFalse(isServiceAlarmSet(getServiceIntent()));
michael@0 67 }
michael@0 68
michael@0 69 public void testServicePrefIntentDisabled() throws Exception {
michael@0 70 intent.setAction(AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF)
michael@0 71 .putExtra("enabled", false);
michael@0 72 startService(intent);
michael@0 73 await();
michael@0 74 assertFalse(isServiceAlarmSet(getServiceIntent()));
michael@0 75 }
michael@0 76
michael@0 77 public void testServicePrefIntentEnabled() throws Exception {
michael@0 78 intent.setAction(AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF)
michael@0 79 .putExtra("enabled", true);
michael@0 80 startService(intent);
michael@0 81 await();
michael@0 82 assertTrue(isServiceAlarmSet(getServiceIntent()));
michael@0 83 }
michael@0 84
michael@0 85 public void testServicePrefCancelled() throws Exception {
michael@0 86 intent.setAction(AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF)
michael@0 87 .putExtra("enabled", true);
michael@0 88 startService(intent);
michael@0 89 await();
michael@0 90
michael@0 91 assertTrue(isServiceAlarmSet(getServiceIntent()));
michael@0 92 barrier.reset();
michael@0 93
michael@0 94 intent.putExtra("enabled", false);
michael@0 95 startService(intent);
michael@0 96 await();
michael@0 97 assertFalse(isServiceAlarmSet(getServiceIntent()));
michael@0 98 }
michael@0 99 }

mercurial