1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.settings.datausage;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.when;
24 
25 import android.app.Activity;
26 import android.content.ComponentName;
27 import android.content.Intent;
28 import android.graphics.Typeface;
29 import android.net.ConnectivityManager;
30 import android.net.NetworkTemplate;
31 import android.os.Bundle;
32 import android.telephony.SubscriptionManager;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.widget.Button;
36 import android.widget.LinearLayout;
37 import android.widget.ProgressBar;
38 import android.widget.TextView;
39 
40 import androidx.fragment.app.FragmentActivity;
41 import androidx.preference.PreferenceViewHolder;
42 
43 import com.android.settings.R;
44 import com.android.settings.SettingsActivity;
45 import com.android.settings.SubSettings;
46 import com.android.settingslib.Utils;
47 
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.MockitoAnnotations;
52 import org.robolectric.Robolectric;
53 import org.robolectric.RobolectricTestRunner;
54 import org.robolectric.Shadows;
55 import org.robolectric.shadows.ShadowActivity;
56 
57 import java.util.concurrent.TimeUnit;
58 
59 @RunWith(RobolectricTestRunner.class)
60 public class DataUsageSummaryPreferenceTest {
61 
62     private static final long CYCLE_DURATION_MILLIS = 1000000000L;
63     private static final long UPDATE_LAG_MILLIS = 10000000L;
64     private static final String DUMMY_CARRIER = "z-mobile";
65 
66     private Activity mActivity;
67     private PreferenceViewHolder mHolder;
68     private DataUsageSummaryPreference mSummaryPreference;
69     private TextView mUsageTitle;
70     private TextView mCycleTime;
71     private TextView mCarrierInfo;
72     private TextView mDataLimits;
73     private TextView mDataUsed;
74     private TextView mDataRemaining;
75     private Button mLaunchButton;
76     private LinearLayout mLabelBar;
77     private TextView mLabel1;
78     private TextView mLabel2;
79     private ProgressBar mProgressBar;
80 
81     private long mCycleEnd;
82     private long mUpdateTime;
83 
84     @Before
setUp()85     public void setUp() {
86         MockitoAnnotations.initMocks(this);
87         mActivity = spy(Robolectric.setupActivity(Activity.class));
88         mSummaryPreference = new DataUsageSummaryPreference(mActivity, null /* attrs */);
89         LayoutInflater inflater = LayoutInflater.from(mActivity);
90         View view = inflater.inflate(mSummaryPreference.getLayoutResource(), null /* root */,
91                 false /* attachToRoot */);
92 
93         mHolder = spy(PreferenceViewHolder.createInstanceForTests(view));
94 
95         final long now = System.currentTimeMillis();
96         mCycleEnd = now + CYCLE_DURATION_MILLIS;
97         mUpdateTime = now - UPDATE_LAG_MILLIS;
98     }
99 
100     @Test
testSetUsageInfo_withLaunchIntent_launchButtonShown()101     public void testSetUsageInfo_withLaunchIntent_launchButtonShown() {
102         mSummaryPreference.setUsageInfo(mCycleEnd, mUpdateTime, DUMMY_CARRIER, 0 /* numPlans */,
103                 new Intent());
104 
105         bindViewHolder();
106         assertThat(mLaunchButton.getVisibility()).isEqualTo(View.VISIBLE);
107     }
108 
109     @Test
testSetUsageInfo_withoutLaunchIntent_launchButtonNotShown()110     public void testSetUsageInfo_withoutLaunchIntent_launchButtonNotShown() {
111         mSummaryPreference.setUsageInfo(mCycleEnd, mUpdateTime, DUMMY_CARRIER, 0 /* numPlans */,
112                 null /* launchIntent */);
113 
114         bindViewHolder();
115         assertThat(mLaunchButton.getVisibility()).isEqualTo(View.GONE);
116     }
117 
118     @Test
testSetUsageInfo_withDataPlans_carrierInfoShown()119     public void testSetUsageInfo_withDataPlans_carrierInfoShown() {
120         mSummaryPreference.setUsageInfo(mCycleEnd, mUpdateTime, DUMMY_CARRIER, 1 /* numPlans */,
121                 new Intent());
122 
123         bindViewHolder();
124         assertThat(mCarrierInfo.getVisibility()).isEqualTo(View.VISIBLE);
125     }
126 
127     @Test
testSetUsageInfo_withNoDataPlans_carrierInfoNotShown()128     public void testSetUsageInfo_withNoDataPlans_carrierInfoNotShown() {
129         mSummaryPreference.setUsageInfo(mCycleEnd, mUpdateTime, DUMMY_CARRIER, 0 /* numPlans */,
130                 new Intent());
131 
132         bindViewHolder();
133         assertThat(mCarrierInfo.getVisibility()).isEqualTo(View.GONE);
134     }
135 
136     @Test
testCarrierUpdateTime_shouldFormatDaysCorrectly()137     public void testCarrierUpdateTime_shouldFormatDaysCorrectly() {
138         int baseUnit = 2;
139         int smudge = 6;
140         final long updateTime = System.currentTimeMillis()
141                 - TimeUnit.DAYS.toMillis(baseUnit) - TimeUnit.HOURS.toMillis(smudge);
142         mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
143         mSummaryPreference.setUsageInfo(mCycleEnd, updateTime, DUMMY_CARRIER, 1 /* numPlans */,
144                 new Intent());
145 
146         bindViewHolder();
147         assertThat(mCarrierInfo.getText().toString())
148                 .isEqualTo("Updated by " + DUMMY_CARRIER + " " + baseUnit + " days ago");
149     }
150 
151     @Test
testCarrierUpdateTime_shouldFormatHoursCorrectly()152     public void testCarrierUpdateTime_shouldFormatHoursCorrectly() {
153         int baseUnit = 2;
154         int smudge = 6;
155         final long updateTime = System.currentTimeMillis()
156                 - TimeUnit.HOURS.toMillis(baseUnit) - TimeUnit.MINUTES.toMillis(smudge);
157         mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
158         mSummaryPreference.setUsageInfo(mCycleEnd, updateTime, DUMMY_CARRIER, 1 /* numPlans */,
159                 new Intent());
160 
161         bindViewHolder();
162         assertThat(mCarrierInfo.getText().toString())
163                 .isEqualTo("Updated by " + DUMMY_CARRIER + " " + baseUnit + " hr ago");
164     }
165 
166     @Test
testCarrierUpdateTime_shouldFormatMinutesCorrectly()167     public void testCarrierUpdateTime_shouldFormatMinutesCorrectly() {
168         int baseUnit = 2;
169         int smudge = 6;
170         final long updateTime = System.currentTimeMillis()
171                 - TimeUnit.MINUTES.toMillis(baseUnit) - TimeUnit.SECONDS.toMillis(smudge);
172         mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
173         mSummaryPreference.setUsageInfo(mCycleEnd, updateTime, DUMMY_CARRIER, 1 /* numPlans */,
174                 new Intent());
175 
176         bindViewHolder();
177         assertThat(mCarrierInfo.getText().toString())
178                 .isEqualTo("Updated by " + DUMMY_CARRIER + " " + baseUnit + " min ago");
179     }
180 
181     @Test
testCarrierUpdateTime_shouldFormatLessThanMinuteCorrectly()182     public void testCarrierUpdateTime_shouldFormatLessThanMinuteCorrectly() {
183         final long updateTime = System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(45);
184         mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
185         mSummaryPreference.setUsageInfo(mCycleEnd, updateTime, DUMMY_CARRIER, 1 /* numPlans */,
186                 new Intent());
187 
188         bindViewHolder();
189         assertThat(mCarrierInfo.getText().toString())
190                 .isEqualTo("Updated by " + DUMMY_CARRIER + " just now");
191     }
192 
193     @Test
testCarrierUpdateTimeWithNoCarrier_shouldSayJustNow()194     public void testCarrierUpdateTimeWithNoCarrier_shouldSayJustNow() {
195         final long updateTime = System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(45);
196         mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
197         mSummaryPreference.setUsageInfo(mCycleEnd, updateTime, null /* carrier */,
198                 1 /* numPlans */, new Intent());
199 
200         bindViewHolder();
201         assertThat(mCarrierInfo.getText().toString())
202                 .isEqualTo("Updated just now");
203     }
204 
205     @Test
testCarrierUpdateTimeWithNoCarrier_shouldFormatTime()206     public void testCarrierUpdateTimeWithNoCarrier_shouldFormatTime() {
207         final long updateTime = System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(2);
208         mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
209         mSummaryPreference.setUsageInfo(mCycleEnd, updateTime, null /* carrier */,
210                 1 /* numPlans */, new Intent());
211 
212         bindViewHolder();
213         assertThat(mCarrierInfo.getText().toString())
214                 .isEqualTo("Updated 2 min ago");
215     }
216 
217     @Test
setUsageInfo_withRecentCarrierUpdate_doesNotSetCarrierInfoWarningColorAndFont()218     public void setUsageInfo_withRecentCarrierUpdate_doesNotSetCarrierInfoWarningColorAndFont() {
219         final long updateTime = System.currentTimeMillis() - TimeUnit.HOURS.toMillis(1);
220         mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
221         mSummaryPreference.setUsageInfo(mCycleEnd, updateTime, DUMMY_CARRIER, 1 /* numPlans */,
222                 new Intent());
223 
224         bindViewHolder();
225         assertThat(mCarrierInfo.getVisibility()).isEqualTo(View.VISIBLE);
226         assertThat(mCarrierInfo.getCurrentTextColor()).isEqualTo(
227                 Utils.getColorAttrDefaultColor(mActivity, android.R.attr.textColorSecondary));
228         assertThat(mCarrierInfo.getTypeface()).isEqualTo(Typeface.SANS_SERIF);
229     }
230 
231     @Test
testSetUsageInfo_withStaleCarrierUpdate_setsCarrierInfoWarningColorAndFont()232     public void testSetUsageInfo_withStaleCarrierUpdate_setsCarrierInfoWarningColorAndFont() {
233         final long updateTime = System.currentTimeMillis() - TimeUnit.HOURS.toMillis(7);
234         mSummaryPreference.setUsageInfo(mCycleEnd, updateTime, DUMMY_CARRIER, 1 /* numPlans */,
235                 new Intent());
236 
237         bindViewHolder();
238         assertThat(mCarrierInfo.getVisibility()).isEqualTo(View.VISIBLE);
239         assertThat(mCarrierInfo.getCurrentTextColor()).isEqualTo(
240                 Utils.getColorAttrDefaultColor(mActivity, android.R.attr.colorError));
241         assertThat(mCarrierInfo.getTypeface()).isEqualTo(
242                 DataUsageSummaryPreference.SANS_SERIF_MEDIUM);
243     }
244 
245     @Test
testSetUsageInfo_withNoDataPlans_usageTitleNotShown()246     public void testSetUsageInfo_withNoDataPlans_usageTitleNotShown() {
247         mSummaryPreference.setUsageInfo(mCycleEnd, mUpdateTime, DUMMY_CARRIER, 0 /* numPlans */,
248                 new Intent());
249 
250         bindViewHolder();
251         assertThat(mUsageTitle.getVisibility()).isEqualTo(View.GONE);
252     }
253 
254     @Test
testSetUsageInfo_withMultipleDataPlans_usageTitleShown()255     public void testSetUsageInfo_withMultipleDataPlans_usageTitleShown() {
256         mSummaryPreference.setUsageInfo(mCycleEnd, mUpdateTime, DUMMY_CARRIER, 2 /* numPlans */,
257                 new Intent());
258 
259         bindViewHolder();
260         assertThat(mUsageTitle.getVisibility()).isEqualTo(View.VISIBLE);
261     }
262 
263     @Test
testSetUsageInfo_cycleRemainingTimeIsLessOneDay()264     public void testSetUsageInfo_cycleRemainingTimeIsLessOneDay() {
265         // just under one day
266         final long cycleEnd = System.currentTimeMillis() + TimeUnit.HOURS.toMillis(23);
267         mSummaryPreference.setUsageInfo(cycleEnd, mUpdateTime, DUMMY_CARRIER, 0 /* numPlans */,
268                 new Intent());
269 
270         bindViewHolder();
271         assertThat(mCycleTime.getVisibility()).isEqualTo(View.VISIBLE);
272         assertThat(mCycleTime.getText()).isEqualTo(
273                 mActivity.getString(R.string.billing_cycle_less_than_one_day_left));
274     }
275 
276     @Test
testSetUsageInfo_cycleRemainingTimeNegativeDaysLeft_shouldDisplayNoneLeft()277     public void testSetUsageInfo_cycleRemainingTimeNegativeDaysLeft_shouldDisplayNoneLeft() {
278         final long cycleEnd = System.currentTimeMillis() - 1L;
279         mSummaryPreference.setUsageInfo(cycleEnd, mUpdateTime, DUMMY_CARRIER, 0 /* numPlans */,
280                 new Intent());
281 
282         bindViewHolder();
283         assertThat(mCycleTime.getVisibility()).isEqualTo(View.VISIBLE);
284         assertThat(mCycleTime.getText()).isEqualTo(
285                 mActivity.getString(R.string.billing_cycle_none_left));
286     }
287 
288     @Test
testSetUsageInfo_cycleRemainingTimeDaysLeft_shouldUsePlurals()289     public void testSetUsageInfo_cycleRemainingTimeDaysLeft_shouldUsePlurals() {
290         final int daysLeft = 3;
291         final long cycleEnd = System.currentTimeMillis() + TimeUnit.DAYS.toMillis(daysLeft)
292                 + TimeUnit.HOURS.toMillis(1);
293         mSummaryPreference.setUsageInfo(cycleEnd, mUpdateTime, DUMMY_CARRIER, 0 /* numPlans */,
294                 new Intent());
295 
296         bindViewHolder();
297         assertThat(mCycleTime.getVisibility()).isEqualTo(View.VISIBLE);
298         assertThat(mCycleTime.getText()).isEqualTo(daysLeft + " days left");
299     }
300 
301     @Test
testSetLimitInfo_withLimitInfo_dataLimitsShown()302     public void testSetLimitInfo_withLimitInfo_dataLimitsShown() {
303         final String limitText = "test limit text";
304         mSummaryPreference.setLimitInfo(limitText);
305 
306         bindViewHolder();
307         assertThat(mDataLimits.getVisibility()).isEqualTo(View.VISIBLE);
308         assertThat(mDataLimits.getText()).isEqualTo(limitText);
309     }
310 
311     @Test
testSetLimitInfo_withNullLimitInfo_dataLimitsNotShown()312     public void testSetLimitInfo_withNullLimitInfo_dataLimitsNotShown() {
313         mSummaryPreference.setLimitInfo(null);
314 
315         bindViewHolder();
316         assertThat(mDataLimits.getVisibility()).isEqualTo(View.GONE);
317     }
318 
319     @Test
testSetLimitInfo_withEmptyLimitInfo_dataLimitsNotShown()320     public void testSetLimitInfo_withEmptyLimitInfo_dataLimitsNotShown() {
321         final String emptyLimitText = "";
322         mSummaryPreference.setLimitInfo(emptyLimitText);
323 
324         bindViewHolder();
325         assertThat(mDataLimits.getVisibility()).isEqualTo(View.GONE);
326     }
327 
328     @Test
testSetChartEnabledFalse_hidesLabelBar()329     public void testSetChartEnabledFalse_hidesLabelBar() {
330         setValidLabels();
331         mSummaryPreference.setChartEnabled(false);
332 
333         bindViewHolder();
334         assertThat(mLabelBar.getVisibility()).isEqualTo(View.GONE);
335         assertThat(mProgressBar.getVisibility()).isEqualTo(View.GONE);
336     }
337 
338     @Test
testSetEmptyLabels_hidesLabelBar()339     public void testSetEmptyLabels_hidesLabelBar() {
340         mSummaryPreference.setLabels("", "");
341 
342         bindViewHolder();
343         assertThat(mLabelBar.getVisibility()).isEqualTo(View.GONE);
344         assertThat(mProgressBar.getVisibility()).isEqualTo(View.GONE);
345     }
346 
347     @Test
testLabelBar_isVisible_whenLabelsSet()348     public void testLabelBar_isVisible_whenLabelsSet() {
349         setValidLabels();
350         //mChartEnabled defaults to true
351 
352         bindViewHolder();
353         assertThat(mLabelBar.getVisibility()).isEqualTo(View.VISIBLE);
354         assertThat(mProgressBar.getVisibility()).isEqualTo(View.VISIBLE);
355     }
356 
357     @Test
testSetProgress_updatesProgressBar()358     public void testSetProgress_updatesProgressBar() {
359         setValidLabels();
360         mSummaryPreference.setProgress(.5f);
361 
362         bindViewHolder();
363         assertThat(mProgressBar.getProgress()).isEqualTo(50);
364     }
365 
setValidLabels()366     private void setValidLabels() {
367         mSummaryPreference.setLabels("0.0 GB", "5.0 GB");
368     }
369 
370     @Test
testSetUsageAndRemainingInfo_withUsageInfo_dataUsageAndRemainingShown()371     public void testSetUsageAndRemainingInfo_withUsageInfo_dataUsageAndRemainingShown() {
372         mSummaryPreference.setUsageInfo(mCycleEnd, mUpdateTime, DUMMY_CARRIER, 1 /* numPlans */,
373                 new Intent());
374         mSummaryPreference.setUsageNumbers(
375                 BillingCycleSettings.MIB_IN_BYTES,
376                 10 * BillingCycleSettings.MIB_IN_BYTES,
377                 true /* hasMobileData */);
378 
379         bindViewHolder();
380         assertThat(mDataUsed.getText().toString()).isEqualTo("1.00 MB used");
381         assertThat(mDataRemaining.getText().toString()).isEqualTo("9.00 MB left");
382         assertThat(mDataRemaining.getVisibility()).isEqualTo(View.VISIBLE);
383         final int colorId = Utils.getColorAttrDefaultColor(mActivity, android.R.attr.colorAccent);
384         assertThat(mDataRemaining.getCurrentTextColor()).isEqualTo(colorId);
385     }
386 
387     @Test
testSetUsageInfo_withDataOverusage()388     public void testSetUsageInfo_withDataOverusage() {
389         mSummaryPreference.setUsageInfo(mCycleEnd, mUpdateTime, DUMMY_CARRIER, 1 /* numPlans */,
390                 new Intent());
391         mSummaryPreference.setUsageNumbers(
392                 11 * BillingCycleSettings.MIB_IN_BYTES,
393                 10 * BillingCycleSettings.MIB_IN_BYTES,
394                 true /* hasMobileData */);
395 
396         bindViewHolder();
397         assertThat(mDataUsed.getText().toString()).isEqualTo("11.00 MB used");
398         assertThat(mDataRemaining.getText().toString()).isEqualTo("1.00 MB over");
399         final int colorId = Utils.getColorAttrDefaultColor(mActivity, android.R.attr.colorError);
400         assertThat(mDataRemaining.getCurrentTextColor()).isEqualTo(colorId);
401     }
402 
403     @Test
testSetUsageInfo_withUsageInfo_dataUsageShown()404     public void testSetUsageInfo_withUsageInfo_dataUsageShown() {
405         mSummaryPreference.setUsageInfo(mCycleEnd, mUpdateTime, DUMMY_CARRIER, 0 /* numPlans */,
406                 new Intent());
407         mSummaryPreference.setUsageNumbers(
408                 BillingCycleSettings.MIB_IN_BYTES, -1L, true /* hasMobileData */);
409 
410         bindViewHolder();
411         assertThat(mDataUsed.getText().toString()).isEqualTo("1.00 MB used");
412         assertThat(mDataRemaining.getText()).isEqualTo("");
413     }
414 
415     @Test
testSetAppIntent_toMdpApp_intentCorrect()416     public void testSetAppIntent_toMdpApp_intentCorrect() {
417         final FragmentActivity activity = Robolectric.setupActivity(FragmentActivity.class);
418         final Intent intent = new Intent(SubscriptionManager.ACTION_MANAGE_SUBSCRIPTION_PLANS);
419         intent.setPackage("test-owner.example.com");
420         intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, 42);
421 
422         mSummaryPreference.setUsageInfo(mCycleEnd, mUpdateTime, DUMMY_CARRIER, 0 /* numPlans */,
423                 intent);
424 
425         bindViewHolder();
426         assertThat(mLaunchButton.getVisibility()).isEqualTo(View.VISIBLE);
427         assertThat(mLaunchButton.getText())
428                 .isEqualTo(mActivity.getString(R.string.launch_mdp_app_text));
429 
430         mLaunchButton.callOnClick();
431         ShadowActivity shadowActivity = Shadows.shadowOf(activity);
432         Intent startedIntent = shadowActivity.getNextStartedActivity();
433         assertThat(startedIntent.getAction())
434                 .isEqualTo(SubscriptionManager.ACTION_MANAGE_SUBSCRIPTION_PLANS);
435         assertThat(startedIntent.getPackage()).isEqualTo("test-owner.example.com");
436         assertThat(startedIntent.getIntExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, -1))
437                 .isEqualTo(42);
438     }
439 
440     @Test
testSetUsageInfo_withOverflowStrings_dataRemainingNotShown()441     public void testSetUsageInfo_withOverflowStrings_dataRemainingNotShown() {
442         LayoutInflater inflater = LayoutInflater.from(mActivity);
443         View view = inflater.inflate(mSummaryPreference.getLayoutResource(), null /* root */,
444                 false /* attachToRoot */);
445 
446         TextView dataUsed = spy(new TextView(mActivity));
447         TextView dataRemaining = spy(new TextView(mActivity));
448         doReturn(dataUsed).when(mHolder).findViewById(R.id.data_usage_view);
449         doReturn(dataRemaining).when(mHolder).findViewById(R.id.data_remaining_view);
450 
451         mSummaryPreference.setUsageInfo(mCycleEnd, mUpdateTime, DUMMY_CARRIER, 1 /* numPlans */,
452                 new Intent());
453         mSummaryPreference.setUsageNumbers(
454                 BillingCycleSettings.MIB_IN_BYTES,
455                 10 * BillingCycleSettings.MIB_IN_BYTES,
456                 true /* hasMobileData */);
457 
458         when(mActivity.getResources()).thenCallRealMethod();
459         when(mActivity.getText(R.string.data_used_formatted))
460                 .thenReturn("^1 ^2 used with long trailing text");
461         when(mActivity.getText(R.string.data_remaining)).thenReturn("^1 left");
462 
463         bindViewHolder();
464 
465         doReturn(500).when(dataUsed).getMeasuredWidth();
466         doReturn(500).when(dataRemaining).getMeasuredWidth();
467 
468         assertThat(dataRemaining.getVisibility()).isEqualTo(View.VISIBLE);
469 
470         MeasurableLinearLayout layout =
471                 (MeasurableLinearLayout) mHolder.findViewById(R.id.usage_layout);
472         layout.measure(
473                 View.MeasureSpec.makeMeasureSpec(800, View.MeasureSpec.EXACTLY),
474                 View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY));
475 
476         assertThat(dataUsed.getText().toString()).isEqualTo("1.00 MB used with long trailing text");
477         assertThat(dataRemaining.getVisibility()).isEqualTo(View.GONE);
478     }
479 
480     @Test
testSetWifiMode_withUsageInfo_dataUsageShown()481     public void testSetWifiMode_withUsageInfo_dataUsageShown() {
482         final int daysLeft = 3;
483         final long cycleEnd = System.currentTimeMillis() + TimeUnit.DAYS.toMillis(daysLeft)
484                 + TimeUnit.HOURS.toMillis(1);
485         final FragmentActivity activity = Robolectric.setupActivity(FragmentActivity.class);
486         mSummaryPreference = spy(mSummaryPreference);
487         mSummaryPreference.setUsageInfo(cycleEnd, mUpdateTime, DUMMY_CARRIER, 0 /* numPlans */,
488                 new Intent());
489         mSummaryPreference.setUsageNumbers(1000000L, -1L, true);
490         final String cycleText = "The quick fox";
491         mSummaryPreference.setWifiMode(true /* isWifiMode */, cycleText, false /* isSingleWifi */);
492         doReturn(200L).when(mSummaryPreference).getHistoricalUsageLevel();
493 
494         bindViewHolder();
495         assertThat(mUsageTitle.getText().toString())
496                 .isEqualTo(mActivity.getString(R.string.data_usage_wifi_title));
497         assertThat(mUsageTitle.getVisibility()).isEqualTo(View.VISIBLE);
498         assertThat(mCycleTime.getVisibility()).isEqualTo(View.VISIBLE);
499         assertThat(mCycleTime.getText()).isEqualTo(cycleText);
500         assertThat(mCarrierInfo.getVisibility()).isEqualTo(View.GONE);
501         assertThat(mDataLimits.getVisibility()).isEqualTo(View.GONE);
502         assertThat(mLaunchButton.getVisibility()).isEqualTo(View.VISIBLE);
503         assertThat(mLaunchButton.getText())
504                 .isEqualTo(mActivity.getString(R.string.launch_wifi_text));
505 
506         mLaunchButton.callOnClick();
507         ShadowActivity shadowActivity = Shadows.shadowOf(activity);
508         Intent startedIntent = shadowActivity.getNextStartedActivity();
509         assertThat(startedIntent.getComponent()).isEqualTo(new ComponentName("com.android.settings",
510                 SubSettings.class.getName()));
511 
512         final Bundle expect = new Bundle(1);
513         expect.putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE,
514                 NetworkTemplate.buildTemplateWifiWildcard());
515         final Bundle actual = startedIntent
516                 .getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
517         assertThat((NetworkTemplate) actual.getParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE))
518                 .isEqualTo(NetworkTemplate.buildTemplateWifiWildcard());
519 
520         assertThat(startedIntent.getIntExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID, 0))
521                 .isEqualTo(R.string.wifi_data_usage);
522     }
523 
524     @Test
testSetWifiMode_noUsageInfo_shouldDisableLaunchButton()525     public void testSetWifiMode_noUsageInfo_shouldDisableLaunchButton() {
526         mSummaryPreference = spy(mSummaryPreference);
527         mSummaryPreference.setWifiMode(true /* isWifiMode */, "Test cycle text",
528                 false /* isSingleWifi */);
529         doReturn(0L).when(mSummaryPreference).getHistoricalUsageLevel();
530 
531         bindViewHolder();
532 
533         assertThat(mLaunchButton.isEnabled()).isFalse();
534     }
535 
536     @Test
launchWifiDataUsage_shouldSetWifiNetworkTypeInIntentExtra()537     public void launchWifiDataUsage_shouldSetWifiNetworkTypeInIntentExtra() {
538         mSummaryPreference.launchWifiDataUsage(mActivity);
539 
540         final Intent launchIntent = Shadows.shadowOf(mActivity).getNextStartedActivity();
541         final Bundle args =
542             launchIntent.getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
543 
544         assertThat(args.getInt(DataUsageList.EXTRA_NETWORK_TYPE))
545             .isEqualTo(ConnectivityManager.TYPE_WIFI);
546     }
547 
bindViewHolder()548     private void bindViewHolder() {
549         mSummaryPreference.onBindViewHolder(mHolder);
550         mUsageTitle = (TextView) mHolder.findViewById(R.id.usage_title);
551         mCycleTime = (TextView) mHolder.findViewById(R.id.cycle_left_time);
552         mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
553         mDataLimits = (TextView) mHolder.findViewById(R.id.data_limits);
554         mDataUsed = spy((TextView) mHolder.findViewById(R.id.data_usage_view));
555         mDataRemaining = spy((TextView) mHolder.findViewById(R.id.data_remaining_view));
556         mLaunchButton = (Button) mHolder.findViewById(R.id.launch_mdp_app_button);
557         mLabelBar = (LinearLayout) mHolder.findViewById(R.id.label_bar);
558         mLabel1 = (TextView) mHolder.findViewById(R.id.text1);
559         mLabel2 = (TextView) mHolder.findViewById(R.id.text2);
560         mProgressBar = (ProgressBar) mHolder.findViewById(R.id.determinateBar);
561     }
562 }
563