Skip to content

Commit

Permalink
Add functionality to test core RC APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
karenyz committed Nov 5, 2024
1 parent 442bab2 commit ca6ac88
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ package com.google.firebase.testing.config

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.ScrollView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.tasks.Task
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.google.firebase.crashlytics.recordFatalException
import com.google.firebase.remoteconfig.ConfigUpdate
import com.google.firebase.remoteconfig.ConfigUpdateListener
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.firebase.remoteconfig.FirebaseRemoteConfigException


class MainActivity : AppCompatActivity() {

Expand All @@ -34,17 +40,63 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

remoteConfig = FirebaseRemoteConfig.getInstance()

findViewById<TextView>(R.id.greeting_text).text = getText(R.string.firebase_greetings)

// Remote Config functionality

findViewById<Button>(R.id.get_config_button).setOnClickListener {
val configMap = remoteConfig.all
var configString = "";
configString += "--------- config start ---------\n"
configMap.forEach { entry ->
val keyValueString = entry.key + ": " + entry.value.asString() + "\n"
configString += keyValueString
}
configString += "--------- config end ---------\n"
logToConsole(configString)
}

findViewById<Button>(R.id.clear_button).setOnClickListener {
findViewById<TextView>(R.id.log_window).text = ""
}

findViewById<Button>(R.id.fetch_and_activate_button).setOnClickListener {
remoteConfig.fetchAndActivate().addOnCompleteListener {didActivate: Task<Boolean> ->
Log.d("RolloutsTestApp", "FetchAndActivate completed. Did activate? : " + didActivate.result)
logToConsole("Config fetched and activated!\nDid config change: " + didActivate.result)
}
}

findViewById<Button>(R.id.activate_button).setOnClickListener {
remoteConfig.activate().addOnCompleteListener { didActivate: Task<Boolean> ->
Log.d("RolloutsTestApp", "Activate completed. Did activate? : " + didActivate.result)
logToConsole("Config activated!\nDid config change: " + didActivate.result)
}
}

findViewById<Button>(R.id.fetch_button).setOnClickListener {
remoteConfig = FirebaseRemoteConfig.getInstance()
remoteConfig.fetch(0).addOnCompleteListener {
Log.d("RolloutsTestApp", "Fetched config!")
logToConsole("Config fetched!")
}
}

remoteConfig.addOnConfigUpdateListener(object : ConfigUpdateListener {
override fun onUpdate(configUpdate: ConfigUpdate) {
remoteConfig.activate().addOnCompleteListener { didActivate: Task<Boolean> ->
Log.d("RolloutsTestApp", "Activate completed. Did activate? : " + didActivate.result)
val logString = "Real-time update!\nUpdated keys: " + configUpdate.updatedKeys.toString()
logToConsole(logString)
}
}
}

override fun onError(error: FirebaseRemoteConfigException) {
Log.w("RolloutsTestApp", "Config update error with code: " + error.code, error)
}
})

// Crashlytics integration

findViewById<Button>(R.id.jvm_crash_button).setOnClickListener {
throw RuntimeException("JVM Crash")
Expand All @@ -66,4 +118,12 @@ class MainActivity : AppCompatActivity() {
FirebaseCrashlytics.getInstance().recordException(RuntimeException("This is an non-fatal"))
}
}

fun logToConsole(message: String) {
val console = findViewById<TextView>(R.id.log_window)
console.append(message + "\n")
// scroll to bottom
val scrollView = findViewById<ScrollView>(R.id.scroll_view)
scrollView.post { scrollView.fullScroll(View.FOCUS_DOWN) }
}
}
105 changes: 88 additions & 17 deletions firebase-config/test-app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,47 +39,118 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_margin="16dp"
android:background="#DADADA">
<TextView
android:id="@+id/log_window"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:fontFamily="monospace"
android:hint="@string/app_name" />
</ScrollView>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<Button
android:id="@+id/get_config_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin = "5dp"
android:layout_weight="1"
android:text="@string/get_active_config" />

<Button
android:id="@+id/clear_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin = "5dp"
android:layout_weight="1"
android:backgroundTint="@color/cardview_dark_background"
android:text="@string/clear" />
</LinearLayout>

<Button
android:id="@+id/fetch_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:id="@+id/fetch_and_activate_button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin = "5dp"
android:layout_weight="1"
android:text="@string/fetch_config" />
android:text="@string/fetch_and_activate_config"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<Button
android:id="@+id/fetch_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin = "5dp"
android:layout_weight="1"
android:text="@string/fetch_config" />

<Button
android:id="@+id/activate_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin = "5dp"
android:layout_weight="1"
android:text="@string/activate_config" />
</LinearLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/crashlytics_label"
android:padding="3dip" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<Button
android:id="@+id/jvm_crash_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="@string/jvm_crash" />

<Button
android:id="@+id/anr_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="5dp"
android:minWidth="100dp"
android:layout_weight="1"
android:text="@string/anr" />

<Button
android:id="@+id/on_demand_fatal_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:text="@string/on_demand_fatal" />

<Button
android:id="@+id/non_fatal_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:text="@string/non_fatal" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
5 changes: 5 additions & 0 deletions firebase-config/test-app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
<resources>
<string name="app_name">Firebase Remote Config Test App</string>
<string name="firebase_greetings">Greetings from Firebase!</string>
<string name="clear">clear</string>
<string name="fetch_and_activate_config">Fetch &amp; Activate </string>
<string name="fetch_config">Fetch Config</string>
<string name="activate_config">Activate Config</string>
<string name="get_active_config">Get Active Config</string>
<string name="crashlytics_label">Crashlytics</string>
<string name="jvm_crash">JVM Crash</string>
<string name="anr">ANR</string>
<string name="on_demand_fatal">On-demand Fatal</string>
Expand Down

0 comments on commit ca6ac88

Please sign in to comment.