Skip to content

Commit

Permalink
Update RemoteConfig testapp to use the new Realtime methods (firebase…
Browse files Browse the repository at this point in the history
…#706)

Add GUI buttons to enable and disable auto-fetch by adding and removing a ConfigUpdate listener.
Add an automated test to verify that the config update listener is called with the correct keys.
Update an existing test to assert the fetched values.
  • Loading branch information
AlmostMatt authored May 5, 2023
1 parent 2c9ce4a commit f792497
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,34 @@ public void DisplayAllKeys() {
}
}

public void EnableAutoFetch() {
DebugLog("Enabling auto-fetch:");
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.OnConfigUpdateListener
+= ConfigUpdateListenerEventHandler;
}

public void DisableAutoFetch() {
DebugLog("Disabling auto-fetch:");
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.OnConfigUpdateListener
-= ConfigUpdateListenerEventHandler;
}

private void ConfigUpdateListenerEventHandler(
object sender, Firebase.RemoteConfig.ConfigUpdateEventArgs args) {
if (args.Error != Firebase.RemoteConfig.RemoteConfigError.None) {
DebugLog(String.Format("Error occurred while listening: {0}", args.Error));
return;
}
DebugLog(String.Format("Auto-fetch has received a new config. Updated keys: {0}",
string.Join(", ", args.UpdatedKeys)));
var info = Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.Info;
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.ActivateAsync()
.ContinueWithOnMainThread(task => {
DebugLog(String.Format("Remote data loaded and ready (last fetch time {0}).",
info.FetchTime));
});
}

// [START fetch_async]
// Start a fetch request.
// FetchAsync only fetches new data if the current data is older than the provided
Expand Down Expand Up @@ -209,6 +237,12 @@ void GUIDisplayControls() {
if (GUILayout.Button("Fetch Remote Data")) {
FetchDataAsync();
}
if (GUILayout.Button("Enable Auto-Fetch")) {
EnableAutoFetch();
}
if (GUILayout.Button("Disable Auto-Fetch")) {
DisableAutoFetch();
}
GUILayout.EndVertical();
GUILayout.EndScrollView();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Firebase.Sample.RemoteConfig {
using Firebase.RemoteConfig;
using Firebase.Extensions;
using System;
using System.Linq;
using System.Threading.Tasks;

// An automated version of the UIHandler that runs tests on Firebase Remote Config.
Expand All @@ -13,6 +15,11 @@ protected override void Start() {
Func<Task>[] tests = {
TestDisplayData,
TestDisplayAllKeys,
// Skip the Realtime RC test on desktop as it is not yet supported.
#if (UNITY_IOS || UNITY_TVOS || UNITY_ANDROID) && !UNITY_EDITOR
TestAddOnConfigUpdateListener,
TestAddAndRemoveConfigUpdateListener,
#endif // !(UNITY_IOS || UNITY_TVOS || UNITY_ANDROID) || UNITY_EDITOR
TestFetchData,
};
testRunner = AutomatedTestRunner.CreateTestRunner(
Expand All @@ -32,6 +39,15 @@ protected override void Update() {
}
}

// Throw when value1 != value2.
private void AssertEq<T>(string message, T value1, T value2) {
if (!(object.Equals(value1, value2))) {
throw new Exception(String.Format("Assertion failed ({0}): {1} != {2} ({3})",
testRunner.CurrentTestDescription, value1, value2,
message));
}
}

Task TestDisplayData() {
DisplayData();
return Task.FromResult(true);
Expand All @@ -42,18 +58,83 @@ Task TestDisplayAllKeys() {
return Task.FromResult(true);
}

private void ConfigUpdateListenerEventHandler(
object sender, ConfigUpdateEventArgs args) {
if (args.Error != RemoteConfigError.None) {
DebugLog(String.Format("Error occurred while listening: {0}", args.Error));
return;
}
DebugLog(String.Format("Auto-fetch has received a new config. Updated keys: {0}",
string.Join(", ", args.UpdatedKeys)));
var info = FirebaseRemoteConfig.DefaultInstance.Info;
FirebaseRemoteConfig.DefaultInstance.ActivateAsync()
.ContinueWithOnMainThread(task => {
DebugLog(String.Format("Remote data loaded and ready (last fetch time {0}).",
info.FetchTime));
});
}

Task TestAddOnConfigUpdateListener() {
bool hasDefaultValue =
FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_string").Source
== ValueSource.DefaultValue;
if (!hasDefaultValue) {
// Some previous run of the integration test already has cached local data.
// This can happen if the test is run twice in a row on the same device.
DebugLog("WARNING: The device already has fetched data from a previous "
+ "run of the test. To test config update listener, clear app data and "
+ "re-run the test.");
return Task.FromResult(true);
}

TaskCompletionSource<bool> test_success = new TaskCompletionSource<bool>();
EventHandler<ConfigUpdateEventArgs> myHandler =
(object sender, ConfigUpdateEventArgs args) => {
DebugLog(String.Format("Auto-fetch has received a config"));
// Verify that the config update contains all expected keys.
String[] expectedKeys = new String[] {
"config_test_string", "config_test_int", "config_test_int", "config_test_float"
};
foreach (String expectedKey in expectedKeys) {
if (!args.UpdatedKeys.Contains(expectedKey)) {
test_success.SetException(new Exception(String.Format(
"ConfigUpdate does not contain an update for key '{0}'",
expectedKey)));
}
}
test_success.SetResult(true);
};
DebugLog("Enabling auto-fetch:");
FirebaseRemoteConfig.DefaultInstance.OnConfigUpdateListener
+= myHandler;
return test_success.Task;
}

Task TestAddAndRemoveConfigUpdateListener() {
// This test just verifies that listeners can be added and removed.
EventHandler<ConfigUpdateEventArgs> myHandler =
(object sender, ConfigUpdateEventArgs args) => {};
DebugLog("Adding a config update listener");
FirebaseRemoteConfig.DefaultInstance.OnConfigUpdateListener
+= myHandler;
DebugLog("Removing a config update listener");
FirebaseRemoteConfig.DefaultInstance.OnConfigUpdateListener
-= myHandler;
return Task.FromResult(true);
}

Task TestFetchData() {
// Note: FetchDataAsync calls both Fetch and Activate.
return FetchDataAsync().ContinueWithOnMainThread((_) => {
DebugLog("TestFetchData data=" + String.Join(",", new string[] {
"config_test_string: " +
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_string").StringValue,
"config_test_int: " +
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_int").LongValue,
"config_test_float: " +
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_float").DoubleValue,
"config_test_bool: " +
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_bool").BooleanValue
}));
// Verify that RemoteConfig now has the expected values.
AssertEq("Unexpected value for config_test_string", "Hello from the new cloud x3",
FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_string").StringValue);
AssertEq("Unexpected value for config_test_int", 42,
FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_int").LongValue);
AssertEq("Unexpected value for config_test_float", 3.14,
FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_float").DoubleValue);
AssertEq("Unexpected value for config_test_bool", true,
FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_bool").BooleanValue);
});
}
}
Expand Down

0 comments on commit f792497

Please sign in to comment.