From a7382f3265d0bb76532b561059e56bafa855542e Mon Sep 17 00:00:00 2001 From: Mark Renouf Date: Wed, 30 Aug 2023 14:59:06 -0400 Subject: Switches to HiltTestApplication in tests. This allows testing with @HiltAndroidTest, @TestInstallIn, etc. Usage to follow in a CL series. Bug: 296633726 Bug: 300157408 Flag: EXEMPT Change-Id: I13fbf29005e11ea7375d77d11da4bc1bfa68b666 --- java/tests/Android.bp | 1 + java/tests/AndroidManifest.xml | 2 +- .../intentresolver/ChooserWrapperActivity.java | 2 +- .../com/android/intentresolver/TestApplication.kt | 26 ---------------------- .../UnbundledChooserActivityTest.java | 9 +++++++- .../UnbundledChooserActivityWorkProfileTest.java | 9 +++++++- 6 files changed, 19 insertions(+), 30 deletions(-) delete mode 100644 java/tests/src/com/android/intentresolver/TestApplication.kt (limited to 'java/tests') diff --git a/java/tests/Android.bp b/java/tests/Android.bp index e4d24eb5..974b8a47 100644 --- a/java/tests/Android.bp +++ b/java/tests/Android.bp @@ -44,6 +44,7 @@ android_test { "androidx.lifecycle_lifecycle-common-java8", "androidx.lifecycle_lifecycle-extensions", "androidx.lifecycle_lifecycle-runtime-testing", + "hilt_android_testing", "IntentResolver-core", "junit", "kotlinx_coroutines_test", diff --git a/java/tests/AndroidManifest.xml b/java/tests/AndroidManifest.xml index ae6a2205..35dc2ee6 100644 --- a/java/tests/AndroidManifest.xml +++ b/java/tests/AndroidManifest.xml @@ -23,7 +23,7 @@ - + diff --git a/java/tests/src/com/android/intentresolver/ChooserWrapperActivity.java b/java/tests/src/com/android/intentresolver/ChooserWrapperActivity.java index 6dbf9b3f..d488e02b 100644 --- a/java/tests/src/com/android/intentresolver/ChooserWrapperActivity.java +++ b/java/tests/src/com/android/intentresolver/ChooserWrapperActivity.java @@ -263,7 +263,7 @@ public class ChooserWrapperActivity extends ChooserActivity implements IChooserW @Override public Context createContextAsUser(UserHandle user, int flags) { // return the current context as a work profile doesn't really exist in these tests - return getApplicationContext(); + return this; } @Override diff --git a/java/tests/src/com/android/intentresolver/TestApplication.kt b/java/tests/src/com/android/intentresolver/TestApplication.kt deleted file mode 100644 index b57fd4d9..00000000 --- a/java/tests/src/com/android/intentresolver/TestApplication.kt +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (C) 2022 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.intentresolver - -import android.content.Context -import android.os.UserHandle - -class TestApplication : MainApplication() { - - // return the current context as a work profile doesn't really exist in these tests - override fun createContextAsUser(user: UserHandle, flags: Int): Context = this -} diff --git a/java/tests/src/com/android/intentresolver/UnbundledChooserActivityTest.java b/java/tests/src/com/android/intentresolver/UnbundledChooserActivityTest.java index 2838f00b..0b3cb20a 100644 --- a/java/tests/src/com/android/intentresolver/UnbundledChooserActivityTest.java +++ b/java/tests/src/com/android/intentresolver/UnbundledChooserActivityTest.java @@ -146,6 +146,9 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import java.util.function.Function; +import dagger.hilt.android.testing.HiltAndroidRule; +import dagger.hilt.android.testing.HiltAndroidTest; + /** * Instrumentation tests for ChooserActivity. *

@@ -153,6 +156,7 @@ import java.util.function.Function; *

*/ @RunWith(Parameterized.class) +@HiltAndroidTest public class UnbundledChooserActivityTest { private static final UserHandle PERSONAL_USER_HANDLE = InstrumentationRegistry @@ -187,7 +191,10 @@ public class UnbundledChooserActivityTest { cleanOverrideData(); } - @Rule + @Rule(order = 0) + public HiltAndroidRule mHiltAndroidRule = new HiltAndroidRule(this); + + @Rule(order = 1) public ActivityTestRule mActivityRule = new ActivityTestRule<>(ChooserWrapperActivity.class, false, false); diff --git a/java/tests/src/com/android/intentresolver/UnbundledChooserActivityWorkProfileTest.java b/java/tests/src/com/android/intentresolver/UnbundledChooserActivityWorkProfileTest.java index 92bccb7d..f0e88564 100644 --- a/java/tests/src/com/android/intentresolver/UnbundledChooserActivityWorkProfileTest.java +++ b/java/tests/src/com/android/intentresolver/UnbundledChooserActivityWorkProfileTest.java @@ -64,15 +64,22 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; +import dagger.hilt.android.testing.HiltAndroidRule; +import dagger.hilt.android.testing.HiltAndroidTest; + @DeviceFilter.MediumType @RunWith(Parameterized.class) +@HiltAndroidTest public class UnbundledChooserActivityWorkProfileTest { private static final UserHandle PERSONAL_USER_HANDLE = InstrumentationRegistry .getInstrumentation().getTargetContext().getUser(); private static final UserHandle WORK_USER_HANDLE = UserHandle.of(10); - @Rule + @Rule(order = 0) + public HiltAndroidRule mHiltAndroidRule = new HiltAndroidRule(this); + + @Rule(order = 1) public ActivityTestRule mActivityRule = new ActivityTestRule<>(ChooserWrapperActivity.class, false, false); -- cgit v1.2.3-59-g8ed1b