• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.settings.fuelgauge.batterytip.AnomalyConfigJobService;
33 
34 import org.json.JSONException;
35 import org.json.JSONObject;
36 import org.junit.Before;
37 import org.junit.Ignore;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.RobolectricTestRunner;
43 import org.robolectric.RuntimeEnvironment;
44 
45 import java.io.PrintWriter;
46 import java.io.StringWriter;
47 
48 @RunWith(RobolectricTestRunner.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
setUp()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
testDumpDefaultBrowser_DefaultBrowser_ReturnBrowserName()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
testDumpDefaultBrowser_NoDefault_ReturnNull()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
testDumpAnomalyDetection_returnAnomalyInfo()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     @Ignore
104     @Test
testDump_printServiceAsKey()105     public void testDump_printServiceAsKey() {
106         mResolveInfo.activityInfo = new ActivityInfo();
107         mResolveInfo.activityInfo.packageName = PACKAGE_BROWSER;
108         StringWriter stringWriter = new StringWriter();
109         PrintWriter printWriter = new PrintWriter(stringWriter);
110 
111         mTestService.dump(null, printWriter, null);
112 
113         assertThat(stringWriter.toString())
114                 .contains("{\"" + SettingsDumpService.KEY_SERVICE + "\":");
115     }
116 
117     /**
118      * Test service used to pass in the mock {@link PackageManager}
119      */
120     private class TestService extends SettingsDumpService {
121         private PackageManager mPm;
122 
setPackageManager(PackageManager pm)123         public void setPackageManager(PackageManager pm) {
124             mPm = pm;
125         }
126 
127         @Override
getPackageManager()128         public PackageManager getPackageManager() {
129             return mPm;
130         }
131     }
132 }
133