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.settings.wifi;
18 
19 import static com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_MAX;
20 import static com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_MIN;
21 import static com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_UNREACHABLE;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyLong;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 
32 import android.content.Context;
33 import android.net.wifi.WifiManager;
34 import android.os.HandlerThread;
35 import android.os.PersistableBundle;
36 import android.telephony.CarrierConfigManager;
37 
38 import androidx.lifecycle.Lifecycle;
39 import androidx.test.core.app.ApplicationProvider;
40 import androidx.test.ext.junit.runners.AndroidJUnit4;
41 
42 import com.android.settings.network.CarrierConfigCache;
43 import com.android.settings.testutils.FakeFeatureFactory;
44 import com.android.wifitrackerlib.MergedCarrierEntry;
45 import com.android.wifitrackerlib.WifiEntry;
46 import com.android.wifitrackerlib.WifiPickerTracker;
47 
48 import org.junit.Before;
49 import org.junit.Rule;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.mockito.Mock;
53 import org.mockito.Spy;
54 import org.mockito.junit.MockitoJUnit;
55 import org.mockito.junit.MockitoRule;
56 
57 @RunWith(AndroidJUnit4.class)
58 public class WifiPickerTrackerHelperTest {
59 
60     private static final int SUB_ID = 2;
61 
62     @Rule
63     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
64     @Spy
65     Context mContext = ApplicationProvider.getApplicationContext();
66     @Mock
67     public WifiManager mWifiManager;
68     @Mock
69     public CarrierConfigCache mCarrierConfigCache;
70     @Mock
71     public WifiPickerTracker mWifiPickerTracker;
72     @Mock
73     public MergedCarrierEntry mMergedCarrierEntry;
74     @Mock
75     public WifiEntry.ConnectCallback mConnectCallback;
76 
77     private WifiPickerTrackerHelper mWifiPickerTrackerHelper;
78 
79     private FakeFeatureFactory mFeatureFactory;
80     private PersistableBundle mCarrierConfig;
81 
82     @Before
setUp()83     public void setUp() {
84         when(mContext.getSystemService(WifiManager.class)).thenReturn(mWifiManager);
85         mCarrierConfig = new PersistableBundle();
86         when(mCarrierConfigCache.getConfigForSubId(SUB_ID)).thenReturn(mCarrierConfig);
87         CarrierConfigCache.setTestInstance(mContext, mCarrierConfigCache);
88 
89         mFeatureFactory = FakeFeatureFactory.setupForTest();
90         when(mFeatureFactory.wifiTrackerLibProvider
91                 .createWifiPickerTracker(
92                         any(), any(), any(), any(), any(), anyLong(), anyLong(), any()))
93                 .thenReturn(mWifiPickerTracker);
94         mWifiPickerTrackerHelper = new WifiPickerTrackerHelper(mock(Lifecycle.class),
95                 mContext, null);
96         when(mWifiPickerTracker.getMergedCarrierEntry()).thenReturn(mMergedCarrierEntry);
97         mWifiPickerTrackerHelper.setWifiPickerTracker(mWifiPickerTracker);
98     }
99 
100     @Test
getWifiPickerTracker_returnNonNull()101     public void getWifiPickerTracker_returnNonNull() {
102         assertThat(mWifiPickerTrackerHelper.getWifiPickerTracker()).isNotNull();
103     }
104 
105     @Test
onDestroy_workerThreadQuit()106     public void onDestroy_workerThreadQuit() {
107         final HandlerThread workerThread = mock(HandlerThread.class);
108         mWifiPickerTrackerHelper.setWorkerThread(workerThread);
109 
110         mWifiPickerTrackerHelper.onDestroy();
111 
112         verify(workerThread).quit();
113     }
114 
115     @Test
isCarrierNetworkProvisionEnabled_getNullConfig_returnFalse()116     public void isCarrierNetworkProvisionEnabled_getNullConfig_returnFalse() {
117         when(mCarrierConfigCache.getConfigForSubId(SUB_ID)).thenReturn(null);
118 
119         assertThat(mWifiPickerTrackerHelper.isCarrierNetworkProvisionEnabled(SUB_ID)).isFalse();
120     }
121 
122     @Test
isCarrierNetworkProvisionEnabled_returnCorrect()123     public void isCarrierNetworkProvisionEnabled_returnCorrect() {
124         mCarrierConfig.putBoolean(
125                 CarrierConfigManager.KEY_CARRIER_PROVISIONS_WIFI_MERGED_NETWORKS_BOOL, true);
126 
127         assertThat(mWifiPickerTrackerHelper.isCarrierNetworkProvisionEnabled(SUB_ID)).isTrue();
128 
129         mCarrierConfig.putBoolean(
130                 CarrierConfigManager.KEY_CARRIER_PROVISIONS_WIFI_MERGED_NETWORKS_BOOL, false);
131 
132         assertThat(mWifiPickerTrackerHelper.isCarrierNetworkProvisionEnabled(SUB_ID)).isFalse();
133     }
134 
135     @Test
isCarrierNetworkEnabled_returnCorrect()136     public void isCarrierNetworkEnabled_returnCorrect() {
137         when(mMergedCarrierEntry.isEnabled()).thenReturn(true);
138 
139         assertThat(mWifiPickerTrackerHelper.isCarrierNetworkEnabled()).isTrue();
140 
141         when(mMergedCarrierEntry.isEnabled()).thenReturn(false);
142 
143         assertThat(mWifiPickerTrackerHelper.isCarrierNetworkEnabled()).isFalse();
144     }
145 
146     @Test
setCarrierNetworkEnabled_shouldSetCorrect()147     public void setCarrierNetworkEnabled_shouldSetCorrect() {
148         mWifiPickerTrackerHelper.setCarrierNetworkEnabled(true);
149 
150         verify(mMergedCarrierEntry).setEnabled(true);
151 
152         mWifiPickerTrackerHelper.setCarrierNetworkEnabled(false);
153 
154         verify(mMergedCarrierEntry).setEnabled(false);
155     }
156 
157     @Test
setCarrierNetworkEnabled_mergedCarrierEntryIsNull_shouldNotSet()158     public void setCarrierNetworkEnabled_mergedCarrierEntryIsNull_shouldNotSet() {
159         when(mWifiPickerTracker.getMergedCarrierEntry()).thenReturn(null);
160 
161         mWifiPickerTrackerHelper.setCarrierNetworkEnabled(true);
162 
163         verify(mMergedCarrierEntry, never()).setEnabled(true);
164 
165         mWifiPickerTrackerHelper.setCarrierNetworkEnabled(false);
166 
167         verify(mMergedCarrierEntry, never()).setEnabled(true);
168     }
169 
170     @Test
connectCarrierNetwork_returnTrueAndConnect()171     public void connectCarrierNetwork_returnTrueAndConnect() {
172         when(mMergedCarrierEntry.canConnect()).thenReturn(true);
173 
174         assertThat(mWifiPickerTrackerHelper.connectCarrierNetwork(mConnectCallback)).isTrue();
175         verify(mMergedCarrierEntry).connect(mConnectCallback);
176     }
177 
178     @Test
connectCarrierNetwork_mergedCarrierEntryIsNull_returnFalse()179     public void connectCarrierNetwork_mergedCarrierEntryIsNull_returnFalse() {
180         when(mWifiPickerTracker.getMergedCarrierEntry()).thenReturn(null);
181 
182         assertThat(mWifiPickerTrackerHelper.connectCarrierNetwork(mConnectCallback)).isFalse();
183     }
184 
185     @Test
connectCarrierNetwork_canConnectIsFalse_returnFalseAndNeverConnect()186     public void connectCarrierNetwork_canConnectIsFalse_returnFalseAndNeverConnect() {
187         when(mMergedCarrierEntry.canConnect()).thenReturn(false);
188 
189         assertThat(mWifiPickerTrackerHelper.connectCarrierNetwork(mConnectCallback)).isFalse();
190         verify(mMergedCarrierEntry, never()).connect(mConnectCallback);
191     }
192 
193     @Test
getCarrierNetworkLevel_mergedCarrierEntryIsNull_returnMinLevel()194     public void getCarrierNetworkLevel_mergedCarrierEntryIsNull_returnMinLevel() {
195         when(mWifiPickerTracker.getMergedCarrierEntry()).thenReturn(null);
196 
197         assertThat(mWifiPickerTrackerHelper.getCarrierNetworkLevel()).isEqualTo(WIFI_LEVEL_MIN);
198     }
199 
200     @Test
getCarrierNetworkLevel_getUnreachableLevel_returnMinLevel()201     public void getCarrierNetworkLevel_getUnreachableLevel_returnMinLevel() {
202         when(mMergedCarrierEntry.getLevel()).thenReturn(WIFI_LEVEL_UNREACHABLE);
203 
204         assertThat(mWifiPickerTrackerHelper.getCarrierNetworkLevel()).isEqualTo(WIFI_LEVEL_MIN);
205     }
206 
207     @Test
getCarrierNetworkLevel_getAvailableLevel_returnSameLevel()208     public void getCarrierNetworkLevel_getAvailableLevel_returnSameLevel() {
209         for (int level = WIFI_LEVEL_MIN; level <= WIFI_LEVEL_MAX; level++) {
210             when(mMergedCarrierEntry.getLevel()).thenReturn(level);
211 
212             assertThat(mWifiPickerTrackerHelper.getCarrierNetworkLevel()).isEqualTo(level);
213         }
214     }
215 }
216