michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: package org.mozilla.gecko.background.announcements; michael@0: michael@0: import java.util.concurrent.BrokenBarrierException; michael@0: michael@0: import org.mozilla.gecko.background.common.GlobalConstants; michael@0: import org.mozilla.gecko.background.helpers.BackgroundServiceTestCase; michael@0: michael@0: import android.content.Intent; michael@0: import android.content.SharedPreferences; michael@0: michael@0: public class TestAnnouncementsBroadcastService michael@0: extends BackgroundServiceTestCase { michael@0: public static class MockAnnouncementsBroadcastService extends AnnouncementsBroadcastService { michael@0: @Override michael@0: protected SharedPreferences getSharedPreferences() { michael@0: return this.getSharedPreferences(sharedPrefsName, michael@0: GlobalConstants.SHARED_PREFERENCES_MODE); michael@0: } michael@0: michael@0: @Override michael@0: protected void onHandleIntent(Intent intent) { michael@0: super.onHandleIntent(intent); michael@0: try { michael@0: barrier.await(); michael@0: } catch (InterruptedException e) { michael@0: fail("Awaiting thread should not be interrupted."); michael@0: } catch (BrokenBarrierException e) { michael@0: // This will happen on timeout - do nothing. michael@0: } michael@0: } michael@0: } michael@0: michael@0: public TestAnnouncementsBroadcastService() { michael@0: super(MockAnnouncementsBroadcastService.class); michael@0: } michael@0: michael@0: @Override michael@0: public void setUp() throws Exception { michael@0: super.setUp(); michael@0: // We can't mock AlarmManager since it has a package-private constructor, so instead we reset michael@0: // the alarm by hand. michael@0: cancelAlarm(getServiceIntent()); michael@0: } michael@0: michael@0: @Override michael@0: public void tearDown() throws Exception { michael@0: cancelAlarm(getServiceIntent()); michael@0: AnnouncementsConstants.DISABLED = false; michael@0: super.tearDown(); michael@0: } michael@0: michael@0: protected Intent getServiceIntent() { michael@0: final Intent intent = new Intent(getContext(), AnnouncementsService.class); michael@0: return intent; michael@0: } michael@0: michael@0: public void testIgnoredServicePrefIntents() throws Exception { michael@0: // Intent without "enabled" extra is ignored. michael@0: intent.setAction(AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF); michael@0: startService(intent); michael@0: await(); michael@0: michael@0: assertFalse(isServiceAlarmSet(getServiceIntent())); michael@0: } michael@0: michael@0: public void testServicePrefIntentDisabled() throws Exception { michael@0: intent.setAction(AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF) michael@0: .putExtra("enabled", false); michael@0: startService(intent); michael@0: await(); michael@0: assertFalse(isServiceAlarmSet(getServiceIntent())); michael@0: } michael@0: michael@0: public void testServicePrefIntentEnabled() throws Exception { michael@0: intent.setAction(AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF) michael@0: .putExtra("enabled", true); michael@0: startService(intent); michael@0: await(); michael@0: assertTrue(isServiceAlarmSet(getServiceIntent())); michael@0: } michael@0: michael@0: public void testServicePrefCancelled() throws Exception { michael@0: intent.setAction(AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF) michael@0: .putExtra("enabled", true); michael@0: startService(intent); michael@0: await(); michael@0: michael@0: assertTrue(isServiceAlarmSet(getServiceIntent())); michael@0: barrier.reset(); michael@0: michael@0: intent.putExtra("enabled", false); michael@0: startService(intent); michael@0: await(); michael@0: assertFalse(isServiceAlarmSet(getServiceIntent())); michael@0: } michael@0: }