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 17package com.android.settings.trustagent; 18 19import android.content.pm.ResolveInfo; 20import android.content.pm.ServiceInfo; 21import android.content.pm.PackageManager; 22 23import com.android.settings.testutils.SettingsRobolectricTestRunner; 24import com.android.settings.TestConfig; 25 26import org.junit.Before; 27import org.junit.Test; 28import org.junit.runner.RunWith; 29import org.mockito.Mock; 30import org.mockito.MockitoAnnotations; 31import org.robolectric.annotation.Config; 32 33import static junit.framework.Assert.assertFalse; 34import static junit.framework.Assert.assertTrue; 35import static org.mockito.Mockito.mock; 36import static org.mockito.Mockito.when; 37 38@RunWith(SettingsRobolectricTestRunner.class) 39@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 40public class TrustAgentFeatureProviderImplTest { 41 42 private static final String CANNED_PACKAGE_NAME = "com.test.package"; 43 44 @Mock 45 private PackageManager mPackageManager; 46 47 private TrustAgentManagerImpl mImpl; 48 49 @Before 50 public void setUp() throws PackageManager.NameNotFoundException { 51 MockitoAnnotations.initMocks(this); 52 mImpl = new TrustAgentManagerImpl(); 53 } 54 55 @Test 56 public void shouldProvideTrust_doesProvideTrustWithPermission() { 57 when(mPackageManager.checkPermission(TrustAgentManager.PERMISSION_PROVIDE_AGENT, 58 CANNED_PACKAGE_NAME)).thenReturn(PackageManager.PERMISSION_GRANTED); 59 60 ServiceInfo serviceInfo = new ServiceInfo(); 61 serviceInfo.packageName = CANNED_PACKAGE_NAME; 62 ResolveInfo resolveInfo = new ResolveInfo(); 63 resolveInfo.serviceInfo = serviceInfo; 64 65 assertTrue(mImpl.shouldProvideTrust(resolveInfo, mPackageManager)); 66 } 67 68 @Test 69 public void shouldProvideTrust_doesNotProvideTrustWithoutPermission() { 70 when(mPackageManager.checkPermission(TrustAgentManager.PERMISSION_PROVIDE_AGENT, 71 CANNED_PACKAGE_NAME)).thenReturn(PackageManager.PERMISSION_DENIED); 72 73 ServiceInfo serviceInfo = new ServiceInfo(); 74 serviceInfo.packageName = CANNED_PACKAGE_NAME; 75 ResolveInfo resolveInfo = new ResolveInfo(); 76 resolveInfo.serviceInfo = serviceInfo; 77 78 assertFalse(mImpl.shouldProvideTrust(resolveInfo, mPackageManager)); 79 } 80} 81