• 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 package com.android.settings.bluetooth;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 import static org.mockito.Matchers.any;
20 import static org.mockito.Matchers.anyInt;
21 import static org.mockito.Matchers.eq;
22 import static org.mockito.Mockito.never;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.bluetooth.BluetoothAdapter;
28 import android.content.Context;
29 import android.content.Intent;
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 
40 @RunWith(SettingsRobolectricTestRunner.class)
41 public class AlwaysDiscoverableTest {
42 
43     @Mock
44     private LocalBluetoothAdapter mLocalAdapter;
45 
46     @Mock
47     private Context mContext;
48 
49     private AlwaysDiscoverable mAlwaysDiscoverable;
50 
51     @Before
setUp()52     public void setUp() {
53         MockitoAnnotations.initMocks(this);
54         mAlwaysDiscoverable = new AlwaysDiscoverable(mContext, mLocalAdapter);
55     }
56 
57     @Test
isStartedWithoutStart()58     public void isStartedWithoutStart() {
59         assertThat(mAlwaysDiscoverable.mStarted).isFalse();
60     }
61 
62     @Test
isStartedWithStart()63     public void isStartedWithStart() {
64         mAlwaysDiscoverable.start();
65         assertThat(mAlwaysDiscoverable.mStarted).isTrue();
66     }
67 
68     @Test
isStartedWithStartStop()69     public void isStartedWithStartStop() {
70         mAlwaysDiscoverable.start();
71         mAlwaysDiscoverable.stop();
72         assertThat(mAlwaysDiscoverable.mStarted).isFalse();
73     }
74 
75     @Test
stopWithoutStart()76     public void stopWithoutStart() {
77         mAlwaysDiscoverable.stop();
78         // expect no crash
79         verify(mLocalAdapter, never()).setScanMode(anyInt());
80     }
81 
82     @Test
startSetsModeAndRegistersReceiver()83     public void startSetsModeAndRegistersReceiver() {
84         when(mLocalAdapter.getScanMode()).thenReturn(BluetoothAdapter.SCAN_MODE_NONE);
85         mAlwaysDiscoverable.start();
86         verify(mLocalAdapter).setScanMode(eq(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE));
87         verify(mContext).registerReceiver(eq(mAlwaysDiscoverable), any());
88     }
89 
90     @Test
stopUnregistersReceiver()91     public void stopUnregistersReceiver() {
92         mAlwaysDiscoverable.start();
93         mAlwaysDiscoverable.stop();
94         verify(mContext).unregisterReceiver(mAlwaysDiscoverable);
95     }
96 
97     @Test
resetsToDiscoverableModeWhenScanModeChanges()98     public void resetsToDiscoverableModeWhenScanModeChanges() {
99         mAlwaysDiscoverable.start();
100         verify(mLocalAdapter, times(1))
101             .setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
102 
103         sendScanModeChangedIntent(BluetoothAdapter.SCAN_MODE_CONNECTABLE,
104                 BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
105 
106         verify(mLocalAdapter, times(2))
107             .setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
108     }
109 
sendScanModeChangedIntent(int newMode, int previousMode)110     private void sendScanModeChangedIntent(int newMode, int previousMode) {
111         when(mLocalAdapter.getScanMode()).thenReturn(newMode);
112         Intent intent = new Intent(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
113         intent.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE, newMode);
114         intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_SCAN_MODE, previousMode);
115         mAlwaysDiscoverable.onReceive(mContext, intent);
116     }
117 }
118