• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.bluetooth;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.ArgumentMatchers.eq;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.content.Context;
29 
30 import androidx.preference.Preference;
31 import androidx.preference.PreferenceScreen;
32 
33 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.RuntimeEnvironment;
42 import org.robolectric.annotation.Config;
43 
44 @RunWith(RobolectricTestRunner.class)
45 @Config(shadows = {ShadowBluetoothAdapter.class})
46 public class BluetoothDeviceNamePreferenceControllerTest {
47 
48     private static final String DEVICE_NAME = "Nightshade";
49     private static final int ORDER = 1;
50     private static final String KEY_DEVICE_NAME = "test_key_name";
51 
52     private Context mContext;
53     @Mock
54     private PreferenceScreen mPreferenceScreen;
55     private Preference mPreference;
56 
57     private BluetoothDeviceNamePreferenceController mController;
58 
59     @Before
setUp()60     public void setUp() {
61         MockitoAnnotations.initMocks(this);
62 
63         mContext = spy(RuntimeEnvironment.application);
64 
65         when(mPreferenceScreen.getContext()).thenReturn(mContext);
66         mPreference = new Preference(mContext);
67         mPreference.setKey(KEY_DEVICE_NAME);
68         mController = spy(new BluetoothDeviceNamePreferenceController(mContext, KEY_DEVICE_NAME));
69         doReturn(DEVICE_NAME).when(mController).getDeviceName();
70     }
71 
72     @Test
testUpdateDeviceName_showSummaryWithDeviceName()73     public void testUpdateDeviceName_showSummaryWithDeviceName() {
74         mController.updatePreferenceState(mPreference);
75 
76         final CharSequence summary = mPreference.getSummary();
77 
78         assertThat(summary.toString())
79                 .isEqualTo("Visible as \u201CNightshade\u201D to other devices");
80         assertThat(mPreference.isSelectable()).isFalse();
81     }
82 
83     @Test
testCreateBluetoothDeviceNamePreference()84     public void testCreateBluetoothDeviceNamePreference() {
85         Preference preference =
86             mController.createBluetoothDeviceNamePreference(mPreferenceScreen, ORDER);
87 
88         assertThat(preference.getKey()).isEqualTo(mController.getPreferenceKey());
89         assertThat(preference.getOrder()).isEqualTo(ORDER);
90         verify(mPreferenceScreen).addPreference(preference);
91     }
92 
93     @Test
testOnStart_receiverRegistered()94     public void testOnStart_receiverRegistered() {
95         mController.onStart();
96         verify(mContext).registerReceiver(eq(mController.mReceiver), any());
97     }
98 
99     @Test
testOnStop_receiverUnregistered()100     public void testOnStop_receiverUnregistered() {
101         // register it first
102         mContext.registerReceiver(mController.mReceiver, null);
103 
104         mController.onStop();
105         verify(mContext).unregisterReceiver(mController.mReceiver);
106     }
107 }
108