Merge "[Audiosharing] Use DialogFragment instead of raw AlertDialog" into main
This commit is contained in:
@@ -46,17 +46,21 @@ import android.os.Bundle;
|
||||
import android.os.Looper;
|
||||
import android.platform.test.flag.junit.SetFlagsRule;
|
||||
import android.util.Pair;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.DialogFragment;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
|
||||
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||
import com.android.settings.testutils.shadow.ShadowFragment;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
import com.android.settingslib.flags.Flags;
|
||||
@@ -73,8 +77,14 @@ import org.mockito.junit.MockitoRule;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.annotation.Implementation;
|
||||
import org.robolectric.annotation.Implements;
|
||||
import org.robolectric.annotation.RealObject;
|
||||
import org.robolectric.annotation.Resetter;
|
||||
import org.robolectric.shadow.api.Shadow;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/** Tests for {@link BluetoothDevicePairingDetailBase}. */
|
||||
@@ -82,7 +92,7 @@ import java.util.concurrent.Executor;
|
||||
@Config(shadows = {
|
||||
ShadowBluetoothAdapter.class,
|
||||
ShadowAlertDialogCompat.class,
|
||||
com.android.settings.testutils.shadow.ShadowFragment.class,
|
||||
ShadowFragment.class,
|
||||
})
|
||||
public class BluetoothDevicePairingDetailBaseTest {
|
||||
|
||||
@@ -133,7 +143,6 @@ public class BluetoothDevicePairingDetailBaseTest {
|
||||
mFragment.mLocalManager = mLocalManager;
|
||||
mFragment.mBluetoothAdapter = mBluetoothAdapter;
|
||||
mFragment.initPreferencesFromPreferenceScreen();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -199,22 +208,26 @@ public class BluetoothDevicePairingDetailBaseTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowDialogFragment.class)
|
||||
public void onDeviceBondStateChanged_bonded_pairAndJoinSharingEnabled_handle() {
|
||||
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
|
||||
ShadowDialogFragment.reset();
|
||||
when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
|
||||
mFragment.mSelectedList.add(mBluetoothDevice);
|
||||
setUpFragmentWithPairAndJoinSharingIntent(true);
|
||||
mFragment.onDeviceBondStateChanged(mCachedBluetoothDevice, BluetoothDevice.BOND_BONDED);
|
||||
shadowOf(Looper.getMainLooper()).idle();
|
||||
|
||||
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
|
||||
assertThat(dialog).isNotNull();
|
||||
TextView message = dialog.findViewById(R.id.message);
|
||||
assertThat(message).isNotNull();
|
||||
assertThat(message.getText().toString()).isEqualTo(
|
||||
ProgressDialogFragment progressDialog = mFragment.mProgressDialog;
|
||||
assertThat(progressDialog).isNotNull();
|
||||
assertThat(progressDialog.getMessage()).isEqualTo(
|
||||
mContext.getString(R.string.progress_dialog_connect_device_content,
|
||||
TEST_DEVICE_ADDRESS));
|
||||
assertThat(
|
||||
ShadowDialogFragment.isIsShowing(ProgressDialogFragment.class.getName())).isTrue();
|
||||
verify(mFragment, never()).finish();
|
||||
|
||||
ShadowDialogFragment.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -283,9 +296,11 @@ public class BluetoothDevicePairingDetailBaseTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowDialogFragment.class)
|
||||
public void
|
||||
onProfileConnectionStateChanged_deviceInSelectedListAndConnected_pairAndJoinSharing() {
|
||||
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
|
||||
ShadowDialogFragment.reset();
|
||||
when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
|
||||
mFragment.mSelectedList.add(mBluetoothDevice);
|
||||
setUpFragmentWithPairAndJoinSharingIntent(true);
|
||||
@@ -309,6 +324,8 @@ public class BluetoothDevicePairingDetailBaseTest {
|
||||
assertThat(btDevice).isNotNull();
|
||||
assertThat(btDevice).isEqualTo(mBluetoothDevice);
|
||||
verify(mFragment).finish();
|
||||
|
||||
ShadowDialogFragment.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -393,7 +410,13 @@ public class BluetoothDevicePairingDetailBaseTest {
|
||||
doReturn(intent).when(activity).getIntent();
|
||||
doReturn(activity).when(mFragment).getActivity();
|
||||
FragmentManager fragmentManager = mock(FragmentManager.class);
|
||||
FragmentTransaction fragmentTransaction = mock(FragmentTransaction.class);
|
||||
doReturn(fragmentTransaction).when(fragmentManager).beginTransaction();
|
||||
doReturn(fragmentManager).when(mFragment).getFragmentManager();
|
||||
doReturn(fragmentManager).when(mFragment).getChildFragmentManager();
|
||||
Lifecycle lifecycle = mock(Lifecycle.class);
|
||||
when(lifecycle.getCurrentState()).thenReturn(Lifecycle.State.RESUMED);
|
||||
doReturn(lifecycle).when(mFragment).getLifecycle();
|
||||
mFragment.mShouldTriggerAudioSharingShareThenPairFlow =
|
||||
mFragment.shouldTriggerAudioSharingShareThenPairFlow();
|
||||
}
|
||||
@@ -425,4 +448,41 @@ public class BluetoothDevicePairingDetailBaseTest {
|
||||
return "test_tag";
|
||||
}
|
||||
}
|
||||
|
||||
/** Shadow of DialogFragment. */
|
||||
@Implements(value = DialogFragment.class)
|
||||
public static class ShadowDialogFragment {
|
||||
@RealObject
|
||||
private DialogFragment mDialogFragment;
|
||||
private static Map<String, Boolean> sDialogStatus = new HashMap<>();
|
||||
|
||||
/** Resetter of the shadow. */
|
||||
@Resetter
|
||||
public static void reset() {
|
||||
sDialogStatus.clear();
|
||||
}
|
||||
|
||||
/** Implementation for DialogFragment#show. */
|
||||
@Implementation
|
||||
public void show(@NonNull FragmentManager manager, @Nullable String tag) {
|
||||
sDialogStatus.put(mDialogFragment.getClass().getName(), true);
|
||||
}
|
||||
|
||||
/** Implementation for DialogFragment#dismissAllowingStateLoss. */
|
||||
@Implementation
|
||||
public void dismissAllowingStateLoss() {
|
||||
sDialogStatus.put(mDialogFragment.getClass().getName(), false);
|
||||
}
|
||||
|
||||
/** Implementation for DialogFragment#dismiss. */
|
||||
@Implementation
|
||||
public void dismiss() {
|
||||
sDialogStatus.put(mDialogFragment.getClass().getName(), false);
|
||||
}
|
||||
|
||||
/** Check if DialogFragment is showing. */
|
||||
public static boolean isIsShowing(String clazzName) {
|
||||
return sDialogStatus.getOrDefault(clazzName, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright (C) 2024 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.bluetooth;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.robolectric.shadows.ShadowLooper.shadowMainLooper;
|
||||
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.junit.MockitoJUnit;
|
||||
import org.mockito.junit.MockitoRule;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.androidx.fragment.FragmentController;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowAlertDialogCompat.class})
|
||||
public class ProgressDialogFragmentTest {
|
||||
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
|
||||
|
||||
private static final String TEST_MESSAGE1 = "message1";
|
||||
private static final String TEST_MESSAGE2 = "message2";
|
||||
|
||||
private Fragment mParent;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
ShadowAlertDialogCompat.reset();
|
||||
mParent = new Fragment();
|
||||
FragmentController.setupFragment(
|
||||
mParent, FragmentActivity.class, /* containerViewId= */ 0, /* bundle= */ null);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
ShadowAlertDialogCompat.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMetricsCategory_correctValue() {
|
||||
ProgressDialogFragment fragment = ProgressDialogFragment.newInstance(mParent);
|
||||
// TODO: update real metric
|
||||
assertThat(fragment.getMetricsCategory()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onCreateDialog_unattachedFragment_nullDialogFragment() {
|
||||
ProgressDialogFragment fragment = ProgressDialogFragment.newInstance(new Fragment());
|
||||
assertThat(fragment).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onCreateDialog_showDialog() {
|
||||
ProgressDialogFragment fragment = ProgressDialogFragment.newInstance(mParent);
|
||||
fragment.show(TEST_MESSAGE1);
|
||||
shadowMainLooper().idle();
|
||||
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
|
||||
assertThat(dialog).isNotNull();
|
||||
assertThat(dialog.isShowing()).isTrue();
|
||||
TextView view = dialog.findViewById(R.id.message);
|
||||
assertThat(view).isNotNull();
|
||||
assertThat(view.getText().toString()).isEqualTo(TEST_MESSAGE1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dismissDialog_succeed() {
|
||||
ProgressDialogFragment fragment = ProgressDialogFragment.newInstance(mParent);
|
||||
fragment.show(TEST_MESSAGE1);
|
||||
shadowMainLooper().idle();
|
||||
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
|
||||
assertThat(dialog).isNotNull();
|
||||
assertThat(dialog.isShowing()).isTrue();
|
||||
|
||||
fragment.dismissAllowingStateLoss();
|
||||
shadowMainLooper().idle();
|
||||
assertThat(dialog.isShowing()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void showDialog_sameMessage_keepExistingDialog() {
|
||||
ProgressDialogFragment fragment = ProgressDialogFragment.newInstance(mParent);
|
||||
fragment.show(TEST_MESSAGE1);
|
||||
shadowMainLooper().idle();
|
||||
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
|
||||
assertThat(dialog).isNotNull();
|
||||
assertThat(dialog.isShowing()).isTrue();
|
||||
|
||||
fragment.show(TEST_MESSAGE1);
|
||||
shadowMainLooper().idle();
|
||||
assertThat(dialog.isShowing()).isTrue();
|
||||
TextView view = dialog.findViewById(R.id.message);
|
||||
assertThat(view).isNotNull();
|
||||
assertThat(view.getText().toString()).isEqualTo(TEST_MESSAGE1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void showDialog_newMessage_keepAndUpdateDialog() {
|
||||
ProgressDialogFragment fragment = ProgressDialogFragment.newInstance(mParent);
|
||||
fragment.show(TEST_MESSAGE1);
|
||||
shadowMainLooper().idle();
|
||||
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
|
||||
assertThat(dialog).isNotNull();
|
||||
assertThat(dialog.isShowing()).isTrue();
|
||||
TextView view = dialog.findViewById(R.id.message);
|
||||
assertThat(view).isNotNull();
|
||||
assertThat(view.getText().toString()).isEqualTo(TEST_MESSAGE1);
|
||||
|
||||
fragment.show(TEST_MESSAGE2);
|
||||
shadowMainLooper().idle();
|
||||
assertThat(dialog.isShowing()).isTrue();
|
||||
assertThat(view.getText().toString()).isEqualTo(TEST_MESSAGE2);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user