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

Commit 5269e502 authored by Sudheer Shanka's avatar Sudheer Shanka
Browse files

Don't return null in the default ActivityManagerService.Injector.

Usually with injectors like these, we are expected to use methods
provided by these instead of directly accessing the fields, so just
returning "null" in Injector.getContext() is counterintuitive and
ideally other callsites which are directly accessing mContext should
be updated to use injector provided one instead.

Bug: 138952436
Test: atest services/tests/servicestests/src/com/android/server/am/ActivityManagerServiceTest.java
Test: atest services/tests/mockingservicestests/src/com/android/server/am/AppCompactorTest.java
Test: atest services/tests/servicestests/src/com/android/server/am/AppErrorDialogTest.java
Change-Id: Idb7cb9a5b7c4c0d237ece9716932a260956a0109
parent 0b31d6c0
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -2419,7 +2419,7 @@ public class ActivityManagerService extends IActivityManager.Stub
    // handlers to other threads.  So take care to be explicit about the looper.
    public ActivityManagerService(Context systemContext, ActivityTaskManagerService atm) {
        LockGuard.installLock(this, LockGuard.INDEX_ACTIVITY);
        mInjector = new Injector();
        mInjector = new Injector(systemContext);
        mContext = systemContext;
        mFactoryTest = FactoryTest.getMode();
@@ -18894,9 +18894,14 @@ public class ActivityManagerService extends IActivityManager.Stub
    @VisibleForTesting
    public static class Injector {
        private NetworkManagementInternal mNmi;
        private Context mContext;
        public Injector(Context context) {
            mContext = context;
        }
        public Context getContext() {
            return null;
            return mContext;
        }
        public AppOpsService getAppOpsService(File file, Handler handler) {
+8 −6
Original line number Diff line number Diff line
@@ -82,7 +82,9 @@ public final class AppCompactorTest {
                true /* allowIo */);
        mThread.start();

        ActivityManagerService ams = new ActivityManagerService(new TestInjector(), mThread);
        ActivityManagerService ams = new ActivityManagerService(
                new TestInjector(InstrumentationRegistry.getInstrumentation().getContext()),
                mThread);
        mCompactorUnderTest = new AppCompactor(ams,
                new AppCompactor.PropertyChangedCallbackForTest() {
                    @Override
@@ -660,6 +662,11 @@ public final class AppCompactorTest {
    }

    private class TestInjector extends Injector {

        TestInjector(Context context) {
            super(context);
        }

        @Override
        public AppOpsService getAppOpsService(File file, Handler handler) {
            return mAppOpsService;
@@ -669,10 +676,5 @@ public final class AppCompactorTest {
        public Handler getUiHandler(ActivityManagerService service) {
            return mHandler;
        }

        @Override
        public Context getContext() {
            return InstrumentationRegistry.getInstrumentation().getContext();
        }
    }
}
+3 −4
Original line number Diff line number Diff line
@@ -133,7 +133,7 @@ public class ActivityManagerServiceTest {
        mHandlerThread = new HandlerThread(TAG);
        mHandlerThread.start();
        mHandler = new TestHandler(mHandlerThread.getLooper());
        mInjector = new TestInjector();
        mInjector = new TestInjector(mContext);
        mAms = new ActivityManagerService(mInjector, mServiceThreadRule.getThread());
        mAms.mWaitForNetworkTimeoutMs = 2000;
        mAms.mActivityTaskManager = new ActivityTaskManagerService(mContext);
@@ -920,9 +920,8 @@ public class ActivityManagerServiceTest {
    private class TestInjector extends Injector {
        private boolean mRestricted = true;

        @Override
        public Context getContext() {
            return mContext;
        TestInjector(Context context) {
            super(context);
        }

        @Override
+1 −6
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@ public class AppErrorDialogTest {
    @Before
    public void setUp() throws Exception {
        mContext = getInstrumentation().getTargetContext();
        mService = new ActivityManagerService(new ActivityManagerService.Injector() {
        mService = new ActivityManagerService(new ActivityManagerService.Injector(mContext) {
            @Override
            public AppOpsService getAppOpsService(File file, Handler handler) {
                return null;
@@ -66,11 +66,6 @@ public class AppErrorDialogTest {
            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());
+5 −4
Original line number Diff line number Diff line
@@ -93,7 +93,8 @@ public class CoreSettingsObserverTest {
        mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
        when(mContext.getContentResolver()).thenReturn(mContentResolver);

        mAms = new ActivityManagerService(new TestInjector(), mServiceThreadRule.getThread());
        mAms = new ActivityManagerService(new TestInjector(mContext),
                mServiceThreadRule.getThread());
        mCoreSettingsObserver = new CoreSettingsObserver(mAms);
    }

@@ -158,9 +159,9 @@ public class CoreSettingsObserverTest {
    }

    private class TestInjector extends Injector {
        @Override
        public Context getContext() {
            return getInstrumentation().getContext();

        TestInjector(Context context) {
            super(context);
        }

        @Override
Loading