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.Matchers.anyInt; 21 import static org.mockito.Matchers.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 import android.support.annotation.NonNull; 32 33 import com.android.settings.fuelgauge.batterytip.AnomalyConfigJobService; 34 import com.android.settings.testutils.SettingsRobolectricTestRunner; 35 36 import org.json.JSONException; 37 import org.json.JSONObject; 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 import org.robolectric.RuntimeEnvironment; 44 45 import java.io.OutputStream; 46 import java.io.PrintWriter; 47 48 @RunWith(SettingsRobolectricTestRunner.class) 49 public class SettingsDumpServiceTest { 50 51 private static final String PACKAGE_BROWSER = "com.android.test.browser"; 52 private static final String PACKAGE_NULL = "android"; 53 private static final int ANOMALY_VERSION = 2; 54 55 @Mock 56 private PackageManager mPackageManager; 57 @Mock 58 private ResolveInfo mResolveInfo; 59 private TestService mTestService; 60 61 @Before 62 public void setUp() { 63 MockitoAnnotations.initMocks(this); 64 65 when(mPackageManager.resolveActivity(TestService.BROWSER_INTENT, 66 PackageManager.MATCH_DEFAULT_ONLY)).thenReturn(mResolveInfo); 67 mTestService = spy(new TestService()); 68 mTestService.setPackageManager(mPackageManager); 69 } 70 71 @Test 72 public void testDumpDefaultBrowser_DefaultBrowser_ReturnBrowserName() { 73 mResolveInfo.activityInfo = new ActivityInfo(); 74 mResolveInfo.activityInfo.packageName = PACKAGE_BROWSER; 75 76 assertThat(mTestService.dumpDefaultBrowser()).isEqualTo(PACKAGE_BROWSER); 77 } 78 79 @Test 80 public void testDumpDefaultBrowser_NoDefault_ReturnNull() { 81 mResolveInfo.activityInfo = new ActivityInfo(); 82 mResolveInfo.activityInfo.packageName = PACKAGE_NULL; 83 84 assertThat(mTestService.dumpDefaultBrowser()).isEqualTo(null); 85 } 86 87 @Test 88 public void testDumpAnomalyDetection_returnAnomalyInfo() throws JSONException { 89 final SharedPreferences sharedPreferences = 90 RuntimeEnvironment.application.getSharedPreferences(AnomalyConfigJobService.PREF_DB, 91 Context.MODE_PRIVATE); 92 SharedPreferences.Editor editor = sharedPreferences.edit(); 93 editor.putInt(AnomalyConfigJobService.KEY_ANOMALY_CONFIG_VERSION, ANOMALY_VERSION); 94 editor.commit(); 95 doReturn(sharedPreferences).when(mTestService).getSharedPreferences(anyString(), anyInt()); 96 97 final JSONObject jsonObject = mTestService.dumpAnomalyDetection(); 98 99 assertThat(jsonObject.getInt(AnomalyConfigJobService.KEY_ANOMALY_CONFIG_VERSION)).isEqualTo( 100 ANOMALY_VERSION); 101 } 102 103 @Test 104 public void testDump_ReturnJsonObject() throws JSONException { 105 mResolveInfo.activityInfo = new ActivityInfo(); 106 mResolveInfo.activityInfo.packageName = PACKAGE_BROWSER; 107 TestPrintWriter printWriter = new TestPrintWriter(System.out); 108 109 mTestService.dump(null, printWriter, null); 110 JSONObject object = (JSONObject) printWriter.getPrintObject(); 111 112 assertThat(object.get(TestService.KEY_SERVICE)).isNotNull(); 113 } 114 115 /** 116 * Test service used to pass in the mock {@link PackageManager} 117 */ 118 private class TestService extends SettingsDumpService { 119 private PackageManager mPm; 120 121 public void setPackageManager(PackageManager pm) { 122 mPm = pm; 123 } 124 125 @Override 126 public PackageManager getPackageManager() { 127 return mPm; 128 } 129 } 130 131 /** 132 * Test printWriter to store the object to be printed 133 */ 134 private class TestPrintWriter extends PrintWriter { 135 private Object mPrintObject; 136 137 private TestPrintWriter(@NonNull OutputStream out) { 138 super(out); 139 } 140 141 @Override 142 public void println(Object object) { 143 mPrintObject = object; 144 } 145 146 private Object getPrintObject() { 147 return mPrintObject; 148 } 149 } 150 } 151