• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import static org.mockito.ArgumentMatchers.anyInt;
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Matchers.anyString;
23 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 
30 import android.app.admin.DevicePolicyManager;
31 import android.content.ComponentName;
32 import android.content.Context;
33 import android.content.pm.ApplicationInfo;
34 import android.content.pm.PackageManager;
35 import android.content.pm.UserInfo;
36 import android.net.ConnectivityManager;
37 import android.net.LinkAddress;
38 import android.net.LinkProperties;
39 import android.net.Network;
40 import android.net.wifi.WifiManager;
41 import android.os.Bundle;
42 import android.os.UserManager;
43 import android.os.storage.DiskInfo;
44 import android.os.storage.StorageManager;
45 import android.os.storage.VolumeInfo;
46 import android.util.IconDrawableFactory;
47 import android.widget.EditText;
48 import android.widget.TextView;
49 
50 import com.android.settings.testutils.SettingsRobolectricTestRunner;
51 
52 import org.junit.Before;
53 import org.junit.Test;
54 import org.junit.runner.RunWith;
55 import org.mockito.Mock;
56 import org.mockito.MockitoAnnotations;
57 import org.robolectric.RuntimeEnvironment;
58 
59 import java.net.InetAddress;
60 import java.util.ArrayList;
61 import java.util.List;
62 
63 @RunWith(SettingsRobolectricTestRunner.class)
64 public class UtilsTest {
65 
66     private static final String PACKAGE_NAME = "com.android.app";
67     private static final int USER_ID = 1;
68 
69     @Mock
70     private WifiManager wifiManager;
71     @Mock
72     private Network network;
73     @Mock
74     private ConnectivityManager connectivityManager;
75     @Mock
76     private DevicePolicyManager mDevicePolicyManager;
77     @Mock
78     private UserManager mUserManager;
79     @Mock
80     private PackageManager mPackageManager;
81     @Mock
82     private IconDrawableFactory mIconDrawableFactory;
83     @Mock
84     private ApplicationInfo mApplicationInfo;
85     private Context mContext;
86 
87     @Before
setUp()88     public void setUp() {
89         MockitoAnnotations.initMocks(this);
90 
91         mContext = spy(RuntimeEnvironment.application);
92         when(mContext.getSystemService(WifiManager.class)).thenReturn(wifiManager);
93         when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
94                 .thenReturn(connectivityManager);
95     }
96 
97     @Test
testGetWifiIpAddresses_succeeds()98     public void testGetWifiIpAddresses_succeeds() throws Exception {
99         when(wifiManager.getCurrentNetwork()).thenReturn(network);
100         LinkAddress address = new LinkAddress(InetAddress.getByName("127.0.0.1"), 0);
101         LinkProperties lp = new LinkProperties();
102         lp.addLinkAddress(address);
103         when(connectivityManager.getLinkProperties(network)).thenReturn(lp);
104 
105         assertThat(Utils.getWifiIpAddresses(mContext)).isEqualTo("127.0.0.1");
106     }
107 
108     @Test
testGetWifiIpAddresses_nullLinkProperties()109     public void testGetWifiIpAddresses_nullLinkProperties() {
110         when(wifiManager.getCurrentNetwork()).thenReturn(network);
111         // Explicitly set the return value to null for readability sake.
112         when(connectivityManager.getLinkProperties(network)).thenReturn(null);
113 
114         assertThat(Utils.getWifiIpAddresses(mContext)).isNull();
115     }
116 
117     @Test
testGetWifiIpAddresses_nullNetwork()118     public void testGetWifiIpAddresses_nullNetwork() {
119         // Explicitly set the return value to null for readability sake.
120         when(wifiManager.getCurrentNetwork()).thenReturn(null);
121 
122         assertThat(Utils.getWifiIpAddresses(mContext)).isNull();
123     }
124 
125     @Test
testInitializeVolumeDoesntBreakOnNullVolume()126     public void testInitializeVolumeDoesntBreakOnNullVolume() {
127         VolumeInfo info = new VolumeInfo("id", 0, new DiskInfo("id", 0), "");
128         StorageManager storageManager = mock(StorageManager.class, RETURNS_DEEP_STUBS);
129         when(storageManager.findVolumeById(anyString())).thenReturn(info);
130 
131         Utils.maybeInitializeVolume(storageManager, new Bundle());
132     }
133 
134     @Test
testGetInstallationStatus_notInstalled_shouldReturnUninstalled()135     public void testGetInstallationStatus_notInstalled_shouldReturnUninstalled() {
136         assertThat(Utils.getInstallationStatus(new ApplicationInfo()))
137                 .isEqualTo(R.string.not_installed);
138     }
139 
140     @Test
testGetInstallationStatus_enabled_shouldReturnInstalled()141     public void testGetInstallationStatus_enabled_shouldReturnInstalled() {
142         final ApplicationInfo info = new ApplicationInfo();
143         info.flags = ApplicationInfo.FLAG_INSTALLED;
144         info.enabled = true;
145 
146         assertThat(Utils.getInstallationStatus(info)).isEqualTo(R.string.installed);
147     }
148 
149     @Test
testGetInstallationStatus_disabled_shouldReturnDisabled()150     public void testGetInstallationStatus_disabled_shouldReturnDisabled() {
151         final ApplicationInfo info = new ApplicationInfo();
152         info.flags = ApplicationInfo.FLAG_INSTALLED;
153         info.enabled = false;
154 
155         assertThat(Utils.getInstallationStatus(info)).isEqualTo(R.string.disabled);
156     }
157 
158     @Test
testIsProfileOrDeviceOwner_deviceOwnerApp_returnTrue()159     public void testIsProfileOrDeviceOwner_deviceOwnerApp_returnTrue() {
160         when(mDevicePolicyManager.isDeviceOwnerAppOnAnyUser(PACKAGE_NAME)).thenReturn(true);
161 
162         assertThat(Utils.isProfileOrDeviceOwner(mUserManager, mDevicePolicyManager, PACKAGE_NAME))
163             .isTrue();
164     }
165 
166     @Test
testIsProfileOrDeviceOwner_profileOwnerApp_returnTrue()167     public void testIsProfileOrDeviceOwner_profileOwnerApp_returnTrue() {
168         final List<UserInfo> userInfos = new ArrayList<>();
169         userInfos.add(new UserInfo());
170 
171         when(mUserManager.getUsers()).thenReturn(userInfos);
172         when(mDevicePolicyManager.getProfileOwnerAsUser(userInfos.get(0).id))
173             .thenReturn(new ComponentName(PACKAGE_NAME, ""));
174 
175         assertThat(Utils.isProfileOrDeviceOwner(mUserManager, mDevicePolicyManager, PACKAGE_NAME))
176             .isTrue();
177     }
178 
179     @Test
testSetEditTextCursorPosition_shouldGetExpectedEditTextLenght()180     public void testSetEditTextCursorPosition_shouldGetExpectedEditTextLenght() {
181         final EditText editText = new EditText(mContext);
182         final CharSequence text = "test";
183         editText.setText(text, TextView.BufferType.EDITABLE);
184         final int length = editText.getText().length();
185         Utils.setEditTextCursorPosition(editText);
186 
187         assertThat(editText.getSelectionEnd()).isEqualTo(length);
188     }
189 
190     @Test
testGetBadgedIcon_usePackageNameAndUserId()191     public void testGetBadgedIcon_usePackageNameAndUserId()
192         throws PackageManager.NameNotFoundException {
193         doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfoAsUser(
194                 PACKAGE_NAME, PackageManager.GET_META_DATA, USER_ID);
195 
196         Utils.getBadgedIcon(mIconDrawableFactory, mPackageManager, PACKAGE_NAME, USER_ID);
197 
198         // Verify that it uses the correct user id
199         verify(mPackageManager).getApplicationInfoAsUser(eq(PACKAGE_NAME), anyInt(), eq(USER_ID));
200         verify(mIconDrawableFactory).getBadgedIcon(mApplicationInfo, USER_ID);
201     }
202 }
203