• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 android.app.Instrumentation;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.net.ConnectivityManager;
25 import android.provider.Settings;
26 import android.util.Log;
27 import android.view.View;
28 import android.widget.Button;
29 
30 import androidx.fragment.app.Fragment;
31 import androidx.fragment.app.FragmentActivity;
32 import androidx.test.ext.junit.runners.AndroidJUnit4;
33 import androidx.test.filters.MediumTest;
34 import androidx.test.platform.app.InstrumentationRegistry;
35 import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
36 import androidx.test.runner.lifecycle.Stage;
37 
38 import com.android.settings.R;
39 import com.android.settings.Settings.NetworkProviderSettingsActivity;
40 import com.android.settings.fuelgauge.batterysaver.BatterySaverButtonPreferenceControllerComponentTest;
41 import com.android.settings.network.NetworkProviderSettings;
42 import com.android.settings.testutils.CommonUtils;
43 import com.android.settings.testutils.Constants;
44 import com.android.settings.testutils.UiUtils;
45 
46 import org.junit.Ignore;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 
50 import java.net.URL;
51 import java.util.List;
52 
53 @RunWith(AndroidJUnit4.class)
54 @MediumTest
55 /*
56 This test is just for demonstration purpose. For component tests, this approach is not recommended.
57 The reason why it is written this way is because the current Settings app wifi codes have tight
58 coupling with UI, so it's not easy to drive from API without binding the test deeply with the code.
59  */
60 public class WifiSettings2ActivityTest {
61     private static final String TAG =
62             BatterySaverButtonPreferenceControllerComponentTest.class.getSimpleName();
63     private final Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation();
64 
65     @Test @Ignore
test_connect_to_wifi()66     public void test_connect_to_wifi() throws Exception {
67         //For some reason the ActivityScenario gets null activity here
68         mInstrumentation.getTargetContext().startActivity(
69                 new Intent(Settings.ACTION_WIFI_SETTINGS).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
70         UiUtils.waitForActivitiesInStage(Constants.ACTIVITY_LAUNCH_WAIT_TIMEOUT, Stage.RESUMED);
71 
72         final NetworkProviderSettings[] settings = new NetworkProviderSettings[1];
73         mInstrumentation.runOnMainSync(() -> {
74             NetworkProviderSettingsActivity activity = (NetworkProviderSettingsActivity)
75                     ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(
76                             Stage.RESUMED).iterator().next();
77             settings[0] =
78                     (NetworkProviderSettings) activity.getSupportFragmentManager().getFragments()
79                             .get(0);
80         });
81 
82         //For some reason this view does not appear immediately after the fragment is resumed.
83         View root = settings[0].getView();
84         UiUtils.waitUntilCondition(Constants.VIEW_APPEAR_WAIT_MEDIUM_TIMEOUT,
85                 () -> root.findViewById(R.id.settings_button) != null);
86         View view = root.findViewById(R.id.settings_button);
87         view.callOnClick();
88 
89         UiUtils.waitForActivitiesInStage(Constants.ACTIVITY_LAUNCH_WAIT_TIMEOUT, Stage.RESUMED);
90         Button[] button = new Button[1];
91         mInstrumentation.runOnMainSync(() -> {
92             FragmentActivity activity =
93                     (FragmentActivity) ActivityLifecycleMonitorRegistry.getInstance()
94                             .getActivitiesInStage(Stage.RESUMED).iterator().next();
95             List<Fragment> fragments = activity.getSupportFragmentManager().getFragments();
96             Log.d(TAG, "fragment class is " + fragments.get(0).getClass());
97             button[0] = fragments.get(0).getView().findViewById(R.id.button3);
98         });
99 
100         //HttpURLConnection needs to run outside of main thread, so running it in the test thread
101         final URL url = new URL("https://www.google.net/");
102 
103         //Make sure the connectivity is available before disconnecting from wifi
104         assertThat(CommonUtils.connectToURL(url)).isTrue();
105 
106         //Disconnect from wifi
107         button[0].callOnClick();
108 
109         //Make sure the Internet connectivity is gone
110         assertThat(CommonUtils.connectToURL(url)).isFalse();
111 
112         //Connect to wifi
113         button[0].callOnClick();
114         ConnectivityManager manager =
115                 (ConnectivityManager) mInstrumentation.getTargetContext().getSystemService(
116                         Context.CONNECTIVITY_SERVICE);
117 
118         //For some reason I can't find a way to tell the time that the internet connectivity is
119         //actually available with the new, non-deprecated ways, so I still need to use this.
120         UiUtils.waitUntilCondition(Constants.WIFI_CONNECT_WAIT_TIMEOUT,
121                 () -> manager.getActiveNetworkInfo().isConnected());
122 
123         //Make sure the connectivity is back again
124         assertThat(CommonUtils.connectToURL(url)).isTrue();
125     }
126 }
127