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

Commit 866f40b2 authored by changbetty's avatar changbetty
Browse files

[Testing] Use AndroidJUnit4 instead in MmsMessagePreferenceControllerTest

1. Use AndroidJunit4 instead of RobolectricTestRunner
2. Change the Copy Right
3. Remove the ShadowSubscriptionManager

Bug: 168278301
Test: atest -c MmsMessagePreferenceControllerTest
Change-Id: Ie2e9c99f9899d422fde41fcd6cf4a918bc0eaf98
parent 279ececa
Loading
Loading
Loading
Loading
+119 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.telephony;

import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;

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

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

import android.content.Context;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.telephony.data.ApnSetting;

import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

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

@RunWith(AndroidJUnit4.class)
public class MmsMessagePreferenceControllerTest {
    private static final int SUB_ID = 2;

    @Mock
    private TelephonyManager mTelephonyManager;
    @Mock
    private SubscriptionManager mSubscriptionManager;

    private MmsMessagePreferenceController mController;
    private SwitchPreference mPreference;
    private Context mContext;

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

        mContext = spy(ApplicationProvider.getApplicationContext());
        when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
        when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
        when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
        when(mTelephonyManager.createForSubscriptionId(SUB_ID)).thenReturn(mTelephonyManager);

        mPreference = new SwitchPreference(mContext);
        mController = new MmsMessagePreferenceController(mContext, "mms_message");
        mController.init(SUB_ID);
        mPreference.setKey(mController.getPreferenceKey());
    }

    @Test
    public void getAvailabilityStatus_invalidSubscription_returnUnavailable() {
        mController.init(SubscriptionManager.INVALID_SUBSCRIPTION_ID);

        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
    }

    @Test
    public void getAvailabilityStatus_mobileDataOn_returnUnavailable() {
        when(mTelephonyManager.isDataEnabled()).thenReturn(true);

        assertThat(mController.getAvailabilityStatus(SUB_ID)).isEqualTo(CONDITIONALLY_UNAVAILABLE);
    }

    @Test
    public void getAvailabilityStatus_meteredOff_returnUnavailable() {
        when(mTelephonyManager.isApnMetered(ApnSetting.TYPE_MMS)).thenReturn(false);

        assertThat(mController.getAvailabilityStatus(SUB_ID)).isEqualTo(CONDITIONALLY_UNAVAILABLE);
    }

    @Test
    public void getAvailabilityStatus_mobileDataOffWithValidSubId_returnAvailable() {
        mController.init(SUB_ID);
        when(mTelephonyManager.isDataEnabled()).thenReturn(false);
        when(mTelephonyManager.isApnMetered(ApnSetting.TYPE_MMS)).thenReturn(true);

        assertThat(mController.getAvailabilityStatus(SUB_ID)).isEqualTo(AVAILABLE);
    }

    @Test
    public void isChecked_returnDataFromTelephonyManager() {
        when(mTelephonyManager.isDataEnabledForApn(ApnSetting.TYPE_MMS)).thenReturn(false);
        assertThat(mController.isChecked()).isFalse();

        when(mTelephonyManager.isDataEnabledForApn(ApnSetting.TYPE_MMS)).thenReturn(true);
        assertThat(mController.isChecked()).isTrue();
    }

    @Test
    public void setChecked_setDataIntoSubscriptionManager() {
        mController.setChecked(true);
        verify(mTelephonyManager).setAlwaysAllowMmsData(true);

        mController.setChecked(false);
        verify(mTelephonyManager).setAlwaysAllowMmsData(false);
    }
}