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