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