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; |
Fan Zhang | c75174b | 2018-07-19 14:07:58 -0700 | [diff] [blame] | 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; |
Ajay Nadathur | d1b730b | 2017-09-14 17:12:27 -0700 | [diff] [blame] | 25 | import static org.mockito.ArgumentMatchers.eq; |
Tony Mantler | 8d16737 | 2017-07-14 15:37:17 -0700 | [diff] [blame] | 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 | |
Fan Zhang | c75174b | 2018-07-19 14:07:58 -0700 | [diff] [blame] | 55 | import com.android.settingslib.SettingsLibRobolectricTestRunner; |
| 56 | |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 57 | import org.junit.Before; |
| 58 | import org.junit.Test; |
| 59 | import org.junit.runner.RunWith; |
Maurice Lam | f74b9e5 | 2017-03-23 14:58:47 -0700 | [diff] [blame] | 60 | import org.mockito.ArgumentCaptor; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 61 | import org.mockito.Mock; |
| 62 | import org.mockito.MockitoAnnotations; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 63 | import org.robolectric.annotation.Config; |
Ajay Nadathur | d1b730b | 2017-09-14 17:12:27 -0700 | [diff] [blame] | 64 | import org.robolectric.annotation.Implementation; |
| 65 | import org.robolectric.annotation.Implements; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 66 | |
| 67 | import java.util.ArrayList; |
| 68 | import java.util.List; |
| 69 | import java.util.Map; |
| 70 | |
Fan Zhang | c75174b | 2018-07-19 14:07:58 -0700 | [diff] [blame] | 71 | @RunWith(SettingsLibRobolectricTestRunner.class) |
James Lemieux | 5c50dc1 | 2018-02-12 01:30:32 -0800 | [diff] [blame] | 72 | @Config(shadows = TileUtilsTest.TileUtilsShadowRemoteViews.class) |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 73 | public class TileUtilsTest { |
| 74 | |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 75 | private Context mContext; |
| 76 | @Mock |
| 77 | private PackageManager mPackageManager; |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 78 | @Mock |
| 79 | private Resources mResources; |
Doris Ling | 485df11 | 2016-12-19 10:45:47 -0800 | [diff] [blame] | 80 | @Mock |
| 81 | private UserManager mUserManager; |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 82 | @Mock |
| 83 | private IContentProvider mIContentProvider; |
| 84 | @Mock |
| 85 | private ContentResolver mContentResolver; |
| 86 | |
| 87 | private static final String URI_GET_SUMMARY = "content://authority/text/summary"; |
| 88 | private static final String URI_GET_ICON = "content://authority/icon/my_icon"; |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 89 | |
| 90 | @Before |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 91 | public void setUp() throws NameNotFoundException { |
Tony Mantler | f6631bf | 2017-10-10 11:09:08 -0700 | [diff] [blame] | 92 | mContext = spy(application); |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 93 | MockitoAnnotations.initMocks(this); |
| 94 | when(mContext.getPackageManager()).thenReturn(mPackageManager); |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 95 | when(mPackageManager.getResourcesForApplication(anyString())).thenReturn(mResources); |
Tony Mantler | 8d16737 | 2017-07-14 15:37:17 -0700 | [diff] [blame] | 96 | when(mPackageManager.getResourcesForApplication((String) isNull())).thenReturn(mResources); |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 97 | when(mPackageManager.getApplicationInfo(eq("abc"), anyInt())) |
| 98 | .thenReturn(application.getApplicationInfo()); |
| 99 | mContentResolver = spy(application.getContentResolver()); |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 100 | when(mContext.getContentResolver()).thenReturn(mContentResolver); |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 101 | when(mContext.getPackageName()).thenReturn("com.android.settings"); |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void getTilesForIntent_shouldParseCategory() { |
| 106 | final String testCategory = "category1"; |
| 107 | Intent intent = new Intent(); |
| 108 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 109 | List<Tile> outTiles = new ArrayList<>(); |
| 110 | List<ResolveInfo> info = new ArrayList<>(); |
| 111 | info.add(newInfo(true, testCategory)); |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 112 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 113 | .thenReturn(info); |
| 114 | |
| 115 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 116 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 117 | false /* checkCategory */, true /* forceTintExternalIcon */); |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 118 | |
| 119 | assertThat(outTiles.size()).isEqualTo(1); |
| 120 | assertThat(outTiles.get(0).category).isEqualTo(testCategory); |
| 121 | } |
| 122 | |
| 123 | @Test |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 124 | public void getTilesForIntent_shouldParseKeyHintForSystemApp() { |
| 125 | String keyHint = "key"; |
| 126 | Intent intent = new Intent(); |
| 127 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 128 | List<Tile> outTiles = new ArrayList<>(); |
| 129 | List<ResolveInfo> info = new ArrayList<>(); |
| 130 | ResolveInfo resolveInfo = newInfo(true, null /* category */, keyHint); |
| 131 | info.add(resolveInfo); |
| 132 | |
| 133 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 134 | .thenReturn(info); |
| 135 | |
| 136 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 137 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 138 | false /* checkCategory */, true /* forceTintExternalIcon */); |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 139 | |
| 140 | assertThat(outTiles.size()).isEqualTo(1); |
| 141 | assertThat(outTiles.get(0).key).isEqualTo(keyHint); |
| 142 | } |
| 143 | |
| 144 | @Test |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 145 | public void getTilesForIntent_shouldSkipNonSystemApp() { |
| 146 | final String testCategory = "category1"; |
| 147 | Intent intent = new Intent(); |
| 148 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 149 | List<Tile> outTiles = new ArrayList<>(); |
| 150 | List<ResolveInfo> info = new ArrayList<>(); |
| 151 | info.add(newInfo(false, testCategory)); |
| 152 | |
| 153 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 154 | .thenReturn(info); |
| 155 | |
| 156 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 157 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 158 | false /* checkCategory */, true /* forceTintExternalIcon */); |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 159 | |
| 160 | assertThat(outTiles.isEmpty()).isTrue(); |
| 161 | } |
| 162 | |
Doris Ling | 485df11 | 2016-12-19 10:45:47 -0800 | [diff] [blame] | 163 | @Test |
| 164 | public void getCategories_shouldHandleExtraIntentAction() { |
| 165 | final String testCategory = "category1"; |
| 166 | final String testAction = "action1"; |
| 167 | Map<Pair<String, String>, Tile> cache = new ArrayMap<>(); |
| 168 | List<ResolveInfo> info = new ArrayList<>(); |
| 169 | info.add(newInfo(true, testCategory)); |
| 170 | Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 1); |
| 171 | when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); |
| 172 | List<UserHandle> userHandleList = new ArrayList<>(); |
| 173 | userHandleList.add(UserHandle.CURRENT); |
| 174 | when(mUserManager.getUserProfiles()).thenReturn(userHandleList); |
| 175 | |
Tony Mantler | 8d16737 | 2017-07-14 15:37:17 -0700 | [diff] [blame] | 176 | when(mPackageManager.queryIntentActivitiesAsUser(argThat( |
| 177 | event -> testAction.equals(event.getAction())), anyInt(), anyInt())) |
| 178 | .thenReturn(info); |
Doris Ling | 485df11 | 2016-12-19 10:45:47 -0800 | [diff] [blame] | 179 | |
| 180 | List<DashboardCategory> categoryList = TileUtils.getCategories( |
Fan Zhang | 1b0dfa3 | 2018-07-20 12:57:55 -0700 | [diff] [blame] | 181 | mContext, cache, testAction, CategoryManager.SETTING_PKG); |
Doris Ling | b8d2cd4 | 2017-11-27 12:24:09 -0800 | [diff] [blame] | 182 | assertThat(categoryList.get(0).getTile(0).category).isEqualTo(testCategory); |
Doris Ling | 485df11 | 2016-12-19 10:45:47 -0800 | [diff] [blame] | 183 | } |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 184 | |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 185 | @Test |
Fan Zhang | c75174b | 2018-07-19 14:07:58 -0700 | [diff] [blame] | 186 | public void getCategories_withPackageName() { |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 187 | ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 188 | Map<Pair<String, String>, Tile> cache = new ArrayMap<>(); |
| 189 | Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 1); |
| 190 | when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); |
| 191 | List<UserHandle> userHandleList = new ArrayList<>(); |
| 192 | |
| 193 | userHandleList.add(new UserHandle(ActivityManager.getCurrentUser())); |
| 194 | when(mUserManager.getUserProfiles()).thenReturn(userHandleList); |
| 195 | |
Fan Zhang | 1b0dfa3 | 2018-07-20 12:57:55 -0700 | [diff] [blame] | 196 | TileUtils.getCategories(mContext, cache, null /* action */, CategoryManager.SETTING_PKG); |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 197 | verify(mPackageManager, atLeastOnce()).queryIntentActivitiesAsUser( |
Fan Zhang | e138ef1 | 2017-05-11 15:29:56 -0700 | [diff] [blame] | 198 | intentCaptor.capture(), anyInt(), anyInt()); |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 199 | |
| 200 | assertThat(intentCaptor.getAllValues().get(0).getPackage()) |
Fan Zhang | 1b0dfa3 | 2018-07-20 12:57:55 -0700 | [diff] [blame] | 201 | .isEqualTo(CategoryManager.SETTING_PKG); |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Test |
Fan Zhang | c75174b | 2018-07-19 14:07:58 -0700 | [diff] [blame] | 205 | public void getTilesForIntent_shouldReadMetadataTitleAsString() { |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 206 | Intent intent = new Intent(); |
| 207 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 208 | List<Tile> outTiles = new ArrayList<>(); |
| 209 | List<ResolveInfo> info = new ArrayList<>(); |
| 210 | ResolveInfo resolveInfo = newInfo(true, null /* category */, null, URI_GET_ICON, |
| 211 | URI_GET_SUMMARY, "my title", 0); |
| 212 | info.add(resolveInfo); |
| 213 | |
| 214 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 215 | .thenReturn(info); |
| 216 | |
| 217 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 218 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 219 | false /* checkCategory */, true /* forceTintExternalIcon */); |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 220 | |
| 221 | assertThat(outTiles.size()).isEqualTo(1); |
| 222 | assertThat(outTiles.get(0).title).isEqualTo("my title"); |
| 223 | } |
| 224 | |
| 225 | @Test |
Fan Zhang | c75174b | 2018-07-19 14:07:58 -0700 | [diff] [blame] | 226 | public void getTilesForIntent_shouldReadMetadataTitleFromResource() { |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 227 | Intent intent = new Intent(); |
| 228 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 229 | List<Tile> outTiles = new ArrayList<>(); |
| 230 | List<ResolveInfo> info = new ArrayList<>(); |
| 231 | ResolveInfo resolveInfo = newInfo(true, null /* category */, null, URI_GET_ICON, |
| 232 | URI_GET_SUMMARY, null, 123); |
| 233 | info.add(resolveInfo); |
| 234 | |
| 235 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 236 | .thenReturn(info); |
| 237 | |
| 238 | when(mResources.getString(eq(123))) |
| 239 | .thenReturn("my localized title"); |
| 240 | |
| 241 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 242 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 243 | false /* checkCategory */, true /* forceTintExternalIcon */); |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 244 | |
| 245 | assertThat(outTiles.size()).isEqualTo(1); |
| 246 | assertThat(outTiles.get(0).title).isEqualTo("my localized title"); |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 247 | |
| 248 | // Icon should be tintable because the tile is not from settings package, and |
| 249 | // "forceTintExternalIcon" is set |
| 250 | assertThat(outTiles.get(0).isIconTintable).isTrue(); |
| 251 | } |
| 252 | |
| 253 | @Test |
| 254 | public void getTilesForIntent_shouldNotTintIconIfInSettingsPackage() { |
| 255 | Intent intent = new Intent(); |
| 256 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 257 | List<Tile> outTiles = new ArrayList<>(); |
| 258 | List<ResolveInfo> info = new ArrayList<>(); |
| 259 | ResolveInfo resolveInfo = newInfo(true, null /* category */, null, URI_GET_ICON, |
| 260 | URI_GET_SUMMARY, null, 123); |
| 261 | resolveInfo.activityInfo.packageName = "com.android.settings"; |
| 262 | resolveInfo.activityInfo.applicationInfo.packageName = "com.android.settings"; |
| 263 | info.add(resolveInfo); |
| 264 | |
| 265 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 266 | .thenReturn(info); |
| 267 | |
| 268 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 269 | null /* defaultCategory */, outTiles, false /* usePriority */, |
| 270 | false /* checkCategory */, true /* forceTintExternalIcon */); |
| 271 | |
| 272 | assertThat(outTiles.size()).isEqualTo(1); |
| 273 | assertThat(outTiles.get(0).isIconTintable).isFalse(); |
| 274 | } |
| 275 | |
| 276 | @Test |
| 277 | public void getTilesForIntent_shouldMarkIconTintableIfMetadataSet() { |
| 278 | Intent intent = new Intent(); |
| 279 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 280 | List<Tile> outTiles = new ArrayList<>(); |
| 281 | List<ResolveInfo> info = new ArrayList<>(); |
| 282 | ResolveInfo resolveInfo = newInfo(true, null /* category */, null, URI_GET_ICON, |
| 283 | URI_GET_SUMMARY, null, 123); |
| 284 | resolveInfo.activityInfo.metaData |
| 285 | .putBoolean(TileUtils.META_DATA_PREFERENCE_ICON_TINTABLE, true); |
| 286 | info.add(resolveInfo); |
| 287 | |
| 288 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 289 | .thenReturn(info); |
| 290 | |
| 291 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 292 | null /* defaultCategory */, outTiles, false /* usePriority */, |
| 293 | false /* checkCategory */, false /* forceTintExternalIcon */); |
| 294 | |
| 295 | assertThat(outTiles.size()).isEqualTo(1); |
| 296 | assertThat(outTiles.get(0).isIconTintable).isTrue(); |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | @Test |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 300 | public void getTilesForIntent_shouldNotProcessInvalidUriContentSystemApp() |
| 301 | throws RemoteException { |
| 302 | Intent intent = new Intent(); |
| 303 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 304 | List<Tile> outTiles = new ArrayList<>(); |
| 305 | List<ResolveInfo> info = new ArrayList<>(); |
| 306 | ResolveInfo resolveInfo = newInfo(true, null /* category */, null, null, URI_GET_SUMMARY); |
| 307 | info.add(resolveInfo); |
| 308 | |
| 309 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 310 | .thenReturn(info); |
| 311 | |
| 312 | // Case 1: No provider associated with the uri specified. |
| 313 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 314 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 315 | false /* checkCategory */, true /* forceTintExternalIcon */); |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 316 | |
| 317 | assertThat(outTiles.size()).isEqualTo(1); |
| 318 | assertThat(outTiles.get(0).icon.getResId()).isEqualTo(314159); |
| 319 | assertThat(outTiles.get(0).summary).isEqualTo("static-summary"); |
| 320 | |
| 321 | // Case 2: Empty bundle. |
| 322 | Bundle bundle = new Bundle(); |
| 323 | when(mIContentProvider.call(anyString(), |
| 324 | eq(TileUtils.getMethodFromUri(Uri.parse(URI_GET_SUMMARY))), eq(URI_GET_SUMMARY), |
| 325 | any())).thenReturn(bundle); |
William Luh | 5a0a0d8 | 2017-04-18 11:34:38 -0700 | [diff] [blame] | 326 | when(mContentResolver.acquireUnstableProvider(anyString())) |
| 327 | .thenReturn(mIContentProvider); |
| 328 | when(mContentResolver.acquireUnstableProvider(any(Uri.class))) |
| 329 | .thenReturn(mIContentProvider); |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 330 | |
| 331 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 332 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 333 | false /* checkCategory */, true /* forceTintExternalIcon */); |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 334 | |
| 335 | assertThat(outTiles.size()).isEqualTo(1); |
| 336 | assertThat(outTiles.get(0).icon.getResId()).isEqualTo(314159); |
| 337 | assertThat(outTiles.get(0).summary).isEqualTo("static-summary"); |
| 338 | } |
| 339 | |
| 340 | @Test |
Fan Zhang | c75174b | 2018-07-19 14:07:58 -0700 | [diff] [blame] | 341 | public void getTilesForIntent_shouldProcessUriContentForSystemApp() { |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 342 | Intent intent = new Intent(); |
| 343 | Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>(); |
| 344 | List<Tile> outTiles = new ArrayList<>(); |
| 345 | List<ResolveInfo> info = new ArrayList<>(); |
| 346 | ResolveInfo resolveInfo = newInfo(true, null /* category */, null, URI_GET_ICON, |
| 347 | URI_GET_SUMMARY); |
| 348 | info.add(resolveInfo); |
| 349 | |
| 350 | when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt())) |
| 351 | .thenReturn(info); |
| 352 | |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 353 | TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache, |
| 354 | null /* defaultCategory */, outTiles, false /* usePriority */, |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 355 | false /* checkCategory */, true /* forceTintExternalIcon */); |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 356 | |
| 357 | assertThat(outTiles.size()).isEqualTo(1); |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Soroosh Mariooryad | 56ce766 | 2017-02-06 15:23:00 -0800 | [diff] [blame] | 360 | public static ResolveInfo newInfo(boolean systemApp, String category) { |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 361 | return newInfo(systemApp, category, null); |
| 362 | } |
| 363 | |
Soroosh Mariooryad | 56ce766 | 2017-02-06 15:23:00 -0800 | [diff] [blame] | 364 | private static ResolveInfo newInfo(boolean systemApp, String category, String keyHint) { |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 365 | return newInfo(systemApp, category, keyHint, null, null); |
| 366 | } |
| 367 | |
Soroosh Mariooryad | 56ce766 | 2017-02-06 15:23:00 -0800 | [diff] [blame] | 368 | private static ResolveInfo newInfo(boolean systemApp, String category, String keyHint, |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 369 | String iconUri, String summaryUri) { |
| 370 | return newInfo(systemApp, category, keyHint, iconUri, summaryUri, null, 0); |
| 371 | } |
| 372 | |
| 373 | private static ResolveInfo newInfo(boolean systemApp, String category, String keyHint, |
| 374 | String iconUri, String summaryUri, String title, int titleResId) { |
| 375 | |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 376 | ResolveInfo info = new ResolveInfo(); |
| 377 | info.system = systemApp; |
| 378 | info.activityInfo = new ActivityInfo(); |
| 379 | info.activityInfo.packageName = "abc"; |
| 380 | info.activityInfo.name = "123"; |
| 381 | info.activityInfo.metaData = new Bundle(); |
| 382 | info.activityInfo.metaData.putString("com.android.settings.category", category); |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 383 | info.activityInfo.metaData.putInt("com.android.settings.icon", 314159); |
| 384 | info.activityInfo.metaData.putString("com.android.settings.summary", "static-summary"); |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 385 | if (keyHint != null) { |
| 386 | info.activityInfo.metaData.putString("com.android.settings.keyhint", keyHint); |
| 387 | } |
Shahriyar Amini | 676add4 | 2016-12-16 11:29:39 -0800 | [diff] [blame] | 388 | if (iconUri != null) { |
| 389 | info.activityInfo.metaData.putString("com.android.settings.icon_uri", iconUri); |
| 390 | } |
| 391 | if (summaryUri != null) { |
| 392 | info.activityInfo.metaData.putString("com.android.settings.summary_uri", summaryUri); |
| 393 | } |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 394 | if (titleResId != 0) { |
Fan Zhang | dadfd50 | 2017-07-26 11:00:51 -0700 | [diff] [blame] | 395 | info.activityInfo.metaData.putInt(TileUtils.META_DATA_PREFERENCE_TITLE, titleResId); |
Maurice Lam | b10c6ff | 2017-06-08 20:29:21 -0700 | [diff] [blame] | 396 | } else if (title != null) { |
William Luh | 4c978a3 | 2017-03-31 15:08:16 -0700 | [diff] [blame] | 397 | info.activityInfo.metaData.putString(TileUtils.META_DATA_PREFERENCE_TITLE, title); |
| 398 | } |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 399 | info.activityInfo.applicationInfo = new ApplicationInfo(); |
Shahriyar Amini | 6b32ae3 | 2016-11-22 14:49:04 -0800 | [diff] [blame] | 400 | if (systemApp) { |
| 401 | info.activityInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 402 | } |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 403 | return info; |
| 404 | } |
Ido Ofir | a560cd9 | 2017-01-24 13:27:36 -0800 | [diff] [blame] | 405 | |
| 406 | private void addMetadataToInfo(ResolveInfo info, String key, String value) { |
| 407 | if (!TextUtils.isEmpty(key)) { |
| 408 | if (info.activityInfo == null) { |
| 409 | info.activityInfo = new ActivityInfo(); |
| 410 | } |
| 411 | if (info.activityInfo.metaData == null) { |
| 412 | info.activityInfo.metaData = new Bundle(); |
| 413 | } |
| 414 | info.activityInfo.metaData.putString(key, value); |
| 415 | } |
| 416 | } |
Jaewoong Jung | 78c5e5d | 2017-06-15 18:02:44 -0700 | [diff] [blame] | 417 | |
Ajay Nadathur | d1b730b | 2017-09-14 17:12:27 -0700 | [diff] [blame] | 418 | @Implements(RemoteViews.class) |
| 419 | public static class TileUtilsShadowRemoteViews { |
| 420 | |
| 421 | private Integer overrideViewId; |
| 422 | private CharSequence overrideText; |
| 423 | |
| 424 | @Implementation |
| 425 | public void setTextViewText(int viewId, CharSequence text) { |
| 426 | overrideViewId = viewId; |
| 427 | overrideText = text; |
| 428 | } |
| 429 | } |
Fan Zhang | cdbe1d6 | 2016-11-22 15:47:05 -0800 | [diff] [blame] | 430 | } |