Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ae9c4140 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add test cases for highlight Preference click behavior"

parents e0c452a1 69974d0c
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceScreen;
import androidx.recyclerview.widget.RecyclerView;

@@ -140,7 +141,8 @@ public class TopLevelHighlightMixin implements Parcelable, DialogInterface.OnSho
        }
    }

    String getHighlightPreferenceKey() {
    @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
    public String getHighlightPreferenceKey() {
        return mCurrentKey;
    }

+8 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
@@ -72,6 +73,13 @@ public class TopLevelSettings extends DashboardFragment implements SplitLayoutLi
        setArguments(args);
    }

    /** Dependency injection ctor only for testing. */
    @VisibleForTesting
    public TopLevelSettings(TopLevelHighlightMixin highlightMixin) {
        this();
        mHighlightMixin = highlightMixin;
    }

    @Override
    protected int getPreferenceScreenResId() {
        return R.xml.top_level_settings;
+78 −0
Original line number Diff line number Diff line
@@ -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;
        }
    }
}
+42 −0
Original line number Diff line number Diff line
/*
 * 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;
    }
}