• 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 
20 import static org.mockito.ArgumentMatchers.any;
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Mockito.verify;
23 
24 import android.bluetooth.BluetoothAdapter;
25 import android.content.Context;
26 import android.content.Intent;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.robolectric.RobolectricTestRunner;
34 
35 @RunWith(RobolectricTestRunner.class)
36 public class AlwaysDiscoverableTest {
37 
38     @Mock
39     private Context mContext;
40 
41     private AlwaysDiscoverable mAlwaysDiscoverable;
42     private BluetoothAdapter mBluetoothAdapter;
43 
44     @Before
setUp()45     public void setUp() {
46         MockitoAnnotations.initMocks(this);
47         mAlwaysDiscoverable = new AlwaysDiscoverable(mContext);
48         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
49     }
50 
51     @Test
isStartedWithoutStart()52     public void isStartedWithoutStart() {
53         assertThat(mAlwaysDiscoverable.mStarted).isFalse();
54     }
55 
56     @Test
isStartedWithStart()57     public void isStartedWithStart() {
58         mAlwaysDiscoverable.start();
59         assertThat(mAlwaysDiscoverable.mStarted).isTrue();
60     }
61 
62     @Test
isStartedWithStartStop()63     public void isStartedWithStartStop() {
64         mAlwaysDiscoverable.start();
65         mAlwaysDiscoverable.stop();
66         assertThat(mAlwaysDiscoverable.mStarted).isFalse();
67     }
68 
69     @Test
stopWithoutStart()70     public void stopWithoutStart() {
71         mAlwaysDiscoverable.stop();
72         // expect no crash
73         mBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
74     }
75 
76     @Test
startSetsModeAndRegistersReceiver()77     public void startSetsModeAndRegistersReceiver() {
78         mBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_NONE);
79         mAlwaysDiscoverable.start();
80         assertThat(mBluetoothAdapter.getScanMode())
81                 .isEqualTo(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
82         verify(mContext).registerReceiver(eq(mAlwaysDiscoverable), any());
83     }
84 
85     @Test
stopUnregistersReceiver()86     public void stopUnregistersReceiver() {
87         mAlwaysDiscoverable.start();
88         mAlwaysDiscoverable.stop();
89         verify(mContext).unregisterReceiver(mAlwaysDiscoverable);
90     }
91 
92     @Test
resetsToDiscoverableModeWhenScanModeChanges()93     public void resetsToDiscoverableModeWhenScanModeChanges() {
94         mAlwaysDiscoverable.start();
95         assertThat(mBluetoothAdapter.getScanMode())
96                 .isEqualTo(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
97 
98         sendScanModeChangedIntent(BluetoothAdapter.SCAN_MODE_CONNECTABLE,
99                 BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
100 
101         assertThat(mBluetoothAdapter.getScanMode())
102                 .isEqualTo(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
103     }
104 
sendScanModeChangedIntent(int newMode, int previousMode)105     private void sendScanModeChangedIntent(int newMode, int previousMode) {
106         mBluetoothAdapter.setScanMode(newMode);
107         Intent intent = new Intent(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
108         intent.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE, newMode);
109         intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_SCAN_MODE, previousMode);
110         mAlwaysDiscoverable.onReceive(mContext, intent);
111     }
112 }
113