use of com.optimizely.ab.android.shared.ServiceScheduler in project android-sdk by optimizely.
the class EventRescheduler method onReceive.
/**
* Called when intent filter has kicked in.
* @param context current context
* @param intent broadcast intent received. Try and reschedule.
* @see BroadcastReceiver#onReceive(Context, Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
if (context != null && intent != null) {
ServiceScheduler serviceScheduler = new ServiceScheduler(context, new ServiceScheduler.PendingIntentFactory(context), LoggerFactory.getLogger(ServiceScheduler.class));
Intent eventServiceIntent = new Intent(context, EventIntentService.class);
reschedule(context, intent, eventServiceIntent, serviceScheduler);
} else {
logger.warn("Received invalid broadcast to event rescheduler");
}
}
use of com.optimizely.ab.android.shared.ServiceScheduler in project android-sdk by optimizely.
the class EventReschedulerTest method flushOnWifiConnectionIfScheduled.
@Test
public void flushOnWifiConnectionIfScheduled() {
final Intent eventServiceIntent = mock(Intent.class);
ServiceScheduler serviceScheduler = mock(ServiceScheduler.class);
when(intent.getAction()).thenReturn(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
when(intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)).thenReturn(true);
when(serviceScheduler.isScheduled(eventServiceIntent)).thenReturn(true);
rescheduler.reschedule(context, intent, eventServiceIntent, serviceScheduler);
verify(context).startService(eventServiceIntent);
verify(logger).info("Preemptively flushing events since wifi became available");
}
use of com.optimizely.ab.android.shared.ServiceScheduler in project android-sdk by optimizely.
the class OptimizelyManagerTest method injectOptimizelyNullListener.
@Test
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.LOLLIPOP)
public void injectOptimizelyNullListener() {
Context context = mock(Context.class);
PackageManager packageManager = mock(PackageManager.class);
when(context.getPackageName()).thenReturn("com.optly");
when(context.getApplicationContext()).thenReturn(context);
when(context.getApplicationContext().getPackageManager()).thenReturn(packageManager);
try {
when(packageManager.getPackageInfo("com.optly", 0)).thenReturn(mock(PackageInfo.class));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
UserProfileService userProfileService = mock(UserProfileService.class);
ServiceScheduler serviceScheduler = mock(ServiceScheduler.class);
ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
ArgumentCaptor<DefaultUserProfileService.StartCallback> callbackArgumentCaptor = ArgumentCaptor.forClass(DefaultUserProfileService.StartCallback.class);
optimizelyManager.setOptimizelyStartListener(null);
optimizelyManager.injectOptimizely(context, userProfileService, minDatafile);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
ServiceScheduler.PendingIntentFactory pendingIntentFactory = new ServiceScheduler.PendingIntentFactory(context);
Intent intent = new Intent(context, DatafileService.class);
intent.putExtra(DatafileService.EXTRA_DATAFILE_CONFIG, optimizelyManager.getDatafileConfig().toJSONString());
serviceScheduler.schedule(intent, optimizelyManager.getDatafileDownloadInterval() * 1000);
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail("Timed out");
}
verify(logger).info("No listener to send Optimizely to");
verify(serviceScheduler).schedule(captor.capture(), eq(TimeUnit.HOURS.toMillis(1L)));
Intent intent2 = captor.getValue();
assertTrue(intent2.getComponent().getShortClassName().contains("DatafileService"));
assertEquals(optimizelyManager.getDatafileConfig().toJSONString(), intent2.getStringExtra(DatafileService.EXTRA_DATAFILE_CONFIG));
}
use of com.optimizely.ab.android.shared.ServiceScheduler in project android-sdk by optimizely.
the class ServiceSchedulerTest method setup.
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Before
public void setup() {
context = mock(Context.class);
optlyStorage = mock(OptlyStorage.class);
alarmManager = mock(AlarmManager.class);
jobScheduler = mock(JobScheduler.class);
when(context.getApplicationContext()).thenReturn(context);
when(context.getSystemService(Context.ALARM_SERVICE)).thenReturn(alarmManager);
when(context.getSystemService(Context.JOB_SCHEDULER_SERVICE)).thenReturn(jobScheduler);
pendingIntentFactory = mock(ServiceScheduler.PendingIntentFactory.class);
logger = mock(Logger.class);
serviceScheduler = new ServiceScheduler(context, pendingIntentFactory, logger);
}
use of com.optimizely.ab.android.shared.ServiceScheduler in project android-sdk by optimizely.
the class DatafileServiceTest method testIntentExtraData.
@Test
@Ignore
public void testIntentExtraData() {
Context context = mock(Context.class);
when(context.getPackageName()).thenReturn("com.optly");
ServiceScheduler serviceScheduler = mock(ServiceScheduler.class);
ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
ServiceScheduler.PendingIntentFactory pendingIntentFactory = new ServiceScheduler.PendingIntentFactory(context);
Intent intent = new Intent(context, DatafileService.class);
intent.putExtra(DatafileService.EXTRA_DATAFILE_CONFIG, new DatafileConfig("1", null).toJSONString());
serviceScheduler.schedule(intent, TimeUnit.HOURS.toMillis(1L));
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail("Timed out");
}
verify(serviceScheduler).schedule(captor.capture(), eq(TimeUnit.HOURS.toMillis(1L)));
Intent intent2 = captor.getValue();
assertTrue(intent2.getComponent().getShortClassName().contains("DatafileService"));
}
Aggregations