Files
packages_apps_Settings/tests/robotests/src/com/android/settings/UtilsTest.java
jackqdyulei 8164c64edc Add NPE check for method assignDefaultPhoto in Utils.java
When user press back button while adding user, app may crash because
of null context. In this cl, I add NPE check for context in method
assignDefaultPhoto and change this method to return boolean value.
If the return value is false, then stop the following steps(e.g.
creating a dialog after pressing button may also cause crash.)

Bug: 33829308
Test: make -j40 RunSettingsRoboTests
Change-Id: I811b92c268fb20f8c43ab3b0a9ceebe76f5d83be
2016-12-28 11:30:54 -08:00

75 lines
2.5 KiB
Java

package com.android.settings;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.Network;
import android.net.wifi.WifiManager;
import java.net.InetAddress;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class UtilsTest {
private Context mContext;
@Mock private WifiManager wifiManager;
@Mock private Network network;
@Mock private ConnectivityManager connectivityManager;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(WifiManager.class)).thenReturn(wifiManager);
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
.thenReturn(connectivityManager);
}
@Test
public void testGetWifiIpAddresses_succeeds() throws Exception {
when(wifiManager.getCurrentNetwork()).thenReturn(network);
LinkAddress address = new LinkAddress(InetAddress.getByName("127.0.0.1"), 0);
LinkProperties lp = new LinkProperties();
lp.addLinkAddress(address);
when(connectivityManager.getLinkProperties(network)).thenReturn(lp);
assertThat(Utils.getWifiIpAddresses(mContext)).isEqualTo("127.0.0.1");
}
@Test
public void testGetWifiIpAddresses_nullLinkProperties() {
when(wifiManager.getCurrentNetwork()).thenReturn(network);
// Explicitly set the return value to null for readability sake.
when(connectivityManager.getLinkProperties(network)).thenReturn(null);
assertThat(Utils.getWifiIpAddresses(mContext)).isNull();
}
@Test
public void testGetWifiIpAddresses_nullNetwork() {
// Explicitly set the return value to null for readability sake.
when(wifiManager.getCurrentNetwork()).thenReturn(null);
assertThat(Utils.getWifiIpAddresses(mContext)).isNull();
}
@Test
public void testAssignDefaultPhoto_ContextNull_ReturnFalseAndNotCrash() {
// Should not crash here
assertThat(Utils.assignDefaultPhoto(null, 0)).isFalse();
}
}