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