Merge "Implement NetworkRequestDialog architecture(cont.)"

This commit is contained in:
Cosmo Hsieh
2018-11-14 01:43:47 +00:00
committed by Android (Google) Code Review
5 changed files with 231 additions and 8 deletions

View File

@@ -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;
}
}
}

View File

@@ -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();
}
}