Home | History | Annotate | Download | only in wfd
      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 import static org.mockito.Mockito.mock;
     21 import static org.mockito.Mockito.never;
     22 import static org.mockito.Mockito.verify;
     23 import static org.mockito.Mockito.when;
     24 
     25 import android.app.Activity;
     26 import android.content.Context;
     27 import android.content.pm.PackageManager;
     28 import android.hardware.display.DisplayManager;
     29 import android.media.MediaRouter;
     30 import android.net.wifi.p2p.WifiP2pManager;
     31 
     32 import com.android.settings.R;
     33 import com.android.settings.dashboard.SummaryLoader;
     34 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     35 
     36 import org.junit.Before;
     37 import org.junit.Test;
     38 import org.junit.runner.RunWith;
     39 import org.mockito.Mock;
     40 import org.mockito.MockitoAnnotations;
     41 
     42 @RunWith(SettingsRobolectricTestRunner.class)
     43 public class WifiDisplaySettingsTest {
     44 
     45     @Mock
     46     private Activity mActivity;
     47     @Mock
     48     private SummaryLoader mSummaryLoader;
     49     @Mock
     50     private MediaRouter mMediaRouter;
     51     @Mock
     52     private PackageManager mPackageManager;
     53 
     54     private SummaryLoader.SummaryProvider mSummaryProvider;
     55 
     56     @Before
     57     public void setUp() {
     58         MockitoAnnotations.initMocks(this);
     59         when(mActivity.getSystemService(Context.MEDIA_ROUTER_SERVICE)).thenReturn(mMediaRouter);
     60         when(mActivity.getPackageManager()).thenReturn(mPackageManager);
     61         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)).thenReturn(true);
     62 
     63         mSummaryProvider = WifiDisplaySettings.SUMMARY_PROVIDER_FACTORY
     64             .createSummaryProvider(mActivity, mSummaryLoader);
     65     }
     66 
     67     @Test
     68     public void listenToSummary_disconnected_shouldProvideDisconnectedSummary() {
     69         mSummaryProvider.setListening(true);
     70 
     71         verify(mActivity).getString(R.string.disconnected);
     72         verify(mActivity, never()).getString(R.string.wifi_display_status_connected);
     73     }
     74 
     75     @Test
     76     public void listenToSummary_connected_shouldProvideConnectedSummary() {
     77         final MediaRouter.RouteInfo route = mock(MediaRouter.RouteInfo.class);
     78         when(mMediaRouter.getRouteCount()).thenReturn(1);
     79         when(mMediaRouter.getRouteAt(0)).thenReturn(route);
     80         when(route.matchesTypes(MediaRouter.ROUTE_TYPE_REMOTE_DISPLAY)).thenReturn(true);
     81         when(route.isSelected()).thenReturn(true);
     82         when(route.isConnecting()).thenReturn(false);
     83 
     84         mSummaryProvider.setListening(true);
     85 
     86         verify(mActivity).getString(R.string.wifi_display_status_connected);
     87     }
     88 
     89     @Test
     90     public void isAvailable_nullService_shouldReturnFalse() {
     91         assertThat(WifiDisplaySettings.isAvailable(mActivity)).isFalse();
     92     }
     93 
     94     @Test
     95     public void isAvailable_noWifiDirectFeature_shouldReturnFalse() {
     96         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT))
     97             .thenReturn(false);
     98 
     99         assertThat(WifiDisplaySettings.isAvailable(mActivity)).isFalse();
    100     }
    101 
    102     @Test
    103     public void isAvailable_hasService_shouldReturnTrue() {
    104         when(mActivity.getSystemService(Context.DISPLAY_SERVICE))
    105             .thenReturn(mock(DisplayManager.class));
    106         when(mActivity.getSystemService(Context.WIFI_P2P_SERVICE))
    107             .thenReturn(mock(WifiP2pManager.class));
    108 
    109         assertThat(WifiDisplaySettings.isAvailable(mActivity)).isTrue();
    110     }
    111 }
    112