@Test @SmallTest public void testGetInitialState_nullIntent() { mockContextReturns(null); assertThat(mTracker.getInitialState(), is(nullValue())); }
@Override public Boolean getInitialState() { IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent intent = mAppContext.registerReceiver(null, intentFilter); if (intent == null) { Log.e(TAG, "getInitialState - null intent received"); return null; } int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, BATTERY_PLUGGED_NONE); int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPercentage = level / (float) scale; return (plugged != BATTERY_PLUGGED_NONE || status == BatteryManager.BATTERY_STATUS_UNKNOWN || batteryPercentage > BATTERY_LOW_PERCENTAGE); }
BatteryNotLowTracker extends BroadcastReceiverConstraintTracker<Boolean> { @Override public Boolean getInitialState() { IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent intent = mAppContext.registerReceiver(null, intentFilter); if (intent == null) { Log.e(TAG, "getInitialState - null intent received"); return null; } int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, BATTERY_PLUGGED_NONE); int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPercentage = level / (float) scale; return (plugged != BATTERY_PLUGGED_NONE || status == BatteryManager.BATTERY_STATUS_UNKNOWN || batteryPercentage > BATTERY_LOW_PERCENTAGE); } }
BatteryNotLowTracker extends BroadcastReceiverConstraintTracker<Boolean> { @Override public Boolean getInitialState() { IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent intent = mAppContext.registerReceiver(null, intentFilter); if (intent == null) { Log.e(TAG, "getInitialState - null intent received"); return null; } int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, BATTERY_PLUGGED_NONE); int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPercentage = level / (float) scale; return (plugged != BATTERY_PLUGGED_NONE || status == BatteryManager.BATTERY_STATUS_UNKNOWN || batteryPercentage > BATTERY_LOW_PERCENTAGE); } BatteryNotLowTracker(Context context); }
BatteryNotLowTracker extends BroadcastReceiverConstraintTracker<Boolean> { @Override public Boolean getInitialState() { IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent intent = mAppContext.registerReceiver(null, intentFilter); if (intent == null) { Log.e(TAG, "getInitialState - null intent received"); return null; } int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, BATTERY_PLUGGED_NONE); int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPercentage = level / (float) scale; return (plugged != BATTERY_PLUGGED_NONE || status == BatteryManager.BATTERY_STATUS_UNKNOWN || batteryPercentage > BATTERY_LOW_PERCENTAGE); } BatteryNotLowTracker(Context context); @Override Boolean getInitialState(); @Override IntentFilter getIntentFilter(); @Override void onBroadcastReceive(Context context, @NonNull Intent intent); }
BatteryNotLowTracker extends BroadcastReceiverConstraintTracker<Boolean> { @Override public Boolean getInitialState() { IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent intent = mAppContext.registerReceiver(null, intentFilter); if (intent == null) { Log.e(TAG, "getInitialState - null intent received"); return null; } int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, BATTERY_PLUGGED_NONE); int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPercentage = level / (float) scale; return (plugged != BATTERY_PLUGGED_NONE || status == BatteryManager.BATTERY_STATUS_UNKNOWN || batteryPercentage > BATTERY_LOW_PERCENTAGE); } BatteryNotLowTracker(Context context); @Override Boolean getInitialState(); @Override IntentFilter getIntentFilter(); @Override void onBroadcastReceive(Context context, @NonNull Intent intent);  }