Loading src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragment.java +23 −2 Original line number Diff line number Diff line Loading @@ -117,7 +117,8 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl private int mLatestStatusCode = WifiDppUtils.EASY_CONNECT_EVENT_FAILURE_NONE; private WifiPickerTracker mWifiPickerTracker; @VisibleForTesting WifiPickerTracker mWifiPickerTracker; private HandlerThread mWorkerThread; private final Handler mHandler = new Handler() { Loading Loading @@ -193,6 +194,9 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl if (id == -1) { continue; } if (!canConnectWifi(qrCodeWifiConfiguration.SSID)) return; wifiManager.enableNetwork(id, /* attemptConnect */ false); // WifiTracker only contains a hidden SSID Wi-Fi network if it's saved. // We can't check if a hidden SSID Wi-Fi network is reachable in advance. Loading Loading @@ -266,6 +270,21 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl return false; } @VisibleForTesting boolean canConnectWifi(String ssid) { final List<WifiEntry> wifiEntries = mWifiPickerTracker.getWifiEntries(); for (WifiEntry wifiEntry : wifiEntries) { if (!TextUtils.equals(wifiEntry.getSsid(), sanitizeSsid(ssid))) continue; if (!wifiEntry.canConnect()) { Log.w(TAG, "Wi-Fi is not allowed to connect by your organization. SSID:" + ssid); showErrorMessageAndRestartCamera(R.string.not_allowed_by_ent); return false; } } return true; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Loading Loading @@ -575,7 +594,8 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl message.sendToTarget(); } private void showErrorMessageAndRestartCamera(@StringRes int messageResId) { @VisibleForTesting void showErrorMessageAndRestartCamera(@StringRes int messageResId) { final Message message = mHandler.obtainMessage(MESSAGE_SHOW_ERROR_MESSAGE, getString(messageResId)); message.arg1 = ARG_RESTART_CAMERA; Loading Loading @@ -603,6 +623,7 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl if (wifiConfig.networkId == newNetworkId) { mLatestStatusCode = WifiDppUtils.EASY_CONNECT_EVENT_SUCCESS; mEnrolleeWifiConfiguration = wifiConfig; if (!canConnectWifi(wifiConfig.SSID)) return; wifiManager.connect(wifiConfig, WifiDppQrCodeScannerFragment.this); return; } Loading tests/robotests/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragmentTest.java 0 → 100644 +87 −0 Original line number Diff line number Diff line /* * Copyright (C) 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settings.wifi.dpp; import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import com.android.wifitrackerlib.WifiEntry; import com.android.wifitrackerlib.WifiPickerTracker; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; import org.robolectric.RobolectricTestRunner; import java.util.Arrays; @RunWith(RobolectricTestRunner.class) public class WifiDppQrCodeScannerFragmentTest { static final String WIFI_SSID = "wifi-ssid"; @Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule(); @Mock WifiPickerTracker mWifiPickerTracker; @Mock WifiEntry mWifiEntry; WifiDppQrCodeScannerFragment mFragment; @Before public void setUp() { when(mWifiEntry.getSsid()).thenReturn(WIFI_SSID); when(mWifiPickerTracker.getWifiEntries()).thenReturn(Arrays.asList(mWifiEntry)); mFragment = spy(new WifiDppQrCodeScannerFragment()); mFragment.mWifiPickerTracker = mWifiPickerTracker; } @Test public void canConnectWifi_noAvailableWifiMatch_returnTrue() { when(mWifiEntry.getSsid()).thenReturn("diff-wifi-ssid"); when(mWifiEntry.canConnect()).thenReturn(false); assertThat(mFragment.canConnectWifi(WIFI_SSID)).isTrue(); } @Test public void canConnectWifi_wifiCanConnect_returnTrue() { when(mWifiEntry.canConnect()).thenReturn(true); assertThat(mFragment.canConnectWifi(WIFI_SSID)).isTrue(); } @Test public void canConnectWifi_wifiCanNotConnect_returnFalseAndShowError() { when(mWifiEntry.canConnect()).thenReturn(false); doNothing().when(mFragment).showErrorMessageAndRestartCamera(anyInt()); assertThat(mFragment.canConnectWifi(WIFI_SSID)).isFalse(); verify(mFragment).showErrorMessageAndRestartCamera(anyInt()); } } Loading
src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragment.java +23 −2 Original line number Diff line number Diff line Loading @@ -117,7 +117,8 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl private int mLatestStatusCode = WifiDppUtils.EASY_CONNECT_EVENT_FAILURE_NONE; private WifiPickerTracker mWifiPickerTracker; @VisibleForTesting WifiPickerTracker mWifiPickerTracker; private HandlerThread mWorkerThread; private final Handler mHandler = new Handler() { Loading Loading @@ -193,6 +194,9 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl if (id == -1) { continue; } if (!canConnectWifi(qrCodeWifiConfiguration.SSID)) return; wifiManager.enableNetwork(id, /* attemptConnect */ false); // WifiTracker only contains a hidden SSID Wi-Fi network if it's saved. // We can't check if a hidden SSID Wi-Fi network is reachable in advance. Loading Loading @@ -266,6 +270,21 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl return false; } @VisibleForTesting boolean canConnectWifi(String ssid) { final List<WifiEntry> wifiEntries = mWifiPickerTracker.getWifiEntries(); for (WifiEntry wifiEntry : wifiEntries) { if (!TextUtils.equals(wifiEntry.getSsid(), sanitizeSsid(ssid))) continue; if (!wifiEntry.canConnect()) { Log.w(TAG, "Wi-Fi is not allowed to connect by your organization. SSID:" + ssid); showErrorMessageAndRestartCamera(R.string.not_allowed_by_ent); return false; } } return true; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Loading Loading @@ -575,7 +594,8 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl message.sendToTarget(); } private void showErrorMessageAndRestartCamera(@StringRes int messageResId) { @VisibleForTesting void showErrorMessageAndRestartCamera(@StringRes int messageResId) { final Message message = mHandler.obtainMessage(MESSAGE_SHOW_ERROR_MESSAGE, getString(messageResId)); message.arg1 = ARG_RESTART_CAMERA; Loading Loading @@ -603,6 +623,7 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl if (wifiConfig.networkId == newNetworkId) { mLatestStatusCode = WifiDppUtils.EASY_CONNECT_EVENT_SUCCESS; mEnrolleeWifiConfiguration = wifiConfig; if (!canConnectWifi(wifiConfig.SSID)) return; wifiManager.connect(wifiConfig, WifiDppQrCodeScannerFragment.this); return; } Loading
tests/robotests/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragmentTest.java 0 → 100644 +87 −0 Original line number Diff line number Diff line /* * Copyright (C) 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settings.wifi.dpp; import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import com.android.wifitrackerlib.WifiEntry; import com.android.wifitrackerlib.WifiPickerTracker; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; import org.robolectric.RobolectricTestRunner; import java.util.Arrays; @RunWith(RobolectricTestRunner.class) public class WifiDppQrCodeScannerFragmentTest { static final String WIFI_SSID = "wifi-ssid"; @Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule(); @Mock WifiPickerTracker mWifiPickerTracker; @Mock WifiEntry mWifiEntry; WifiDppQrCodeScannerFragment mFragment; @Before public void setUp() { when(mWifiEntry.getSsid()).thenReturn(WIFI_SSID); when(mWifiPickerTracker.getWifiEntries()).thenReturn(Arrays.asList(mWifiEntry)); mFragment = spy(new WifiDppQrCodeScannerFragment()); mFragment.mWifiPickerTracker = mWifiPickerTracker; } @Test public void canConnectWifi_noAvailableWifiMatch_returnTrue() { when(mWifiEntry.getSsid()).thenReturn("diff-wifi-ssid"); when(mWifiEntry.canConnect()).thenReturn(false); assertThat(mFragment.canConnectWifi(WIFI_SSID)).isTrue(); } @Test public void canConnectWifi_wifiCanConnect_returnTrue() { when(mWifiEntry.canConnect()).thenReturn(true); assertThat(mFragment.canConnectWifi(WIFI_SSID)).isTrue(); } @Test public void canConnectWifi_wifiCanNotConnect_returnFalseAndShowError() { when(mWifiEntry.canConnect()).thenReturn(false); doNothing().when(mFragment).showErrorMessageAndRestartCamera(anyInt()); assertThat(mFragment.canConnectWifi(WIFI_SSID)).isFalse(); verify(mFragment).showErrorMessageAndRestartCamera(anyInt()); } }