Skip to content

Commit f792497

Browse files
authored
Update RemoteConfig testapp to use the new Realtime methods (#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.
1 parent 2c9ce4a commit f792497

File tree

2 files changed

+125
-10
lines changed

2 files changed

+125
-10
lines changed

remote_config/testapp/Assets/Firebase/Sample/RemoteConfig/UIHandler.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,34 @@ public void DisplayAllKeys() {
113113
}
114114
}
115115

116+
public void EnableAutoFetch() {
117+
DebugLog("Enabling auto-fetch:");
118+
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.OnConfigUpdateListener
119+
+= ConfigUpdateListenerEventHandler;
120+
}
121+
122+
public void DisableAutoFetch() {
123+
DebugLog("Disabling auto-fetch:");
124+
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.OnConfigUpdateListener
125+
-= ConfigUpdateListenerEventHandler;
126+
}
127+
128+
private void ConfigUpdateListenerEventHandler(
129+
object sender, Firebase.RemoteConfig.ConfigUpdateEventArgs args) {
130+
if (args.Error != Firebase.RemoteConfig.RemoteConfigError.None) {
131+
DebugLog(String.Format("Error occurred while listening: {0}", args.Error));
132+
return;
133+
}
134+
DebugLog(String.Format("Auto-fetch has received a new config. Updated keys: {0}",
135+
string.Join(", ", args.UpdatedKeys)));
136+
var info = Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.Info;
137+
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.ActivateAsync()
138+
.ContinueWithOnMainThread(task => {
139+
DebugLog(String.Format("Remote data loaded and ready (last fetch time {0}).",
140+
info.FetchTime));
141+
});
142+
}
143+
116144
// [START fetch_async]
117145
// Start a fetch request.
118146
// FetchAsync only fetches new data if the current data is older than the provided
@@ -209,6 +237,12 @@ void GUIDisplayControls() {
209237
if (GUILayout.Button("Fetch Remote Data")) {
210238
FetchDataAsync();
211239
}
240+
if (GUILayout.Button("Enable Auto-Fetch")) {
241+
EnableAutoFetch();
242+
}
243+
if (GUILayout.Button("Disable Auto-Fetch")) {
244+
DisableAutoFetch();
245+
}
212246
GUILayout.EndVertical();
213247
GUILayout.EndScrollView();
214248
}

remote_config/testapp/Assets/Firebase/Sample/RemoteConfig/UIHandlerAutomated.cs

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace Firebase.Sample.RemoteConfig {
2+
using Firebase.RemoteConfig;
23
using Firebase.Extensions;
34
using System;
5+
using System.Linq;
46
using System.Threading.Tasks;
57

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

42+
// Throw when value1 != value2.
43+
private void AssertEq<T>(string message, T value1, T value2) {
44+
if (!(object.Equals(value1, value2))) {
45+
throw new Exception(String.Format("Assertion failed ({0}): {1} != {2} ({3})",
46+
testRunner.CurrentTestDescription, value1, value2,
47+
message));
48+
}
49+
}
50+
3551
Task TestDisplayData() {
3652
DisplayData();
3753
return Task.FromResult(true);
@@ -42,18 +58,83 @@ Task TestDisplayAllKeys() {
4258
return Task.FromResult(true);
4359
}
4460

61+
private void ConfigUpdateListenerEventHandler(
62+
object sender, ConfigUpdateEventArgs args) {
63+
if (args.Error != RemoteConfigError.None) {
64+
DebugLog(String.Format("Error occurred while listening: {0}", args.Error));
65+
return;
66+
}
67+
DebugLog(String.Format("Auto-fetch has received a new config. Updated keys: {0}",
68+
string.Join(", ", args.UpdatedKeys)));
69+
var info = FirebaseRemoteConfig.DefaultInstance.Info;
70+
FirebaseRemoteConfig.DefaultInstance.ActivateAsync()
71+
.ContinueWithOnMainThread(task => {
72+
DebugLog(String.Format("Remote data loaded and ready (last fetch time {0}).",
73+
info.FetchTime));
74+
});
75+
}
76+
77+
Task TestAddOnConfigUpdateListener() {
78+
bool hasDefaultValue =
79+
FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_string").Source
80+
== ValueSource.DefaultValue;
81+
if (!hasDefaultValue) {
82+
// Some previous run of the integration test already has cached local data.
83+
// This can happen if the test is run twice in a row on the same device.
84+
DebugLog("WARNING: The device already has fetched data from a previous "
85+
+ "run of the test. To test config update listener, clear app data and "
86+
+ "re-run the test.");
87+
return Task.FromResult(true);
88+
}
89+
90+
TaskCompletionSource<bool> test_success = new TaskCompletionSource<bool>();
91+
EventHandler<ConfigUpdateEventArgs> myHandler =
92+
(object sender, ConfigUpdateEventArgs args) => {
93+
DebugLog(String.Format("Auto-fetch has received a config"));
94+
// Verify that the config update contains all expected keys.
95+
String[] expectedKeys = new String[] {
96+
"config_test_string", "config_test_int", "config_test_int", "config_test_float"
97+
};
98+
foreach (String expectedKey in expectedKeys) {
99+
if (!args.UpdatedKeys.Contains(expectedKey)) {
100+
test_success.SetException(new Exception(String.Format(
101+
"ConfigUpdate does not contain an update for key '{0}'",
102+
expectedKey)));
103+
}
104+
}
105+
test_success.SetResult(true);
106+
};
107+
DebugLog("Enabling auto-fetch:");
108+
FirebaseRemoteConfig.DefaultInstance.OnConfigUpdateListener
109+
+= myHandler;
110+
return test_success.Task;
111+
}
112+
113+
Task TestAddAndRemoveConfigUpdateListener() {
114+
// This test just verifies that listeners can be added and removed.
115+
EventHandler<ConfigUpdateEventArgs> myHandler =
116+
(object sender, ConfigUpdateEventArgs args) => {};
117+
DebugLog("Adding a config update listener");
118+
FirebaseRemoteConfig.DefaultInstance.OnConfigUpdateListener
119+
+= myHandler;
120+
DebugLog("Removing a config update listener");
121+
FirebaseRemoteConfig.DefaultInstance.OnConfigUpdateListener
122+
-= myHandler;
123+
return Task.FromResult(true);
124+
}
125+
45126
Task TestFetchData() {
127+
// Note: FetchDataAsync calls both Fetch and Activate.
46128
return FetchDataAsync().ContinueWithOnMainThread((_) => {
47-
DebugLog("TestFetchData data=" + String.Join(",", new string[] {
48-
"config_test_string: " +
49-
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_string").StringValue,
50-
"config_test_int: " +
51-
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_int").LongValue,
52-
"config_test_float: " +
53-
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_float").DoubleValue,
54-
"config_test_bool: " +
55-
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_bool").BooleanValue
56-
}));
129+
// Verify that RemoteConfig now has the expected values.
130+
AssertEq("Unexpected value for config_test_string", "Hello from the new cloud x3",
131+
FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_string").StringValue);
132+
AssertEq("Unexpected value for config_test_int", 42,
133+
FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_int").LongValue);
134+
AssertEq("Unexpected value for config_test_float", 3.14,
135+
FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_float").DoubleValue);
136+
AssertEq("Unexpected value for config_test_bool", true,
137+
FirebaseRemoteConfig.DefaultInstance.GetValue("config_test_bool").BooleanValue);
57138
});
58139
}
59140
}

0 commit comments

Comments
 (0)