Skip to content

Commit

Permalink
#9 Never update existing network but rather remove it and add
Browse files Browse the repository at this point in the history
  • Loading branch information
Kosta Zaikin committed Nov 11, 2016
1 parent c1601ad commit 7354eb1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private Task<Params> enableWifi(final Params params) {
*/
@Nonnull
private Continuation<Params, Integer> getNetworkId() {
return task -> mNetworkManager.getNetworkId(task.getResult());
return task -> mNetworkManager.createNetwork(task.getResult());
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class NetworkManager {
* @return NetworkId of a network.
* @throws Exception if network could not be set up.
*/
int getNetworkId(Params params) throws Exception {
int createNetwork(Params params) throws Exception {
Log.d(Tag.NAME, "Get connected network id...");
int connectedNetId = getConnectedNetworkWithSsid(params.quotedSsid);
if (connectedNetId != NO_ID) {
Expand All @@ -43,20 +43,16 @@ int getNetworkId(Params params) throws Exception {
int netId = getConfiguredNetworkId(params);
if (netId == NO_ID) {
Log.d(Tag.NAME, "Configured network not found");
return addNetwork(params);
}

Log.d(Tag.NAME, "Configured network found. It is not connected");
if (updateNetworkConfiguration(params, netId)) {
Log.d(Tag.NAME, "Network configuration updated");
return netId;
} else {
Log.d(Tag.NAME, "Network configuration update failed. Removing configured network");
if (mWifiManager.removeNetwork(netId)) {
return addNetwork(params);
// updated network configuration are not able to connect on some devices.
// removing network and adding it again is quite fast and is durable enough
Log.d(Tag.NAME, "Configured network found. It is not connected");
Log.d(Tag.NAME, "Removing configured network");
if (!mWifiManager.removeNetwork(netId)) {
throw new IllegalStateException("Unable to remove existing network");
}
throw new IllegalStateException("Unable to remove existing network");
}
return addNetwork(params);
}

private int getConnectedNetworkWithSsid(String maskedSsid) {
Expand All @@ -79,7 +75,7 @@ private int getConfiguredNetworkId(Params params) {
}

private int addNetwork(Params params) {
Log.d(Tag.NAME, "Add network");
Log.d(Tag.NAME, "Adding network");

WifiConfiguration wfc = WifiConfigurationBuilder.create(params);
int result = mWifiManager.addNetwork(wfc);
Expand All @@ -89,12 +85,4 @@ private int addNetwork(Params params) {
Log.d(Tag.NAME, "Network added");
return result;
}

private boolean updateNetworkConfiguration(Params params, int netId) {
Log.d(Tag.NAME, "Updating network configuration");
WifiConfiguration config = WifiConfigurationBuilder.create(params);
config.networkId = netId;
return mWifiManager.updateNetwork(config) != NO_ID;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import javax.annotation.Nonnull;

import bolts.CancellationToken;
import bolts.Continuation;
import bolts.Task;
import ru.yandex.qatools.wifitool.utils.ConnectivityChecker;

Expand Down Expand Up @@ -63,7 +62,7 @@ public void enabledWifi_Succeeds() throws Exception {
doReturn(WifiManager.WIFI_STATE_ENABLED).when(mData.wifiManager).getWifiState();

doReturn(true).when(mConnectivityChecker).isWifiNetworkConnected(NET_ID);
doReturn(NET_ID).when(mNetworkManager).getNetworkId(any(Params.class));
doReturn(NET_ID).when(mNetworkManager).createNetwork(any(Params.class));
doReturn(Task.forResult(null)).when(mConnectivityMonitor)
.wait(anyInt(), any(CancellationToken.class));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import javax.annotation.Nonnull;

import static junit.framework.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
Expand All @@ -18,8 +20,9 @@
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class NetworkManagerTest {
@Nonnull
private final TestData mData;

@Nonnull
private NetworkManager mNetworkManager;

public NetworkManagerTest() {
Expand All @@ -32,37 +35,49 @@ public NetworkManagerTest() {
public void networkIsConfiguredAndConnected_ReturnsId() throws Exception {
mData.whenNetworkIsConnected();

assertEquals(NET_ID, mNetworkManager.getNetworkId(UNSECURE_PARAMS));
assertEquals(NET_ID, mNetworkManager.createNetwork(UNSECURE_PARAMS));
}

@Test
public void networkIsConfiguredAndNotConnected_CanUpdate_ReturnsId() throws Exception {
public void networkIsConfiguredAndNotConnected_CanBeRemoved_ReturnsId() throws Exception {
mData.whenNetworkIsConfigured();
doReturn(NET_ID).when(mData.wifiManager).updateNetwork(any(WifiConfiguration.class));
whenNetworkCanBeRemoved();
whenNetworkCanBeAdded();

assertEquals(NET_ID, mNetworkManager.getNetworkId(UNSECURE_PARAMS));
assertEquals(NET_ID, mNetworkManager.createNetwork(UNSECURE_PARAMS));
}

@Test(expected = Exception.class)
public void networkIsConfiguredAndNotConnected_CantUpdate_Throws() throws Exception {
mData.whenNetworkIsConfigured();
doReturn(NO_ID).when(mData.wifiManager).updateNetwork(any(WifiConfiguration.class));

assertEquals(NET_ID, mNetworkManager.getNetworkId(UNSECURE_PARAMS));
assertEquals(NET_ID, mNetworkManager.createNetwork(UNSECURE_PARAMS));
}

@Test
public void networkIsNotConfigured_CanAdd_ReturnsId() throws Exception {
doReturn(NET_ID).when(mData.wifiManager).addNetwork(any(WifiConfiguration.class));
whenNetworkCanBeAdded();

assertEquals(NET_ID, mNetworkManager.getNetworkId(UNSECURE_PARAMS));
assertEquals(NET_ID, mNetworkManager.createNetwork(UNSECURE_PARAMS));
}

@Test(expected = Exception.class)
public void networkIsNotConfigured_CantAdd_Throws() throws Exception {
doReturn(NO_ID).when(mData.wifiManager).addNetwork(any(WifiConfiguration.class));
whenNetworkCanNotBeAdded();

mNetworkManager.createNetwork(UNSECURE_PARAMS);
}

mNetworkManager.getNetworkId(UNSECURE_PARAMS);
private void whenNetworkCanBeRemoved() {
doReturn(true).when(mData.wifiManager).removeNetwork(NET_ID);
}

private void whenNetworkCanBeAdded() {
doReturn(NET_ID).when(mData.wifiManager).addNetwork(any(WifiConfiguration.class));
}

private void whenNetworkCanNotBeAdded() {
doReturn(NO_ID).when(mData.wifiManager).addNetwork(any(WifiConfiguration.class));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ class TestData {
private static final int NO_RETRIES = 0;
private static final int NO_DELAY = 0;

@Nonnull
static final Params UNSECURE_PARAMS =
new Params(SOME_SSID, NO_PASS, NO_SECURITY, NO_RETRIES, NO_DELAY);

@Mock
@Nonnull
WifiManager wifiManager;

@Mock
Expand Down

0 comments on commit 7354eb1

Please sign in to comment.