|
15 | 15 | #include <inttypes.h> |
16 | 16 |
|
17 | 17 | #include <algorithm> |
| 18 | +#include <chrono> |
18 | 19 | #include <cstdio> |
19 | 20 | #include <cstdlib> |
20 | 21 | #include <cstring> |
21 | 22 | #include <ctime> |
| 23 | +#include <future> |
| 24 | +#include <memory> |
22 | 25 |
|
23 | 26 | #include "app_framework.h" // NOLINT |
24 | 27 | #include "firebase/app.h" |
| 28 | +#include "firebase/internal/platform.h" |
25 | 29 | #include "firebase/log.h" |
26 | 30 | #include "firebase/remote_config.h" |
| 31 | +#include "firebase/remote_config/config_update_listener_registration.h" |
27 | 32 | #include "firebase/util.h" |
28 | 33 | #include "firebase_test_framework.h" // NOLINT |
29 | 34 |
|
@@ -192,6 +197,8 @@ TEST_F(FirebaseRemoteConfigTest, TestInitializeAndTerminate) { |
192 | 197 | // Already tested via SetUp() and TearDown(). |
193 | 198 | } |
194 | 199 |
|
| 200 | +/// This test requires to be run on a device or simulator that does not have a |
| 201 | +/// newer version of the config saved on disk from a previous test run. |
195 | 202 | TEST_F(FirebaseRemoteConfigTest, TestSetDefault) { |
196 | 203 | ASSERT_NE(rc_, nullptr); |
197 | 204 |
|
@@ -254,6 +261,93 @@ TEST_F(FirebaseRemoteConfigTest, TestSetDefault) { |
254 | 261 | } |
255 | 262 | } |
256 | 263 |
|
| 264 | +/// This test requires to be run on a device or simulator that does not have the |
| 265 | +/// template version number stored on the disk or auto-fetch will be skipped. |
| 266 | +TEST_F(FirebaseRemoteConfigTest, TestAddOnConfigUpdateListener) { |
| 267 | + ASSERT_NE(rc_, nullptr); |
| 268 | + |
| 269 | + // Check if the config has default values. If not, we have cached data |
| 270 | + // from a previous test run, and auto-fetch will not happen. |
| 271 | + EXPECT_TRUE(WaitForCompletion(SetDefaults(rc_), "SetDefaults")); |
| 272 | + bool validated_defaults = true; |
| 273 | + firebase::remote_config::ValueInfo value_info; |
| 274 | + bool bool_value = rc_->GetBoolean("TestBoolean", &value_info); |
| 275 | + bool has_cached_data = |
| 276 | + value_info.source != firebase::remote_config::kValueSourceDefaultValue; |
| 277 | + |
| 278 | + if (has_cached_data) { |
| 279 | + LogWarning( |
| 280 | + "Can't validate defaults, they've been overridden by server values.\n" |
| 281 | +#if defined(__ANDROID__) |
| 282 | + "Delete the app's data and run this test again to test\n" |
| 283 | + " AddOnConfigUpdateListener: adb shell pm clear [bundle ID]" |
| 284 | +#elif defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE |
| 285 | + "Uninstall and re-install the app and run this again to test " |
| 286 | + "AddOnConfigUpdateListener." |
| 287 | +#else // Desktop |
| 288 | + "Delete the Remote Config cache and run this test again to test " |
| 289 | + "AddOnConfigUpdateListener:\n" |
| 290 | +#if defined(_WIN32) |
| 291 | + " del remote_config_data" |
| 292 | +#else |
| 293 | + " rm remote_config_data" |
| 294 | +#endif // defined(_WIN32) |
| 295 | +#endif // defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE |
| 296 | + ); |
| 297 | + } |
| 298 | +// Realtime RC is not yet supported on desktop. |
| 299 | +#if FIREBASE_PLATFORM_DESKTOP |
| 300 | + rc_->AddOnConfigUpdateListener( |
| 301 | + [](firebase::remote_config::ConfigUpdate&&, |
| 302 | + firebase::remote_config::RemoteConfigError) {}); |
| 303 | +#else |
| 304 | + auto config_update_promise = std::make_shared<std::promise<void> >(); |
| 305 | + |
| 306 | + firebase::remote_config::ConfigUpdateListenerRegistration registration = |
| 307 | + rc_->AddOnConfigUpdateListener( |
| 308 | + [&, config_update_promise]( |
| 309 | + firebase::remote_config::ConfigUpdate&& configUpdate, |
| 310 | + firebase::remote_config::RemoteConfigError remoteConfigError) { |
| 311 | + EXPECT_EQ(configUpdate.updated_keys.size(), 5); |
| 312 | + config_update_promise->set_value(); |
| 313 | + }); |
| 314 | + if (!has_cached_data) { |
| 315 | + auto config_update_future = config_update_promise->get_future(); |
| 316 | + ASSERT_EQ(std::future_status::ready, |
| 317 | + config_update_future.wait_for(std::chrono::milliseconds(10000))); |
| 318 | + |
| 319 | + // On Android WaitForCompletion must be called from the main thread, |
| 320 | + // so Activate is called here outside of the listener. |
| 321 | + EXPECT_TRUE(WaitForCompletion(rc_->Activate(), "Activate")); |
| 322 | + LogDebug("Real-time Config Update keys retrieved."); |
| 323 | + |
| 324 | + std::map<std::string, firebase::Variant> key_values = rc_->GetAll(); |
| 325 | + EXPECT_EQ(key_values.size(), 6); |
| 326 | + |
| 327 | + for (auto key_valur_pair : kServerValue) { |
| 328 | + firebase::Variant k_value = key_valur_pair.value; |
| 329 | + firebase::Variant fetched_value = key_values[key_valur_pair.key]; |
| 330 | + EXPECT_EQ(k_value.type(), fetched_value.type()); |
| 331 | + EXPECT_EQ(k_value, fetched_value); |
| 332 | + } |
| 333 | + registration.Remove(); |
| 334 | + } |
| 335 | +#endif // !FIREBASE_PLATFORM_DESKTOP |
| 336 | +} |
| 337 | + |
| 338 | +TEST_F(FirebaseRemoteConfigTest, TestRemoveConfigUpdateListener) { |
| 339 | +#if !FIREBASE_PLATFORM_DESKTOP |
| 340 | + |
| 341 | + firebase::remote_config::ConfigUpdateListenerRegistration registration = |
| 342 | + rc_->AddOnConfigUpdateListener( |
| 343 | + [](firebase::remote_config::ConfigUpdate&& configUpdate, |
| 344 | + firebase::remote_config::RemoteConfigError remoteConfigError) {}); |
| 345 | + |
| 346 | + registration.Remove(); |
| 347 | + |
| 348 | +#endif // !FIREBASE_PLATFORM_DESKTOP |
| 349 | +} |
| 350 | + |
257 | 351 | TEST_F(FirebaseRemoteConfigTest, TestGetKeys) { |
258 | 352 | ASSERT_NE(rc_, nullptr); |
259 | 353 |
|
@@ -405,5 +499,4 @@ TEST_F(FirebaseRemoteConfigTest, TestFetchSecondsParameter) { |
405 | 499 |
|
406 | 500 | FLAKY_TEST_SECTION_END(); |
407 | 501 | } |
408 | | - |
409 | 502 | } // namespace firebase_testapp_automated |
0 commit comments