Merge "Add test cases for highlight Preference click behavior"

This commit is contained in:
TreeHugger Robot
2022-07-04 13:08:53 +00:00
committed by Android (Google) Code Review
4 changed files with 131 additions and 1 deletions

View File

@@ -65,7 +65,12 @@ import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.Utils;
import com.android.settings.homepage.TopLevelHighlightMixin;
import com.android.settings.homepage.TopLevelSettings;
import com.android.settings.search.SearchFeatureProviderImpl;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowActivityEmbeddingUtils;
import com.android.settings.testutils.shadow.ShadowSplitController;
import com.android.settings.testutils.shadow.ShadowTileUtils;
import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
@@ -542,6 +547,64 @@ public class DashboardFeatureProviderImplTest {
.isEqualTo(MetricsEvent.SETTINGS_GESTURES);
}
/** This test is for large screen devices Activity embedding. */
@Test
@Config(shadows = {ShadowActivityEmbeddingUtils.class, ShadowSplitController.class})
public void bindPreference_clickHighlightedPreference_shouldNotStartActivity() {
ShadowSplitController.setIsActivityEmbedded(true);
ShadowActivityEmbeddingUtils.setIsEmbeddingActivityEnabled(true);
mFeatureFactory.searchFeatureProvider = new SearchFeatureProviderImpl();
String clickPrefKey = "highlight_pref_key";
String highlightMixinPrefKey = "highlight_pref_key";
FragmentActivity activity = Robolectric.buildActivity(FragmentActivity.class).get();
Preference preference = new Preference(RuntimeEnvironment.application);
Tile tile = new ActivityTile(mActivityInfo, CategoryKey.CATEGORY_HOMEPAGE);
mActivityInfo.metaData.putString(META_DATA_PREFERENCE_KEYHINT, "key");
mActivityInfo.metaData.putString("com.android.settings.intent.action", "TestAction");
tile.userHandle = null;
TopLevelSettings largeScreenTopLevelSettings = new TopLevelSettings(
new TestTopLevelHighlightMixin(highlightMixinPrefKey, true /* activityEmbedded */));
largeScreenTopLevelSettings.setHighlightPreferenceKey(clickPrefKey);
mImpl.bindPreferenceToTileAndGetObservers(activity, largeScreenTopLevelSettings,
mForceRoundedIcon, preference, tile, clickPrefKey, Preference.DEFAULT_ORDER);
preference.performClick();
ShadowActivity shadowActivity = Shadows.shadowOf(activity);
assertThat(shadowActivity.getNextStartedActivityForResult()).isEqualTo(null);
}
/** This test is for large screen devices Activity embedding. */
@Test
@Config(shadows = {ShadowActivityEmbeddingUtils.class, ShadowSplitController.class})
public void bindPreference_clickNotHighlightedPreference_shouldStartActivity() {
ShadowSplitController.setIsActivityEmbedded(true);
ShadowActivityEmbeddingUtils.setIsEmbeddingActivityEnabled(true);
mFeatureFactory.searchFeatureProvider = new SearchFeatureProviderImpl();
String clickPrefKey = "not_highlight_pref_key";
String highlightMixinPrefKey = "highlight_pref_key";
FragmentActivity activity = Robolectric.buildActivity(FragmentActivity.class).get();
Preference preference = new Preference(RuntimeEnvironment.application);
Tile tile = new ActivityTile(mActivityInfo, CategoryKey.CATEGORY_HOMEPAGE);
mActivityInfo.metaData.putString(META_DATA_PREFERENCE_KEYHINT, "key");
mActivityInfo.metaData.putString("com.android.settings.intent.action", "TestAction");
tile.userHandle = null;
TopLevelSettings largeScreenTopLevelSettings = new TopLevelSettings(
new TestTopLevelHighlightMixin(highlightMixinPrefKey, true /* activityEmbedded */));
largeScreenTopLevelSettings.setHighlightPreferenceKey(clickPrefKey);
mImpl.bindPreferenceToTileAndGetObservers(activity, largeScreenTopLevelSettings,
mForceRoundedIcon, preference, tile, clickPrefKey, Preference.DEFAULT_ORDER);
preference.performClick();
Intent launchIntent = Shadows.shadowOf(activity).getNextStartedActivityForResult().intent;
assertThat(launchIntent.getAction()).isEqualTo("TestAction");
}
@Test
public void clickPreference_withUnresolvableIntent_shouldNotLaunchAnything() {
ReflectionHelpers.setField(
@@ -694,4 +757,19 @@ public class DashboardFeatureProviderImplTest {
return "TestFragment";
}
}
private static class TestTopLevelHighlightMixin extends TopLevelHighlightMixin {
private final String mHighlightPreferenceKey;
TestTopLevelHighlightMixin(String highlightPreferenceKey,
boolean activityEmbedded) {
super(activityEmbedded);
mHighlightPreferenceKey = highlightPreferenceKey;
}
@Override
public String getHighlightPreferenceKey() {
return mHighlightPreferenceKey;
}
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (C) 2022 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.shadow;
import android.app.Activity;
import androidx.window.embedding.SplitController;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
/**
* Shadow class for {@link SplitController} to test embedding activity features.
*/
@Implements(SplitController.class)
public class ShadowSplitController {
private static boolean sIsActivityEmbedded;
@Implementation
protected static boolean isActivityEmbedded(Activity activity) {
return sIsActivityEmbedded;
}
public static void setIsActivityEmbedded(boolean isActivityEmbedded) {
sIsActivityEmbedded = isActivityEmbedded;
}
}