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
17package com.android.settings.network;
18
19
20import android.bluetooth.BluetoothAdapter;
21import android.bluetooth.BluetoothPan;
22import android.bluetooth.BluetoothProfile;
23import android.content.ContentResolver;
24import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
27import android.database.ContentObserver;
28import android.net.ConnectivityManager;
29import android.provider.Settings;
30import android.support.v7.preference.Preference;
31
32import com.android.settings.R;
33import com.android.settings.SettingsRobolectricTestRunner;
34import com.android.settings.TestConfig;
35
36import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39import org.mockito.Mock;
40import org.mockito.MockitoAnnotations;
41import org.robolectric.RuntimeEnvironment;
42import org.robolectric.annotation.Config;
43import org.robolectric.util.ReflectionHelpers;
44
45import java.util.concurrent.atomic.AtomicReference;
46
47import static org.mockito.Matchers.any;
48import static org.mockito.Mockito.mock;
49import static org.mockito.Mockito.spy;
50import static org.mockito.Mockito.verify;
51import static org.mockito.Mockito.verifyNoMoreInteractions;
52import static org.mockito.Mockito.verifyZeroInteractions;
53import static org.mockito.Mockito.when;
54
55@RunWith(SettingsRobolectricTestRunner.class)
56@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
57public class TetherPreferenceControllerTest {
58
59    @Mock
60    private Context mContext;
61    @Mock
62    private ConnectivityManager mConnectivityManager;
63    @Mock
64    private BluetoothAdapter mBluetoothAdapter;
65    @Mock
66    private Preference mPreference;
67
68    private TetherPreferenceController mController;
69
70    @Before
71    public void setUp() {
72        MockitoAnnotations.initMocks(this);
73        mController = spy(TetherPreferenceController.class);
74        ReflectionHelpers.setField(mController, "mContext", mContext);
75        ReflectionHelpers.setField(mController, "mConnectivityManager", mConnectivityManager);
76        ReflectionHelpers.setField(mController, "mBluetoothAdapter", mBluetoothAdapter);
77        ReflectionHelpers.setField(mController, "mPreference", mPreference);
78    }
79
80    @Test
81    public void goThroughLifecycle_shouldDestoryBluetoothProfile() {
82        final BluetoothPan pan = mock(BluetoothPan.class);
83        final AtomicReference<BluetoothPan> panRef =
84                ReflectionHelpers.getField(mController, "mBluetoothPan");
85        panRef.set(pan);
86
87        mController.onDestroy();
88
89        verify(mBluetoothAdapter).closeProfileProxy(BluetoothProfile.PAN, pan);
90    }
91
92    @Test
93    public void updateSummary_noPreference_noInteractionWithConnectivityManager() {
94        ReflectionHelpers.setField(mController, "mPreference", null);
95        mController.updateSummary();
96        verifyNoMoreInteractions(mConnectivityManager);
97    }
98
99    @Test
100    public void updateSummary_wifiTethered_shouldShowHotspotMessage() {
101        when(mConnectivityManager.getTetheredIfaces()).thenReturn(new String[]{"123"});
102        when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"123"});
103
104        mController.updateSummary();
105        verify(mPreference).setSummary(R.string.tether_settings_summary_hotspot_on_tether_off);
106    }
107
108    @Test
109    public void updateSummary_btThetherOn_shouldShowTetherMessage() {
110        when(mConnectivityManager.getTetheredIfaces()).thenReturn(new String[]{"123"});
111        when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[]{"123"});
112
113        mController.updateSummary();
114        verify(mPreference).setSummary(R.string.tether_settings_summary_hotspot_off_tether_on);
115    }
116
117    @Test
118    public void updateSummary_tetherOff_shouldShowTetherOffMessage() {
119        when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[]{"123"});
120        when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"456"});
121
122        mController.updateSummary();
123        verify(mPreference).setSummary(R.string.switch_off_text);
124    }
125
126    @Test
127    public void updateSummary_wifiBtTetherOn_shouldShowHotspotAndTetherMessage() {
128        when(mConnectivityManager.getTetheredIfaces()).thenReturn(new String[]{"123", "456"});
129        when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"456"});
130        when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[]{"23"});
131
132        mController.updateSummary();
133        verify(mPreference).setSummary(R.string.tether_settings_summary_hotspot_on_tether_on);
134    }
135
136    @Test
137    public void airplaneModeOn_shouldUpdateSummaryToOff() {
138        ReflectionHelpers.setField(mController, "mContext", RuntimeEnvironment.application);
139
140        Settings.Global.putInt(RuntimeEnvironment.application.getContentResolver(),
141                Settings.Global.AIRPLANE_MODE_ON, 0);
142
143        mController.onResume();
144
145        verifyZeroInteractions(mPreference);
146
147        Settings.Global.putInt(RuntimeEnvironment.application.getContentResolver(),
148                Settings.Global.AIRPLANE_MODE_ON, 1);
149
150        final ContentObserver observer = ReflectionHelpers.getField(mController,
151                "mAirplaneModeObserver");
152        observer.onChange(true, Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON));
153
154        verify(mPreference).setSummary(R.string.switch_off_text);
155    }
156
157    @Test
158    public void onResume_shouldRegisterTetherReceiver() {
159        when(mContext.getContentResolver()).thenReturn(mock(ContentResolver.class));
160
161        mController.onResume();
162
163        verify(mContext).registerReceiver(
164            any(TetherPreferenceController.TetherBroadcastReceiver.class), any(IntentFilter.class));
165    }
166
167    @Test
168    public void onPause_shouldUnregisterTetherReceiver() {
169        when(mContext.getContentResolver()).thenReturn(mock(ContentResolver.class));
170        mController.onResume();
171
172        mController.onPause();
173
174        verify(mContext).unregisterReceiver(
175            any(TetherPreferenceController.TetherBroadcastReceiver.class));
176    }
177
178    @Test
179    public void tetherStatesChanged_shouldUpdateSummary() {
180        final Context context = RuntimeEnvironment.application;
181        ReflectionHelpers.setField(mController, "mContext", context);
182        mController.onResume();
183
184        context.sendBroadcast(new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED));
185
186        verify(mController).updateSummary();
187    }
188
189}
190