Skip to content

Commit ca6ac88

Browse files
committed
Add functionality to test core RC APIs
1 parent 442bab2 commit ca6ac88

File tree

3 files changed

+156
-20
lines changed

3 files changed

+156
-20
lines changed

firebase-config/test-app/src/main/kotlin/com/google/firebase/testing/config/MainActivity.kt

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@ package com.google.firebase.testing.config
1818

1919
import android.os.Bundle
2020
import android.util.Log
21+
import android.view.View
2122
import android.widget.Button
23+
import android.widget.ScrollView
2224
import android.widget.TextView
2325
import androidx.appcompat.app.AppCompatActivity
2426
import com.google.android.gms.tasks.Task
2527
import com.google.firebase.crashlytics.FirebaseCrashlytics
2628
import com.google.firebase.crashlytics.recordFatalException
29+
import com.google.firebase.remoteconfig.ConfigUpdate
30+
import com.google.firebase.remoteconfig.ConfigUpdateListener
2731
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
32+
import com.google.firebase.remoteconfig.FirebaseRemoteConfigException
33+
2834

2935
class MainActivity : AppCompatActivity() {
3036

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

43+
remoteConfig = FirebaseRemoteConfig.getInstance()
44+
3745
findViewById<TextView>(R.id.greeting_text).text = getText(R.string.firebase_greetings)
3846

47+
// Remote Config functionality
48+
49+
findViewById<Button>(R.id.get_config_button).setOnClickListener {
50+
val configMap = remoteConfig.all
51+
var configString = "";
52+
configString += "--------- config start ---------\n"
53+
configMap.forEach { entry ->
54+
val keyValueString = entry.key + ": " + entry.value.asString() + "\n"
55+
configString += keyValueString
56+
}
57+
configString += "--------- config end ---------\n"
58+
logToConsole(configString)
59+
}
60+
61+
findViewById<Button>(R.id.clear_button).setOnClickListener {
62+
findViewById<TextView>(R.id.log_window).text = ""
63+
}
64+
65+
findViewById<Button>(R.id.fetch_and_activate_button).setOnClickListener {
66+
remoteConfig.fetchAndActivate().addOnCompleteListener {didActivate: Task<Boolean> ->
67+
Log.d("RolloutsTestApp", "FetchAndActivate completed. Did activate? : " + didActivate.result)
68+
logToConsole("Config fetched and activated!\nDid config change: " + didActivate.result)
69+
}
70+
}
71+
72+
findViewById<Button>(R.id.activate_button).setOnClickListener {
73+
remoteConfig.activate().addOnCompleteListener { didActivate: Task<Boolean> ->
74+
Log.d("RolloutsTestApp", "Activate completed. Did activate? : " + didActivate.result)
75+
logToConsole("Config activated!\nDid config change: " + didActivate.result)
76+
}
77+
}
78+
3979
findViewById<Button>(R.id.fetch_button).setOnClickListener {
40-
remoteConfig = FirebaseRemoteConfig.getInstance()
4180
remoteConfig.fetch(0).addOnCompleteListener {
4281
Log.d("RolloutsTestApp", "Fetched config!")
82+
logToConsole("Config fetched!")
83+
}
84+
}
85+
86+
remoteConfig.addOnConfigUpdateListener(object : ConfigUpdateListener {
87+
override fun onUpdate(configUpdate: ConfigUpdate) {
4388
remoteConfig.activate().addOnCompleteListener { didActivate: Task<Boolean> ->
44-
Log.d("RolloutsTestApp", "Activate completed. Did activate? : " + didActivate.result)
89+
val logString = "Real-time update!\nUpdated keys: " + configUpdate.updatedKeys.toString()
90+
logToConsole(logString)
4591
}
4692
}
47-
}
93+
94+
override fun onError(error: FirebaseRemoteConfigException) {
95+
Log.w("RolloutsTestApp", "Config update error with code: " + error.code, error)
96+
}
97+
})
98+
99+
// Crashlytics integration
48100

49101
findViewById<Button>(R.id.jvm_crash_button).setOnClickListener {
50102
throw RuntimeException("JVM Crash")
@@ -66,4 +118,12 @@ class MainActivity : AppCompatActivity() {
66118
FirebaseCrashlytics.getInstance().recordException(RuntimeException("This is an non-fatal"))
67119
}
68120
}
121+
122+
fun logToConsole(message: String) {
123+
val console = findViewById<TextView>(R.id.log_window)
124+
console.append(message + "\n")
125+
// scroll to bottom
126+
val scrollView = findViewById<ScrollView>(R.id.scroll_view)
127+
scrollView.post { scrollView.fullScroll(View.FOCUS_DOWN) }
128+
}
69129
}

firebase-config/test-app/src/main/res/layout/activity_main.xml

Lines changed: 88 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,47 +39,118 @@
3939
app:layout_constraintStart_toStartOf="parent"
4040
app:layout_constraintTop_toTopOf="parent">
4141

42+
<ScrollView
43+
android:id="@+id/scroll_view"
44+
android:layout_width="match_parent"
45+
android:layout_height="350dp"
46+
android:layout_margin="16dp"
47+
android:background="#DADADA">
48+
<TextView
49+
android:id="@+id/log_window"
50+
android:layout_width="match_parent"
51+
android:layout_height="wrap_content"
52+
android:layout_margin="16dp"
53+
android:fontFamily="monospace"
54+
android:hint="@string/app_name" />
55+
</ScrollView>
56+
57+
<LinearLayout
58+
android:layout_width="match_parent"
59+
android:layout_height="match_parent"
60+
android:orientation="horizontal">
61+
62+
<Button
63+
android:id="@+id/get_config_button"
64+
android:layout_width="wrap_content"
65+
android:layout_height="wrap_content"
66+
android:layout_margin = "5dp"
67+
android:layout_weight="1"
68+
android:text="@string/get_active_config" />
69+
70+
<Button
71+
android:id="@+id/clear_button"
72+
android:layout_width="wrap_content"
73+
android:layout_height="wrap_content"
74+
android:layout_margin = "5dp"
75+
android:layout_weight="1"
76+
android:backgroundTint="@color/cardview_dark_background"
77+
android:text="@string/clear" />
78+
</LinearLayout>
79+
4280
<Button
43-
android:id="@+id/fetch_button"
44-
android:layout_width="wrap_content"
45-
android:layout_height="wrap_content"
46-
android:layout_marginStart="5dp"
47-
android:layout_marginLeft="5dp"
48-
android:layout_marginEnd="5dp"
49-
android:layout_marginRight="5dp"
81+
android:id="@+id/fetch_and_activate_button"
82+
android:layout_width="match_parent"
83+
android:layout_height="0dp"
84+
android:layout_margin = "5dp"
5085
android:layout_weight="1"
51-
android:text="@string/fetch_config" />
86+
android:text="@string/fetch_and_activate_config"/>
87+
88+
<LinearLayout
89+
android:layout_width="match_parent"
90+
android:layout_height="match_parent"
91+
android:orientation="horizontal">
92+
93+
<Button
94+
android:id="@+id/fetch_button"
95+
android:layout_width="wrap_content"
96+
android:layout_height="wrap_content"
97+
android:layout_margin = "5dp"
98+
android:layout_weight="1"
99+
android:text="@string/fetch_config" />
100+
101+
<Button
102+
android:id="@+id/activate_button"
103+
android:layout_width="wrap_content"
104+
android:layout_height="wrap_content"
105+
android:layout_margin = "5dp"
106+
android:layout_weight="1"
107+
android:text="@string/activate_config" />
108+
</LinearLayout>
109+
110+
<TextView
111+
android:layout_width="match_parent"
112+
android:layout_height="match_parent"
113+
android:text="@string/crashlytics_label"
114+
android:padding="3dip" />
115+
116+
<LinearLayout
117+
android:layout_width="match_parent"
118+
android:layout_height="match_parent"
119+
android:orientation="horizontal">
52120

53121
<Button
54122
android:id="@+id/jvm_crash_button"
55123
android:layout_width="wrap_content"
56-
android:layout_height="wrap_content"
124+
android:layout_height="match_parent"
57125
android:layout_marginStart="5dp"
58-
android:layout_marginLeft="5dp"
59126
android:layout_marginEnd="5dp"
60-
android:layout_marginRight="5dp"
61127
android:layout_weight="1"
62128
android:text="@string/jvm_crash" />
63129

64130
<Button
65131
android:id="@+id/anr_button"
66-
android:layout_width="match_parent"
67-
android:layout_height="wrap_content"
132+
android:layout_width="wrap_content"
133+
android:layout_height="match_parent"
134+
android:layout_marginEnd="5dp"
135+
android:minWidth="100dp"
68136
android:layout_weight="1"
69137
android:text="@string/anr" />
70138

71139
<Button
72140
android:id="@+id/on_demand_fatal_button"
73-
android:layout_width="match_parent"
74-
android:layout_height="wrap_content"
141+
android:layout_width="wrap_content"
142+
android:layout_height="match_parent"
143+
android:layout_marginEnd="5dp"
75144
android:layout_weight="1"
76145
android:text="@string/on_demand_fatal" />
77146

78147
<Button
79148
android:id="@+id/non_fatal_button"
80-
android:layout_width="match_parent"
81-
android:layout_height="wrap_content"
149+
android:layout_width="wrap_content"
150+
android:layout_height="match_parent"
151+
android:layout_marginEnd="5dp"
82152
android:layout_weight="1"
83153
android:text="@string/non_fatal" />
154+
</LinearLayout>
84155
</LinearLayout>
85156
</androidx.constraintlayout.widget.ConstraintLayout>

firebase-config/test-app/src/main/res/values/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
<resources>
1818
<string name="app_name">Firebase Remote Config Test App</string>
1919
<string name="firebase_greetings">Greetings from Firebase!</string>
20+
<string name="clear">clear</string>
21+
<string name="fetch_and_activate_config">Fetch &amp; Activate </string>
2022
<string name="fetch_config">Fetch Config</string>
23+
<string name="activate_config">Activate Config</string>
24+
<string name="get_active_config">Get Active Config</string>
25+
<string name="crashlytics_label">Crashlytics</string>
2126
<string name="jvm_crash">JVM Crash</string>
2227
<string name="anr">ANR</string>
2328
<string name="on_demand_fatal">On-demand Fatal</string>

0 commit comments

Comments
 (0)