MobileNetworkPreferenceControllerTest.java revision 9f1e911759dc6fedaac9fa65afb79f6a93022bf4
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16package com.android.settings.network;
17
18import android.content.Context;
19import android.net.ConnectivityManager;
20import android.os.UserHandle;
21import android.os.UserManager;
22import android.support.v7.preference.Preference;
23import android.support.v7.preference.PreferenceScreen;
24import android.telephony.PhoneStateListener;
25import android.telephony.TelephonyManager;
26
27import com.android.settings.testutils.SettingsRobolectricTestRunner;
28import com.android.settings.TestConfig;
29import com.android.settingslib.core.lifecycle.Lifecycle;
30
31import org.junit.Before;
32import org.junit.Test;
33import org.junit.runner.RunWith;
34import org.mockito.Answers;
35import org.mockito.Mock;
36import org.mockito.MockitoAnnotations;
37import org.robolectric.annotation.Config;
38
39import static com.google.common.truth.Truth.assertThat;
40import static org.mockito.Matchers.any;
41import static org.mockito.Matchers.anyString;
42import static org.mockito.Mockito.doReturn;
43import static org.mockito.Mockito.mock;
44import static org.mockito.Mockito.spy;
45import static org.mockito.Mockito.verify;
46import static org.mockito.Mockito.when;
47
48@RunWith(SettingsRobolectricTestRunner.class)
49@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
50public class MobileNetworkPreferenceControllerTest {
51
52    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
53    private Context mContext;
54    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
55    private UserManager mUserManager;
56    @Mock
57    private ConnectivityManager mConnectivityManager;
58    @Mock
59    private TelephonyManager mTelephonyManager;
60    @Mock
61    private PreferenceScreen mScreen;
62
63    private Lifecycle mLifecycle;
64    private MobileNetworkPreferenceController mController;
65
66    @Before
67    public void setUp() {
68        MockitoAnnotations.initMocks(this);
69        mLifecycle = new Lifecycle();
70        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
71        when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
72                .thenReturn(mConnectivityManager);
73        when(mContext.getSystemService(Context.TELEPHONY_SERVICE))
74                .thenReturn(mTelephonyManager);
75    }
76
77    @Test
78    public void secondaryUser_prefIsNotAvailable() {
79        when(mUserManager.isAdminUser()).thenReturn(false);
80        when(mUserManager.hasUserRestriction(anyString(), any(UserHandle.class)))
81                .thenReturn(false);
82        when(mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE))
83                .thenReturn(true);
84
85        mController = new MobileNetworkPreferenceController(mContext);
86        assertThat(mController.isAvailable()).isFalse();
87    }
88
89    @Test
90    public void wifiOnly_prefIsNotAvailable() {
91        when(mUserManager.isAdminUser()).thenReturn(true);
92        when(mUserManager.hasUserRestriction(anyString(), any(UserHandle.class)))
93                .thenReturn(false);
94        when(mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE))
95                .thenReturn(false);
96
97        mController = new MobileNetworkPreferenceController(mContext);
98        assertThat(mController.isAvailable()).isFalse();
99    }
100
101    @Test
102    public void goThroughLifecycle_isAvailable_shouldListenToServiceChange() {
103        mController = spy(new MobileNetworkPreferenceController(mContext));
104        mLifecycle.addObserver(mController);
105        doReturn(true).when(mController).isAvailable();
106
107        mLifecycle.onResume();
108        verify(mTelephonyManager).listen(mController.mPhoneStateListener,
109                PhoneStateListener.LISTEN_SERVICE_STATE);
110
111        mLifecycle.onPause();
112        verify(mTelephonyManager).listen(mController.mPhoneStateListener,
113                PhoneStateListener.LISTEN_NONE);
114    }
115
116    @Test
117    public void serviceStateChange_shouldUpdatePrefSummary() {
118        final String testCarrierName = "test";
119        final Preference mPreference = mock(Preference.class);
120        mController = spy(new MobileNetworkPreferenceController(mContext));
121        mLifecycle.addObserver(mController);
122
123        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
124        doReturn(true).when(mController).isAvailable();
125
126        // Display pref and go through lifecycle to set up listener.
127        mController.displayPreference(mScreen);
128        mLifecycle.onResume();
129        verify(mController).onResume();
130        verify(mTelephonyManager).listen(mController.mPhoneStateListener,
131                PhoneStateListener.LISTEN_SERVICE_STATE);
132
133        // Trigger listener update
134        when(mTelephonyManager.getNetworkOperatorName()).thenReturn(testCarrierName);
135        mController.mPhoneStateListener.onServiceStateChanged(null);
136
137        // Carrier name should be set.
138        verify(mPreference).setSummary(testCarrierName);
139    }
140
141}
142