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

Commit 6efd09a0 authored by Eric Schwarzenbach's avatar Eric Schwarzenbach
Browse files

Fix AccessPointPreferenceTest and add updateIcon test.

Dependency injects the getIcon() functionality in AccessPointPreference
to enable mocking the method in robolectric where it normally fails to
find the resource. Adds a test to verify that getIcon is called on
refresh. Also modifies the constructors to reduce redundancy.

Bug: 35854035, 64898200
Test: make -j40 RunSettingsLibRoboTests
Change-Id: Iaf9e27651db1d9f16f58ac80ac413e9f84f08b39
parent 3af87363
Loading
Loading
Loading
Loading
+41 −34
Original line number Original line Diff line number Diff line
@@ -15,6 +15,7 @@
 */
 */
package com.android.settingslib.wifi;
package com.android.settingslib.wifi;


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


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


    private boolean mForSavedNetworks = false;
    private boolean mForSavedNetworks = false;
@@ -86,60 +88,53 @@ public class AccessPointPreference extends Preference {
        return builder.toString();
        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.
    // Used for dummy pref.
    public AccessPointPreference(Context context, AttributeSet attrs) {
    public AccessPointPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        super(context, attrs);
        mFrictionSld = null;
        mFrictionSld = null;
        mBadgePadding = 0;
        mBadgePadding = 0;
        mBadgeCache = null;
        mBadgeCache = null;
        mIconInjector = new IconInjector(context);
    }
    }


    public AccessPointPreference(AccessPoint accessPoint, Context context, UserBadgeCache cache,
    public AccessPointPreference(AccessPoint accessPoint, Context context, UserBadgeCache cache,
            boolean forSavedNetworks) {
            boolean forSavedNetworks) {
        super(context);
        this(accessPoint, context, cache, 0 /* iconResId */, forSavedNetworks);
        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);
        refresh();
        refresh();
    }
    }


    public AccessPointPreference(AccessPoint accessPoint, Context context, UserBadgeCache cache,
    public AccessPointPreference(AccessPoint accessPoint, Context context, UserBadgeCache cache,
            int iconResId, boolean forSavedNetworks) {
            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);
        super(context);
        setWidgetLayoutResource(R.layout.access_point_friction_widget);
        setWidgetLayoutResource(R.layout.access_point_friction_widget);
        mBadgeCache = cache;
        mBadgeCache = cache;
        mAccessPoint = accessPoint;
        mAccessPoint = accessPoint;
        mForSavedNetworks = forSavedNetworks;
        mForSavedNetworks = forSavedNetworks;
        mAccessPoint.setTag(this);
        mAccessPoint.setTag(this);
        mLevel = -1;
        mLevel = level;
        mDefaultIconResId = iconResId;
        mDefaultIconResId = iconResId;

        mFrictionSld = frictionSld;
        TypedArray frictionSld;
        mIconInjector = iconInjector;
        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()
        mBadgePadding = context.getResources()
                .getDimensionPixelSize(R.dimen.wifi_preference_badge_padding);
                .getDimensionPixelSize(R.dimen.wifi_preference_badge_padding);
    }
    }
@@ -179,7 +174,7 @@ public class AccessPointPreference extends Preference {
        }
        }
        TronUtils.logWifiSettingsSpeed(context, mWifiSpeed);
        TronUtils.logWifiSettingsSpeed(context, mWifiSpeed);


        Drawable drawable = context.getDrawable(Utils.getWifiIconResource(level));
        Drawable drawable = mIconInjector.getIcon(level);
        if (!mForSavedNetworks && drawable != null) {
        if (!mForSavedNetworks && drawable != null) {
            drawable.setTint(Utils.getColorAttr(context, android.R.attr.colorControlNormal));
            drawable.setTint(Utils.getColorAttr(context, android.R.attr.colorControlNormal));
            setIcon(drawable);
            setIcon(drawable);
@@ -319,4 +314,16 @@ public class AccessPointPreference extends Preference {
            return mBadges.valueAt(index);
            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 Original line Diff line number Diff line
@@ -16,16 +16,23 @@
package com.android.settingslib.wifi;
package com.android.settingslib.wifi;


import static com.google.common.truth.Truth.assertThat;
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.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;


import android.content.Context;
import android.content.Context;


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


import org.junit.Before;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Config;


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


    private Context mContext = RuntimeEnvironment.application;
    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
    @Test
    public void generatePreferenceKey_shouldReturnSsidPlusSecurity() {
    public void generatePreferenceKey_shouldReturnSsidPlusSecurity() {
        String ssid = "ssid";
        String ssid = "ssid";
@@ -78,4 +101,16 @@ public class AccessPointPreferenceTest {
                RuntimeEnvironment.application, pref, ap))
                RuntimeEnvironment.application, pref, ap))
                .isEqualTo("ssid,connected,Wifi signal full.,Secure network");
                .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);
    }
}
}