diff options
| author | 2024-01-04 11:59:06 +0000 | |
|---|---|---|
| committer | 2024-01-04 11:59:06 +0000 | |
| commit | 40cfc64ca76304c207ef69e891c3521f7d5aef25 (patch) | |
| tree | ad4e13b9d02c8aaa521f2d1eb43d4293f82b78db | |
| parent | 061573f22bf79294d8cdfcae1be4d689273de24f (diff) | |
| parent | 2c4ee02e3eb655d63a774109176b5251509f6841 (diff) | |
Merge "SysuiTestCase#context should be stable" into main
| -rw-r--r-- | packages/SystemUI/multivalentTests/src/com/android/systemui/SysuiTestCaseSelfTest.kt | 35 | ||||
| -rw-r--r-- | packages/SystemUI/tests/utils/src/com/android/systemui/SysuiTestCase.java | 21 |
2 files changed, 50 insertions, 6 deletions
diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/SysuiTestCaseSelfTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/SysuiTestCaseSelfTest.kt new file mode 100644 index 000000000000..be6bb9c39299 --- /dev/null +++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/SysuiTestCaseSelfTest.kt @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2024 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.systemui + +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.filters.SmallTest +import com.google.common.truth.Truth +import org.junit.Test +import org.junit.runner.RunWith + +@SmallTest +@RunWith(AndroidJUnit4::class) +class SysuiTestCaseSelfTest : SysuiTestCase() { + private val contextBeforeSetup = context + + // cf b/311612168 + @Test + fun captureCorrectContextBeforeSetupRuns() { + Truth.assertThat(contextBeforeSetup).isEqualTo(context) + } +} diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/SysuiTestCase.java b/packages/SystemUI/tests/utils/src/com/android/systemui/SysuiTestCase.java index 29e737eac99b..d23dae9c762c 100644 --- a/packages/SystemUI/tests/utils/src/com/android/systemui/SysuiTestCase.java +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/SysuiTestCase.java @@ -33,6 +33,7 @@ import android.testing.TestWithLooperRule; import android.testing.TestableLooper; import android.util.Log; +import androidx.annotation.NonNull; import androidx.core.animation.AndroidXAnimatorIsolationRule; import androidx.test.InstrumentationRegistry; import androidx.test.uiautomator.UiDevice; @@ -68,8 +69,20 @@ public abstract class SysuiTestCase { public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(DEVICE_DEFAULT); @Rule - public SysuiTestableContext mContext = new SysuiTestableContext( - InstrumentationRegistry.getContext(), getLeakCheck()); + public SysuiTestableContext mContext = createTestableContext(); + + @NonNull + private SysuiTestableContext createTestableContext() { + SysuiTestableContext context = new SysuiTestableContext( + InstrumentationRegistry.getContext(), getLeakCheck()); + if (isRobolectricTest()) { + // Manually associate a Display to context for Robolectric test. Similar to b/214297409 + return context.createDefaultDisplayContext(); + } else { + return context; + } + } + @Rule public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule = new DexmakerShareClassLoaderRule(); @@ -84,10 +97,6 @@ public abstract class SysuiTestCase { @Before public void SysuiSetup() throws Exception { - // Manually associate a Display to context for Robolectric test. Similar to b/214297409 - if (isRobolectricTest()) { - mContext = mContext.createDefaultDisplayContext(); - } mSysuiDependency = new SysuiTestDependency(mContext, shouldFailOnLeakedReceiver()); mDependency = mSysuiDependency.install(); mRealInstrumentation = InstrumentationRegistry.getInstrumentation(); |