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

Commit 73f8eda9 authored by Matthew Fritze's avatar Matthew Fritze Committed by Android (Google) Code Review
Browse files

Merge "Revert "Linked google truth to Robolectric tests and added an example.""

parents ba154656 7a781073
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -8,8 +8,7 @@ LOCAL_SRC_FILES := $(call all-java-files-under, src)

# Include the testing libraries (JUnit4 + Robolectric libs).
LOCAL_STATIC_JAVA_LIBRARIES := \
    platform-system-robolectric \
    truth-prebuilt
    platform-system-robolectric

LOCAL_JAVA_LIBRARIES := \
    junit4-target \
+49 −47
Original line number Diff line number Diff line
@@ -3,24 +3,23 @@ package com.android.settings.datausage;
import android.net.NetworkPolicy;
import android.net.NetworkTemplate;
import com.android.settings.TestConfig;
import com.android.settingslib.net.DataUsageController.DataUsageInfo;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import static com.google.common.truth.Truth.assertThat;
import static com.android.settingslib.net.DataUsageController.*;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import static org.junit.Assert.assertEquals;

@RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DataUsageInfoControllerTest {
    private DataUsageInfoController mInfoController;
    private  DataUsageInfo info;
    private static final int NEGATIVE = -1;
    private static final int ZERO = 0;
    private static final int POSITIVE_SMALL = 1;
    private static final int POSITIVE_LARGE = 5;

    @Before
    public void setUp()  {
@@ -28,53 +27,52 @@ public class DataUsageInfoControllerTest {
        info = new DataUsageInfo();
    }


    @Test
    public void testLowUsageLowWarning_LimitUsed() {
        info.warningLevel = POSITIVE_SMALL;
        info.limitLevel = POSITIVE_LARGE;
        info.usageLevel = POSITIVE_SMALL;
        assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.limitLevel);
        info.warningLevel = 5;
        info.limitLevel = 10;
        info.usageLevel = 5;
        assertEquals(mInfoController.getSummaryLimit(info), info.limitLevel);
    }

    @Test
    public void testLowUsageEqualWarning_LimitUsed() {
        info.warningLevel = POSITIVE_LARGE;
        info.limitLevel = POSITIVE_LARGE;
        info.usageLevel = POSITIVE_SMALL;
        assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.limitLevel);
        info.warningLevel = 10;
        info.limitLevel = 10;
        info.usageLevel = 5;
        assertEquals(mInfoController.getSummaryLimit(info), info.limitLevel);
    }

    @Test
    public void testNoLimitNoUsage_WarningUsed() {
        info.warningLevel = POSITIVE_LARGE;
        info.limitLevel = ZERO;
        info.usageLevel = ZERO;
        assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.warningLevel);
        info.warningLevel = 10;
        info.limitLevel = 0;
        info.usageLevel = 0;
        assertEquals(mInfoController.getSummaryLimit(info), info.warningLevel);
    }

    @Test
    public void testNoLimitLowUsage_WarningUsed() {
        info.warningLevel = POSITIVE_LARGE;
        info.limitLevel = ZERO;
        info.usageLevel = POSITIVE_SMALL;
        assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.warningLevel);
        info.warningLevel = 10;
        info.limitLevel = 0;
        info.usageLevel = 5;
        assertEquals(mInfoController.getSummaryLimit(info), info.warningLevel);
    }

    @Test
    public void testLowWarningNoLimit_UsageUsed() {
        info.warningLevel = POSITIVE_SMALL;
        info.limitLevel = ZERO;
        info.usageLevel = POSITIVE_LARGE;
        assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.usageLevel);
        info.warningLevel = 5;
        info.limitLevel = 0;
        info.usageLevel = 10;
        assertEquals(mInfoController.getSummaryLimit(info), info.usageLevel);
    }

    @Test
    public void testLowWarningLowLimit_UsageUsed() {
        info.warningLevel = POSITIVE_SMALL;
        info.limitLevel = POSITIVE_SMALL;
        info.usageLevel = POSITIVE_LARGE;
        assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.usageLevel);
        info.warningLevel = 5;
        info.limitLevel = 5;
        info.usageLevel = 10;
        assertEquals(mInfoController.getSummaryLimit(info), info.usageLevel);
    }

    private NetworkPolicy getDefaultNetworkPolicy() {
@@ -89,56 +87,60 @@ public class DataUsageInfoControllerTest {

    @Test
    public void testNullArguments_NoError() {
        try {
            mInfoController.updateDataLimit(null, null);
            mInfoController.updateDataLimit(info, null);
            mInfoController.updateDataLimit(null, getDefaultNetworkPolicy());
        } catch (Exception e) {
            fail("Update Data Limit should drop calls with null arguments");
        }
    }

    @Test
    public void testNegativeWarning_UpdatedToZero() {
        NetworkPolicy policy = getDefaultNetworkPolicy();
        policy.warningBytes = NEGATIVE;
        policy.warningBytes = -5;
        mInfoController.updateDataLimit(info, policy);
        assertThat(info.warningLevel).isEqualTo(ZERO);
        Assert.assertEquals(0, info.warningLevel);
    }

    @Test
    public void testWarningZero_UpdatedToZero() {
        NetworkPolicy policy = getDefaultNetworkPolicy();
        policy.warningBytes = ZERO;
        policy.warningBytes = 0;
        mInfoController.updateDataLimit(info, policy);
        assertThat(info.warningLevel).isEqualTo(ZERO);
        Assert.assertEquals(0, info.warningLevel);
    }

    @Test
    public void testWarningPositive_UpdatedToWarning() {
        NetworkPolicy policy = getDefaultNetworkPolicy();
        policy.warningBytes = POSITIVE_SMALL;
        policy.warningBytes = 5;
        mInfoController.updateDataLimit(info, policy);
        assertThat(info.warningLevel).isEqualTo(policy.warningBytes);
        Assert.assertEquals(policy.warningBytes, info.warningLevel);
    }

    @Test
    public void testLimitNegative_UpdatedToZero() {
        NetworkPolicy policy = getDefaultNetworkPolicy();
        policy.limitBytes = NEGATIVE;
        policy.limitBytes = -5;
        mInfoController.updateDataLimit(info, policy);
        assertThat(info.limitLevel).isEqualTo(ZERO);
        Assert.assertEquals(0, info.limitLevel);
    }

    @Test
    public void testLimitZero_UpdatedToZero() {
        NetworkPolicy policy = getDefaultNetworkPolicy();
        policy.limitBytes = ZERO;
        policy.limitBytes = 0;
        mInfoController.updateDataLimit(info, policy);
        assertThat(info.limitLevel).isEqualTo(ZERO);
        Assert.assertEquals(0, info.limitLevel);
    }

    @Test
    public void testLimitPositive_UpdatedToLimit() {
        NetworkPolicy policy = getDefaultNetworkPolicy();
        policy.limitBytes = POSITIVE_SMALL;
        policy.limitBytes = 5;
        mInfoController.updateDataLimit(info, policy);
        assertThat(info.limitLevel).isEqualTo(policy.limitBytes);
        Assert.assertEquals(policy.limitBytes, info.limitLevel);
    }
}