Utilizing Firebase Remote Configs within Flutter Integration Tests
Firebase Remote Config, a service provided by Google Firebase, empowers developers and product owners to remotely manage and update app configurations without necessitating new app releases or updates.
Examples of its use cases include:
- Customizing messages and titles displayed on the Onboarding Screen.
- Enabling or disabling specific app features on demand.
- Controlling the display of onboarding screens based on predefined flags.
- Updating menu names dynamically for usability assessments.
In all the above scenarios, traditionally, manual code changes, deployment, and app publishing are required. However, leveraging Firebase Remote Config allows for seamless runtime updates without altering the codebase or publishing a new app version. By fetching values from Firebase Remote Config at runtime, these configurations can be updated within the app without any code modifications or the need for an updated release.
Note :
This article delves into the utilization of Firebase Remote Config within integration tests. For guidance on setting up Firebase Remote Config in your project, I recommend checking out the following resources:
For our Integration test scenario, let’s consider fetching menu names from Firebase Remote Config and utilizing them within the integration test for locator purposes.
Step 1: Initializing Firebase remote Config to Fetch Value
Future<String> fetchRemoteConfigValues(String keyName) async {
await Firebase.initializeApp();
var remoteConfig = FirebaseRemoteConfig.instance;
try {
await remoteConfig.setConfigSettings(
RemoteConfigSettings(
fetchTimeout: const Duration(seconds: 30),
minimumFetchInterval: const Duration(hours: 6),
),
);
await remoteConfig.fetchAndActivate();
final value = remoteConfig.getString(keyName);
return value;
} catch (e) {
print('Error fetching remote config: $e');
}
return null;
}
Explanation:
- Firebase Initialization: Initializes the Firebase app using
Firebase.initializeApp()
. - Remote Config Initialization: Retrieves the instance of
FirebaseRemoteConfig
. - Config Settings Setup:
- Sets configuration settings for Remote Config:
fetchTimeout
: Defines the duration the method will wait for a fetch operation (30 seconds in this case).minimumFetchInterval
: Specifies the minimum interval between fetches (set to 6 hours here).
4. Fetch and Activation:
- Invokes
remoteConfig.fetchAndActivate()
, initiating a fetch request to retrieve updated configuration values from Firebase. - Activates the fetched configurations, making them available for use within the test.
5. Value Retrieval:
- Retrieves the configuration value associated with the provided
keyName
usingremoteConfig.getString(keyName)
. - Returns the fetched value.
Step 2: Calling this in the SetupAll method of the Test
Suppose the key used to fetch the menu name is ‘home_menu_key’. We’ll retrieve its value within the setUpAll
function and employ that value in our test cases.
setUpAll(() async {
final menu_Name = await fetchRemoteConfigValues('home_menu_key');
});
Step 3: Utilizing Value in Integration Test
Next, we’ll employ the menuName
variable in our tests. The following step involves clicking on the home menu.
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_remote_config/firebase_remote_config.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
void main() {
final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.fullyLive;
group('Home Menu Tests:', () {
String menu_Name = '';
setUpAll(() async {
menu_Name = await fetchRemoteConfigValues('home_menu_key');
});
testWidgets(
'validate Home Menu Navigation',
(WidgetTester tester) async {
await tester.pumpWidget(const MyApp());
await tester.pump(const Duration(seconds: 2));
await tester.tap(find.text(menu_Name));
},
skip: false,
timeout: const Timeout(Duration(minutes: 5)),
);
});
}
Future<String> fetchRemoteConfigValues(String keyName) async {
await Firebase.initializeApp();
var remoteConfig = FirebaseRemoteConfig.instance;
try {
await remoteConfig.setConfigSettings(
RemoteConfigSettings(
fetchTimeout: const Duration(seconds: 30),
minimumFetchInterval: const Duration(hours: 6),
),
);
await remoteConfig.fetchAndActivate();
final value = remoteConfig.getString(keyName);
return value;
} catch (e) {
print('Error fetching remote config: $e');
}
}
In conclusion, Firebase Remote Config proves to be a powerful tool for dynamic configuration management in Flutter apps, enabling seamless updates and adaptations without necessitating code modifications or app republishing. Its integration into integration tests allows for real-time validation of configurations, ensuring robustness and flexibility within the app’s functionalities.