From 7655b694ec15e4e31b473a96fc3d4507cdb53456 Mon Sep 17 00:00:00 2001 From: Sudheer Shanka Date: Mon, 1 May 2017 11:55:53 -0700 Subject: [PATCH] Fix CtsHostsideNetworkTests. The changes made to ensuring network connectivity on app start only applies to activities. There is no change in how the services are handled. Bug: 27803922 Test: cts-tradefed run singleCommand cts-dev -m CtsHostsideNetworkTests -t \ com.android.cts.net.HostsideRestrictBackgroundNetworkTests Change-Id: Ib82b07195e395c89701dd525b850c0935d94dfe7 --- ...ractRestrictBackgroundNetworkTestCase.java | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) 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 577f62c95c..d43d8aabae 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 @@ -817,29 +817,37 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation } protected void launchComponentAndAssertNetworkAccess(int type) throws Exception { - if (type == TYPE_COMPONENT_ACTIVTIY) { + if (type == TYPE_COMPONENT_FOREGROUND_SERVICE) { + startForegroundService(); + assertForegroundServiceNetworkAccess(); + return; + } else if (type == TYPE_COMPONENT_ACTIVTIY) { turnScreenOn(); - } - final CountDownLatch latch = new CountDownLatch(1); - final Intent launchIntent = getIntentForComponent(type); - final Bundle extras = new Bundle(); - final String[] errors = new String[] {null}; - extras.putBinder(KEY_NETWORK_STATE_OBSERVER, getNewNetworkStateObserver(latch, errors)); - launchIntent.putExtras(extras); - if (type == TYPE_COMPONENT_ACTIVTIY) { + final CountDownLatch latch = new CountDownLatch(1); + final Intent launchIntent = getIntentForComponent(type); + final Bundle extras = new Bundle(); + final String[] errors = new String[]{null}; + extras.putBinder(KEY_NETWORK_STATE_OBSERVER, getNewNetworkStateObserver(latch, errors)); + launchIntent.putExtras(extras); mContext.startActivity(launchIntent); - } else if (type == TYPE_COMPONENT_FOREGROUND_SERVICE) { - mContext.startService(launchIntent); - } - if (latch.await(FOREGROUND_PROC_NETWORK_TIMEOUT_MS, TimeUnit.MILLISECONDS)) { - if (!errors[0].isEmpty()) { - fail("Network is not available for app2 (" + mUid + "): " + errors[0]); + if (latch.await(FOREGROUND_PROC_NETWORK_TIMEOUT_MS, TimeUnit.MILLISECONDS)) { + if (!errors[0].isEmpty()) { + fail("Network is not available for app2 (" + mUid + "): " + errors[0]); + } + } else { + fail("Timed out waiting for network availability status from app2 (" + mUid + ")"); } } else { - fail("Timed out waiting for network availability status from app2 (" + mUid + ")"); + throw new IllegalArgumentException("Unknown type: " + type); } } + private void startForegroundService() throws Exception { + final Intent launchIntent = getIntentForComponent(TYPE_COMPONENT_FOREGROUND_SERVICE); + mContext.startForegroundService(launchIntent); + assertForegroundServiceState(); + } + private Intent getIntentForComponent(int type) { final Intent intent = new Intent(); if (type == TYPE_COMPONENT_ACTIVTIY) {