aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTony Mak <tonymak@google.com>2017-06-05 10:38:40 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2017-06-05 10:38:42 +0000
commit50ca63412cd9751be5e042393cb08c7827d0c140 (patch)
tree52eeccad1d6f044818d25711142d09a64f4a8b42
parentbc580f9248984fb4773455dffe6164d4e1e2fd66 (diff)
parent2ec98fa5c2b6127ff7aefe8a4a175c47d2f6da97 (diff)
downloadtradefederation-50ca63412cd9751be5e042393cb08c7827d0c140.tar.gz
Merge "stop-user command should work in pre-N devices" into oc-dev
-rw-r--r--src/com/android/tradefed/device/TestDevice.java14
-rw-r--r--tests/src/com/android/tradefed/device/TestDeviceTest.java249
2 files changed, 174 insertions, 89 deletions
diff --git a/src/com/android/tradefed/device/TestDevice.java b/src/com/android/tradefed/device/TestDevice.java
index d5d9d41d0..066ef123c 100644
--- a/src/com/android/tradefed/device/TestDevice.java
+++ b/src/com/android/tradefed/device/TestDevice.java
@@ -843,10 +843,12 @@ public class TestDevice extends NativeDevice {
@Override
public boolean stopUser(int userId, boolean waitFlag, boolean forceFlag)
throws DeviceNotAvailableException {
- checkApiLevelAgainstNextRelease("stopUser", API_LEVEL_GET_CURRENT_USER);
- if (userId == getCurrentUser()) {
- CLog.d("Cannot stop current user.");
- return false;
+ final int apiLevel = getApiLevel();
+ if (waitFlag && apiLevel < 23) {
+ throw new IllegalArgumentException("stop-user -w requires API level >= 23");
+ }
+ if (forceFlag && apiLevel < 24) {
+ throw new IllegalArgumentException("stop-user -f requires API level >= 24");
}
StringBuilder cmd = new StringBuilder("am stop-user ");
if (waitFlag) {
@@ -863,6 +865,10 @@ public class TestDevice extends NativeDevice {
CLog.e("Cannot stop System user.");
return false;
}
+ if (output.contains("Can't stop current user")) {
+ CLog.e("Cannot stop current user.");
+ return false;
+ }
if (isUserRunning(userId)) {
CLog.w("User Id: %s is still running after the stop-user command.", userId);
return false;
diff --git a/tests/src/com/android/tradefed/device/TestDeviceTest.java b/tests/src/com/android/tradefed/device/TestDeviceTest.java
index 3fe3b8744..f91ad3472 100644
--- a/tests/src/com/android/tradefed/device/TestDeviceTest.java
+++ b/tests/src/com/android/tradefed/device/TestDeviceTest.java
@@ -74,6 +74,7 @@ public class TestDeviceTest extends TestCase {
// For getCurrentUser, the min api should be 24. We make the stub return 23, the logic should
// increment it by one.
private static final int MIN_API_LEVEL_GET_CURRENT_USER = 23;
+ private static final int MIN_API_LEVEL_STOP_USER = 22;
private static final String RAWIMAGE_RESOURCE = "/testdata/rawImage.zip";
private IDevice mMockIDevice;
private IShellOutputReceiver mMockReceiver;
@@ -2428,20 +2429,29 @@ public class TestDeviceTest extends TestCase {
* Unit test for {@link TestDevice#stopUser(int)}, cannot stop current user.
*/
public void testStopUser_notCurrent() throws Exception {
- mTestDevice = new TestableTestDevice() {
- @Override
- public int getCurrentUser() throws DeviceNotAvailableException {
- return 0;
- }
- @Override
- public int getApiLevel() throws DeviceNotAvailableException {
- return MIN_API_LEVEL_GET_CURRENT_USER;
- }
- @Override
- public String getProperty(String name) throws DeviceNotAvailableException {
- return "N\n";
- }
- };
+ mTestDevice =
+ new TestableTestDevice() {
+ @Override
+ public String executeShellCommand(String command)
+ throws DeviceNotAvailableException {
+ return "Can't stop current user";
+ }
+
+ @Override
+ public int getCurrentUser() throws DeviceNotAvailableException {
+ return 0;
+ }
+
+ @Override
+ public int getApiLevel() throws DeviceNotAvailableException {
+ return MIN_API_LEVEL_STOP_USER;
+ }
+
+ @Override
+ public String getProperty(String name) throws DeviceNotAvailableException {
+ return "N\n";
+ }
+ };
assertFalse(mTestDevice.stopUser(0));
}
@@ -2449,88 +2459,157 @@ public class TestDeviceTest extends TestCase {
* Unit test for {@link TestDevice#stopUser(int)}, cannot stop system
*/
public void testStopUser_notSystem() throws Exception {
- mTestDevice = new TestableTestDevice() {
- @Override
- public String executeShellCommand(String command) throws DeviceNotAvailableException {
- return "Error: Can't stop system user 0";
- }
- @Override
- public int getCurrentUser() throws DeviceNotAvailableException {
- return 10;
- }
- @Override
- public int getApiLevel() throws DeviceNotAvailableException {
- return MIN_API_LEVEL_GET_CURRENT_USER;
- }
- @Override
- public String getProperty(String name) throws DeviceNotAvailableException {
- return "N\n";
- }
- };
+ mTestDevice =
+ new TestableTestDevice() {
+ @Override
+ public String executeShellCommand(String command)
+ throws DeviceNotAvailableException {
+ return "Error: Can't stop system user 0";
+ }
+
+ @Override
+ public int getCurrentUser() throws DeviceNotAvailableException {
+ return 10;
+ }
+
+ @Override
+ public int getApiLevel() throws DeviceNotAvailableException {
+ return MIN_API_LEVEL_STOP_USER;
+ }
+
+ @Override
+ public String getProperty(String name) throws DeviceNotAvailableException {
+ return "N\n";
+ }
+ };
assertFalse(mTestDevice.stopUser(0));
}
/**
- * Unit test for {@link TestDevice#stopUser(int, boolean, boolean)}, for a success stop
+ * Unit test for {@link TestDevice#stopUser(int, boolean, boolean)}, for a success stop in API
+ * 22.
*/
- public void testStopUser_success() throws Exception {
- mTestDevice = new TestableTestDevice() {
- @Override
- public String executeShellCommand(String command) throws DeviceNotAvailableException {
- if (command.contains("am")) {
- assertEquals("am stop-user -w -f 0", command);
- } else if (command.contains("pm")) {
- assertEquals("pm list users", command);
- } else {
- fail("Unexpected command");
- }
- return "Users:\n\tUserInfo{0:Test:13}";
- }
- @Override
- public int getCurrentUser() throws DeviceNotAvailableException {
- return 10;
- }
- @Override
- public int getApiLevel() throws DeviceNotAvailableException {
- return MIN_API_LEVEL_GET_CURRENT_USER;
- }
- @Override
- public String getProperty(String name) throws DeviceNotAvailableException {
- return "N\n";
- }
- };
- assertTrue(mTestDevice.stopUser(0, true, true));
+ public void testStopUser_success_api22() throws Exception {
+ verifyStopUserSuccess(22, false, false, "am stop-user 10");
+ }
+
+ /**
+ * Unit test for {@link TestDevice#stopUser(int, boolean, boolean)}, for a success stop in API
+ * 23.
+ */
+ public void testStopUser_success_api23() throws Exception {
+ verifyStopUserSuccess(23, true, false, "am stop-user -w 10");
+ }
+
+ /**
+ * Unit test for {@link TestDevice#stopUser(int, boolean, boolean)}, for a success stop in API
+ * 24.
+ */
+ public void testStopUser_success_api24() throws Exception {
+ verifyStopUserSuccess(24, true, true, "am stop-user -w -f 10");
+ }
+
+ private void verifyStopUserSuccess(
+ int apiLevel, boolean wait, boolean force, String expectedCommand) throws Exception {
+ mTestDevice =
+ new TestableTestDevice() {
+ @Override
+ public String executeShellCommand(String command)
+ throws DeviceNotAvailableException {
+ if (command.contains("am")) {
+ assertEquals(expectedCommand, command);
+ } else if (command.contains("pm")) {
+ assertEquals("pm list users", command);
+ } else {
+ fail("Unexpected command");
+ }
+ return "Users:\n\tUserInfo{0:Test:13}";
+ }
+
+ @Override
+ public int getCurrentUser() throws DeviceNotAvailableException {
+ return 0;
+ }
+
+ @Override
+ public int getApiLevel() throws DeviceNotAvailableException {
+ return apiLevel;
+ }
+
+ @Override
+ public String getProperty(String name) throws DeviceNotAvailableException {
+ return "N\n";
+ }
+ };
+ assertTrue(mTestDevice.stopUser(10, wait, force));
+ }
+
+ public void testStopUser_wait_api22() throws Exception {
+ mTestDevice =
+ new TestableTestDevice() {
+ @Override
+ public int getApiLevel() throws DeviceNotAvailableException {
+ return 22;
+ }
+ };
+ try {
+ mTestDevice.stopUser(10, true, false);
+ fail("IllegalArgumentException should be thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
+ public void testStopUser_force_api22() throws Exception {
+ mTestDevice =
+ new TestableTestDevice() {
+ @Override
+ public int getApiLevel() throws DeviceNotAvailableException {
+ return 22;
+ }
+ };
+ try {
+ mTestDevice.stopUser(10, false, true);
+ fail("IllegalArgumentException should be thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
}
/**
* Unit test for {@link TestDevice#stopUser(int)}, for a failed stop
*/
public void testStopUser_failed() throws Exception {
- mTestDevice = new TestableTestDevice() {
- @Override
- public String executeShellCommand(String command) throws DeviceNotAvailableException {
- if (command.contains("am")) {
- assertEquals("am stop-user 0", command);
- } else if (command.contains("pm")) {
- assertEquals("pm list users", command);
- } else {
- fail("Unexpected command");
- }
- return "Users:\n\tUserInfo{0:Test:13} running";
- }
- @Override
- public int getCurrentUser() throws DeviceNotAvailableException {
- return 10;
- }
- @Override
- public int getApiLevel() throws DeviceNotAvailableException {
- return MIN_API_LEVEL_GET_CURRENT_USER;
- }
- @Override
- public String getProperty(String name) throws DeviceNotAvailableException {
- return "N\n";
- }
- };
+ mTestDevice =
+ new TestableTestDevice() {
+ @Override
+ public String executeShellCommand(String command)
+ throws DeviceNotAvailableException {
+ if (command.contains("am")) {
+ assertEquals("am stop-user 0", command);
+ } else if (command.contains("pm")) {
+ assertEquals("pm list users", command);
+ } else {
+ fail("Unexpected command");
+ }
+ return "Users:\n\tUserInfo{0:Test:13} running";
+ }
+
+ @Override
+ public int getCurrentUser() throws DeviceNotAvailableException {
+ return 10;
+ }
+
+ @Override
+ public int getApiLevel() throws DeviceNotAvailableException {
+ return MIN_API_LEVEL_STOP_USER;
+ }
+
+ @Override
+ public String getProperty(String name) throws DeviceNotAvailableException {
+ return "N\n";
+ }
+ };
assertFalse(mTestDevice.stopUser(0));
}