Files
app_Settings/tests/robotests/src/com/android/settings/UtilsTest.java
jackqdyulei 3ee22131a4 Add summary for items in App Usage List in Battery page
This summary shows the active time of the app. For example
"Used for 27m".

Bug: 34467139
Test: RunSettingsRoboTests & Take screenshots
Change-Id: I0ef48427ad21be4dacd290fffb2c5d519ef58506
2017-01-27 14:15:38 -08:00

101 lines
3.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 android.text.format.DateUtils;
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();
}
@Test
public void testFormatElapsedTime_WithSeconds_ShowSeconds() {
final double testMillis = 5 * DateUtils.MINUTE_IN_MILLIS;
final String expectedTime = "5m 0s";
assertThat(Utils.formatElapsedTime(mContext, testMillis, true)).isEqualTo(expectedTime);
}
@Test
public void testFormatElapsedTime_NoSeconds_DoNotShowSeconds() {
final double testMillis = 5 * DateUtils.MINUTE_IN_MILLIS;
final String expectedTime = "5m";
assertThat(Utils.formatElapsedTime(mContext, testMillis, false)).isEqualTo(expectedTime);
}
@Test
public void testFormatElapsedTime_TimeMoreThanOneDay_ShowCorrectly() {
final double testMillis = 2 * DateUtils.DAY_IN_MILLIS
+ 4 * DateUtils.HOUR_IN_MILLIS + 15 * DateUtils.MINUTE_IN_MILLIS;
final String expectedTime = "2d 4h 15m";
assertThat(Utils.formatElapsedTime(mContext, testMillis, false)).isEqualTo(expectedTime);
}
}