From 70fb9afe02eba1d9aafe7518afa4925773b4b8e8 Mon Sep 17 00:00:00 2001 From: Salakar Date: Wed, 20 Feb 2019 01:52:05 +0000 Subject: [PATCH] [utils] add support for `isRunningInTestLab` flag --- .../utils/ReactNativeFirebaseUtilsModule.java | 31 +++++++++++++------ packages/utils/e2e/utils.e2e.js | 20 +++--------- packages/utils/lib/index.d.ts | 16 +++++----- packages/utils/lib/index.js | 7 ++++- packages/utils/lib/index.js.flow | 7 ++++- 5 files changed, 47 insertions(+), 34 deletions(-) diff --git a/packages/utils/android/src/main/java/io/invertase/firebase/utils/ReactNativeFirebaseUtilsModule.java b/packages/utils/android/src/main/java/io/invertase/firebase/utils/ReactNativeFirebaseUtilsModule.java index bdb6023e..ee01765d 100644 --- a/packages/utils/android/src/main/java/io/invertase/firebase/utils/ReactNativeFirebaseUtilsModule.java +++ b/packages/utils/android/src/main/java/io/invertase/firebase/utils/ReactNativeFirebaseUtilsModule.java @@ -19,6 +19,7 @@ package io.invertase.firebase.utils; import android.app.Activity; import android.content.IntentSender; +import android.provider.Settings; import android.util.Log; import com.facebook.react.bridge.Arguments; @@ -32,16 +33,32 @@ import com.google.android.gms.common.GoogleApiAvailability; import java.util.HashMap; import java.util.Map; +import io.invertase.firebase.app.ReactNativeFirebaseApp; import io.invertase.firebase.common.ReactNativeFirebaseModule; -import io.invertase.firebase.common.SharedUtils; public class ReactNativeFirebaseUtilsModule extends ReactNativeFirebaseModule { private static final String TAG = "Utils"; + private static final String FIREBASE_TEST_LAB = "firebase.test.lab"; + ReactNativeFirebaseUtilsModule(ReactApplicationContext reactContext) { super(reactContext, TAG); } + /** + * Is this app running in Firebase Test Lab + * + * @return Boolean + */ + private static Boolean isRunningInTestLab() { + String testLabSetting = Settings.System.getString( + ReactNativeFirebaseApp.getApplicationContext().getContentResolver(), + FIREBASE_TEST_LAB + ); + + return "true".equals(testLabSetting); + } + @ReactMethod public void androidGetPlayServicesStatus(Promise promise) { promise.resolve(getPlayServicesStatusMap()); @@ -53,16 +70,12 @@ public class ReactNativeFirebaseUtilsModule extends ReactNativeFirebaseModule { @ReactMethod public void androidPromptForPlayServices() { int status = isGooglePlayServicesAvailable(); + GoogleApiAvailability gapi = GoogleApiAvailability.getInstance(); - if ( - status != ConnectionResult.SUCCESS && - GoogleApiAvailability.getInstance().isUserResolvableError(status) - ) { + if (status != ConnectionResult.SUCCESS && gapi.isUserResolvableError(status)) { Activity activity = getActivity(); if (activity != null) { - GoogleApiAvailability.getInstance() - .getErrorDialog(activity, status, status) - .show(); + gapi.getErrorDialog(activity, status, status).show(); } } } @@ -129,8 +142,8 @@ public class ReactNativeFirebaseUtilsModule extends ReactNativeFirebaseModule { @Override public Map getConstants() { Map constants = new HashMap<>(); + constants.put("isRunningInTestLab", isRunningInTestLab()); constants.put("androidPlayServices", getPlayServicesStatusMap()); - constants.put("isFirebaseTestLab", SharedUtils.isFirebaseTestLab()); return constants; } } diff --git a/packages/utils/e2e/utils.e2e.js b/packages/utils/e2e/utils.e2e.js index e11946f2..f90432a3 100644 --- a/packages/utils/e2e/utils.e2e.js +++ b/packages/utils/e2e/utils.e2e.js @@ -22,23 +22,11 @@ describe('utils()', () => { should.exist(app.utils); app.utils().app.should.equal(app); }); + }); - // removing as pending if module.options.hasMultiAppSupport = true - xit('supports multiple apps', async () => { - firebase.utils().app.name.should.equal('[DEFAULT]'); - - firebase - .utils(firebase.app('secondaryFromNative')) - .app.name.should.equal('secondaryFromNative'); - - firebase - .app('secondaryFromNative') - .utils() - .app.name.should.equal('secondaryFromNative'); + describe('isRunningInTestLab', () => { + it('returns true or false', () => { + should.equal(firebase.utils().isRunningInTestLab, false); }); }); - - describe('aMethod()', () => { - // TODO - }); }); diff --git a/packages/utils/lib/index.d.ts b/packages/utils/lib/index.d.ts index d0ba8cb8..4fa9d944 100644 --- a/packages/utils/lib/index.d.ts +++ b/packages/utils/lib/index.d.ts @@ -30,7 +30,12 @@ export namespace Utils { export interface Statics {} export interface Module extends ReactNativeFirebaseModule { - + /** + * Returns true if this app is running inside a Firebase Test Lab environment. Always returns false on iOS. + * + * @android + */ + isRunningInTestLab: boolean; } } @@ -68,12 +73,9 @@ declare module '@react-native-firebase/utils' { declare module '@react-native-firebase/app-types' { interface ReactNativeFirebaseNamespace { /** - * Utils integrates across Firebase features and provides - * you with unlimited reporting for up to 500 distinct events - * that you can define using the Firebase SDK. Utils reports - * help you understand clearly how your users behave, which enables - * you to make informed decisions regarding app marketing and - * performance optimizations. + * Utils provides a collection of utilities to aid in using Firebase + * and related services inside React Native, e.g. Test Lab helpers + * and Google Play Services version helpers. */ utils: ReactNativeFirebaseModuleAndStatics< Utils.Module, diff --git a/packages/utils/lib/index.js b/packages/utils/lib/index.js index 5211ca72..bff6e13f 100644 --- a/packages/utils/lib/index.js +++ b/packages/utils/lib/index.js @@ -21,6 +21,8 @@ import { getFirebaseRoot, } from '@react-native-firebase/app/lib/internal'; +import { isIOS } from '@react-native-firebase/common'; + import version from './version'; const statics = {}; @@ -30,7 +32,10 @@ const namespace = 'utils'; const nativeModuleName = 'RNFBUtilsModule'; class FirebaseUtilsModule extends FirebaseModule { - + get isRunningInTestLab() { + if (isIOS) return false; + return this.native.isRunningInTestLab; + } } // import { SDK_VERSION } from '@react-native-firebase/utils'; diff --git a/packages/utils/lib/index.js.flow b/packages/utils/lib/index.js.flow index 11974afc..9addba9a 100644 --- a/packages/utils/lib/index.js.flow +++ b/packages/utils/lib/index.js.flow @@ -21,7 +21,12 @@ import type { ReactNativeFirebaseModule } from '@react-native-firebase/app-types export interface Statics {} export interface Module extends ReactNativeFirebaseModule { - + /** + * Returns true if this app is running inside a Firebase Test Lab environment. Always returns false on iOS. + * + * @android + */ + isRunningInTestLab: boolean; } declare module '@react-native-firebase/utils' {