OSDN Git Service

Change all mocks that are Dependencys to be properly injected.
authorEliot Courtney <edcourtney@google.com>
Thu, 14 Dec 2017 09:54:28 +0000 (18:54 +0900)
committerEliot Courtney <edcourtney@google.com>
Tue, 26 Dec 2017 07:57:15 +0000 (16:57 +0900)
If a class being unit tested is changed to Dependency.get() a dependency
rather than receiving it as an argument, the tests could break in subtle
ways. Make sure all mocked Dependencys are properly injected as test
dependencies.

Bug: 63874929
Bug: 62602530
Test: runtest systemui
Change-Id: I2b23a2ea0ac9067952624a5c302ca419685307ae

packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationEntryManagerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationListenerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationLockscreenUserManagerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationLoggerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationRemoteInputManagerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationViewHierarchyManagerTest.java
packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarTest.java

index 1354717..d8de4e3 100644 (file)
@@ -73,22 +73,24 @@ public class NotificationEntryManagerTest extends SysuiTestCase {
 
     @Mock private NotificationPresenter mPresenter;
     @Mock private ExpandableNotificationRow mRow;
+    @Mock private NotificationListContainer mListContainer;
+    @Mock private NotificationEntryManager.Callback mCallback;
+    @Mock private HeadsUpManager mHeadsUpManager;
+    @Mock private NotificationListenerService.RankingMap mRankingMap;
+    @Mock private RemoteInputController mRemoteInputController;
+    @Mock private IStatusBarService mBarService;
+
+    // Dependency mocks:
+    @Mock private ForegroundServiceController mForegroundServiceController;
     @Mock private NotificationLockscreenUserManager mLockscreenUserManager;
     @Mock private NotificationGroupManager mGroupManager;
     @Mock private NotificationGutsManager mGutsManager;
     @Mock private NotificationRemoteInputManager mRemoteInputManager;
     @Mock private NotificationMediaManager mMediaManager;
-    @Mock private ForegroundServiceController mForegroundServiceController;
     @Mock private NotificationListener mNotificationListener;
-    @Mock private MetricsLogger mMetricsLogger;
     @Mock private DeviceProvisionedController mDeviceProvisionedController;
     @Mock private VisualStabilityManager mVisualStabilityManager;
-    @Mock private NotificationListContainer mListContainer;
-    @Mock private NotificationEntryManager.Callback mCallback;
-    @Mock private HeadsUpManager mHeadsUpManager;
-    @Mock private NotificationListenerService.RankingMap mRankingMap;
-    @Mock private RemoteInputController mRemoteInputController;
-    @Mock private IStatusBarService mBarService;
+    @Mock private MetricsLogger mMetricsLogger;
 
     private NotificationData.Entry mEntry;
     private StatusBarNotification mSbn;
@@ -135,6 +137,20 @@ public class NotificationEntryManagerTest extends SysuiTestCase {
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
+        mDependency.injectTestDependency(ForegroundServiceController.class,
+                mForegroundServiceController);
+        mDependency.injectTestDependency(NotificationLockscreenUserManager.class,
+                mLockscreenUserManager);
+        mDependency.injectTestDependency(NotificationGroupManager.class, mGroupManager);
+        mDependency.injectTestDependency(NotificationGutsManager.class, mGutsManager);
+        mDependency.injectTestDependency(NotificationRemoteInputManager.class, mRemoteInputManager);
+        mDependency.injectTestDependency(NotificationMediaManager.class, mMediaManager);
+        mDependency.injectTestDependency(NotificationListener.class, mNotificationListener);
+        mDependency.injectTestDependency(DeviceProvisionedController.class,
+                mDeviceProvisionedController);
+        mDependency.injectTestDependency(VisualStabilityManager.class, mVisualStabilityManager);
+        mDependency.injectTestDependency(MetricsLogger.class, mMetricsLogger);
+
         mHandler = new Handler(Looper.getMainLooper());
         mCountDownLatch = new CountDownLatch(1);
 
index b71f510..5a4c4b8 100644 (file)
@@ -19,7 +19,6 @@ package com.android.systemui.statusbar;
 import static junit.framework.Assert.assertTrue;
 
 import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -38,6 +37,8 @@ import com.android.systemui.SysuiTestCase;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
 
 import java.util.HashSet;
 import java.util.Set;
@@ -49,24 +50,27 @@ public class NotificationListenerTest extends SysuiTestCase {
     private static final String TEST_PACKAGE_NAME = "test";
     private static final int TEST_UID = 0;
 
-    private NotificationPresenter mPresenter;
-    private Handler mHandler;
+    @Mock private NotificationPresenter mPresenter;
+    @Mock private NotificationListenerService.RankingMap mRanking;
+    @Mock private NotificationData mNotificationData;
+
+    // Dependency mocks:
+    @Mock private NotificationEntryManager mEntryManager;
+    @Mock private NotificationRemoteInputManager mRemoteInputManager;
+
     private NotificationListener mListener;
+    private Handler mHandler;
     private StatusBarNotification mSbn;
-    private NotificationListenerService.RankingMap mRanking;
     private Set<String> mKeysKeptForRemoteInput;
-    private NotificationData mNotificationData;
-    private NotificationRemoteInputManager mRemoteInputManager;
-    private NotificationEntryManager mEntryManager;
 
     @Before
     public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
+        mDependency.injectTestDependency(NotificationRemoteInputManager.class,
+                mRemoteInputManager);
+
         mHandler = new Handler(Looper.getMainLooper());
-        mPresenter = mock(NotificationPresenter.class);
-        mNotificationData = mock(NotificationData.class);
-        mRanking = mock(NotificationListenerService.RankingMap.class);
-        mRemoteInputManager = mock(NotificationRemoteInputManager.class);
-        mEntryManager = mock(NotificationEntryManager.class);
         mKeysKeptForRemoteInput = new HashSet<>();
 
         when(mPresenter.getHandler()).thenReturn(mHandler);
index e4368d8..ad0e24c 100644 (file)
@@ -22,7 +22,6 @@ import static android.content.Intent.ACTION_USER_SWITCHED;
 import static junit.framework.Assert.assertFalse;
 import static junit.framework.Assert.assertTrue;
 
-import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -49,26 +48,30 @@ import com.google.android.collect.Lists;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
 
 @SmallTest
 @RunWith(AndroidTestingRunner.class)
 @TestableLooper.RunWithLooper
 public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
-    private NotificationPresenter mPresenter;
-    private NotificationEntryManager mEntryManager;
-    private TestNotificationLockscreenUserManager mLockscreenUserManager;
-    private DeviceProvisionedController mDeviceProvisionedController;
+    @Mock private NotificationPresenter mPresenter;
+    @Mock private UserManager mUserManager;
+
+    // Dependency mocks:
+    @Mock private NotificationEntryManager mEntryManager;
+    @Mock private DeviceProvisionedController mDeviceProvisionedController;
+
     private int mCurrentUserId;
     private Handler mHandler;
-    private UserManager mUserManager;
+    private TestNotificationLockscreenUserManager mLockscreenUserManager;
 
     @Before
     public void setUp() {
-        mUserManager = mock(UserManager.class);
-        mPresenter = mock(NotificationPresenter.class);
-        mEntryManager = mock(NotificationEntryManager.class);
-        mDependency.injectMockDependency(DeviceProvisionedController.class);
-        mDeviceProvisionedController = mDependency.get(DeviceProvisionedController.class);
+        MockitoAnnotations.initMocks(this);
+        mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
+        mDependency.injectTestDependency(DeviceProvisionedController.class,
+                mDeviceProvisionedController);
 
         mHandler = new Handler(Looper.getMainLooper());
         mContext.addMockSystemService(UserManager.class, mUserManager);
@@ -80,8 +83,6 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
         when(mPresenter.getEntryManager()).thenReturn(mEntryManager);
 
         mLockscreenUserManager = new TestNotificationLockscreenUserManager(mContext);
-        mDependency.injectTestDependency(NotificationLockscreenUserManager.class,
-                mLockscreenUserManager);
         mLockscreenUserManager.setUpWithPresenter(mPresenter);
     }
 
index 4908870..20a5a63 100644 (file)
@@ -54,13 +54,15 @@ public class NotificationLoggerTest extends SysuiTestCase {
     private static final int TEST_UID = 0;
 
     @Mock private NotificationPresenter mPresenter;
-    @Mock private NotificationEntryManager mEntryManager;
-    @Mock private NotificationListener mListener;
     @Mock private NotificationListContainer mListContainer;
     @Mock private IStatusBarService mBarService;
     @Mock private NotificationData mNotificationData;
     @Mock private ExpandableNotificationRow mRow;
 
+    // Dependency mocks:
+    @Mock private NotificationEntryManager mEntryManager;
+    @Mock private NotificationListener mListener;
+
     private NotificationData.Entry mEntry;
     private StatusBarNotification mSbn;
     private TestableNotificationLogger mLogger;
@@ -68,6 +70,8 @@ public class NotificationLoggerTest extends SysuiTestCase {
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
+        mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
+        mDependency.injectTestDependency(NotificationListener.class, mListener);
 
         when(mPresenter.getEntryManager()).thenReturn(mEntryManager);
         when(mEntryManager.getNotificationData()).thenReturn(mNotificationData);
index e943fd3..0163363 100644 (file)
@@ -34,23 +34,28 @@ public class NotificationRemoteInputManagerTest extends SysuiTestCase {
     private static final String TEST_PACKAGE_NAME = "test";
     private static final int TEST_UID = 0;
 
-    private Handler mHandler;
-    private TestableNotificationRemoteInputManager mRemoteInputManager;
-    private StatusBarNotification mSbn;
-    private NotificationData.Entry mEntry;
-
     @Mock private NotificationPresenter mPresenter;
-    @Mock private NotificationEntryManager mEntryManager;
     @Mock private RemoteInputController.Delegate mDelegate;
-    @Mock private NotificationLockscreenUserManager mLockscreenUserManager;
     @Mock private NotificationRemoteInputManager.Callback mCallback;
     @Mock private RemoteInputController mController;
     @Mock private NotificationListenerService.RankingMap mRanking;
     @Mock private ExpandableNotificationRow mRow;
 
+    // Dependency mocks:
+    @Mock private NotificationEntryManager mEntryManager;
+    @Mock private NotificationLockscreenUserManager mLockscreenUserManager;
+
+    private Handler mHandler;
+    private TestableNotificationRemoteInputManager mRemoteInputManager;
+    private StatusBarNotification mSbn;
+    private NotificationData.Entry mEntry;
+
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
+        mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
+        mDependency.injectTestDependency(NotificationLockscreenUserManager.class,
+                mLockscreenUserManager);
         mHandler = new Handler(Looper.getMainLooper());
 
         when(mPresenter.getHandler()).thenReturn(mHandler);
index 7ea3ec4..b9baf43 100644 (file)
@@ -50,20 +50,26 @@ import java.util.List;
 @TestableLooper.RunWithLooper
 public class NotificationViewHierarchyManagerTest extends SysuiTestCase {
     @Mock private NotificationPresenter mPresenter;
+    @Mock private NotificationData mNotificationData;
+    @Spy private FakeListContainer mListContainer = new FakeListContainer();
+
+    // Dependency mocks:
     @Mock private NotificationEntryManager mEntryManager;
     @Mock private NotificationLockscreenUserManager mLockscreenUserManager;
     @Mock private NotificationGroupManager mGroupManager;
     @Mock private VisualStabilityManager mVisualStabilityManager;
-    @Mock private NotificationData mNotificationData;
-    @Spy private FakeListContainer mListContainer = new FakeListContainer();
 
     private NotificationViewHierarchyManager mViewHierarchyManager;
-    private NotificationTestHelper mHelper;
+    private NotificationTestHelper mHelper = new NotificationTestHelper(mContext);;
 
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
-        mHelper = new NotificationTestHelper(mContext);
+        mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
+        mDependency.injectTestDependency(NotificationLockscreenUserManager.class,
+                mLockscreenUserManager);
+        mDependency.injectTestDependency(NotificationGroupManager.class, mGroupManager);
+        mDependency.injectTestDependency(VisualStabilityManager.class, mVisualStabilityManager);
 
         when(mPresenter.getEntryManager()).thenReturn(mEntryManager);
         when(mEntryManager.getNotificationData()).thenReturn(mNotificationData);
index c10de61..52b2308 100644 (file)
@@ -95,6 +95,8 @@ import com.android.systemui.statusbar.stack.NotificationStackScrollLayout;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
 
 import java.io.ByteArrayOutputStream;
 import java.io.PrintWriter;
@@ -104,32 +106,33 @@ import java.util.ArrayList;
 @RunWith(AndroidTestingRunner.class)
 @RunWithLooper
 public class StatusBarTest extends SysuiTestCase {
+    @Mock private StatusBarKeyguardViewManager mStatusBarKeyguardViewManager;
+    @Mock private UnlockMethodCache mUnlockMethodCache;
+    @Mock private KeyguardIndicationController mKeyguardIndicationController;
+    @Mock private NotificationStackScrollLayout mStackScroller;
+    @Mock private HeadsUpManager mHeadsUpManager;
+    @Mock private SystemServicesProxy mSystemServicesProxy;
+    @Mock private NotificationPanelView mNotificationPanelView;
+    @Mock private IStatusBarService mBarService;
+    @Mock private ScrimController mScrimController;
+    @Mock private ArrayList<Entry> mNotificationList;
+    @Mock private FingerprintUnlockController mFingerprintUnlockController;
+    @Mock private NotificationData mNotificationData;
+
+    // Mock dependencies:
+    @Mock private NotificationViewHierarchyManager mViewHierarchyManager;
+    @Mock private VisualStabilityManager mVisualStabilityManager;
+    @Mock private NotificationListener mNotificationListener;
 
-    private StatusBarKeyguardViewManager mStatusBarKeyguardViewManager;
-    private UnlockMethodCache mUnlockMethodCache;
-    private KeyguardIndicationController mKeyguardIndicationController;
-    private NotificationStackScrollLayout mStackScroller;
     private TestableStatusBar mStatusBar;
     private FakeMetricsLogger mMetricsLogger;
-    private HeadsUpManager mHeadsUpManager;
-    private NotificationData mNotificationData;
     private PowerManager mPowerManager;
-    private SystemServicesProxy mSystemServicesProxy;
-    private NotificationPanelView mNotificationPanelView;
-    private ScrimController mScrimController;
-    private IStatusBarService mBarService;
-    private NotificationListener mNotificationListener;
-    private NotificationLogger mNotificationLogger;
-    private ArrayList<Entry> mNotificationList;
-    private FingerprintUnlockController mFingerprintUnlockController;
-    private DisplayMetrics mDisplayMetrics = new DisplayMetrics();
     private TestableNotificationEntryManager mEntryManager;
-    private NotificationViewHierarchyManager mViewHierarchyManager;
-    private VisualStabilityManager mVisualStabilityManager;
+    private NotificationLogger mNotificationLogger;
 
     @Before
     public void setup() throws Exception {
-        mContext.setTheme(R.style.Theme_SystemUI_Light);
+        MockitoAnnotations.initMocks(this);
         mDependency.injectMockDependency(AssistManager.class);
         mDependency.injectMockDependency(DeviceProvisionedController.class);
         mDependency.injectMockDependency(NotificationGroupManager.class);
@@ -137,54 +140,38 @@ public class StatusBarTest extends SysuiTestCase {
         mDependency.injectMockDependency(NotificationRemoteInputManager.class);
         mDependency.injectMockDependency(NotificationMediaManager.class);
         mDependency.injectMockDependency(ForegroundServiceController.class);
-        mNotificationListener = mDependency.injectMockDependency(NotificationListener.class);
-        mViewHierarchyManager = mDependency.injectMockDependency(
-                NotificationViewHierarchyManager.class);
-        mVisualStabilityManager = mDependency.injectMockDependency(VisualStabilityManager.class);
+        mDependency.injectTestDependency(NotificationViewHierarchyManager.class,
+                mViewHierarchyManager);
+        mDependency.injectTestDependency(VisualStabilityManager.class, mVisualStabilityManager);
+        mDependency.injectTestDependency(NotificationListener.class, mNotificationListener);
         mDependency.injectTestDependency(KeyguardMonitor.class, mock(KeyguardMonitorImpl.class));
-        CommandQueue commandQueue = mock(CommandQueue.class);
-        when(commandQueue.asBinder()).thenReturn(new Binder());
-        mContext.putComponent(CommandQueue.class, commandQueue);
+
         mContext.addMockSystemService(TrustManager.class, mock(TrustManager.class));
         mContext.addMockSystemService(FingerprintManager.class, mock(FingerprintManager.class));
-        mStatusBarKeyguardViewManager = mock(StatusBarKeyguardViewManager.class);
-        mUnlockMethodCache = mock(UnlockMethodCache.class);
-        mKeyguardIndicationController = mock(KeyguardIndicationController.class);
-        mStackScroller = mock(NotificationStackScrollLayout.class);
-        when(mStackScroller.generateLayoutParams(any())).thenReturn(new LayoutParams(0, 0));
+
         mMetricsLogger = new FakeMetricsLogger();
-        mHeadsUpManager = mock(HeadsUpManager.class);
-        mNotificationData = mock(NotificationData.class);
-        mSystemServicesProxy = mock(SystemServicesProxy.class);
-        mNotificationPanelView = mock(NotificationPanelView.class);
-        when(mNotificationPanelView.getLayoutParams()).thenReturn(new LayoutParams(0, 0));
-        mNotificationList = mock(ArrayList.class);
-        mScrimController = mock(ScrimController.class);
-        mFingerprintUnlockController = mock(FingerprintUnlockController.class);
+        mDependency.injectTestDependency(MetricsLogger.class, mMetricsLogger);
+        mNotificationLogger = new NotificationLogger(mNotificationListener,
+                mDependency.get(UiOffloadThread.class));
+        mDependency.injectTestDependency(NotificationLogger.class, mNotificationLogger);
+
         IPowerManager powerManagerService = mock(IPowerManager.class);
         HandlerThread handlerThread = new HandlerThread("TestThread");
         handlerThread.start();
         mPowerManager = new PowerManager(mContext, powerManagerService,
                 new Handler(handlerThread.getLooper()));
-        when(powerManagerService.isInteractive()).thenReturn(true);
-        mBarService = mock(IStatusBarService.class);
-        mNotificationLogger = new NotificationLogger(mNotificationListener, mDependency.get(
-                UiOffloadThread.class));
 
-        mDependency.injectTestDependency(MetricsLogger.class, mMetricsLogger);
+        CommandQueue commandQueue = mock(CommandQueue.class);
+        when(commandQueue.asBinder()).thenReturn(new Binder());
+        mContext.putComponent(CommandQueue.class, commandQueue);
 
-        mNotificationListener = mDependency.get(NotificationListener.class);
-        mNotificationLogger =  mDependency.get(NotificationLogger.class);
-        mEntryManager = new TestableNotificationEntryManager(mMetricsLogger,
-                mSystemServicesProxy, mPowerManager, mContext);
+        mContext.setTheme(R.style.Theme_SystemUI_Light);
+
+        when(mStackScroller.generateLayoutParams(any())).thenReturn(new LayoutParams(0, 0));
+        when(mNotificationPanelView.getLayoutParams()).thenReturn(new LayoutParams(0, 0));
+        when(powerManagerService.isInteractive()).thenReturn(true);
+        when(mStackScroller.getActivatedChild()).thenReturn(null);
 
-        mStatusBar = new TestableStatusBar(mStatusBarKeyguardViewManager, mUnlockMethodCache,
-                mKeyguardIndicationController, mStackScroller, mHeadsUpManager,
-                mPowerManager, mNotificationPanelView, mBarService, mNotificationListener,
-                mNotificationLogger, mVisualStabilityManager, mViewHierarchyManager,
-                mEntryManager, mScrimController, mFingerprintUnlockController);
-        mStatusBar.mContext = mContext;
-        mStatusBar.mComponents = mContext.getComponents();
         doAnswer(invocation -> {
             OnDismissAction onDismissAction = (OnDismissAction) invocation.getArguments()[0];
             onDismissAction.onDismiss();
@@ -197,11 +184,19 @@ public class StatusBarTest extends SysuiTestCase {
             return null;
         }).when(mStatusBarKeyguardViewManager).addAfterKeyguardGoneRunnable(any());
 
+        mEntryManager = new TestableNotificationEntryManager(mMetricsLogger,
+                mSystemServicesProxy, mPowerManager, mContext);
+        mStatusBar = new TestableStatusBar(mStatusBarKeyguardViewManager, mUnlockMethodCache,
+                mKeyguardIndicationController, mStackScroller, mHeadsUpManager,
+                mPowerManager, mNotificationPanelView, mBarService, mNotificationListener,
+                mNotificationLogger, mVisualStabilityManager, mViewHierarchyManager,
+                mEntryManager, mScrimController, mFingerprintUnlockController);
+        mStatusBar.mContext = mContext;
+        mStatusBar.mComponents = mContext.getComponents();
         mEntryManager.setUpForTest(mStatusBar, mStackScroller, mStatusBar, mHeadsUpManager,
                 mNotificationData);
         mNotificationLogger.setUpWithPresenter(mStatusBar, mStackScroller);
 
-        when(mStackScroller.getActivatedChild()).thenReturn(null);
         TestableLooper.get(this).setMessageHandler(m -> {
             if (m.getCallback() == mStatusBar.mNotificationLogger.getVisibilityReporter()) {
                 return false;