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

Commit 44ce407d authored by Maurice Lam's avatar Maurice Lam
Browse files

Add flag to not connect in WifiDialogActivity

Add flag connect_for_caller, which if false will not connect to the
Wi-Fi network on behalf of the caller.

Test: cd tests/robotests && mma
Bug: 34951731
Change-Id: Ie24a8ed1891aea1caec75a2de9a3d2bdb3dad8a7
parent 8277e3ce
Loading
Loading
Loading
Loading
+26 −12
Original line number Diff line number Diff line
@@ -22,7 +22,9 @@ import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.ActionListener;
import android.os.Bundle;
import android.support.annotation.VisibleForTesting;
import android.util.Log;

import com.android.settings.SetupWizardUtils;
@@ -40,6 +42,15 @@ public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialo
    private static final String KEY_ACCESS_POINT_STATE = "access_point_state";
    private static final String KEY_WIFI_CONFIGURATION = "wifi_configuration";

    /**
     * Boolean extra indicating whether this activity should connect to an access point on the
     * caller's behalf. If this is set to false, the caller should check
     * {@link #KEY_WIFI_CONFIGURATION} in the result data and save that using
     * {@link WifiManager#connect(WifiConfiguration, ActionListener)}. Default is true.
     */
    @VisibleForTesting
    static final String KEY_CONNECT_FOR_CALLER = "connect_for_caller";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final Intent intent = getIntent();
@@ -55,7 +66,8 @@ public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialo
            accessPoint = new AccessPoint(this, accessPointState);
        }

        WifiDialog dialog = WifiDialog.createModal(this, this, accessPoint, WifiConfigUiBase.MODE_CONNECT);
        WifiDialog dialog = WifiDialog.createModal(
                this, this, accessPoint, WifiConfigUiBase.MODE_CONNECT);
        dialog.show();
        dialog.setOnDismissListener(this);
    }
@@ -102,6 +114,7 @@ public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialo
        final AccessPoint accessPoint = dialog.getController().getAccessPoint();
        final WifiManager wifiManager = getSystemService(WifiManager.class);

        if (getIntent().getBooleanExtra(KEY_CONNECT_FOR_CALLER, true)) {
            if (config == null) {
                if (accessPoint != null && accessPoint.isSaved()) {
                    wifiManager.connect(accessPoint.getConfig(), null /* listener */);
@@ -116,6 +129,7 @@ public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialo
                    }
                }
            }
        }

        Intent resultData = new Intent();
        if (accessPoint != null) {
+8 −0
Original line number Diff line number Diff line
@@ -16,10 +16,18 @@

package com.android.settings;

import android.os.Build;

/**
 * Constants for Robolectric config
 */
public class TestConfig {

    /**
     * @deprecated New tests should use {@link #SDK_VERSION_O}
     */
    @Deprecated
    public static final int SDK_VERSION = 23;
    public static final int SDK_VERSION_O = Build.VERSION_CODES.O;
    public static final String MANIFEST_PATH = "packages/apps/Settings/AndroidManifest.xml";
}
+49 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.testutils.shadow;

import static org.robolectric.RuntimeEnvironment.application;

import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

import org.robolectric.annotation.HiddenApi;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadow.api.Shadow;

@Implements(WifiManager.class)
public class ShadowWifiManager extends org.robolectric.shadows.ShadowWifiManager {

    public WifiConfiguration savedWifiConfig;

    @HiddenApi // @SystemApi
    @Implementation
    public void connect(WifiConfiguration config, WifiManager.ActionListener listener) {
        savedWifiConfig = config;
    }

    @HiddenApi
    @Implementation
    public void save(WifiConfiguration config, WifiManager.ActionListener listener) {
        savedWifiConfig = config;
    }

    public static ShadowWifiManager get() {
        return Shadow.extract(application.getSystemService(WifiManager.class));
    }
}
+97 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.doReturn;

import android.content.Intent;
import android.net.wifi.WifiConfiguration;

import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import com.android.settings.testutils.shadow.ShadowConnectivityManager;
import com.android.settings.testutils.shadow.ShadowWifiManager;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowAlertDialog;
import org.robolectric.util.ReflectionHelpers;

@RunWith(SettingsRobolectricTestRunner.class)
@Config(
        manifest = TestConfig.MANIFEST_PATH,
        sdk = TestConfig.SDK_VERSION_O,
        shadows = {
                SettingsShadowResources.class,
                SettingsShadowResources.SettingsShadowTheme.class,
                ShadowConnectivityManager.class,
                ShadowWifiManager.class
        }
)
public class WifiDialogActivityTest {

    public static final String AP1_SSID = "\"ap1\"";
    @Mock
    private WifiConfigController mController;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        WifiConfiguration wifiConfig = new WifiConfiguration();
        wifiConfig.SSID = AP1_SSID;
        doReturn(wifiConfig).when(mController).getConfig();
    }

    @Test
    public void onSubmit_shouldConnectToNetwork() {
        WifiDialogActivity activity = Robolectric.setupActivity(WifiDialogActivity.class);
        WifiDialog dialog = (WifiDialog) ShadowAlertDialog.getLatestAlertDialog();
        assertThat(dialog).isNotNull();

        ReflectionHelpers.setField(dialog, "mController", mController);

        activity.onSubmit(dialog);

        assertThat(ShadowWifiManager.get().savedWifiConfig.SSID).isEqualTo(AP1_SSID);
    }

    @Test
    public void onSubmit_shouldNotConnectToNetwork_whenConnectForCallerIsFalse() {
        WifiDialogActivity activity =
                Robolectric.buildActivity(
                        WifiDialogActivity.class,
                        new Intent().putExtra(WifiDialogActivity.KEY_CONNECT_FOR_CALLER, false))
                .setup().get();
        WifiDialog dialog = (WifiDialog) ShadowAlertDialog.getLatestAlertDialog();
        assertThat(dialog).isNotNull();

        ReflectionHelpers.setField(dialog, "mController", mController);

        activity.onSubmit(dialog);

        assertThat(ShadowWifiManager.get().savedWifiConfig).isNull();
    }
}