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 17 package com.android.settings.wifi; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.Mockito.doReturn; 21 22 import android.content.Intent; 23 import android.net.wifi.WifiConfiguration; 24 25 import com.android.settings.testutils.SettingsRobolectricTestRunner; 26 import com.android.settings.testutils.shadow.SettingsShadowResources; 27 import com.android.settings.testutils.shadow.ShadowConnectivityManager; 28 import com.android.settings.testutils.shadow.ShadowWifiManager; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.Robolectric; 36 import org.robolectric.annotation.Config; 37 import org.robolectric.shadows.ShadowAlertDialog; 38 import org.robolectric.util.ReflectionHelpers; 39 40 @RunWith(SettingsRobolectricTestRunner.class) 41 @Config(shadows = { 42 SettingsShadowResources.SettingsShadowTheme.class, 43 ShadowConnectivityManager.class, 44 ShadowWifiManager.class 45 } 46 ) 47 public class WifiDialogActivityTest { 48 49 private static final String AP1_SSID = "\"ap1\""; 50 @Mock 51 private WifiConfigController mController; 52 53 @Before 54 public void setUp() { 55 MockitoAnnotations.initMocks(this); 56 57 WifiConfiguration wifiConfig = new WifiConfiguration(); 58 wifiConfig.SSID = AP1_SSID; 59 doReturn(wifiConfig).when(mController).getConfig(); 60 } 61 62 @Test 63 public void onSubmit_shouldConnectToNetwork() { 64 WifiDialogActivity activity = Robolectric.setupActivity(WifiDialogActivity.class); 65 WifiDialog dialog = (WifiDialog) ShadowAlertDialog.getLatestAlertDialog(); 66 assertThat(dialog).isNotNull(); 67 68 ReflectionHelpers.setField(dialog, "mController", mController); 69 70 activity.onSubmit(dialog); 71 72 assertThat(ShadowWifiManager.get().savedWifiConfig.SSID).isEqualTo(AP1_SSID); 73 } 74 75 @Test 76 public void onSubmit_shouldNotConnectToNetwork_whenConnectForCallerIsFalse() { 77 WifiDialogActivity activity = 78 Robolectric.buildActivity( 79 WifiDialogActivity.class, 80 new Intent().putExtra(WifiDialogActivity.KEY_CONNECT_FOR_CALLER, false)) 81 .setup().get(); 82 WifiDialog dialog = (WifiDialog) ShadowAlertDialog.getLatestAlertDialog(); 83 assertThat(dialog).isNotNull(); 84 85 ReflectionHelpers.setField(dialog, "mController", mController); 86 87 activity.onSubmit(dialog); 88 89 assertThat(ShadowWifiManager.get().savedWifiConfig).isNull(); 90 } 91 } 92