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