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.wifi;
18
19import static com.google.common.truth.Truth.assertThat;
20import static org.mockito.Matchers.any;
21import static org.mockito.Matchers.anyString;
22import static org.mockito.Mockito.spy;
23import static org.mockito.Mockito.verify;
24
25import android.content.BroadcastReceiver;
26import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.net.wifi.WifiManager;
30
31import com.android.settings.R;
32import com.android.settings.testutils.SettingsRobolectricTestRunner;
33import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener;
34import com.android.settingslib.wifi.WifiStatusTracker;
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;
42
43@RunWith(SettingsRobolectricTestRunner.class)
44public class WifiSummaryUpdaterTest {
45    @Mock private WifiStatusTracker mWifiTracker;
46    @Mock private SummaryListener mListener;
47
48    private Context mContext;
49    private WifiSummaryUpdater mSummaryUpdater;
50
51
52    @Before
53    public void setUp() {
54        MockitoAnnotations.initMocks(this);
55        mContext = spy(RuntimeEnvironment.application.getApplicationContext());
56        mSummaryUpdater = new WifiSummaryUpdater(mContext, mListener, mWifiTracker);
57    }
58
59    @Test
60    public void register_true_shouldRegisterListenerAndTracker() {
61        mSummaryUpdater.register(true);
62
63        verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
64        verify(mWifiTracker).setListening(true);
65    }
66
67    @Test
68    public void register_false_shouldUnregisterListenerAndTracker() {
69        mSummaryUpdater.register(true);
70        mSummaryUpdater.register(false);
71
72        verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
73        verify(mWifiTracker).setListening(false);
74    }
75
76    @Test
77    public void onReceive_networkStateChanged_shouldSendSummaryChange() {
78        mSummaryUpdater.register(true);
79        mContext.sendBroadcast(new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION));
80
81        verify(mListener).onSummaryChanged(anyString());
82    }
83
84    @Test
85    public void onReceive_rssiChanged_shouldSendSummaryChange() {
86        mSummaryUpdater.register(true);
87        mContext.sendBroadcast(new Intent(WifiManager.RSSI_CHANGED_ACTION));
88
89        verify(mListener).onSummaryChanged(anyString());
90    }
91
92    @Test
93    public void getSummary_wifiDisabled_shouldReturnDisabled() {
94        mWifiTracker.enabled = false;
95
96        assertThat(mSummaryUpdater.getSummary())
97            .isEqualTo(mContext.getString(R.string.switch_off_text));
98    }
99
100    @Test
101    public void getSummary_wifiDisconnected_shouldReturnDisconnected() {
102        mWifiTracker.enabled = true;
103        mWifiTracker.connected = false;
104
105        assertThat(mSummaryUpdater.getSummary())
106            .isEqualTo(mContext.getString(R.string.disconnected));
107    }
108
109    @Test
110    public void getSummary_wifiConnected_shouldReturnSsid() {
111        mWifiTracker.enabled = true;
112        mWifiTracker.connected = true;
113        mWifiTracker.ssid = "Test Ssid";
114
115        assertThat(mSummaryUpdater.getSummary()).isEqualTo("Test Ssid");
116    }
117
118    @Test
119    public void getSummary_wifiConnected_withSpeedLabel_shouldReturnSsid_withSpeedLabel() {
120        mWifiTracker.enabled = true;
121        mWifiTracker.connected = true;
122        mWifiTracker.ssid = "Test Ssid";
123        mWifiTracker.statusLabel = "Very Fast";
124
125        assertThat(mSummaryUpdater.getSummary()).isEqualTo("Test Ssid / Very Fast");
126    }
127
128    private class SummaryListener implements OnSummaryChangeListener {
129        String summary;
130
131        @Override
132        public void onSummaryChanged(String summary) {
133            this.summary = summary;
134        }
135    }
136}
137