-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Adds support for custom `AppComponentFactory` during tests. - Designed to be used together with `ActivityScenarioRule`. - Supports `MonitoringInstrumentation`. - In order to avoid breaking open source, support for `RoboMonitoringInstrumentation` will be done later, once `AppComponentFactoryRegistry` is available in a published `androidx.test:monitor` release. PiperOrigin-RevId: 551196131
- Loading branch information
1 parent
f5a9b66
commit 8d7d142
Showing
7 changed files
with
519 additions
and
12 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
ext/junit/java/androidx/test/ext/junit/rules/AppComponentFactoryRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2023 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 androidx.test.ext.junit.rules | ||
|
||
import android.app.AppComponentFactory | ||
import android.os.Build | ||
import androidx.test.annotation.ExperimentalTestApi | ||
import androidx.test.platform.app.AppComponentFactoryRegistry | ||
import org.junit.rules.ExternalResource | ||
|
||
/** | ||
* [AppComponentFactory] let you define a [AppComponentFactory] before the tests starts and clean-up | ||
* the factory after the test. | ||
* | ||
* This rule is designed to be used with [ActivityScenarioRule]. | ||
* | ||
* Example: | ||
* | ||
* ```kotlin | ||
* // Set-up `AppComponentFactoryRule` with your custom `AppComponentFactory`. | ||
* private val factoryRule = AppComponentFactoryRule(MyAppComponentFactory()) | ||
* | ||
* // Set-up `ActivityScenarioRule` with your custom `Activity`. | ||
* private val activityRule = ActivityScenarioRule<MyActivity>() | ||
* | ||
* // Creates a `RuleChain` for ordering the test rules above. We need to ensure the | ||
* // `AppComponentFactoryRule` will always run BEFORE the `ActivityScenarioRule` so that | ||
* // your custom `AppComponentFactory` is available when the activity is launched. | ||
* @[Rule JvmField] val ruleChain = RuleChain.outerRule(factoryRule).around(activityRule) | ||
* ``` | ||
* | ||
* @see <a href="https://junit.org/junit4/javadoc/4.12/org/junit/rules/RuleChain.html">RuleChain</a> | ||
*/ | ||
@ExperimentalTestApi | ||
class AppComponentFactoryRule(private val factory: AppComponentFactory) : ExternalResource() { | ||
|
||
override fun before() { | ||
check(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | ||
"AppComponentFactoryRule is not supported on 'VERSION.SDK_INT < VERSION_CODES.P'" | ||
} | ||
AppComponentFactoryRegistry.appComponentFactory = factory | ||
} | ||
|
||
override fun after() { | ||
AppComponentFactoryRegistry.appComponentFactory = null | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
ext/junit/javatests/androidx/test/ext/junit/rules/AppComponentFactoryRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2023 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 androidx.test.ext.junit.rules | ||
|
||
import android.app.Activity | ||
import android.app.AppComponentFactory | ||
import android.app.Application | ||
import android.content.Intent | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.RuleChain | ||
import org.junit.runner.RunWith | ||
import org.robolectric.annotation.Config | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class AppComponentFactoryRuleTest { | ||
|
||
private val appComponentFactoryRule = AppComponentFactoryRule(DummyAppComponentFactory()) | ||
|
||
private val activityScenarioRule = ActivityScenarioRule(DummyActivity::class.java) | ||
|
||
@[JvmField Rule] | ||
val ruleChain: RuleChain = | ||
RuleChain.outerRule(appComponentFactoryRule).around(activityScenarioRule) | ||
|
||
@Test | ||
@Config(minSdk = 28) | ||
fun shouldCreateNewAppComponentsUsingAppComponentFactorySet() { | ||
activityScenarioRule.scenario.onActivity { activity: DummyActivity -> | ||
assertThat(activity.text).isEqualTo("instantiateActivity") | ||
assertThat(activity.application).isInstanceOf(DummyApplication::class.java) | ||
assertThat((activity.application as DummyApplication).text) | ||
.isEqualTo("instantiateApplication") | ||
} | ||
} | ||
} | ||
|
||
private class DummyAppComponentFactory : AppComponentFactory() { | ||
override fun instantiateApplication(cl: ClassLoader, className: String): Application = | ||
if (className == DummyApplication::class.java.name) { | ||
DummyApplication(text = "instantiateApplication") | ||
} else { | ||
super.instantiateApplication(cl, className) | ||
} | ||
|
||
override fun instantiateActivity(cl: ClassLoader, className: String, intent: Intent?): Activity = | ||
if (className == DummyActivity::class.java.name) { | ||
DummyActivity(text = "instantiateActivity") | ||
} else { | ||
super.instantiateActivity(cl, className, intent) | ||
} | ||
} | ||
|
||
private class DummyApplication(val text: String) : Application() | ||
|
||
private class DummyActivity(val text: String) : Activity() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
runner/monitor/java/androidx/test/platform/app/AppComponentFactoryRegistry.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright 2023 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 androidx.test.platform.app | ||
|
||
import android.app.Activity | ||
import android.app.AppComponentFactory | ||
import android.app.Application | ||
import android.app.Instrumentation | ||
import android.content.Intent | ||
import android.os.Build | ||
import android.os.Build.VERSION | ||
import android.os.Build.VERSION_CODES | ||
import androidx.annotation.ChecksSdkIntAtLeast | ||
import androidx.annotation.RestrictTo | ||
import java.util.concurrent.atomic.AtomicReference | ||
|
||
/** | ||
* An exposed registry instance that holds a reference to an application [AppComponentFactory] which | ||
* will be used by a test [Instrumentation]. | ||
* | ||
* [AppComponentFactoryRegistry] is a low level APIs, and is used internally by Android testing | ||
* frameworks. It is **NOT** designed for direct use by third party clients. | ||
* | ||
* TODO(b/275323224): In order to avoid breaking open source, support for | ||
* `RoboMonitoringInstrumentation` will be done later, once `AppComponentFactoryRegistry` is | ||
* available in a published `androidx.test:monitor` release. | ||
* | ||
* @hide | ||
*/ | ||
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) | ||
object AppComponentFactoryRegistry { | ||
|
||
private val factoryRef = AtomicReference<AppComponentFactory?>(/* initialValue= */ null) | ||
|
||
/** [AppComponentFactory] to be used by the current test [Instrumentation]. */ | ||
@JvmStatic | ||
var appComponentFactory: AppComponentFactory? | ||
/** | ||
* Set the [AppComponentFactory] to be used by the current test [Instrumentation]. | ||
* | ||
* @throws [IllegalArgumentException] if called from an Android version smaller than 28. | ||
*/ | ||
set(value) { | ||
check(isVersionCodeAtLeastP()) { | ||
"AppComponentFactoryRegistry is not supported on 'VERSION.SDK_INT < VERSION_CODES.P'" | ||
} | ||
factoryRef.set(value) | ||
} | ||
/** Returns the [AppComponentFactory] to be used by the current test [Instrumentation]. */ | ||
get() = factoryRef.get() | ||
|
||
/** | ||
* Returns an instance of an [Application] given a [className] using the registered | ||
* [AppComponentFactory] as factory to instantiate it. | ||
* | ||
* It may return null if: | ||
* - an instance of [AppComponentFactory] has not been registered with [appComponentFactory]. | ||
* - the registered [AppComponentFactory] can not create an instance of the requested [className]. | ||
* - the current Android [Build.VERSION.SDK_INT] is smaller than [Build.VERSION_CODES.P]. | ||
* | ||
* This function is a shorthand for getting an instance of the registered factory or null. | ||
* | ||
* @see [AppComponentFactory.instantiateApplication] | ||
*/ | ||
@JvmStatic | ||
fun instantiateApplication( | ||
cl: ClassLoader, | ||
className: String, | ||
): Application? = | ||
if (isVersionCodeAtLeastP()) { | ||
appComponentFactory?.instantiateApplication(cl, className) | ||
} else { | ||
null | ||
} | ||
|
||
/** | ||
* Returns an instance of an [Activity] given a [className] using the registered | ||
* [AppComponentFactory] as factory to instantiate it. | ||
* | ||
* It may return null if: | ||
* - an instance of [AppComponentFactory] has not been registered with [appComponentFactory]. | ||
* - the registered [AppComponentFactory] can not create an instance of the requested [className]. | ||
* - the current Android [Build.VERSION.SDK_INT] is smaller than [Build.VERSION_CODES.P]. | ||
* | ||
* This function is a shorthand for getting an instance of the registered factory or null. | ||
* | ||
* @see [AppComponentFactory.instantiateApplication] | ||
*/ | ||
@JvmStatic | ||
fun instantiateActivity( | ||
cl: ClassLoader, | ||
className: String, | ||
intent: Intent? = null, | ||
): Activity? = | ||
if (isVersionCodeAtLeastP()) { | ||
appComponentFactory?.instantiateActivity(cl, className, intent) | ||
} else { | ||
null | ||
} | ||
|
||
@ChecksSdkIntAtLeast(api = VERSION_CODES.P) | ||
private fun isVersionCodeAtLeastP(): Boolean = VERSION.SDK_INT >= VERSION_CODES.P | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.