2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.bluetooth;
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;
26 import android.content.Context;
27 import android.support.v7.preference.Preference;
28 import android.support.v7.preference.PreferenceScreen;
30 import com.android.settings.TestConfig;
31 import com.android.settings.testutils.SettingsRobolectricTestRunner;
32 import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
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;
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;
48 private Context mContext;
50 private LocalBluetoothAdapter mLocalAdapter;
52 private PreferenceScreen mPreferenceScreen;
53 private Preference mPreference;
55 private BluetoothDeviceNamePreferenceController mController;
59 MockitoAnnotations.initMocks(this);
61 mContext = spy(RuntimeEnvironment.application);
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);
71 public void testUpdateDeviceName_showSummaryWithDeviceName() {
72 mController.updateDeviceName(mPreference, DEVICE_NAME);
74 final CharSequence summary = mPreference.getSummary();
75 assertThat(summary.toString())
76 .isEqualTo("Visible as 'Nightshade' to other devices");
77 assertThat(mPreference.isSelectable()).isFalse();
81 public void testCreateBluetoothDeviceNamePreference() {
82 Preference preference = mController.createBluetoothDeviceNamePreference(mPreferenceScreen,
85 assertThat(preference.getKey()).isEqualTo(mController.KEY_DEVICE_NAME);
86 assertThat(preference.getOrder()).isEqualTo(ORDER);
87 verify(mPreferenceScreen).addPreference(preference);
91 public void testOnStart_receiverRegistered() {
92 mController.onStart();
93 verify(mContext).registerReceiver(eq(mController.mReceiver), any());
97 public void testOnStop_receiverUnregistered() {
99 mContext.registerReceiver(mController.mReceiver, null);
101 mController.onStop();
102 verify(mContext).unregisterReceiver(mController.mReceiver);