Migrate robolectric tests to junit tests

This change do the 2 things:

1. Add new junit tests files which replace robolectric
   RobolectricTestRunner & RuntimeEnvironment with
   AndroidX objects without problem.
2. Remove the robolectric test files which have it's new junit files.

This change migrate 103 files, there are still 1209
files to go.

Bug: 174728471
Test: atest
      make RunSettingsRoboTests
Change-Id: I15ed3f4745b85862f720aabbf710ce1475aced93
This commit is contained in:
Arc Wang
2020-12-09 17:03:43 +08:00
parent f705f7933e
commit 62c78ff3fa
108 changed files with 3082 additions and 338 deletions

View File

@@ -24,7 +24,6 @@ import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerListHelper;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.DatabaseIndexingUtils;
import com.android.settingslib.search.Indexable;
import com.android.settingslib.core.AbstractPreferenceController;
import org.robolectric.RuntimeEnvironment;

View File

@@ -30,4 +30,4 @@ public class FakeSettingsPanelActivity extends SettingsPanelActivity {
final Intent intent = new Intent(FakePanelContent.FAKE_ACTION);
return intent;
}
}
}

View File

@@ -115,4 +115,4 @@ public class FakeSettingsFragment extends DashboardFragment {
return keys;
}
};
}
}

View File

@@ -12,9 +12,7 @@
* 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.testutils;
import android.content.Context;
@@ -26,8 +24,6 @@ import android.provider.Settings;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.slices.SliceBackgroundWorker;
import java.io.IOException;
public class FakeToggleController extends TogglePreferenceController {
public static final String AVAILABILITY_KEY = "fake_toggle_availability_key";

View File

@@ -322,4 +322,4 @@ public class SliceTester {
expectedKeywords.add(data.getScreenTitle().toString());
assertThat(keywords).containsExactlyElementsIn(expectedKeywords);
}
}
}

View File

@@ -1,18 +0,0 @@
package com.android.settings.widget;
import android.content.Context;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class DonutViewTest {
@Test
public void getPercentageStringSpannable_doesntCrashForMissingPercentage() {
Context context = RuntimeEnvironment.application;
DonutView.getPercentageStringSpannable(context.getResources(), "50%", "h");
}
}

View File

@@ -21,13 +21,14 @@ import android.text.Spannable;
import android.text.style.ClickableSpan;
import android.widget.TextView;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class LinkifyUtilsTest {
private static final String TEST_STRING = "to LINK_BEGINscanning settingsLINK_END.";
private static final String WRONG_STRING = "to scanning settingsLINK_END.";
@@ -41,7 +42,7 @@ public class LinkifyUtilsTest {
public void setUp() throws Exception {
mSpanStringBuilder = new StringBuilder(TEST_STRING);
mWrongSpanStringBuilder = new StringBuilder(WRONG_STRING);
mTextView = new TextView(RuntimeEnvironment.application);
mTextView = new TextView(ApplicationProvider.getApplicationContext());
}
@Test

View File

@@ -0,0 +1,25 @@
/*
* Copyright (C) 2020 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;
/**
* Convenience methods and constants for testing.
*/
public class TestUtils {
public static final long KILOBYTE = 1024L; // TODO: Change to 1000 in O Robolectric.
public static final long MEGABYTE = KILOBYTE * KILOBYTE;
public static final long GIGABYTE = KILOBYTE * MEGABYTE;
}

View File

@@ -19,6 +19,9 @@ package com.android.settings.accessibility;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
@@ -30,10 +33,8 @@ import androidx.preference.SwitchPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class AccessibilityShortcutPreferenceControllerTest {
private Context mContext;
@@ -42,7 +43,7 @@ public class AccessibilityShortcutPreferenceControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mPreference = new SwitchPreference(mContext);
mController = new AccessibilityShortcutPreferenceController(mContext,
"accessibility_shortcut_preference");

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -26,16 +26,16 @@ import android.content.Context;
import android.provider.Settings;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class DisableAnimationsPreferenceControllerTest {
private Context mContext;
@@ -44,7 +44,7 @@ public class DisableAnimationsPreferenceControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mPreference = new SwitchPreference(mContext);
mController = new DisableAnimationsPreferenceController(mContext, "disable_animation");
}

View File

@@ -22,16 +22,16 @@ import android.content.Context;
import android.provider.Settings;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class FontWeightAdjustmentPreferenceControllerTest {
private static final int ON = FontWeightAdjustmentPreferenceController.BOLD_TEXT_ADJUSTMENT;
private static final int OFF = 0;
@@ -42,7 +42,7 @@ public class FontWeightAdjustmentPreferenceControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mPreference = new SwitchPreference(mContext);
mController = new FontWeightAdjustmentPreferenceController(
mContext, "font_weight_adjustment");

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -22,16 +22,16 @@ import android.content.Context;
import android.provider.Settings;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class HighTextContrastPreferenceControllerTest {
private static final int ON = 1;
@@ -44,7 +44,7 @@ public class HighTextContrastPreferenceControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mPreference = new SwitchPreference(mContext);
mController = new HighTextContrastPreferenceController(mContext, "text_contrast");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -25,16 +25,16 @@ import android.content.Context;
import android.provider.Settings;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class LargePointerIconPreferenceControllerTest {
private static final int UNKNOWN = -1;
@@ -45,7 +45,7 @@ public class LargePointerIconPreferenceControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mPreference = new SwitchPreference(mContext);
mController = new LargePointerIconPreferenceController(mContext, "large_pointer");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -20,15 +20,16 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class MagnificationPreferenceControllerTest {
private Context mContext;
@@ -36,7 +37,7 @@ public class MagnificationPreferenceControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mController = new MagnificationPreferenceController(mContext, "magnification");
}

View File

@@ -22,13 +22,13 @@ import android.content.ComponentName;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
/** Tests for {@link PreferredShortcuts} */
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class PreferredShortcutsTest {
private static final String PACKAGE_NAME_1 = "com.test1.example";

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -23,16 +23,16 @@ import android.os.UserHandle;
import android.provider.Settings;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class PrimaryMonoPreferenceControllerTest {
private static final int ON = 1;
@@ -45,7 +45,7 @@ public class PrimaryMonoPreferenceControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mPreference = new SwitchPreference(mContext);
mController = new PrimaryMonoPreferenceController(mContext, "test_key");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -21,15 +21,16 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.provider.Settings;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class SelectLongPressTimeoutPreferenceControllerTest {
private static final int VALID_VALUE = 1500;
private static final int INVALID_VALUE = 0;
@@ -40,7 +41,7 @@ public class SelectLongPressTimeoutPreferenceControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mController = new SelectLongPressTimeoutPreferenceController(mContext, "press_timeout");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -18,13 +18,14 @@ package com.android.settings.accounts;
import static com.google.common.truth.Truth.assertThat;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.lang.reflect.Modifier;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class RemoveUserFragmentTest {
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -22,13 +22,14 @@ import android.accounts.Account;
import android.content.Context;
import android.os.UserHandle;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class SyncStateSwitchPreferenceTest {
private Context mContext;
@@ -36,7 +37,7 @@ public class SyncStateSwitchPreferenceTest {
@Before
public void setup() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -11,26 +11,26 @@
* 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
* limitations under the License.
*/
package com.android.settings.applications;
import static com.google.common.truth.Truth.assertThat;
import android.app.AppOpsManager;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class AppStateInstallAppsBridgeTest {
@Test
public void testInstallAppsStateCanInstallApps() {
AppStateInstallAppsBridge.InstallAppsState appState =
new AppStateInstallAppsBridge.InstallAppsState();
new AppStateInstallAppsBridge.InstallAppsState();
assertThat(appState.canInstallApps()).isFalse();
appState.permissionRequested = true;
@@ -46,7 +46,7 @@ public class AppStateInstallAppsBridgeTest {
@Test
public void testInstallAppsStateIsPotentialAppSource() {
AppStateInstallAppsBridge.InstallAppsState appState =
new AppStateInstallAppsBridge.InstallAppsState();
new AppStateInstallAppsBridge.InstallAppsState();
assertThat(appState.isPotentialAppSource()).isFalse();
appState.appOpMode = AppOpsManager.MODE_ERRORED;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -11,24 +11,24 @@
* 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
* limitations under the License.
*/
package com.android.settings.applications;
import static org.junit.Assert.assertTrue;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.R;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class EnterpriseDefaultAppsTest {
@Test
public void testNumberOfIntentsCorrelateWithUI() {
final int concatenation_templates[] =
final int[] concatenation_templates =
new int[]{0 /* no need for single app name */,
R.string.app_names_concatenation_template_2,
R.string.app_names_concatenation_template_3};

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -18,13 +18,14 @@ package com.android.settings.applications.assist;
import android.net.Uri;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.List;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class AssistSettingObserverTest {
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -37,11 +37,12 @@ import static com.android.settings.applications.manageapplications.ManageApplica
import static com.google.common.truth.Truth.assertThat;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class AppFilterRegistryTest {
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -20,13 +20,14 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class FaceSettingsAttentionPreferenceControllerTest {
private Context mContext;
@@ -34,7 +35,7 @@ public class FaceSettingsAttentionPreferenceControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mController = new FaceSettingsAttentionPreferenceController(mContext);
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright (C) 2020 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.core;
import android.content.Context;
public class BadPreferenceController extends BasePreferenceController {
public BadPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
throw new IllegalArgumentException("error");
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public boolean useDynamicSliceSummary() {
return true;
}
}

View File

@@ -0,0 +1,96 @@
/*
* Copyright (C) 2020 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.core;
import static com.google.common.truth.Truth.assertWithMessage;
import android.content.Context;
import com.android.settings.core.codeinspection.CodeInspector;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
public class BasePreferenceControllerSignatureInspector extends CodeInspector {
private final List<String> mExemptList;
public BasePreferenceControllerSignatureInspector(List<Class<?>> classes) {
super(classes);
mExemptList = new ArrayList<>();
initializeExemptList(mExemptList,
"exempt_invalid_base_preference_controller_constructor");
}
@Override
public void run() {
StringBuilder badClasses = new StringBuilder();
for (Class c : mClasses) {
if (!isConcreteSettingsClass(c)) {
// Not a Settings class, or is abstract, don't care.
continue;
}
if (!BasePreferenceController.class.isAssignableFrom(c)) {
// Not a BasePreferenceController, don't care.
continue;
}
final String className = c.getName();
if (mExemptList.remove(className)) {
continue;
}
final Constructor[] constructors = c.getDeclaredConstructors();
if (constructors == null || constructors.length == 0) {
badClasses.append(c.getName()).append(",");
}
boolean hasValidConstructor = false;
for (Constructor constructor : constructors) {
if (hasValidConstructorSignature(constructor)) {
hasValidConstructor = true;
break;
}
}
if (!hasValidConstructor) {
badClasses.append(className).append(",");
}
}
assertWithMessage("All BasePreferenceController (and subclasses) constructor must either"
+ " only take Context, or (Context, String). No other types are allowed")
.that(badClasses.toString())
.isEmpty();
assertWithMessage("Something in the exempt list is no longer relevant. Please remove"
+ "it from packages/apps/Settings/tests/robotests/assets/"
+ "exempt_invalid_base_preference_controller_constructor")
.that(mExemptList)
.isEmpty();
}
private static boolean hasValidConstructorSignature(Constructor constructor) {
final Class[] parameterTypes = constructor.getParameterTypes();
if (parameterTypes.length == 1) {
return Context.class.isAssignableFrom(parameterTypes[0]);
} else if (parameterTypes.length == 2) {
return Context.class.isAssignableFrom(parameterTypes[0])
&& String.class.isAssignableFrom(parameterTypes[1]);
}
return false;
}
}

View File

@@ -1,33 +1,35 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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
* 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.
* 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.core;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.slices.SliceData;
import com.android.settings.widget.SeekBarPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class SettingsSliderPreferenceControllerTest {
private FakeSliderPreferenceController mSliderController;
@@ -36,9 +38,9 @@ public class SettingsSliderPreferenceControllerTest {
@Before
public void setUp() {
mPreference = new SeekBarPreference(RuntimeEnvironment.application);
mSliderController = new FakeSliderPreferenceController(RuntimeEnvironment.application,
"key");
mPreference = new SeekBarPreference(ApplicationProvider.getApplicationContext());
mSliderController = new FakeSliderPreferenceController(
ApplicationProvider.getApplicationContext(), "key");
mPreference.setContinuousUpdates(true);
mPreference.setMin(mSliderController.getMin());
@@ -71,7 +73,7 @@ public class SettingsSliderPreferenceControllerTest {
private class FakeSliderPreferenceController extends SliderPreferenceController {
private final int MAX_STEPS = 2112;
private static final int MAX_STEPS = 2112;
private int mPosition;
private FakeSliderPreferenceController(Context context, String key) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -20,16 +20,16 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.slices.SliceData;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class TogglePreferenceControllerTest {
private FakeToggle mToggleController;
@@ -39,7 +39,7 @@ public class TogglePreferenceControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mPreference = new SwitchPreference(mContext);
mToggleController = new FakeToggle(mContext, "key");
}
@@ -102,7 +102,7 @@ public class TogglePreferenceControllerTest {
private static class FakeToggle extends TogglePreferenceController {
private boolean checkedFlag;
private boolean mCheckedFlag;
private FakeToggle(Context context, String preferenceKey) {
super(context, preferenceKey);
@@ -110,12 +110,12 @@ public class TogglePreferenceControllerTest {
@Override
public boolean isChecked() {
return checkedFlag;
return mCheckedFlag;
}
@Override
public boolean setChecked(boolean isChecked) {
checkedFlag = isChecked;
mCheckedFlag = isChecked;
return true;
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (C) 2020 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.core.codeinspection;
import com.google.common.reflect.ClassPath;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Scans and builds all classes in current classloader.
*/
public class ClassScanner {
public List<Class<?>> getClassesForPackage(String packageName) throws ClassNotFoundException {
final List<Class<?>> classes = new ArrayList<>();
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
ClassPath classPath = ClassPath.from(classLoader);
// Some anonymous classes don't return true when calling isAnonymousClass(), but they
// always seem to be nested anonymous classes like com.android.settings.Foo$1$2. In
// general we don't want any anonymous classes so we just filter these out by searching
// for $[0-9] in the name.
Pattern anonymousClassPattern = Pattern.compile(".*\\$\\d+.*");
Matcher anonymousClassMatcher = anonymousClassPattern.matcher("");
for (ClassPath.ClassInfo info : classPath.getAllClasses()) {
if (info.getPackageName().startsWith(packageName)) {
try {
Class clazz = classLoader.loadClass(info.getName());
if (clazz.isAnonymousClass() || anonymousClassMatcher.reset(
clazz.getName()).matches()) {
continue;
}
classes.add(clazz);
} catch (NoClassDefFoundError e) {
// do nothing. this class hasn't been found by the
// loader, and we don't care.
}
}
}
} catch (final IOException e) {
throw new ClassNotFoundException("Error when parsing " + packageName, e);
}
return classes;
}
}

View File

@@ -0,0 +1,94 @@
/*
* Copyright (C) 2020 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.core.codeinspection;
import static com.google.common.truth.Truth.assertWithMessage;
import androidx.test.core.app.ApplicationProvider;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Modifier;
import java.util.List;
/**
* Inspector takes a list of class objects and perform static code analysis in its {@link #run()}
* method.
*/
public abstract class CodeInspector {
protected static final String PACKAGE_NAME = "com.android.settings";
private static final String TEST_CLASS_SUFFIX = "Test";
private static final String TEST_INNER_CLASS_SIGNATURE = "Test$";
protected final List<Class<?>> mClasses;
public CodeInspector(List<Class<?>> classes) {
mClasses = classes;
}
/**
* Code inspection runner method.
*/
public abstract void run();
protected void assertNoObsoleteInExemptList(String listName, List<String> list) {
final StringBuilder obsoleteExemptItems = new StringBuilder(listName).append(
" contains item that should not be exempted.\n");
for (String c : list) {
obsoleteExemptItems.append(c).append("\n");
}
assertWithMessage(obsoleteExemptItems.toString()).that(list).isEmpty();
}
protected boolean isConcreteSettingsClass(Class clazz) {
// Abstract classes
if (Modifier.isAbstract(clazz.getModifiers())) {
return false;
}
final String packageName = clazz.getPackage().getName();
// Classes that are not in Settings
if (!packageName.contains(PACKAGE_NAME + ".") && !packageName.endsWith(PACKAGE_NAME)) {
return false;
}
final String className = clazz.getName();
// Classes from tests
if (className.endsWith(TEST_CLASS_SUFFIX)) {
return false;
}
if (className.contains(TEST_INNER_CLASS_SIGNATURE)) {
return false;
}
return true;
}
public static void initializeExemptList(List<String> exemptList, String filename) {
try {
final InputStream in = ApplicationProvider.getApplicationContext()
.getAssets().open(filename);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
exemptList.add(line);
}
} catch (Exception e) {
throw new IllegalArgumentException("Error initializing exempt list " + filename, e);
}
}
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright (C) 2020 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.core.codeinspection;
import static org.junit.Assert.fail;
import android.util.ArraySet;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnAttach;
import com.android.settingslib.core.lifecycle.events.OnCreate;
import com.android.settingslib.core.lifecycle.events.OnCreateOptionsMenu;
import com.android.settingslib.core.lifecycle.events.OnDestroy;
import com.android.settingslib.core.lifecycle.events.OnOptionsItemSelected;
import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnPrepareOptionsMenu;
import com.android.settingslib.core.lifecycle.events.OnResume;
import com.android.settingslib.core.lifecycle.events.OnSaveInstanceState;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
import com.android.settingslib.core.lifecycle.events.SetPreferenceScreen;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
public class LifecycleObserverCodeInspector extends CodeInspector {
private static final List<Class> LIFECYCLE_EVENTS = Arrays.asList(
OnAttach.class,
OnCreate.class,
OnCreateOptionsMenu.class,
OnDestroy.class,
OnOptionsItemSelected.class,
OnPause.class,
OnPrepareOptionsMenu.class,
OnResume.class,
OnSaveInstanceState.class,
OnStart.class,
OnStop.class,
SetPreferenceScreen.class
);
public LifecycleObserverCodeInspector(List<Class<?>> classes) {
super(classes);
}
@Override
public void run() {
final Set<String> notImplementingLifecycleObserver = new ArraySet<>();
for (Class clazz : mClasses) {
if (!isConcreteSettingsClass(clazz)) {
continue;
}
boolean classObservesLifecycleEvent = false;
for (Class event : LIFECYCLE_EVENTS) {
if (event.isAssignableFrom(clazz)) {
classObservesLifecycleEvent = true;
break;
}
}
if (classObservesLifecycleEvent && !LifecycleObserver.class.isAssignableFrom(clazz)) {
// Observes LifecycleEvent but not implementing LifecycleObserver. Something is
// wrong.
notImplementingLifecycleObserver.add(clazz.getName());
}
}
if (!notImplementingLifecycleObserver.isEmpty()) {
final String errorTemplate =
"The following class(es) implements lifecycle.events.*, but don't "
+ "implement LifecycleObserver. Something is wrong:\n";
final StringBuilder error = new StringBuilder(errorTemplate);
for (String name : notImplementingLifecycleObserver) {
error.append(name).append('\n');
}
fail(error.toString());
}
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2020 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.core.instrumentation;
import static com.google.common.truth.Truth.assertWithMessage;
import android.util.ArraySet;
import androidx.fragment.app.Fragment;
import com.android.settings.core.codeinspection.CodeInspector;
import com.android.settingslib.core.instrumentation.Instrumentable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* {@link CodeInspector} that verifies all fragments implements Instrumentable.
*/
public class InstrumentableFragmentCodeInspector extends CodeInspector {
private final List<String> mNotImplementingInstrumentableExemptList;
public InstrumentableFragmentCodeInspector(List<Class<?>> classes) {
super(classes);
mNotImplementingInstrumentableExemptList = new ArrayList<>();
initializeExemptList(mNotImplementingInstrumentableExemptList,
"exempt_not_implementing_instrumentable");
}
@Override
public void run() {
final Set<String> broken = new ArraySet<>();
for (Class clazz : mClasses) {
if (!isConcreteSettingsClass(clazz)) {
continue;
}
final String className = clazz.getName();
// If it's a fragment, it must also be instrumentable.
final boolean allowlisted =
mNotImplementingInstrumentableExemptList.remove(className);
if (Fragment.class.isAssignableFrom(clazz)
&& !Instrumentable.class.isAssignableFrom(clazz)
&& !allowlisted) {
broken.add(className);
}
}
final StringBuilder sb = new StringBuilder(
"All fragments should implement Instrumentable, but the following are not:\n");
for (String c : broken) {
sb.append(c).append("\n");
}
assertWithMessage(sb.toString()).that(broken.isEmpty()).isTrue();
assertNoObsoleteInExemptList("exempt_not_implementing_instrumentable",
mNotImplementingInstrumentableExemptList);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -29,6 +29,9 @@ import android.content.pm.ProviderInfo;
import android.os.Bundle;
import android.util.Pair;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settingslib.drawer.ActivityTile;
import com.android.settingslib.drawer.CategoryKey;
import com.android.settingslib.drawer.DashboardCategory;
@@ -38,13 +41,11 @@ import com.android.settingslib.drawer.Tile;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.HashMap;
import java.util.Map;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class CategoryManagerTest {
private ActivityInfo mActivityInfo;
@@ -55,7 +56,7 @@ public class CategoryManagerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mActivityInfo = new ActivityInfo();
mActivityInfo.packageName = "pkg";
mActivityInfo.name = "class";
@@ -144,7 +145,8 @@ public class CategoryManagerTest {
mCategoryByKeyMap.put(CATEGORY_HOMEPAGE, category);
// Sort their priorities
mCategoryManager.sortCategories(RuntimeEnvironment.application, mCategoryByKeyMap);
mCategoryManager.sortCategories(ApplicationProvider.getApplicationContext(),
mCategoryByKeyMap);
// Verify they are now sorted.
assertThat(category.getTile(0)).isSameInstanceAs(tile3);

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -18,14 +18,15 @@ package com.android.settings.dashboard;
import static com.google.common.truth.Truth.assertThat;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.accounts.AccountDashboardFragment;
import com.android.settingslib.drawer.CategoryKey;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class DashboardFragmentRegistryTest {
@Test
public void pageAndKeyShouldHave1to1Mapping() {

View File

@@ -0,0 +1,84 @@
/*
* Copyright (C) 2020 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.dashboard;
import android.content.Context;
import androidx.fragment.app.Fragment;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerListHelper;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.DatabaseIndexingUtils;
import com.android.settingslib.core.AbstractPreferenceController;
import java.util.List;
public class DashboardFragmentSearchIndexProviderInspector {
public static boolean isSharingPreferenceControllers(Class clazz) {
final Context context = ApplicationProvider.getApplicationContext();
final Fragment fragment;
try {
fragment = Fragment.instantiate(context, clazz.getName());
} catch (Throwable e) {
// Can't do much with exception, assume the test passed.
return true;
}
if (!(fragment instanceof DashboardFragment)) {
return true;
}
final BaseSearchIndexProvider provider =
(BaseSearchIndexProvider) DatabaseIndexingUtils.getSearchIndexProvider(clazz);
if (provider == null) {
return true;
}
final List<AbstractPreferenceController> controllersFromSearchIndexProvider;
final List<AbstractPreferenceController> controllersFromFragment;
try {
controllersFromSearchIndexProvider = provider.getPreferenceControllers(context);
} catch (Throwable e) {
// Can't do much with exception, assume the test passed.
return true;
}
try {
controllersFromFragment =
((DashboardFragment) fragment).createPreferenceControllers(context);
List<BasePreferenceController> controllersFromXml = PreferenceControllerListHelper
.getPreferenceControllersFromXml(context,
((DashboardFragment) fragment).getPreferenceScreenResId());
final List<BasePreferenceController> uniqueControllerFromXml =
PreferenceControllerListHelper.filterControllers(
controllersFromXml, controllersFromFragment);
controllersFromFragment.addAll(uniqueControllerFromXml);
} catch (Throwable e) {
// Can't do much with exception, assume the test passed.
return true;
}
if (controllersFromFragment == controllersFromSearchIndexProvider) {
return true;
} else if (controllersFromFragment != null && controllersFromSearchIndexProvider != null) {
return controllersFromFragment.size() == controllersFromSearchIndexProvider.size();
} else {
return false;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -20,6 +20,8 @@ import static com.google.common.truth.Truth.assertThat;
import android.icu.text.Collator;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.datetime.timezone.RegionZonePicker.TimeZoneInfoComparator;
import com.android.settings.datetime.timezone.TimeZoneInfo.Formatter;
@@ -31,9 +33,8 @@ import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class RegionZonePickerTest {
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -23,18 +23,19 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.datetime.timezone.TimeZoneInfo.Formatter;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.Date;
import java.util.Locale;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class TimeZoneInfoPreferenceControllerTest {
private TimeZoneInfo mTimeZoneInfo;
@@ -42,7 +43,7 @@ public class TimeZoneInfoPreferenceControllerTest {
@Before
public void setUp() {
final Context context = RuntimeEnvironment.application;
final Context context = ApplicationProvider.getApplicationContext();
final Date now = new Date(0L); // 00:00 1/1/1970
final Formatter formatter = new Formatter(Locale.US, now);
mTimeZoneInfo = formatter.format("America/Los_Angeles");

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -18,6 +18,8 @@ package com.android.settings.datetime.timezone;
import static com.google.common.truth.Truth.assertThat;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.datetime.timezone.TimeZoneInfo.Formatter;
import org.junit.Test;
@@ -25,9 +27,8 @@ import org.junit.runner.RunWith;
import java.util.Date;
import java.util.Locale;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class TimeZoneInfoTest {
@Test

View File

@@ -22,14 +22,14 @@ import android.content.Context;
import android.content.SharedPreferences;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class OverlaySettingsPreferenceControllerTest {
private Context mContext;
@@ -38,7 +38,7 @@ public class OverlaySettingsPreferenceControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mController = new OverlaySettingsPreferenceController(mContext);
mPreference = new SwitchPreference(mContext);
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright (C) 2020 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.development.autofill;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
final class AutofillTestingHelper {
private final ContentResolver mResolver;
AutofillTestingHelper(Context context) {
mResolver = context.getContentResolver();
}
public void setLoggingLevel(int max) {
setGlobal(Settings.Global.AUTOFILL_LOGGING_LEVEL, max);
}
public void setMaxPartitionsSize(int max) {
setGlobal(Settings.Global.AUTOFILL_MAX_PARTITIONS_SIZE, max);
}
public void setMaxVisibleDatasets(int level) {
setGlobal(Settings.Global.AUTOFILL_MAX_VISIBLE_DATASETS, level);
}
public int getLoggingLevel() throws SettingNotFoundException {
return getGlobal(Settings.Global.AUTOFILL_LOGGING_LEVEL);
}
public int getMaxPartitionsSize() throws SettingNotFoundException {
return getGlobal(Settings.Global.AUTOFILL_MAX_PARTITIONS_SIZE);
}
public int getMaxVisibleDatasets() throws SettingNotFoundException {
return getGlobal(Settings.Global.AUTOFILL_MAX_VISIBLE_DATASETS);
}
private void setGlobal(String key, int value) {
Settings.Global.putInt(mResolver, key, value);
}
private int getGlobal(String key) throws SettingNotFoundException {
return Settings.Global.getInt(mResolver, key);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -22,14 +22,15 @@ import android.os.SystemProperties;
import android.provider.Settings;
import android.util.FeatureFlagUtils;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class FeatureFlagPersistentTest {
private static final String TEST_FEATURE_NAME = "test_feature";
@@ -43,7 +44,7 @@ public class FeatureFlagPersistentTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
FeatureFlagPersistent.getAllPersistentFlags().add(TEST_FEATURE_NAME);
FeatureFlagUtils.getAllFeatureFlags().put(TEST_FEATURE_NAME, "false");
@@ -137,7 +138,8 @@ public class FeatureFlagPersistentTest {
FeatureFlagPersistent.setEnabled(mContext, TEST_FEATURE_NAME, true);
assertThat(SystemProperties.get(FeatureFlagUtils.PERSIST_PREFIX + TEST_FEATURE_NAME)).isEqualTo("true");
assertThat(SystemProperties.get(FeatureFlagUtils.PERSIST_PREFIX + TEST_FEATURE_NAME))
.isEqualTo("true");
assertThat(FeatureFlagUtils.isEnabled(mContext, TEST_FEATURE_NAME)).isTrue();
}
@@ -150,7 +152,8 @@ public class FeatureFlagPersistentTest {
FeatureFlagPersistent.setEnabled(mContext, TEST_FEATURE_NAME, false);
assertThat(SystemProperties.get(FeatureFlagUtils.PERSIST_PREFIX + TEST_FEATURE_NAME)).isEqualTo("false");
assertThat(SystemProperties.get(FeatureFlagUtils.PERSIST_PREFIX + TEST_FEATURE_NAME))
.isEqualTo("false");
assertThat(FeatureFlagUtils.isEnabled(mContext, TEST_FEATURE_NAME)).isFalse();
}
}

View File

@@ -22,15 +22,15 @@ import android.content.Context;
import android.os.SystemProperties;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class TranscodeDefaultOptionPreferenceControllerTest {
private static final String TRANSCODE_DEFAULT_SYS_PROP_KEY =
"persist.sys.fuse.transcode_default";

View File

@@ -22,13 +22,13 @@ import android.content.Context;
import android.os.SystemProperties;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class TranscodeGlobalTogglePreferenceControllerTest {
private static final String TRANSCODE_ENABLED_PROP_KEY = "persist.sys.fuse.transcode_enabled";

View File

@@ -22,15 +22,15 @@ import android.content.Context;
import android.os.SystemProperties;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class TranscodeUserControlPreferenceControllerTest {
private static final String TRANSCODE_USER_CONTROL_SYS_PROP_KEY =
"persist.sys.fuse.transcode_user_control";

View File

@@ -1,3 +1,18 @@
/*
* Copyright (C) 2020 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.deviceinfo.storage;
import static com.google.common.truth.Truth.assertThat;
@@ -8,14 +23,15 @@ import static org.mockito.Mockito.when;
import android.os.storage.VolumeInfo;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settingslib.deviceinfo.PrivateStorageInfo;
import com.android.settingslib.deviceinfo.StorageVolumeProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class VolumeSizesLoaderTest {
@Test
public void getVolumeSize_getsValidSizes() throws Exception {

View File

@@ -1,3 +1,18 @@
/*
* Copyright (C) 2020 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.fuelgauge.batterysaver;
import static com.google.common.truth.Truth.assertThat;
@@ -9,13 +24,14 @@ import android.provider.Settings;
import android.provider.Settings.Global;
import android.provider.Settings.Secure;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class BatterySaverScheduleRadioButtonsControllerTest {
private Context mContext;
private ContentResolver mResolver;
@@ -24,7 +40,7 @@ public class BatterySaverScheduleRadioButtonsControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mSeekBarController = new BatterySaverScheduleSeekBarController(mContext);
mController = new BatterySaverScheduleRadioButtonsController(
mContext, mSeekBarController);

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -21,13 +21,15 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.provider.Settings;
import android.provider.Settings.Global;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class BatterySaverStickyPreferenceControllerTest {
private static final String PREF_KEY = "battery_saver_sticky";
@@ -37,7 +39,7 @@ public class BatterySaverStickyPreferenceControllerTest {
@Before
public void setup() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mController = new BatterySaverStickyPreferenceController(mContext, PREF_KEY);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -21,6 +21,8 @@ import static com.google.common.truth.Truth.assertThat;
import android.os.Parcel;
import android.text.format.DateUtils;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -28,9 +30,8 @@ import org.junit.runner.RunWith;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class AppInfoTest {
private static final String PACKAGE_NAME = "com.android.app";

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -22,13 +22,14 @@ import android.content.Context;
import android.provider.Settings;
import android.text.format.DateUtils;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class BatteryTipPolicyTest {
private static final String BATTERY_TIP_CONSTANTS_VALUE = "battery_tip_enabled=true"
@@ -54,7 +55,7 @@ public class BatteryTipPolicyTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
}
@Test

View File

@@ -21,16 +21,16 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.core.TogglePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class OneHandedAppTapsExitPreferenceControllerTest {
private static final String KEY = "gesture_app_taps_to_exit";
@@ -42,7 +42,7 @@ public class OneHandedAppTapsExitPreferenceControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mController = new OneHandedAppTapsExitPreferenceController(mContext, KEY);
mSwitchPreference = new SwitchPreference(mContext);
mSwitchPreference.setKey(KEY);

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -22,14 +22,15 @@ import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class CardDatabaseHelperTest {
private Context mContext;
@@ -38,7 +39,7 @@ public class CardDatabaseHelperTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mCardDatabaseHelper = CardDatabaseHelper.getInstance(mContext);
mDatabase = mCardDatabaseHelper.getReadableDatabase();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -18,18 +18,19 @@ package com.android.settings.homepage.contextualcards;
import static com.google.common.truth.Truth.assertThat;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.homepage.contextualcards.ContextualCardLookupTable.ControllerRendererMapping;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class ContextualCardLookupTableTest {
private static final int UNSUPPORTED_CARD_TYPE = -99999;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -11,9 +11,8 @@
* 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
* limitations under the License.
*/
package com.android.settings.homepage.contextualcards;
import static com.android.settings.intelligence.ContextualCardProto.ContextualCard.Category.IMPORTANT_VALUE;
@@ -23,15 +22,16 @@ import static com.google.common.truth.Truth.assertThat;
import android.net.Uri;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class ContextualCardsDiffCallbackTest {
private static final Uri TEST_SLICE_URI = Uri.parse("content://test/test");

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -18,13 +18,14 @@ package com.android.settings.homepage.contextualcards.conditional;
import static com.google.common.truth.Truth.assertThat;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.homepage.contextualcards.ContextualCard;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class ConditionFooterContextualCardTest {
@Test(expected = IllegalArgumentException.class)

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -18,13 +18,14 @@ package com.android.settings.homepage.contextualcards.conditional;
import static com.google.common.truth.Truth.assertThat;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.homepage.contextualcards.ContextualCard;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class ConditionHeaderContextualCardTest {
@Test(expected = IllegalArgumentException.class)

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -21,13 +21,14 @@ import static com.android.settings.homepage.contextualcards.conditional.Conditio
import static com.google.common.truth.Truth.assertThat;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.homepage.contextualcards.ContextualCard;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class ConditionalContextualCardTest {
@Test(expected = IllegalArgumentException.class)

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -11,9 +11,8 @@
* 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
* limitations under the License.
*/
package com.android.settings.homepage.contextualcards.slices;
import static com.google.common.truth.Truth.assertThat;
@@ -31,14 +30,14 @@ import android.provider.Settings;
import androidx.slice.SliceMetadata;
import androidx.slice.SliceProvider;
import androidx.slice.widget.SliceLiveData;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class FaceSetupSliceTest {
private Context mContext;
@@ -48,7 +47,7 @@ public class FaceSetupSliceTest {
public void setUp() {
// Set-up specs for SliceMetadata.
SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
mContext = spy(RuntimeEnvironment.application);
mContext = spy(ApplicationProvider.getApplicationContext());
mPackageManager = mock(PackageManager.class);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
}

View File

@@ -0,0 +1,104 @@
/*
* Copyright (C) 2020 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.network.ims;
import android.content.Context;
import android.telephony.ims.ImsException;
/**
* Controller class for mock VT status
*/
public class MockVtQueryImsState extends VtQueryImsState {
private Boolean mIsTtyOnVolteEnabled;
private Boolean mIsEnabledOnPlatform;
private Boolean mIsProvisionedOnDevice;
private Boolean mIsEnabledByUser;
private Boolean mIsServiceStateReady;
/**
* Constructor
*
* @param context {@link Context}
* @param subId subscription's id
*/
public MockVtQueryImsState(Context context, int subId) {
super(context, subId);
}
public void setIsTtyOnVolteEnabled(boolean enabled) {
mIsTtyOnVolteEnabled = enabled;
}
@Override
boolean isTtyOnVolteEnabled(int subId) {
if (mIsTtyOnVolteEnabled != null) {
return mIsTtyOnVolteEnabled;
}
return super.isTtyOnVolteEnabled(subId);
}
public void setIsEnabledByPlatform(boolean isEnabled) {
mIsEnabledOnPlatform = isEnabled;
}
@Override
boolean isEnabledByPlatform(int subId) throws InterruptedException, ImsException,
IllegalArgumentException {
if (mIsEnabledOnPlatform != null) {
return mIsEnabledOnPlatform;
}
return super.isEnabledByPlatform(subId);
}
public void setIsProvisionedOnDevice(boolean isProvisioned) {
mIsProvisionedOnDevice = isProvisioned;
}
@Override
boolean isProvisionedOnDevice(int subId) {
if (mIsProvisionedOnDevice != null) {
return mIsProvisionedOnDevice;
}
return super.isProvisionedOnDevice(subId);
}
public void setServiceStateReady(boolean isReady) {
mIsServiceStateReady = isReady;
}
@Override
boolean isServiceStateReady(int subId) throws InterruptedException, ImsException,
IllegalArgumentException {
if (mIsServiceStateReady != null) {
return mIsServiceStateReady;
}
return super.isServiceStateReady(subId);
}
public void setIsEnabledByUser(boolean enabled) {
mIsEnabledByUser = enabled;
}
@Override
boolean isEnabledByUser(int subId) {
if (mIsEnabledByUser != null) {
return mIsEnabledByUser;
}
return super.isEnabledByUser(subId);
}
}

View File

@@ -0,0 +1,106 @@
/*
* Copyright (C) 2020 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.network.ims;
import android.content.Context;
import android.telephony.ims.ImsException;
/**
* Controller class for mock Wifi calling status
*/
public class MockWifiCallingQueryImsState extends WifiCallingQueryImsState {
private Boolean mIsTtyOnVolteEnabled;
private Boolean mIsEnabledOnPlatform;
private Boolean mIsProvisionedOnDevice;
private Boolean mIsServiceStateReady;
private Boolean mIsEnabledByUser;
/**
* Constructor
*
* @param context {@code Context}
* @param subId subscription's id
*/
public MockWifiCallingQueryImsState(Context context, int subId) {
super(context, subId);
}
public void setIsTtyOnVolteEnabled(boolean enabled) {
mIsTtyOnVolteEnabled = enabled;
}
@Override
boolean isTtyOnVolteEnabled(int subId) {
if (mIsTtyOnVolteEnabled != null) {
return mIsTtyOnVolteEnabled;
}
return super.isTtyOnVolteEnabled(subId);
}
public void setIsEnabledByPlatform(boolean isEnabled) {
mIsEnabledOnPlatform = isEnabled;
}
@Override
boolean isEnabledByPlatform(int subId) throws InterruptedException, ImsException,
IllegalArgumentException {
if (mIsEnabledOnPlatform != null) {
return mIsEnabledOnPlatform;
}
return super.isEnabledByPlatform(subId);
}
public void setIsProvisionedOnDevice(boolean isProvisioned) {
mIsProvisionedOnDevice = isProvisioned;
}
@Override
boolean isProvisionedOnDevice(int subId) {
if (mIsProvisionedOnDevice != null) {
return mIsProvisionedOnDevice;
}
return super.isProvisionedOnDevice(subId);
}
public void setServiceStateReady(boolean isReady) {
mIsServiceStateReady = isReady;
}
@Override
boolean isServiceStateReady(int subId) throws InterruptedException, ImsException,
IllegalArgumentException {
if (mIsServiceStateReady != null) {
return mIsServiceStateReady;
}
return super.isServiceStateReady(subId);
}
public void setIsEnabledByUser(boolean enabled) {
mIsEnabledByUser = enabled;
}
@Override
boolean isEnabledByUser(int subId) {
if (mIsEnabledByUser != null) {
return mIsEnabledByUser;
}
return super.isEnabledByUser(subId);
}
}

View File

@@ -18,20 +18,22 @@ package com.android.settings.nfc;
import static com.google.common.truth.Truth.assertThat;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class NfcDetectionPointControllerTest {
private NfcDetectionPointController mController;
@Before
public void setUp() {
mController = new NfcDetectionPointController(RuntimeEnvironment.application, "fakeKey");
mController = new NfcDetectionPointController(ApplicationProvider.getApplicationContext(),
"fakeKey");
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -20,13 +20,14 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class SecureNfcPreferenceControllerTest {
private Context mContext;
@@ -34,7 +35,7 @@ public class SecureNfcPreferenceControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mController = new SecureNfcPreferenceController(mContext, "nfc_secure_settings");
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (C) 2020 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.notification.zen;
import android.app.AutomaticZenRule;
import android.app.NotificationManager;
import android.service.notification.ZenPolicy;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
abstract class ZenRuleCustomPrefContrTestBase {
public static final String RULE_ID = "test_rule_id";
public static final String PREF_KEY = "main_pref";
AutomaticZenRule mRule = new AutomaticZenRule("test", null, null, null, null,
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
abstract AbstractZenCustomRulePreferenceController getController();
void updateControllerZenPolicy(ZenPolicy policy) {
mRule.setZenPolicy(policy);
getController().onResume(mRule, RULE_ID);
}
}

View File

@@ -0,0 +1,119 @@
/*
* Copyright (C) 2020 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.panel;
import static com.android.settings.slices.CustomSliceRegistry.WIFI_SLICE_URI;
import android.app.settings.SettingsEnums;
import android.content.Intent;
import android.net.Uri;
import androidx.core.graphics.drawable.IconCompat;
import java.util.Arrays;
import java.util.List;
/**
* Fake PanelContent for testing.
*/
public class FakePanelContent implements PanelContent {
public static final String FAKE_ACTION = "fake_action";
public static final CharSequence TITLE = "title";
public static final List<Uri> SLICE_URIS = Arrays.asList(
WIFI_SLICE_URI
);
public static final Intent INTENT = new Intent();
private CharSequence mTitle = TITLE;
private CharSequence mSubTitle;
private IconCompat mIcon;
private int mViewType;
private boolean mIsCustomizedButtonUsed = false;
private CharSequence mCustomizedButtonTitle;
@Override
public IconCompat getIcon() {
return mIcon;
}
public void setIcon(IconCompat icon) {
mIcon = icon;
}
@Override
public CharSequence getSubTitle() {
return mSubTitle;
}
public void setSubTitle(CharSequence subTitle) {
mSubTitle = subTitle;
}
@Override
public CharSequence getTitle() {
return mTitle;
}
public void setTitle(CharSequence title) {
mTitle = title;
}
@Override
public List<Uri> getSlices() {
return SLICE_URIS;
}
@Override
public Intent getSeeMoreIntent() {
return INTENT;
}
@Override
public int getMetricsCategory() {
return SettingsEnums.TESTING;
}
public void setViewType(int viewType) {
mViewType = viewType;
}
@Override
public int getViewType() {
return mViewType;
}
@Override
public boolean isCustomizedButtonUsed() {
return mIsCustomizedButtonUsed;
}
public void setIsCustomizedButtonUsed(boolean isUsed) {
mIsCustomizedButtonUsed = isUsed;
}
@Override
public CharSequence getCustomizedButtonTitle() {
return mCustomizedButtonTitle;
}
public void setCustomizedButtonTitle(CharSequence title) {
mCustomizedButtonTitle = title;
}
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright (C) 2020 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.panel;
import android.content.ComponentName;
import android.content.Intent;
public class FakeSettingsPanelActivity extends SettingsPanelActivity {
@Override
public ComponentName getCallingActivity() {
return new ComponentName("fake-package", "fake-class");
}
@Override
public Intent getIntent() {
final Intent intent = new Intent(FakePanelContent.FAKE_ACTION);
return intent;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -20,25 +20,25 @@ import static com.google.common.truth.Truth.assertThat;
import android.net.Uri;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.slices.CustomSliceRegistry;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class NfcPanelTest {
private NfcPanel mPanel;
@Before
public void setUp() {
mPanel = NfcPanel.create(RuntimeEnvironment.application);
mPanel = NfcPanel.create(ApplicationProvider.getApplicationContext());
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -12,9 +12,7 @@
* 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.panel;
import static com.android.settings.panel.SettingsPanelActivity.KEY_MEDIA_PACKAGE_NAME;
@@ -26,13 +24,14 @@ import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class PanelFeatureProviderImplTest {
private static final String TEST_PACKAGENAME = "com.test.packagename";
@@ -43,7 +42,7 @@ public class PanelFeatureProviderImplTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mProvider = new PanelFeatureProviderImpl();
mBundle = new Bundle();
mBundle.putString(KEY_MEDIA_PACKAGE_NAME, TEST_PACKAGENAME);

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -22,14 +22,14 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.net.Uri;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class PanelSlicesLoaderCountdownLatchTest {
private Context mContext;
@@ -43,7 +43,7 @@ public class PanelSlicesLoaderCountdownLatchTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mSliceCountdownLatch = new PanelSlicesLoaderCountdownLatch(URIS.length);
}
@@ -89,4 +89,4 @@ public class PanelSlicesLoaderCountdownLatchTest {
// Verify the second call returns false without external state change
assertThat(mSliceCountdownLatch.isPanelReadyToLoad()).isFalse();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -20,24 +20,25 @@ import static com.google.common.truth.Truth.assertThat;
import android.net.Uri;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.slices.CustomSliceRegistry;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class WifiPanelTest {
private WifiPanel mPanel;
@Before
public void setUp() {
mPanel = WifiPanel.create(RuntimeEnvironment.application);
mPanel = WifiPanel.create(ApplicationProvider.getApplicationContext());
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -11,20 +11,20 @@
* 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
* limitations under the License.
*/
package com.android.settings.password;
import static com.google.common.truth.Truth.assertThat;
import android.app.admin.DevicePolicyManager;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class ScreenLockTypeTest {
@Test

View File

@@ -18,6 +18,8 @@ package com.android.settings.search;
import static com.google.common.truth.Truth.assertThat;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.DisplaySettings;
import com.android.settings.backup.UserBackupSettingsActivity;
import com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment;
@@ -39,9 +41,8 @@ import com.android.settings.wifi.WifiSettings;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class CustomSiteMapRegistryTest {
@Test

View File

@@ -0,0 +1,53 @@
/*
* Copyright (C) 2020 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.search;
import android.util.Log;
import com.android.settingslib.search.Indexable;
import java.lang.reflect.Field;
/**
* Utility class for {@like DatabaseIndexingManager} to handle the mapping between Payloads
* and Preference controllers, and managing indexable classes.
*/
public class DatabaseIndexingUtils {
private static final String TAG = "IndexingUtil";
public static final String FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER =
"SEARCH_INDEX_DATA_PROVIDER";
public static Indexable.SearchIndexProvider getSearchIndexProvider(final Class<?> clazz) {
try {
final Field f = clazz.getField(FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER);
return (Indexable.SearchIndexProvider) f.get(null);
} catch (NoSuchFieldException e) {
Log.d(TAG, "Cannot find field '" + FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER + "'");
} catch (SecurityException se) {
Log.d(TAG,
"Security exception for field '" + FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER + "'");
} catch (IllegalAccessException e) {
Log.d(TAG, "Illegal access to field '" + FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER + "'");
} catch (IllegalArgumentException e) {
Log.d(TAG, "Illegal argument when accessing field '"
+ FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER + "'");
}
return null;
}
}

View File

@@ -0,0 +1,117 @@
/*
* Copyright (C) 2020 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.search;
import android.content.Context;
import android.provider.SearchIndexableResource;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.search.SearchIndexableRaw;
import java.util.ArrayList;
import java.util.List;
/**
* Test class for Settings Search Indexing.
* If you change this class, please run robotests to make sure they still pass.
*/
public class FakeSettingsFragment extends DashboardFragment {
public static final String TITLE = "raw title";
public static final String SUMMARY_ON = "raw summary on";
public static final String SUMMARY_OFF = "raw summary off";
public static final String ENTRIES = "rawentries";
public static final String KEYWORDS = "keywords, keywordss, keywordsss";
public static final String SPACE_KEYWORDS = "keywords keywordss keywordsss";
public static final String SCREEN_TITLE = "raw screen title";
public static final String CLASS_NAME = FakeSettingsFragment.class.getName();
public static final int ICON = 0xff;
public static final String INTENT_ACTION = "raw action";
public static final String PACKAGE_NAME = "raw target package";
public static final String TARGET_CLASS = "raw target class";
public static final String TARGET_PACKAGE = "raw package name";
public static final String KEY = "raw key";
public static final boolean ENABLED = true;
@Override
public int getMetricsCategory() {
return MetricsProto.MetricsEvent.DISPLAY;
}
@Override
protected String getLogTag() {
return "";
}
@Override
protected int getPreferenceScreenResId() {
return com.android.settings.R.xml.display_settings;
}
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
return null;
}
/** Index provider used to expose this fragment in search. */
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {
@Override
public List<SearchIndexableRaw> getRawDataToIndex(Context context,
boolean enabled) {
final SearchIndexableRaw data = new SearchIndexableRaw(context);
data.title = TITLE;
data.summaryOn = SUMMARY_ON;
data.summaryOff = SUMMARY_OFF;
data.entries = ENTRIES;
data.keywords = KEYWORDS;
data.screenTitle = SCREEN_TITLE;
data.packageName = PACKAGE_NAME;
data.intentAction = INTENT_ACTION;
data.intentTargetClass = TARGET_CLASS;
data.intentTargetPackage = TARGET_PACKAGE;
data.key = KEY;
data.iconResId = ICON;
data.enabled = ENABLED;
final List<SearchIndexableRaw> result = new ArrayList<>(1);
result.add(data);
return result;
}
@Override
public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
boolean enabled) {
final ArrayList<SearchIndexableResource> result = new ArrayList<>();
final SearchIndexableResource sir = new SearchIndexableResource(context);
sir.xmlResId = com.android.settings.R.xml.display_settings;
result.add(sir);
return result;
}
@Override
public List<String> getNonIndexableKeys(Context context) {
List<String> keys = super.getNonIndexableKeys(context);
keys.add("pref_key_1");
keys.add("pref_key_3");
return keys;
}
};
}

View File

@@ -0,0 +1,204 @@
/*
* Copyright (C) 2020 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.search;
import static com.google.common.truth.Truth.assertWithMessage;
import android.provider.SearchIndexableResource;
import android.util.ArraySet;
import android.util.Log;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.core.codeinspection.CodeInspector;
import com.android.settings.dashboard.DashboardFragmentSearchIndexProviderInspector;
import com.android.settingslib.search.Indexable;
import com.android.settingslib.search.SearchIndexableData;
import com.android.settingslib.search.SearchIndexableResources;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* {@link CodeInspector} to ensure fragments implement search components correctly.
*/
public class SearchIndexProviderCodeInspector extends CodeInspector {
private static final String TAG = "SearchCodeInspector";
private static final String NOT_CONTAINING_PROVIDER_OBJECT_ERROR =
"Indexable should have public field "
+ DatabaseIndexingUtils.FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER
+ " but these are not:\n";
private static final String NOT_SHARING_PREF_CONTROLLERS_BETWEEN_FRAG_AND_PROVIDER =
"DashboardFragment should share pref controllers with its SearchIndexProvider, but "
+ " these are not: \n";
private static final String NOT_IN_INDEXABLE_PROVIDER_REGISTRY =
"Class containing " + DatabaseIndexingUtils.FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER
+ " must be added to " + SearchIndexableResources.class.getName()
+ " but these are not: \n";
private static final String NOT_PROVIDING_VALID_RESOURCE_ERROR =
"SearchIndexableProvider must either provide no resource to index, or valid ones. "
+ "But the followings contain resource with xml id = 0\n";
private final List<String> mNotImplementingIndexProviderExemptList;
private final List<String> mNotInSearchIndexableRegistryExemptList;
private final List<String> mNotSharingPrefControllersExemptList;
public SearchIndexProviderCodeInspector(List<Class<?>> classes) {
super(classes);
mNotImplementingIndexProviderExemptList = new ArrayList<>();
mNotInSearchIndexableRegistryExemptList = new ArrayList<>();
mNotSharingPrefControllersExemptList = new ArrayList<>();
initializeExemptList(mNotImplementingIndexProviderExemptList,
"exempt_not_implementing_index_provider");
initializeExemptList(mNotInSearchIndexableRegistryExemptList,
"exempt_not_in_search_index_provider_registry");
initializeExemptList(mNotSharingPrefControllersExemptList,
"exempt_not_sharing_pref_controllers_with_search_provider");
}
@Override
public void run() {
final Set<String> notImplementingIndexProvider = new ArraySet<>();
final Set<String> notInSearchProviderRegistry = new ArraySet<>();
final Set<String> notSharingPreferenceControllers = new ArraySet<>();
final Set<String> notProvidingValidResource = new ArraySet<>();
final Set<Class> providerClasses = new ArraySet<>();
final SearchFeatureProvider provider = new SearchFeatureProviderImpl();
for (SearchIndexableData bundle :
provider.getSearchIndexableResources().getProviderValues()) {
providerClasses.add(bundle.getTargetClass());
}
for (Class clazz : mClasses) {
if (!isConcreteSettingsClass(clazz)) {
continue;
}
final String className = clazz.getName();
// Skip fragments if it's not SettingsPreferenceFragment.
if (!SettingsPreferenceFragment.class.isAssignableFrom(clazz)) {
continue;
}
final boolean hasSearchIndexProvider = hasSearchIndexProvider(clazz);
// If it implements Indexable, it must also implement the index provider field.
if (!hasSearchIndexProvider) {
if (!mNotImplementingIndexProviderExemptList.remove(className)) {
notImplementingIndexProvider.add(className);
}
continue;
}
// If it implements index provider field AND it's a DashboardFragment, its fragment and
// search provider must share the same set of PreferenceControllers.
final boolean isSharingPrefControllers = DashboardFragmentSearchIndexProviderInspector
.isSharingPreferenceControllers(clazz);
if (!isSharingPrefControllers) {
if (!mNotSharingPrefControllersExemptList.remove(className)) {
notSharingPreferenceControllers.add(className);
}
continue;
}
// Must be in SearchProviderRegistry
if (!providerClasses.contains(clazz)) {
if (!mNotInSearchIndexableRegistryExemptList.remove(className)) {
notInSearchProviderRegistry.add(className);
}
}
// Search provider must either don't provider resource xml, or provide valid ones.
if (!hasValidResourceFromProvider(clazz)) {
notProvidingValidResource.add(className);
}
}
// Build error messages
final String indexProviderError = buildErrorMessage(NOT_CONTAINING_PROVIDER_OBJECT_ERROR,
notImplementingIndexProvider);
final String notSharingPrefControllerError = buildErrorMessage(
NOT_SHARING_PREF_CONTROLLERS_BETWEEN_FRAG_AND_PROVIDER,
notSharingPreferenceControllers);
final String notInProviderRegistryError =
buildErrorMessage(NOT_IN_INDEXABLE_PROVIDER_REGISTRY, notInSearchProviderRegistry);
final String notProvidingValidResourceError = buildErrorMessage(
NOT_PROVIDING_VALID_RESOURCE_ERROR, notProvidingValidResource);
assertWithMessage(indexProviderError)
.that(notImplementingIndexProvider)
.isEmpty();
assertWithMessage(notSharingPrefControllerError)
.that(notSharingPreferenceControllers)
.isEmpty();
assertWithMessage(notInProviderRegistryError)
.that(notInSearchProviderRegistry)
.isEmpty();
assertWithMessage(notProvidingValidResourceError)
.that(notProvidingValidResource)
.isEmpty();
assertNoObsoleteInExemptList("exempt_not_implementing_index_provider",
mNotImplementingIndexProviderExemptList);
assertNoObsoleteInExemptList("exempt_not_in_search_index_provider_registry",
mNotInSearchIndexableRegistryExemptList);
assertNoObsoleteInExemptList(
"exempt_not_sharing_pref_controllers_with_search_provider",
mNotSharingPrefControllersExemptList);
}
private boolean hasSearchIndexProvider(Class clazz) {
try {
final Field f = clazz.getField(
DatabaseIndexingUtils.FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER);
return f != null;
} catch (NoClassDefFoundError e) {
// Cannot find class def, ignore
return true;
} catch (NoSuchFieldException e) {
Log.e(TAG, "error fetching search provider from class " + clazz.getName());
return false;
}
}
private boolean hasValidResourceFromProvider(Class clazz) {
try {
final Indexable.SearchIndexProvider provider =
DatabaseIndexingUtils.getSearchIndexProvider(clazz);
final List<SearchIndexableResource> resources = provider.getXmlResourcesToIndex(
ApplicationProvider.getApplicationContext(), true /* enabled */);
if (resources == null) {
// No resource, that's fine.
return true;
}
for (SearchIndexableResource res : resources) {
if (res.xmlResId == 0) {
// Invalid resource
return false;
}
}
} catch (Exception e) {
// Ignore.
}
return true;
}
private String buildErrorMessage(String errorSummary, Set<String> errorClasses) {
final StringBuilder error = new StringBuilder(errorSummary);
for (String c : errorClasses) {
error.append(c).append("\n");
}
return error.toString();
}
}

View File

@@ -20,15 +20,16 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class CredentialManagementAppButtonsControllerTest {
private Context mContext;
@@ -38,7 +39,7 @@ public class CredentialManagementAppButtonsControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mController = new CredentialManagementAppButtonsController(
mContext, PREF_KEY_CREDENTIAL_MANAGEMENT_APP);
}

View File

@@ -20,15 +20,16 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class CredentialManagementAppHeaderControllerTest {
private Context mContext;
@@ -38,7 +39,7 @@ public class CredentialManagementAppHeaderControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mController = new CredentialManagementAppHeaderController(
mContext, PREF_KEY_CREDENTIAL_MANAGEMENT_APP);
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (C) 2020 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.slices;
import android.content.Context;
import com.android.settings.core.BasePreferenceController;
public class FakeContextOnlyPreferenceController extends BasePreferenceController {
public static final String KEY = "fakeController2";
public FakeContextOnlyPreferenceController(Context context) {
super(context, KEY);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (C) 2020 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.slices;
import android.content.Context;
import com.android.settings.core.BasePreferenceController;
public class FakePreferenceController extends BasePreferenceController {
public FakePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public int getSliceType() {
return SliceData.SliceType.SLIDER;
}
@Override
public boolean isSliceable() {
return true;
}
@Override
public boolean isPublicSlice() {
return true;
}
@Override
public boolean useDynamicSliceSummary() {
return true;
}
}

View File

@@ -0,0 +1,174 @@
/*
* Copyright (C) 2020 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.slices;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_CONTROLLER;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import android.content.Context;
import android.os.Bundle;
import android.provider.SearchIndexableResource;
import android.text.TextUtils;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.PreferenceXmlParserUtils;
import com.android.settings.core.SliderPreferenceController;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.core.codeinspection.CodeInspector;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.SearchFeatureProvider;
import com.android.settings.search.SearchFeatureProviderImpl;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.search.Indexable;
import com.android.settingslib.search.SearchIndexableData;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class SliceControllerInXmlCodeInspector extends CodeInspector {
private static final List<Class> sSliceControllerClasses = Arrays.asList(
TogglePreferenceController.class,
SliderPreferenceController.class
);
private final List<String> mXmlDeclaredControllers = new ArrayList<>();
private final List<String> mExemptedClasses = new ArrayList<>();
private static final String ERROR_MISSING_CONTROLLER =
"The following controllers were expected to be declared by "
+ "'settings:controller=Controller_Class_Name' in their corresponding Xml. "
+ "If it should not appear in XML, add the controller's classname to "
+ "exempt_slice_controller_not_in_xml. Controllers:\n";
private final Context mContext;
private final SearchFeatureProvider mSearchProvider;
private final FakeFeatureFactory mFakeFeatureFactory;
public SliceControllerInXmlCodeInspector(List<Class<?>> classes) throws Exception {
super(classes);
mContext = ApplicationProvider.getApplicationContext();
mSearchProvider = new SearchFeatureProviderImpl();
mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
mFakeFeatureFactory.searchFeatureProvider = mSearchProvider;
CodeInspector.initializeExemptList(mExemptedClasses,
"exempt_slice_controller_not_in_xml");
initDeclaredControllers();
}
private void initDeclaredControllers() throws IOException, XmlPullParserException {
final List<Integer> xmlResources = getIndexableXml();
for (int xmlResId : xmlResources) {
final List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(mContext,
xmlResId, PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_PREF_CONTROLLER);
for (Bundle bundle : metadata) {
final String controllerClassName = bundle.getString(METADATA_CONTROLLER);
if (TextUtils.isEmpty(controllerClassName)) {
continue;
}
mXmlDeclaredControllers.add(controllerClassName);
}
}
// We definitely have some controllers in xml, so assert not-empty here as a proxy to
// make sure the parser didn't fail
assertThat(mXmlDeclaredControllers).isNotEmpty();
}
@Override
public void run() {
final List<String> missingControllersInXml = new ArrayList<>();
for (Class<?> clazz : mClasses) {
if (!isConcreteSettingsClass(clazz)) {
// Only care about non-abstract classes.
continue;
}
if (!isInlineSliceClass(clazz)) {
// Only care about inline-slice controller classes.
continue;
}
if (!mXmlDeclaredControllers.contains(clazz.getName())) {
// Class clazz should have been declared in XML (unless allowlisted).
missingControllersInXml.add(clazz.getName());
}
}
// Removed allowlisted classes
missingControllersInXml.removeAll(mExemptedClasses);
final String missingControllerError =
buildErrorMessage(ERROR_MISSING_CONTROLLER, missingControllersInXml);
assertWithMessage(missingControllerError).that(missingControllersInXml).isEmpty();
}
private boolean isInlineSliceClass(Class clazz) {
while (clazz != null) {
clazz = clazz.getSuperclass();
if (sSliceControllerClasses.contains(clazz)) {
return true;
}
}
return false;
}
private String buildErrorMessage(String errorSummary, List<String> errorClasses) {
final StringBuilder error = new StringBuilder(errorSummary);
for (String c : errorClasses) {
error.append(c).append("\n");
}
return error.toString();
}
private List<Integer> getIndexableXml() {
final List<Integer> xmlResSet = new ArrayList<>();
final Collection<SearchIndexableData> bundles = FeatureFactory.getFactory(
mContext).getSearchFeatureProvider().getSearchIndexableResources()
.getProviderValues();
for (SearchIndexableData bundle : bundles) {
Indexable.SearchIndexProvider provider = bundle.getSearchIndexProvider();
if (provider == null) {
continue;
}
List<SearchIndexableResource> resources = provider.getXmlResourcesToIndex(mContext,
true);
if (resources == null) {
continue;
}
for (SearchIndexableResource resource : resources) {
// Add '0's anyway. It won't break the test.
xmlResSet.add(resource.xmlResId);
}
}
return xmlResSet;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -20,24 +20,25 @@ import static com.google.common.truth.Truth.assertThat;
import android.net.Uri;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class SliceDataTest {
private final String KEY = "KEY";
private final String TITLE = "title";
private final String SUMMARY = "summary";
private final String SCREEN_TITLE = "screen title";
private final String KEYWORDS = "a, b, c";
private final String FRAGMENT_NAME = "fragment name";
private final int ICON = 1234; // I declare a thumb war
private final Uri URI = Uri.parse("content://com.android.settings.slices/test");
private final String PREF_CONTROLLER = "com.android.settings.slices.tester";
private final int SLICE_TYPE = SliceData.SliceType.SWITCH;
private final String UNAVAILABLE_SLICE_SUBTITLE = "subtitleOfUnavailableSlice";
private static final String KEY = "KEY";
private static final String TITLE = "title";
private static final String SUMMARY = "summary";
private static final String SCREEN_TITLE = "screen title";
private static final String KEYWORDS = "a, b, c";
private static final String FRAGMENT_NAME = "fragment name";
private static final int ICON = 1234; // I declare a thumb war
private static final Uri URI = Uri.parse("content://com.android.settings.slices/test");
private static final String PREF_CONTROLLER = "com.android.settings.slices.tester";
private static final int SLICE_TYPE = SliceData.SliceType.SWITCH;
private static final String UNAVAILABLE_SLICE_SUBTITLE = "subtitleOfUnavailableSlice";
@Test
public void testBuilder_buildsMatchingObject() {

View File

@@ -0,0 +1,82 @@
/*
* Copyright (C) 2020 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.slices;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.provider.SettingsSlicesContract;
import com.android.settings.testutils.FakeIndexProvider;
import com.android.settings.testutils.FakeToggleController;
class SliceTestUtils {
public static final String FAKE_TITLE = "title";
public static final String FAKE_SUMMARY = "summary";
public static final String FAKE_SCREEN_TITLE = "screen_title";
public static final String FAKE_KEYWORDS = "a, b, c";
public static final int FAKE_ICON = 1234;
public static final String FAKE_FRAGMENT_NAME = FakeIndexProvider.class.getName();
public static final String FAKE_CONTROLLER_NAME = FakeToggleController.class.getName();
public static void insertSliceToDb(Context context, String key) {
insertSliceToDb(context, key, true /* isPlatformSlice */);
}
public static void insertSliceToDb(Context context, String key, boolean isPlatformSlice) {
insertSliceToDb(context, key, isPlatformSlice, null /*customizedUnavailableSliceSubtitle*/);
}
public static void insertSliceToDb(Context context, String key, boolean isPlatformSlice,
String customizedUnavailableSliceSubtitle) {
insertSliceToDb(context, key, isPlatformSlice, customizedUnavailableSliceSubtitle, false);
}
public static void insertSliceToDb(Context context, String key, boolean isPlatformSlice,
String customizedUnavailableSliceSubtitle, boolean isPublicSlice) {
final SQLiteDatabase db = SlicesDatabaseHelper.getInstance(context).getWritableDatabase();
ContentValues values = new ContentValues();
values.put(SlicesDatabaseHelper.IndexColumns.KEY, key);
values.put(SlicesDatabaseHelper.IndexColumns.SLICE_URI,
new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(isPlatformSlice
? SettingsSlicesContract.AUTHORITY
: SettingsSliceProvider.SLICE_AUTHORITY)
.appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
.appendPath(key)
.build().toSafeString());
values.put(SlicesDatabaseHelper.IndexColumns.TITLE, FAKE_TITLE);
values.put(SlicesDatabaseHelper.IndexColumns.SUMMARY, FAKE_SUMMARY);
values.put(SlicesDatabaseHelper.IndexColumns.SCREENTITLE, FAKE_SCREEN_TITLE);
values.put(SlicesDatabaseHelper.IndexColumns.KEYWORDS, FAKE_KEYWORDS);
values.put(SlicesDatabaseHelper.IndexColumns.ICON_RESOURCE, FAKE_ICON);
values.put(SlicesDatabaseHelper.IndexColumns.FRAGMENT, FAKE_FRAGMENT_NAME);
values.put(SlicesDatabaseHelper.IndexColumns.CONTROLLER, FAKE_CONTROLLER_NAME);
values.put(SlicesDatabaseHelper.IndexColumns.SLICE_TYPE, SliceData.SliceType.INTENT);
values.put(SlicesDatabaseHelper.IndexColumns.UNAVAILABLE_SLICE_SUBTITLE,
customizedUnavailableSliceSubtitle);
values.put(SlicesDatabaseHelper.IndexColumns.PUBLIC_SLICE, isPublicSlice);
db.replaceOrThrow(SlicesDatabaseHelper.Tables.TABLE_SLICES_INDEX, null, values);
db.close();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -12,9 +12,7 @@
* 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.slices;
import static com.google.common.truth.Truth.assertThat;
@@ -27,24 +25,24 @@ import android.net.Uri;
import android.provider.SettingsSlicesContract;
import androidx.slice.Slice;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class SpecialCaseSliceManagerTest {
private final String FAKE_PARAMETER_KEY = "fake_parameter_key";
private final String FAKE_PARAMETER_VALUE = "fake_value";
private static final String FAKE_PARAMETER_KEY = "fake_parameter_key";
private static final String FAKE_PARAMETER_VALUE = "fake_value";
private Context mContext;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
CustomSliceRegistry.sUriToSlice.clear();
CustomSliceRegistry.sUriToSlice.put(FakeSliceable.URI, FakeSliceable.class);
}
@@ -113,7 +111,7 @@ public class SpecialCaseSliceManagerTest {
static final Slice SLICE = new Slice.Builder(URI).build();
static boolean backingData = false;
static boolean sBackingData = false;
final Context mContext;
@@ -133,7 +131,7 @@ public class SpecialCaseSliceManagerTest {
@Override
public void onNotifyChange(Intent intent) {
backingData = !backingData;
sBackingData = !sBackingData;
}
@Override

View File

@@ -26,14 +26,15 @@ import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class MediaControlsPreferenceControllerTest {
private static final String KEY = "media_controls_resume_switch";
@@ -46,7 +47,7 @@ public class MediaControlsPreferenceControllerTest {
@Before
public void setUp() {
mContext = spy(RuntimeEnvironment.application);
mContext = spy(ApplicationProvider.getApplicationContext());
mContentResolver = mContext.getContentResolver();
mOriginalQs = Settings.Global.getInt(mContentResolver,
Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS, 1);

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2020 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.testutils;
import android.content.pm.ApplicationInfo;
/**
* Helper for mocking installed applications.
*/
public class ApplicationTestUtils {
/**
* Create and populate an {@link android.content.pm.ApplicationInfo} object that describes an
* installed app.
*
* @param uid The app's uid
* @param packageName The app's package name.
* @param flags Flags describing the app. See {@link android.content.pm.ApplicationInfo#flags}
* for possible values.
* @param targetSdkVersion The app's target SDK version
*
* @see android.content.pm.ApplicationInfo
*/
public static ApplicationInfo buildInfo(int uid, String packageName, int flags,
int targetSdkVersion) {
final ApplicationInfo info = new ApplicationInfo();
info.uid = uid;
info.packageName = packageName;
info.flags = flags;
info.targetSdkVersion = targetSdkVersion;
return info;
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright (C) 2020 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.testutils;
import android.content.Intent;
import android.os.BatteryManager;
public class BatteryTestUtils {
public static Intent getChargingIntent() {
return getCustomBatteryIntent(
BatteryManager.BATTERY_PLUGGED_AC,
50 /* level */,
100 /* scale */,
BatteryManager.BATTERY_STATUS_CHARGING);
}
public static Intent getDischargingIntent() {
return getCustomBatteryIntent(
0 /* plugged */,
10 /* level */,
100 /* scale */,
BatteryManager.BATTERY_STATUS_DISCHARGING);
}
private static Intent getCustomBatteryIntent(int plugged, int level, int scale, int status) {
Intent intent = new Intent();
intent.putExtra(BatteryManager.EXTRA_PLUGGED, plugged);
intent.putExtra(BatteryManager.EXTRA_LEVEL, level);
intent.putExtra(BatteryManager.EXTRA_SCALE, scale);
intent.putExtra(BatteryManager.EXTRA_STATUS, status);
return intent;
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2020 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.testutils;
import android.app.Activity;
import android.content.Intent;
import android.os.UserHandle;
public class CustomActivity extends Activity {
@Override
public void startActivityAsUser(Intent intent, UserHandle user) {}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright (C) 2020 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.testutils;
import android.content.Context;
import com.android.settings.core.BasePreferenceController;
public class FakeCopyableController extends BasePreferenceController {
public FakeCopyableController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public boolean isSliceable() {
return true;
}
@Override
public boolean isCopyableSlice() {
return true;
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright (C) 2020 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.testutils;
import android.content.Context;
import com.android.settings.R;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.Indexable;
import java.util.List;
public class FakeIndexProvider implements Indexable {
public static final String KEY = "TestKey";
/**
* The fake SearchIndexProvider. Note that the use of location_settings below implies that tests
* using this should be using the res/xml-mcc999/location_settings.xml or
* res/xml-mcc998/location_settings.xml. Annotate tests with
* {@code @Config(qualifiers = "mcc999")}.
*/
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.location_settings) {
@Override
public List<String> getNonIndexableKeys(Context context) {
List<String> result = super.getNonIndexableKeys(context);
result.add(KEY);
return result;
}
};
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (C) 2020 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.testutils;
import android.content.Context;
public class FakeInvalidSliderController extends FakeSliderController {
public FakeInvalidSliderController(Context context, String key) {
super(context, key);
}
@Override
public int getMax() {
// Return 0 to make it invalid
return 0;
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2020 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.testutils;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.core.SliderPreferenceController;
public class FakeSliderController extends SliderPreferenceController {
public static final String AVAILABILITY_KEY = "fake_slider_availability_key";
public static final int MAX_VALUE = 9;
private static final String SETTING_KEY = "fake_slider_key";
public FakeSliderController(Context context, String key) {
super(context, key);
}
@Override
public int getSliderPosition() {
return Settings.System.getInt(mContext.getContentResolver(), SETTING_KEY, 0);
}
@Override
public boolean setSliderPosition(int position) {
return Settings.System.putInt(mContext.getContentResolver(), SETTING_KEY, position);
}
@Override
public int getMax() {
return MAX_VALUE;
}
@Override
public int getMin() {
return 0;
}
@Override
public int getAvailabilityStatus() {
return Settings.Global.getInt(mContext.getContentResolver(), AVAILABILITY_KEY, AVAILABLE);
}
@Override
public boolean isSliceable() {
return true;
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright (C) 2020 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.testutils;
import android.content.Context;
import android.content.IntentFilter;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.slices.SliceBackgroundWorker;
public class FakeToggleController extends TogglePreferenceController {
public static final String AVAILABILITY_KEY = "fake_toggle_availability_key";
public static final IntentFilter INTENT_FILTER = new IntentFilter(
WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
private static final String SETTING_KEY = "toggle_key";
private static final int ON = 1;
private static final int OFF = 0;
private boolean mIsAsyncUpdate = false;
public FakeToggleController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
public boolean isChecked() {
return Settings.System.getInt(mContext.getContentResolver(),
SETTING_KEY, OFF) == ON;
}
@Override
public boolean setChecked(boolean isChecked) {
return Settings.System.putInt(mContext.getContentResolver(), SETTING_KEY,
isChecked ? ON : OFF);
}
@Override
public int getAvailabilityStatus() {
return Settings.Global.getInt(mContext.getContentResolver(),
AVAILABILITY_KEY, AVAILABLE);
}
@Override
public IntentFilter getIntentFilter() {
return INTENT_FILTER;
}
@Override
public boolean isSliceable() {
return true;
}
@Override
public Class<? extends SliceBackgroundWorker> getBackgroundWorkerClass() {
return TestWorker.class;
}
@Override
public boolean hasAsyncUpdate() {
return mIsAsyncUpdate;
}
public void setAsyncUpdate(boolean isAsyncUpdate) {
mIsAsyncUpdate = isAsyncUpdate;
}
public static class TestWorker extends SliceBackgroundWorker<Void> {
public TestWorker(Context context, Uri uri) {
super(context, uri);
}
@Override
protected void onSlicePinned() {
}
@Override
protected void onSliceUnpinned() {
}
@Override
public void close() {
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (C) 2020 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.testutils;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.core.BasePreferenceController;
public class FakeUnavailablePreferenceController extends BasePreferenceController {
public static final String AVAILABILITY_KEY = "fake_availability_key";
public FakeUnavailablePreferenceController(Context context) {
super(context, "key");
}
@Override
public int getAvailabilityStatus() {
return Settings.Global.getInt(mContext.getContentResolver(), AVAILABILITY_KEY, 0);
}
@Override
public boolean isSliceable() {
return true;
}
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright (C) 2020 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.testutils;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;
import com.google.common.base.Preconditions;
/**
* Helper for building {@link ResolveInfo}s to be used in Robolectric tests.
*
* <p>The resulting {@link PackageInfo}s should typically be added to {@link
* org.robolectric.shadows.ShadowPackageManager#addResolveInfoForIntent(Intent, ResolveInfo)}.
*/
public final class ResolveInfoBuilder {
private final String mPackageName;
private ActivityInfo mActivityInfo;
private ProviderInfo mProviderInfo;
public ResolveInfoBuilder(String packageName) {
this.mPackageName = Preconditions.checkNotNull(packageName);
}
public ResolveInfoBuilder setActivity(String packageName, String className) {
mActivityInfo = new ActivityInfo();
mActivityInfo.packageName = packageName;
mActivityInfo.name = className;
return this;
}
public ResolveInfoBuilder setProvider(
String packageName, String className, String authority, boolean isSystemApp) {
mProviderInfo = new ProviderInfo();
mProviderInfo.authority = authority;
mProviderInfo.applicationInfo = new ApplicationInfo();
if (isSystemApp) {
mProviderInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
}
mProviderInfo.packageName = mPackageName;
mProviderInfo.applicationInfo.packageName = mPackageName;
mProviderInfo.name = className;
return this;
}
public ResolveInfo build() {
ResolveInfo info = new ResolveInfo();
info.activityInfo = mActivityInfo;
info.resolvePackageName = mPackageName;
info.providerInfo = mProviderInfo;
return info;
}
}

View File

@@ -0,0 +1,324 @@
/*
* Copyright (C) 2020 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.testutils;
import static android.app.slice.Slice.HINT_TITLE;
import static android.app.slice.Slice.SUBTYPE_COLOR;
import static android.app.slice.SliceItem.FORMAT_IMAGE;
import static android.app.slice.SliceItem.FORMAT_INT;
import static android.app.slice.SliceItem.FORMAT_TEXT;
import static com.google.common.truth.Truth.assertThat;
import android.app.PendingIntent;
import android.content.Context;
import android.text.TextUtils;
import androidx.core.graphics.drawable.IconCompat;
import androidx.slice.Slice;
import androidx.slice.SliceItem;
import androidx.slice.SliceMetadata;
import androidx.slice.builders.ListBuilder;
import androidx.slice.core.SliceAction;
import androidx.slice.core.SliceQuery;
import androidx.slice.widget.EventInfo;
import com.android.settings.Utils;
import com.android.settings.slices.SettingsSliceProvider;
import com.android.settings.slices.SliceBuilderUtils;
import com.android.settings.slices.SliceData;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Testing utility class to verify the contents of the different Settings Slices.
*
* TODO (77712944) check Summary, range (metadata.getRange()), toggle icons.
*/
public class SliceTester {
/**
* Test the contents of an intent based slice, including:
* - No toggles
* - Correct intent
* - Correct title
* - Correct keywords
* - TTL
* - Color
*/
public static void testSettingsIntentSlice(Context context, Slice slice, SliceData sliceData) {
final SliceMetadata metadata = SliceMetadata.from(context, slice);
final long sliceTTL = metadata.getExpiry();
assertThat(sliceTTL).isEqualTo(ListBuilder.INFINITY);
final SliceItem colorItem = SliceQuery.findSubtype(slice, FORMAT_INT, SUBTYPE_COLOR);
final int color = colorItem.getInt();
assertThat(color).isEqualTo(Utils.getColorAccentDefaultColor(context));
final List<SliceAction> toggles = metadata.getToggles();
assertThat(toggles).isEmpty();
final PendingIntent primaryPendingIntent = metadata.getPrimaryAction().getAction();
assertThat(primaryPendingIntent).isEqualTo(
SliceBuilderUtils.getContentPendingIntent(context, sliceData));
assertThat(metadata.getTitle()).isEqualTo(sliceData.getTitle());
assertKeywords(metadata, sliceData);
}
/**
* Test the contents of an toggle based slice, including:
* - Contains one toggle
* - Correct toggle intent
* - Correct content intent
* - Correct title
* - Correct keywords
* - TTL
* - Color
*/
public static void testSettingsToggleSlice(Context context, Slice slice, SliceData sliceData) {
final SliceMetadata metadata = SliceMetadata.from(context, slice);
final SliceItem colorItem = SliceQuery.findSubtype(slice, FORMAT_INT, SUBTYPE_COLOR);
final int color = colorItem.getInt();
assertThat(color).isEqualTo(Utils.getColorAccentDefaultColor(context));
final List<SliceAction> toggles = metadata.getToggles();
assertThat(toggles).hasSize(1);
final long sliceTTL = metadata.getExpiry();
assertThat(sliceTTL).isEqualTo(ListBuilder.INFINITY);
final SliceAction mainToggleAction = toggles.get(0);
assertThat(mainToggleAction.getIcon()).isNull();
// Check intent in Toggle Action
final PendingIntent togglePendingIntent = mainToggleAction.getAction();
assertThat(togglePendingIntent).isEqualTo(SliceBuilderUtils.getActionIntent(context,
SettingsSliceProvider.ACTION_TOGGLE_CHANGED, sliceData));
// Check primary intent
final PendingIntent primaryPendingIntent = metadata.getPrimaryAction().getAction();
assertThat(primaryPendingIntent).isEqualTo(
SliceBuilderUtils.getContentPendingIntent(context, sliceData));
assertThat(metadata.getTitle()).isEqualTo(sliceData.getTitle());
assertKeywords(metadata, sliceData);
}
/**
* Test the contents of an slider based slice, including:
* - No intent
* - Correct title
* - Correct keywords
* - TTL
* - Color
*/
public static void testSettingsSliderSlice(Context context, Slice slice, SliceData sliceData) {
final SliceMetadata metadata = SliceMetadata.from(context, slice);
final SliceAction primaryAction = metadata.getPrimaryAction();
final IconCompat icon = primaryAction.getIcon();
if (icon == null) {
final SliceItem colorItem = SliceQuery.findSubtype(slice, FORMAT_INT, SUBTYPE_COLOR);
final int color = colorItem.getInt();
assertThat(color).isEqualTo(Utils.getColorAccentDefaultColor(context));
} else {
final IconCompat expectedIcon = IconCompat.createWithResource(context,
sliceData.getIconResource());
assertThat(expectedIcon.toString()).isEqualTo(icon.toString());
}
final long sliceTTL = metadata.getExpiry();
assertThat(sliceTTL).isEqualTo(ListBuilder.INFINITY);
final int headerType = metadata.getHeaderType();
assertThat(headerType).isEqualTo(EventInfo.ROW_TYPE_SLIDER);
// Check primary intent
final PendingIntent primaryPendingIntent = primaryAction.getAction();
assertThat(primaryPendingIntent).isEqualTo(
SliceBuilderUtils.getContentPendingIntent(context, sliceData));
assertThat(metadata.getTitle()).isEqualTo(sliceData.getTitle());
assertKeywords(metadata, sliceData);
}
/**
* Test the copyable slice, including:
* - No intent
* - Correct title
* - Correct intent
* - Correct keywords
* - TTL
* - Color
*/
public static void testSettingsCopyableSlice(Context context, Slice slice,
SliceData sliceData) {
final SliceMetadata metadata = SliceMetadata.from(context, slice);
final SliceItem colorItem = SliceQuery.findSubtype(slice, FORMAT_INT, SUBTYPE_COLOR);
final int color = colorItem.getInt();
assertThat(color).isEqualTo(Utils.getColorAccentDefaultColor(context));
final SliceAction primaryAction = metadata.getPrimaryAction();
final IconCompat expectedIcon = IconCompat.createWithResource(context,
sliceData.getIconResource());
assertThat(expectedIcon.toString()).isEqualTo(primaryAction.getIcon().toString());
final long sliceTTL = metadata.getExpiry();
assertThat(sliceTTL).isEqualTo(ListBuilder.INFINITY);
// Check primary intent
final PendingIntent primaryPendingIntent = primaryAction.getAction();
assertThat(primaryPendingIntent).isEqualTo(
SliceBuilderUtils.getContentPendingIntent(context, sliceData));
assertThat(metadata.getTitle()).isEqualTo(sliceData.getTitle());
assertKeywords(metadata, sliceData);
}
/**
* Test the contents of an unavailable slice, including:
* - No toggles
* - Correct title
* - Correct intent
* - Correct keywords
* - Color
* - TTL
*/
public static void testSettingsUnavailableSlice(Context context, Slice slice,
SliceData sliceData) {
final SliceMetadata metadata = SliceMetadata.from(context, slice);
final long sliceTTL = metadata.getExpiry();
assertThat(sliceTTL).isEqualTo(ListBuilder.INFINITY);
final SliceItem colorItem = SliceQuery.findSubtype(slice, FORMAT_INT, SUBTYPE_COLOR);
final int color = colorItem.getInt();
assertThat(color).isEqualTo(Utils.getColorAccentDefaultColor(context));
final List<SliceAction> toggles = metadata.getToggles();
assertThat(toggles).isEmpty();
final PendingIntent primaryPendingIntent = metadata.getPrimaryAction().getAction();
assertThat(primaryPendingIntent).isEqualTo(SliceBuilderUtils.getContentPendingIntent(
context, sliceData));
assertThat(metadata.getTitle()).isEqualTo(sliceData.getTitle());
assertKeywords(metadata, sliceData);
}
/**
* Assert any slice item contains title.
*
* @param sliceItems All slice items of a Slice.
* @param title Title for asserting.
*/
public static void assertAnySliceItemContainsTitle(List<SliceItem> sliceItems, String title) {
assertThat(hasText(sliceItems, title, HINT_TITLE)).isTrue();
}
/**
* Assert any slice item contains subtitle.
*
* @param sliceItems All slice items of a Slice.
* @param subtitle Subtitle for asserting.
*/
public static void assertAnySliceItemContainsSubtitle(List<SliceItem> sliceItems,
String subtitle) {
// Subtitle has no hints
assertThat(hasText(sliceItems, subtitle, null /* hints */)).isTrue();
}
/**
* Assert no slice item contains subtitle.
*
* @param sliceItems All slice items of a Slice.
* @param subtitle Subtitle for asserting.
*/
public static void assertNoSliceItemContainsSubtitle(List<SliceItem> sliceItems,
String subtitle) {
// Subtitle has no hints
assertThat(hasText(sliceItems, subtitle, null /* hints */)).isFalse();
}
private static boolean hasText(List<SliceItem> sliceItems, String text, String hints) {
boolean hasText = false;
for (SliceItem item : sliceItems) {
List<SliceItem> textItems = SliceQuery.findAll(item, FORMAT_TEXT, hints,
null /* non-hints */);
if (textItems == null) {
continue;
}
for (SliceItem textItem : textItems) {
if (TextUtils.equals(textItem.getText(), text)) {
hasText = true;
break;
}
}
}
return hasText;
}
/**
* Assert any slice item contains icon.
*
* @param sliceItems All slice items of a Slice.
* @param icon Icon for asserting.
*/
public static void assertAnySliceItemContainsIcon(List<SliceItem> sliceItems, IconCompat icon) {
boolean hasIcon = false;
for (SliceItem item : sliceItems) {
List<SliceItem> iconItems = SliceQuery.findAll(item, FORMAT_IMAGE,
(String) null /* hints */, null /* non-hints */);
if (iconItems == null) {
continue;
}
for (SliceItem iconItem : iconItems) {
if (icon.toString().equals(iconItem.getIcon().toString())) {
hasIcon = true;
break;
}
}
}
assertThat(hasIcon).isTrue();
}
private static void assertKeywords(SliceMetadata metadata, SliceData data) {
final List<String> keywords = metadata.getSliceKeywords();
final Set<String> expectedKeywords = Arrays.stream(data.getKeywords().split(","))
.map(s -> s = s.trim())
.collect(Collectors.toSet());
expectedKeywords.add(data.getTitle());
expectedKeywords.add(data.getScreenTitle().toString());
assertThat(keywords).containsExactlyElementsIn(expectedKeywords);
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2020 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.testutils;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_KEY;
import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag
.FLAG_INCLUDE_PREF_SCREEN;
import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_KEY;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import com.android.settings.core.PreferenceXmlParserUtils;
import org.xmlpull.v1.XmlPullParserException;
import java.util.ArrayList;
import java.util.List;
/**
* Util class for parsing XML
*/
public class XmlTestUtils {
/**
* Parses a preference screen's xml, collects and returns all keys used by preferences
* on the screen.
*
* @param context of the preference screen.
* @param xmlId of the Preference Xml to be parsed.
* @return List of all keys in the preference Xml
*/
public static List<String> getKeysFromPreferenceXml(Context context, int xmlId) {
final List<String> keys = new ArrayList<>();
try {
List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(context, xmlId,
FLAG_NEED_KEY | FLAG_INCLUDE_PREF_SCREEN);
for (Bundle bundle : metadata) {
final String key = bundle.getString(METADATA_KEY);
if (!TextUtils.isEmpty(key)) {
keys.add(key);
}
}
} catch (java.io.IOException | XmlPullParserException e) {
return null;
}
return keys;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -23,15 +23,15 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@Ignore
@RunWith(AndroidJUnit4.class)
public class MultiUserTopIntroPreferenceControllerTest {
private Context mContext;
@@ -39,10 +39,11 @@ public class MultiUserTopIntroPreferenceControllerTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mContext = ApplicationProvider.getApplicationContext();
mController = new MultiUserTopIntroPreferenceController(mContext, "top_info");
}
@Ignore
@Test
public void getAvailabilityStatus_multiUserOff_shouldReturnEnabled() {
mController.mUserCaps.mEnabled = true;
@@ -51,6 +52,7 @@ public class MultiUserTopIntroPreferenceControllerTest {
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE);
}
@Ignore
@Test
public void getAvailabilityStatus_multiUserOn_shouldReturnDisabled() {
mController.mUserCaps.mEnabled = true;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -20,19 +20,20 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class LocalClassLoaderContextThemeWrapperTest {
private LocalClassLoaderContextThemeWrapper mContextThemeWrapper;
@Test
public void getClassLoader_shouldUseLocalClassLoader() {
final Context context = RuntimeEnvironment.application;
final Context context = ApplicationProvider.getApplicationContext();
final Class clazz = LocalClassLoaderContextThemeWrapperTest.class;
mContextThemeWrapper = new LocalClassLoaderContextThemeWrapper(clazz, context, 0);

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -23,11 +23,12 @@ import static org.mockito.Mockito.when;
import android.net.ConnectivityManager;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public final class VpnUtilsTest {
@Test
public void testIsAlwaysOnVpnSet() {

Some files were not shown because too many files have changed in this diff Show More