AF: Unit tests for PasswordsPreferenceController.
Bug: 169455298 Test: atest SettingsUnitTests:com.android.settings.applications.autofill.PasswordsPreferenceControllerTest Change-Id: I5cbbb17f192b963a634690a95a591d3f51f47436
This commit is contained in:
@@ -31,6 +31,7 @@ import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
|
||||
@@ -47,16 +48,23 @@ public class PasswordsPreferenceController extends BasePreferenceController {
|
||||
private final List<AutofillServiceInfo> mServices;
|
||||
|
||||
public PasswordsPreferenceController(Context context, String preferenceKey) {
|
||||
this(context, preferenceKey,
|
||||
AutofillServiceInfo.getAvailableServices(context, UserHandle.myUserId()));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public PasswordsPreferenceController(
|
||||
Context context, String preferenceKey, List<AutofillServiceInfo> availableServices) {
|
||||
super(context, preferenceKey);
|
||||
mPm = context.getPackageManager();
|
||||
mIconFactory = IconDrawableFactory.newInstance(mContext);
|
||||
mServices = AutofillServiceInfo.getAvailableServices(mContext, UserHandle.myUserId());
|
||||
for (int i = mServices.size() - 1; i >= 0; i--) {
|
||||
final String passwordsActivity = mServices.get(i).getPasswordsActivity();
|
||||
for (int i = availableServices.size() - 1; i >= 0; i--) {
|
||||
final String passwordsActivity = availableServices.get(i).getPasswordsActivity();
|
||||
if (TextUtils.isEmpty(passwordsActivity)) {
|
||||
mServices.remove(i);
|
||||
availableServices.remove(i);
|
||||
}
|
||||
}
|
||||
mServices = availableServices;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* 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.applications.autofill;
|
||||
|
||||
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
|
||||
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.os.Looper;
|
||||
import android.service.autofill.AutofillServiceInfo;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import com.google.android.collect.Lists;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class PasswordsPreferenceControllerTest {
|
||||
|
||||
private Context mContext;
|
||||
private PreferenceScreen mScreen;
|
||||
private PreferenceCategory mPasswordsPreferenceCategory;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = ApplicationProvider.getApplicationContext();
|
||||
if (Looper.myLooper() == null) {
|
||||
Looper.prepare(); // needed to create the preference screen
|
||||
}
|
||||
mScreen = new PreferenceManager(mContext).createPreferenceScreen(mContext);
|
||||
mPasswordsPreferenceCategory = new PreferenceCategory(mContext);
|
||||
mPasswordsPreferenceCategory.setKey("passwords");
|
||||
mScreen.addPreference(mPasswordsPreferenceCategory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_noServices_returnsUnavailable() {
|
||||
PasswordsPreferenceController controller =
|
||||
createControllerWithServices(Collections.emptyList());
|
||||
assertThat(controller.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_noPasswords_returnsUnavailable() {
|
||||
AutofillServiceInfo service = new AutofillServiceInfo.TestDataBuilder().build();
|
||||
PasswordsPreferenceController controller =
|
||||
createControllerWithServices(Lists.newArrayList(service));
|
||||
assertThat(controller.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_withPasswords_returnsAvailable() {
|
||||
PasswordsPreferenceController controller =
|
||||
createControllerWithServices(Lists.newArrayList(createServiceWithPasswords()));
|
||||
assertThat(controller.getAvailabilityStatus()).isEqualTo(AVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_noServices_noPreferencesAdded() {
|
||||
PasswordsPreferenceController controller =
|
||||
createControllerWithServices(Collections.emptyList());
|
||||
controller.displayPreference(mScreen);
|
||||
assertThat(mPasswordsPreferenceCategory.getPreferenceCount()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_noPasswords_noPreferencesAdded() {
|
||||
AutofillServiceInfo service = new AutofillServiceInfo.TestDataBuilder().build();
|
||||
PasswordsPreferenceController controller =
|
||||
createControllerWithServices(Lists.newArrayList(service));
|
||||
controller.displayPreference(mScreen);
|
||||
assertThat(mPasswordsPreferenceCategory.getPreferenceCount()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_withPasswords_addsPreference() {
|
||||
AutofillServiceInfo service = createServiceWithPasswords();
|
||||
PasswordsPreferenceController controller =
|
||||
createControllerWithServices(Lists.newArrayList(service));
|
||||
controller.displayPreference(mScreen);
|
||||
|
||||
assertThat(mPasswordsPreferenceCategory.getPreferenceCount()).isEqualTo(1);
|
||||
Preference pref = mPasswordsPreferenceCategory.getPreference(0);
|
||||
assertThat(pref.getIcon()).isNotNull();
|
||||
assertThat(pref.getIntent().getComponent())
|
||||
.isEqualTo(
|
||||
new ComponentName(
|
||||
service.getServiceInfo().packageName,
|
||||
service.getPasswordsActivity()));
|
||||
}
|
||||
|
||||
private PasswordsPreferenceController createControllerWithServices(
|
||||
List<AutofillServiceInfo> availableServices) {
|
||||
return new PasswordsPreferenceController(
|
||||
mContext, mPasswordsPreferenceCategory.getKey(), availableServices);
|
||||
}
|
||||
|
||||
private AutofillServiceInfo createServiceWithPasswords() {
|
||||
return new AutofillServiceInfo.TestDataBuilder()
|
||||
.setPasswordsActivity("com.android.test.Passwords")
|
||||
.build();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user