Update DialogFragment UI when BT device is add/remove/rename

* Add AudioSwitchCallback() in AudioSwitchPreferenceController.
  This callback is used to notify SoudSettings to update the dialogFragment UI.
* Add UpdatableListPreferenceDialogFragment that updates the available
  options when dialog is shown
* Add test to verify the adapter count when
  onListPreferenceUpdated() is called.

Bug: 77783217
Test: make -j50 RunSettingsRoboTests
Change-Id: I8cac1b30ec50df026f4b7722dd1cd2f69e77a4cb
Merged-In: I8cac1b30ec50df026f4b7722dd1cd2f69e77a4cb
This commit is contained in:
hughchen
2018-04-23 20:13:14 +08:00
committed by Hugh Chen
parent 0cf2e41bf8
commit b6ac12eed4
6 changed files with 316 additions and 2 deletions

View File

@@ -16,7 +16,6 @@
package com.android.settings.sound;
import static android.media.AudioSystem.DEVICE_OUT_BLUETOOTH_SCO;
import static android.media.AudioSystem.DEVICE_OUT_HEARING_AID;
import static android.media.AudioSystem.DEVICE_OUT_USB_HEADSET;
@@ -93,6 +92,8 @@ public class HandsFreeProfileOutputPreferenceControllerTest {
private HeadsetProfile mHeadsetProfile;
@Mock
private HearingAidProfile mHearingAidProfile;
@Mock
private AudioSwitchPreferenceController.AudioSwitchCallback mAudioSwitchPreferenceCallback;
private Context mContext;
private PreferenceScreen mScreen;
@@ -156,6 +157,7 @@ public class HandsFreeProfileOutputPreferenceControllerTest {
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
mScreen.addPreference(mPreference);
mController.displayPreference(mScreen);
mController.setCallback(mAudioSwitchPreferenceCallback);
}
@After

View File

@@ -94,6 +94,8 @@ public class MediaOutputPreferenceControllerTest {
private A2dpProfile mA2dpProfile;
@Mock
private HearingAidProfile mHearingAidProfile;
@Mock
private AudioSwitchPreferenceController.AudioSwitchCallback mAudioSwitchPreferenceCallback;
private Context mContext;
private PreferenceScreen mScreen;
@@ -157,6 +159,7 @@ public class MediaOutputPreferenceControllerTest {
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
mScreen.addPreference(mPreference);
mController.displayPreference(mScreen);
mController.setCallback(mAudioSwitchPreferenceCallback);
}
@After

View File

@@ -0,0 +1,90 @@
/*
* Copyright 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.widget;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.support.v7.preference.ListPreference;
import android.widget.ArrayAdapter;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
import static org.mockito.Mockito.spy;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = ShadowBluetoothUtils.class)
public class UpdatableListPreferenceDialogFragmentTest {
private Context mContext;
private UpdatableListPreferenceDialogFragment mUpdatableListPrefDlgFragment;
private static final String KEY = "Test_Key";
private ArrayAdapter mAdapter;
private ArrayList<CharSequence> mEntries;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mUpdatableListPrefDlgFragment = UpdatableListPreferenceDialogFragment
.newInstance(KEY, MetricsProto.MetricsEvent.DIALOG_SWITCH_A2DP_DEVICES);
mEntries = spy(new ArrayList<>());
mUpdatableListPrefDlgFragment.setEntries(mEntries);
mUpdatableListPrefDlgFragment.
setMetricsCategory(mUpdatableListPrefDlgFragment.getArguments());
initAdapter();
}
private void initAdapter() {
mAdapter = spy(new ArrayAdapter<>(
mContext,
com.android.internal.R.layout.select_dialog_singlechoice,
mEntries));
mUpdatableListPrefDlgFragment.setAdapter(mAdapter);
}
@Test
public void getMetricsCategory() {
assertThat(mUpdatableListPrefDlgFragment.getMetricsCategory())
.isEqualTo(MetricsProto.MetricsEvent.DIALOG_SWITCH_A2DP_DEVICES);
}
@Test
public void onListPreferenceUpdated_verifyAdapterCanBeUpdate() {
assertThat(mUpdatableListPrefDlgFragment.getAdapter().getCount()).
isEqualTo(0);
ListPreference listPreference = new ListPreference(mContext);
final CharSequence[] charSequences = {"Test_DEVICE_1", "Test_DEVICE_2"};
listPreference.setEntries(charSequences);
mUpdatableListPrefDlgFragment.onListPreferenceUpdated(listPreference);
assertThat(mUpdatableListPrefDlgFragment.getAdapter().getCount()).
isEqualTo(2);
}
}