1 /*
2  * Copyright (C) 2017 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.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26 
27 import android.content.res.Resources;
28 import android.telephony.SubscriptionManager;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.MockitoAnnotations;
34 import org.robolectric.RobolectricTestRunner;
35 import org.robolectric.RuntimeEnvironment;
36 
37 @RunWith(RobolectricTestRunner.class)
38 public class CellularFallbackPreferenceControllerTest {
39     private static final String KEY_CELLULAR_FALLBACK = "wifi_cellular_data_fallback";
40 
41     private CellularFallbackPreferenceController mController;
42 
43     @Before
setUp()44     public void setUp() {
45         MockitoAnnotations.initMocks(this);
46 
47         mController = spy(new CellularFallbackPreferenceController(RuntimeEnvironment.application,
48                 KEY_CELLULAR_FALLBACK));
49     }
50 
51     @Test
isAvailable_invalidActiveSubscriptionId_shouldReturnFalse()52     public void isAvailable_invalidActiveSubscriptionId_shouldReturnFalse() {
53         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
54                 .when(mController).getActiveDataSubscriptionId();
55 
56         assertThat(mController.isAvailable()).isFalse();
57     }
58 
59     @Test
isAvailable_avoidBadWifiConfigIsFalse_shouldReturnTrue()60     public void isAvailable_avoidBadWifiConfigIsFalse_shouldReturnTrue() {
61         final Resources resources = mock(Resources.class);
62 
63         doReturn(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID)
64                 .when(mController).getActiveDataSubscriptionId();
65         doReturn(resources).when(mController).getResourcesForSubId(anyInt());
66         when(resources.getInteger(
67                 com.android.internal.R.integer.config_networkAvoidBadWifi))
68                 .thenReturn(0);
69 
70         assertThat(mController.isAvailable()).isTrue();
71     }
72 
73     @Test
isAvailable_avoidBadWifiConfigIsTrue_shouldReturnFalse()74     public void isAvailable_avoidBadWifiConfigIsTrue_shouldReturnFalse() {
75         final Resources resources = mock(Resources.class);
76 
77         doReturn(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID)
78                 .when(mController).getActiveDataSubscriptionId();
79         doReturn(resources).when(mController).getResourcesForSubId(anyInt());
80         when(resources.getInteger(
81                 com.android.internal.R.integer.config_networkAvoidBadWifi))
82                 .thenReturn(1);
83 
84         assertThat(mController.isAvailable()).isFalse();
85     }
86 }