From cf57eb7558ad07b4fff11d1be0a52d16fdeb4593 Mon Sep 17 00:00:00 2001 From: cosmohsieh Date: Wed, 7 Nov 2018 13:55:24 +0800 Subject: [PATCH] Implement NetworkRequestDialog architecture(cont.) For NetworkRequestTimeoutDialogFragment, the dialog show after WiFi scanning timeout or get no WiFi ap to list. Dialog will ask if user want to continue scanning or cancel. Bug: 117399926 Test: RunSettingsRoboTests Change-Id: I0551a753c10265e69a7830833813852a95eab5ef --- res/values/strings.xml | 4 + .../wifi/NetworkRequestDialogFragment.java | 52 +++++++++-- .../NetworkRequestTimeoutDialogFragment.java | 66 ++++++++++++++ .../NetworkRequestDialogFragmentTest.java | 27 +++++- ...tworkRequestTimeoutDialogFragmentTest.java | 90 +++++++++++++++++++ 5 files changed, 231 insertions(+), 8 deletions(-) create mode 100644 src/com/android/settings/wifi/NetworkRequestTimeoutDialogFragment.java create mode 100644 tests/robotests/src/com/android/settings/wifi/NetworkRequestTimeoutDialogFragmentTest.java diff --git a/res/values/strings.xml b/res/values/strings.xml index f30c63d2f80..969353e703e 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -10266,6 +10266,10 @@ Choose device + + No devices found. Make sure the device is turned on and available to connect. + + Scan again %1$d device connected diff --git a/src/com/android/settings/wifi/NetworkRequestDialogFragment.java b/src/com/android/settings/wifi/NetworkRequestDialogFragment.java index 9aac5090e92..c58ff579110 100644 --- a/src/com/android/settings/wifi/NetworkRequestDialogFragment.java +++ b/src/com/android/settings/wifi/NetworkRequestDialogFragment.java @@ -19,6 +19,8 @@ package com.android.settings.wifi; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; +import android.os.Handler; +import android.os.Message; import androidx.appcompat.app.AlertDialog; import android.os.Bundle; import android.view.LayoutInflater; @@ -37,14 +39,16 @@ import java.util.List; public class NetworkRequestDialogFragment extends InstrumentedDialogFragment implements DialogInterface.OnClickListener { + /** Message sent to us to stop scanning wifi and pop up timeout dialog. */ + private static final int MESSAGE_STOP_SCAN_WIFI_LIST = 0; + + /** Delayed time to stop scanning wifi. */ + private static final int DELAY_TIME_STOP_SCAN_MS = 30*1000; + private List mAccessPointList; - public static NetworkRequestDialogFragment newInstance(int uid, String packageName) { - Bundle args = new Bundle(); - args.putInt("uid", uid); - args.putString("packageName", packageName); + public static NetworkRequestDialogFragment newInstance() { NetworkRequestDialogFragment dialogFragment = new NetworkRequestDialogFragment(); - dialogFragment.setArguments(args); return dialogFragment; } @@ -84,6 +88,44 @@ public class NetworkRequestDialogFragment extends InstrumentedDialogFragment imp public void onClick(DialogInterface dialog, int which) { } + @Override + public void onPause() { + super.onPause(); + + mHandler.removeMessages(MESSAGE_STOP_SCAN_WIFI_LIST); + } + + @Override + public void onResume() { + super.onResume(); + + // TODO(b/117399926): Starts to scan current WiFi. + + // Sets time-out to stop scanning. + mHandler.sendEmptyMessageDelayed(MESSAGE_STOP_SCAN_WIFI_LIST, DELAY_TIME_STOP_SCAN_MS); + } + + private Handler mHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case MESSAGE_STOP_SCAN_WIFI_LIST: + removeMessages(MESSAGE_STOP_SCAN_WIFI_LIST); + stopScanningAndPopTimeoutDialog(); + break; + default: + // Do nothing. + break; + } + } + }; + + protected void stopScanningAndPopTimeoutDialog() { + dismiss(); + NetworkRequestTimeoutDialogFragment fragment = NetworkRequestTimeoutDialogFragment.newInstance(); + fragment.show(getActivity().getSupportFragmentManager(), null); + } + @Override public int getMetricsCategory() { return MetricsProto.MetricsEvent.WIFI_SCANNING_NEEDED_DIALOG; diff --git a/src/com/android/settings/wifi/NetworkRequestTimeoutDialogFragment.java b/src/com/android/settings/wifi/NetworkRequestTimeoutDialogFragment.java new file mode 100644 index 00000000000..08f285b0b8c --- /dev/null +++ b/src/com/android/settings/wifi/NetworkRequestTimeoutDialogFragment.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2018 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 android.app.Dialog; +import android.content.DialogInterface; +import android.os.Bundle; +import androidx.appcompat.app.AlertDialog; +import com.android.internal.logging.nano.MetricsProto; +import com.android.settings.R; +import com.android.settings.core.instrumentation.InstrumentedDialogFragment; + +public class NetworkRequestTimeoutDialogFragment extends InstrumentedDialogFragment implements + DialogInterface.OnClickListener { + + public static NetworkRequestTimeoutDialogFragment newInstance() { + NetworkRequestTimeoutDialogFragment fragment = new NetworkRequestTimeoutDialogFragment(); + return fragment; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getContext()) + .setMessage(R.string.network_connection_timeout_dialog_message) + .setPositiveButton(R.string.network_connection_timeout_dialog_ok, this) + .setNegativeButton(R.string.cancel, null); + return builder.create(); + } + + @Override + public int getMetricsCategory() { + return MetricsProto.MetricsEvent.WIFI_SCANNING_NEEDED_DIALOG; + } + + @Override + public void onClick(DialogInterface dialog, int which) { + switch (which) { + case DialogInterface.BUTTON_POSITIVE: + startScanningDialog(); + break; + case DialogInterface.BUTTON_NEGATIVE: + default: + // Do nothing. + break; + } + } + + protected void startScanningDialog() { + NetworkRequestDialogFragment fragment = NetworkRequestDialogFragment.newInstance(); + fragment.show(getActivity().getSupportFragmentManager(), null); + } +} diff --git a/tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java b/tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java index f987f404021..17718a692c7 100644 --- a/tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java +++ b/tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java @@ -18,8 +18,6 @@ package com.android.settings.wifi; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; - import android.content.DialogInterface; import android.widget.Button; import androidx.appcompat.app.AlertDialog; @@ -33,6 +31,7 @@ import org.junit.runner.RunWith; import org.robolectric.Robolectric; import org.robolectric.annotation.Config; import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; +import org.robolectric.shadows.ShadowLooper; @RunWith(SettingsRobolectricTestRunner.class) @Config(shadows = {SettingsShadowResourcesImpl.class, ShadowAlertDialogCompat.class}) @@ -44,7 +43,7 @@ public class NetworkRequestDialogFragmentTest { @Before public void setUp() { mActivity = Robolectric.setupActivity(FragmentActivity.class); - networkRequestDialogFragment = spy(NetworkRequestDialogFragment.newInstance(-1, null)); + networkRequestDialogFragment = spy(NetworkRequestDialogFragment.newInstance()); } @Test @@ -67,4 +66,26 @@ public class NetworkRequestDialogFragmentTest { positiveButton.performClick(); assertThat(alertDialog.isShowing()).isFalse(); } + + @Test + public void onResumeAndWaitTimeout_shouldCallTimeoutDialog() { + FakeNetworkRequestDialogFragment fakeFragment = new FakeNetworkRequestDialogFragment(); + FakeNetworkRequestDialogFragment spyFakeFragment = spy(fakeFragment); + spyFakeFragment.show(mActivity.getSupportFragmentManager(), null); + + assertThat(fakeFragment.bCalledStopAndPop).isFalse(); + + ShadowLooper.getShadowMainLooper().runToEndOfTasks(); + + assertThat(fakeFragment.bCalledStopAndPop).isTrue(); + } + + class FakeNetworkRequestDialogFragment extends NetworkRequestDialogFragment { + boolean bCalledStopAndPop = false; + + @Override + public void stopScanningAndPopTimeoutDialog() { + bCalledStopAndPop = true; + } + } } diff --git a/tests/robotests/src/com/android/settings/wifi/NetworkRequestTimeoutDialogFragmentTest.java b/tests/robotests/src/com/android/settings/wifi/NetworkRequestTimeoutDialogFragmentTest.java new file mode 100644 index 00000000000..ed28e60c0a1 --- /dev/null +++ b/tests/robotests/src/com/android/settings/wifi/NetworkRequestTimeoutDialogFragmentTest.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2018 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.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.internal.verification.VerificationModeFactory.times; + +import android.content.DialogInterface; +import android.widget.Button; +import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.FragmentActivity; +import com.android.settings.R; +import com.android.settings.testutils.SettingsRobolectricTestRunner; +import com.android.settings.testutils.shadow.SettingsShadowResourcesImpl; +import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; +import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(shadows = {SettingsShadowResourcesImpl.class, ShadowAlertDialogCompat.class}) +public class NetworkRequestTimeoutDialogFragmentTest { + + private FragmentActivity mActivity; + private NetworkRequestTimeoutDialogFragment mFragment; + + @Before + public void setUp() { + mActivity = Robolectric.setupActivity(FragmentActivity.class); + mFragment = spy(NetworkRequestTimeoutDialogFragment.newInstance()); + mFragment.show(mActivity.getSupportFragmentManager(), null); + } + + @Test + public void display_shouldShowTheDialog() { + AlertDialog alertDialog = ShadowAlertDialogCompat.getLatestAlertDialog(); + + assertThat(alertDialog).isNotNull(); + assertThat(alertDialog.isShowing()).isTrue(); + + ShadowAlertDialogCompat shadowAlertDialog = ShadowAlertDialogCompat.shadowOf(alertDialog); + assertThat(RuntimeEnvironment.application + .getString(R.string.network_connection_timeout_dialog_message)) + .isEqualTo(shadowAlertDialog.getMessage()); + } + + @Test + public void clickPositiveButton_shouldCallStartScanningDialog() { + AlertDialog alertDialog = ShadowAlertDialogCompat.getLatestAlertDialog(); + assertThat(alertDialog.isShowing()).isTrue(); + + Button positiveButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE); + assertThat(positiveButton).isNotNull(); + + positiveButton.performClick(); + verify(mFragment, times(1)).startScanningDialog(); + } + + @Test + public void clickNegativeButton_shouldCloseTheDialog() { + AlertDialog alertDialog = ShadowAlertDialogCompat.getLatestAlertDialog(); + assertThat(alertDialog.isShowing()).isTrue(); + + Button negativeButton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE); + assertThat(negativeButton).isNotNull(); + + negativeButton.performClick(); + assertThat(alertDialog.isShowing()).isFalse(); + } +}