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

Commit f705f793 authored by Jeremy Goldman's avatar Jeremy Goldman Committed by Android (Google) Code Review
Browse files

Merge "Remove Settings robolectric tests which also have a junit test."

parents b802a1a8 be4d554b
Loading
Loading
Loading
Loading
+0 −82
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.datausage;

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

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.util.ArraySet;

import androidx.preference.Preference;

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

@RunWith(RobolectricTestRunner.class)
public class AppPrefLoaderTest {

    @Mock
    private PackageManager mPackageManager;

    private AppPrefLoader mLoader;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        final ArraySet<String> pkgs = new ArraySet<>(2);
        pkgs.add("pkg0");
        pkgs.add("pkg1");
        mLoader = new AppPrefLoader(RuntimeEnvironment.application, pkgs, mPackageManager);
    }

    @Test
    public void loadInBackground_packageNotFound_shouldReturnEmptySet()
            throws NameNotFoundException {
        when(mPackageManager.getApplicationInfo(anyString(), anyInt()))
            .thenThrow(new NameNotFoundException());

        assertThat(mLoader.loadInBackground()).isEmpty();
    }

    @Test
    public void loadInBackground_shouldReturnPreference() throws NameNotFoundException {
        ApplicationInfo info = mock(ApplicationInfo.class);
        when(mPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(info);
        final Drawable drawable = mock(Drawable.class);
        final String label = "Label1";
        when(info.loadIcon(mPackageManager)).thenReturn(drawable);
        when(info.loadLabel(mPackageManager)).thenReturn(label);

        Preference preference = mLoader.loadInBackground().valueAt(0);
        assertThat(preference.getTitle()).isEqualTo(label);
        assertThat(preference.getIcon()).isEqualTo(drawable);
        assertThat(preference.isSelectable()).isFalse();
    }
}
+0 −97
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.datausage;

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

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;

import android.content.Context;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.preference.PreferenceViewHolder;

import com.android.settings.network.ProxySubscriptionManager;

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

@RunWith(RobolectricTestRunner.class)
public class CellDataPreferenceTest {

    @Mock
    private ProxySubscriptionManager mProxySubscriptionMgr;
    @Mock
    private SubscriptionManager mSubscriptionManager;
    @Mock
    private SubscriptionInfo mSubInfo;

    private Context mContext;
    private PreferenceViewHolder mHolder;
    private CellDataPreference mPreference;

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

        mContext = RuntimeEnvironment.application;
        mPreference = new CellDataPreference(mContext, null) {
            @Override
            ProxySubscriptionManager getProxySubscriptionManager() {
                return mProxySubscriptionMgr;
            }
            @Override
            SubscriptionInfo getActiveSubscriptionInfo(int subId) {
                return mSubInfo;
            }
        };
        doNothing().when(mSubscriptionManager).setDefaultDataSubId(anyInt());
        doReturn(mSubscriptionManager).when(mProxySubscriptionMgr).get();
        doNothing().when(mProxySubscriptionMgr).addActiveSubscriptionsListener(any());
        doNothing().when(mProxySubscriptionMgr).removeActiveSubscriptionsListener(any());

        final LayoutInflater inflater = LayoutInflater.from(mContext);
        final View view = inflater.inflate(mPreference.getLayoutResource(),
                new LinearLayout(mContext), false);

        mHolder = PreferenceViewHolder.createInstanceForTests(view);
    }

    @Test
    public void noActiveSub_shouldDisable() {
        mSubInfo = null;
        mPreference.mOnSubscriptionsChangeListener.onChanged();
        assertThat(mPreference.isEnabled()).isFalse();
    }

    @Test
    public void hasActiveSub_shouldEnable() {
        mPreference.mOnSubscriptionsChangeListener.onChanged();
        assertThat(mPreference.isEnabled()).isTrue();
    }
}
+0 −144
Original line number Diff line number Diff line
package com.android.settings.datausage;

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

import android.net.NetworkPolicy;
import android.net.NetworkTemplate;

import com.android.settingslib.net.DataUsageController.DataUsageInfo;

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

@RunWith(RobolectricTestRunner.class)
public class DataUsageInfoControllerTest {

    private static final int NEGATIVE = -1;
    private static final int ZERO = 0;
    private static final int POSITIVE_SMALL = 1;
    private static final int POSITIVE_LARGE = 5;

    private DataUsageInfoController mInfoController;
    private DataUsageInfo info;

    @Before
    public void setUp()  {
        mInfoController = new DataUsageInfoController();
        info = new DataUsageInfo();
    }

    @Test
    public void testLowUsageLowWarning_LimitUsed() {
        info.warningLevel = POSITIVE_SMALL;
        info.limitLevel = POSITIVE_LARGE;
        info.usageLevel = POSITIVE_SMALL;
        assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.limitLevel);
    }

    @Test
    public void testLowUsageEqualWarning_LimitUsed() {
        info.warningLevel = POSITIVE_LARGE;
        info.limitLevel = POSITIVE_LARGE;
        info.usageLevel = POSITIVE_SMALL;
        assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.limitLevel);
    }

    @Test
    public void testNoLimitNoUsage_WarningUsed() {
        info.warningLevel = POSITIVE_LARGE;
        info.limitLevel = ZERO;
        info.usageLevel = ZERO;
        assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.warningLevel);
    }

    @Test
    public void testNoLimitLowUsage_WarningUsed() {
        info.warningLevel = POSITIVE_LARGE;
        info.limitLevel = ZERO;
        info.usageLevel = POSITIVE_SMALL;
        assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.warningLevel);
    }

    @Test
    public void testLowWarningNoLimit_UsageUsed() {
        info.warningLevel = POSITIVE_SMALL;
        info.limitLevel = ZERO;
        info.usageLevel = POSITIVE_LARGE;
        assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.usageLevel);
    }

    @Test
    public void testLowWarningLowLimit_UsageUsed() {
        info.warningLevel = POSITIVE_SMALL;
        info.limitLevel = POSITIVE_SMALL;
        info.usageLevel = POSITIVE_LARGE;
        assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.usageLevel);
    }

    private NetworkPolicy getDefaultNetworkPolicy() {
        NetworkTemplate template =
            new NetworkTemplate(NetworkTemplate.MATCH_WIFI_WILDCARD, null, null);
        int cycleDay  = -1;
        String cycleTimezone = "UTC";
        long warningBytes = -1;
        long limitBytes = -1;
        return new NetworkPolicy(template, cycleDay, cycleTimezone, warningBytes, limitBytes, true);
    }

    @Test
    public void testNullArguments_NoError() {
        mInfoController.updateDataLimit(null, null);
        mInfoController.updateDataLimit(info, null);
        mInfoController.updateDataLimit(null, getDefaultNetworkPolicy());
    }

    @Test
    public void testNegativeWarning_UpdatedToZero() {
        NetworkPolicy policy = getDefaultNetworkPolicy();
        policy.warningBytes = NEGATIVE;
        mInfoController.updateDataLimit(info, policy);
        assertThat(info.warningLevel).isEqualTo(ZERO);
    }

    @Test
    public void testWarningZero_UpdatedToZero() {
        NetworkPolicy policy = getDefaultNetworkPolicy();
        policy.warningBytes = ZERO;
        mInfoController.updateDataLimit(info, policy);
        assertThat(info.warningLevel).isEqualTo(ZERO);
    }

    @Test
    public void testWarningPositive_UpdatedToWarning() {
        NetworkPolicy policy = getDefaultNetworkPolicy();
        policy.warningBytes = POSITIVE_SMALL;
        mInfoController.updateDataLimit(info, policy);
        assertThat(info.warningLevel).isEqualTo(policy.warningBytes);
    }

    @Test
    public void testLimitNegative_UpdatedToZero() {
        NetworkPolicy policy = getDefaultNetworkPolicy();
        policy.limitBytes = NEGATIVE;
        mInfoController.updateDataLimit(info, policy);
        assertThat(info.limitLevel).isEqualTo(ZERO);
    }

    @Test
    public void testLimitZero_UpdatedToZero() {
        NetworkPolicy policy = getDefaultNetworkPolicy();
        policy.limitBytes = ZERO;
        mInfoController.updateDataLimit(info, policy);
        assertThat(info.limitLevel).isEqualTo(ZERO);
    }

    @Test
    public void testLimitPositive_UpdatedToLimit() {
        NetworkPolicy policy = getDefaultNetworkPolicy();
        policy.limitBytes = POSITIVE_SMALL;
        mInfoController.updateDataLimit(info, policy);
        assertThat(info.limitLevel).isEqualTo(policy.limitBytes);
    }
}
 No newline at end of file
+0 −86
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.deviceinfo;

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

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.storage.VolumeRecord;
import android.widget.Button;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentActivity;

import com.android.settings.R;
import com.android.settings.deviceinfo.PrivateVolumeForget.ForgetConfirmFragment;
import com.android.settings.testutils.shadow.ShadowStorageManager;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.androidx.fragment.FragmentController;

@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowStorageManager.class)
public class PrivateVolumeForgetTest {

    private PrivateVolumeForget mFragment;
    private FragmentActivity mActivity;

    @Before
    public void setUp() {
        final Bundle bundle = new Bundle();
        bundle.putString(VolumeRecord.EXTRA_FS_UUID, "id");
        mFragment = FragmentController.of(new PrivateVolumeForget(), bundle)
                .create()
                .start()
                .resume()
                .visible()
                .get();
        mActivity = mFragment.getActivity();
    }

    @After
    public void tearDown() {
        ShadowStorageManager.reset();
    }

    @Test
    public void OnClickListener_shouldCallForget() {
        assertThat(ShadowStorageManager.isForgetCalled()).isFalse();

        final Button confirm = mFragment.getView().findViewById(R.id.confirm);

        confirm.performClick();
        final ForgetConfirmFragment confirmFragment =
                (ForgetConfirmFragment) mActivity.getSupportFragmentManager().findFragmentByTag(
                        PrivateVolumeForget.TAG_FORGET_CONFIRM);

        assertThat(confirmFragment).isNotNull();

        final AlertDialog dialog = (AlertDialog) confirmFragment.getDialog();
        final Button forget = dialog.getButton(DialogInterface.BUTTON_POSITIVE);

        forget.performClick();

        assertThat(ShadowStorageManager.isForgetCalled()).isTrue();
    }
}
 No newline at end of file
+0 −182
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.network;

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

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.Settings;
import android.provider.SettingsSlicesContract;

import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;

import com.android.settings.AirplaneModeEnabler;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.RestrictedSwitchPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;

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

@RunWith(RobolectricTestRunner.class)
public class AirplaneModePreferenceControllerTest {

    private static final int ON = 1;
    private static final int OFF = 0;

    @Mock
    private PackageManager mPackageManager;
    @Mock
    private AirplaneModeEnabler mAirplaneModeEnabler;
    private Context mContext;
    private ContentResolver mResolver;
    private PreferenceManager mPreferenceManager;
    private PreferenceScreen mScreen;
    private RestrictedSwitchPreference mPreference;
    private AirplaneModePreferenceController mController;
    private LifecycleOwner mLifecycleOwner;
    private Lifecycle mLifecycle;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        FakeFeatureFactory.setupForTest();
        mContext = spy(RuntimeEnvironment.application);
        mResolver = RuntimeEnvironment.application.getContentResolver();
        doReturn(mPackageManager).when(mContext).getPackageManager();
        mController = new AirplaneModePreferenceController(mContext,
                SettingsSlicesContract.KEY_AIRPLANE_MODE);

        mPreferenceManager = new PreferenceManager(mContext);
        mScreen = mPreferenceManager.createPreferenceScreen(mContext);
        mPreference = new RestrictedSwitchPreference(mContext);
        mPreference.setKey(SettingsSlicesContract.KEY_AIRPLANE_MODE);
        mScreen.addPreference(mPreference);
        mController.setFragment(null);
        mLifecycleOwner = () -> mLifecycle;
        mLifecycle = new Lifecycle(mLifecycleOwner);
        mLifecycle.addObserver(mController);
    }

    @Test
    public void getSliceUri_shouldUsePlatformAuthority() {
        assertThat(mController.getSliceUri().getAuthority())
                .isEqualTo(SettingsSlicesContract.AUTHORITY);
    }

    @Test
    @Config(qualifiers = "mcc999")
    public void airplaneModePreference_shouldNotBeAvailable_ifSetToNotVisible() {
        assertThat(mController.getAvailabilityStatus())
                .isNotEqualTo(BasePreferenceController.AVAILABLE);

        mController.displayPreference(mScreen);

        // This should not crash
        mController.onStart();
        mController.onStop();
    }

    @Test
    public void airplaneModePreference_shouldNotBeAvailable_ifHasLeanbackFeature() {
        when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK)).thenReturn(true);
        assertThat(mController.getAvailabilityStatus())
                .isNotEqualTo(BasePreferenceController.AVAILABLE);

        mController.displayPreference(mScreen);

        // This should not crash
        mController.onStart();
        mController.onStop();
    }

    @Test
    public void airplaneModePreference_shouldBeAvailable_ifNoLeanbackFeature() {
        when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK)).thenReturn(false);
        assertThat(mController.getAvailabilityStatus())
                .isEqualTo(BasePreferenceController.AVAILABLE);
    }

    @Test
    public void airplaneModePreference_testSetValue_updatesCorrectly() {

        // Set airplane mode ON by setChecked
        mController.setAirplaneModeEnabler(mAirplaneModeEnabler);
        assertThat(mController.setChecked(true)).isTrue();

        // Check return value if set same status.
        when(mAirplaneModeEnabler.isAirplaneModeOn()).thenReturn(true);
        assertThat(mController.setChecked(true)).isFalse();

        // Set to OFF
        assertThat(mController.setChecked(false)).isTrue();
    }

    @Test
    public void airplaneModePreference_testGetValue_correctValueReturned() {
        // Set airplane mode ON
        Settings.Global.putInt(mResolver, Settings.Global.AIRPLANE_MODE_ON, ON);

        mController.displayPreference(mScreen);
        mController.onStart();

        assertThat(mController.isChecked()).isTrue();

        Settings.Global.putInt(mResolver, Settings.Global.AIRPLANE_MODE_ON, OFF);
        assertThat(mController.isChecked()).isFalse();
    }

    @Test
    public void airplaneModePreference_testPreferenceUI_updatesCorrectly() {
        // Airplane mode default off
        Settings.Global.putInt(mResolver, Settings.Global.AIRPLANE_MODE_ON, OFF);

        mController.displayPreference(mScreen);
        mController.onStop();

        assertThat(mPreference.isChecked()).isFalse();

        mController.onAirplaneModeChanged(true);

        assertThat(mPreference.isChecked()).isTrue();
    }

    @Test
    public void isSliceable_returnsTrue() {
        assertThat(mController.isSliceable()).isTrue();
    }

    @Test
    public void isPublicSlice_returnsTrue() {
        assertThat(mController.isPublicSlice()).isTrue();
    }
}
Loading