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

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

Merge "Fix AccessPointPreferenceTest and add updateIcon test."

parents 5f677ee0 6efd09a0
Loading
Loading
Loading
Loading
+41 −34
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */
package com.android.settingslib.wifi;

import android.annotation.Nullable;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
@@ -60,9 +61,10 @@ public class AccessPointPreference extends Preference {
            R.string.accessibility_wifi_signal_full
    };

    private final StateListDrawable mFrictionSld;
    @Nullable private final StateListDrawable mFrictionSld;
    private final int mBadgePadding;
    private final UserBadgeCache mBadgeCache;
    private final IconInjector mIconInjector;
    private TextView mTitleView;

    private boolean mForSavedNetworks = false;
@@ -86,60 +88,53 @@ public class AccessPointPreference extends Preference {
        return builder.toString();
    }

    @Nullable
    private static StateListDrawable getFrictionStateListDrawable(Context context) {
        TypedArray frictionSld;
        try {
            frictionSld = context.getTheme().obtainStyledAttributes(FRICTION_ATTRS);
        } catch (Resources.NotFoundException e) {
            // Fallback for platforms that do not need friction icon resources.
            frictionSld = null;
        }
        return frictionSld != null ? (StateListDrawable) frictionSld.getDrawable(0) : null;
    }

    // Used for dummy pref.
    public AccessPointPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        mFrictionSld = null;
        mBadgePadding = 0;
        mBadgeCache = null;
        mIconInjector = new IconInjector(context);
    }

    public AccessPointPreference(AccessPoint accessPoint, Context context, UserBadgeCache cache,
            boolean forSavedNetworks) {
        super(context);
        setWidgetLayoutResource(R.layout.access_point_friction_widget);
        mBadgeCache = cache;
        mAccessPoint = accessPoint;
        mForSavedNetworks = forSavedNetworks;
        mAccessPoint.setTag(this);
        mLevel = -1;

        TypedArray frictionSld;
        try {
            frictionSld = context.getTheme().obtainStyledAttributes(FRICTION_ATTRS);
        } catch (Resources.NotFoundException e) {
            // Fallback for platforms that do not need friction icon resources.
            frictionSld = null;
        }
        mFrictionSld = frictionSld != null ? (StateListDrawable) frictionSld.getDrawable(0) : null;

        // Distance from the end of the title at which this AP's user badge should sit.
        mBadgePadding = context.getResources()
                .getDimensionPixelSize(R.dimen.wifi_preference_badge_padding);
        this(accessPoint, context, cache, 0 /* iconResId */, forSavedNetworks);
        refresh();
    }

    public AccessPointPreference(AccessPoint accessPoint, Context context, UserBadgeCache cache,
            int iconResId, boolean forSavedNetworks) {
        this(accessPoint, context, cache, iconResId, forSavedNetworks,
                getFrictionStateListDrawable(context), -1 /* level */, new IconInjector(context));
    }

    @VisibleForTesting
    AccessPointPreference(AccessPoint accessPoint, Context context, UserBadgeCache cache,
                          int iconResId, boolean forSavedNetworks, StateListDrawable frictionSld,
                          int level, IconInjector iconInjector) {
        super(context);
        setWidgetLayoutResource(R.layout.access_point_friction_widget);
        mBadgeCache = cache;
        mAccessPoint = accessPoint;
        mForSavedNetworks = forSavedNetworks;
        mAccessPoint.setTag(this);
        mLevel = -1;
        mLevel = level;
        mDefaultIconResId = iconResId;

        TypedArray frictionSld;
        try {
            frictionSld = context.getTheme().obtainStyledAttributes(FRICTION_ATTRS);
        } catch (Resources.NotFoundException e) {
            // Fallback for platforms that do not need friction icon resources.
            frictionSld = null;
        }
        mFrictionSld = frictionSld != null ? (StateListDrawable) frictionSld.getDrawable(0) : null;

        // Distance from the end of the title at which this AP's user badge should sit.
        mFrictionSld = frictionSld;
        mIconInjector = iconInjector;
        mBadgePadding = context.getResources()
                .getDimensionPixelSize(R.dimen.wifi_preference_badge_padding);
    }
@@ -179,7 +174,7 @@ public class AccessPointPreference extends Preference {
        }
        TronUtils.logWifiSettingsSpeed(context, mWifiSpeed);

        Drawable drawable = context.getDrawable(Utils.getWifiIconResource(level));
        Drawable drawable = mIconInjector.getIcon(level);
        if (!mForSavedNetworks && drawable != null) {
            drawable.setTint(Utils.getColorAttr(context, android.R.attr.colorControlNormal));
            setIcon(drawable);
@@ -319,4 +314,16 @@ public class AccessPointPreference extends Preference {
            return mBadges.valueAt(index);
        }
    }

    static class IconInjector {
        private final Context mContext;

        public IconInjector(Context context) {
            mContext = context;
        }

        public Drawable getIcon(int level) {
            return mContext.getDrawable(Utils.getWifiIconResource(level));
        }
    }
}
+35 −0
Original line number Diff line number Diff line
@@ -16,16 +16,23 @@
package com.android.settingslib.wifi;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;

import android.content.Context;

import android.graphics.drawable.ColorDrawable;
import com.android.settingslib.SettingsLibRobolectricTestRunner;
import com.android.settingslib.TestConfig;

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;

@@ -35,6 +42,22 @@ public class AccessPointPreferenceTest {

    private Context mContext = RuntimeEnvironment.application;

    @Mock private AccessPoint mockAccessPoint;
    @Mock private AccessPointPreference.UserBadgeCache mockUserBadgeCache;
    @Mock private AccessPointPreference.IconInjector mockIconInjector;

    private AccessPointPreference createWithAccessPoint(AccessPoint accessPoint) {
        return new AccessPointPreference(accessPoint, mContext, mockUserBadgeCache,
                0, true, null, -1, mockIconInjector);
    }

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        when(mockIconInjector.getIcon(anyInt())).thenReturn(new ColorDrawable());
    }

    @Test
    public void generatePreferenceKey_shouldReturnSsidPlusSecurity() {
        String ssid = "ssid";
@@ -78,4 +101,16 @@ public class AccessPointPreferenceTest {
                RuntimeEnvironment.application, pref, ap))
                .isEqualTo("ssid,connected,Wifi signal full.,Secure network");
    }

    @Test
    public void refresh_shouldUpdateIcon() {
        int level = 1;
        when(mockAccessPoint.getSpeed()).thenReturn(0);
        when(mockAccessPoint.getLevel()).thenReturn(level);

        AccessPointPreference pref = createWithAccessPoint(mockAccessPoint);
        pref.refresh();

        verify(mockIconInjector).getIcon(level);
    }
}