Fabian Kozynski | 8f571e5 | 2019-09-04 11:18:46 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.qs.tiles |
| 18 | |
| 19 | import android.content.Context |
| 20 | import android.content.pm.UserInfo |
| 21 | import android.graphics.Bitmap |
| 22 | import android.testing.AndroidTestingRunner |
| 23 | import android.view.LayoutInflater |
| 24 | import android.view.View |
| 25 | import android.view.ViewGroup |
| 26 | import androidx.test.filters.SmallTest |
| 27 | import com.android.systemui.SysuiTestCase |
| 28 | import com.android.systemui.statusbar.policy.UserSwitcherController |
| 29 | import org.junit.Before |
| 30 | import org.junit.Test |
| 31 | import org.junit.runner.RunWith |
| 32 | import org.mockito.ArgumentMatchers.any |
| 33 | import org.mockito.ArgumentMatchers.anyBoolean |
| 34 | import org.mockito.ArgumentMatchers.anyInt |
| 35 | import org.mockito.Mock |
| 36 | import org.mockito.Mockito.`when` |
| 37 | import org.mockito.Mockito.verify |
| 38 | import org.mockito.MockitoAnnotations |
| 39 | |
| 40 | @RunWith(AndroidTestingRunner::class) |
| 41 | @SmallTest |
| 42 | class UserDetailViewAdapterTest : SysuiTestCase() { |
| 43 | |
| 44 | @Mock private lateinit var mUserSwitcherController: UserSwitcherController |
| 45 | @Mock private lateinit var mParent: ViewGroup |
| 46 | @Mock private lateinit var mUserDetailItemView: UserDetailItemView |
| 47 | @Mock private lateinit var mOtherView: View |
| 48 | @Mock private lateinit var mInflatedUserDetailItemView: UserDetailItemView |
| 49 | @Mock private lateinit var mUserInfo: UserInfo |
| 50 | @Mock private lateinit var mPicture: Bitmap |
| 51 | @Mock private lateinit var mLayoutInflater: LayoutInflater |
| 52 | private lateinit var adapter: UserDetailView.Adapter |
| 53 | |
| 54 | @Before |
| 55 | fun setUp() { |
| 56 | MockitoAnnotations.initMocks(this) |
| 57 | |
| 58 | mContext.addMockSystemService(Context.LAYOUT_INFLATER_SERVICE, mLayoutInflater) |
| 59 | `when`(mLayoutInflater.inflate(anyInt(), any(ViewGroup::class.java), anyBoolean())) |
| 60 | .thenReturn(mInflatedUserDetailItemView) |
| 61 | adapter = UserDetailView.Adapter(mContext, mUserSwitcherController) |
| 62 | } |
| 63 | |
| 64 | private fun clickableTest( |
| 65 | current: Boolean, |
| 66 | guest: Boolean, |
| 67 | convertView: View, |
| 68 | shouldBeClickable: Boolean |
| 69 | ) { |
| 70 | val user = createUserRecord(current, guest) |
| 71 | val v = adapter.createUserDetailItemView(convertView, mParent, user) |
| 72 | if (shouldBeClickable) { |
| 73 | verify(v).setOnClickListener(adapter) |
| 74 | } else { |
| 75 | verify(v).setOnClickListener(null) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @Test |
| 80 | fun testGuestIsClickable_differentViews_notCurrent() { |
| 81 | clickableTest(false, true, mOtherView, true) |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | fun testGuestIsClickable_differentViews_Current() { |
| 86 | clickableTest(true, true, mOtherView, true) |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | fun testGuestIsClickable_sameView_notCurrent() { |
| 91 | clickableTest(false, true, mUserDetailItemView, true) |
| 92 | } |
| 93 | |
| 94 | @Test |
| 95 | fun testGuestIsClickable_sameView_Current() { |
| 96 | clickableTest(true, true, mUserDetailItemView, true) |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | fun testNotGuestCurrentUserIsNotClickable_otherView() { |
| 101 | clickableTest(true, false, mOtherView, false) |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | fun testNotGuestCurrentUserIsNotClickable_sameView() { |
| 106 | clickableTest(true, false, mUserDetailItemView, false) |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | fun testNotGuestNotCurrentUserIsClickable_otherView() { |
| 111 | clickableTest(false, false, mOtherView, true) |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | fun testNotGuestNotCurrentUserIsClickable_sameView() { |
| 116 | clickableTest(false, false, mUserDetailItemView, true) |
| 117 | } |
| 118 | |
| 119 | private fun createUserRecord(current: Boolean, guest: Boolean) = |
| 120 | UserSwitcherController.UserRecord( |
| 121 | mUserInfo, |
| 122 | mPicture, |
| 123 | guest, |
| 124 | current, |
| 125 | false /* isAddUser */, |
| 126 | false /* isRestricted */, |
| 127 | true /* isSwitchToEnabled */) |
| 128 | } |