• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 package com.android.settings.connecteddevice;
17 
18 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
19 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static junit.framework.Assert.assertTrue;
24 
25 import static org.mockito.Mockito.when;
26 
27 import android.bluetooth.BluetoothAdapter;
28 import android.content.BroadcastReceiver;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.pm.PackageManager;
32 import android.support.v7.preference.Preference;
33 import android.support.v7.preference.PreferenceManager;
34 import android.support.v7.preference.PreferenceScreen;
35 import android.text.TextUtils;
36 
37 import com.android.settings.R;
38 import com.android.settings.testutils.SettingsRobolectricTestRunner;
39 import com.android.settingslib.RestrictedPreference;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.RuntimeEnvironment;
47 import org.robolectric.Shadows;
48 import org.robolectric.annotation.Config;
49 import org.robolectric.shadows.ShadowApplicationPackageManager;
50 import org.robolectric.util.ReflectionHelpers;
51 
52 @RunWith(SettingsRobolectricTestRunner.class)
53 @Config(shadows = ShadowApplicationPackageManager.class)
54 public class AddDevicePreferenceControllerTest {
55 
56     @Mock
57     private PreferenceScreen mScreen;
58     @Mock
59     private BluetoothAdapter mBluetoothAdapter;
60 
61     private Context mContext;
62     private AddDevicePreferenceController mAddDevicePreferenceController;
63     private RestrictedPreference mAddDevicePreference;
64     private ShadowApplicationPackageManager mPackageManager;
65 
66 
67     @Before
setUp()68     public void setUp() {
69         MockitoAnnotations.initMocks(this);
70 
71         mContext = RuntimeEnvironment.application;
72         mPackageManager = (ShadowApplicationPackageManager) Shadows.shadowOf(
73                 mContext.getPackageManager());
74         mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, true);
75 
76         mAddDevicePreferenceController = new AddDevicePreferenceController(mContext,
77                 "add_bt_devices");
78         ReflectionHelpers.setField(mAddDevicePreferenceController,
79                 "mBluetoothAdapter", mBluetoothAdapter);
80 
81         String key = mAddDevicePreferenceController.getPreferenceKey();
82         mAddDevicePreference = new RestrictedPreference(mContext);
83         mAddDevicePreference.setKey(key);
84         when(mScreen.findPreference(key)).thenReturn(mAddDevicePreference);
85         mAddDevicePreferenceController.displayPreference(mScreen);
86     }
87 
88     @Test
addDevice_bt_resume_on_then_off()89     public void addDevice_bt_resume_on_then_off() {
90         when(mBluetoothAdapter.isEnabled()).thenReturn(true);
91         mAddDevicePreferenceController.updateState();
92         assertTrue(TextUtils.isEmpty(mAddDevicePreference.getSummary()));
93 
94         Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
95         intent.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
96         BroadcastReceiver receiver = ReflectionHelpers.getField(
97                 mAddDevicePreferenceController, "mReceiver");
98         when(mBluetoothAdapter.isEnabled()).thenReturn(false);
99         receiver.onReceive(mContext, intent);
100         assertThat(mAddDevicePreference.getSummary()).isEqualTo(
101                 mContext.getString(R.string.connected_device_add_device_summary));
102     }
103 
104     @Test
addDevice_bt_resume_off_then_on()105     public void addDevice_bt_resume_off_then_on() {
106         when(mBluetoothAdapter.isEnabled()).thenReturn(false);
107         mAddDevicePreferenceController.updateState();
108         assertThat(mAddDevicePreference.getSummary()).isEqualTo(
109                 mContext.getString(R.string.connected_device_add_device_summary));
110 
111         Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
112         intent.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_ON);
113         BroadcastReceiver receiver = ReflectionHelpers.getField(
114                 mAddDevicePreferenceController, "mReceiver");
115         when(mBluetoothAdapter.isEnabled()).thenReturn(true);
116         receiver.onReceive(mContext, intent);
117         assertTrue(TextUtils.isEmpty(mAddDevicePreference.getSummary()));
118     }
119 
120     @Test
addDevice_Availability_UnSupported()121     public void addDevice_Availability_UnSupported() {
122         mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, false);
123         assertThat(mAddDevicePreferenceController.getAvailabilityStatus())
124                 .isEqualTo(UNSUPPORTED_ON_DEVICE);
125     }
126 
127     @Test
addDevice_Availability_Supported()128     public void addDevice_Availability_Supported() {
129         mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, true);
130         assertThat(mAddDevicePreferenceController.getAvailabilityStatus())
131                 .isEqualTo(AVAILABLE);
132     }
133 }
134