aboutsummaryrefslogtreecommitdiff
path: root/v1/src/test/java/com/xtremelabs/robolectric/shadows/WifiManagerTest.java
blob: b9e86745497c227ca427411f56210adc9b11dc17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.xtremelabs.robolectric.shadows;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Pair;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

import static android.content.Context.WIFI_SERVICE;
import static com.xtremelabs.robolectric.Robolectric.application;
import static com.xtremelabs.robolectric.Robolectric.shadowOf;
import static junit.framework.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

@RunWith(WithTestDefaultsRunner.class)
public class WifiManagerTest {

    private WifiManager wifiManager;
    private ShadowWifiManager shadowWifiManager;

    @Before
    public void setUp() throws Exception {
        wifiManager = (WifiManager) application.getSystemService(WIFI_SERVICE);
        shadowWifiManager = shadowOf(wifiManager);
    }

    @Test
    public void shouldReturnWifiInfo() {
        assertEquals(wifiManager.getConnectionInfo().getClass(), WifiInfo.class);
    }

    @Test(expected = SecurityException.class)
    public void setWifiEnabled_shouldThrowSecurityExceptionWhenAccessWifiStatePermissionNotGranted() throws Exception {
        shadowWifiManager.setAccessWifiStatePermission(false);
        wifiManager.setWifiEnabled(true);
    }

    @Test(expected = SecurityException.class)
    public void isWifiEnabled_shouldThrowSecurityExceptionWhenAccessWifiStatePermissionNotGranted() throws Exception {
        shadowWifiManager.setAccessWifiStatePermission(false);
        wifiManager.isWifiEnabled();
    }

    @Test(expected = SecurityException.class)
    public void getConnectionInfo_shouldThrowSecurityExceptionWhenAccessWifiStatePermissionNotGranted() throws Exception {
        shadowWifiManager.setAccessWifiStatePermission(false);
        wifiManager.getConnectionInfo();
    }

    @Test
    public void shouldEnableNetworks() throws Exception {
        int networkId = 666;
        wifiManager.enableNetwork(networkId, true);
        Pair<Integer, Boolean> lastEnabled = shadowWifiManager.getLastEnabledNetwork();
        assertThat(lastEnabled, equalTo(new Pair<Integer, Boolean>(666, true)));

        int anotherNetworkId = 777;
        wifiManager.enableNetwork(anotherNetworkId, false);
        lastEnabled = shadowWifiManager.getLastEnabledNetwork();
        assertThat(lastEnabled, equalTo(new Pair<Integer, Boolean>(777, false)));
    }

    @Test
    public void shouldReturnSetScanResults() throws Exception {
        List<ScanResult> scanResults = new ArrayList<ScanResult>();
        shadowWifiManager.setScanResults(scanResults);
        assertThat(wifiManager.getScanResults(), sameInstance(scanResults));
    }

    @Test
    public void shouldRecordTheLastAddedNetwork() throws Exception {
        WifiConfiguration wifiConfiguration = new WifiConfiguration();
        wifiConfiguration.networkId = -1;
        int networkId = wifiManager.addNetwork(wifiConfiguration);
        assertThat(networkId, equalTo(0));
        assertThat(wifiManager.getConfiguredNetworks().get(0), not(sameInstance(wifiConfiguration)));
        assertThat(wifiConfiguration.networkId, equalTo(-1));
        assertThat(wifiManager.getConfiguredNetworks().get(0).networkId, equalTo(0));

        WifiConfiguration anotherConfig = new WifiConfiguration();
        assertThat(wifiManager.addNetwork(anotherConfig), equalTo(1));
        assertThat(anotherConfig.networkId, equalTo(-1));
        assertThat(wifiManager.getConfiguredNetworks().get(1).networkId, equalTo(1));
    }

    @Test
    public void updateNetwork_shouldReplaceNetworks() throws Exception {
        WifiConfiguration wifiConfiguration = new WifiConfiguration();
        wifiConfiguration.networkId = -1;
        wifiManager.addNetwork(wifiConfiguration);

        WifiConfiguration anotherConfig = new WifiConfiguration();
        int networkId = wifiManager.addNetwork(anotherConfig);

        assertThat(networkId, equalTo(1));
        WifiConfiguration configuration = new WifiConfiguration();
        configuration.networkId = networkId;
        configuration.priority = 44;

        assertThat(wifiManager.updateNetwork(configuration), equalTo(networkId));
        List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
        assertThat(configuredNetworks.size(), equalTo(2));
        assertThat(configuration.priority, equalTo(44));
        assertThat(configuredNetworks.get(1).priority, equalTo(44));
    }

    @Test
    public void updateNetwork_shouldRejectNullandNewConfigs() throws Exception {
        WifiConfiguration config = new WifiConfiguration();
        config.networkId = -1;
        assertThat(wifiManager.updateNetwork(config), equalTo(-1));
        assertThat(wifiManager.updateNetwork(null), equalTo(-1));
        assertTrue(wifiManager.getConfiguredNetworks().isEmpty());
    }

    @Test
    public void shouldSaveConfigurations() throws Exception {
        shadowWifiManager.wasSaved = false;
        assertThat(wifiManager.saveConfiguration(), equalTo(true));
        assertThat(shadowWifiManager.wasSaved, equalTo(true));
    }

    @Test
    public void shouldCreateWifiLock() throws Exception {
        assertNotNull(wifiManager.createWifiLock("TAG"));
        assertNotNull(wifiManager.createWifiLock(1, "TAG"));
    }

    @Test
    public void shouldAcquireAndReleaseWifilockRefCounted() throws Exception {
        WifiManager.WifiLock lock = wifiManager.createWifiLock("TAG");
        lock.acquire();
        lock.acquire();
        assertTrue(lock.isHeld());
        lock.release();
        assertTrue(lock.isHeld());
        lock.release();
        assertFalse(lock.isHeld());
    }

    @Test
    public void shouldAcquireAndReleaseWifilockNonRefCounted() throws Exception {
        WifiManager.WifiLock lock = wifiManager.createWifiLock("TAG");
        lock.setReferenceCounted(false);
        lock.acquire();
        assertTrue(lock.isHeld());
        lock.acquire();
        assertTrue(lock.isHeld());
        lock.release();
        assertFalse(lock.isHeld());
    }

    @Test(expected = RuntimeException.class)
    public void shouldThrowRuntimeExceptionIfLockisUnderlocked() throws Exception {
        WifiManager.WifiLock lock = wifiManager.createWifiLock("TAG");
        lock.release();
    }

    @Test(expected = UnsupportedOperationException.class)
    public void shouldThrowUnsupportedOperationIfLockisOverlocked() throws Exception {
        WifiManager.WifiLock lock = wifiManager.createWifiLock("TAG");
        for (int i=0; i<ShadowWifiManager.ShadowWifiLock.MAX_ACTIVE_LOCKS; i++) lock.acquire();
    }
    
    @Test
    public void shouldCalculateSignalLevelSetBefore() {
        ShadowWifiManager.setSignalLevelInPercent(0.5f);
        assertEquals(2, WifiManager.calculateSignalLevel(0, 5));
        assertEquals(2, WifiManager.calculateSignalLevel(2, 5));

        ShadowWifiManager.setSignalLevelInPercent(0.9f);
        assertEquals(3, WifiManager.calculateSignalLevel(0, 5));
        assertEquals(3, WifiManager.calculateSignalLevel(2, 5));

        ShadowWifiManager.setSignalLevelInPercent(1f);
        assertEquals(3, WifiManager.calculateSignalLevel(0, 4));
        assertEquals(3, WifiManager.calculateSignalLevel(2, 4));

        ShadowWifiManager.setSignalLevelInPercent(0);
        assertEquals(0, WifiManager.calculateSignalLevel(0, 5));
        assertEquals(0, WifiManager.calculateSignalLevel(2, 5));
    }
    
    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowIllegalArgumentExceptionWhenSignalLevelToLow() {
    	ShadowWifiManager.setSignalLevelInPercent(-0.01f);
    }
    
    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowIllegalArgumentExceptionWhenSignalLevelToHigh() {
    	ShadowWifiManager.setSignalLevelInPercent(1.01f);
    }
}