2525
2626package com.shopify.testify.ext
2727
28+ import android.content.Context
2829import android.graphics.Color
2930import android.os.Bundle
3031import android.view.View
@@ -34,6 +35,8 @@ import androidx.annotation.VisibleForTesting
3435import androidx.appcompat.app.AppCompatActivity
3536import androidx.test.rule.ActivityTestRule
3637import com.shopify.testify.ScreenshotRule
38+ import com.shopify.testify.resources.TestifyResourcesOverride
39+ import java.util.Locale
3740
3841/* *
3942 * The TestHarnessActivity is used as scaffolding for testing arbitrary views.
@@ -42,7 +45,7 @@ import com.shopify.testify.ScreenshotRule
4245 * This empty activity can be used as a generic container for testing your custom [View] classes.
4346 */
4447@VisibleForTesting
45- open class TestHarnessActivity : AppCompatActivity () {
48+ open class TestHarnessActivity : AppCompatActivity (), TestifyResourcesOverride {
4649
4750 override fun onCreate (savedInstanceState : Bundle ? ) {
4851 super .onCreate(savedInstanceState)
@@ -53,11 +56,23 @@ open class TestHarnessActivity : AppCompatActivity() {
5356 id = R .id.harness_root
5457 })
5558
56- if ( intent?.hasExtra (EXTRA_TESTIFY_HARNESS_ACTIVITY_TITLE ) == true ) {
57- title = intent.getStringExtra( EXTRA_TESTIFY_HARNESS_ACTIVITY_TITLE )
59+ intent.getStringExtra (EXTRA_TESTIFY_HARNESS_ACTIVITY_TITLE )?. let {
60+ title = it
5861 }
5962 }
6063
64+ /* *
65+ *
66+ * Starting in Android API Version 24 (Nougat), the proper way to dynamically alter an Activity's
67+ * resources and locale is to wrap the base [Context] in [AppCompatActivity.attachBaseContext]
68+ * with a Context that has been updated with a new [Locale]. Testify provides a helper interface,
69+ * [TestifyResourcesOverride] which provides a Context extension method, [TestifyResourcesOverride.wrap].
70+ *
71+ */
72+ override fun attachBaseContext (newBase : Context ? ) {
73+ super .attachBaseContext(newBase?.wrap())
74+ }
75+
6176 companion object {
6277 const val EXTRA_TESTIFY_HARNESS_ACTIVITY_TITLE = " extra_title"
6378 }
0 commit comments