Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1abb56b4 authored by Riddle Hsu's avatar Riddle Hsu
Browse files

Reduce unused initialization for ActivityManager tests

- Only initialize the necessary members of AMS for testing.
- Use the thread which created by test so it can be shutdown.
  Originally the uncontrolled threads may have some delayed
  message that executes in arbitrary time and causes unexpected
  errors.

Except fixing the thread leakage, the testing time of below
command (currently the amount is 146) is reduced from 13s to 6s.

Bug: 117913959
Test: atest -b FrameworksServicesTests
      adb shell am instrument -e package com.android.server.am \
      -e annotation android.platform.test.annotations.Presubmit \
      -e notAnnotation androidx.test.filters.FlakyTest \
      -w -r com.android.frameworks.servicestests/androidx.test.runner.AndroidJUnitRunner

Change-Id: Ib6db36af58fc299ff558744d33a21f4aed317a37
parent ec397364
Loading
Loading
Loading
Loading
+23 −9
Original line number Diff line number Diff line
@@ -2259,24 +2259,38 @@ public class ActivityManagerService extends IActivityManager.Stub
    @VisibleForTesting
    public ActivityManagerService(Injector injector) {
        this(injector, null /* handlerThread */);
    }
    /**
     * Provides the basic functionality for activity task related tests when a handler thread is
     * given to initialize the dependency members.
     */
    @VisibleForTesting
    ActivityManagerService(Injector injector, ServiceThread handlerThread) {
        final boolean hasHandlerThread = handlerThread != null;
        mInjector = injector;
        mContext = mInjector.getContext();
        mUiContext = null;
        mAppErrors = null;
        mAppOpsService = mInjector.getAppOpsService(null, null);
        mAppOpsService = mInjector.getAppOpsService(null /* file */, null /* handler */);
        mBatteryStatsService = null;
        mConstants = null;
        mHandler = null;
        mHandlerThread = null;
        mIntentFirewall = null;
        mHandler = hasHandlerThread ? new MainHandler(handlerThread.getLooper()) : null;
        mHandlerThread = handlerThread;
        mConstants = hasHandlerThread ? new ActivityManagerConstants(this, mHandler) : null;
        mIntentFirewall = hasHandlerThread
                ? new IntentFirewall(new IntentFirewallInterface(), mHandler) : null;
        mProcessCpuThread = null;
        mProcessStats = null;
        mProviderMap = null;
        mServices = null;
        // For the usage of {@link ActiveServices#cleanUpServices} that may be invoked from
        // {@link ActivityStackSupervisor#cleanUpRemovedTaskLocked}.
        mServices = hasHandlerThread ? new ActiveServices(this) : null;
        mSystemThread = null;
        mUiHandler = injector.getUiHandler(null);
        mUserController = null;
        mPendingIntentController = null;
        mUiHandler = injector.getUiHandler(null /* service */);
        mUserController = hasHandlerThread ? new UserController(this) : null;
        mPendingIntentController = hasHandlerThread
                ? new PendingIntentController(handlerThread.getLooper(), mUserController) : null;
        mProcStartHandlerThread = null;
        mProcStartHandler = null;
        mHiddenApiBlacklist = null;
+46 −10
Original line number Diff line number Diff line
@@ -58,8 +58,9 @@ import android.content.res.Configuration;
import android.graphics.Rect;
import android.hardware.display.DisplayManager;
import android.hardware.display.DisplayManagerGlobal;
import android.os.HandlerThread;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import android.os.UserHandle;
import android.service.voice.IVoiceInteractionSession;
import android.testing.DexmakerShareClassLoaderRule;
@@ -69,7 +70,9 @@ import android.view.DisplayInfo;
import androidx.test.InstrumentationRegistry;

import com.android.internal.app.IVoiceInteractor;
import com.android.server.AppOpsService;
import com.android.server.AttributeCache;
import com.android.server.ServiceThread;
import com.android.server.wm.AppWindowContainerController;
import com.android.server.wm.PinnedStackWindowController;
import com.android.server.wm.RootWindowContainerController;
@@ -82,6 +85,7 @@ import org.junit.After;
import org.junit.Before;
import org.mockito.MockitoAnnotations;

import java.io.File;
import java.util.List;

/**
@@ -97,7 +101,7 @@ public class ActivityTestsBase {
            new DexmakerShareClassLoaderRule();

    private final Context mContext = InstrumentationRegistry.getContext();
    private HandlerThread mHandlerThread;
    final TestInjector mTestInjector = new TestInjector();

    ActivityTaskManagerService mService;
    ActivityStackSupervisor mSupervisor;
@@ -115,13 +119,12 @@ public class ActivityTestsBase {
            MockitoAnnotations.initMocks(this);
            AttributeCache.init(mContext);
        }
        mHandlerThread = new HandlerThread("ActivityTestsBaseThread");
        mHandlerThread.start();
        mTestInjector.setUp();
    }

    @After
    public void tearDown() {
        mHandlerThread.quitSafely();
        mTestInjector.tearDown();
    }

    protected ActivityTaskManagerService createActivityTaskManagerService() {
@@ -143,7 +146,7 @@ public class ActivityTestsBase {
    }

    ActivityManagerService setupActivityManagerService(TestActivityTaskManagerService atm) {
        final TestActivityManagerService am = spy(new TestActivityManagerService(mContext, atm));
        final TestActivityManagerService am = spy(new TestActivityManagerService(mTestInjector));
        setupActivityManagerService(am, atm);
        return am;
    }
@@ -173,6 +176,7 @@ public class ActivityTestsBase {
        doReturn(mockPackageManager).when(am).getPackageManagerInternalLocked();
        doReturn(null).when(mockPackageManager).getDefaultHomeActivity(anyInt());
        doNothing().when(am).grantEphemeralAccessLocked(anyInt(), any(), anyInt(), anyInt());
        am.mActivityTaskManager = atm;
        am.mWindowManager = prepareMockWindowManager();
        atm.setWindowManager(am.mWindowManager);

@@ -192,8 +196,6 @@ public class ActivityTestsBase {
        // An id appended to the end of the component name to make it unique
        private static int sCurrentActivityId = 0;



        private final ActivityTaskManagerService mService;

        private ComponentName mComponent;
@@ -487,6 +489,40 @@ public class ActivityTestsBase {
        }
    }

    private static class TestInjector extends ActivityManagerService.Injector {
        private ServiceThread mHandlerThread;

        @Override
        public Context getContext() {
            return InstrumentationRegistry.getContext();
        }

        @Override
        public AppOpsService getAppOpsService(File file, Handler handler) {
            return null;
        }

        @Override
        public Handler getUiHandler(ActivityManagerService service) {
            return mHandlerThread.getThreadHandler();
        }

        @Override
        public boolean isNetworkRestrictedForUid(int uid) {
            return false;
        }

        void setUp() {
            mHandlerThread = new ServiceThread("ActivityTestsThread",
                    Process.THREAD_PRIORITY_DEFAULT, true /* allowIo */);
            mHandlerThread.start();
        }

        void tearDown() {
            mHandlerThread.quitSafely();
        }
    }

    /**
     * An {@link ActivityManagerService} subclass which provides a test
     * {@link ActivityStackSupervisor}.
@@ -495,8 +531,8 @@ public class ActivityTestsBase {

        private ActivityManagerInternal mInternal;

        TestActivityManagerService(Context context, TestActivityTaskManagerService atm) {
            super(context, atm);
        TestActivityManagerService(TestInjector testInjector) {
            super(testInjector, testInjector.mHandlerThread);
            mUgmInternal = mock(UriGrantsManagerInternal.class);
        }

+1 −1
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ import java.util.List;
@SmallTest
@Presubmit
@RunWith(AndroidJUnit4.class)
public class BroadcastRecordTest extends ActivityTestsBase {
public class BroadcastRecordTest {

    @Test
    public void testCleanupDisabledPackageReceivers() {
+3 −4
Original line number Diff line number Diff line
@@ -116,8 +116,7 @@ public class RecentTasksTest extends ActivityTestsBase {

        mTaskPersister = new TestTaskPersister(mContext.getFilesDir());
        mService = spy(new MyTestActivityTaskManagerService(mContext));
        final TestActivityManagerService am =
                spy(new MyTestActivityManagerService(mContext, mService));
        final TestActivityManagerService am = spy(new MyTestActivityManagerService());
        setupActivityManagerService(am, mService);
        mRecentTasks = (TestRecentTasks) mService.getRecentTasks();
        mRecentTasks.loadParametersFromResources(mContext.getResources());
@@ -848,8 +847,8 @@ public class RecentTasksTest extends ActivityTestsBase {
    }

    private class MyTestActivityManagerService extends TestActivityManagerService {
        MyTestActivityManagerService(Context context, TestActivityTaskManagerService atm) {
            super(context, atm);
        MyTestActivityManagerService() {
            super(mTestInjector);
        }

        @Override
+1 −3
Original line number Diff line number Diff line
@@ -69,13 +69,11 @@ public class TaskRecordTests extends ActivityTestsBase {

    private static final String TASK_TAG = "task";

    private ActivityTaskManagerService mService;

    @Before
    public void setUp() throws Exception {
        super.setUp();
        TaskRecord.setTaskRecordFactory(null);
        mService = createActivityTaskManagerService();
        setupActivityTaskManagerService();
    }

    @Test