more testable MetricsLogger interface

Begin migration to non-static methods to allow easier mocking.
New clients should use the non-static methods.
Old clients that want to unit test metrics
should move to the new interface.

Bug: 35138327
Test: runtest --path frameworks/base/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarTest.java
Change-Id: I168f3787ee50ddde8aa2d42c05b2b816d1d3a30e
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarTest.java
index 9f56da7..09f6b55 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarTest.java
@@ -23,14 +23,15 @@
 import static org.mockito.Mockito.when;
 
 import android.metrics.LogMaker;
-import android.metrics.MetricsReader;
 import android.support.test.filters.FlakyTest;
 import android.support.test.filters.SmallTest;
 import android.support.test.metricshelper.MetricsAsserts;
 import android.support.test.runner.AndroidJUnit4;
 import android.util.DisplayMetrics;
 
+import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.internal.logging.testing.FakeMetricsLogger;
 import com.android.keyguard.KeyguardHostView.OnDismissAction;
 import com.android.systemui.SysuiTestCase;
 import com.android.systemui.statusbar.ActivatableNotificationView;
@@ -43,9 +44,6 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
-// TODO(gpitsch): We have seen some flakes in these tests, needs some investigation.
-// Q: How is mMetricsReader being used by the tested code?
-// A: StatusBar uses MetricsLogger to write to the event log, then read back by MetricsReader
 @SmallTest
 @RunWith(AndroidJUnit4.class)
 public class StatusBarTest extends SysuiTestCase {
@@ -55,8 +53,8 @@
     KeyguardIndicationController mKeyguardIndicationController;
     NotificationStackScrollLayout mStackScroller;
     StatusBar mStatusBar;
+    FakeMetricsLogger mMetricsLogger;
 
-    private MetricsReader mMetricsReader;
     private DisplayMetrics mDisplayMetrics = new DisplayMetrics();
 
     @Before
@@ -65,8 +63,9 @@
         mUnlockMethodCache = mock(UnlockMethodCache.class);
         mKeyguardIndicationController = mock(KeyguardIndicationController.class);
         mStackScroller = mock(NotificationStackScrollLayout.class);
+        mMetricsLogger = new FakeMetricsLogger();
         mStatusBar = new TestableStatusBar(mStatusBarKeyguardViewManager, mUnlockMethodCache,
-                mKeyguardIndicationController, mStackScroller);
+                mKeyguardIndicationController, mStackScroller, mMetricsLogger);
 
         doAnswer(invocation -> {
             OnDismissAction onDismissAction = (OnDismissAction) invocation.getArguments()[0];
@@ -81,15 +80,6 @@
         }).when(mStatusBarKeyguardViewManager).addAfterKeyguardGoneRunnable(any());
 
         when(mStackScroller.getActivatedChild()).thenReturn(null);
-
-        mMetricsReader = new MetricsReader();
-        mMetricsReader.checkpoint(); // clear out old logs
-        try {
-            // pause so that no new events arrive in the rest of this millisecond.
-            Thread.sleep(2);
-        } catch (InterruptedException e) {
-            // pass
-        }
     }
 
     @Test
@@ -127,10 +117,10 @@
         when(mStatusBarKeyguardViewManager.isShowing()).thenReturn(false);
         when(mStatusBarKeyguardViewManager.isBouncerShowing()).thenReturn(false);
         when(mUnlockMethodCache.isMethodSecure()).thenReturn(false);
-
         mStatusBar.onKeyguardViewManagerStatesUpdated();
 
-        MetricsAsserts.assertHasLog("missing hidden insecure lockscreen log", mMetricsReader,
+        MetricsAsserts.assertHasLog("missing hidden insecure lockscreen log",
+                mMetricsLogger.getLogs(),
                 new LogMaker(MetricsEvent.LOCKSCREEN)
                         .setType(MetricsEvent.TYPE_CLOSE)
                         .setSubtype(0));
@@ -150,7 +140,8 @@
 
         mStatusBar.onKeyguardViewManagerStatesUpdated();
 
-        MetricsAsserts.assertHasLog("missing hidden secure lockscreen log", mMetricsReader,
+        MetricsAsserts.assertHasLog("missing hidden secure lockscreen log",
+                mMetricsLogger.getLogs(),
                 new LogMaker(MetricsEvent.LOCKSCREEN)
                         .setType(MetricsEvent.TYPE_CLOSE)
                         .setSubtype(1));
@@ -170,7 +161,8 @@
 
         mStatusBar.onKeyguardViewManagerStatesUpdated();
 
-        MetricsAsserts.assertHasLog("missing insecure lockscreen showing", mMetricsReader,
+        MetricsAsserts.assertHasLog("missing insecure lockscreen showing",
+                mMetricsLogger.getLogs(),
                 new LogMaker(MetricsEvent.LOCKSCREEN)
                         .setType(MetricsEvent.TYPE_OPEN)
                         .setSubtype(0));
@@ -190,7 +182,8 @@
 
         mStatusBar.onKeyguardViewManagerStatesUpdated();
 
-        MetricsAsserts.assertHasLog("missing secure lockscreen showing log", mMetricsReader,
+        MetricsAsserts.assertHasLog("missing secure lockscreen showing log",
+                mMetricsLogger.getLogs(),
                 new LogMaker(MetricsEvent.LOCKSCREEN)
                         .setType(MetricsEvent.TYPE_OPEN)
                         .setSubtype(1));
@@ -210,7 +203,8 @@
 
         mStatusBar.onKeyguardViewManagerStatesUpdated();
 
-        MetricsAsserts.assertHasLog("missing bouncer log", mMetricsReader,
+        MetricsAsserts.assertHasLog("missing bouncer log",
+                mMetricsLogger.getLogs(),
                 new LogMaker(MetricsEvent.BOUNCER)
                         .setType(MetricsEvent.TYPE_OPEN)
                         .setSubtype(1));
@@ -223,7 +217,8 @@
         ActivatableNotificationView view =  mock(ActivatableNotificationView.class);
         mStatusBar.onActivated(view);
 
-        MetricsAsserts.assertHasLog("missing lockscreen note tap log", mMetricsReader,
+        MetricsAsserts.assertHasLog("missing lockscreen note tap log",
+                mMetricsLogger.getLogs(),
                 new LogMaker(MetricsEvent.ACTION_LS_NOTE)
                         .setType(MetricsEvent.TYPE_ACTION));
     }
@@ -231,11 +226,12 @@
     static class TestableStatusBar extends StatusBar {
         public TestableStatusBar(StatusBarKeyguardViewManager man,
                 UnlockMethodCache unlock, KeyguardIndicationController key,
-                NotificationStackScrollLayout stack) {
+                NotificationStackScrollLayout stack, MetricsLogger logger) {
             mStatusBarKeyguardViewManager = man;
             mUnlockMethodCache = unlock;
             mKeyguardIndicationController = key;
             mStackScroller = stack;
+            mMetricsLogger = logger;
         }
 
         @Override