1 /* 2 * Copyright (C) 2017 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 */ 16 17 package com.android.settings.wfd; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.when; 23 24 import android.app.Activity; 25 import android.content.Context; 26 import android.content.pm.PackageManager; 27 import android.hardware.display.DisplayManager; 28 import android.media.MediaRouter; 29 import android.net.wifi.p2p.WifiP2pManager; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.RobolectricTestRunner; 37 38 @RunWith(RobolectricTestRunner.class) 39 public class WifiDisplaySettingsTest { 40 41 @Mock 42 private Activity mActivity; 43 @Mock 44 private MediaRouter mMediaRouter; 45 @Mock 46 private PackageManager mPackageManager; 47 48 @Before setUp()49 public void setUp() { 50 MockitoAnnotations.initMocks(this); 51 when(mActivity.getSystemService(Context.MEDIA_ROUTER_SERVICE)).thenReturn(mMediaRouter); 52 when(mActivity.getPackageManager()).thenReturn(mPackageManager); 53 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)).thenReturn(true); 54 } 55 56 @Test isAvailable_nullService_shouldReturnFalse()57 public void isAvailable_nullService_shouldReturnFalse() { 58 assertThat(WifiDisplaySettings.isAvailable(mActivity)).isFalse(); 59 } 60 61 @Test isAvailable_noWifiDirectFeature_shouldReturnFalse()62 public void isAvailable_noWifiDirectFeature_shouldReturnFalse() { 63 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)) 64 .thenReturn(false); 65 66 assertThat(WifiDisplaySettings.isAvailable(mActivity)).isFalse(); 67 } 68 69 @Test isAvailable_hasService_shouldReturnTrue()70 public void isAvailable_hasService_shouldReturnTrue() { 71 when(mActivity.getSystemService(Context.DISPLAY_SERVICE)) 72 .thenReturn(mock(DisplayManager.class)); 73 when(mActivity.getSystemService(Context.WIFI_P2P_SERVICE)) 74 .thenReturn(mock(WifiP2pManager.class)); 75 76 assertThat(WifiDisplaySettings.isAvailable(mActivity)).isTrue(); 77 } 78 } 79