@Test public void testSnapshotAfterValidBroadcasts() { ShadowSystemClock.setElapsedRealtime(5000); when(mContext.registerReceiver( Matchers.isNull(BroadcastReceiver.class), Matchers.any(IntentFilter.class))) .thenReturn(createBatteryIntent(BatteryManager.BATTERY_STATUS_CHARGING, 50, 100)); when(mContext.registerReceiver( Matchers.isNotNull(BroadcastReceiver.class), Matchers.any(IntentFilter.class))) .thenReturn(null); DeviceBatteryMetrics metrics = new DeviceBatteryMetrics(); DeviceBatteryMetricsCollector collector = new DeviceBatteryMetricsCollector(mContext); ArgumentCaptor<BroadcastReceiver> captor = ArgumentCaptor.forClass(BroadcastReceiver.class); verify(mContext, times(2)).registerReceiver(captor.capture(), Matchers.any(IntentFilter.class)); final List<BroadcastReceiver> receivers = captor.getAllValues(); assertThat(receivers.size()).isEqualTo(2); BroadcastReceiver receiver = receivers.get(1); assertThat(receiver).isNotNull(); ShadowSystemClock.setElapsedRealtime(9000); receiver.onReceive(mContext, new Intent(Intent.ACTION_POWER_DISCONNECTED)); ShadowSystemClock.setElapsedRealtime(15000); receiver.onReceive(mContext, new Intent(Intent.ACTION_POWER_CONNECTED)); ShadowSystemClock.setElapsedRealtime(22000); receiver.onReceive(mContext, new Intent(Intent.ACTION_POWER_DISCONNECTED)); ShadowSystemClock.setElapsedRealtime(28000); when(mContext.registerReceiver( Matchers.isNull(BroadcastReceiver.class), Matchers.any(IntentFilter.class))) .thenReturn(createBatteryIntent(BatteryManager.BATTERY_STATUS_CHARGING, 20, 100)); collector.getSnapshot(metrics); verifySnapshot(metrics, 20, 12000, 11000); ShadowSystemClock.setElapsedRealtime(30000); receiver.onReceive(mContext, new Intent(Intent.ACTION_POWER_CONNECTED)); ShadowSystemClock.setElapsedRealtime(42000); when(mContext.registerReceiver( Matchers.isNull(BroadcastReceiver.class), Matchers.any(IntentFilter.class))) .thenReturn(createBatteryIntent(BatteryManager.BATTERY_STATUS_CHARGING, 30, 100)); collector.getSnapshot(metrics); verifySnapshot(metrics, 30, 14000, 23000); ShadowSystemClock.setElapsedRealtime(50000); receiver.onReceive(mContext, new Intent(Intent.ACTION_POWER_CONNECTED)); ShadowSystemClock.setElapsedRealtime(51000); when(mContext.registerReceiver( Matchers.isNull(BroadcastReceiver.class), Matchers.any(IntentFilter.class))) .thenReturn(createBatteryIntent(BatteryManager.BATTERY_STATUS_CHARGING, 40, 100)); collector.getSnapshot(metrics); verifySnapshot(metrics, 40, 14000, 32000); ShadowSystemClock.setElapsedRealtime(55000); receiver.onReceive(mContext, new Intent(Intent.ACTION_POWER_DISCONNECTED)); ShadowSystemClock.setElapsedRealtime(59000); receiver.onReceive(mContext, new Intent(Intent.ACTION_POWER_DISCONNECTED)); ShadowSystemClock.setElapsedRealtime(65000); when(mContext.registerReceiver( Matchers.isNull(BroadcastReceiver.class), Matchers.any(IntentFilter.class))) .thenReturn(createBatteryIntent(BatteryManager.BATTERY_STATUS_CHARGING, 60, 100)); collector.getSnapshot(metrics); verifySnapshot(metrics, 60, 24000, 36000); }
@Override @ThreadSafe(enableChecks = false) public boolean getSnapshot(DeviceBatteryMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.batteryLevelPct = getBatteryLevel(getBatteryIntent()); long now = SystemClock.elapsedRealtime(); synchronized (this) { if (mIsCurrentlyCharging) { snapshot.chargingRealtimeMs = mChargingRealtimeMs + (now - mLastUpdateMs); snapshot.batteryRealtimeMs = mBatteryRealtimeMs; } else { snapshot.chargingRealtimeMs = mChargingRealtimeMs; snapshot.batteryRealtimeMs = mBatteryRealtimeMs + (now - mLastUpdateMs); } return true; } }
DeviceBatteryMetricsCollector extends SystemMetricsCollector<DeviceBatteryMetrics> { @Override @ThreadSafe(enableChecks = false) public boolean getSnapshot(DeviceBatteryMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.batteryLevelPct = getBatteryLevel(getBatteryIntent()); long now = SystemClock.elapsedRealtime(); synchronized (this) { if (mIsCurrentlyCharging) { snapshot.chargingRealtimeMs = mChargingRealtimeMs + (now - mLastUpdateMs); snapshot.batteryRealtimeMs = mBatteryRealtimeMs; } else { snapshot.chargingRealtimeMs = mChargingRealtimeMs; snapshot.batteryRealtimeMs = mBatteryRealtimeMs + (now - mLastUpdateMs); } return true; } } }
DeviceBatteryMetricsCollector extends SystemMetricsCollector<DeviceBatteryMetrics> { @Override @ThreadSafe(enableChecks = false) public boolean getSnapshot(DeviceBatteryMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.batteryLevelPct = getBatteryLevel(getBatteryIntent()); long now = SystemClock.elapsedRealtime(); synchronized (this) { if (mIsCurrentlyCharging) { snapshot.chargingRealtimeMs = mChargingRealtimeMs + (now - mLastUpdateMs); snapshot.batteryRealtimeMs = mBatteryRealtimeMs; } else { snapshot.chargingRealtimeMs = mChargingRealtimeMs; snapshot.batteryRealtimeMs = mBatteryRealtimeMs + (now - mLastUpdateMs); } return true; } } DeviceBatteryMetricsCollector(Context context); }
DeviceBatteryMetricsCollector extends SystemMetricsCollector<DeviceBatteryMetrics> { @Override @ThreadSafe(enableChecks = false) public boolean getSnapshot(DeviceBatteryMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.batteryLevelPct = getBatteryLevel(getBatteryIntent()); long now = SystemClock.elapsedRealtime(); synchronized (this) { if (mIsCurrentlyCharging) { snapshot.chargingRealtimeMs = mChargingRealtimeMs + (now - mLastUpdateMs); snapshot.batteryRealtimeMs = mBatteryRealtimeMs; } else { snapshot.chargingRealtimeMs = mChargingRealtimeMs; snapshot.batteryRealtimeMs = mBatteryRealtimeMs + (now - mLastUpdateMs); } return true; } } DeviceBatteryMetricsCollector(Context context); @Override @ThreadSafe(enableChecks = false) boolean getSnapshot(DeviceBatteryMetrics snapshot); @Override DeviceBatteryMetrics createMetrics(); }
DeviceBatteryMetricsCollector extends SystemMetricsCollector<DeviceBatteryMetrics> { @Override @ThreadSafe(enableChecks = false) public boolean getSnapshot(DeviceBatteryMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.batteryLevelPct = getBatteryLevel(getBatteryIntent()); long now = SystemClock.elapsedRealtime(); synchronized (this) { if (mIsCurrentlyCharging) { snapshot.chargingRealtimeMs = mChargingRealtimeMs + (now - mLastUpdateMs); snapshot.batteryRealtimeMs = mBatteryRealtimeMs; } else { snapshot.chargingRealtimeMs = mChargingRealtimeMs; snapshot.batteryRealtimeMs = mBatteryRealtimeMs + (now - mLastUpdateMs); } return true; } } DeviceBatteryMetricsCollector(Context context); @Override @ThreadSafe(enableChecks = false) boolean getSnapshot(DeviceBatteryMetrics snapshot); @Override DeviceBatteryMetrics createMetrics();  }