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 
21 import static org.mockito.Mockito.doReturn;
22 
23 import android.content.Intent;
24 import android.net.wifi.WifiConfiguration;
25 
26 import com.android.settings.R;
27 import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
28 import com.android.settings.testutils.shadow.ShadowConnectivityManager;
29 import com.android.settings.testutils.shadow.ShadowWifiManager;
30 
31 import com.google.android.setupcompat.util.WizardManagerHelper;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.robolectric.Robolectric;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.annotation.Config;
41 import org.robolectric.util.ReflectionHelpers;
42 
43 @RunWith(RobolectricTestRunner.class)
44 @Config(shadows = {
45         ShadowConnectivityManager.class,
46         ShadowWifiManager.class,
47         ShadowAlertDialogCompat.class
48 })
49 public class WifiDialogActivityTest {
50 
51     private static final String AP1_SSID = "\"ap1\"";
52     @Mock
53     private WifiConfigController mController;
54 
55     @Before
setUp()56     public void setUp() {
57         MockitoAnnotations.initMocks(this);
58 
59         WifiConfiguration wifiConfig = new WifiConfiguration();
60         wifiConfig.SSID = AP1_SSID;
61         doReturn(wifiConfig).when(mController).getConfig();
62     }
63 
64     @Test
onSubmit_shouldConnectToNetwork()65     public void onSubmit_shouldConnectToNetwork() {
66         WifiDialogActivity activity = Robolectric.setupActivity(WifiDialogActivity.class);
67         WifiDialog dialog = (WifiDialog) ShadowAlertDialogCompat.getLatestAlertDialog();
68         assertThat(dialog).isNotNull();
69 
70         ReflectionHelpers.setField(dialog, "mController", mController);
71 
72         activity.onSubmit(dialog);
73 
74         assertThat(ShadowWifiManager.get().savedWifiConfig.SSID).isEqualTo(AP1_SSID);
75     }
76 
77     @Test
onSubmit_whenConnectForCallerIsFalse_shouldNotConnectToNetwork()78     public void onSubmit_whenConnectForCallerIsFalse_shouldNotConnectToNetwork() {
79         WifiDialogActivity activity =
80                 Robolectric.buildActivity(
81                         WifiDialogActivity.class,
82                         new Intent().putExtra(WifiDialogActivity.KEY_CONNECT_FOR_CALLER, false))
83                         .setup().get();
84         WifiDialog dialog = (WifiDialog) ShadowAlertDialogCompat.getLatestAlertDialog();
85 
86         assertThat(dialog).isNotNull();
87 
88         ReflectionHelpers.setField(dialog, "mController", mController);
89 
90         activity.onSubmit(dialog);
91 
92         assertThat(ShadowWifiManager.get().savedWifiConfig).isNull();
93     }
94 
95     @Test
onSubmit_whenLaunchInSetupFlow_shouldBeLightThemeForWifiDialog()96     public void onSubmit_whenLaunchInSetupFlow_shouldBeLightThemeForWifiDialog() {
97         WifiDialogActivity activity =
98                 Robolectric.buildActivity(
99                         WifiDialogActivity.class,
100                         new Intent()
101                                 .putExtra(WifiDialogActivity.KEY_CONNECT_FOR_CALLER, false)
102                                 .putExtra(WizardManagerHelper.EXTRA_IS_FIRST_RUN, true)
103                                 .putExtra(WizardManagerHelper.EXTRA_IS_SETUP_FLOW, true))
104                         .setup().get();
105         WifiDialog dialog = (WifiDialog) ShadowAlertDialogCompat.getLatestAlertDialog();
106 
107         assertThat(dialog).isNotNull();
108 
109         activity.onSubmit(dialog);
110 
111         assertThat(dialog.getContext().getThemeResId())
112                 .isEqualTo(R.style.SuwAlertDialogThemeCompat_Light);
113     }
114 }
115