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

Commit a22bd67d authored by Kevin Chang's avatar Kevin Chang
Browse files

Add test cases for BalanceSeekBar

Bug: 123722274
Test: Manual
      make RunSettingsRoboTests ROBOTEST_FILTER=BalanceSeekBar

Change-Id: Ie6b560b2f92d78a0d3bd7ddb0e0d0a85c7803827
parent 38d1640d
Loading
Loading
Loading
Loading
+13 −6
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@ import android.provider.Settings;
import android.util.AttributeSet;
import android.widget.SeekBar;

import androidx.annotation.VisibleForTesting;

import com.android.settings.R;

/**
@@ -37,7 +39,6 @@ import com.android.settings.R;
 * Updates Settings.System for balance on progress changed.
 */
public class BalanceSeekBar extends SeekBar {
    private static final String TAG = "BalanceSeekBar";
    private final Context mContext;
    private final Object mListenerLock = new Object();
    private OnSeekBarChangeListener mOnSeekBarChangeListener;
@@ -88,7 +89,8 @@ public class BalanceSeekBar extends SeekBar {
    };

    // Percentage of max to be used as a snap to threshold
    private static final float SNAP_TO_PERCENTAGE = 0.03f;
    @VisibleForTesting
    static final float SNAP_TO_PERCENTAGE = 0.03f;
    private final Paint mCenterMarkerPaint;
    private final Rect mCenterMarkerRect;
    // changed in setMax()
@@ -148,5 +150,10 @@ public class BalanceSeekBar extends SeekBar {
        canvas.restore();
        super.onDraw(canvas);
    }

    @VisibleForTesting
    OnSeekBarChangeListener getProxySeekBarListener() {
        return mProxySeekBarListener;
    }
}
+6 −4
Original line number Diff line number Diff line
@@ -34,7 +34,9 @@ import com.android.settings.widget.SeekBarPreference;

/** A slider preference that directly controls audio balance **/
public class BalanceSeekBarPreference extends SeekBarPreference {
    private static final String TAG = "BalanceSeekBarPreference";
    private static final int BALANCE_CENTER_VALUE = 100;
    private static final int BALANCE_MAX_VALUE = 200;

    private final Context mContext;
    private BalanceSeekBar mSeekBar;
    private ImageView mIconView;
@@ -62,9 +64,9 @@ public class BalanceSeekBarPreference extends SeekBarPreference {
        final float balance = Settings.System.getFloatForUser(
                mContext.getContentResolver(), Settings.System.MASTER_BALANCE,
                0.f /* default */, UserHandle.USER_CURRENT);
        // Rescale balance to range 0-200 centered at 100.
        mSeekBar.setMax(200);
        mSeekBar.setProgress((int)(balance * 100.f) + 100);
        // Rescale balance to range 0-BALANCE_MAX_VALUE centered at BALANCE_MAX_VALUE / 2.
        mSeekBar.setMax(BALANCE_MAX_VALUE);
        mSeekBar.setProgress((int) (balance * 100.f) + BALANCE_CENTER_VALUE);
        mSeekBar.setEnabled(isEnabled());
    }
}
+65 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.accessibility;

import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.preference.PreferenceViewHolder;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

@RunWith(RobolectricTestRunner.class)
public class BalanceSeekBarPreferenceTest {
    private static final int BALANCE_CENTER_VALUE = 100;
    private static final int BALANCE_MAX_VALUE = 200;

    private Context mContext;
    private AttributeSet mAttrs;
    private PreferenceViewHolder mHolder;
    private BalanceSeekBar mSeekBar;
    private BalanceSeekBarPreference mSeekBarPreference;

    @Before
    public void setUp() {
        mContext = RuntimeEnvironment.application;
        mSeekBarPreference = new BalanceSeekBarPreference(mContext, mAttrs);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        final View view =
                inflater.inflate(mSeekBarPreference.getLayoutResource(),
                        new LinearLayout(mContext), false);
        mHolder = PreferenceViewHolder.createInstanceForTests(view);
        mSeekBar = (BalanceSeekBar) view.findViewById(com.android.internal.R.id.seekbar);
    }

    @Test
    public void seekBarPreferenceOnBindViewHolder_shouldInitSeekBarValue() {
        mSeekBarPreference.onBindViewHolder(mHolder);

        assertThat(mSeekBar.getMax()).isEqualTo(BALANCE_MAX_VALUE);
        assertThat(mSeekBar.getProgress()).isEqualTo(BALANCE_CENTER_VALUE);
    }
}
+120 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.accessibility;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.SeekBar;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

@RunWith(RobolectricTestRunner.class)
public class BalanceSeekBarTest {
    // Fix the maximum process value to 200 for testing the BalanceSeekBar.
    // It affects the SeekBar value of center(100) and snapThreshold(200 * SNAP_TO_PERCENTAGE).
    private static final int MAX_PROGRESS_VALUE = 200;

    private Context mContext;
    private AttributeSet mAttrs;
    private BalanceSeekBar mSeekBar;
    private BalanceSeekBar.OnSeekBarChangeListener mProxySeekBarListener;
    private SeekBar.OnSeekBarChangeListener mockSeekBarChangeListener;

    @Before
    public void setUp() {
        mContext = RuntimeEnvironment.application;
        mSeekBar = new BalanceSeekBar(mContext, mAttrs);
        mProxySeekBarListener = mSeekBar.getProxySeekBarListener();
        mockSeekBarChangeListener = mock(SeekBar.OnSeekBarChangeListener.class);
        mSeekBar.setOnSeekBarChangeListener(mockSeekBarChangeListener);
    }

    @Test
    public void onStartTrackingTouch_shouldInvokeMethod() {
        mProxySeekBarListener.onStartTrackingTouch(mSeekBar);

        verify(mockSeekBarChangeListener, times(1)).onStartTrackingTouch(mSeekBar);
    }

    @Test
    public void onStopTrackingTouch_shouldInvokeMethod() {
        mProxySeekBarListener.onStopTrackingTouch(mSeekBar);

        verify(mockSeekBarChangeListener, times(1)).onStopTrackingTouch(mSeekBar);
    }

    @Test
    public void onProgressChanged_shouldInvokeMethod() {
        // Assign the test value of SeekBar progress
        mProxySeekBarListener.onProgressChanged(mSeekBar, MAX_PROGRESS_VALUE, true);

        verify(mockSeekBarChangeListener, times(1)).onProgressChanged(eq(mSeekBar),
                eq(MAX_PROGRESS_VALUE), eq(true));
    }

    @Test
    public void setMaxTest_shouldSetValue() {
        mSeekBar.setMax(MAX_PROGRESS_VALUE);

        assertThat(getBalanceSeekBarCenter(mSeekBar)).isEqualTo(MAX_PROGRESS_VALUE / 2);
        assertThat(getBalanceSeekBarSnapThreshold(mSeekBar)).isEqualTo(
                MAX_PROGRESS_VALUE * BalanceSeekBar.SNAP_TO_PERCENTAGE);
    }

    @Test
    public void setProgressTest_shouldSnapToCenter() {
        // Assign the test value of SeekBar progress within the threshold (94-106 in this case).
        final int progressWithinThreshold = 102;
        mSeekBar.setMax(MAX_PROGRESS_VALUE);
        mSeekBar.setProgress(progressWithinThreshold + 10); //set progress which is over threshold.
        mProxySeekBarListener.onProgressChanged(mSeekBar, progressWithinThreshold, true);

        assertThat(mSeekBar.getProgress()).isEqualTo(getBalanceSeekBarCenter(mSeekBar));
    }

    @Test
    public void setProgressTest_shouldMaintainInputValue() {
        // Assign the test value of SeekBar progress without the threshold.
        final int progressWithoutThreshold = 107;
        mSeekBar.setMax(MAX_PROGRESS_VALUE);
        mSeekBar.setProgress(progressWithoutThreshold);
        mProxySeekBarListener.onProgressChanged(mSeekBar, progressWithoutThreshold, true);

        assertThat(mSeekBar.getProgress()).isEqualTo(progressWithoutThreshold);
    }

    // method to get the center from BalanceSeekBar for testing setMax().
    private int getBalanceSeekBarCenter(BalanceSeekBar seekBar) {
        return seekBar.getMax() / 2;
    }

    // method to get the snapThreshold from BalanceSeekBar for testing setMax().
    private float getBalanceSeekBarSnapThreshold(BalanceSeekBar seekBar) {
        return seekBar.getMax() * BalanceSeekBar.SNAP_TO_PERCENTAGE;
    }
}