1 /* 2 * Copyright (C) 2016 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 package com.android.settings.accounts; 17 18 import static com.google.common.truth.Truth.assertThat; 19 import static org.mockito.Matchers.any; 20 import static org.mockito.Matchers.anyInt; 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.when; 23 import static org.robolectric.Shadows.shadowOf; 24 25 import android.accounts.Account; 26 import android.app.Activity; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.content.pm.PackageManager; 30 import android.content.pm.ResolveInfo; 31 import android.os.Bundle; 32 import android.os.UserHandle; 33 import android.support.v7.preference.Preference; 34 35 import com.android.internal.logging.nano.MetricsProto; 36 import com.android.settings.dashboard.DashboardFeatureProviderImpl; 37 import com.android.settings.testutils.SettingsRobolectricTestRunner; 38 import com.android.settingslib.drawer.CategoryKey; 39 import com.android.settingslib.drawer.Tile; 40 41 import org.junit.Before; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.robolectric.Robolectric; 45 import org.robolectric.RuntimeEnvironment; 46 import org.robolectric.util.ReflectionHelpers; 47 48 @RunWith(SettingsRobolectricTestRunner.class) 49 public class AccountDetailDashboardFragmentTest { 50 51 private static final String METADATA_CATEGORY = "com.android.settings.category"; 52 private static final String METADATA_ACCOUNT_TYPE = "com.android.settings.ia.account"; 53 private static final String METADATA_USER_HANDLE = "user_handle"; 54 55 private AccountDetailDashboardFragment mFragment; 56 private Context mContext; 57 58 @Before 59 public void setUp() { 60 mContext = RuntimeEnvironment.application; 61 62 final Bundle args = new Bundle(); 63 args.putParcelable(METADATA_USER_HANDLE, UserHandle.CURRENT); 64 65 mFragment = new AccountDetailDashboardFragment(); 66 mFragment.setArguments(args); 67 mFragment.mAccountType = "com.abc"; 68 mFragment.mAccount = new Account("name1 (at) abc.com", "com.abc"); 69 } 70 71 @Test 72 public void testCategory_isAccountDetail() { 73 assertThat(new AccountDetailDashboardFragment().getCategoryKey()) 74 .isEqualTo(CategoryKey.CATEGORY_ACCOUNT_DETAIL); 75 } 76 77 @Test 78 public void refreshDashboardTiles_HasAccountType_shouldDisplay() { 79 final Tile tile = new Tile(); 80 final Bundle metaData = new Bundle(); 81 metaData.putString(METADATA_CATEGORY, CategoryKey.CATEGORY_ACCOUNT_DETAIL); 82 metaData.putString(METADATA_ACCOUNT_TYPE, "com.abc"); 83 tile.metaData = metaData; 84 85 assertThat(mFragment.displayTile(tile)).isTrue(); 86 } 87 88 @Test 89 public void refreshDashboardTiles_NoAccountType_shouldNotDisplay() { 90 final Tile tile = new Tile(); 91 final Bundle metaData = new Bundle(); 92 metaData.putString(METADATA_CATEGORY, CategoryKey.CATEGORY_ACCOUNT_DETAIL); 93 tile.metaData = metaData; 94 95 assertThat(mFragment.displayTile(tile)).isFalse(); 96 } 97 98 @Test 99 public void refreshDashboardTiles_OtherAccountType_shouldNotDisplay() { 100 final Tile tile = new Tile(); 101 final Bundle metaData = new Bundle(); 102 metaData.putString(METADATA_CATEGORY, CategoryKey.CATEGORY_ACCOUNT_DETAIL); 103 metaData.putString(METADATA_ACCOUNT_TYPE, "com.other"); 104 tile.metaData = metaData; 105 106 assertThat(mFragment.displayTile(tile)).isFalse(); 107 } 108 109 @Test 110 public void refreshDashboardTiles_HasAccountType_shouldAddAccountNameToIntent() { 111 final DashboardFeatureProviderImpl dashboardFeatureProvider = 112 new DashboardFeatureProviderImpl(mContext); 113 final PackageManager packageManager = mock(PackageManager.class); 114 ReflectionHelpers.setField(dashboardFeatureProvider, "mPackageManager", packageManager); 115 when(packageManager.resolveActivity(any(Intent.class), anyInt())) 116 .thenReturn(mock(ResolveInfo.class)); 117 118 final Tile tile = new Tile(); 119 tile.key = "key"; 120 tile.metaData = new Bundle(); 121 tile.metaData.putString(METADATA_CATEGORY, CategoryKey.CATEGORY_ACCOUNT); 122 tile.metaData.putString(METADATA_ACCOUNT_TYPE, "com.abc"); 123 tile.metaData.putString("com.android.settings.intent.action", Intent.ACTION_ASSIST); 124 tile.intent = new Intent(); 125 tile.userHandle = null; 126 mFragment.displayTile(tile); 127 128 final Activity activity = Robolectric.setupActivity(Activity.class); 129 final Preference preference = new Preference(mContext); 130 dashboardFeatureProvider.bindPreferenceToTile(activity, 131 MetricsProto.MetricsEvent.DASHBOARD_SUMMARY, preference, tile, "key", 132 Preference.DEFAULT_ORDER); 133 134 preference.performClick(); 135 136 final Intent intent = shadowOf(activity).getNextStartedActivityForResult().intent; 137 138 assertThat(intent.getStringExtra("extra.accountName")).isEqualTo("name1 (at) abc.com"); 139 } 140 } 141