1 /*
2  * Copyright (C) 2019 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.ArgumentMatchers.anyInt;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.content.pm.PackageManager;
28 import android.media.MediaRouter;
29 
30 import com.android.settings.R;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 
40 @RunWith(RobolectricTestRunner.class)
41 public class WifiDisplayPreferenceControllerTest {
42     private Context mContext;
43     private WifiDisplayPreferenceController mWifiDisplayPreferenceController;
44     @Mock
45     private MediaRouter mMediaRouter;
46     @Mock
47     private PackageManager mPackageManager;
48 
49     @Before
setUp()50     public void setUp() {
51         MockitoAnnotations.initMocks(this);
52         mContext = spy(RuntimeEnvironment.application);
53         when(mContext.getSystemService(Context.MEDIA_ROUTER_SERVICE)).thenReturn(mMediaRouter);
54         when(mContext.getPackageManager()).thenReturn(mPackageManager);
55         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)).thenReturn(true);
56         mWifiDisplayPreferenceController = new WifiDisplayPreferenceController(mContext, "key");
57     }
58 
59     @Test
getSummary_disconnected_shouldProvideDisconnectedSummary()60     public void getSummary_disconnected_shouldProvideDisconnectedSummary() {
61         assertThat(mWifiDisplayPreferenceController.getSummary().toString()).contains(
62                 mContext.getString(R.string.disconnected));
63     }
64 
65     @Test
getSummary_connected_shouldProvideConnectedSummary()66     public void getSummary_connected_shouldProvideConnectedSummary() {
67         final MediaRouter.RouteInfo route = mock(MediaRouter.RouteInfo.class);
68         when(mMediaRouter.getRouteCount()).thenReturn(1);
69         when(mMediaRouter.getRouteAt(0)).thenReturn(route);
70         when(route.matchesTypes(anyInt())).thenReturn(true);
71         when(route.isSelected()).thenReturn(true);
72         when(route.isConnecting()).thenReturn(false);
73 
74         assertThat(mWifiDisplayPreferenceController.getSummary().toString()).contains(
75                 mContext.getString(R.string.wifi_display_status_connected));
76     }
77 }
78