Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 03f3d9cb authored by Weng Su's avatar Weng Su Committed by Android (Google) Code Review
Browse files

Merge "Check WiFi restrictions for WiFi QR code intent"

parents 1dcbabe1 26a0fdb4
Loading
Loading
Loading
Loading
+23 −1
Original line number Diff line number Diff line
@@ -18,11 +18,14 @@ package com.android.settings.wifi.dpp;

import android.app.settings.SettingsEnums;
import android.content.Intent;
import android.util.EventLog;
import android.util.Log;

import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.FragmentTransaction;

import com.android.settings.R;
import com.android.settingslib.wifi.WifiRestrictionsCache;

/**
 * To provision "this" device with specified Wi-Fi network.
@@ -37,6 +40,9 @@ public class WifiDppEnrolleeActivity extends WifiDppBaseActivity implements
    static final String ACTION_ENROLLEE_QR_CODE_SCANNER =
            "android.settings.WIFI_DPP_ENROLLEE_QR_CODE_SCANNER";

    @VisibleForTesting
    protected WifiRestrictionsCache mWifiRestrictionsCache;

    @Override
    public int getMetricsCategory() {
        return SettingsEnums.SETTINGS_WIFI_DPP_ENROLLEE;
@@ -50,6 +56,14 @@ public class WifiDppEnrolleeActivity extends WifiDppBaseActivity implements
            return;
        }

        if (!isWifiConfigAllowed()) {
            Log.e(TAG, "The user is not allowed to configure Wi-Fi.");
            finish();
            EventLog.writeEvent(0x534e4554, "202017876", getApplicationContext().getUserId(),
                    "The user is not allowed to configure Wi-Fi.");
            return;
        }

        switch (action) {
            case ACTION_ENROLLEE_QR_CODE_SCANNER:
                String ssid = intent.getStringExtra(WifiDppUtils.EXTRA_WIFI_SSID);
@@ -61,7 +75,15 @@ public class WifiDppEnrolleeActivity extends WifiDppBaseActivity implements
        }
    }

    private void showQrCodeScannerFragment(String ssid) {
    private boolean isWifiConfigAllowed() {
        if (mWifiRestrictionsCache == null) {
            mWifiRestrictionsCache = WifiRestrictionsCache.getInstance(getApplicationContext());
        }
        return mWifiRestrictionsCache.isConfigWifiAllowed();
    }

    @VisibleForTesting
    protected void showQrCodeScannerFragment(String ssid) {
        WifiDppQrCodeScannerFragment fragment =
                (WifiDppQrCodeScannerFragment) mFragmentManager.findFragmentByTag(
                        WifiDppUtils.TAG_FRAGMENT_QR_CODE_SCANNER);
+66 −0
Original line number Diff line number Diff line
@@ -16,16 +16,82 @@

package com.android.settings.wifi.dpp;

import static com.android.settings.wifi.dpp.WifiDppEnrolleeActivity.ACTION_ENROLLEE_QR_CODE_SCANNER;

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 android.content.Intent;

import com.android.settingslib.wifi.WifiRestrictionsCache;

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.Robolectric;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class WifiDppEnrolleeActivityTest {

    private static final String WIFI_SSID = "wifi-ssid";

    @Rule
    public final MockitoRule mMockitoRule = MockitoJUnit.rule();
    @Mock
    WifiRestrictionsCache mWifiRestrictionsCache;
    @Mock
    Intent mIntent;

    WifiDppEnrolleeActivity mActivity;

    @Before
    public void setUp() {
        when(mWifiRestrictionsCache.isConfigWifiAllowed()).thenReturn(true);
        when(mIntent.getAction()).thenReturn(ACTION_ENROLLEE_QR_CODE_SCANNER);
        when(mIntent.getStringExtra(WifiDppUtils.EXTRA_WIFI_SSID)).thenReturn(WIFI_SSID);

        mActivity = spy(Robolectric.setupActivity(WifiDppEnrolleeActivity.class));
        mActivity.mWifiRestrictionsCache = mWifiRestrictionsCache;
    }

    @Test
    public void launchActivity_noIntentAction_shouldNotFatalException() {
        WifiDppEnrolleeActivity wifiDppEnrolleeActivity =
                Robolectric.setupActivity(WifiDppEnrolleeActivity.class);
    }

    @Test
    public void handleIntent_noIntentAction_shouldFinish() {
        when(mIntent.getAction()).thenReturn(null);

        mActivity.handleIntent(mIntent);

        verify(mActivity).finish();
    }

    @Test
    public void handleIntent_notAllowedConfigWifi_shouldFinish() {
        when(mWifiRestrictionsCache.isConfigWifiAllowed()).thenReturn(false);

        mActivity.handleIntent(mIntent);

        verify(mActivity).finish();
    }

    @Test
    public void handleIntent_hasIntentDataAndAllowedConfigWifi_shouldShowFragment() {
        when(mWifiRestrictionsCache.isConfigWifiAllowed()).thenReturn(true);
        doNothing().when(mActivity).showQrCodeScannerFragment(WIFI_SSID);

        mActivity.handleIntent(mIntent);

        verify(mActivity).showQrCodeScannerFragment(WIFI_SSID);
    }
}