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.content.Context;
21import android.net.ConnectivityManager;
22import android.net.IConnectivityManager;
23import android.net.NetworkRequest;
24import android.os.IBinder;
25import android.support.v7.preference.Preference;
26import android.support.v7.preference.PreferenceScreen;
27
28import com.android.settings.testutils.SettingsRobolectricTestRunner;
29import com.android.settings.TestConfig;
30import com.android.settingslib.core.lifecycle.Lifecycle;
31
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.mockito.Mock;
36import org.mockito.MockitoAnnotations;
37import org.robolectric.annotation.Config;
38import org.robolectric.shadows.ShadowServiceManager;
39
40import static org.mockito.Matchers.any;
41import static org.mockito.Matchers.anyString;
42import static org.mockito.Mockito.doReturn;
43import static org.mockito.Mockito.spy;
44import static org.mockito.Mockito.verify;
45import static org.mockito.Mockito.when;
46
47@RunWith(SettingsRobolectricTestRunner.class)
48@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
49public class VpnPreferenceControllerTest {
50
51    @Mock
52    private Context mContext;
53    @Mock
54    private ConnectivityManager mConnectivityManager;
55    @Mock
56    private IBinder mBinder;
57    @Mock
58    private IConnectivityManager mConnectivityManagerService;
59    @Mock
60    private PreferenceScreen mScreen;
61    @Mock
62    private Preference mPreference;
63    private VpnPreferenceController mController;
64    private Lifecycle mLifecycle;
65
66    @Before
67    public void setUp() {
68        MockitoAnnotations.initMocks(this);
69        when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
70                .thenReturn(mConnectivityManager);
71        when(mBinder.queryLocalInterface("android.net.IConnectivityManager"))
72                .thenReturn(mConnectivityManagerService);
73        ShadowServiceManager.addService(Context.CONNECTIVITY_SERVICE, mBinder);
74        when(mScreen.findPreference(anyString())).thenReturn(mPreference);
75
76        mController = spy(new VpnPreferenceController(mContext));
77        mLifecycle = new Lifecycle();
78        mLifecycle.addObserver(mController);
79    }
80
81    @Test
82    public void displayPreference_available_shouldSetDependency() {
83
84        doReturn(true).when(mController).isAvailable();
85        mController.displayPreference(mScreen);
86
87        verify(mPreference).setDependency(AirplaneModePreferenceController.KEY_TOGGLE_AIRPLANE);
88    }
89
90    @Test
91    public void goThroughLifecycle_shouldRegisterUnregisterListener() {
92        doReturn(true).when(mController).isAvailable();
93
94        mLifecycle.onResume();
95        verify(mConnectivityManager).registerNetworkCallback(
96                any(NetworkRequest.class), any(ConnectivityManager.NetworkCallback.class));
97
98        mLifecycle.onPause();
99        verify(mConnectivityManager).unregisterNetworkCallback(
100                any(ConnectivityManager.NetworkCallback.class));
101    }
102
103}
104