@Test public void testGetSnapshot() { ShadowSystemClock.setElapsedRealtime(1); mAppWakeupMetricsCollector.recordWakeupStart(AppWakeupMetrics.WakeupReason.ALARM, "key1"); ShadowSystemClock.setElapsedRealtime(2); mAppWakeupMetricsCollector.recordWakeupStart( AppWakeupMetrics.WakeupReason.JOB_SCHEDULER, "key2"); ShadowSystemClock.setElapsedRealtime(3); mAppWakeupMetricsCollector.recordWakeupStart( AppWakeupMetrics.WakeupReason.JOB_SCHEDULER, "key3"); ShadowSystemClock.setElapsedRealtime(4); mAppWakeupMetricsCollector.recordWakeupStart(AppWakeupMetrics.WakeupReason.ALARM, "key4"); mAppWakeupMetricsCollector.getSnapshot(mAppWakeupMetrics); assertThat(mAppWakeupMetrics.appWakeups.size()).isEqualTo(0); ShadowSystemClock.setElapsedRealtime(20); mAppWakeupMetricsCollector.recordWakeupEnd("key1"); ShadowSystemClock.setElapsedRealtime(30); mAppWakeupMetricsCollector.recordWakeupEnd("key3"); ShadowSystemClock.setElapsedRealtime(35); mAppWakeupMetricsCollector.getSnapshot(mAppWakeupMetrics); assertThat(mAppWakeupMetrics.appWakeups.size()).isEqualTo(2); assertThat(mAppWakeupMetrics.appWakeups.get("key1")) .isEqualTo(new WakeupDetails(WakeupReason.ALARM, 1, 19)); assertThat(mAppWakeupMetrics.appWakeups.get("key3")) .isEqualTo(new WakeupDetails(WakeupReason.JOB_SCHEDULER, 1, 27)); ShadowSystemClock.setElapsedRealtime(41); mAppWakeupMetricsCollector.recordWakeupStart(AppWakeupMetrics.WakeupReason.ALARM, "key1"); ShadowSystemClock.setElapsedRealtime(50); mAppWakeupMetricsCollector.recordWakeupEnd("key1"); ShadowSystemClock.setElapsedRealtime(57); mAppWakeupMetricsCollector.recordWakeupEnd("key2"); mAppWakeupMetricsCollector.getSnapshot(mAppWakeupMetrics); assertThat(mAppWakeupMetrics.appWakeups.size()).isEqualTo(3); assertThat(mAppWakeupMetrics.appWakeups.get("key1")) .isEqualTo(new WakeupDetails(WakeupReason.ALARM, 2, 28)); assertThat(mAppWakeupMetrics.appWakeups.get("key2")) .isEqualTo(new WakeupDetails(WakeupReason.JOB_SCHEDULER, 1, 55)); assertThat(mAppWakeupMetrics.appWakeups.get("key3")) .isEqualTo(new WakeupDetails(WakeupReason.JOB_SCHEDULER, 1, 27)); ShadowSystemClock.setElapsedRealtime(65); mAppWakeupMetricsCollector.recordWakeupStart(AppWakeupMetrics.WakeupReason.ALARM, "key4"); ShadowSystemClock.setElapsedRealtime(65); mAppWakeupMetricsCollector.recordWakeupEnd("key1"); mAppWakeupMetricsCollector.getSnapshot(mAppWakeupMetrics); assertThat(mAppWakeupMetrics.appWakeups.size()).isEqualTo(3); assertThat(mAppWakeupMetrics.appWakeups.get("key1")) .isEqualTo(new WakeupDetails(WakeupReason.ALARM, 2, 28)); assertThat(mAppWakeupMetrics.appWakeups.get("key2")) .isEqualTo(new WakeupDetails(WakeupReason.JOB_SCHEDULER, 1, 55)); assertThat(mAppWakeupMetrics.appWakeups.get("key3")) .isEqualTo(new WakeupDetails(WakeupReason.JOB_SCHEDULER, 1, 27)); }
@Override @ThreadSafe(enableChecks = false) public synchronized boolean getSnapshot(AppWakeupMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.appWakeups.clear(); for (int i = 0; i < mMetrics.appWakeups.size(); i++) { WakeupDetails details = new WakeupDetails(); details.set(mMetrics.appWakeups.valueAt(i)); snapshot.appWakeups.put(mMetrics.appWakeups.keyAt(i), details); } return true; }
AppWakeupMetricsCollector extends SystemMetricsCollector<AppWakeupMetrics> { @Override @ThreadSafe(enableChecks = false) public synchronized boolean getSnapshot(AppWakeupMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.appWakeups.clear(); for (int i = 0; i < mMetrics.appWakeups.size(); i++) { WakeupDetails details = new WakeupDetails(); details.set(mMetrics.appWakeups.valueAt(i)); snapshot.appWakeups.put(mMetrics.appWakeups.keyAt(i), details); } return true; } }
AppWakeupMetricsCollector extends SystemMetricsCollector<AppWakeupMetrics> { @Override @ThreadSafe(enableChecks = false) public synchronized boolean getSnapshot(AppWakeupMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.appWakeups.clear(); for (int i = 0; i < mMetrics.appWakeups.size(); i++) { WakeupDetails details = new WakeupDetails(); details.set(mMetrics.appWakeups.valueAt(i)); snapshot.appWakeups.put(mMetrics.appWakeups.keyAt(i), details); } return true; } AppWakeupMetricsCollector(); }
AppWakeupMetricsCollector extends SystemMetricsCollector<AppWakeupMetrics> { @Override @ThreadSafe(enableChecks = false) public synchronized boolean getSnapshot(AppWakeupMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.appWakeups.clear(); for (int i = 0; i < mMetrics.appWakeups.size(); i++) { WakeupDetails details = new WakeupDetails(); details.set(mMetrics.appWakeups.valueAt(i)); snapshot.appWakeups.put(mMetrics.appWakeups.keyAt(i), details); } return true; } AppWakeupMetricsCollector(); @Override @ThreadSafe(enableChecks = false) synchronized boolean getSnapshot(AppWakeupMetrics snapshot); @Override AppWakeupMetrics createMetrics(); synchronized void recordWakeupStart(AppWakeupMetrics.WakeupReason reason, String id); synchronized void recordWakeupEnd(String id); }
AppWakeupMetricsCollector extends SystemMetricsCollector<AppWakeupMetrics> { @Override @ThreadSafe(enableChecks = false) public synchronized boolean getSnapshot(AppWakeupMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); snapshot.appWakeups.clear(); for (int i = 0; i < mMetrics.appWakeups.size(); i++) { WakeupDetails details = new WakeupDetails(); details.set(mMetrics.appWakeups.valueAt(i)); snapshot.appWakeups.put(mMetrics.appWakeups.keyAt(i), details); } return true; } AppWakeupMetricsCollector(); @Override @ThreadSafe(enableChecks = false) synchronized boolean getSnapshot(AppWakeupMetrics snapshot); @Override AppWakeupMetrics createMetrics(); synchronized void recordWakeupStart(AppWakeupMetrics.WakeupReason reason, String id); synchronized void recordWakeupEnd(String id);  }