MobileNetworkPreferenceControllerTest.java revision f86905ed50f7a5543edb1b7425e0fa2db985de1c
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 com.android.settings.SettingsRobolectricTestRunner; 23import com.android.settings.TestConfig; 24import org.junit.Before; 25import org.junit.Test; 26import org.junit.runner.RunWith; 27import org.mockito.Answers; 28import org.mockito.Mock; 29import org.mockito.MockitoAnnotations; 30import org.robolectric.annotation.Config; 31 32import static com.google.common.truth.Truth.assertThat; 33import static org.mockito.Matchers.any; 34import static org.mockito.Matchers.anyString; 35import static org.mockito.Mockito.when; 36 37@RunWith(SettingsRobolectricTestRunner.class) 38@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 39public class MobileNetworkPreferenceControllerTest { 40 41 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 42 private Context mContext; 43 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 44 private UserManager mUserManager; 45 @Mock 46 private ConnectivityManager mConnectivityManager; 47 48 private MobileNetworkPreferenceController mController; 49 50 @Before 51 public void setUp() { 52 MockitoAnnotations.initMocks(this); 53 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 54 when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)) 55 .thenReturn(mConnectivityManager); 56 } 57 58 @Test 59 public void secondaryUser_prefIsNotAvailable() { 60 when(mUserManager.isAdminUser()).thenReturn(false); 61 when(mUserManager.hasUserRestriction(anyString(), any(UserHandle.class))) 62 .thenReturn(false); 63 when(mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)) 64 .thenReturn(true); 65 66 mController = new MobileNetworkPreferenceController(mContext); 67 assertThat(mController.isAvailable()).isFalse(); 68 } 69 70 @Test 71 public void wifiOnly_prefIsNotAvailable() { 72 when(mUserManager.isAdminUser()).thenReturn(true); 73 when(mUserManager.hasUserRestriction(anyString(), any(UserHandle.class))) 74 .thenReturn(false); 75 when(mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)) 76 .thenReturn(false); 77 78 mController = new MobileNetworkPreferenceController(mContext); 79 assertThat(mController.isAvailable()).isFalse(); 80 } 81} 82