Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d53aae77 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Cleaning up unused keyguard user switcher." into main

parents da87a74f 11c6e655
Loading
Loading
Loading
Loading
+0 −11
Original line number Diff line number Diff line
@@ -167,17 +167,6 @@ class BackActionInteractorTest : SysuiTestCase() {
        verify(statusBarKeyguardViewManager, never()).onBackPressed()
    }

    @Test
    fun testOnBackRequested_closeUserSwitcherIfOpen() {
        whenever(shadeBackActionInteractor.closeUserSwitcherIfOpen()).thenReturn(true)

        val result = backActionInteractor.onBackRequested()

        assertTrue(result)
        verify(statusBarKeyguardViewManager, never()).onBackPressed()
        verify(shadeBackActionInteractor, never()).animateCollapseQs(anyBoolean())
    }

    @Test
    fun testOnBackRequested_returnsFalse() {
        // make shouldBackBeHandled return false
+3 −43
Original line number Diff line number Diff line
@@ -45,7 +45,6 @@ import android.annotation.IdRes;
import android.content.ContentResolver;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;
@@ -57,7 +56,6 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewPropertyAnimator;
import android.view.ViewStub;
import android.view.ViewTreeObserver;
import android.view.accessibility.AccessibilityManager;

@@ -74,10 +72,8 @@ import com.android.keyguard.KeyguardSliceViewController;
import com.android.keyguard.KeyguardStatusView;
import com.android.keyguard.KeyguardStatusViewController;
import com.android.keyguard.KeyguardUpdateMonitor;
import com.android.keyguard.dagger.KeyguardQsUserSwitchComponent;
import com.android.keyguard.dagger.KeyguardStatusBarViewComponent;
import com.android.keyguard.dagger.KeyguardStatusViewComponent;
import com.android.keyguard.dagger.KeyguardUserSwitcherComponent;
import com.android.keyguard.logging.KeyguardLogger;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.biometrics.AuthController;
@@ -147,12 +143,13 @@ import com.android.systemui.statusbar.SysuiStatusBarStateController;
import com.android.systemui.statusbar.VibratorHelper;
import com.android.systemui.statusbar.notification.ConversationNotificationManager;
import com.android.systemui.statusbar.notification.DynamicPrivacyController;
import com.android.systemui.statusbar.notification.headsup.HeadsUpTouchHelper;
import com.android.systemui.statusbar.notification.NotificationWakeUpCoordinator;
import com.android.systemui.statusbar.notification.NotificationWakeUpCoordinatorLogger;
import com.android.systemui.statusbar.notification.data.repository.NotificationsKeyguardViewStateRepository;
import com.android.systemui.statusbar.notification.domain.interactor.ActiveNotificationsInteractor;
import com.android.systemui.statusbar.notification.domain.interactor.NotificationsKeyguardInteractor;
import com.android.systemui.statusbar.notification.headsup.HeadsUpManager;
import com.android.systemui.statusbar.notification.headsup.HeadsUpTouchHelper;
import com.android.systemui.statusbar.notification.row.NotificationGutsManager;
import com.android.systemui.statusbar.notification.stack.AmbientState;
import com.android.systemui.statusbar.notification.stack.NotificationListContainer;
@@ -172,17 +169,13 @@ import com.android.systemui.statusbar.phone.LightBarController;
import com.android.systemui.statusbar.phone.LockscreenGestureLogger;
import com.android.systemui.statusbar.phone.ScreenOffAnimationController;
import com.android.systemui.statusbar.phone.ScrimController;
import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager;
import com.android.systemui.statusbar.phone.ShadeTouchableRegionManager;
import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager;
import com.android.systemui.statusbar.phone.TapAgainViewController;
import com.android.systemui.statusbar.phone.UnlockedScreenOffAnimationController;
import com.android.systemui.statusbar.policy.CastController;
import com.android.systemui.statusbar.policy.ConfigurationController;
import com.android.systemui.statusbar.notification.headsup.HeadsUpManager;
import com.android.systemui.statusbar.policy.KeyguardQsUserSwitchController;
import com.android.systemui.statusbar.policy.KeyguardStateController;
import com.android.systemui.statusbar.policy.KeyguardUserSwitcherController;
import com.android.systemui.statusbar.policy.KeyguardUserSwitcherView;
import com.android.systemui.statusbar.policy.ResourcesSplitShadeStateController;
import com.android.systemui.statusbar.policy.SplitShadeStateController;
import com.android.systemui.statusbar.policy.data.repository.FakeUserSetupRepository;
@@ -227,8 +220,6 @@ public class NotificationPanelViewControllerBaseTest extends SysuiTestCase {
    @Mock protected HeadsUpManager mHeadsUpManager;
    @Mock protected NotificationGutsManager mGutsManager;
    @Mock protected KeyguardStatusBarView mKeyguardStatusBar;
    @Mock protected KeyguardUserSwitcherView mUserSwitcherView;
    @Mock protected ViewStub mUserSwitcherStubView;
    @Mock protected HeadsUpTouchHelper.Callback mHeadsUpCallback;
    @Mock protected KeyguardUpdateMonitor mUpdateMonitor;
    @Mock protected KeyguardBypassController mKeyguardBypassController;
@@ -254,12 +245,6 @@ public class NotificationPanelViewControllerBaseTest extends SysuiTestCase {
    @Mock protected ConversationNotificationManager mConversationNotificationManager;
    @Mock protected StatusBarKeyguardViewManager mStatusBarKeyguardViewManager;
    @Mock protected KeyguardStatusViewComponent.Factory mKeyguardStatusViewComponentFactory;
    @Mock protected KeyguardQsUserSwitchComponent.Factory mKeyguardQsUserSwitchComponentFactory;
    @Mock protected KeyguardQsUserSwitchComponent mKeyguardQsUserSwitchComponent;
    @Mock protected KeyguardQsUserSwitchController mKeyguardQsUserSwitchController;
    @Mock protected KeyguardUserSwitcherComponent.Factory mKeyguardUserSwitcherComponentFactory;
    @Mock protected KeyguardUserSwitcherComponent mKeyguardUserSwitcherComponent;
    @Mock protected KeyguardUserSwitcherController mKeyguardUserSwitcherController;
    @Mock protected KeyguardStatusViewComponent mKeyguardStatusViewComponent;
    @Mock protected KeyguardStatusBarViewComponent.Factory mKeyguardStatusBarViewComponentFactory;
    @Mock protected KeyguardStatusBarViewComponent mKeyguardStatusBarViewComponent;
@@ -477,9 +462,6 @@ public class NotificationPanelViewControllerBaseTest extends SysuiTestCase {
                .thenReturn(SPLIT_SHADE_FULL_TRANSITION_DISTANCE);
        when(mView.getContext()).thenReturn(getContext());
        when(mView.findViewById(R.id.keyguard_header)).thenReturn(mKeyguardStatusBar);
        when(mView.findViewById(R.id.keyguard_user_switcher_view)).thenReturn(mUserSwitcherView);
        when(mView.findViewById(R.id.keyguard_user_switcher_stub)).thenReturn(
                mUserSwitcherStubView);
        when(mView.findViewById(R.id.keyguard_clock_container)).thenReturn(mKeyguardClockSwitch);
        when(mView.findViewById(R.id.notification_stack_scroller))
                .thenReturn(mNotificationStackScrollLayout);
@@ -511,14 +493,6 @@ public class NotificationPanelViewControllerBaseTest extends SysuiTestCase {
        when(mFragmentService.getFragmentHostManager(mView)).thenReturn(mFragmentHostManager);
        FlingAnimationUtils.Builder flingAnimationUtilsBuilder = new FlingAnimationUtils.Builder(
                mDisplayMetrics);
        when(mKeyguardQsUserSwitchComponentFactory.build(any()))
                .thenReturn(mKeyguardQsUserSwitchComponent);
        when(mKeyguardQsUserSwitchComponent.getKeyguardQsUserSwitchController())
                .thenReturn(mKeyguardQsUserSwitchController);
        when(mKeyguardUserSwitcherComponentFactory.build(any()))
                .thenReturn(mKeyguardUserSwitcherComponent);
        when(mKeyguardUserSwitcherComponent.getKeyguardUserSwitcherController())
                .thenReturn(mKeyguardUserSwitcherController);
        when(mScreenOffAnimationController.shouldAnimateClockChange()).thenReturn(true);
        when(mQs.getView()).thenReturn(mView);
        when(mQSFragment.getView()).thenReturn(mView);
@@ -627,8 +601,6 @@ public class NotificationPanelViewControllerBaseTest extends SysuiTestCase {
                .thenReturn(mKeyguardStatusBarViewController);
        when(mLayoutInflater.inflate(eq(R.layout.keyguard_status_view), any(), anyBoolean()))
                .thenReturn(keyguardStatusView);
        when(mLayoutInflater.inflate(eq(R.layout.keyguard_user_switcher), any(), anyBoolean()))
                .thenReturn(mUserSwitcherView);
        when(mNotificationRemoteInputManager.isRemoteInputActive())
                .thenReturn(false);
        doAnswer(invocation -> {
@@ -690,8 +662,6 @@ public class NotificationPanelViewControllerBaseTest extends SysuiTestCase {
                mNotificationsQSContainerController,
                mNotificationStackScrollLayoutController,
                mKeyguardStatusViewComponentFactory,
                mKeyguardQsUserSwitchComponentFactory,
                mKeyguardUserSwitcherComponentFactory,
                mKeyguardStatusBarViewComponentFactory,
                mLockscreenShadeTransitionController,
                mAuthController,
@@ -883,16 +853,6 @@ public class NotificationPanelViewControllerBaseTest extends SysuiTestCase {
        mNotificationPanelViewController.updateResources();
    }

    protected void updateMultiUserSetting(boolean enabled) {
        when(mResources.getBoolean(R.bool.qs_show_user_switcher_for_single_user)).thenReturn(false);
        when(mUserManager.isUserSwitcherEnabled(false)).thenReturn(enabled);
        final ArgumentCaptor<ContentObserver> observerCaptor =
                ArgumentCaptor.forClass(ContentObserver.class);
        verify(mContentResolver)
                .registerContentObserver(any(), anyBoolean(), observerCaptor.capture());
        observerCaptor.getValue().onChange(/* selfChange */ false);
    }

    protected void updateSmallestScreenWidth(int smallestScreenWidthDp) {
        Configuration configuration = new Configuration();
        configuration.smallestScreenWidthDp = smallestScreenWidthDp;
+0 −1320

File changed.

Preview size limit exceeded, changes collapsed.

+0 −217
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

@file:OptIn(ExperimentalCoroutinesApi::class)

package com.android.systemui.shade

import android.platform.test.annotations.DisableFlags
import android.testing.TestableLooper
import android.view.HapticFeedbackConstants
import android.view.View
import android.view.ViewStub
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.internal.util.CollectionUtils
import com.android.keyguard.KeyguardClockSwitch.LARGE
import com.android.systemui.Flags
import com.android.systemui.res.R
import com.android.systemui.statusbar.StatusBarState.KEYGUARD
import com.android.systemui.statusbar.StatusBarState.SHADE
import com.android.systemui.statusbar.StatusBarState.SHADE_LOCKED
import com.android.systemui.util.mockito.eq
import com.android.systemui.util.mockito.whenever
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentCaptor
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.Captor
import org.mockito.Mockito.atLeastOnce
import org.mockito.Mockito.clearInvocations
import org.mockito.Mockito.never
import org.mockito.Mockito.times
import org.mockito.Mockito.verify

@RunWith(AndroidJUnit4::class)
@TestableLooper.RunWithLooper(setAsMainLooper = true)
@SmallTest
@DisableFlags(Flags.FLAG_MIGRATE_CLOCKS_TO_BLUEPRINT)
class NotificationPanelViewControllerWithCoroutinesTest :
    NotificationPanelViewControllerBaseTest() {

    @Captor private lateinit var viewCaptor: ArgumentCaptor<View>

    override fun getMainDispatcher() = Dispatchers.Main.immediate

    @Test
    fun testDisableUserSwitcherAfterEnabling_returnsViewStubToTheViewHierarchy() = runTest {
        launch(Dispatchers.Main.immediate) { givenViewAttached() }
        advanceUntilIdle()

        whenever(mResources.getBoolean(com.android.internal.R.bool.config_keyguardUserSwitcher))
            .thenReturn(true)
        updateMultiUserSetting(true)
        clearInvocations(mView)

        updateMultiUserSetting(false)

        verify(mView, atLeastOnce()).addView(viewCaptor.capture(), anyInt())
        val userSwitcherStub =
            CollectionUtils.find(viewCaptor.allValues) { view ->
                view.id == R.id.keyguard_user_switcher_stub
            }
        assertThat(userSwitcherStub).isNotNull()
        assertThat(userSwitcherStub).isInstanceOf(ViewStub::class.java)
    }

    @Test
    fun testChangeSmallestScreenWidthAndUserSwitchEnabled_inflatesUserSwitchView() = runTest {
        launch(Dispatchers.Main.immediate) { givenViewAttached() }
        advanceUntilIdle()

        whenever(mView.findViewById<View>(R.id.keyguard_user_switcher_view)).thenReturn(null)
        updateSmallestScreenWidth(300)
        whenever(mResources.getBoolean(com.android.internal.R.bool.config_keyguardUserSwitcher))
            .thenReturn(true)
        whenever(mResources.getBoolean(R.bool.qs_show_user_switcher_for_single_user))
            .thenReturn(false)
        whenever(mUserManager.isUserSwitcherEnabled(false)).thenReturn(true)

        updateSmallestScreenWidth(800)

        verify(mUserSwitcherStubView).inflate()
    }

    @Test
    fun testFinishInflate_userSwitcherDisabled_doNotInflateUserSwitchView_initClock() = runTest {
        launch(Dispatchers.Main.immediate) { givenViewAttached() }
        advanceUntilIdle()

        whenever(mResources.getBoolean(com.android.internal.R.bool.config_keyguardUserSwitcher))
            .thenReturn(true)
        whenever(mResources.getBoolean(R.bool.qs_show_user_switcher_for_single_user))
            .thenReturn(false)
        whenever(mUserManager.isUserSwitcherEnabled(false /* showEvenIfNotActionable */))
            .thenReturn(false)

        mNotificationPanelViewController.onFinishInflate()

        verify(mUserSwitcherStubView, never()).inflate()
        verify(mKeyguardStatusViewController, times(3)).displayClock(LARGE, /* animate */ true)

        coroutineContext.cancelChildren()
    }

    @Test
    fun testReInflateViews_userSwitcherDisabled_doNotInflateUserSwitchView() = runTest {
        launch(Dispatchers.Main.immediate) { givenViewAttached() }
        advanceUntilIdle()

        whenever(mResources.getBoolean(com.android.internal.R.bool.config_keyguardUserSwitcher))
            .thenReturn(true)
        whenever(mResources.getBoolean(R.bool.qs_show_user_switcher_for_single_user))
            .thenReturn(false)
        whenever(mUserManager.isUserSwitcherEnabled(false /* showEvenIfNotActionable */))
            .thenReturn(false)

        mNotificationPanelViewController.reInflateViews()

        verify(mUserSwitcherStubView, never()).inflate()

        coroutineContext.cancelChildren()
    }

    @Test
    fun testDoubleTapRequired_Keyguard() = runTest {
        launch(Dispatchers.Main.immediate) {
            val listener = getFalsingTapListener()
            mStatusBarStateController.setState(KEYGUARD)

            listener.onAdditionalTapRequired()

            verify(mKeyguardIndicationController).showTransientIndication(anyInt())
        }
        advanceUntilIdle()
    }

    @Test
    @DisableFlags(Flags.FLAG_MSDL_FEEDBACK)
    fun doubleTapRequired_onKeyguard_usesPerformHapticFeedback() = runTest {
        launch(Dispatchers.Main.immediate) {
            val listener = getFalsingTapListener()
            mStatusBarStateController.setState(KEYGUARD)

            listener.onAdditionalTapRequired()
            verify(mKeyguardIndicationController).showTransientIndication(anyInt())
            verify(mVibratorHelper)
                .performHapticFeedback(eq(mView), eq(HapticFeedbackConstants.REJECT))
        }
        advanceUntilIdle()
    }

    @Test
    fun testDoubleTapRequired_ShadeLocked() = runTest {
        launch(Dispatchers.Main.immediate) {
            val listener = getFalsingTapListener()
            mStatusBarStateController.setState(SHADE_LOCKED)

            listener.onAdditionalTapRequired()

            verify(mTapAgainViewController).show()
        }
        advanceUntilIdle()
    }

    @Test
    @DisableFlags(Flags.FLAG_MSDL_FEEDBACK)
    fun doubleTapRequired_shadeLocked_usesPerformHapticFeedback() = runTest {
        launch(Dispatchers.Main.immediate) {
            val listener = getFalsingTapListener()
            mStatusBarStateController.setState(SHADE_LOCKED)

            listener.onAdditionalTapRequired()
            verify(mVibratorHelper)
                .performHapticFeedback(eq(mView), eq(HapticFeedbackConstants.REJECT))

            verify(mTapAgainViewController).show()
        }
        advanceUntilIdle()
    }

    @Test
    fun testOnAttachRefreshStatusBarState() = runTest {
        launch(Dispatchers.Main.immediate) {
            mStatusBarStateController.setState(KEYGUARD)
            whenever(mKeyguardStateController.isKeyguardFadingAway()).thenReturn(false)
            mOnAttachStateChangeListeners.forEach { it.onViewAttachedToWindow(mView) }
            verify(mKeyguardStatusViewController)
                .setKeyguardStatusViewVisibility(
                    KEYGUARD /*statusBarState*/,
                    false /*keyguardFadingAway*/,
                    false /*goingToFullShade*/,
                    SHADE, /*oldStatusBarState*/
                )
        }
        advanceUntilIdle()
    }
}
+0 −201
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.systemui.statusbar.policy

import android.content.Context
import android.content.pm.UserInfo
import android.graphics.Bitmap
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.internal.util.UserIcons
import com.android.systemui.res.R
import com.android.systemui.SysuiTestCase
import com.android.systemui.qs.tiles.UserDetailItemView
import com.android.systemui.user.data.source.UserRecord
import com.android.systemui.util.mockito.whenever
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.anyBoolean
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.Mock
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations

@RunWith(AndroidJUnit4::class)
@SmallTest
class KeyguardUserSwitcherAdapterTest : SysuiTestCase() {
    @Mock
    private lateinit var userSwitcherController: UserSwitcherController
    @Mock
    private lateinit var parent: ViewGroup
    @Mock
    private lateinit var keyguardUserDetailItemView: KeyguardUserDetailItemView
    @Mock
    private lateinit var otherView: View
    @Mock
    private lateinit var inflatedUserDetailItemView: KeyguardUserDetailItemView
    @Mock
    private lateinit var layoutInflater: LayoutInflater
    @Mock
    private lateinit var keyguardUserSwitcherController: KeyguardUserSwitcherController

    private lateinit var adapter: KeyguardUserSwitcherController.KeyguardUserAdapter
    private lateinit var picture: Bitmap

    @Before
    fun setUp() {
        MockitoAnnotations.initMocks(this)

        whenever(userSwitcherController.isUserSwitcherEnabled).thenReturn(true)

        mContext.addMockSystemService(Context.LAYOUT_INFLATER_SERVICE, layoutInflater)
        `when`(layoutInflater.inflate(anyInt(), any(ViewGroup::class.java), anyBoolean()))
                .thenReturn(inflatedUserDetailItemView)
        adapter = KeyguardUserSwitcherController.KeyguardUserAdapter(
                mContext,
                mContext.resources,
                LayoutInflater.from(mContext),
                userSwitcherController, keyguardUserSwitcherController)
        picture = UserIcons.convertToBitmap(mContext.getDrawable(R.drawable.ic_avatar_user))
    }

    /**
     * Uses the KeyguardUserAdapter to create a UserDetailItemView where the convertView has an
     * incompatible type
     */
    private fun createViewFromDifferentType(
        isCurrentUser: Boolean,
        isGuestUser: Boolean
    ): UserDetailItemView? {
        val user = createUserRecord(isCurrentUser, isGuestUser)
        return adapter.createUserDetailItemView(otherView, parent, user)
    }

    /**
     * Uses the KeyguardUserAdapter to create a UserDetailItemView where the convertView is an
     * instance of KeyguardUserDetailItemView
     */
    private fun createViewFromSameType(
        isCurrentUser: Boolean,
        isGuestUser: Boolean
    ): UserDetailItemView? {
        val user = createUserRecord(isCurrentUser, isGuestUser)
        return adapter.createUserDetailItemView(keyguardUserDetailItemView, parent, user)
    }

    @Test
    fun shouldSetOnClickListener_notCurrentUser_notGuestUser_oldViewIsSameType() {
        val v: UserDetailItemView? = createViewFromSameType(
                isCurrentUser = false, isGuestUser = false)
        assertNotNull(v)
        verify(v)!!.setOnClickListener(adapter)
    }

    @Test
    fun shouldSetOnClickListener_notCurrentUser_guestUser_oldViewIsSameType() {
        val v: UserDetailItemView? = createViewFromSameType(
                isCurrentUser = false, isGuestUser = true)
        assertNotNull(v)
        verify(v)!!.setOnClickListener(adapter)
    }

    @Test
    fun shouldSetOnOnClickListener_currentUser_notGuestUser_oldViewIsSameType() {
        val v: UserDetailItemView? = createViewFromSameType(
                isCurrentUser = true, isGuestUser = false)
        assertNotNull(v)
        verify(v)!!.setOnClickListener(adapter)
    }

    @Test
    fun shouldSetOnClickListener_currentUser_guestUser_oldViewIsSameType() {
        val v: UserDetailItemView? = createViewFromSameType(
                isCurrentUser = true, isGuestUser = true)
        assertNotNull(v)
        verify(v)!!.setOnClickListener(adapter)
    }

    @Test
    fun shouldSetOnClickListener_notCurrentUser_notGuestUser_oldViewIsDifferentType() {
        val v: UserDetailItemView? = createViewFromDifferentType(
                isCurrentUser = false, isGuestUser = false)
        assertNotNull(v)
        verify(v)!!.setOnClickListener(adapter)
    }

    @Test
    fun shouldSetOnClickListener_notCurrentUser_guestUser_oldViewIsDifferentType() {
        val v: UserDetailItemView? = createViewFromDifferentType(
                isCurrentUser = false, isGuestUser = true)
        assertNotNull(v)
        verify(v)!!.setOnClickListener(adapter)
    }

    @Test
    fun shouldSetOnOnClickListener_currentUser_notGuestUser_oldViewIsDifferentType() {
        val v: UserDetailItemView? = createViewFromDifferentType(
                isCurrentUser = true, isGuestUser = false)
        assertNotNull(v)
        verify(v)!!.setOnClickListener(adapter)
    }

    @Test
    fun shouldSetOnClickListener_currentUser_guestUser_oldViewIsDifferentType() {
        val v: UserDetailItemView? = createViewFromDifferentType(
                isCurrentUser = true, isGuestUser = true)
        assertNotNull(v)
        verify(v)!!.setOnClickListener(adapter)
    }

    @Test
    fun testCurrentUserIsAlwaysFirst() {
        `when`(userSwitcherController.users).thenReturn(arrayListOf(
                createUserRecord(isCurrentUser = false, isGuestUser = false),
                createUserRecord(isCurrentUser = true, isGuestUser = false),
                createUserRecord(isCurrentUser = false, isGuestUser = true),
                createUserRecord(isCurrentUser = false, isGuestUser = false)
        ))

        adapter.notifyDataSetChanged()
        assertTrue("Expected current user to be first in list", adapter.getItem(0).isCurrent)
        assertFalse("Did not expect current user in position 1", adapter.getItem(1).isCurrent)
        assertFalse("Did not expect current user in position 2", adapter.getItem(2).isCurrent)
        assertTrue("Expected guest user to remain in position 2", adapter.getItem(2).isGuest)
        assertFalse("Did not expect current user in position 3", adapter.getItem(3).isCurrent)
    }

    private fun createUserRecord(isCurrentUser: Boolean, isGuestUser: Boolean) =
        UserRecord(
            UserInfo(0 /* id */, "name", 0 /* flags */),
            picture,
            isGuestUser,
            isCurrentUser,
            false /* isAddUser */,
            false /* isRestricted */,
            true /* isSwitchToEnabled */,
            false /* isAddSupervisedUser */
        )
}
Loading