Use TestDeviceState checks instead of DeviceState
use the internal state maintained instead of the ddmlib
one before checking for properties.
Test: unit tests
Bug: 117626268
Change-Id: I49946eb12d33f6a8c08dae85fac0b658eda13232
diff --git a/src/com/android/tradefed/device/NativeDevice.java b/src/com/android/tradefed/device/NativeDevice.java
index 83af872..f6a15bf 100644
--- a/src/com/android/tradefed/device/NativeDevice.java
+++ b/src/com/android/tradefed/device/NativeDevice.java
@@ -19,7 +19,6 @@
import com.android.ddmlib.FileListingService;
import com.android.ddmlib.FileListingService.FileEntry;
import com.android.ddmlib.IDevice;
-import com.android.ddmlib.IDevice.DeviceState;
import com.android.ddmlib.IShellOutputReceiver;
import com.android.ddmlib.InstallException;
import com.android.ddmlib.Log.LogLevel;
@@ -428,7 +427,8 @@
if (getIDevice() instanceof StubDevice) {
return null;
}
- if (!DeviceState.ONLINE.equals(getIDevice().getState())) {
+ if (!TestDeviceState.ONLINE.equals(getDeviceState())) {
+ // Only query property for online device
CLog.d("Device %s is not online cannot get property %s.", getSerialNumber(), name);
return null;
}
@@ -4016,7 +4016,7 @@
*/
@Override
public String getMacAddress() {
- if (mIDevice instanceof StubDevice) {
+ if (getIDevice() instanceof StubDevice) {
// Do not query MAC addresses from stub devices.
return null;
}
diff --git a/tests/src/com/android/tradefed/device/NativeDeviceTest.java b/tests/src/com/android/tradefed/device/NativeDeviceTest.java
index 081021a..f0c55b8 100644
--- a/tests/src/com/android/tradefed/device/NativeDeviceTest.java
+++ b/tests/src/com/android/tradefed/device/NativeDeviceTest.java
@@ -27,7 +27,6 @@
import com.android.ddmlib.AdbCommandRejectedException;
import com.android.ddmlib.FileListingService.FileEntry;
import com.android.ddmlib.IDevice;
-import com.android.ddmlib.IDevice.DeviceState;
import com.android.ddmlib.IShellOutputReceiver;
import com.android.ddmlib.ShellCommandUnresponsiveException;
import com.android.ddmlib.SyncException;
@@ -2187,6 +2186,23 @@
receiver.addOutput(address.getBytes(), 0, address.length());
}
};
+ mTestDevice =
+ new TestableAndroidNativeDevice() {
+ @Override
+ public void recoverDevice() throws DeviceNotAvailableException {
+ // ignore
+ }
+
+ @Override
+ public IDevice getIDevice() {
+ return device;
+ }
+
+ @Override
+ IWifiHelper createWifiHelper() {
+ return mMockWifi;
+ }
+ };
mTestDevice.setIDevice(device);
assertNull(mTestDevice.getMacAddress());
}
@@ -2195,9 +2211,10 @@
@Test
public void testGetSimState_unavailableDevice() {
mMockIDevice = EasyMock.createMock(IDevice.class);
- EasyMock.expect(mMockIDevice.getState()).andReturn(DeviceState.UNAUTHORIZED);
- EasyMock.expect(mMockIDevice.getSerialNumber()).andReturn("serial");
+ mMockStateMonitor.setState(TestDeviceState.NOT_AVAILABLE);
+ EasyMock.expect(mMockIDevice.getSerialNumber()).andReturn("serial").times(2);
EasyMock.replay(mMockIDevice, mMockStateMonitor, mMockDvcMonitor);
+ mTestDevice.setDeviceState(TestDeviceState.NOT_AVAILABLE);
assertNull(mTestDevice.getSimState());
EasyMock.verify(mMockIDevice, mMockStateMonitor, mMockDvcMonitor);
}
@@ -2206,9 +2223,10 @@
@Test
public void testGetSimOperator_unavailableDevice() {
mMockIDevice = EasyMock.createMock(IDevice.class);
- EasyMock.expect(mMockIDevice.getState()).andReturn(DeviceState.UNAUTHORIZED);
- EasyMock.expect(mMockIDevice.getSerialNumber()).andReturn("serial");
+ mMockStateMonitor.setState(TestDeviceState.NOT_AVAILABLE);
+ EasyMock.expect(mMockIDevice.getSerialNumber()).andReturn("serial").times(2);
EasyMock.replay(mMockIDevice, mMockStateMonitor, mMockDvcMonitor);
+ mTestDevice.setDeviceState(TestDeviceState.NOT_AVAILABLE);
assertNull(mTestDevice.getSimOperator());
EasyMock.verify(mMockIDevice, mMockStateMonitor, mMockDvcMonitor);
}
@@ -2280,7 +2298,6 @@
@Test
public void testGetLogcatSince() throws Exception {
long date = 1512990942000L; // 2017-12-11 03:15:42.015
- EasyMock.expect(mMockIDevice.getState()).andReturn(DeviceState.ONLINE);
SettableFuture<String> value = SettableFuture.create();
value.set("23");
EasyMock.expect(mMockIDevice.getSystemProperty("ro.build.version.sdk")).andReturn(value);
diff --git a/tests/src/com/android/tradefed/device/TestDeviceTest.java b/tests/src/com/android/tradefed/device/TestDeviceTest.java
index e570966..62918ae 100644
--- a/tests/src/com/android/tradefed/device/TestDeviceTest.java
+++ b/tests/src/com/android/tradefed/device/TestDeviceTest.java
@@ -699,7 +699,6 @@
for (int i=0; i <= TestDevice.MAX_RETRY_ATTEMPTS; i++) {
assertRecoverySuccess();
}
- EasyMock.expect(mMockIDevice.getState()).andReturn(DeviceState.ONLINE).times(2);
EasyMock.expect(mMockStateMonitor.waitForDeviceOnline()).andReturn(mMockIDevice).times(3);
injectSystemProperty("ro.build.version.sdk", "23").times(3);
replayMocks();
@@ -1698,7 +1697,6 @@
final String property, final String value) {
SettableFuture<String> valueResponse = SettableFuture.create();
valueResponse.set(value);
- EasyMock.expect(mMockIDevice.getState()).andReturn(DeviceState.ONLINE);
return EasyMock.expect(mMockIDevice.getSystemProperty(property)).andReturn(valueResponse);
}