1 package com.android.settings.datausage; 2 3 import android.net.NetworkPolicy; 4 import android.net.NetworkTemplate; 5 import com.android.settings.testutils.SettingsRobolectricTestRunner; 6 import com.android.settings.TestConfig; 7 import com.android.settingslib.net.DataUsageController.DataUsageInfo; 8 import org.junit.Before; 9 import org.junit.Test; 10 import org.junit.runner.RunWith; 11 import org.robolectric.annotation.Config; 12 13 import static com.google.common.truth.Truth.assertThat; 14 15 @RunWith(SettingsRobolectricTestRunner.class) 16 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 17 public class DataUsageInfoControllerTest { 18 private DataUsageInfoController mInfoController; 19 private DataUsageInfo info; 20 private static final int NEGATIVE = -1; 21 private static final int ZERO = 0; 22 private static final int POSITIVE_SMALL = 1; 23 private static final int POSITIVE_LARGE = 5; 24 25 @Before setUp()26 public void setUp() { 27 mInfoController = new DataUsageInfoController(); 28 info = new DataUsageInfo(); 29 } 30 31 @Test testLowUsageLowWarning_LimitUsed()32 public void testLowUsageLowWarning_LimitUsed() { 33 info.warningLevel = POSITIVE_SMALL; 34 info.limitLevel = POSITIVE_LARGE; 35 info.usageLevel = POSITIVE_SMALL; 36 assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.limitLevel); 37 } 38 39 @Test testLowUsageEqualWarning_LimitUsed()40 public void testLowUsageEqualWarning_LimitUsed() { 41 info.warningLevel = POSITIVE_LARGE; 42 info.limitLevel = POSITIVE_LARGE; 43 info.usageLevel = POSITIVE_SMALL; 44 assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.limitLevel); 45 } 46 47 @Test testNoLimitNoUsage_WarningUsed()48 public void testNoLimitNoUsage_WarningUsed() { 49 info.warningLevel = POSITIVE_LARGE; 50 info.limitLevel = ZERO; 51 info.usageLevel = ZERO; 52 assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.warningLevel); 53 } 54 55 @Test testNoLimitLowUsage_WarningUsed()56 public void testNoLimitLowUsage_WarningUsed() { 57 info.warningLevel = POSITIVE_LARGE; 58 info.limitLevel = ZERO; 59 info.usageLevel = POSITIVE_SMALL; 60 assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.warningLevel); 61 } 62 63 @Test testLowWarningNoLimit_UsageUsed()64 public void testLowWarningNoLimit_UsageUsed() { 65 info.warningLevel = POSITIVE_SMALL; 66 info.limitLevel = ZERO; 67 info.usageLevel = POSITIVE_LARGE; 68 assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.usageLevel); 69 } 70 71 @Test testLowWarningLowLimit_UsageUsed()72 public void testLowWarningLowLimit_UsageUsed() { 73 info.warningLevel = POSITIVE_SMALL; 74 info.limitLevel = POSITIVE_SMALL; 75 info.usageLevel = POSITIVE_LARGE; 76 assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.usageLevel); 77 } 78 getDefaultNetworkPolicy()79 private NetworkPolicy getDefaultNetworkPolicy() { 80 NetworkTemplate template = new NetworkTemplate(NetworkTemplate.MATCH_WIFI_WILDCARD, 81 null, null); 82 int cycleDay = -1; 83 String cycleTimezone = "UTC"; 84 long warningBytes = -1; 85 long limitBytes = -1; 86 return new NetworkPolicy(template,cycleDay, cycleTimezone, warningBytes, limitBytes, true); 87 } 88 89 @Test testNullArguments_NoError()90 public void testNullArguments_NoError() { 91 mInfoController.updateDataLimit(null, null); 92 mInfoController.updateDataLimit(info, null); 93 mInfoController.updateDataLimit(null, getDefaultNetworkPolicy()); 94 } 95 96 @Test testNegativeWarning_UpdatedToZero()97 public void testNegativeWarning_UpdatedToZero() { 98 NetworkPolicy policy = getDefaultNetworkPolicy(); 99 policy.warningBytes = NEGATIVE; 100 mInfoController.updateDataLimit(info, policy); 101 assertThat(info.warningLevel).isEqualTo(ZERO); 102 } 103 104 @Test testWarningZero_UpdatedToZero()105 public void testWarningZero_UpdatedToZero() { 106 NetworkPolicy policy = getDefaultNetworkPolicy(); 107 policy.warningBytes = ZERO; 108 mInfoController.updateDataLimit(info, policy); 109 assertThat(info.warningLevel).isEqualTo(ZERO); 110 } 111 112 @Test testWarningPositive_UpdatedToWarning()113 public void testWarningPositive_UpdatedToWarning() { 114 NetworkPolicy policy = getDefaultNetworkPolicy(); 115 policy.warningBytes = POSITIVE_SMALL; 116 mInfoController.updateDataLimit(info, policy); 117 assertThat(info.warningLevel).isEqualTo(policy.warningBytes); 118 } 119 120 @Test testLimitNegative_UpdatedToZero()121 public void testLimitNegative_UpdatedToZero() { 122 NetworkPolicy policy = getDefaultNetworkPolicy(); 123 policy.limitBytes = NEGATIVE; 124 mInfoController.updateDataLimit(info, policy); 125 assertThat(info.limitLevel).isEqualTo(ZERO); 126 } 127 128 @Test testLimitZero_UpdatedToZero()129 public void testLimitZero_UpdatedToZero() { 130 NetworkPolicy policy = getDefaultNetworkPolicy(); 131 policy.limitBytes = ZERO; 132 mInfoController.updateDataLimit(info, policy); 133 assertThat(info.limitLevel).isEqualTo(ZERO); 134 } 135 136 @Test testLimitPositive_UpdatedToLimit()137 public void testLimitPositive_UpdatedToLimit() { 138 NetworkPolicy policy = getDefaultNetworkPolicy(); 139 policy.limitBytes = POSITIVE_SMALL; 140 mInfoController.updateDataLimit(info, policy); 141 assertThat(info.limitLevel).isEqualTo(policy.limitBytes); 142 } 143 }