Merge "Companion Device App showing in Settings" into sc-dev
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.companion.Association;
|
||||
import android.companion.CompanionDeviceManager;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class BluetoothDetailsCompanionAppsControllerTest extends
|
||||
BluetoothDetailsControllerTestBase {
|
||||
private static final String PACKAGE_NAME_ONE = "com.google.android.deskclock";
|
||||
private static final String PACKAGE_NAME_TWO = "com.google.android.calculator";
|
||||
private static final String PACKAGE_NAME_THREE = "com.google.android.GoogleCamera";
|
||||
private static final CharSequence APP_NAME_ONE = "deskclock";
|
||||
private static final CharSequence APP_NAME_TWO = "calculator";
|
||||
private static final CharSequence APP_NAME_THREE = "GoogleCamera";
|
||||
|
||||
@Mock
|
||||
private CompanionDeviceManager mCompanionDeviceManager;
|
||||
@Mock
|
||||
private PackageManager mPackageManager;
|
||||
|
||||
private BluetoothDetailsCompanionAppsController mController;
|
||||
private PreferenceCategory mProfiles;
|
||||
private List<String> mPackages;
|
||||
private List<CharSequence> mAppNames;
|
||||
private List<Association> mAssociations;
|
||||
|
||||
|
||||
@Override
|
||||
public void setUp() {
|
||||
super.setUp();
|
||||
mPackages = Arrays.asList(PACKAGE_NAME_ONE, PACKAGE_NAME_TWO, PACKAGE_NAME_THREE);
|
||||
mAppNames = Arrays.asList(APP_NAME_ONE, APP_NAME_TWO, APP_NAME_THREE);
|
||||
mProfiles = spy(new PreferenceCategory(mContext));
|
||||
mAssociations = new ArrayList<>();
|
||||
when(mCompanionDeviceManager.getAllAssociations()).thenReturn(mAssociations);
|
||||
when(mProfiles.getPreferenceManager()).thenReturn(mPreferenceManager);
|
||||
setupDevice(mDeviceConfig);
|
||||
mController =
|
||||
new BluetoothDetailsCompanionAppsController(mContext, mFragment, mCachedDevice,
|
||||
mLifecycle);
|
||||
mController.mCompanionDeviceManager = mCompanionDeviceManager;
|
||||
mController.mPackageManager = mPackageManager;
|
||||
mController.mProfilesContainer = mProfiles;
|
||||
mProfiles.setKey(mController.getPreferenceKey());
|
||||
mScreen.addPreference(mProfiles);
|
||||
}
|
||||
|
||||
private void setupFakeLabelAndInfo(String packageName, CharSequence appName) {
|
||||
ApplicationInfo appInfo = mock(ApplicationInfo.class);
|
||||
try {
|
||||
when(mPackageManager.getApplicationInfo(packageName, 0)).thenReturn(appInfo);
|
||||
when(mPackageManager.getApplicationLabel(appInfo)).thenReturn(appName);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void addFakeAssociation(String packageName, CharSequence appName) {
|
||||
setupFakeLabelAndInfo(packageName, appName);
|
||||
Association association = new Association(
|
||||
0, mCachedDevice.getAddress(), packageName, "", true, System.currentTimeMillis());
|
||||
mAssociations.add(association);
|
||||
showScreen(mController);
|
||||
}
|
||||
|
||||
private Preference getPreference(int index) {
|
||||
PreferenceCategory preferenceCategory = mProfiles.findPreference(
|
||||
mController.getPreferenceKey());
|
||||
return Objects.requireNonNull(preferenceCategory).getPreference(index);
|
||||
}
|
||||
|
||||
private void removeAssociation(String packageName) {
|
||||
mAssociations = mAssociations.stream()
|
||||
.filter(a -> !a.getPackageName().equals(packageName))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
when(mCompanionDeviceManager.getAllAssociations()).thenReturn(mAssociations);
|
||||
|
||||
showScreen(mController);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addOneAssociation_preferenceShouldBeAdded() {
|
||||
addFakeAssociation(PACKAGE_NAME_ONE, APP_NAME_ONE);
|
||||
|
||||
Preference preferenceOne = getPreference(0);
|
||||
|
||||
assertThat(preferenceOne.getClass()).isEqualTo(CompanionAppWidgetPreference.class);
|
||||
assertThat(preferenceOne.getKey()).isEqualTo(PACKAGE_NAME_ONE);
|
||||
assertThat(preferenceOne.getTitle()).isEqualTo(APP_NAME_ONE.toString());
|
||||
assertThat(mProfiles.getPreferenceCount()).isEqualTo(1);
|
||||
|
||||
removeAssociation(PACKAGE_NAME_ONE);
|
||||
|
||||
assertThat(mProfiles.getPreferenceCount()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeOneAssociation_preferenceShouldBeRemoved() {
|
||||
addFakeAssociation(PACKAGE_NAME_ONE, APP_NAME_ONE);
|
||||
|
||||
removeAssociation(PACKAGE_NAME_ONE);
|
||||
|
||||
assertThat(mProfiles.getPreferenceCount()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addMultipleAssociations_preferencesShouldBeAdded() {
|
||||
for (int i = 0; i < mPackages.size(); i++) {
|
||||
addFakeAssociation(mPackages.get(i), mAppNames.get(i));
|
||||
|
||||
Preference preference = getPreference(i);
|
||||
|
||||
assertThat(preference.getClass()).isEqualTo(CompanionAppWidgetPreference.class);
|
||||
assertThat(preference.getKey()).isEqualTo(mPackages.get(i));
|
||||
assertThat(preference.getTitle()).isEqualTo(mAppNames.get(i).toString());
|
||||
assertThat(mProfiles.getPreferenceCount()).isEqualTo(i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeMultipleAssociations_preferencesShouldBeRemoved() {
|
||||
for (int i = 0; i < mPackages.size(); i++) {
|
||||
addFakeAssociation(mPackages.get(i), mAppNames.get(i).toString());
|
||||
}
|
||||
|
||||
for (int i = 0; i < mPackages.size(); i++) {
|
||||
removeAssociation(mPackages.get(i));
|
||||
|
||||
assertThat(mProfiles.getPreferenceCount()).isEqualTo(mPackages.size() - i - 1);
|
||||
|
||||
if (i == mPackages.size() - 1) {
|
||||
break;
|
||||
}
|
||||
|
||||
assertThat(getPreference(0).getKey()).isEqualTo(mPackages.get(i + 1));
|
||||
assertThat(getPreference(0).getTitle()).isEqualTo(mAppNames.get(i + 1).toString());
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.view.ContextThemeWrapper;
|
||||
import android.view.View;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowAlertDialogCompat.class})
|
||||
public class CompanionAppWidgetPreferenceTest {
|
||||
private static final String TITLE_ONE = "Test Title 1";
|
||||
private static final String TITLE_TWO = "Test Title 1";
|
||||
private static final String KEY_ONE = "Test Key 1";
|
||||
private static final String KEY_TWO = "Test Key 1";
|
||||
|
||||
private Context mContext;
|
||||
private Drawable mWidgetIconOne;
|
||||
private Drawable mWidgetIconTwo;
|
||||
private Drawable mAppIconOne;
|
||||
private Drawable mAppIconTwo;
|
||||
|
||||
@Mock
|
||||
private View.OnClickListener mWidgetListenerOne;
|
||||
@Mock
|
||||
private View.OnClickListener mWidgetListenerTwo;
|
||||
|
||||
private List<CompanionAppWidgetPreference> mPreferenceContainer;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mPreferenceContainer = new ArrayList<>();
|
||||
Context context = spy(RuntimeEnvironment.application.getApplicationContext());
|
||||
mContext = new ContextThemeWrapper(context, R.style.Theme_Settings);
|
||||
mWidgetIconOne = mock(Drawable.class);
|
||||
mAppIconOne = mock(Drawable.class);
|
||||
mWidgetListenerOne = mock(View.OnClickListener.class);
|
||||
mWidgetIconTwo = mock(Drawable.class);
|
||||
mAppIconTwo = mock(Drawable.class);
|
||||
mWidgetListenerTwo = mock(View.OnClickListener.class);
|
||||
}
|
||||
|
||||
private void setUpPreferenceContainer(Drawable widgetIcon, Drawable appIcon,
|
||||
View.OnClickListener listener, String title, String key) {
|
||||
CompanionAppWidgetPreference preference = new CompanionAppWidgetPreference(
|
||||
widgetIcon, listener, mContext);
|
||||
preference.setIcon(appIcon);
|
||||
preference.setTitle(title);
|
||||
preference.setKey(key);
|
||||
mPreferenceContainer.add(preference);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setUpPreferenceContainer_preferenceShouldBeAdded() {
|
||||
setUpPreferenceContainer(
|
||||
mWidgetIconOne, mAppIconOne, mWidgetListenerOne, TITLE_ONE, KEY_ONE);
|
||||
|
||||
assertThat(mPreferenceContainer.get(0).getIcon()).isEqualTo(mAppIconOne);
|
||||
assertThat(mPreferenceContainer.get(0).getKey()).isEqualTo(KEY_ONE);
|
||||
assertThat(mPreferenceContainer.get(0).getTitle()).isEqualTo(TITLE_ONE);
|
||||
|
||||
setUpPreferenceContainer(
|
||||
mWidgetIconTwo, mAppIconTwo, mWidgetListenerTwo, TITLE_TWO, KEY_TWO);
|
||||
|
||||
assertThat(mPreferenceContainer.get(1).getIcon()).isEqualTo(mAppIconTwo);
|
||||
assertThat(mPreferenceContainer.get(1).getKey()).isEqualTo(KEY_TWO);
|
||||
assertThat(mPreferenceContainer.get(1).getTitle()).isEqualTo(TITLE_TWO);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user