1c86440538fb156718228299bd44698e24995632eAntony Sargent/*
2c86440538fb156718228299bd44698e24995632eAntony Sargent * Copyright (C) 2017 The Android Open Source Project
3c86440538fb156718228299bd44698e24995632eAntony Sargent *
4c86440538fb156718228299bd44698e24995632eAntony Sargent * Licensed under the Apache License, Version 2.0 (the "License");
5c86440538fb156718228299bd44698e24995632eAntony Sargent * you may not use this file except in compliance with the License.
6c86440538fb156718228299bd44698e24995632eAntony Sargent * You may obtain a copy of the License at
7c86440538fb156718228299bd44698e24995632eAntony Sargent *
8c86440538fb156718228299bd44698e24995632eAntony Sargent *      http://www.apache.org/licenses/LICENSE-2.0
9c86440538fb156718228299bd44698e24995632eAntony Sargent *
10c86440538fb156718228299bd44698e24995632eAntony Sargent * Unless required by applicable law or agreed to in writing, software
11c86440538fb156718228299bd44698e24995632eAntony Sargent * distributed under the License is distributed on an "AS IS" BASIS,
12c86440538fb156718228299bd44698e24995632eAntony Sargent * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c86440538fb156718228299bd44698e24995632eAntony Sargent * See the License for the specific language governing permissions and
14c86440538fb156718228299bd44698e24995632eAntony Sargent * limitations under the License.
15c86440538fb156718228299bd44698e24995632eAntony Sargent */
16c86440538fb156718228299bd44698e24995632eAntony Sargentpackage com.android.settings.bluetooth;
17c86440538fb156718228299bd44698e24995632eAntony Sargent
18c86440538fb156718228299bd44698e24995632eAntony Sargentimport static com.google.common.truth.Truth.assertThat;
19c86440538fb156718228299bd44698e24995632eAntony Sargent
20c86440538fb156718228299bd44698e24995632eAntony Sargentimport static org.mockito.Matchers.any;
21c86440538fb156718228299bd44698e24995632eAntony Sargentimport static org.mockito.Matchers.anyInt;
22c86440538fb156718228299bd44698e24995632eAntony Sargentimport static org.mockito.Matchers.eq;
23c86440538fb156718228299bd44698e24995632eAntony Sargentimport static org.mockito.Mockito.never;
24c86440538fb156718228299bd44698e24995632eAntony Sargentimport static org.mockito.Mockito.times;
25c86440538fb156718228299bd44698e24995632eAntony Sargentimport static org.mockito.Mockito.verify;
26c86440538fb156718228299bd44698e24995632eAntony Sargentimport static org.mockito.Mockito.when;
27c86440538fb156718228299bd44698e24995632eAntony Sargent
28c86440538fb156718228299bd44698e24995632eAntony Sargentimport android.bluetooth.BluetoothAdapter;
29c86440538fb156718228299bd44698e24995632eAntony Sargentimport android.content.Context;
30c86440538fb156718228299bd44698e24995632eAntony Sargentimport android.content.Intent;
31c86440538fb156718228299bd44698e24995632eAntony Sargent
32c86440538fb156718228299bd44698e24995632eAntony Sargentimport com.android.settings.TestConfig;
33c86440538fb156718228299bd44698e24995632eAntony Sargentimport com.android.settings.testutils.SettingsRobolectricTestRunner;
34c86440538fb156718228299bd44698e24995632eAntony Sargentimport com.android.settingslib.bluetooth.LocalBluetoothAdapter;
35c86440538fb156718228299bd44698e24995632eAntony Sargent
36c86440538fb156718228299bd44698e24995632eAntony Sargentimport org.junit.Before;
37c86440538fb156718228299bd44698e24995632eAntony Sargentimport org.junit.Test;
38c86440538fb156718228299bd44698e24995632eAntony Sargentimport org.junit.runner.RunWith;
39c86440538fb156718228299bd44698e24995632eAntony Sargentimport org.mockito.Mock;
40c86440538fb156718228299bd44698e24995632eAntony Sargentimport org.mockito.MockitoAnnotations;
41c86440538fb156718228299bd44698e24995632eAntony Sargentimport org.robolectric.annotation.Config;
42c86440538fb156718228299bd44698e24995632eAntony Sargent
43c86440538fb156718228299bd44698e24995632eAntony Sargent@RunWith(SettingsRobolectricTestRunner.class)
44c86440538fb156718228299bd44698e24995632eAntony Sargent@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
45c86440538fb156718228299bd44698e24995632eAntony Sargentpublic class AlwaysDiscoverableTest {
46c86440538fb156718228299bd44698e24995632eAntony Sargent    @Mock
47c86440538fb156718228299bd44698e24995632eAntony Sargent    private LocalBluetoothAdapter mLocalAdapter;
48c86440538fb156718228299bd44698e24995632eAntony Sargent
49c86440538fb156718228299bd44698e24995632eAntony Sargent    @Mock
50c86440538fb156718228299bd44698e24995632eAntony Sargent    private Context mContext;
51c86440538fb156718228299bd44698e24995632eAntony Sargent
52c86440538fb156718228299bd44698e24995632eAntony Sargent    private AlwaysDiscoverable mAlwaysDiscoverable;
53c86440538fb156718228299bd44698e24995632eAntony Sargent
54c86440538fb156718228299bd44698e24995632eAntony Sargent    @Before
55c86440538fb156718228299bd44698e24995632eAntony Sargent    public void setUp() {
56c86440538fb156718228299bd44698e24995632eAntony Sargent        MockitoAnnotations.initMocks(this);
57c86440538fb156718228299bd44698e24995632eAntony Sargent        mAlwaysDiscoverable = new AlwaysDiscoverable(mContext, mLocalAdapter);
58c86440538fb156718228299bd44698e24995632eAntony Sargent    }
59c86440538fb156718228299bd44698e24995632eAntony Sargent
60c86440538fb156718228299bd44698e24995632eAntony Sargent    @Test
61c86440538fb156718228299bd44698e24995632eAntony Sargent    public void isStartedWithoutStart() {
62c86440538fb156718228299bd44698e24995632eAntony Sargent        assertThat(mAlwaysDiscoverable.mStarted).isFalse();
63c86440538fb156718228299bd44698e24995632eAntony Sargent    }
64c86440538fb156718228299bd44698e24995632eAntony Sargent
65c86440538fb156718228299bd44698e24995632eAntony Sargent    @Test
66c86440538fb156718228299bd44698e24995632eAntony Sargent    public void isStartedWithStart() {
67c86440538fb156718228299bd44698e24995632eAntony Sargent        mAlwaysDiscoverable.start();
68c86440538fb156718228299bd44698e24995632eAntony Sargent        assertThat(mAlwaysDiscoverable.mStarted).isTrue();
69c86440538fb156718228299bd44698e24995632eAntony Sargent    }
70c86440538fb156718228299bd44698e24995632eAntony Sargent
71c86440538fb156718228299bd44698e24995632eAntony Sargent    @Test
72c86440538fb156718228299bd44698e24995632eAntony Sargent    public void isStartedWithStartStop() {
73c86440538fb156718228299bd44698e24995632eAntony Sargent        mAlwaysDiscoverable.start();
74c86440538fb156718228299bd44698e24995632eAntony Sargent        mAlwaysDiscoverable.stop();
75c86440538fb156718228299bd44698e24995632eAntony Sargent        assertThat(mAlwaysDiscoverable.mStarted).isFalse();
76c86440538fb156718228299bd44698e24995632eAntony Sargent    }
77c86440538fb156718228299bd44698e24995632eAntony Sargent
78c86440538fb156718228299bd44698e24995632eAntony Sargent    @Test
79c86440538fb156718228299bd44698e24995632eAntony Sargent    public void stopWithoutStart() {
80c86440538fb156718228299bd44698e24995632eAntony Sargent        mAlwaysDiscoverable.stop();
81c86440538fb156718228299bd44698e24995632eAntony Sargent        // expect no crash
82c86440538fb156718228299bd44698e24995632eAntony Sargent        verify(mLocalAdapter, never()).setScanMode(anyInt());
83c86440538fb156718228299bd44698e24995632eAntony Sargent    }
84c86440538fb156718228299bd44698e24995632eAntony Sargent
85c86440538fb156718228299bd44698e24995632eAntony Sargent    @Test
86c86440538fb156718228299bd44698e24995632eAntony Sargent    public void startSetsModeAndRegistersReceiver() {
87c86440538fb156718228299bd44698e24995632eAntony Sargent        when(mLocalAdapter.getScanMode()).thenReturn(BluetoothAdapter.SCAN_MODE_NONE);
88c86440538fb156718228299bd44698e24995632eAntony Sargent        mAlwaysDiscoverable.start();
89c86440538fb156718228299bd44698e24995632eAntony Sargent        verify(mLocalAdapter).setScanMode(eq(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE));
90c86440538fb156718228299bd44698e24995632eAntony Sargent        verify(mContext).registerReceiver(eq(mAlwaysDiscoverable), any());
91c86440538fb156718228299bd44698e24995632eAntony Sargent    }
92c86440538fb156718228299bd44698e24995632eAntony Sargent
93c86440538fb156718228299bd44698e24995632eAntony Sargent    @Test
94c86440538fb156718228299bd44698e24995632eAntony Sargent    public void stopUnregistersReceiver() {
95c86440538fb156718228299bd44698e24995632eAntony Sargent        mAlwaysDiscoverable.start();
96c86440538fb156718228299bd44698e24995632eAntony Sargent        mAlwaysDiscoverable.stop();
97c86440538fb156718228299bd44698e24995632eAntony Sargent        verify(mContext).unregisterReceiver(mAlwaysDiscoverable);
98c86440538fb156718228299bd44698e24995632eAntony Sargent    }
99c86440538fb156718228299bd44698e24995632eAntony Sargent
100c86440538fb156718228299bd44698e24995632eAntony Sargent    @Test
101c86440538fb156718228299bd44698e24995632eAntony Sargent    public void resetsToDiscoverableModeWhenScanModeChanges() {
102c86440538fb156718228299bd44698e24995632eAntony Sargent        mAlwaysDiscoverable.start();
103c86440538fb156718228299bd44698e24995632eAntony Sargent        verify(mLocalAdapter, times(1)).setScanMode(
104c86440538fb156718228299bd44698e24995632eAntony Sargent                BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
105c86440538fb156718228299bd44698e24995632eAntony Sargent
106c86440538fb156718228299bd44698e24995632eAntony Sargent        sendScanModeChangedIntent(BluetoothAdapter.SCAN_MODE_CONNECTABLE,
107c86440538fb156718228299bd44698e24995632eAntony Sargent                BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
108c86440538fb156718228299bd44698e24995632eAntony Sargent
109c86440538fb156718228299bd44698e24995632eAntony Sargent        verify(mLocalAdapter, times(2)).setScanMode(
110c86440538fb156718228299bd44698e24995632eAntony Sargent                BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
111c86440538fb156718228299bd44698e24995632eAntony Sargent    }
112c86440538fb156718228299bd44698e24995632eAntony Sargent
113c86440538fb156718228299bd44698e24995632eAntony Sargent    private void sendScanModeChangedIntent(int newMode, int previousMode) {
114c86440538fb156718228299bd44698e24995632eAntony Sargent        when(mLocalAdapter.getScanMode()).thenReturn(newMode);
115c86440538fb156718228299bd44698e24995632eAntony Sargent        Intent intent = new Intent(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
116c86440538fb156718228299bd44698e24995632eAntony Sargent        intent.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE, newMode);
117c86440538fb156718228299bd44698e24995632eAntony Sargent        intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_SCAN_MODE, previousMode);
118c86440538fb156718228299bd44698e24995632eAntony Sargent        mAlwaysDiscoverable.onReceive(mContext, intent);
119c86440538fb156718228299bd44698e24995632eAntony Sargent    }
120c86440538fb156718228299bd44698e24995632eAntony Sargent}
121