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

Commit b30145c2 authored by Garfield Tan's avatar Garfield Tan
Browse files

Fix some issues about AMS creation in tests.

In commit ce8db89c we fixed a mem leak
issue caused by thread leak. That makes some tests using the constructor
in question crash. Therefore instead of trying to restore the original
state, convert these tests to use another constructor to avoid the mem
leak.

Also fixed another NPE when getting resources from null context and null
UiHandler for some of these tests.

Bug: 138840116
Test: atest FrameworksServicesTests:ActivityManagerServiceTest
Test: atest FrameworksServicesTests:ActivityManagerInternalTest
Test: atest FrameworksServicesTests:CoreSettingsObserverTest doesn't
fail for these reasons.
Test: atest FrameworksMockingServicesTests:AppCompactorTest
Test: atest FrameworksServicesTests:AppErrorDialogTest
Change-Id: Id349a3e1100faa5b4f5033d94c0f892d3f3745bf
parent 2ef7b3ec
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -2373,11 +2373,6 @@ 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.
+19 −1
Original line number Diff line number Diff line
@@ -21,12 +21,17 @@ import static com.android.server.am.AppCompactor.compactActionIntToString;

import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Process;
import android.platform.test.annotations.Presubmit;
import android.provider.DeviceConfig;
import android.text.TextUtils;

import androidx.test.platform.app.InstrumentationRegistry;

import com.android.server.ServiceThread;
import com.android.server.appop.AppOpsService;
import com.android.server.testables.TestableDeviceConfig;

@@ -54,6 +59,8 @@ import java.util.concurrent.TimeUnit;
@RunWith(MockitoJUnitRunner.class)
public final class AppCompactorTest {

    private ServiceThread mThread;

    @Mock
    private AppOpsService mAppOpsService;
    private AppCompactor mCompactorUnderTest;
@@ -70,7 +77,12 @@ public final class AppCompactorTest {
        mHandlerThread = new HandlerThread("");
        mHandlerThread.start();
        mHandler = new Handler(mHandlerThread.getLooper());
        ActivityManagerService ams = new ActivityManagerService(new TestInjector());

        mThread = new ServiceThread("TestServiceThread", Process.THREAD_PRIORITY_DEFAULT,
                true /* allowIo */);
        mThread.start();

        ActivityManagerService ams = new ActivityManagerService(new TestInjector(), mThread);
        mCompactorUnderTest = new AppCompactor(ams,
                new AppCompactor.PropertyChangedCallbackForTest() {
                    @Override
@@ -85,6 +97,7 @@ public final class AppCompactorTest {
    @After
    public void tearDown() {
        mHandlerThread.quit();
        mThread.quit();
        mCountDown = null;
    }

@@ -656,5 +669,10 @@ public final class AppCompactorTest {
        public Handler getUiHandler(ActivityManagerService service) {
            return mHandler;
        }

        @Override
        public Context getContext() {
            return InstrumentationRegistry.getInstrumentation().getContext();
        }
    }
}
+11 −1
Original line number Diff line number Diff line
@@ -18,13 +18,17 @@ package com.android.server.am;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;

import android.app.ActivityManagerInternal;
import android.os.SystemClock;

import androidx.test.filters.MediumTest;
import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@@ -43,6 +47,8 @@ public class ActivityManagerInternalTest {
    private static final long TEST_PROC_STATE_SEQ2 = 1112;
    private static final long TEST_PROC_STATE_SEQ3 = 1113;

    @Rule public ServiceThreadRule mServiceThreadRule = new ServiceThreadRule();

    @Mock private ActivityManagerService.Injector mMockInjector;

    private ActivityManagerService mAms;
@@ -52,7 +58,11 @@ public class ActivityManagerInternalTest {
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        mAms = new ActivityManagerService(mMockInjector);
        doReturn(InstrumentationRegistry.getInstrumentation().getContext()).when(mMockInjector)
                .getContext();
        doReturn(mServiceThreadRule.getThread().getThreadHandler()).when(mMockInjector)
                .getUiHandler(any());
        mAms = new ActivityManagerService(mMockInjector, mServiceThreadRule.getThread());
        mAmi = mAms.new LocalService();
    }

+5 −2
Original line number Diff line number Diff line
@@ -70,13 +70,14 @@ import androidx.test.filters.FlakyTest;
import androidx.test.filters.MediumTest;
import androidx.test.filters.SmallTest;

import com.android.server.appop.AppOpsService;
import com.android.server.am.ProcessList.IsolatedUidRange;
import com.android.server.am.ProcessList.IsolatedUidRangeAllocator;
import com.android.server.appop.AppOpsService;
import com.android.server.wm.ActivityTaskManagerService;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
@@ -114,6 +115,8 @@ public class ActivityManagerServiceTest {
        UidRecord.CHANGE_ACTIVE
    };

    @Rule public ServiceThreadRule mServiceThreadRule = new ServiceThreadRule();

    private Context mContext = getInstrumentation().getTargetContext();
    @Mock private AppOpsService mAppOpsService;
    @Mock private PackageManager mPackageManager;
@@ -131,7 +134,7 @@ public class ActivityManagerServiceTest {
        mHandlerThread.start();
        mHandler = new TestHandler(mHandlerThread.getLooper());
        mInjector = new TestInjector();
        mAms = new ActivityManagerService(mInjector);
        mAms = new ActivityManagerService(mInjector, mServiceThreadRule.getThread());
        mAms.mWaitForNetworkTimeoutMs = 2000;
        mAms.mActivityTaskManager = new ActivityTaskManagerService(mContext);
        mAms.mActivityTaskManager.initialize(null, null, mHandler.getLooper());
+11 −2
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import com.android.server.appop.AppOpsService;
import com.android.server.wm.ActivityTaskManagerService;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.File;
@@ -41,6 +42,9 @@ import java.io.File;
@FlakyTest(bugId = 113616538)
public class AppErrorDialogTest {

    @Rule
    public ServiceThreadRule mServiceThreadRule = new ServiceThreadRule();

    private Context mContext;
    private ActivityManagerService mService;

@@ -55,14 +59,19 @@ public class AppErrorDialogTest {

            @Override
            public Handler getUiHandler(ActivityManagerService service) {
                return null;
                return mServiceThreadRule.getThread().getThreadHandler();
            }

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

            @Override
            public Context getContext() {
                return mContext;
            }
        }, mServiceThreadRule.getThread());
        mService.mActivityTaskManager = new ActivityTaskManagerService(mContext);
        mService.mActivityTaskManager.initialize(null, null, mContext.getMainLooper());
    }
Loading