• 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 
26 import android.content.Context;
27 import android.support.v7.preference.Preference;
28 import android.support.v7.preference.PreferenceScreen;
29 
30 import com.android.settings.TestConfig;
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 import org.robolectric.annotation.Config;
41 
42 @RunWith(SettingsRobolectricTestRunner.class)
43 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
44 public class BluetoothDeviceNamePreferenceControllerTest {
45     private static final String DEVICE_NAME = "Nightshade";
46     private static final int ORDER = 1;
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         doReturn(mContext).when(mPreferenceScreen).getContext();
64         mPreference = new Preference(mContext);
65         mPreference.setKey(BluetoothDeviceNamePreferenceController.KEY_DEVICE_NAME);
66         mController = new BluetoothDeviceNamePreferenceController(
67                 mContext, mLocalAdapter);
68     }
69 
70     @Test
testUpdateDeviceName_showSummaryWithDeviceName()71     public void testUpdateDeviceName_showSummaryWithDeviceName() {
72         mController.updateDeviceName(mPreference, DEVICE_NAME);
73 
74         final CharSequence summary = mPreference.getSummary();
75 
76         assertThat(summary.toString())
77                 .isEqualTo("Visible as \u201CNightshade\u201D to other devices");
78         assertThat(mPreference.isSelectable()).isFalse();
79     }
80 
81     @Test
testCreateBluetoothDeviceNamePreference()82     public void testCreateBluetoothDeviceNamePreference() {
83         Preference preference = mController.createBluetoothDeviceNamePreference(mPreferenceScreen,
84                 ORDER);
85 
86         assertThat(preference.getKey()).isEqualTo(mController.KEY_DEVICE_NAME);
87         assertThat(preference.getOrder()).isEqualTo(ORDER);
88         verify(mPreferenceScreen).addPreference(preference);
89     }
90 
91     @Test
testOnStart_receiverRegistered()92     public void testOnStart_receiverRegistered() {
93         mController.onStart();
94         verify(mContext).registerReceiver(eq(mController.mReceiver), any());
95     }
96 
97     @Test
testOnStop_receiverUnregistered()98     public void testOnStop_receiverUnregistered() {
99         // register it first
100         mContext.registerReceiver(mController.mReceiver, null);
101 
102         mController.onStop();
103         verify(mContext).unregisterReceiver(mController.mReceiver);
104     }
105 }
106