Skip to content

Commit 713cacf

Browse files
committed
Merge branch 'realtime-rc-main' into realtime-rc-android
2 parents 38710e3 + ba23fae commit 713cacf

File tree

7 files changed

+119
-20
lines changed

7 files changed

+119
-20
lines changed

remote_config/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ build_flatbuffers("${flatbuffer_schemas}"
4545
# Common source files used by all platforms
4646
set(common_SRCS
4747
src/common.cc
48-
src/remote_config.cc
49-
src/config_update_listener_registration.cc)
48+
src/remote_config.cc)
5049

5150

5251
# Run gradle command to make jar
@@ -64,6 +63,7 @@ set(android_SRCS
6463

6564
# Source files used by the iOS implementation.
6665
set(ios_SRCS
66+
src/ios/config_update_listener_registration.cc
6767
src/ios/remote_config_ios.mm)
6868

6969
# Source files used by the desktop implementation.

remote_config/integration_test/src/integration_test.cc

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@
1515
#include <inttypes.h>
1616

1717
#include <algorithm>
18+
#include <chrono>
1819
#include <cstdio>
1920
#include <cstdlib>
2021
#include <cstring>
2122
#include <ctime>
23+
#include <future>
24+
#include <memory>
2225

2326
#include "app_framework.h" // NOLINT
2427
#include "firebase/app.h"
28+
#include "firebase/internal/platform.h"
2529
#include "firebase/log.h"
2630
#include "firebase/remote_config.h"
31+
#include "firebase/remote_config/config_update_listener_registration.h"
2732
#include "firebase/util.h"
2833
#include "firebase_test_framework.h" // NOLINT
2934

@@ -188,6 +193,60 @@ static Future<void> SetZeroIntervalConfigSettings(RemoteConfig* rc) {
188193

189194
// Test cases below.
190195

196+
/// This test requires to be run on a device or simulator that does not have the
197+
/// template version number stored on the disk.
198+
TEST_F(FirebaseRemoteConfigTest, TestAddOnConfigUpdateListener) {
199+
ASSERT_NE(rc_, nullptr);
200+
201+
EXPECT_TRUE(WaitForCompletion(SetDefaults(rc_), "SetDefaults"));
202+
203+
#if FIREBASE_PLATFORM_IOS
204+
205+
auto config_update_promise = std::make_shared<std::promise<void>>();
206+
207+
firebase::remote_config::ConfigUpdateListenerRegistration* registration =
208+
rc_->AddOnConfigUpdateListener(
209+
[&, config_update_promise](
210+
firebase::remote_config::ConfigUpdate&& configUpdate,
211+
firebase::remote_config::RemoteConfigError remoteConfigError) {
212+
EXPECT_EQ(configUpdate.updated_keys.size(), 5);
213+
EXPECT_TRUE(WaitForCompletion(rc_->Activate(), "Activate"));
214+
LogDebug("Real-time Config Update keys retrieved.");
215+
216+
std::map<std::string, firebase::Variant> key_values = rc_->GetAll();
217+
EXPECT_EQ(key_values.size(), 6);
218+
219+
for (auto key_valur_pair : kServerValue) {
220+
firebase::Variant k_value = key_valur_pair.value;
221+
firebase::Variant fetched_value = key_values[key_valur_pair.key];
222+
EXPECT_EQ(k_value.type(), fetched_value.type());
223+
EXPECT_EQ(k_value, fetched_value);
224+
}
225+
config_update_promise->set_value();
226+
});
227+
EXPECT_NE(nullptr, registration);
228+
auto config_update_future = config_update_promise->get_future();
229+
ASSERT_EQ(std::future_status::ready,
230+
config_update_future.wait_for(std::chrono::milliseconds(10000)));
231+
232+
registration->Remove();
233+
234+
#endif
235+
}
236+
237+
TEST_F(FirebaseRemoteConfigTest, TestRemoveConfigUpdateListener) {
238+
#if FIREBASE_PLATFORM_IOS
239+
240+
firebase::remote_config::ConfigUpdateListenerRegistration* registration =
241+
rc_->AddOnConfigUpdateListener(
242+
[](firebase::remote_config::ConfigUpdate&& configUpdate,
243+
firebase::remote_config::RemoteConfigError remoteConfigError) {});
244+
245+
registration->Remove();
246+
247+
#endif
248+
}
249+
191250
TEST_F(FirebaseRemoteConfigTest, TestInitializeAndTerminate) {
192251
// Already tested via SetUp() and TearDown().
193252
}
@@ -405,15 +464,4 @@ TEST_F(FirebaseRemoteConfigTest, TestFetchSecondsParameter) {
405464

406465
FLAKY_TEST_SECTION_END();
407466
}
408-
409-
TEST_F(FirebaseRemoteConfigTest, TestAddOnConfigUpdateListener) {
410-
EXPECT_EQ(nullptr, rc_->AddOnConfigUpdateListener(
411-
[](ConfigUpdate&&, RemoteConfigError) {}));
412-
}
413-
414-
TEST_F(FirebaseRemoteConfigTest, TestRemoveConfigUpdateListener) {
415-
EXPECT_EQ(nullptr, rc_->AddOnConfigUpdateListener(
416-
[](ConfigUpdate&&, RemoteConfigError) {}));
417-
}
418-
419467
} // namespace firebase_testapp_automated

remote_config/src/include/firebase/remote_config.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
#include "firebase/future.h"
2525
#include "firebase/internal/common.h"
2626
#include "firebase/internal/platform.h"
27-
#include "remote_config/src/common.h"
28-
#include "remote_config/src/include/firebase/remote_config/config_update_listener_registration.h"
27+
#include "remote_config/config_update_listener_registration.h"
2928
#ifndef SWIG
3029
#include "firebase/variant.h"
3130
#endif // SWIG

remote_config/src/include/firebase/remote_config/config_update_listener_registration.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ class ConfigUpdateListenerRegistration {
2929
public:
3030
ConfigUpdateListenerRegistration(std::function<void()> listener_removal_function);
3131

32+
/// @brief ConfigUpdateListenerRegistration constructor that takes in a
33+
/// function as a parameter. The parameter function connects `Remove` to the
34+
/// native platform's `Remove` method.
35+
ConfigUpdateListenerRegistration(
36+
std::function<void()> listener_removal_function);
37+
3238
~ConfigUpdateListenerRegistration();
3339

3440
/// @brief The listener being tracked by this

remote_config/src/config_update_listener_registration.cc renamed to remote_config/src/ios/config_update_listener_registration.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
namespace firebase {
1818
namespace remote_config {
1919

20+
ConfigUpdateListenerRegistration::ConfigUpdateListenerRegistration(
21+
std::function<void()> listener_removal_function)
22+
: listenerRemovalFunction(listener_removal_function) {}
23+
2024
ConfigUpdateListenerRegistration::ConfigUpdateListenerRegistration(
2125
std::function<void()> listener_removal_function)
2226
: listenerRemovalFunction(listener_removal_function) {}
2327

2428
ConfigUpdateListenerRegistration::~ConfigUpdateListenerRegistration() {}
2529

26-
// TODO: almostmatt, merge this with quan's ios changes. also see about whether to make this no-op after first call
2730
void ConfigUpdateListenerRegistration::Remove() {
2831
this->listenerRemovalFunction();
2932
}

remote_config/src/ios/remote_config_ios.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#ifndef FIREBASE_REMOTE_CONFIG_SRC_IOS_REMOTE_CONFIG_IOS_H_
1616
#define FIREBASE_REMOTE_CONFIG_SRC_IOS_REMOTE_CONFIG_IOS_H_
1717

18+
#include <functional>
19+
1820
#include "firebase/app.h"
1921
#include "app/memory/unique_ptr.h"
2022
#include "app/src/reference_counted_future_impl.h"
@@ -86,8 +88,8 @@ class RemoteConfigInternal {
8688
const ConfigInfo GetInfo() const;
8789

8890
ConfigUpdateListenerRegistration* AddOnConfigUpdateListener(
89-
std::function<void(ConfigUpdate&&, RemoteConfigError)>
90-
config_update_listener);
91+
std::function<void(ConfigUpdate&&, RemoteConfigError)>
92+
config_update_listener);
9193

9294
bool Initialized() const;
9395

remote_config/src/ios/remote_config_ios.mm

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "app/src/log.h"
2424
#include "app/src/time.h"
2525
#include "remote_config/src/common.h"
26+
#include "remote_config/src/include/firebase/remote_config.h"
2627

2728
namespace firebase {
2829
namespace remote_config {
@@ -173,6 +174,30 @@ static void GetInfoFromFIRRemoteConfig(FIRRemoteConfig *rc_instance, ConfigInfo
173174
}
174175
}
175176

177+
static RemoteConfigError ConvertFIRRemoteConfigUpdateError(NSError *error) {
178+
switch(error.code) {
179+
case FIRRemoteConfigUpdateErrorStreamError:
180+
return kRemoteConfigErrorConfigUpdateStreamError;
181+
case FIRRemoteConfigUpdateErrorNotFetched:
182+
return kRemoteConfigErrorConfigUpdateNotFetched;
183+
case FIRRemoteConfigUpdateErrorMessageInvalid:
184+
return kRemoteConfigErrorConfigUpdateMessageInvalid;
185+
case FIRRemoteConfigUpdateErrorUnavailable:
186+
return kRemoteConfigErrorConfigUpdateUnavailable;
187+
default:
188+
return kRemoteConfigErrorUnimplemented;
189+
}
190+
}
191+
192+
static ConfigUpdate ConvertConfigUpdateKeys(NSSet<NSString *> *keys) {
193+
ConfigUpdate configUpdate;
194+
195+
for (NSString *key in keys) {
196+
configUpdate.updated_keys.push_back(util::NSStringToString(key).c_str());
197+
}
198+
return configUpdate;
199+
}
200+
176201
namespace internal {
177202
RemoteConfigInternal::RemoteConfigInternal(const firebase::App &app)
178203
: app_(app), future_impl_(kRemoteConfigFnCount) {
@@ -492,8 +517,24 @@ static Variant FIRValueToVariant(FIRRemoteConfigValue *in_value, ValueInfo *valu
492517

493518
ConfigUpdateListenerRegistration* RemoteConfigInternal::AddOnConfigUpdateListener(
494519
std::function<void(ConfigUpdate&&, RemoteConfigError)>
495-
config_update_listener) {
496-
return nullptr;
520+
config_update_listener) {
521+
FIRConfigUpdateListenerRegistration *registration;
522+
registration = [impl() addOnConfigUpdateListener: ^(FIRRemoteConfigUpdate *_Nullable update,
523+
NSError *_Nullable error) {
524+
if (error) {
525+
config_update_listener({}, ConvertFIRRemoteConfigUpdateError(error));
526+
return;
527+
}
528+
529+
config_update_listener(ConvertConfigUpdateKeys(update.updatedKeys), kRemoteConfigErrorNone);
530+
}];
531+
532+
ConfigUpdateListenerRegistration *registrationWrapper =
533+
new ConfigUpdateListenerRegistration([registration]() {
534+
[registration remove];
535+
});
536+
537+
return registrationWrapper;
497538
}
498539
} // namespace internal
499540
} // namespace remote_config

0 commit comments

Comments
 (0)