Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit aebf3bc2 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Fixed NetworkStatsServiceTest and converted it to JUnit4."

parents 4d657f1e 574f370c
Loading
Loading
Loading
Loading
+38 −20
Original line number Original line Diff line number Diff line
@@ -40,7 +40,9 @@ import static android.text.format.DateUtils.DAY_IN_MILLIS;
import static android.text.format.DateUtils.HOUR_IN_MILLIS;
import static android.text.format.DateUtils.HOUR_IN_MILLIS;
import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
import static android.text.format.DateUtils.WEEK_IN_MILLIS;
import static android.text.format.DateUtils.WEEK_IN_MILLIS;

import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL;
import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL;

import static org.easymock.EasyMock.anyInt;
import static org.easymock.EasyMock.anyInt;
import static org.easymock.EasyMock.anyLong;
import static org.easymock.EasyMock.anyLong;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.anyObject;
@@ -49,12 +51,12 @@ import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.isA;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


import android.app.AlarmManager;
import android.app.AlarmManager;
import android.app.IAlarmListener;
import android.app.IAlarmManager;
import android.app.PendingIntent;
import android.app.usage.NetworkStatsManager;
import android.app.usage.NetworkStatsManager;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.Intent;
@@ -63,6 +65,7 @@ import android.net.IConnectivityManager;
import android.net.INetworkManagementEventObserver;
import android.net.INetworkManagementEventObserver;
import android.net.INetworkStatsSession;
import android.net.INetworkStatsSession;
import android.net.LinkProperties;
import android.net.LinkProperties;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState;
import android.net.NetworkInfo.DetailedState;
import android.net.NetworkState;
import android.net.NetworkState;
@@ -80,11 +83,10 @@ import android.os.MessageQueue;
import android.os.MessageQueue.IdleHandler;
import android.os.MessageQueue.IdleHandler;
import android.os.Message;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager;
import android.os.WorkSource;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.telephony.TelephonyManager;
import android.telephony.TelephonyManager;
import android.test.AndroidTestCase;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.Suppress;
import android.util.TrustedTime;
import android.util.TrustedTime;


import com.android.internal.net.VpnInfo;
import com.android.internal.net.VpnInfo;
@@ -97,6 +99,10 @@ import libcore.io.IoUtils;


import org.easymock.Capture;
import org.easymock.Capture;
import org.easymock.EasyMock;
import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;


import java.io.File;
import java.io.File;
import java.util.ArrayList;
import java.util.ArrayList;
@@ -109,8 +115,8 @@ import java.util.List;
 * TODO: This test is really brittle, largely due to overly-strict use of Easymock.
 * TODO: This test is really brittle, largely due to overly-strict use of Easymock.
 * Rewrite w/ Mockito.
 * Rewrite w/ Mockito.
 */
 */
@LargeTest
@RunWith(AndroidJUnit4.class)
public class NetworkStatsServiceTest extends AndroidTestCase {
public class NetworkStatsServiceTest {
    private static final String TAG = "NetworkStatsServiceTest";
    private static final String TAG = "NetworkStatsServiceTest";


    private static final String TEST_IFACE = "test0";
    private static final String TEST_IFACE = "test0";
@@ -148,12 +154,12 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
    private INetworkStatsSession mSession;
    private INetworkStatsSession mSession;
    private INetworkManagementEventObserver mNetworkObserver;
    private INetworkManagementEventObserver mNetworkObserver;


    @Override
    @Before
    public void setUp() throws Exception {
    public void setUp() throws Exception {
        super.setUp();
        final Context context = InstrumentationRegistry.getContext();


        mServiceContext = new BroadcastInterceptingContext(getContext());
        mServiceContext = new BroadcastInterceptingContext(context);
        mStatsDir = getContext().getFilesDir();
        mStatsDir = context.getFilesDir();
        if (mStatsDir.exists()) {
        if (mStatsDir.exists()) {
            IoUtils.deleteContents(mStatsDir);
            IoUtils.deleteContents(mStatsDir);
        }
        }
@@ -205,7 +211,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {


    }
    }


    @Override
    @After
    public void tearDown() throws Exception {
    public void tearDown() throws Exception {
        IoUtils.deleteContents(mStatsDir);
        IoUtils.deleteContents(mStatsDir);


@@ -219,10 +225,9 @@ public class NetworkStatsServiceTest extends AndroidTestCase {


        mSession.close();
        mSession.close();
        mService = null;
        mService = null;

        super.tearDown();
    }
    }


    @Test
    public void testNetworkStatsWifi() throws Exception {
    public void testNetworkStatsWifi() throws Exception {
        // pretend that wifi network comes online; service should ask about full
        // pretend that wifi network comes online; service should ask about full
        // network state, and poll any existing interfaces before updating.
        // network state, and poll any existing interfaces before updating.
@@ -276,6 +281,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {


    }
    }


    @Test
    public void testStatsRebootPersist() throws Exception {
    public void testStatsRebootPersist() throws Exception {
        assertStatsFilesExist(false);
        assertStatsFilesExist(false);


@@ -366,7 +372,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
    }
    }


    // TODO: simulate reboot to test bucket resize
    // TODO: simulate reboot to test bucket resize
    @Suppress
    // @Test
    public void testStatsBucketResize() throws Exception {
    public void testStatsBucketResize() throws Exception {
        NetworkStatsHistory history = null;
        NetworkStatsHistory history = null;


@@ -425,6 +431,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {


    }
    }


    @Test
    public void testUidStatsAcrossNetworks() throws Exception {
    public void testUidStatsAcrossNetworks() throws Exception {
        // pretend first mobile network comes online
        // pretend first mobile network comes online
        expectCurrentTime();
        expectCurrentTime();
@@ -515,6 +522,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {


    }
    }


    @Test
    public void testUidRemovedIsMoved() throws Exception {
    public void testUidRemovedIsMoved() throws Exception {
        // pretend that network comes online
        // pretend that network comes online
        expectCurrentTime();
        expectCurrentTime();
@@ -585,6 +593,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {


    }
    }


    @Test
    public void testUid3g4gCombinedByTemplate() throws Exception {
    public void testUid3g4gCombinedByTemplate() throws Exception {
        // pretend that network comes online
        // pretend that network comes online
        expectCurrentTime();
        expectCurrentTime();
@@ -658,6 +667,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
        verifyAndReset();
        verifyAndReset();
    }
    }


    @Test
    public void testSummaryForAllUid() throws Exception {
    public void testSummaryForAllUid() throws Exception {
        // pretend that network comes online
        // pretend that network comes online
        expectCurrentTime();
        expectCurrentTime();
@@ -729,6 +739,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
        verifyAndReset();
        verifyAndReset();
    }
    }


    @Test
    public void testForegroundBackground() throws Exception {
    public void testForegroundBackground() throws Exception {
        // pretend that network comes online
        // pretend that network comes online
        expectCurrentTime();
        expectCurrentTime();
@@ -799,6 +810,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
        verifyAndReset();
        verifyAndReset();
    }
    }


    @Test
    public void testRoaming() throws Exception {
    public void testRoaming() throws Exception {
        // pretend that network comes online
        // pretend that network comes online
        expectCurrentTime();
        expectCurrentTime();
@@ -846,6 +858,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
        verifyAndReset();
        verifyAndReset();
    }
    }


    @Test
    public void testTethering() throws Exception {
    public void testTethering() throws Exception {
        // pretend first mobile network comes online
        // pretend first mobile network comes online
        expectCurrentTime();
        expectCurrentTime();
@@ -887,6 +900,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {


    }
    }


    @Test
    public void testRegisterUsageCallback() throws Exception {
    public void testRegisterUsageCallback() throws Exception {
        // pretend that wifi network comes online; service should ask about full
        // pretend that wifi network comes online; service should ask about full
        // network state, and poll any existing interfaces before updating.
        // network state, and poll any existing interfaces before updating.
@@ -1005,6 +1019,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
        EasyMock.verify(mockBinder);
        EasyMock.verify(mockBinder);
    }
    }


    @Test
    public void testUnregisterUsageCallback_unknown_noop() throws Exception {
    public void testUnregisterUsageCallback_unknown_noop() throws Exception {
        String callingPackage = "the.calling.package";
        String callingPackage = "the.calling.package";
        long thresholdInBytes = 10 * 1024 * 1024;  // 10 MB
        long thresholdInBytes = 10 * 1024 * 1024;  // 10 MB
@@ -1204,7 +1219,8 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
        info.setDetailedState(DetailedState.CONNECTED, null, null);
        info.setDetailedState(DetailedState.CONNECTED, null, null);
        final LinkProperties prop = new LinkProperties();
        final LinkProperties prop = new LinkProperties();
        prop.setInterfaceName(TEST_IFACE);
        prop.setInterfaceName(TEST_IFACE);
        return new NetworkState(info, prop, null, null, null, TEST_SSID);
        final NetworkCapabilities capabilities = new NetworkCapabilities();
        return new NetworkState(info, prop, capabilities, null, null, TEST_SSID);
    }
    }


    private static NetworkState buildMobile3gState(String subscriberId) {
    private static NetworkState buildMobile3gState(String subscriberId) {
@@ -1218,7 +1234,8 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
        info.setRoaming(isRoaming);
        info.setRoaming(isRoaming);
        final LinkProperties prop = new LinkProperties();
        final LinkProperties prop = new LinkProperties();
        prop.setInterfaceName(TEST_IFACE);
        prop.setInterfaceName(TEST_IFACE);
        return new NetworkState(info, prop, null, null, subscriberId, null);
        final NetworkCapabilities capabilities = new NetworkCapabilities();
        return new NetworkState(info, prop, capabilities, null, subscriberId, null);
    }
    }


    private static NetworkState buildMobile4gState(String iface) {
    private static NetworkState buildMobile4gState(String iface) {
@@ -1226,7 +1243,8 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
        info.setDetailedState(DetailedState.CONNECTED, null, null);
        info.setDetailedState(DetailedState.CONNECTED, null, null);
        final LinkProperties prop = new LinkProperties();
        final LinkProperties prop = new LinkProperties();
        prop.setInterfaceName(iface);
        prop.setInterfaceName(iface);
        return new NetworkState(info, prop, null, null, null, null);
        final NetworkCapabilities capabilities = new NetworkCapabilities();
        return new NetworkState(info, prop, capabilities, null, null, null);
    }
    }


    private NetworkStats buildEmptyStats() {
    private NetworkStats buildEmptyStats() {