1 /*
2  * Copyright (C) 2020 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.development;
18 
19 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
20 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 
30 import android.content.Context;
31 import android.content.Intent;
32 import android.debug.IAdbManager;
33 import android.os.RemoteException;
34 
35 import androidx.preference.Preference;
36 import androidx.preference.PreferenceScreen;
37 
38 import com.android.settings.testutils.shadow.ShadowUtils;
39 
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.robolectric.RobolectricTestRunner;
46 import org.robolectric.RuntimeEnvironment;
47 import org.robolectric.annotation.Config;
48 import org.robolectric.util.ReflectionHelpers;
49 
50 @RunWith(RobolectricTestRunner.class)
51 @Config(shadows = ShadowUtils.class)
52 public class AdbQrCodePreferenceControllerTest {
53     @Mock
54     private PreferenceScreen mScreen;
55     @Mock
56     private Preference mPreference;
57     @Mock
58     private IAdbManager mAdbManager;
59     @Mock
60     private WirelessDebuggingFragment mFragment;
61 
62     private AdbQrCodePreferenceController mController;
63     private Context mContext;
64 
65     @Before
setUp()66     public void setUp() {
67         MockitoAnnotations.initMocks(this);
68         mContext = RuntimeEnvironment.application;
69         mController = spy(new AdbQrCodePreferenceController(mContext, "test_key"));
70         mController.setParentFragment(mFragment);
71         ReflectionHelpers.setField(mController, "mAdbManager", mAdbManager);
72         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
73         when(mPreference.getContext()).thenReturn(mContext);
74     }
75 
76     @Test
getAvailabilityStatus_isAdbWifiQrSupported_yes_shouldBeTrue()77     public void getAvailabilityStatus_isAdbWifiQrSupported_yes_shouldBeTrue()
78             throws RemoteException {
79         when(mAdbManager.isAdbWifiQrSupported()).thenReturn(true);
80 
81         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
82     }
83 
84     @Test
getAvailabilityStatus_isAdbWifiQrSupported_no_shouldBeFalse()85     public void getAvailabilityStatus_isAdbWifiQrSupported_no_shouldBeFalse()
86             throws RemoteException {
87         when(mAdbManager.isAdbWifiQrSupported()).thenReturn(false);
88 
89         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
90     }
91 
92     @Test
displayPreference_isAdbWifiQrSupported_yes_prefIsVisible()93     public void displayPreference_isAdbWifiQrSupported_yes_prefIsVisible() throws RemoteException {
94         when(mAdbManager.isAdbWifiQrSupported()).thenReturn(true);
95 
96         mController.displayPreference(mScreen);
97         verify(mPreference).setVisible(true);
98     }
99 
100     @Test
displayPreference_isAdbWifiQrSupported_no_prefIsNotVisible()101     public void displayPreference_isAdbWifiQrSupported_no_prefIsNotVisible()
102             throws RemoteException {
103         when(mAdbManager.isAdbWifiQrSupported()).thenReturn(false);
104 
105         mController.displayPreference(mScreen);
106         verify(mPreference).setVisible(false);
107     }
108 
109     @Test
handlePreferenceTreeClick_launchActivity()110     public void handlePreferenceTreeClick_launchActivity() {
111         final String preferenceKey = mController.getPreferenceKey();
112         when(mPreference.getKey()).thenReturn(preferenceKey);
113         mController.handlePreferenceTreeClick(mPreference);
114 
115         verify(mFragment).startActivityForResult(any(Intent.class),
116                 eq(WirelessDebuggingFragment.PAIRING_DEVICE_REQUEST));
117     }
118 }
119