package com.mumux.androidtesting.actions;
import com.mumux.androidtesting.actions.impl.ActionUtils;
import com.mumux.androidtesting.actions.impl.WifiAction;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.IOException;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ActionUtils.class})
public class WifiActionTest {
@Test
public void testWifiSuccess() throws IOException {
Action action = new WifiAction();
action.setValues(new Object[]{true});
Runtime runtime = mock(Runtime.class);
mockStatic(ActionUtils.class);
when(ActionUtils.readFile("/sys/class/net/wlan0/operstate"))
.thenReturn("down")
.thenReturn("down")
.thenReturn("down")
.thenReturn("up");
long start = System.currentTimeMillis();
Assert.assertNull(action.run(null, runtime));
long end = System.currentTimeMillis();
verify(runtime, times(1)).exec("su -c svc wifi enable");
//System.err.println("duration=" + (end - start));
Assert.assertTrue((end - start) >= 200);
}
@Test
public void testWifiFailure() throws IOException {
Action action = new WifiAction();
action.setValues(new Object[]{true});
Runtime runtime = mock(Runtime.class);
mockStatic(ActionUtils.class);
when(ActionUtils.readFile("/sys/class/net/wlan0/operstate"))
.thenReturn("down");
long start = System.currentTimeMillis();
Assert.assertEquals("Wifi setting not updated", action.run(null, runtime));
long end = System.currentTimeMillis();
verify(runtime, times(1)).exec("su -c svc wifi enable");
Assert.assertFalse((end - start) > 3200);
}
@Test
public void testWifiAlreadyOn() throws IOException {
Action action = new WifiAction();
action.setValues(new Object[]{true});
mockStatic(ActionUtils.class);
when(ActionUtils.readFile("/sys/class/net/wlan0/operstate"))
.thenReturn("up");
long start = System.currentTimeMillis();
Assert.assertNull(action.run(null, null));
long end = System.currentTimeMillis();
Assert.assertTrue((end - start) < 100);
}
}