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) {