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; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.ArgumentMatchers.anyInt; 21 import static org.mockito.ArgumentMatchers.anyString; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.spy; 24 import static org.mockito.Mockito.when; 25 26 import android.content.Context; 27 import android.content.SharedPreferences; 28 import android.content.pm.ActivityInfo; 29 import android.content.pm.PackageManager; 30 import android.content.pm.ResolveInfo; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 import org.robolectric.RobolectricTestRunner; 38 import org.robolectric.RuntimeEnvironment; 39 40 import java.io.PrintWriter; 41 import java.io.StringWriter; 42 43 @RunWith(RobolectricTestRunner.class) 44 public class SettingsDumpServiceTest { 45 46 private static final String PACKAGE_BROWSER = "com.android.test.browser"; 47 private static final String PACKAGE_NULL = "android"; 48 private static final int ANOMALY_VERSION = 2; 49 50 @Mock 51 private PackageManager mPackageManager; 52 @Mock 53 private ResolveInfo mResolveInfo; 54 private TestService mTestService; 55 56 @Before setUp()57 public void setUp() { 58 MockitoAnnotations.initMocks(this); 59 60 when(mPackageManager.resolveActivity(TestService.BROWSER_INTENT, 61 PackageManager.MATCH_DEFAULT_ONLY)).thenReturn(mResolveInfo); 62 mTestService = spy(new TestService()); 63 mTestService.setPackageManager(mPackageManager); 64 } 65 66 @Test testDumpDefaultBrowser_DefaultBrowser_ReturnBrowserName()67 public void testDumpDefaultBrowser_DefaultBrowser_ReturnBrowserName() { 68 mResolveInfo.activityInfo = new ActivityInfo(); 69 mResolveInfo.activityInfo.packageName = PACKAGE_BROWSER; 70 71 assertThat(mTestService.dumpDefaultBrowser()).isEqualTo(PACKAGE_BROWSER); 72 } 73 74 @Test testDumpDefaultBrowser_NoDefault_ReturnNull()75 public void testDumpDefaultBrowser_NoDefault_ReturnNull() { 76 mResolveInfo.activityInfo = new ActivityInfo(); 77 mResolveInfo.activityInfo.packageName = PACKAGE_NULL; 78 79 assertThat(mTestService.dumpDefaultBrowser()).isEqualTo(null); 80 } 81 82 @Test testDump_printServiceAsKey()83 public void testDump_printServiceAsKey() { 84 mResolveInfo.activityInfo = new ActivityInfo(); 85 mResolveInfo.activityInfo.packageName = PACKAGE_BROWSER; 86 StringWriter stringWriter = new StringWriter(); 87 PrintWriter printWriter = new PrintWriter(stringWriter); 88 89 mTestService.dump(null, printWriter, null); 90 91 assertThat(stringWriter.toString()) 92 .contains("{\"" + SettingsDumpService.KEY_SERVICE + "\":"); 93 } 94 95 /** 96 * Test service used to pass in the mock {@link PackageManager} 97 */ 98 private class TestService extends SettingsDumpService { 99 private PackageManager mPm; 100 setPackageManager(PackageManager pm)101 public void setPackageManager(PackageManager pm) { 102 mPm = pm; 103 } 104 105 @Override getPackageManager()106 public PackageManager getPackageManager() { 107 return mPm; 108 } 109 } 110 } 111