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 android.content.pm.ActivityInfo; 19 import android.content.pm.PackageManager; 20 import android.content.pm.ResolveInfo; 21 import android.support.annotation.NonNull; 22 import org.json.JSONException; 23 import org.json.JSONObject; 24 import org.junit.Before; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.mockito.Mock; 28 import org.mockito.MockitoAnnotations; 29 import org.robolectric.annotation.Config; 30 31 import java.io.OutputStream; 32 import java.io.PrintWriter; 33 34 import static com.google.common.truth.Truth.assertThat; 35 import static org.mockito.Mockito.when; 36 37 @RunWith(SettingsRobolectricTestRunner.class) 38 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 39 public class SettingsDumpServiceTest { 40 private static final String PACKAGE_BROWSER = "com.android.test.browser"; 41 private static final String PACKAGE_NULL = "android"; 42 @Mock 43 private PackageManager mPackageManager; 44 @Mock 45 private ResolveInfo mResolveInfo; 46 private TestService mTestService; 47 48 @Before setUp()49 public void setUp() { 50 MockitoAnnotations.initMocks(this); 51 52 when(mPackageManager.resolveActivity(TestService.BROWSER_INTENT, 53 PackageManager.MATCH_DEFAULT_ONLY)).thenReturn(mResolveInfo); 54 mTestService = new TestService(); 55 mTestService.setPackageManager(mPackageManager); 56 } 57 58 @Test testDumpDefaultBrowser_DefaultBrowser_ReturnBrowserName()59 public void testDumpDefaultBrowser_DefaultBrowser_ReturnBrowserName() { 60 mResolveInfo.activityInfo = new ActivityInfo(); 61 mResolveInfo.activityInfo.packageName = PACKAGE_BROWSER; 62 63 assertThat(mTestService.dumpDefaultBrowser()).isEqualTo(PACKAGE_BROWSER); 64 } 65 66 @Test testDumpDefaultBrowser_NoDefault_ReturnNull()67 public void testDumpDefaultBrowser_NoDefault_ReturnNull() { 68 mResolveInfo.activityInfo = new ActivityInfo(); 69 mResolveInfo.activityInfo.packageName = PACKAGE_NULL; 70 71 assertThat(mTestService.dumpDefaultBrowser()).isEqualTo(null); 72 } 73 74 @Test testDump_ReturnJsonObject()75 public void testDump_ReturnJsonObject() throws JSONException { 76 mResolveInfo.activityInfo = new ActivityInfo(); 77 mResolveInfo.activityInfo.packageName = PACKAGE_BROWSER; 78 TestPrintWriter printWriter = new TestPrintWriter(System.out); 79 80 mTestService.dump(null, printWriter, null); 81 JSONObject object = (JSONObject)printWriter.getPrintObject(); 82 83 assertThat(object.get(TestService.KEY_SERVICE)).isNotNull(); 84 } 85 86 /** 87 * Test service used to pass in the mock {@link PackageManager} 88 */ 89 private class TestService extends SettingsDumpService { 90 private PackageManager mPm; 91 setPackageManager(PackageManager pm)92 public void setPackageManager(PackageManager pm) { 93 mPm = pm; 94 } 95 96 @Override getPackageManager()97 public PackageManager getPackageManager() { 98 return mPm; 99 } 100 } 101 102 /** 103 * Test printWriter to store the object to be printed 104 */ 105 private class TestPrintWriter extends PrintWriter { 106 private Object mPrintObject; 107 TestPrintWriter(@onNull OutputStream out)108 public TestPrintWriter(@NonNull OutputStream out) { 109 super(out); 110 } 111 112 @Override println(Object object)113 public void println(Object object) { 114 mPrintObject = object; 115 } 116 getPrintObject()117 public Object getPrintObject() { 118 return mPrintObject; 119 } 120 } 121 } 122