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.homepage.contextualcards.deviceinfo;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.ArgumentMatchers.any;
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.usage.NetworkStatsManager;
26 import android.content.Context;
27 import android.telephony.SubscriptionManager;
28 import android.telephony.TelephonyManager;
29 import androidx.core.graphics.drawable.IconCompat;
30 import androidx.slice.Slice;
31 import androidx.slice.SliceItem;
32 import androidx.slice.SliceMetadata;
33 import androidx.slice.SliceProvider;
34 import androidx.slice.core.SliceAction;
35 import androidx.slice.widget.SliceLiveData;
36 import com.android.settings.R;
37 import com.android.settings.testutils.SliceTester;
38 import com.android.settings.testutils.shadow.ShadowDataUsageUtils;
39 import java.util.List;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.robolectric.RobolectricTestRunner;
46 import org.robolectric.RuntimeEnvironment;
47 import org.robolectric.Shadows;
48 import org.robolectric.annotation.Config;
49 import org.robolectric.shadows.ShadowTelephonyManager;
50 
51 @RunWith(RobolectricTestRunner.class)
52 @Config(shadows = ShadowDataUsageUtils.class)
53 public class DataUsageSliceTest {
54     private static final String DATA_USAGE_TITLE = "Data usage";
55     private static final String DATA_USAGE_SUMMARY = "test_summary";
56 
57     @Mock
58     private NetworkStatsManager mNetworkStatsManager;
59 
60     private Context mContext;
61     private DataUsageSlice mDataUsageSlice;
62 
63     @Before
setUp()64     public void setUp() {
65         MockitoAnnotations.initMocks(this);
66 
67         mContext = spy(RuntimeEnvironment.application);
68         // Set-up specs for SliceMetadata.
69         SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
70         when(mContext.getSystemService(NetworkStatsManager.class)).thenReturn(mNetworkStatsManager);
71 
72         mDataUsageSlice = spy(new DataUsageSlice(mContext));
73 
74         final TelephonyManager telephonyManager = mContext.getSystemService(TelephonyManager.class);
75         final ShadowTelephonyManager shadowTelephonyManager = Shadows.shadowOf(telephonyManager);
76         shadowTelephonyManager.setTelephonyManagerForSubscriptionId(
77                 SubscriptionManager.INVALID_SUBSCRIPTION_ID, telephonyManager);
78     }
79 
80     @Test
getSlice_hasSim_shouldBeCorrectSliceContent()81     public void getSlice_hasSim_shouldBeCorrectSliceContent() {
82         ShadowDataUsageUtils.HAS_SIM = true;
83         doReturn(DATA_USAGE_TITLE).when(mDataUsageSlice).getDataUsageText(any());
84         doReturn(DATA_USAGE_SUMMARY).when(mDataUsageSlice).getCycleTime(any());
85 
86         final Slice slice = mDataUsageSlice.getSlice();
87 
88         final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
89         assertThat(metadata.getTitle()).isEqualTo(
90                 mContext.getString(R.string.data_usage_summary_title));
91 
92         final SliceAction primaryAction = metadata.getPrimaryAction();
93         final IconCompat expectedIcon = IconCompat.createWithResource(mContext,
94                 R.drawable.ic_settings_data_usage);
95         assertThat(primaryAction.getIcon().toString()).isEqualTo(expectedIcon.toString());
96     }
97 
98     @Test
getSlice_hasNoSim_shouldShowNoSimCard()99     public void getSlice_hasNoSim_shouldShowNoSimCard() {
100         ShadowDataUsageUtils.HAS_SIM = false;
101 
102         final Slice slice = mDataUsageSlice.getSlice();
103 
104         final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
105         assertThat(metadata.getTitle()).isEqualTo(
106                 mContext.getString(R.string.data_usage_summary_title));
107 
108         final List<SliceItem> sliceItems = slice.getItems();
109         SliceTester.assertAnySliceItemContainsTitle(sliceItems,
110                 mContext.getString(R.string.no_sim_card));
111     }
112 }
113