1 /* 2 * Copyright (C) 2021 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.systemui.people; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.eq; 23 import static org.mockito.Mockito.when; 24 25 import android.app.people.ConversationChannel; 26 import android.content.pm.PackageManager; 27 import android.content.pm.ShortcutInfo; 28 import android.net.Uri; 29 import android.os.Bundle; 30 import android.os.RemoteException; 31 import android.os.UserHandle; 32 import android.widget.RemoteViews; 33 34 import androidx.test.ext.junit.runners.AndroidJUnit4; 35 import androidx.test.filters.SmallTest; 36 37 import com.android.systemui.SysuiTestCase; 38 import com.android.systemui.people.widget.PeopleSpaceWidgetManager; 39 import com.android.systemui.shared.system.PeopleProviderUtils; 40 41 import junit.framework.Assert; 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 49 @RunWith(AndroidJUnit4.class) 50 @SmallTest 51 public class PeopleProviderTest extends SysuiTestCase { 52 private static final String TAG = "PeopleProviderTest"; 53 54 private static final Uri URI = Uri.parse(PeopleProviderUtils.PEOPLE_PROVIDER_SCHEME 55 + PeopleProviderUtils.PEOPLE_PROVIDER_AUTHORITY); 56 57 private static final String SHORTCUT_ID_A = "shortcut_id_a"; 58 private static final String PACKAGE_NAME_A = "package_name_a"; 59 private static final UserHandle USER_HANDLE_A = UserHandle.of(1); 60 private static final String USERNAME = "username"; 61 62 private final ShortcutInfo mShortcutInfo = 63 new ShortcutInfo.Builder(mContext, SHORTCUT_ID_A).setLongLabel(USERNAME).build(); 64 private final ConversationChannel mConversationChannel = 65 new ConversationChannel(mShortcutInfo, USER_HANDLE_A.getIdentifier(), 66 null, null, 0L, false); 67 68 private Bundle mExtras = new Bundle(); 69 70 @Mock 71 private PackageManager mPackageManager; 72 @Mock 73 private PeopleSpaceWidgetManager mPeopleSpaceWidgetManager; 74 @Mock 75 private RemoteViews mRemoteViews; 76 77 @Before setUp()78 public void setUp() throws Exception { 79 MockitoAnnotations.initMocks(this); 80 mContext.setMockPackageManager(mPackageManager); 81 82 PeopleProviderTestable provider = new PeopleProviderTestable(); 83 provider.initializeForTesting( 84 mContext, PeopleProviderUtils.PEOPLE_PROVIDER_AUTHORITY); 85 provider.setPeopleSpaceWidgetManager(mPeopleSpaceWidgetManager); 86 mContext.getContentResolver().addProvider( 87 PeopleProviderUtils.PEOPLE_PROVIDER_AUTHORITY, provider); 88 89 mContext.getTestablePermissions().setPermission( 90 PeopleProviderUtils.GET_PEOPLE_TILE_PREVIEW_PERMISSION, 91 PackageManager.PERMISSION_GRANTED); 92 93 when(mPeopleSpaceWidgetManager.getPreview( 94 eq(SHORTCUT_ID_A), eq(USER_HANDLE_A), eq(PACKAGE_NAME_A), any())) 95 .thenReturn(mRemoteViews); 96 97 mExtras.putString(PeopleProviderUtils.EXTRAS_KEY_SHORTCUT_ID, SHORTCUT_ID_A); 98 mExtras.putString(PeopleProviderUtils.EXTRAS_KEY_PACKAGE_NAME, PACKAGE_NAME_A); 99 mExtras.putParcelable(PeopleProviderUtils.EXTRAS_KEY_USER_HANDLE, USER_HANDLE_A); 100 } 101 102 @Test testPermissionDeniedThrowsSecurityException()103 public void testPermissionDeniedThrowsSecurityException() throws RemoteException { 104 mContext.getTestablePermissions().setPermission( 105 PeopleProviderUtils.GET_PEOPLE_TILE_PREVIEW_PERMISSION, 106 PackageManager.PERMISSION_DENIED); 107 try { 108 Bundle result = mContext.getContentResolver() 109 .call(URI, PeopleProviderUtils.GET_PEOPLE_TILE_PREVIEW_METHOD, null, null); 110 Assert.fail("Call should have failed with SecurityException"); 111 } catch (SecurityException e) { 112 } catch (Exception e) { 113 Assert.fail("Call should have failed with SecurityException"); 114 } 115 } 116 117 @Test testPermissionGrantedNoExtraReturnsNull()118 public void testPermissionGrantedNoExtraReturnsNull() throws RemoteException { 119 try { 120 Bundle result = mContext.getContentResolver() 121 .call(URI, PeopleProviderUtils.GET_PEOPLE_TILE_PREVIEW_METHOD, null, null); 122 Assert.fail("Call should have failed with IllegalArgumentException"); 123 } catch (IllegalArgumentException e) { 124 } catch (Exception e) { 125 Assert.fail("Call should have failed with IllegalArgumentException"); 126 } 127 } 128 129 @Test testPermissionGrantedExtrasReturnsRemoteViews()130 public void testPermissionGrantedExtrasReturnsRemoteViews() throws RemoteException { 131 try { 132 Bundle result = mContext.getContentResolver().call( 133 URI, PeopleProviderUtils.GET_PEOPLE_TILE_PREVIEW_METHOD, null, mExtras); 134 RemoteViews views = result.getParcelable( 135 PeopleProviderUtils.RESPONSE_KEY_REMOTE_VIEWS); 136 assertThat(views).isNotNull(); 137 } catch (Exception e) { 138 Assert.fail("Fail " + e); 139 } 140 } 141 142 @Test testPermissionGrantedNoConversationForShortcutReturnsNull()143 public void testPermissionGrantedNoConversationForShortcutReturnsNull() throws RemoteException { 144 when(mPeopleSpaceWidgetManager.getPreview( 145 eq(SHORTCUT_ID_A), eq(USER_HANDLE_A), eq(PACKAGE_NAME_A), any())) 146 .thenReturn(null); 147 try { 148 Bundle result = mContext.getContentResolver().call( 149 URI, PeopleProviderUtils.GET_PEOPLE_TILE_PREVIEW_METHOD, null, mExtras); 150 assertThat(result).isNull(); 151 } catch (Exception e) { 152 Assert.fail("Fail " + e); 153 } 154 } 155 } 156