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.settings.testutils.shadow.ShadowRestrictedLockUtilsWrapper;
30import com.android.settingslib.core.lifecycle.Lifecycle;
31
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.mockito.Answers;
36import org.mockito.Mock;
37import org.mockito.MockitoAnnotations;
38import org.robolectric.annotation.Config;
39
40import static com.google.common.truth.Truth.assertThat;
41import static org.mockito.Matchers.any;
42import static org.mockito.Matchers.anyString;
43import static org.mockito.Mockito.doReturn;
44import static org.mockito.Mockito.mock;
45import static org.mockito.Mockito.spy;
46import static org.mockito.Mockito.verify;
47import static org.mockito.Mockito.when;
48
49@RunWith(SettingsRobolectricTestRunner.class)
50@Config(
51    manifest = TestConfig.MANIFEST_PATH,
52    sdk = TestConfig.SDK_VERSION,
53    shadows = ShadowRestrictedLockUtilsWrapper.class
54)
55public class MobileNetworkPreferenceControllerTest {
56
57    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
58    private Context mContext;
59    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
60    private UserManager mUserManager;
61    @Mock
62    private ConnectivityManager mConnectivityManager;
63    @Mock
64    private TelephonyManager mTelephonyManager;
65    @Mock
66    private PreferenceScreen mScreen;
67
68    private Lifecycle mLifecycle;
69    private MobileNetworkPreferenceController mController;
70
71    @Before
72    public void setUp() {
73        MockitoAnnotations.initMocks(this);
74        mLifecycle = new Lifecycle();
75        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
76        when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
77                .thenReturn(mConnectivityManager);
78        when(mContext.getSystemService(Context.TELEPHONY_SERVICE))
79                .thenReturn(mTelephonyManager);
80    }
81
82    @Test
83    public void secondaryUser_prefIsNotAvailable() {
84        when(mUserManager.isAdminUser()).thenReturn(false);
85        when(mUserManager.hasUserRestriction(anyString(), any(UserHandle.class)))
86                .thenReturn(false);
87        when(mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE))
88                .thenReturn(true);
89
90        mController = new MobileNetworkPreferenceController(mContext);
91        assertThat(mController.isAvailable()).isFalse();
92    }
93
94    @Test
95    public void wifiOnly_prefIsNotAvailable() {
96        when(mUserManager.isAdminUser()).thenReturn(true);
97        when(mUserManager.hasUserRestriction(anyString(), any(UserHandle.class)))
98                .thenReturn(false);
99        when(mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE))
100                .thenReturn(false);
101
102        mController = new MobileNetworkPreferenceController(mContext);
103        assertThat(mController.isAvailable()).isFalse();
104    }
105
106    @Test
107    public void goThroughLifecycle_isAvailable_shouldListenToServiceChange() {
108        mController = spy(new MobileNetworkPreferenceController(mContext));
109        mLifecycle.addObserver(mController);
110        doReturn(true).when(mController).isAvailable();
111
112        mLifecycle.onResume();
113        verify(mTelephonyManager).listen(mController.mPhoneStateListener,
114                PhoneStateListener.LISTEN_SERVICE_STATE);
115
116        mLifecycle.onPause();
117        verify(mTelephonyManager).listen(mController.mPhoneStateListener,
118                PhoneStateListener.LISTEN_NONE);
119    }
120
121    @Test
122    public void serviceStateChange_shouldUpdatePrefSummary() {
123        final String testCarrierName = "test";
124        final Preference mPreference = mock(Preference.class);
125        mController = spy(new MobileNetworkPreferenceController(mContext));
126        mLifecycle.addObserver(mController);
127
128        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
129        doReturn(true).when(mController).isAvailable();
130
131        // Display pref and go through lifecycle to set up listener.
132        mController.displayPreference(mScreen);
133        mLifecycle.onResume();
134        verify(mController).onResume();
135        verify(mTelephonyManager).listen(mController.mPhoneStateListener,
136                PhoneStateListener.LISTEN_SERVICE_STATE);
137
138        // Trigger listener update
139        when(mTelephonyManager.getNetworkOperatorName()).thenReturn(testCarrierName);
140        mController.mPhoneStateListener.onServiceStateChanged(null);
141
142        // Carrier name should be set.
143        verify(mPreference).setSummary(testCarrierName);
144    }
145
146}
147