-
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 d9dfc9b
Showing
8 changed files
with
496 additions
and
11 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
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,55 @@ | ||
/* | ||
* 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.platform.app.AppComponentFactoryRegistry | ||
import org.junit.Rule | ||
import org.junit.rules.ExternalResource | ||
|
||
/** | ||
* JUnit [Rule] that 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 | ||
* // We use `order` to to ensure the `AppComponentFactoryRule` will always run BEFORE the | ||
* // `ActivityScenarioRule` so that your custom `AppComponentFactory` is available when the | ||
* // activity is launched. | ||
* @get:Rule(order = 1) | ||
* private val factoryRule = AppComponentFactoryRule(MyAppComponentFactory()) | ||
* | ||
* @get:Rule(order = 2) | ||
* private val activityRule = ActivityScenarioRule<MyActivity>() | ||
* ``` | ||
*/ | ||
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
111 changes: 111 additions & 0 deletions
111
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,111 @@ | ||
/* | ||
* 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 | ||
|
||
/** | ||
* 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 { | ||
|
||
/** | ||
* [AppComponentFactory] to be used by the current test [Instrumentation]. | ||
* | ||
* @throws [IllegalArgumentException] if set from an Android version smaller than 28. | ||
*/ | ||
@JvmStatic | ||
@Volatile | ||
var appComponentFactory: AppComponentFactory? = null | ||
set(value) { | ||
check(isVersionCodeAtLeastP()) { | ||
"AppComponentFactoryRegistry is not supported on 'VERSION.SDK_INT < VERSION_CODES.P'" | ||
} | ||
field = value | ||
} | ||
|
||
/** | ||
* 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
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.