Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.settingslib.drawer; |
| 18 | |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
Tony Mantler | 8d16737 | 2017-07-14 15:37:17 -0700 | [diff] [blame] | 21 | import static org.mockito.ArgumentMatchers.any; |
| 22 | import static org.mockito.ArgumentMatchers.anyInt; |
| 23 | import static org.mockito.ArgumentMatchers.anyString; |
| 24 | import static org.mockito.ArgumentMatchers.argThat; |
| 25 | import static org.mockito.ArgumentMatchers.eq; |
| 26 | import static org.mockito.ArgumentMatchers.isNull; |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 27 | import static org.mockito.Mockito.atLeastOnce; |
| 28 | import static org.mockito.Mockito.spy; |
| 29 | import static org.mockito.Mockito.verify; |
| 30 | import static org.mockito.Mockito.when; |
| 31 | import static org.robolectric.RuntimeEnvironment.application; |
| 32 | |
Maurice Lam | f74b9e5 | 2017-03-23 14:58:47 -0700 | [diff] [blame] | 33 | import android.app.ActivityManager; |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 34 | import android.content.ContentResolver; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 35 | import android.content.Context; |
Maurice Lam | f74b9e5 | 2017-03-23 14:58:47 -0700 | [diff] [blame] | 36 | import android.content.IContentProvider; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 37 | import android.content.Intent; |
| 38 | import android.content.pm.ActivityInfo; |
| 39 | import android.content.pm.ApplicationInfo; |
| 40 | import android.content.pm.PackageManager; |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 41 | import android.content.pm.PackageManager.NameNotFoundException; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 42 | import android.content.pm.ResolveInfo; |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 43 | import android.content.res.Resources; |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 44 | import android.net.Uri; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 45 | import android.os.Bundle; |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 46 | import android.os.RemoteException; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 47 | import android.os.UserHandle; |
Doris Ling | 485df11 | 2016-12-19 10:45:47 -0800 | [diff] [blame] | 48 | import android.os.UserManager; |
| 49 | import android.provider.Settings.Global; |
Ido Ofir | a560cd9 | 2017-01-24 13:27:36 -0800 | [diff] [blame] | 50 | import android.text.TextUtils; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 51 | import android.util.ArrayMap; |
| 52 | import android.util.Pair; |
Jaewoong Jung | 78c5e5d | 2017-06-15 18:02:44 -0700 | [diff] [blame] | 53 | import android.widget.RemoteViews; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 54 | |
Maurice Lam | d8ae77b | 2017-03-23 21:26:01 -0700 | [diff] [blame] | 55 | import com.android.settingslib.R; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 56 | import com.android.settingslib.TestConfig; |
Fan Zhang | e138ef1 | 2017-05-11 15:29:56 -0700 | [diff] [blame] | 57 | import com.android.settingslib.suggestions.SuggestionParser; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 58 | |
| 59 | import org.junit.Before; |
| 60 | import org.junit.Test; |
| 61 | import org.junit.runner.RunWith; |
Maurice Lam | f74b9e5 | 2017-03-23 14:58:47 -0700 | [diff] [blame] | 62 | import org.mockito.ArgumentCaptor; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 63 | import org.mockito.Mock; |
| 64 | import org.mockito.MockitoAnnotations; |
| 65 | import org.robolectric.RobolectricTestRunner; |
| 66 | import org.robolectric.annotation.Config; |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 67 | import org.robolectric.annotation.Implementation; |
| 68 | import org.robolectric.annotation.Implements; |
Jaewoong Jung | 78c5e5d | 2017-06-15 18:02:44 -0700 | [diff] [blame] | 69 | import org.robolectric.internal.ShadowExtractor; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 70 | |
| 71 | import java.util.ArrayList; |
Maurice Lam | f74b9e5 | 2017-03-23 14:58:47 -0700 | [diff] [blame] | 72 | import java.util.Collections; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 73 | import java.util.List; |
| 74 | import java.util.Map; |
| 75 | |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 76 | @RunWith(RobolectricTestRunner.class) |
Jaewoong Jung | 78c5e5d | 2017-06-15 18:02:44 -0700 | [diff] [blame] | 77 | @Config(manifest = TestConfig.MANIFEST_PATH, |
| 78 | sdk = TestConfig.SDK_VERSION, |
| 79 | shadows = {TileUtilsTest.TileUtilsShadowRemoteViews.class}) |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 80 | public class TileUtilsTest { |
| 81 | |
| 82 | @Mock |
| 83 | private Context mContext; |
| 84 | @Mock |
| 85 | private PackageManager mPackageManager; |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 86 | @Mock |
| 87 | private Resources mResources; |
Doris Ling | 485df11 | 2016-12-19 10:45:47 -0800 | [diff] [blame] | 88 | @Mock |
| 89 | private UserManager mUserManager; |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 90 | @Mock |
| 91 | private IContentProvider mIContentProvider; |
| 92 | @Mock |
| 93 | private ContentResolver mContentResolver; |
| 94 | |
| 95 | private static final String URI_GET_SUMMARY = "content://authority/text/summary"; |
| 96 | private static final String URI_GET_ICON = "content://authority/icon/my_icon"; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 97 | |
| 98 | @Before |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 99 | public void setUp() throws NameNotFoundException { |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 100 | MockitoAnnotations.initMocks(this); |
| 101 | when(mContext.getPackageManager()).thenReturn(mPackageManager); |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 102 | when(mPackageManager.getResourcesForApplication(anyString())).thenReturn(mResources); |
Tony Mantler | 8d16737 | 2017-07-14 15:37:17 -0700 | [diff] [blame] | 103 | when(mPackageManager.getResourcesForApplication((String) isNull())).thenReturn(mResources); |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 104 | when(mPackageManager.getApplicationInfo(eq("abc"), anyInt())) |
| 105 | .thenReturn(application.getApplicationInfo()); |
| 106 | mContentResolver = spy(application.getContentResolver()); |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 107 | when(mContext.getContentResolver()).thenReturn(mContentResolver); |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 108 | when(mContext.getPackageName()).thenReturn("com.android.settings"); |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void getTilesForIntent_shouldParseCategory() { |
| 113 | final String testCategory = "category1"; |
| 114 | Intent intent = new Intent(); |
| 115 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 116 | List<Tile> outTiles = new ArrayList<>(); |
| 117 | List<ResolveInfo> info = new ArrayList<>(); |
| 118 | info.add(newInfo(true, testCategory)); |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 119 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 120 | .thenReturn(info); |
| 121 | |
| 122 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 123 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 124 | false /* checkCategory */, true /* forceTintExternalIcon */); |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 125 | |
| 126 | assertThat(outTiles.size()).isEqualTo(1); |
| 127 | assertThat(outTiles.get(0).category).isEqualTo(testCategory); |
| 128 | } |
| 129 | |
| 130 | @Test |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 131 | public void getTilesForIntent_shouldParseKeyHintForSystemApp() { |
| 132 | String keyHint = "key"; |
| 133 | Intent intent = new Intent(); |
| 134 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 135 | List<Tile> outTiles = new ArrayList<>(); |
| 136 | List<ResolveInfo> info = new ArrayList<>(); |
| 137 | ResolveInfo resolveInfo = newInfo(true, null /* category */, keyHint); |
| 138 | info.add(resolveInfo); |
| 139 | |
| 140 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 141 | .thenReturn(info); |
| 142 | |
| 143 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 144 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 145 | false /* checkCategory */, true /* forceTintExternalIcon */); |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 146 | |
| 147 | assertThat(outTiles.size()).isEqualTo(1); |
| 148 | assertThat(outTiles.get(0).key).isEqualTo(keyHint); |
| 149 | } |
| 150 | |
| 151 | @Test |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 152 | public void getTilesForIntent_shouldSkipNonSystemApp() { |
| 153 | final String testCategory = "category1"; |
| 154 | Intent intent = new Intent(); |
| 155 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 156 | List<Tile> outTiles = new ArrayList<>(); |
| 157 | List<ResolveInfo> info = new ArrayList<>(); |
| 158 | info.add(newInfo(false, testCategory)); |
| 159 | |
| 160 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 161 | .thenReturn(info); |
| 162 | |
| 163 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 164 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 165 | false /* checkCategory */, true /* forceTintExternalIcon */); |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 166 | |
| 167 | assertThat(outTiles.isEmpty()).isTrue(); |
| 168 | } |
| 169 | |
Doris Ling | 485df11 | 2016-12-19 10:45:47 -0800 | [diff] [blame] | 170 | @Test |
Ido Ofir | a560cd9 | 2017-01-24 13:27:36 -0800 | [diff] [blame] | 171 | public void getTilesForIntent_shouldSkipFilteredApps() { |
Ido Ofir | a560cd9 | 2017-01-24 13:27:36 -0800 | [diff] [blame] | 172 | Intent intent = new Intent(); |
| 173 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 174 | List<Tile> outTiles = new ArrayList<>(); |
| 175 | List<ResolveInfo> info = new ArrayList<>(); |
| 176 | ResolveInfo resolveInfo = newInfo(true, null /* category */, null, URI_GET_ICON, |
| 177 | URI_GET_SUMMARY); |
| 178 | addMetadataToInfo(resolveInfo, "com.android.settings.require_account", "com.google"); |
| 179 | addMetadataToInfo(resolveInfo, "com.android.settings.require_connection", "true"); |
| 180 | info.add(resolveInfo); |
| 181 | |
| 182 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 183 | .thenReturn(info); |
| 184 | |
| 185 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 186 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 187 | false /* checkCategory */, true /* forceTintExternalIcon */); |
Ido Ofir | a560cd9 | 2017-01-24 13:27:36 -0800 | [diff] [blame] | 188 | |
| 189 | assertThat(outTiles.size()).isEqualTo(1); |
Maurice Lam | f74b9e5 | 2017-03-23 14:58:47 -0700 | [diff] [blame] | 190 | SuggestionParser parser = new SuggestionParser( |
| 191 | mContext, |
| 192 | null, |
| 193 | Collections.emptyList(), |
| 194 | "0,10"); |
Soroosh Mariooryad | 56ce766 | 2017-02-06 15:23:00 -0800 | [diff] [blame] | 195 | parser.filterSuggestions(outTiles, 0, false); |
Ido Ofir | a560cd9 | 2017-01-24 13:27:36 -0800 | [diff] [blame] | 196 | assertThat(outTiles.size()).isEqualTo(0); |
| 197 | } |
| 198 | |
| 199 | @Test |
Doris Ling | 485df11 | 2016-12-19 10:45:47 -0800 | [diff] [blame] | 200 | public void getCategories_shouldHandleExtraIntentAction() { |
| 201 | final String testCategory = "category1"; |
| 202 | final String testAction = "action1"; |
| 203 | Map<Pair<String, String>, Tile> cache = new ArrayMap<>(); |
| 204 | List<ResolveInfo> info = new ArrayList<>(); |
| 205 | info.add(newInfo(true, testCategory)); |
| 206 | Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 1); |
| 207 | when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); |
| 208 | List<UserHandle> userHandleList = new ArrayList<>(); |
| 209 | userHandleList.add(UserHandle.CURRENT); |
| 210 | when(mUserManager.getUserProfiles()).thenReturn(userHandleList); |
| 211 | |
Tony Mantler | 8d16737 | 2017-07-14 15:37:17 -0700 | [diff] [blame] | 212 | when(mPackageManager.queryIntentActivitiesAsUser(argThat( |
| 213 | event -> testAction.equals(event.getAction())), anyInt(), anyInt())) |
| 214 | .thenReturn(info); |
Doris Ling | 485df11 | 2016-12-19 10:45:47 -0800 | [diff] [blame] | 215 | |
| 216 | List<DashboardCategory> categoryList = TileUtils.getCategories( |
Fan Zhang | e138ef1 | 2017-05-11 15:29:56 -0700 | [diff] [blame] | 217 | mContext, cache, false /* categoryDefinedInManifest */, testAction, |
| 218 | TileUtils.SETTING_PKG); |
Doris Ling | 485df11 | 2016-12-19 10:45:47 -0800 | [diff] [blame] | 219 | assertThat(categoryList.get(0).tiles.get(0).category).isEqualTo(testCategory); |
| 220 | } |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 221 | |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 222 | @Test |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 223 | public void getCategories_withPackageName() throws Exception { |
| 224 | ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 225 | Map<Pair<String, String>, Tile> cache = new ArrayMap<>(); |
| 226 | Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 1); |
| 227 | when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); |
| 228 | List<UserHandle> userHandleList = new ArrayList<>(); |
| 229 | |
| 230 | userHandleList.add(new UserHandle(ActivityManager.getCurrentUser())); |
| 231 | when(mUserManager.getUserProfiles()).thenReturn(userHandleList); |
| 232 | |
| 233 | TileUtils.getCategories( |
Fan Zhang | e138ef1 | 2017-05-11 15:29:56 -0700 | [diff] [blame] | 234 | mContext, cache, false /* categoryDefinedInManifest */, null /* action */, |
| 235 | TileUtils.SETTING_PKG); |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 236 | verify(mPackageManager, atLeastOnce()).queryIntentActivitiesAsUser( |
Fan Zhang | e138ef1 | 2017-05-11 15:29:56 -0700 | [diff] [blame] | 237 | intentCaptor.capture(), anyInt(), anyInt()); |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 238 | |
| 239 | assertThat(intentCaptor.getAllValues().get(0).getPackage()) |
Fan Zhang | e138ef1 | 2017-05-11 15:29:56 -0700 | [diff] [blame] | 240 | .isEqualTo(TileUtils.SETTING_PKG); |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | @Test |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 244 | public void getTilesForIntent_shouldReadMetadataTitleAsString() throws RemoteException { |
| 245 | Intent intent = new Intent(); |
| 246 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 247 | List<Tile> outTiles = new ArrayList<>(); |
| 248 | List<ResolveInfo> info = new ArrayList<>(); |
| 249 | ResolveInfo resolveInfo = newInfo(true, null /* category */, null, URI_GET_ICON, |
| 250 | URI_GET_SUMMARY, "my title", 0); |
| 251 | info.add(resolveInfo); |
| 252 | |
| 253 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 254 | .thenReturn(info); |
| 255 | |
| 256 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 257 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 258 | false /* checkCategory */, true /* forceTintExternalIcon */); |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 259 | |
| 260 | assertThat(outTiles.size()).isEqualTo(1); |
| 261 | assertThat(outTiles.get(0).title).isEqualTo("my title"); |
| 262 | } |
| 263 | |
| 264 | @Test |
| 265 | public void getTilesForIntent_shouldReadMetadataTitleFromResource() throws RemoteException { |
| 266 | Intent intent = new Intent(); |
| 267 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 268 | List<Tile> outTiles = new ArrayList<>(); |
| 269 | List<ResolveInfo> info = new ArrayList<>(); |
| 270 | ResolveInfo resolveInfo = newInfo(true, null /* category */, null, URI_GET_ICON, |
| 271 | URI_GET_SUMMARY, null, 123); |
| 272 | info.add(resolveInfo); |
| 273 | |
| 274 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 275 | .thenReturn(info); |
| 276 | |
| 277 | when(mResources.getString(eq(123))) |
| 278 | .thenReturn("my localized title"); |
| 279 | |
| 280 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 281 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 282 | false /* checkCategory */, true /* forceTintExternalIcon */); |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 283 | |
| 284 | assertThat(outTiles.size()).isEqualTo(1); |
| 285 | assertThat(outTiles.get(0).title).isEqualTo("my localized title"); |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 286 | |
| 287 | // Icon should be tintable because the tile is not from settings package, and |
| 288 | // "forceTintExternalIcon" is set |
| 289 | assertThat(outTiles.get(0).isIconTintable).isTrue(); |
| 290 | } |
| 291 | |
| 292 | @Test |
| 293 | public void getTilesForIntent_shouldNotTintIconIfInSettingsPackage() { |
| 294 | Intent intent = new Intent(); |
| 295 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 296 | List<Tile> outTiles = new ArrayList<>(); |
| 297 | List<ResolveInfo> info = new ArrayList<>(); |
| 298 | ResolveInfo resolveInfo = newInfo(true, null /* category */, null, URI_GET_ICON, |
| 299 | URI_GET_SUMMARY, null, 123); |
| 300 | resolveInfo.activityInfo.packageName = "com.android.settings"; |
| 301 | resolveInfo.activityInfo.applicationInfo.packageName = "com.android.settings"; |
| 302 | info.add(resolveInfo); |
| 303 | |
| 304 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 305 | .thenReturn(info); |
| 306 | |
| 307 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 308 | null /* defaultCategory */, outTiles, false /* usePriority */, |
| 309 | false /* checkCategory */, true /* forceTintExternalIcon */); |
| 310 | |
| 311 | assertThat(outTiles.size()).isEqualTo(1); |
| 312 | assertThat(outTiles.get(0).isIconTintable).isFalse(); |
| 313 | } |
| 314 | |
| 315 | @Test |
| 316 | public void getTilesForIntent_shouldMarkIconTintableIfMetadataSet() { |
| 317 | Intent intent = new Intent(); |
| 318 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 319 | List<Tile> outTiles = new ArrayList<>(); |
| 320 | List<ResolveInfo> info = new ArrayList<>(); |
| 321 | ResolveInfo resolveInfo = newInfo(true, null /* category */, null, URI_GET_ICON, |
| 322 | URI_GET_SUMMARY, null, 123); |
| 323 | resolveInfo.activityInfo.metaData |
| 324 | .putBoolean(TileUtils.META_DATA_PREFERENCE_ICON_TINTABLE, true); |
| 325 | info.add(resolveInfo); |
| 326 | |
| 327 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 328 | .thenReturn(info); |
| 329 | |
| 330 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 331 | null /* defaultCategory */, outTiles, false /* usePriority */, |
| 332 | false /* checkCategory */, false /* forceTintExternalIcon */); |
| 333 | |
| 334 | assertThat(outTiles.size()).isEqualTo(1); |
| 335 | assertThat(outTiles.get(0).isIconTintable).isTrue(); |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | @Test |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 339 | public void getTilesForIntent_shouldNotProcessInvalidUriContentSystemApp() |
| 340 | throws RemoteException { |
| 341 | Intent intent = new Intent(); |
| 342 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 343 | List<Tile> outTiles = new ArrayList<>(); |
| 344 | List<ResolveInfo> info = new ArrayList<>(); |
| 345 | ResolveInfo resolveInfo = newInfo(true, null /* category */, null, null, URI_GET_SUMMARY); |
| 346 | info.add(resolveInfo); |
| 347 | |
| 348 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 349 | .thenReturn(info); |
| 350 | |
| 351 | // Case 1: No provider associated with the uri specified. |
| 352 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 353 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 354 | false /* checkCategory */, true /* forceTintExternalIcon */); |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 355 | |
| 356 | assertThat(outTiles.size()).isEqualTo(1); |
| 357 | assertThat(outTiles.get(0).icon.getResId()).isEqualTo(314159); |
| 358 | assertThat(outTiles.get(0).summary).isEqualTo("static-summary"); |
| 359 | |
| 360 | // Case 2: Empty bundle. |
| 361 | Bundle bundle = new Bundle(); |
| 362 | when(mIContentProvider.call(anyString(), |
| 363 | eq(TileUtils.getMethodFromUri(Uri.parse(URI_GET_SUMMARY))), eq(URI_GET_SUMMARY), |
| 364 | any())).thenReturn(bundle); |
William Luh | 5a0a0d8 | 2017-04-18 11:34:38 -0700 | [diff] [blame] | 365 | when(mContentResolver.acquireUnstableProvider(anyString())) |
| 366 | .thenReturn(mIContentProvider); |
| 367 | when(mContentResolver.acquireUnstableProvider(any(Uri.class))) |
| 368 | .thenReturn(mIContentProvider); |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 369 | |
| 370 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 371 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 372 | false /* checkCategory */, true /* forceTintExternalIcon */); |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 373 | |
| 374 | assertThat(outTiles.size()).isEqualTo(1); |
| 375 | assertThat(outTiles.get(0).icon.getResId()).isEqualTo(314159); |
| 376 | assertThat(outTiles.get(0).summary).isEqualTo("static-summary"); |
| 377 | } |
| 378 | |
| 379 | @Test |
| 380 | public void getTilesForIntent_shouldProcessUriContentForSystemApp() throws RemoteException { |
| 381 | Intent intent = new Intent(); |
| 382 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 383 | List<Tile> outTiles = new ArrayList<>(); |
| 384 | List<ResolveInfo> info = new ArrayList<>(); |
| 385 | ResolveInfo resolveInfo = newInfo(true, null /* category */, null, URI_GET_ICON, |
| 386 | URI_GET_SUMMARY); |
| 387 | info.add(resolveInfo); |
| 388 | |
| 389 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 390 | .thenReturn(info); |
| 391 | |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 392 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 393 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 394 | false /* checkCategory */, true /* forceTintExternalIcon */); |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 395 | |
| 396 | assertThat(outTiles.size()).isEqualTo(1); |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Maurice Lam | d8ae77b | 2017-03-23 21:26:01 -0700 | [diff] [blame] | 399 | @Test |
| 400 | public void getTilesForIntent_shouldShowRemoteViewIfSpecified() { |
| 401 | Intent intent = new Intent(); |
| 402 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 403 | List<Tile> outTiles = new ArrayList<>(); |
| 404 | List<ResolveInfo> info = new ArrayList<>(); |
| 405 | ResolveInfo resolveInfo = newInfo(true, null /* category */); |
| 406 | resolveInfo.activityInfo.metaData.putInt("com.android.settings.custom_view", |
| 407 | R.layout.user_preference); |
| 408 | info.add(resolveInfo); |
| 409 | |
| 410 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 411 | .thenReturn(info); |
| 412 | |
| 413 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 414 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 415 | false /* checkCategory */, true /* forceTintExternalIcon */); |
Maurice Lam | d8ae77b | 2017-03-23 21:26:01 -0700 | [diff] [blame] | 416 | |
| 417 | assertThat(outTiles.size()).isEqualTo(1); |
| 418 | Tile tile = outTiles.get(0); |
| 419 | assertThat(tile.remoteViews).isNotNull(); |
| 420 | assertThat(tile.remoteViews.getLayoutId()).isEqualTo(R.layout.user_preference); |
| 421 | } |
| 422 | |
Jaewoong Jung | 78c5e5d | 2017-06-15 18:02:44 -0700 | [diff] [blame] | 423 | @Test |
| 424 | public void getTilesForIntent_summaryUriSpecified_shouldOverrideRemoteViewSummary() |
| 425 | throws RemoteException { |
| 426 | Intent intent = new Intent(); |
| 427 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 428 | List<Tile> outTiles = new ArrayList<>(); |
| 429 | List<ResolveInfo> info = new ArrayList<>(); |
| 430 | ResolveInfo resolveInfo = newInfo(true, null /* category */, null, |
| 431 | null, URI_GET_SUMMARY); |
| 432 | resolveInfo.activityInfo.metaData.putInt("com.android.settings.custom_view", |
| 433 | R.layout.user_preference); |
| 434 | info.add(resolveInfo); |
| 435 | |
| 436 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 437 | .thenReturn(info); |
| 438 | |
| 439 | // Mock the content provider interaction. |
| 440 | Bundle bundle = new Bundle(); |
| 441 | bundle.putString(TileUtils.META_DATA_PREFERENCE_SUMMARY, "new summary text"); |
| 442 | when(mIContentProvider.call(anyString(), |
| 443 | eq(TileUtils.getMethodFromUri(Uri.parse(URI_GET_SUMMARY))), eq(URI_GET_SUMMARY), |
| 444 | any())).thenReturn(bundle); |
| 445 | when(mContentResolver.acquireUnstableProvider(anyString())) |
| 446 | .thenReturn(mIContentProvider); |
| 447 | when(mContentResolver.acquireUnstableProvider(any(Uri.class))) |
| 448 | .thenReturn(mIContentProvider); |
| 449 | |
| 450 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 451 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 452 | false /* checkCategory */, true /* forceTintExternalIcon */); |
Jaewoong Jung | 78c5e5d | 2017-06-15 18:02:44 -0700 | [diff] [blame] | 453 | |
| 454 | assertThat(outTiles.size()).isEqualTo(1); |
| 455 | Tile tile = outTiles.get(0); |
| 456 | assertThat(tile.remoteViews).isNotNull(); |
| 457 | assertThat(tile.remoteViews.getLayoutId()).isEqualTo(R.layout.user_preference); |
| 458 | // Make sure the summary TextView got a new text string. |
| 459 | TileUtilsShadowRemoteViews shadowRemoteViews = |
| 460 | (TileUtilsShadowRemoteViews) ShadowExtractor.extract(tile.remoteViews); |
| 461 | assertThat(shadowRemoteViews.overrideViewId).isEqualTo(android.R.id.summary); |
| 462 | assertThat(shadowRemoteViews.overrideText).isEqualTo("new summary text"); |
| 463 | } |
| 464 | |
| 465 | @Test |
| 466 | public void getTilesForIntent_providerUnavailable_shouldNotOverrideRemoteViewSummary() |
| 467 | throws RemoteException { |
| 468 | Intent intent = new Intent(); |
| 469 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 470 | List<Tile> outTiles = new ArrayList<>(); |
| 471 | List<ResolveInfo> info = new ArrayList<>(); |
| 472 | ResolveInfo resolveInfo = newInfo(true, null /* category */, null, |
| 473 | null, URI_GET_SUMMARY); |
| 474 | resolveInfo.activityInfo.metaData.putInt("com.android.settings.custom_view", |
| 475 | R.layout.user_preference); |
| 476 | info.add(resolveInfo); |
| 477 | |
| 478 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 479 | .thenReturn(info); |
| 480 | |
| 481 | // Mock the content provider interaction. |
| 482 | Bundle bundle = new Bundle(); |
| 483 | bundle.putString(TileUtils.META_DATA_PREFERENCE_SUMMARY, "new summary text"); |
| 484 | when(mIContentProvider.call(anyString(), |
| 485 | eq(TileUtils.getMethodFromUri(Uri.parse(URI_GET_SUMMARY))), eq(URI_GET_SUMMARY), |
| 486 | any())).thenReturn(bundle); |
| 487 | |
| 488 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 489 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 490 | false /* checkCategory */, true /* forceTintExternalIcon */); |
Jaewoong Jung | 78c5e5d | 2017-06-15 18:02:44 -0700 | [diff] [blame] | 491 | |
| 492 | assertThat(outTiles.size()).isEqualTo(1); |
| 493 | Tile tile = outTiles.get(0); |
| 494 | assertThat(tile.remoteViews).isNotNull(); |
| 495 | assertThat(tile.remoteViews.getLayoutId()).isEqualTo(R.layout.user_preference); |
| 496 | // Make sure the summary TextView didn't get any text view updates. |
| 497 | TileUtilsShadowRemoteViews shadowRemoteViews = |
| 498 | (TileUtilsShadowRemoteViews) ShadowExtractor.extract(tile.remoteViews); |
| 499 | assertThat(shadowRemoteViews.overrideViewId).isNull(); |
| 500 | assertThat(shadowRemoteViews.overrideText).isNull(); |
| 501 | } |
| 502 | |
Soroosh Mariooryad | 56ce766 | 2017-02-06 15:23:00 -0800 | [diff] [blame] | 503 | public static ResolveInfo newInfo(boolean systemApp, String category) { |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 504 | return newInfo(systemApp, category, null); |
| 505 | } |
| 506 | |
Soroosh Mariooryad | 56ce766 | 2017-02-06 15:23:00 -0800 | [diff] [blame] | 507 | private static ResolveInfo newInfo(boolean systemApp, String category, String keyHint) { |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 508 | return newInfo(systemApp, category, keyHint, null, null); |
| 509 | } |
| 510 | |
Soroosh Mariooryad | 56ce766 | 2017-02-06 15:23:00 -0800 | [diff] [blame] | 511 | private static ResolveInfo newInfo(boolean systemApp, String category, String keyHint, |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 512 | String iconUri, String summaryUri) { |
| 513 | return newInfo(systemApp, category, keyHint, iconUri, summaryUri, null, 0); |
| 514 | } |
| 515 | |
| 516 | private static ResolveInfo newInfo(boolean systemApp, String category, String keyHint, |
| 517 | String iconUri, String summaryUri, String title, int titleResId) { |
| 518 | |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 519 | ResolveInfo info = new ResolveInfo(); |
| 520 | info.system = systemApp; |
| 521 | info.activityInfo = new ActivityInfo(); |
| 522 | info.activityInfo.packageName = "abc"; |
| 523 | info.activityInfo.name = "123"; |
| 524 | info.activityInfo.metaData = new Bundle(); |
| 525 | info.activityInfo.metaData.putString("com.android.settings.category", category); |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 526 | info.activityInfo.metaData.putInt("com.android.settings.icon", 314159); |
| 527 | info.activityInfo.metaData.putString("com.android.settings.summary", "static-summary"); |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 528 | if (keyHint != null) { |
| 529 | info.activityInfo.metaData.putString("com.android.settings.keyhint", keyHint); |
| 530 | } |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 531 | if (iconUri != null) { |
| 532 | info.activityInfo.metaData.putString("com.android.settings.icon_uri", iconUri); |
| 533 | } |
| 534 | if (summaryUri != null) { |
| 535 | info.activityInfo.metaData.putString("com.android.settings.summary_uri", summaryUri); |
| 536 | } |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 537 | if (titleResId != 0) { |
Fan Zhang | dadfd50 | 2017-07-26 11:00:51 -0700 | [diff] [blame] | 538 | info.activityInfo.metaData.putInt(TileUtils.META_DATA_PREFERENCE_TITLE, titleResId); |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 539 | } else if (title != null) { |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 540 | info.activityInfo.metaData.putString(TileUtils.META_DATA_PREFERENCE_TITLE, title); |
| 541 | } |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 542 | info.activityInfo.applicationInfo = new ApplicationInfo(); |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 543 | if (systemApp) { |
| 544 | info.activityInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 545 | } |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 546 | return info; |
| 547 | } |
Ido Ofir | a560cd9 | 2017-01-24 13:27:36 -0800 | [diff] [blame] | 548 | |
| 549 | private void addMetadataToInfo(ResolveInfo info, String key, String value) { |
| 550 | if (!TextUtils.isEmpty(key)) { |
| 551 | if (info.activityInfo == null) { |
| 552 | info.activityInfo = new ActivityInfo(); |
| 553 | } |
| 554 | if (info.activityInfo.metaData == null) { |
| 555 | info.activityInfo.metaData = new Bundle(); |
| 556 | } |
| 557 | info.activityInfo.metaData.putString(key, value); |
| 558 | } |
| 559 | } |
Jaewoong Jung | 78c5e5d | 2017-06-15 18:02:44 -0700 | [diff] [blame] | 560 | |
| 561 | @Implements(RemoteViews.class) |
| 562 | public static class TileUtilsShadowRemoteViews { |
| 563 | |
| 564 | private Integer overrideViewId; |
| 565 | private CharSequence overrideText; |
| 566 | |
| 567 | @Implementation |
| 568 | public void setTextViewText(int viewId, CharSequence text) { |
| 569 | overrideViewId = viewId; |
| 570 | overrideText = text; |
| 571 | } |
| 572 | } |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 573 | } |