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