diff options
| author | 2017-08-17 04:49:04 +0000 | |
|---|---|---|
| committer | 2017-08-17 04:49:04 +0000 | |
| commit | 1c18b565209b0e2dc579b88e916fd843376ab1ef (patch) | |
| tree | 722ff22c781e137bbcddfeffca28c8da4d12ff49 | |
| parent | 551e5a7bb239723f33d1acb4159366ada86a02b7 (diff) | |
| parent | 734b0b716325e55700bb99e99bbb0f170edf65bc (diff) | |
Merge "Fix and enable ConnOnActivityStartTest." into oc-mr1-dev
11 files changed, 185 insertions, 105 deletions
diff --git a/services/tests/servicestests/Android.mk b/services/tests/servicestests/Android.mk index 507b4830e455..19396d43e15e 100644 --- a/services/tests/servicestests/Android.mk +++ b/services/tests/servicestests/Android.mk @@ -29,7 +29,8 @@ LOCAL_STATIC_JAVA_LIBRARIES := \ LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)/aidl -LOCAL_SRC_FILES += aidl/com/android/servicestests/aidl/INetworkStateObserver.aidl +LOCAL_SRC_FILES += aidl/com/android/servicestests/aidl/INetworkStateObserver.aidl \ + aidl/com/android/servicestests/aidl/ICmdReceiverService.aidl LOCAL_JAVA_LIBRARIES := android.test.mock legacy-android-test @@ -61,3 +62,5 @@ LOCAL_DX_FLAGS := --multi-dex LOCAL_STATIC_JAVA_LIBRARIES += ub-uiautomator include $(BUILD_PACKAGE) + +include $(call all-makefiles-under, $(LOCAL_PATH))
\ No newline at end of file diff --git a/services/tests/servicestests/AndroidTest.xml b/services/tests/servicestests/AndroidTest.xml index 63f3b7560467..6a9983e87d90 100644 --- a/services/tests/servicestests/AndroidTest.xml +++ b/services/tests/servicestests/AndroidTest.xml @@ -16,6 +16,7 @@ <configuration description="Runs Frameworks Services Tests."> <target_preparer class="com.android.tradefed.targetprep.TestAppInstallSetup"> <option name="test-file-name" value="FrameworksServicesTests.apk" /> + <option name="test-file-name" value="ConnTestApp.apk" /> </target_preparer> <option name="test-suite-tag" value="apct" /> diff --git a/services/tests/servicestests/aidl/Android.mk b/services/tests/servicestests/aidl/Android.mk index 0c9b83962833..166da1dea7e3 100644 --- a/services/tests/servicestests/aidl/Android.mk +++ b/services/tests/servicestests/aidl/Android.mk @@ -18,6 +18,7 @@ include $(CLEAR_VARS) LOCAL_MODULE_TAGS := tests LOCAL_SDK_VERSION := current LOCAL_SRC_FILES := \ - com/android/servicestests/aidl/INetworkStateObserver.aidl + com/android/servicestests/aidl/INetworkStateObserver.aidl \ + com/android/servicestests/aidl/ICmdReceiverService.aidl LOCAL_MODULE := servicestests-aidl include $(BUILD_STATIC_JAVA_LIBRARY)
\ No newline at end of file diff --git a/services/tests/servicestests/aidl/com/android/servicestests/aidl/ICmdReceiverService.aidl b/services/tests/servicestests/aidl/com/android/servicestests/aidl/ICmdReceiverService.aidl new file mode 100644 index 000000000000..d96450478f90 --- /dev/null +++ b/services/tests/servicestests/aidl/com/android/servicestests/aidl/ICmdReceiverService.aidl @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.servicestests.aidl; + +interface ICmdReceiverService { + void finishActivity(); +}
\ No newline at end of file diff --git a/services/tests/servicestests/res/raw/conntestapp b/services/tests/servicestests/res/raw/conntestapp Binary files differdeleted file mode 100644 index e99316446f7e..000000000000 --- a/services/tests/servicestests/res/raw/conntestapp +++ /dev/null diff --git a/services/tests/servicestests/src/com/android/server/net/ConnOnActivityStartTest.java b/services/tests/servicestests/src/com/android/server/net/ConnOnActivityStartTest.java index 5b4c10f11af8..7f48b8e1759a 100644 --- a/services/tests/servicestests/src/com/android/server/net/ConnOnActivityStartTest.java +++ b/services/tests/servicestests/src/com/android/server/net/ConnOnActivityStartTest.java @@ -16,48 +16,37 @@ package com.android.server.net; -import static android.util.DebugUtils.valueToString; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import com.android.frameworks.servicestests.R; +import com.android.servicestests.aidl.ICmdReceiverService; import com.android.servicestests.aidl.INetworkStateObserver; -import android.app.PendingIntent; -import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.IntentFilter; -import android.content.IntentSender; -import android.content.pm.IPackageDeleteObserver; -import android.content.pm.PackageInstaller; +import android.content.ServiceConnection; import android.content.pm.PackageManager; -import android.net.Uri; import android.os.BatteryManager; import android.os.Bundle; -import android.os.RemoteException; +import android.os.IBinder; import android.os.SystemClock; +import android.provider.Settings; import android.support.test.InstrumentationRegistry; import android.support.test.filters.LargeTest; import android.support.test.runner.AndroidJUnit4; import android.support.test.uiautomator.UiDevice; import android.util.Log; -import libcore.io.IoUtils; - import org.junit.AfterClass; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -76,62 +65,118 @@ import java.util.concurrent.TimeUnit; * Run: adb shell am instrument -e class com.android.server.net.ConnOnActivityStartTest -w \ * com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner */ -@Ignore @LargeTest @RunWith(AndroidJUnit4.class) public class ConnOnActivityStartTest { private static final String TAG = ConnOnActivityStartTest.class.getSimpleName(); - private static final String ACTION_INSTALL_COMPLETE = "com.android.server.net.INSTALL_COMPLETE"; - - private static final String TEST_APP_URI = - "android.resource://com.android.frameworks.servicestests/raw/conntestapp"; private static final String TEST_PKG = "com.android.servicestests.apps.conntestapp"; private static final String TEST_ACTIVITY_CLASS = TEST_PKG + ".ConnTestActivity"; - - private static final String ACTION_FINISH_ACTIVITY = TEST_PKG + ".FINISH"; + private static final String TEST_SERVICE_CLASS = TEST_PKG + ".CmdReceiverService"; private static final String EXTRA_NETWORK_STATE_OBSERVER = TEST_PKG + ".observer"; private static final long BATTERY_OFF_TIMEOUT_MS = 2000; // 2 sec private static final long BATTERY_OFF_CHECK_INTERVAL_MS = 200; // 0.2 sec - private static final long WAIT_FOR_INSTALL_TIMEOUT_MS = 2000; // 2 sec - private static final long NETWORK_CHECK_TIMEOUT_MS = 4000; // 4 sec private static final long SCREEN_ON_DELAY_MS = 500; // 0.5 sec + private static final long BIND_SERVICE_TIMEOUT_SEC = 4; + private static final int REPEAT_TEST_COUNT = 5; + private static final String KEY_PAROLE_DURATION = "parole_duration"; + private static final String DESIRED_PAROLE_DURATION = "0"; + private static Context mContext; private static UiDevice mUiDevice; private static int mTestPkgUid; private static BatteryManager mBatteryManager; + private static boolean mAppIdleConstsUpdated; + private static String mOriginalAppIdleConsts; + + private static ServiceConnection mServiceConnection; + private static ICmdReceiverService mCmdReceiverService; + @BeforeClass public static void setUpOnce() throws Exception { mContext = InstrumentationRegistry.getContext(); mUiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); - installAppAndAssertInstalled(); + setDesiredParoleDuration(); mContext.getPackageManager().setApplicationEnabledSetting(TEST_PKG, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0); mTestPkgUid = mContext.getPackageManager().getPackageUid(TEST_PKG, 0); mBatteryManager = (BatteryManager) mContext.getSystemService(Context.BATTERY_SERVICE); + bindService(); } @AfterClass public static void tearDownOnce() { - mContext.getPackageManager().deletePackage(TEST_PKG, - new IPackageDeleteObserver.Stub() { - @Override - public void packageDeleted(String packageName, int returnCode) - throws RemoteException { - Log.e(TAG, packageName + " deleted, returnCode: " + returnCode); - } - }, 0); + if (mAppIdleConstsUpdated) { + Settings.Global.putString(mContext.getContentResolver(), + Settings.Global.APP_IDLE_CONSTANTS, mOriginalAppIdleConsts); + } + unbindService(); + } + + private static void bindService() throws Exception { + final CountDownLatch bindLatch = new CountDownLatch(1); + mServiceConnection = new ServiceConnection() { + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + Log.i(TAG, "Service connected"); + mCmdReceiverService = ICmdReceiverService.Stub.asInterface(service); + bindLatch.countDown(); + } + + @Override + public void onServiceDisconnected(ComponentName name) { + Log.i(TAG, "Service disconnected"); + } + }; + final Intent intent = new Intent() + .setComponent(new ComponentName(TEST_PKG, TEST_SERVICE_CLASS)); + // Needs to use BIND_ALLOW_OOM_MANAGEMENT and BIND_NOT_FOREGROUND so that the test app + // does not run in the same process state as this app. + mContext.bindService(intent, mServiceConnection, + Context.BIND_AUTO_CREATE + | Context.BIND_ALLOW_OOM_MANAGEMENT + | Context.BIND_NOT_FOREGROUND); + if (!bindLatch.await(BIND_SERVICE_TIMEOUT_SEC, TimeUnit.SECONDS)) { + fail("Timed out waiting for the service to bind in " + mTestPkgUid); + } + } + + private static void unbindService() { + if (mCmdReceiverService != null) { + mContext.unbindService(mServiceConnection); + } + } + + private static void setDesiredParoleDuration() { + mOriginalAppIdleConsts = Settings.Global.getString(mContext.getContentResolver(), + Settings.Global.APP_IDLE_CONSTANTS); + String newAppIdleConstants; + final String newConstant = KEY_PAROLE_DURATION + "=" + DESIRED_PAROLE_DURATION; + if (mOriginalAppIdleConsts == null || "null".equals(mOriginalAppIdleConsts)) { + // app_idle_constants is initially empty, so just assign the desired value. + newAppIdleConstants = newConstant; + } else if (mOriginalAppIdleConsts.contains(KEY_PAROLE_DURATION)) { + // app_idle_constants contains parole_duration, so replace it with the desired value. + newAppIdleConstants = mOriginalAppIdleConsts.replaceAll( + KEY_PAROLE_DURATION + "=\\d+", newConstant); + } else { + // app_idle_constants didn't have parole_duration, so append the desired value. + newAppIdleConstants = mOriginalAppIdleConsts + "," + newConstant; + } + Settings.Global.putString(mContext.getContentResolver(), + Settings.Global.APP_IDLE_CONSTANTS, newAppIdleConstants); + mAppIdleConstsUpdated = true; } @Test @@ -174,6 +219,7 @@ public class ConnOnActivityStartTest { startActivityAndCheckNetworkAccess(); } finally { turnBatteryOn(); + finishActivity(); setAppIdle(false); } } @@ -195,9 +241,9 @@ public class ConnOnActivityStartTest { turnScreenOn(); SystemClock.sleep(SCREEN_ON_DELAY_MS); startActivityAndCheckNetworkAccess(); - Log.d(TAG, testName + " end #" + i); } finally { finishActivity(); + Log.d(TAG, testName + " end #" + i); } } } @@ -348,74 +394,7 @@ public class ConnOnActivityStartTest { } } - private void finishActivity() { - final Intent finishIntent = new Intent(ACTION_FINISH_ACTIVITY) - .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); - mContext.sendBroadcast(finishIntent); - } - private static void installAppAndAssertInstalled() throws Exception { - final CountDownLatch latch = new CountDownLatch(1); - final int[] result = {PackageInstaller.STATUS_SUCCESS}; - final BroadcastReceiver installStatusReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - final String pkgName = intent.getStringExtra(PackageInstaller.EXTRA_PACKAGE_NAME); - if (!TEST_PKG.equals(pkgName)) { - return; - } - result[0] = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, - PackageInstaller.STATUS_FAILURE); - latch.countDown(); - } - }; - mContext.registerReceiver(installStatusReceiver, new IntentFilter(ACTION_INSTALL_COMPLETE)); - try { - installApp(); - if (latch.await(WAIT_FOR_INSTALL_TIMEOUT_MS, TimeUnit.MILLISECONDS)) { - if (result[0] != PackageInstaller.STATUS_SUCCESS) { - fail("Couldn't install test app, result: " - + valueToString(PackageInstaller.class, "STATUS_", result[0])); - } - } else { - fail("Timed out waiting for the test app to install"); - } - } finally { - mContext.unregisterReceiver(installStatusReceiver); - } - } - - private static void installApp() throws Exception { - final Uri packageUri = Uri.parse(TEST_APP_URI); - final InputStream in = mContext.getContentResolver().openInputStream(packageUri); - - final PackageInstaller packageInstaller - = mContext.getPackageManager().getPackageInstaller(); - final PackageInstaller.SessionParams params = new PackageInstaller.SessionParams( - PackageInstaller.SessionParams.MODE_FULL_INSTALL); - params.setAppPackageName(TEST_PKG); - - final int sessionId = packageInstaller.createSession(params); - final PackageInstaller.Session session = packageInstaller.openSession(sessionId); - - OutputStream out = null; - try { - out = session.openWrite(TAG, 0, -1); - final byte[] buffer = new byte[65536]; - int c; - while ((c = in.read(buffer)) != -1) { - out.write(buffer, 0, c); - } - session.fsync(out); - } finally { - IoUtils.closeQuietly(in); - IoUtils.closeQuietly(out); - } - session.commit(createIntentSender(mContext, sessionId)); - } - - private static IntentSender createIntentSender(Context context, int sessionId) { - PendingIntent pendingIntent = PendingIntent.getBroadcast( - context, sessionId, new Intent(ACTION_INSTALL_COMPLETE), 0); - return pendingIntent.getIntentSender(); + private void finishActivity() throws Exception { + mCmdReceiverService.finishActivity(); } }
\ No newline at end of file diff --git a/services/tests/servicestests/test-apps/Android.mk b/services/tests/servicestests/test-apps/Android.mk new file mode 100644 index 000000000000..5053e7d64389 --- /dev/null +++ b/services/tests/servicestests/test-apps/Android.mk @@ -0,0 +1 @@ +include $(call all-subdir-makefiles) diff --git a/services/tests/servicestests/test-apps/ConnTestApp/Android.mk b/services/tests/servicestests/test-apps/ConnTestApp/Android.mk index 030a709b0215..fbfa28a9e93b 100644 --- a/services/tests/servicestests/test-apps/ConnTestApp/Android.mk +++ b/services/tests/servicestests/test-apps/ConnTestApp/Android.mk @@ -18,6 +18,8 @@ include $(CLEAR_VARS) LOCAL_MODULE_TAGS := tests +LOCAL_COMPATIBILITY_SUITE := device-tests + LOCAL_STATIC_JAVA_LIBRARIES := servicestests-aidl LOCAL_SRC_FILES := $(call all-subdir-java-files) diff --git a/services/tests/servicestests/test-apps/ConnTestApp/AndroidManifest.xml b/services/tests/servicestests/test-apps/ConnTestApp/AndroidManifest.xml index 66714109a761..909e9bb7bcd9 100644 --- a/services/tests/servicestests/test-apps/ConnTestApp/AndroidManifest.xml +++ b/services/tests/servicestests/test-apps/ConnTestApp/AndroidManifest.xml @@ -24,6 +24,8 @@ <application> <activity android:name=".ConnTestActivity" android:exported="true" /> + <service android:name=".CmdReceiverService" + android:exported="true" /> </application> </manifest>
\ No newline at end of file diff --git a/services/tests/servicestests/test-apps/ConnTestApp/src/com/android/servicestests/apps/conntestapp/CmdReceiverService.java b/services/tests/servicestests/test-apps/ConnTestApp/src/com/android/servicestests/apps/conntestapp/CmdReceiverService.java new file mode 100644 index 000000000000..6130f3a3fc76 --- /dev/null +++ b/services/tests/servicestests/test-apps/ConnTestApp/src/com/android/servicestests/apps/conntestapp/CmdReceiverService.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.servicestests.apps.conntestapp; + +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; + +import com.android.servicestests.aidl.ICmdReceiverService; + +public class CmdReceiverService extends Service { + private ICmdReceiverService.Stub mBinder = new ICmdReceiverService.Stub() { + @Override + public void finishActivity() { + ConnTestActivity.finishSelf(); + } + }; + + @Override + public IBinder onBind(Intent intent) { + return mBinder; + } +}
\ No newline at end of file diff --git a/services/tests/servicestests/test-apps/ConnTestApp/src/com/android/servicestests/apps/conntestapp/ConnTestActivity.java b/services/tests/servicestests/test-apps/ConnTestApp/src/com/android/servicestests/apps/conntestapp/ConnTestActivity.java index c5c7add29f93..b26cc7fd0130 100644 --- a/services/tests/servicestests/test-apps/ConnTestApp/src/com/android/servicestests/apps/conntestapp/ConnTestActivity.java +++ b/services/tests/servicestests/test-apps/ConnTestApp/src/com/android/servicestests/apps/conntestapp/ConnTestActivity.java @@ -30,6 +30,7 @@ import android.os.RemoteException; import android.os.ServiceManager; import android.util.Log; +import com.android.internal.annotations.GuardedBy; import com.android.servicestests.aidl.INetworkStateObserver; public class ConnTestActivity extends Activity { @@ -39,17 +40,26 @@ public class ConnTestActivity extends Activity { private static final String ACTION_FINISH_ACTIVITY = TEST_PKG + ".FINISH"; private static final String EXTRA_NETWORK_STATE_OBSERVER = TEST_PKG + ".observer"; + private static final Object INSTANCE_LOCK = new Object(); + @GuardedBy("instanceLock") + private static Activity sInstance; + private BroadcastReceiver finishCommandReceiver = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + synchronized (INSTANCE_LOCK) { + sInstance = this; + } + Log.i(TAG, "onCreate called"); notifyNetworkStateObserver(); finishCommandReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { + Log.i(TAG, "finish command received"); ConnTestActivity.this.finish(); } }; @@ -57,13 +67,37 @@ public class ConnTestActivity extends Activity { } @Override + public void onResume() { + super.onResume(); + Log.i(TAG, "onResume called"); + } + + @Override public void onStop() { + Log.i(TAG, "onStop called"); if (finishCommandReceiver != null) { unregisterReceiver(finishCommandReceiver); } super.onStop(); } + @Override + public void finish() { + synchronized (INSTANCE_LOCK) { + sInstance = null; + } + Log.i(TAG, "finish called"); + super.finish(); + } + + public static void finishSelf() { + synchronized (INSTANCE_LOCK) { + if (sInstance != null) { + sInstance.finish(); + } + } + } + private void notifyNetworkStateObserver() { if (getIntent() == null) { return; |