Preparation task for Settings Fragment Migration
Create some compatible files.
These files can be used for Settings Fragment Migration task.
Bug: 110259478
Test: make RunSettingsLibRoboTests -j40
Change-Id: Ib3d52e9a5f5bed5c194d429fdfa4b0d01ed07f01
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/CustomEditTextPreferenceComaptTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/CustomEditTextPreferenceComaptTest.java
new file mode 100644
index 0000000..9ba9967
--- /dev/null
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/CustomEditTextPreferenceComaptTest.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settingslib;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.view.View;
+import android.widget.EditText;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.util.ReflectionHelpers;
+
+@RunWith(SettingsLibRobolectricTestRunner.class)
+public class CustomEditTextPreferenceComaptTest {
+
+ @Mock
+ private View mView;
+
+ private TestPreference mPreference;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mPreference = new TestPreference(RuntimeEnvironment.application);
+ }
+
+ @Test
+ public void bindDialogView_shouldRequestFocus() {
+ final String testText = "";
+ final EditText editText = spy(new EditText(RuntimeEnvironment.application));
+ editText.setText(testText);
+ when(mView.findViewById(android.R.id.edit)).thenReturn(editText);
+
+ mPreference.onBindDialogView(mView);
+
+ verify(editText).requestFocus();
+ }
+
+ @Test
+ public void getEditText_noDialog_shouldNotCrash() {
+ ReflectionHelpers.setField(mPreference, "mFragment",
+ mock(CustomEditTextPreferenceCompat.CustomPreferenceDialogFragment.class));
+
+ mPreference.getEditText();
+
+ // no crash
+ }
+
+ private static class TestPreference extends CustomEditTextPreferenceCompat {
+ public TestPreference(Context context) {
+ super(context);
+ }
+ }
+}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/inputmethod/InputMethodAndSubtypeUtilCompatTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/inputmethod/InputMethodAndSubtypeUtilCompatTest.java
new file mode 100644
index 0000000..ddadac1
--- /dev/null
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/inputmethod/InputMethodAndSubtypeUtilCompatTest.java
@@ -0,0 +1,271 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settingslib.inputmethod;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.pm.ApplicationInfo;
+import android.content.pm.ResolveInfo;
+import android.content.pm.ServiceInfo;
+import android.view.inputmethod.InputMethodInfo;
+import android.view.inputmethod.InputMethodSubtype;
+import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+
+@RunWith(RobolectricTestRunner.class)
+public class InputMethodAndSubtypeUtilCompatTest {
+
+ private static final HashSet<String> EMPTY_STRING_SET = new HashSet<>();
+
+ private static HashSet<String> asHashSet(String... strings) {
+ HashSet<String> hashSet = new HashSet<>();
+ for (String s : strings) {
+ hashSet.add(s);
+ }
+ return hashSet;
+ }
+
+ @Test
+ public void parseInputMethodsAndSubtypesString_EmptyString() {
+ assertThat(InputMethodAndSubtypeUtilCompat.
+ parseInputMethodsAndSubtypesString("")).isEmpty();
+ assertThat(InputMethodAndSubtypeUtilCompat.
+ parseInputMethodsAndSubtypesString(null)).isEmpty();
+ }
+
+ @Test
+ public void parseInputMethodsAndSubtypesString_SingleImeNoSubtype() {
+ HashMap<String, HashSet<String>> r =
+ InputMethodAndSubtypeUtilCompat.parseInputMethodsAndSubtypesString("ime0");
+ assertThat(r).containsExactly("ime0", EMPTY_STRING_SET);
+ }
+
+ @Test
+ public void parseInputMethodsAndSubtypesString_MultipleImesNoSubtype() {
+ HashMap<String, HashSet<String>> r =
+ InputMethodAndSubtypeUtilCompat.parseInputMethodsAndSubtypesString("ime0:ime1");
+ assertThat(r).containsExactly("ime0", EMPTY_STRING_SET, "ime1", EMPTY_STRING_SET);
+ }
+
+ @Test
+ public void parseInputMethodsAndSubtypesString_SingleImeSingleSubtype() {
+ HashMap<String, HashSet<String>> r =
+ InputMethodAndSubtypeUtilCompat.parseInputMethodsAndSubtypesString("ime0;subtype0");
+ assertThat(r).containsExactly("ime0", asHashSet("subtype0"));
+ }
+
+ @Test
+ public void parseInputMethodsAndSubtypesString_SingleImeDuplicateSameSubtypes() {
+ HashMap<String, HashSet<String>> r =
+ InputMethodAndSubtypeUtilCompat.parseInputMethodsAndSubtypesString(
+ "ime0;subtype0;subtype0");
+ assertThat(r).containsExactly("ime0", asHashSet("subtype0"));
+ }
+
+ @Test
+ public void parseInputMethodsAndSubtypesString_SingleImeMultipleSubtypes() {
+ HashMap<String, HashSet<String>> r =
+ InputMethodAndSubtypeUtilCompat.parseInputMethodsAndSubtypesString(
+ "ime0;subtype0;subtype1");
+ assertThat(r).containsExactly("ime0", asHashSet("subtype0", "subtype1"));
+ }
+
+ @Test
+ public void parseInputMethodsAndSubtypesString_MultiplePairsOfImeSubtype() {
+ assertThat(InputMethodAndSubtypeUtilCompat.parseInputMethodsAndSubtypesString(
+ "ime0;subtype0:ime1;subtype1"))
+ .containsExactly("ime0", asHashSet("subtype0"), "ime1", asHashSet("subtype1"));
+ assertThat(InputMethodAndSubtypeUtilCompat.parseInputMethodsAndSubtypesString(
+ "ime0;subtype0;subtype1:ime1;subtype2"))
+ .containsExactly("ime0", asHashSet("subtype0", "subtype1"),
+ "ime1", asHashSet("subtype2"));
+ assertThat(InputMethodAndSubtypeUtilCompat.parseInputMethodsAndSubtypesString(
+ "ime0;subtype0;subtype1:ime1;subtype1;subtype2"))
+ .containsExactly("ime0", asHashSet("subtype0", "subtype1"),
+ "ime1", asHashSet("subtype1", "subtype2"));
+
+ }
+
+ @Test
+ public void parseInputMethodsAndSubtypesString_MixedImeSubtypePairsAndImeNoSubtype() {
+ HashMap<String, HashSet<String>> r =
+ InputMethodAndSubtypeUtilCompat.parseInputMethodsAndSubtypesString(
+ "ime0;subtype0;subtype1:ime1;subtype1;subtype2:ime2");
+ assertThat(r).containsExactly("ime0", asHashSet("subtype0", "subtype1"),
+ "ime1", asHashSet("subtype1", "subtype2"),
+ "ime2", EMPTY_STRING_SET);
+ }
+
+ @Test
+ public void buildInputMethodsAndSubtypesString_EmptyInput() {
+ HashMap<String, HashSet<String>> map = new HashMap<>();
+ assertThat(map).isEmpty();
+ }
+
+ @Test
+ public void buildInputMethodsAndSubtypesString_SingleIme() {
+ HashMap<String, HashSet<String>> map = new HashMap<>();
+ map.put("ime0", new HashSet<>());
+ String result = InputMethodAndSubtypeUtilCompat.buildInputMethodsAndSubtypesString(map);
+ assertThat(result).isEqualTo("ime0");
+ }
+
+ @Test
+ public void buildInputMethodsAndSubtypesString_SingleImeSingleSubtype() {
+ HashMap<String, HashSet<String>> map = new HashMap<>();
+ map.put("ime0", asHashSet("subtype0"));
+ String result = InputMethodAndSubtypeUtilCompat.buildInputMethodsAndSubtypesString(map);
+ assertThat(result).isEqualTo("ime0;subtype0");
+ }
+
+ @Test
+ public void buildInputMethodsAndSubtypesString_SingleImeMultipleSubtypes() {
+ HashMap<String, HashSet<String>> map = new HashMap<>();
+ map.put("ime0", asHashSet("subtype0", "subtype1"));
+ String result = InputMethodAndSubtypeUtilCompat.buildInputMethodsAndSubtypesString(map);
+
+ // We do not expect what order will be used to concatenate items in
+ // InputMethodAndSubtypeUtil.buildInputMethodsAndSubtypesString() hence accept all possible
+ // permutations here.
+ assertThat(result).matches("ime0;subtype0;subtype1|ime0;subtype1;subtype0");
+ }
+
+ @Test
+ public void buildInputMethodsAndSubtypesString_MultipleImesNoSubtypes() {
+ HashMap<String, HashSet<String>> map = new HashMap<>();
+ map.put("ime0", EMPTY_STRING_SET);
+ map.put("ime1", EMPTY_STRING_SET);
+ String result = InputMethodAndSubtypeUtilCompat.buildInputMethodsAndSubtypesString(map);
+
+ // We do not expect what order will be used to concatenate items in
+ // InputMethodAndSubtypeUtil.buildInputMethodsAndSubtypesString() hence accept all possible
+ // permutations here.
+ assertThat(result).matches("ime0:ime1|ime1:ime0");
+ }
+
+ @Test
+ public void buildInputMethodsAndSubtypesString_MultipleImesWithAndWithoutSubtypes() {
+ HashMap<String, HashSet<String>> map = new HashMap<>();
+ map.put("ime0", asHashSet("subtype0", "subtype1"));
+ map.put("ime1", EMPTY_STRING_SET);
+ String result = InputMethodAndSubtypeUtilCompat.buildInputMethodsAndSubtypesString(map);
+
+ // We do not expect what order will be used to concatenate items in
+ // InputMethodAndSubtypeUtil.buildInputMethodsAndSubtypesString() hence accept all possible
+ // permutations here.
+ assertThat(result).matches("ime0;subtype0;subtype1:ime1|ime0;subtype1;subtype0:ime1"
+ + "|ime1:ime0;subtype0;subtype1|ime1:ime0;subtype1;subtype0");
+ }
+
+ @Test
+ public void buildInputMethodsAndSubtypesString_MultipleImesWithSubtypes() {
+ HashMap<String, HashSet<String>> map = new HashMap<>();
+ map.put("ime0", asHashSet("subtype0", "subtype1"));
+ map.put("ime1", asHashSet("subtype2", "subtype3"));
+ String result = InputMethodAndSubtypeUtilCompat.buildInputMethodsAndSubtypesString(map);
+
+ // We do not expect what order will be used to concatenate items in
+ // InputMethodAndSubtypeUtil.buildInputMethodsAndSubtypesString() hence accept all possible
+ // permutations here.
+ assertThat(result).matches("ime0;subtype0;subtype1:ime1;subtype2;subtype3"
+ + "|ime0;subtype1;subtype0:ime1;subtype2;subtype3"
+ + "|ime0;subtype0;subtype1:ime1;subtype3;subtype2"
+ + "|ime0;subtype1;subtype0:ime1;subtype3;subtype2"
+ + "|ime1;subtype2;subtype3:ime0;subtype0;subtype1"
+ + "|ime2;subtype3;subtype2:ime0;subtype0;subtype1"
+ + "|ime3;subtype2;subtype3:ime0;subtype1;subtype0"
+ + "|ime4;subtype3;subtype2:ime0;subtype1;subtype0");
+ }
+
+ @Test
+ public void isValidSystemNonAuxAsciiCapableIme() {
+ // System IME w/ no subtype
+ assertThat(InputMethodAndSubtypeUtilCompat.isValidSystemNonAuxAsciiCapableIme(
+ createDummyIme(true, false)))
+ .isFalse();
+
+ // System IME w/ non-Aux and non-ASCII-capable "keyboard" subtype
+ assertThat(InputMethodAndSubtypeUtilCompat.isValidSystemNonAuxAsciiCapableIme(
+ createDummyIme(true, false, createDummySubtype("keyboard", false, false))))
+ .isFalse();
+
+ // System IME w/ non-Aux and ASCII-capable "keyboard" subtype
+ assertThat(InputMethodAndSubtypeUtilCompat.isValidSystemNonAuxAsciiCapableIme(
+ createDummyIme(true, false, createDummySubtype("keyboard", false, true))))
+ .isTrue();
+
+ // System IME w/ Aux and ASCII-capable "keyboard" subtype
+ assertThat(InputMethodAndSubtypeUtilCompat.isValidSystemNonAuxAsciiCapableIme(
+ createDummyIme(true, true, createDummySubtype("keyboard", true, true))))
+ .isFalse();
+
+ // System IME w/ non-Aux and ASCII-capable "voice" subtype
+ assertThat(InputMethodAndSubtypeUtilCompat.isValidSystemNonAuxAsciiCapableIme(
+ createDummyIme(true, false, createDummySubtype("voice", false, true))))
+ .isFalse();
+
+ // System IME w/ non-Aux and non-ASCII-capable subtype + Non-Aux and ASCII-capable subtype
+ assertThat(InputMethodAndSubtypeUtilCompat.isValidSystemNonAuxAsciiCapableIme(
+ createDummyIme(true, false,
+ createDummySubtype("keyboard", false, true),
+ createDummySubtype("keyboard", false, false))))
+ .isTrue();
+
+ // Non-system IME w/ non-Aux and ASCII-capable "keyboard" subtype
+ assertThat(InputMethodAndSubtypeUtilCompat.isValidSystemNonAuxAsciiCapableIme(
+ createDummyIme(false, false, createDummySubtype("keyboard", false, true))))
+ .isFalse();
+ }
+
+ private static InputMethodInfo createDummyIme(boolean isSystem, boolean isAuxIme,
+ InputMethodSubtype... subtypes) {
+ final ResolveInfo ri = new ResolveInfo();
+ final ServiceInfo si = new ServiceInfo();
+ final ApplicationInfo ai = new ApplicationInfo();
+ ai.packageName = "com.example.android.dummyime";
+ ai.enabled = true;
+ ai.flags |= (isSystem ? ApplicationInfo.FLAG_SYSTEM : 0);
+ si.applicationInfo = ai;
+ si.enabled = true;
+ si.packageName = "com.example.android.dummyime";
+ si.name = "Dummy IME";
+ si.exported = true;
+ si.nonLocalizedLabel = "Dummy IME";
+ ri.serviceInfo = si;
+ return new InputMethodInfo(ri, isAuxIme, "", Arrays.asList(subtypes), 1, false);
+ }
+
+ private static InputMethodSubtype createDummySubtype(
+ String mode, boolean isAuxiliary, boolean isAsciiCapable) {
+ return new InputMethodSubtypeBuilder()
+ .setSubtypeNameResId(0)
+ .setSubtypeIconResId(0)
+ .setSubtypeLocale("en_US")
+ .setLanguageTag("en-US")
+ .setSubtypeMode(mode)
+ .setIsAuxiliary(isAuxiliary)
+ .setIsAsciiCapable(isAsciiCapable)
+ .build();
+ }
+}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/license/LicenseHtmlLoaderCompatTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/license/LicenseHtmlLoaderCompatTest.java
new file mode 100644
index 0000000..f981f36
--- /dev/null
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/license/LicenseHtmlLoaderCompatTest.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2017 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.settingslib.license;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+import android.content.Context;
+
+import com.android.settingslib.SettingsLibRobolectricTestRunner;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.io.File;
+import java.util.ArrayList;
+
+@RunWith(SettingsLibRobolectricTestRunner.class)
+public class LicenseHtmlLoaderCompatTest {
+ @Mock
+ private Context mContext;
+
+ LicenseHtmlLoaderCompat newLicenseHtmlLoader(ArrayList<File> xmlFiles,
+ File cachedHtmlFile, boolean isCachedHtmlFileOutdated,
+ boolean generateHtmlFileSucceeded) {
+ LicenseHtmlLoaderCompat loader = spy(new LicenseHtmlLoaderCompat(mContext));
+ doReturn(xmlFiles).when(loader).getVaildXmlFiles();
+ doReturn(cachedHtmlFile).when(loader).getCachedHtmlFile();
+ doReturn(isCachedHtmlFileOutdated).when(loader).isCachedHtmlFileOutdated(any(), any());
+ doReturn(generateHtmlFileSucceeded).when(loader).generateHtmlFile(any(), any());
+ return loader;
+ }
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ @Test
+ public void testLoadInBackground() {
+ ArrayList<File> xmlFiles = new ArrayList();
+ xmlFiles.add(new File("test.xml"));
+ File cachedHtmlFile = new File("test.html");
+
+ LicenseHtmlLoaderCompat loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true, true);
+
+ assertThat(loader.loadInBackground()).isEqualTo(cachedHtmlFile);
+ verify(loader).generateHtmlFile(any(), any());
+ }
+
+ @Test
+ public void testLoadInBackgroundWithNoVaildXmlFiles() {
+ ArrayList<File> xmlFiles = new ArrayList();
+ File cachedHtmlFile = new File("test.html");
+
+ LicenseHtmlLoaderCompat loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true, true);
+
+ assertThat(loader.loadInBackground()).isNull();
+ verify(loader, never()).generateHtmlFile(any(), any());
+ }
+
+ @Test
+ public void testLoadInBackgroundWithNonOutdatedCachedHtmlFile() {
+ ArrayList<File> xmlFiles = new ArrayList();
+ xmlFiles.add(new File("test.xml"));
+ File cachedHtmlFile = new File("test.html");
+
+ LicenseHtmlLoaderCompat loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, false,
+ true);
+
+ assertThat(loader.loadInBackground()).isEqualTo(cachedHtmlFile);
+ verify(loader, never()).generateHtmlFile(any(), any());
+ }
+
+ @Test
+ public void testLoadInBackgroundWithGenerateHtmlFileFailed() {
+ ArrayList<File> xmlFiles = new ArrayList();
+ xmlFiles.add(new File("test.xml"));
+ File cachedHtmlFile = new File("test.html");
+
+ LicenseHtmlLoaderCompat loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true,
+ false);
+
+ assertThat(loader.loadInBackground()).isNull();
+ verify(loader).generateHtmlFile(any(), any());
+ }
+}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/suggestions/SuggestionControllerMixinCompatTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/suggestions/SuggestionControllerMixinCompatTest.java
new file mode 100644
index 0000000..1ee3afa
--- /dev/null
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/suggestions/SuggestionControllerMixinCompatTest.java
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settingslib.suggestions;
+
+import static androidx.lifecycle.Lifecycle.Event.ON_START;
+import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.ComponentName;
+import android.content.Context;
+
+import com.android.settingslib.SettingsLibRobolectricTestRunner;
+import com.android.settingslib.core.lifecycle.Lifecycle;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+import androidx.lifecycle.LifecycleOwner;
+import androidx.loader.app.LoaderManager;
+
+@RunWith(SettingsLibRobolectricTestRunner.class)
+@Config(shadows = ShadowSuggestionController.class)
+public class SuggestionControllerMixinCompatTest {
+
+ @Mock
+ private SuggestionControllerMixinCompat.SuggestionControllerHost mHost;
+
+ private Context mContext;
+ private LifecycleOwner mLifecycleOwner;
+ private Lifecycle mLifecycle;
+ private SuggestionControllerMixinCompat mMixin;
+ private ComponentName mComponentName;
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mContext = RuntimeEnvironment.application;
+ mLifecycleOwner = () -> mLifecycle;
+ mLifecycle = new Lifecycle(mLifecycleOwner);
+ mComponentName = new ComponentName(
+ "com.android.settings.intelligence",
+ "com.android.settings.intelligence.suggestions.SuggestionService");
+ }
+
+ @After
+ public void tearDown() {
+ ShadowSuggestionController.reset();
+ }
+
+ @Test
+ public void goThroughLifecycle_onStartStop_shouldStartStopController() {
+ mMixin = new SuggestionControllerMixinCompat(mContext, mHost, mLifecycle, mComponentName);
+
+ mLifecycle.handleLifecycleEvent(ON_START);
+ assertThat(ShadowSuggestionController.sStartCalled).isTrue();
+
+ mLifecycle.handleLifecycleEvent(ON_STOP);
+ assertThat(ShadowSuggestionController.sStopCalled).isTrue();
+ }
+
+ @Test
+ public void onServiceConnected_shouldGetSuggestion() {
+ final LoaderManager loaderManager = mock(LoaderManager.class);
+ when(mHost.getLoaderManager()).thenReturn(loaderManager);
+
+ mMixin = new SuggestionControllerMixinCompat(mContext, mHost, mLifecycle, mComponentName);
+ mMixin.onServiceConnected();
+
+ verify(loaderManager).restartLoader(SuggestionLoader.LOADER_ID_SUGGESTIONS,
+ null /* args */, mMixin /* callback */);
+ }
+
+ @Test
+ public void onServiceConnected_hostNotAttached_shouldDoNothing() {
+ when(mHost.getLoaderManager()).thenReturn(null);
+
+ mMixin = new SuggestionControllerMixinCompat(mContext, mHost, mLifecycle, mComponentName);
+ mMixin.onServiceConnected();
+
+ verify(mHost).getLoaderManager();
+ }
+
+ @Test
+ public void onServiceDisconnected_hostNotAttached_shouldDoNothing() {
+ when(mHost.getLoaderManager()).thenReturn(null);
+
+ mMixin = new SuggestionControllerMixinCompat(mContext, mHost, mLifecycle, mComponentName);
+ mMixin.onServiceDisconnected();
+
+ verify(mHost).getLoaderManager();
+ }
+
+ @Test
+ public void doneLoadingg_shouldSetSuggestionLoaded() {
+ mMixin = new SuggestionControllerMixinCompat(mContext, mHost, mLifecycle, mComponentName);
+
+ mMixin.onLoadFinished(mock(SuggestionLoaderCompat.class), null);
+
+ assertThat(mMixin.isSuggestionLoaded()).isTrue();
+
+ mMixin.onLoaderReset(mock(SuggestionLoaderCompat.class));
+
+ assertThat(mMixin.isSuggestionLoaded()).isFalse();
+ }
+}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceMixinCompatTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceMixinCompatTest.java
new file mode 100644
index 0000000..1abbaba
--- /dev/null
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceMixinCompatTest.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2016 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.settingslib.widget;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.android.settingslib.SettingsLibRobolectricTestRunner;
+import com.android.settingslib.core.lifecycle.Lifecycle;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.shadows.ShadowApplication;
+
+import androidx.lifecycle.LifecycleOwner;
+import androidx.preference.PreferenceFragmentCompat;
+import androidx.preference.PreferenceManager;
+import androidx.preference.PreferenceScreen;
+
+@RunWith(SettingsLibRobolectricTestRunner.class)
+public class FooterPreferenceMixinCompatTest {
+
+ @Mock
+ private PreferenceFragmentCompat mFragment;
+ @Mock
+ private PreferenceScreen mScreen;
+
+ private LifecycleOwner mLifecycleOwner;
+ private Lifecycle mLifecycle;
+ private FooterPreferenceMixinCompat mMixin;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mLifecycleOwner = () -> mLifecycle;
+ mLifecycle = new Lifecycle(mLifecycleOwner);
+ when(mFragment.getPreferenceManager()).thenReturn(mock(PreferenceManager.class));
+ when(mFragment.getPreferenceManager().getContext())
+ .thenReturn(ShadowApplication.getInstance().getApplicationContext());
+ mMixin = new FooterPreferenceMixinCompat(mFragment, mLifecycle);
+ }
+
+ @Test
+ public void createFooter_screenNotAvailable_noCrash() {
+ assertThat(mMixin.createFooterPreference()).isNotNull();
+ }
+
+ @Test
+ public void createFooter_screenAvailable_canAttachToScreen() {
+ when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
+
+ final FooterPreference preference = mMixin.createFooterPreference();
+
+ assertThat(preference).isNotNull();
+ verify(mScreen).addPreference(preference);
+ }
+
+ @Test
+ public void createFooter_screenAvailableDelayed_canAttachToScreen() {
+ final FooterPreference preference = mMixin.createFooterPreference();
+
+ mLifecycle.setPreferenceScreen(mScreen);
+
+ assertThat(preference).isNotNull();
+ verify(mScreen).addPreference(preference);
+ }
+
+ @Test
+ public void createFooterTwice_screenAvailable_replaceOldFooter() {
+ when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
+
+ mMixin.createFooterPreference();
+ mMixin.createFooterPreference();
+
+ verify(mScreen).removePreference(any(FooterPreference.class));
+ verify(mScreen, times(2)).addPreference(any(FooterPreference.class));
+ }
+
+}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceMixinTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceMixinTest.java
index 78b7616..8604d18 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceMixinTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceMixinTest.java
@@ -23,11 +23,6 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-import androidx.lifecycle.LifecycleOwner;
-import androidx.preference.PreferenceFragment;
-import androidx.preference.PreferenceManager;
-import androidx.preference.PreferenceScreen;
-
import com.android.settingslib.SettingsLibRobolectricTestRunner;
import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -38,6 +33,11 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.shadows.ShadowApplication;
+import androidx.lifecycle.LifecycleOwner;
+import androidx.preference.PreferenceFragment;
+import androidx.preference.PreferenceManager;
+import androidx.preference.PreferenceScreen;
+
@RunWith(SettingsLibRobolectricTestRunner.class)
public class FooterPreferenceMixinTest {