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
17package com.android.settings.wifi;
18
19import android.content.Context;
20import android.view.LayoutInflater;
21import android.view.View;
22import android.view.ViewGroup;
23import android.widget.Spinner;
24import android.widget.TextView;
25
26import com.android.settings.R;
27import com.android.settings.testutils.SettingsRobolectricTestRunner;
28import com.android.settings.TestConfig;
29import com.android.settings.testutils.shadow.ShadowConnectivityManager;
30import com.android.settingslib.wifi.AccessPoint;
31
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.mockito.Mock;
36import org.mockito.MockitoAnnotations;
37import org.robolectric.RuntimeEnvironment;
38import org.robolectric.annotation.Config;
39
40import static com.google.common.truth.Truth.assertThat;
41import static org.mockito.Mockito.*;
42
43@RunWith(SettingsRobolectricTestRunner.class)
44@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
45        shadows = ShadowConnectivityManager.class)
46public class WifiConfigControllerTest {
47
48    @Mock
49    private WifiConfigUiBase mConfigUiBase;
50    @Mock
51    private Context mContext;
52    @Mock
53    private View mView;
54    @Mock
55    private AccessPoint mAccessPoint;
56
57    public WifiConfigController mController;
58    private static final String HEX_PSK = "01234567012345670123456701234567012345670123456701234567"
59            + "01abcdef";
60    // An invalid ASCII PSK pass phrase. It is 64 characters long, must not be greater than 63
61    private static final String LONG_PSK =
62            "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl";
63    // An invalid PSK pass phrase. It is 7 characters long, must be at least 8
64    private static final String SHORT_PSK = "abcdefg";
65    // Valid PSK pass phrase
66    private static final String GOOD_PSK = "abcdefghijklmnopqrstuvwxyz";
67    private static final int DHCP = 0;
68
69    @Before
70    public void setUp() {
71        MockitoAnnotations.initMocks(this);
72        mContext = RuntimeEnvironment.application;
73        when(mConfigUiBase.getContext()).thenReturn(mContext);
74        when(mAccessPoint.getSecurity()).thenReturn(AccessPoint.SECURITY_PSK);
75        mView = LayoutInflater.from(mContext).inflate(R.layout.wifi_dialog, null);
76        final Spinner ipSettingsSpinner = mView.findViewById(R.id.ip_settings);
77        ipSettingsSpinner.setSelection(DHCP);
78
79        mController = new TestWifiConfigController(mConfigUiBase, mView, mAccessPoint,
80                WifiConfigUiBase.MODE_CONNECT);
81    }
82
83    @Test
84    public void ssidExceeds32Bytes_shouldShowSsidTooLongWarning() {
85        mController = new TestWifiConfigController(mConfigUiBase, mView, null /* accessPoint */,
86                WifiConfigUiBase.MODE_CONNECT);
87        final TextView ssid = mView.findViewById(R.id.ssid);
88        ssid.setText("☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎");
89        mController.showWarningMessagesIfAppropriate();
90
91        assertThat(mView.findViewById(R.id.ssid_too_long_warning).getVisibility())
92                .isEqualTo(View.VISIBLE);
93    }
94
95    @Test
96    public void ssidShorterThan32Bytes_shouldNotShowSsidTooLongWarning() {
97        mController = new TestWifiConfigController(mConfigUiBase, mView, null /* accessPoint */,
98                WifiConfigUiBase.MODE_CONNECT);
99
100        final TextView ssid = mView.findViewById(R.id.ssid);
101        ssid.setText("123456789012345678901234567890");
102        mController.showWarningMessagesIfAppropriate();
103
104        assertThat(mView.findViewById(R.id.ssid_too_long_warning).getVisibility())
105                .isEqualTo(View.GONE);
106
107        ssid.setText("123");
108        mController.showWarningMessagesIfAppropriate();
109
110        assertThat(mView.findViewById(R.id.ssid_too_long_warning).getVisibility())
111                .isEqualTo(View.GONE);
112    }
113
114    @Test
115    public void isSubmittable_noSSID_shouldReturnFalse() {
116        final TextView ssid = mView.findViewById(R.id.ssid);
117        ssid.setText("");
118        assertThat(mController.isSubmittable()).isFalse();
119    }
120
121    @Test
122    public void isSubmittable_longPsk_shouldReturnFalse() {
123        final TextView password = mView.findViewById(R.id.password);
124        password.setText(LONG_PSK);
125        assertThat(mController.isSubmittable()).isFalse();
126
127    }
128
129    @Test
130    public void isSubmittable_shortPsk_shouldReturnFalse() {
131        final TextView password = mView.findViewById(R.id.password);
132        password.setText(SHORT_PSK);
133        assertThat(mController.isSubmittable()).isFalse();
134    }
135
136    @Test
137    public void isSubmittable_goodPsk_shouldReturnTrue() {
138        final TextView password = mView.findViewById(R.id.password);
139        password.setText(GOOD_PSK);
140        assertThat(mController.isSubmittable()).isTrue();
141
142    }
143
144    @Test
145    public void isSubmittable_hexPsk_shouldReturnTrue() {
146        final TextView password = mView.findViewById(R.id.password);
147        password.setText(HEX_PSK);
148        assertThat(mController.isSubmittable()).isTrue();
149
150    }
151
152    @Test
153    public void isSubmittable_savedConfigZeroLengthPassword_shouldReturnTrue() {
154        final TextView password = mView.findViewById(R.id.password);
155        password.setText("");
156        when(mAccessPoint.isSaved()).thenReturn(true);
157        assertThat(mController.isSubmittable()).isTrue();
158    }
159
160    @Test
161    public void isSubmittable_nullAccessPoint_noException() {
162        mController = new TestWifiConfigController(mConfigUiBase, mView, null,
163                WifiConfigUiBase.MODE_CONNECT);
164        mController.isSubmittable();
165    }
166
167    @Test
168    public void getSignalString_notReachable_shouldHaveNoSignalString() {
169        when(mAccessPoint.isReachable()).thenReturn(false);
170
171        assertThat(mController.getSignalString()).isNull();
172    }
173
174    @Test
175    public void showForCarrierAp() {
176        // Setup the mock view for wifi dialog.
177        View view = mock(View.class);
178        TextView nameText = mock(TextView.class);
179        TextView valueText = mock(TextView.class);
180        when(view.findViewById(R.id.name)).thenReturn(nameText);
181        when(view.findViewById(R.id.value)).thenReturn(valueText);
182        LayoutInflater inflater = mock(LayoutInflater.class);
183        when(inflater.inflate(anyInt(), any(ViewGroup.class), anyBoolean())).thenReturn(view);
184        when(mConfigUiBase.getLayoutInflater()).thenReturn(inflater);
185
186        String carrierName = "Test Carrier";
187        when(mAccessPoint.isCarrierAp()).thenReturn(true);
188        when(mAccessPoint.getCarrierName()).thenReturn(carrierName);
189        mController = new TestWifiConfigController(mConfigUiBase, mView, mAccessPoint,
190                WifiConfigUiBase.MODE_CONNECT);
191        // Verify the content of the text fields.
192        verify(nameText).setText(R.string.wifi_carrier_connect);
193        verify(valueText).setText(
194                String.format(mContext.getString(R.string.wifi_carrier_content), carrierName));
195        // Verify that the advance toggle is not visible.
196        assertThat(mView.findViewById(R.id.wifi_advanced_toggle).getVisibility())
197                .isEqualTo(View.GONE);
198        // Verify that the EAP method menu is not visible.
199        assertThat(mView.findViewById(R.id.eap).getVisibility()).isEqualTo(View.GONE);
200    }
201
202    public class TestWifiConfigController extends WifiConfigController {
203
204        public TestWifiConfigController(WifiConfigUiBase parent, View view,
205                AccessPoint accessPoint, int mode) {
206            super(parent, view, accessPoint, mode);
207        }
208
209        @Override
210        boolean isSplitSystemUser() {
211            return false;
212        }
213    }
214}
215