1 /*
2  * Copyright (C) 2022 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.any;
22 import static org.mockito.Mockito.never;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.content.Context;
28 
29 import androidx.fragment.app.Fragment;
30 import androidx.test.core.app.ApplicationProvider;
31 
32 import com.android.wifitrackerlib.WifiEntry;
33 
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.Spy;
40 import org.mockito.junit.MockitoJUnit;
41 import org.mockito.junit.MockitoRule;
42 import org.robolectric.RobolectricTestRunner;
43 import org.robolectric.annotation.Config;
44 
45 @RunWith(RobolectricTestRunner.class)
46 @Config(shadows = ShadowRestrictedPreference.class)
47 public class LongPressWifiEntryPreferenceTest {
48 
49     @Rule
50     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
51     @Spy
52     Context mContext = ApplicationProvider.getApplicationContext();
53     @Mock
54     Fragment mFragment;
55     @Mock
56     WifiEntry mWifiEntry;
57 
58     LongPressWifiEntryPreference mPreference;
59 
60     @Before
setUp()61     public void setUp() {
62         // Fake mWifiEntry as an available Wi-Fi network, and it's not connected.
63         when(mWifiEntry.canConnect()).thenReturn(true);
64         when(mWifiEntry.canDisconnect()).thenReturn(false);
65         when(mWifiEntry.isSaved()).thenReturn(false);
66 
67         mPreference = spy(new LongPressWifiEntryPreference(mContext, mWifiEntry, mFragment));
68     }
69 
70     @Test
shouldEnabled_canConnect_returnTrue()71     public void shouldEnabled_canConnect_returnTrue() {
72         // Fake mWifiEntry as an available Wi-Fi network, and it's not connected.
73         when(mWifiEntry.canConnect()).thenReturn(true);
74 
75         assertThat(mPreference.shouldEnabled()).isTrue();
76     }
77 
78     @Test
shouldEnabled_canNotConnect_returnFalse()79     public void shouldEnabled_canNotConnect_returnFalse() {
80         // Fake mWifiEntry as a restricted Wi-Fi network, and cannot connect.
81         when(mWifiEntry.canConnect()).thenReturn(false);
82 
83         assertThat(mPreference.shouldEnabled()).isFalse();
84     }
85 
86     @Test
shouldEnabled_canNotConnectButCanDisconnect_returnTrue()87     public void shouldEnabled_canNotConnectButCanDisconnect_returnTrue() {
88         // Fake mWifiEntry as a connected Wi-Fi network without saved configuration.
89         when(mWifiEntry.canConnect()).thenReturn(false);
90         when(mWifiEntry.canDisconnect()).thenReturn(true);
91 
92         assertThat(mPreference.shouldEnabled()).isTrue();
93     }
94 
95     @Test
shouldEnabled_canNotConnectButIsSaved_returnTrue()96     public void shouldEnabled_canNotConnectButIsSaved_returnTrue() {
97         // Fake mWifiEntry as a saved Wi-Fi network
98         when(mWifiEntry.canConnect()).thenReturn(false);
99         when(mWifiEntry.isSaved()).thenReturn(true);
100 
101         assertThat(mPreference.shouldEnabled()).isTrue();
102     }
103 
104     @Test
shouldEnabled_canNotConnectButCanDisconnectAndIsSaved_returnTrue()105     public void shouldEnabled_canNotConnectButCanDisconnectAndIsSaved_returnTrue() {
106         // Fake mWifiEntry as a connected Wi-Fi network
107         when(mWifiEntry.canConnect()).thenReturn(false);
108         when(mWifiEntry.canDisconnect()).thenReturn(true);
109         when(mWifiEntry.isSaved()).thenReturn(true);
110 
111         assertThat(mPreference.shouldEnabled()).isTrue();
112     }
113 
114     @Test
checkRestrictionAndSetDisabled_hasAdminRestrictions_doSetDisabledByAdmin()115     public void checkRestrictionAndSetDisabled_hasAdminRestrictions_doSetDisabledByAdmin() {
116         when(mContext.getUser()).thenReturn(null);
117         when(mWifiEntry.hasAdminRestrictions()).thenReturn(true);
118 
119         mPreference.checkRestrictionAndSetDisabled();
120 
121         verify(mPreference).setDisabledByAdmin(any());
122     }
123 
124     @Test
checkRestrictionAndSetDisabled_noAdminRestrictions_doNotSetDisabledByAdmin()125     public void checkRestrictionAndSetDisabled_noAdminRestrictions_doNotSetDisabledByAdmin() {
126         when(mWifiEntry.hasAdminRestrictions()).thenReturn(false);
127 
128         mPreference.checkRestrictionAndSetDisabled();
129 
130         verify(mPreference, never()).setDisabledByAdmin(any());
131     }
132 }
133