• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.car.settings.system;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.when;
23 
24 import android.car.drivingstate.CarUxRestrictions;
25 import android.content.Context;
26 import android.content.pm.PackageManager;
27 
28 import androidx.lifecycle.LifecycleOwner;
29 import androidx.preference.Preference;
30 import androidx.test.core.app.ApplicationProvider;
31 import androidx.test.ext.junit.runners.AndroidJUnit4;
32 
33 import com.android.car.settings.R;
34 import com.android.car.settings.common.FragmentController;
35 import com.android.car.settings.common.PreferenceControllerTestUtil;
36 import com.android.car.settings.testutils.TestLifecycleOwner;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 
44 @RunWith(AndroidJUnit4.class)
45 public class ResetNetworkItemsPreferenceControllerTest {
46     private Context mContext = spy(ApplicationProvider.getApplicationContext());
47     private LifecycleOwner mLifecycleOwner;
48     private Preference mPreference;
49     private ResetNetworkItemsPreferenceController mPreferenceController;
50     private CarUxRestrictions mCarUxRestrictions;
51 
52     @Mock
53     private FragmentController mFragmentController;
54     @Mock
55     private PackageManager mMockPm;
56 
57     @Before
setUp()58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60         mLifecycleOwner = new TestLifecycleOwner();
61         mCarUxRestrictions = new CarUxRestrictions.Builder(/* reqOpt= */ true,
62                 CarUxRestrictions.UX_RESTRICTIONS_BASELINE, /* timestamp= */ 0).build();
63 
64         when(mContext.getPackageManager()).thenReturn(mMockPm);
65 
66         mPreference = new Preference(mContext);
67         mPreferenceController = new ResetNetworkItemsPreferenceController(mContext,
68                 "key", mFragmentController, mCarUxRestrictions);
69         PreferenceControllerTestUtil.assignPreference(mPreferenceController, mPreference);
70     }
71 
72     @Test
onCreate_containsDefaultDescription()73     public void onCreate_containsDefaultDescription() {
74         setSystemFeatures(/* hasWifi= */ false, /* hasTelephony= */ false,
75                 /* hasBluetooth= */ false);
76         mPreferenceController.onCreate(mLifecycleOwner);
77         assertContainsString(mPreference.getTitle(),
78                 mContext.getString(R.string.reset_network_desc));
79     }
80 
81     @Test
onCreate_noWifi_doesNotContainWifiText()82     public void onCreate_noWifi_doesNotContainWifiText() {
83         setSystemFeatures(/* hasWifi= */ false, /* hasTelephony= */ false,
84                 /* hasBluetooth= */ false);
85         mPreferenceController.onCreate(mLifecycleOwner);
86         assertDoesNotContainsString(mPreference.getTitle(),
87                 mContext.getString(R.string.reset_network_item_wifi));
88     }
89 
90     @Test
onCreate_noTelephony_doesNotContainTelephonyText()91     public void onCreate_noTelephony_doesNotContainTelephonyText() {
92         setSystemFeatures(/* hasWifi= */ false, /* hasTelephony= */ false,
93                 /* hasBluetooth= */ false);
94         mPreferenceController.onCreate(mLifecycleOwner);
95         assertDoesNotContainsString(mPreference.getTitle(),
96                 mContext.getString(R.string.reset_network_item_mobile));
97     }
98 
99     @Test
onCreate_noBluetooth_doesNotContainBluetoothText()100     public void onCreate_noBluetooth_doesNotContainBluetoothText() {
101         setSystemFeatures(/* hasWifi= */ false, /* hasTelephony= */ false,
102                 /* hasBluetooth= */ false);
103         mPreferenceController.onCreate(mLifecycleOwner);
104         assertDoesNotContainsString(mPreference.getTitle(),
105                 mContext.getString(R.string.reset_network_item_bluetooth));
106     }
107 
108     @Test
onCreate_hasWifi_containsWifiText()109     public void onCreate_hasWifi_containsWifiText() {
110         setSystemFeatures(/* hasWifi= */ true, /* hasTelephony= */ false,
111                 /* hasBluetooth= */ false);
112         mPreferenceController.onCreate(mLifecycleOwner);
113         assertContainsString(mPreference.getTitle(),
114                 mContext.getString(R.string.reset_network_item_wifi));
115     }
116 
117     @Test
onCreate_hasTelephony_containsTelephonyText()118     public void onCreate_hasTelephony_containsTelephonyText() {
119         setSystemFeatures(/* hasWifi= */ false, /* hasTelephony= */ true,
120                 /* hasBluetooth= */ false);
121         mPreferenceController.onCreate(mLifecycleOwner);
122         assertContainsString(mPreference.getTitle(),
123                 mContext.getString(R.string.reset_network_item_mobile));
124     }
125 
126     @Test
onCreate_hasBluetooth_containsBluetoothText()127     public void onCreate_hasBluetooth_containsBluetoothText() {
128         setSystemFeatures(/* hasWifi= */ false, /* hasTelephony= */ false,
129                 /* hasBluetooth= */ true);
130         mPreferenceController.onCreate(mLifecycleOwner);
131         assertContainsString(mPreference.getTitle(),
132                 mContext.getString(R.string.reset_network_item_bluetooth));
133     }
134 
setSystemFeatures(boolean hasWifi, boolean hasTelephony, boolean hasBluetooth)135     private void setSystemFeatures(boolean hasWifi, boolean hasTelephony, boolean hasBluetooth) {
136         when(mMockPm.hasSystemFeature(PackageManager.FEATURE_WIFI)).thenReturn(hasWifi);
137         when(mMockPm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)).thenReturn(hasTelephony);
138         when(mMockPm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)).thenReturn(hasBluetooth);
139     }
140 
assertContainsString(CharSequence source, String pattern)141     private void assertContainsString(CharSequence source, String pattern) {
142         assertThat(source).isNotNull();
143         String sourceString = source.toString();
144         assertThat(sourceString.contains(pattern)).isTrue();
145     }
146 
assertDoesNotContainsString(CharSequence source, String pattern)147     private void assertDoesNotContainsString(CharSequence source, String pattern) {
148         assertThat(source).isNotNull();
149         String sourceString = source.toString();
150         assertThat(sourceString.contains(pattern)).isFalse();
151     }
152 }
153