1 /* 2 * Copyright (C) 2017 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.settings.accounts; 18 19 import static androidx.lifecycle.Lifecycle.Event.ON_RESUME; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.ArgumentMatchers.anyString; 24 import static org.mockito.Mockito.doReturn; 25 import static org.mockito.Mockito.when; 26 27 import android.accounts.Account; 28 import android.app.Activity; 29 import android.os.Bundle; 30 import android.os.UserHandle; 31 import android.widget.TextView; 32 33 import androidx.lifecycle.LifecycleOwner; 34 import androidx.preference.PreferenceFragmentCompat; 35 import androidx.preference.PreferenceScreen; 36 37 import com.android.settings.R; 38 import com.android.settings.testutils.FakeFeatureFactory; 39 import com.android.settings.testutils.shadow.ShadowAuthenticationHelper; 40 import com.android.settingslib.core.lifecycle.Lifecycle; 41 import com.android.settingslib.widget.LayoutPreference; 42 43 import org.junit.Before; 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 import org.mockito.Mock; 47 import org.mockito.MockitoAnnotations; 48 import org.robolectric.RobolectricTestRunner; 49 import org.robolectric.RuntimeEnvironment; 50 import org.robolectric.annotation.Config; 51 52 @RunWith(RobolectricTestRunner.class) 53 @Config(shadows = ShadowAuthenticationHelper.class) 54 public class AccountHeaderPreferenceControllerTest { 55 56 @Mock 57 private Activity mActivity; 58 @Mock 59 private PreferenceFragmentCompat mFragment; 60 @Mock 61 private PreferenceScreen mScreen; 62 63 private LayoutPreference mHeaderPreference; 64 65 private AccountHeaderPreferenceController mController; 66 67 private LifecycleOwner mLifecycleOwner; 68 private Lifecycle mLifecycle; 69 70 @Before setUp()71 public void setUp() { 72 MockitoAnnotations.initMocks(this); 73 FakeFeatureFactory.setupForTest(); 74 mHeaderPreference = new LayoutPreference( 75 RuntimeEnvironment.application, R.layout.settings_entity_header); 76 doReturn(RuntimeEnvironment.application).when(mActivity).getApplicationContext(); 77 mLifecycleOwner = () -> mLifecycle; 78 mLifecycle = new Lifecycle(mLifecycleOwner); 79 } 80 81 @Test isAvailable_noArgs_shouldReturnNull()82 public void isAvailable_noArgs_shouldReturnNull() { 83 mController = new AccountHeaderPreferenceController(RuntimeEnvironment.application, 84 mLifecycle, mActivity, mFragment, null /* args */); 85 86 assertThat(mController.isAvailable()).isFalse(); 87 } 88 89 @Test onResume_shouldDisplayAccountInEntityHeader()90 public void onResume_shouldDisplayAccountInEntityHeader() { 91 final Account account = new Account("name1@abc.com", "com.abc"); 92 Bundle args = new Bundle(); 93 args.putParcelable(AccountDetailDashboardFragment.KEY_ACCOUNT, account); 94 args.putParcelable(AccountDetailDashboardFragment.KEY_USER_HANDLE, UserHandle.CURRENT); 95 mController = new AccountHeaderPreferenceController(RuntimeEnvironment.application, 96 mLifecycle, mActivity, mFragment, args); 97 98 assertThat(mController.isAvailable()).isTrue(); 99 100 when(mScreen.findPreference(anyString())).thenReturn(mHeaderPreference); 101 102 mController.displayPreference(mScreen); 103 mLifecycle.handleLifecycleEvent(ON_RESUME); 104 105 final CharSequence label = 106 ((TextView) mHeaderPreference.findViewById(R.id.entity_header_title)).getText(); 107 108 assertThat(label).isEqualTo(account.name); 109 } 110 } 111