1 /*
2  * Copyright (C) 2018 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 
17 package com.android.settings;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.ArgumentMatchers.anyString;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 
31 import android.app.ActionBar;
32 import android.app.Activity;
33 import android.app.admin.DevicePolicyManager;
34 import android.content.ComponentName;
35 import android.content.Context;
36 import android.content.pm.ApplicationInfo;
37 import android.content.pm.PackageManager;
38 import android.content.pm.UserInfo;
39 import android.graphics.Bitmap;
40 import android.graphics.Color;
41 import android.graphics.drawable.BitmapDrawable;
42 import android.graphics.drawable.ColorDrawable;
43 import android.graphics.drawable.VectorDrawable;
44 import android.net.ConnectivityManager;
45 import android.net.LinkAddress;
46 import android.net.LinkProperties;
47 import android.net.Network;
48 import android.net.wifi.WifiManager;
49 import android.os.Bundle;
50 import android.os.UserManager;
51 import android.os.storage.DiskInfo;
52 import android.os.storage.StorageManager;
53 import android.os.storage.VolumeInfo;
54 import android.util.IconDrawableFactory;
55 import android.widget.EditText;
56 import android.widget.ScrollView;
57 import android.widget.TextView;
58 
59 import androidx.core.graphics.drawable.IconCompat;
60 import androidx.fragment.app.FragmentActivity;
61 
62 import org.junit.Before;
63 import org.junit.Test;
64 import org.junit.runner.RunWith;
65 import org.mockito.Mock;
66 import org.mockito.MockitoAnnotations;
67 import org.robolectric.Robolectric;
68 import org.robolectric.RobolectricTestRunner;
69 import org.robolectric.RuntimeEnvironment;
70 
71 import java.net.InetAddress;
72 import java.util.ArrayList;
73 import java.util.List;
74 
75 @RunWith(RobolectricTestRunner.class)
76 public class UtilsTest {
77 
78     private static final String PACKAGE_NAME = "com.android.app";
79     private static final int USER_ID = 1;
80 
81     @Mock
82     private WifiManager wifiManager;
83     @Mock
84     private Network network;
85     @Mock
86     private ConnectivityManager connectivityManager;
87     @Mock
88     private DevicePolicyManager mDevicePolicyManager;
89     @Mock
90     private UserManager mUserManager;
91     @Mock
92     private PackageManager mPackageManager;
93     @Mock
94     private IconDrawableFactory mIconDrawableFactory;
95     @Mock
96     private ApplicationInfo mApplicationInfo;
97     private Context mContext;
98 
99     @Before
setUp()100     public void setUp() {
101         MockitoAnnotations.initMocks(this);
102 
103         mContext = spy(RuntimeEnvironment.application);
104         when(mContext.getSystemService(WifiManager.class)).thenReturn(wifiManager);
105         when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
106                 .thenReturn(connectivityManager);
107         when(mContext.getPackageManager()).thenReturn(mPackageManager);
108     }
109 
110     @Test
getWifiIpAddresses_succeeds()111     public void getWifiIpAddresses_succeeds() throws Exception {
112         when(wifiManager.getCurrentNetwork()).thenReturn(network);
113         LinkAddress address = new LinkAddress(InetAddress.getByName("127.0.0.1"), 0);
114         LinkProperties lp = new LinkProperties();
115         lp.addLinkAddress(address);
116         when(connectivityManager.getLinkProperties(network)).thenReturn(lp);
117 
118         assertThat(Utils.getWifiIpAddresses(mContext)).isEqualTo("127.0.0.1");
119     }
120 
121     @Test
getWifiIpAddresses_nullLinkProperties()122     public void getWifiIpAddresses_nullLinkProperties() {
123         when(wifiManager.getCurrentNetwork()).thenReturn(network);
124         // Explicitly set the return value to null for readability sake.
125         when(connectivityManager.getLinkProperties(network)).thenReturn(null);
126 
127         assertThat(Utils.getWifiIpAddresses(mContext)).isNull();
128     }
129 
130     @Test
getWifiIpAddresses_nullNetwork()131     public void getWifiIpAddresses_nullNetwork() {
132         // Explicitly set the return value to null for readability sake.
133         when(wifiManager.getCurrentNetwork()).thenReturn(null);
134 
135         assertThat(Utils.getWifiIpAddresses(mContext)).isNull();
136     }
137 
138     @Test
initializeVolumeDoesntBreakOnNullVolume()139     public void initializeVolumeDoesntBreakOnNullVolume() {
140         VolumeInfo info = new VolumeInfo("id", 0, new DiskInfo("id", 0), "");
141         StorageManager storageManager = mock(StorageManager.class, RETURNS_DEEP_STUBS);
142         when(storageManager.findVolumeById(anyString())).thenReturn(info);
143 
144         Utils.maybeInitializeVolume(storageManager, new Bundle());
145     }
146 
147     @Test
getInstallationStatus_notInstalled_shouldReturnUninstalled()148     public void getInstallationStatus_notInstalled_shouldReturnUninstalled() {
149         assertThat(Utils.getInstallationStatus(new ApplicationInfo()))
150                 .isEqualTo(R.string.not_installed);
151     }
152 
153     @Test
getInstallationStatus_enabled_shouldReturnInstalled()154     public void getInstallationStatus_enabled_shouldReturnInstalled() {
155         final ApplicationInfo info = new ApplicationInfo();
156         info.flags = ApplicationInfo.FLAG_INSTALLED;
157         info.enabled = true;
158 
159         assertThat(Utils.getInstallationStatus(info)).isEqualTo(R.string.installed);
160     }
161 
162     @Test
getInstallationStatus_disabled_shouldReturnDisabled()163     public void getInstallationStatus_disabled_shouldReturnDisabled() {
164         final ApplicationInfo info = new ApplicationInfo();
165         info.flags = ApplicationInfo.FLAG_INSTALLED;
166         info.enabled = false;
167 
168         assertThat(Utils.getInstallationStatus(info)).isEqualTo(R.string.disabled);
169     }
170 
171     @Test
isProfileOrDeviceOwner_deviceOwnerApp_returnTrue()172     public void isProfileOrDeviceOwner_deviceOwnerApp_returnTrue() {
173         when(mDevicePolicyManager.isDeviceOwnerAppOnAnyUser(PACKAGE_NAME)).thenReturn(true);
174 
175         assertThat(Utils.isProfileOrDeviceOwner(mUserManager, mDevicePolicyManager, PACKAGE_NAME))
176             .isTrue();
177     }
178 
179     @Test
isProfileOrDeviceOwner_profileOwnerApp_returnTrue()180     public void isProfileOrDeviceOwner_profileOwnerApp_returnTrue() {
181         final List<UserInfo> userInfos = new ArrayList<>();
182         userInfos.add(new UserInfo());
183 
184         when(mUserManager.getUsers()).thenReturn(userInfos);
185         when(mDevicePolicyManager.getProfileOwnerAsUser(userInfos.get(0).id))
186             .thenReturn(new ComponentName(PACKAGE_NAME, ""));
187 
188         assertThat(Utils.isProfileOrDeviceOwner(mUserManager, mDevicePolicyManager, PACKAGE_NAME))
189             .isTrue();
190     }
191 
192     @Test
setEditTextCursorPosition_shouldGetExpectedEditTextLenght()193     public void setEditTextCursorPosition_shouldGetExpectedEditTextLenght() {
194         final EditText editText = new EditText(mContext);
195         final CharSequence text = "test";
196         editText.setText(text, TextView.BufferType.EDITABLE);
197         final int length = editText.getText().length();
198         Utils.setEditTextCursorPosition(editText);
199 
200         assertThat(editText.getSelectionEnd()).isEqualTo(length);
201     }
202 
203     @Test
createIconWithDrawable_BitmapDrawable()204     public void createIconWithDrawable_BitmapDrawable() {
205         final Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
206         final BitmapDrawable drawable = new BitmapDrawable(mContext.getResources(), bitmap);
207 
208         final IconCompat icon = Utils.createIconWithDrawable(drawable);
209 
210         assertThat(icon.getBitmap()).isNotNull();
211     }
212 
213     @Test
createIconWithDrawable_ColorDrawable()214     public void createIconWithDrawable_ColorDrawable() {
215         final ColorDrawable drawable = new ColorDrawable(Color.BLACK);
216 
217         final IconCompat icon = Utils.createIconWithDrawable(drawable);
218 
219         assertThat(icon.getBitmap()).isNotNull();
220     }
221 
222     @Test
createIconWithDrawable_VectorDrawable()223     public void createIconWithDrawable_VectorDrawable() {
224         final VectorDrawable drawable = VectorDrawable.create(mContext.getResources(),
225                 R.drawable.ic_settings_accent);
226 
227         final IconCompat icon = Utils.createIconWithDrawable(drawable);
228 
229         assertThat(icon.getBitmap()).isNotNull();
230     }
231 
232     @Test
getBadgedIcon_usePackageNameAndUserId()233     public void getBadgedIcon_usePackageNameAndUserId()
234         throws PackageManager.NameNotFoundException {
235         doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfoAsUser(
236                 PACKAGE_NAME, PackageManager.GET_META_DATA, USER_ID);
237 
238         Utils.getBadgedIcon(mIconDrawableFactory, mPackageManager, PACKAGE_NAME, USER_ID);
239 
240         // Verify that it uses the correct user id
241         verify(mPackageManager).getApplicationInfoAsUser(eq(PACKAGE_NAME), anyInt(), eq(USER_ID));
242         verify(mIconDrawableFactory).getBadgedIcon(mApplicationInfo, USER_ID);
243     }
244 
245     @Test
isPackageEnabled_appEnabled_returnTrue()246     public void isPackageEnabled_appEnabled_returnTrue()
247             throws PackageManager.NameNotFoundException{
248         mApplicationInfo.enabled = true;
249         when(mPackageManager.getApplicationInfo(PACKAGE_NAME, 0)).thenReturn(mApplicationInfo);
250 
251         assertThat(Utils.isPackageEnabled(mContext, PACKAGE_NAME)).isTrue();
252     }
253 
254     @Test
isPackageEnabled_appDisabled_returnTrue()255     public void isPackageEnabled_appDisabled_returnTrue()
256             throws PackageManager.NameNotFoundException{
257         mApplicationInfo.enabled = false;
258         when(mPackageManager.getApplicationInfo(PACKAGE_NAME, 0)).thenReturn(mApplicationInfo);
259 
260         assertThat(Utils.isPackageEnabled(mContext, PACKAGE_NAME)).isFalse();
261     }
262 
263     @Test
isPackageEnabled_noApp_returnFalse()264     public void isPackageEnabled_noApp_returnFalse() {
265         assertThat(Utils.isPackageEnabled(mContext, PACKAGE_NAME)).isFalse();
266     }
267 
268     @Test
setActionBarShadowAnimation_nullParameters_shouldNotCrash()269     public void setActionBarShadowAnimation_nullParameters_shouldNotCrash() {
270         // no crash here
271         Utils.setActionBarShadowAnimation(null, null, null);
272     }
273 
274     @Test
setActionBarShadowAnimation_shouldSetElevationToZero()275     public void setActionBarShadowAnimation_shouldSetElevationToZero() {
276         final FragmentActivity activity = Robolectric.setupActivity(FragmentActivity.class);
277         final ActionBar actionBar = activity.getActionBar();
278 
279         Utils.setActionBarShadowAnimation(activity, activity.getLifecycle(),
280                 new ScrollView(mContext));
281 
282         assertThat(actionBar.getElevation()).isEqualTo(0.f);
283     }
284 }
285