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