From 2f54af49aeb6f0402cd0d296c72d5c46cd0a8a5d Mon Sep 17 00:00:00 2001 From: Sudheer Shanka Date: Fri, 2 Jun 2017 18:15:05 -0700 Subject: [PATCH] Try to fix HostsideRestrictBackgroundNetworkTests flakyness again. - Add some logging to dump usage stats if setting an app as inactive fails. Bug: 38384021 Test: cts-tradefed run singleCommand cts-dev -m CtsHostsideNetworkTests -t \ com.android.cts.net.HostsideRestrictBackgroundNetworkTests Change-Id: I96d7acabcf3dc18b4187775bbd2bce17a49dbb7b --- ...ractRestrictBackgroundNetworkTestCase.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java index 289de8693a..c4f63e2a3c 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java @@ -44,6 +44,7 @@ import android.os.Bundle; import android.os.SystemClock; import android.service.notification.NotificationListenerService; import android.test.InstrumentationTestCase; +import android.text.TextUtils; import android.util.Log; import com.android.cts.net.hostside.INetworkStateObserver; @@ -308,6 +309,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation } Log.d(TAG, "App not on foreground state on attempt #" + i + "; sleeping 1s before trying again"); + turnScreenOn(); SystemClock.sleep(SECOND_IN_MS); } fail("App2 is not on foreground state after " + maxTries + " attempts: " + state ); @@ -806,8 +808,36 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation protected void setAppIdle(boolean enabled) throws Exception { Log.i(TAG, "Setting app idle to " + enabled); + final String beforeStats = getUsageStatsDump(); executeSilentShellCommand("am set-inactive " + TEST_APP2_PKG + " " + enabled ); - assertAppIdle(enabled); // Sanity check + try { + assertAppIdle(enabled); // Sanity check + } catch (Exception e) { + final String afterStats = getUsageStatsDump(); + Log.d(TAG, "UsageStats before:\n" + beforeStats); + Log.d(TAG, "UsageStats after:\n" + afterStats); + throw e; + } + } + + private String getUsageStatsDump() throws Exception { + final String output = runShellCommand(mInstrumentation, "dumpsys usagestats").trim(); + final StringBuilder sb = new StringBuilder(); + final TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter('\n'); + splitter.setString(output); + String str; + while (splitter.hasNext()) { + str = splitter.next(); + if (str.contains("package=") + && !str.contains(TEST_PKG) && !str.contains(TEST_APP2_PKG)) { + continue; + } + if (str.contains("config=")) { + continue; + } + sb.append(str).append('\n'); + } + return sb.toString(); } protected void assertAppIdle(boolean enabled) throws Exception {