11namespace 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