Skip to content

Commit ae3d689

Browse files
authored
Check Google Play services version in Analytics test (#1235)
* Add code to get Google Play services version. * Add check for gmscore version to Analytics GetSessionID test. * Log event before GetSessionId.
1 parent 2250ad1 commit ae3d689

File tree

6 files changed

+72
-12
lines changed

6 files changed

+72
-12
lines changed

analytics/integration_test/src/integration_test.cc

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,8 @@ TEST_F(FirebaseAnalyticsTest, TestGetAnalyticsInstanceID) {
107107
}
108108

109109
TEST_F(FirebaseAnalyticsTest, TestGetSessionID) {
110-
#if defined(__ANDROID__)
111-
// Android continues to have random failures on this test despite the
112-
// workarounds below, so just skip it for now.
113-
LogInfo("Skipping TestGetSessionID on Android");
114-
GTEST_SKIP();
115-
return;
116-
#endif
117-
118-
// Android emulator tests are currently not working due to getSessionId being
119-
// disabled on virtual FTL devices, due to an older version of Google Play
120-
// services.
121-
SKIP_TEST_ON_ANDROID_EMULATOR;
110+
// Don't run this test if Google Play services is < 23.0.0.
111+
SKIP_TEST_ON_ANDROID_GOOGLE_PLAY_SERVICES_BELOW(230000);
122112

123113
// iOS simulator tests are currently extra flaky, occasionally failing with an
124114
// "Analytics uninitialized" error even after multiple attempts.
@@ -128,6 +118,9 @@ TEST_F(FirebaseAnalyticsTest, TestGetSessionID) {
128118
// needs to be restarted after consent is denied or it won't generate a new
129119
// sessionID. To not break the tests, skip this test in that case.
130120
#if defined(__ANDROID__)
121+
// Log the Google Play services version for debugging in case this test fails.
122+
LogInfo("Google Play services version: %d", GetGooglePlayServicesVersion());
123+
131124
if (did_test_setconsent_) {
132125
LogInfo(
133126
"Skipping TestGetSessionID after TestSetConsent, as GetSessionId() "
@@ -136,6 +129,10 @@ TEST_F(FirebaseAnalyticsTest, TestGetSessionID) {
136129
return;
137130
}
138131
#endif
132+
// Log an event once, to ensure that there is currently an active Analytics
133+
// session.
134+
firebase::analytics::LogEvent(firebase::analytics::kEventSignUp);
135+
139136
firebase::Future<int64_t> future;
140137

141138
// Give Analytics a moment to initialize and create a session.

testing/test_framework/src/android/android_firebase_test_framework.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,27 @@ bool FirebaseTest::IsRunningOnEmulator() {
244244
return result ? true : false;
245245
}
246246

247+
int FirebaseTest::GetGooglePlayServicesVersion() {
248+
JNIEnv* env = app_framework::GetJniEnv();
249+
jobject activity = app_framework::GetActivity();
250+
jclass test_helper_class = app_framework::FindClass(
251+
env, activity, "com/google/firebase/example/TestHelper");
252+
if (env->ExceptionCheck()) {
253+
env->ExceptionDescribe();
254+
env->ExceptionClear();
255+
return false;
256+
}
257+
jmethodID get_google_play_services_version =
258+
env->GetStaticMethodID(test_helper_class, "getGooglePlayServicesVersion",
259+
"(Landroid/content/Context;)I");
260+
jint result = env->CallStaticIntMethod(
261+
test_helper_class, get_google_play_services_version, activity);
262+
if (env->ExceptionCheck()) {
263+
env->ExceptionDescribe();
264+
env->ExceptionClear();
265+
return false;
266+
}
267+
return static_cast<int>(result);
268+
}
269+
247270
} // namespace firebase_test_framework

testing/test_framework/src/android/java/com/google/firebase/example/TestHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
package com.google.firebase.example;
1616

17+
import android.content.Context;
1718
import android.os.Build;
19+
import com.google.android.gms.common.GoogleApiAvailability;
1820

1921
/**
2022
* A simple class with test helper methods.
@@ -28,4 +30,7 @@ public static boolean isRunningOnEmulator() {
2830
|| Build.MANUFACTURER.contains("Genymotion") || Build.PRODUCT.contains("vbox86p")
2931
|| Build.DEVICE.contains("vbox86p") || Build.HARDWARE.contains("vbox86");
3032
}
33+
public static int getGooglePlayServicesVersion(Context context) {
34+
return GoogleApiAvailability.getInstance().getApkVersion(context);
35+
}
3136
}

testing/test_framework/src/desktop/desktop_firebase_test_framework.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ bool FirebaseTest::IsRunningOnEmulator() {
5454
return false;
5555
}
5656

57+
int FirebaseTest::GetGooglePlayServicesVersion() {
58+
// No Google Play services on desktop.
59+
return 0;
60+
}
61+
5762
} // namespace firebase_test_framework

testing/test_framework/src/firebase_test_framework.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,28 @@ namespace firebase_test_framework {
205205
#define SKIP_TEST_ON_ANDROID_EMULATOR ((void)0)
206206
#endif
207207

208+
#if defined(ANDROID)
209+
#define SKIP_TEST_ON_ANDROID_GOOGLE_PLAY_SERVICES_BELOW(x) \
210+
{ \
211+
int _required_ver_ = (x); \
212+
/* Example: 23.1.2 has version code 230102???. */ \
213+
/* Allow specifying version as 230102 or as 230102000. */ \
214+
if (_required_ver_ < 10000000) { \
215+
_required_ver_ *= 1000; \
216+
} \
217+
int _actual_ver_ = GetGooglePlayServicesVersion(); \
218+
if (_actual_ver_ < _required_ver_) { \
219+
app_framework::LogInfo( \
220+
"Skipping %s, as Google Play services %d is below required %d", \
221+
test_info_->name(), _actual_ver_, _required_ver_); \
222+
GTEST_SKIP(); \
223+
return; \
224+
} \
225+
}
226+
#else
227+
#define SKIP_TEST_ON_ANDROID_GOOGLE_PLAY_SERVICES_BELOW(x) ((void)0)
228+
#endif // defined(ANDROID)
229+
208230
#if defined(STLPORT)
209231
#define SKIP_TEST_IF_USING_STLPORT \
210232
{ \
@@ -332,6 +354,9 @@ class FirebaseTest : public testing::Test {
332354
// on a real device (or on desktop).
333355
static bool IsRunningOnEmulator();
334356

357+
// Return Google Play services version on Android, 0 elsewhere.
358+
static int GetGooglePlayServicesVersion();
359+
335360
// Returns true if the future completed as expected, fails the test and
336361
// returns false otherwise.
337362
static bool WaitForCompletion(const firebase::FutureBase& future,

testing/test_framework/src/ios/ios_firebase_test_framework.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,9 @@ static bool SendHttpRequest(const char* method, const char* url,
200200
#endif
201201
}
202202

203+
int FirebaseTest::GetGooglePlayServicesVersion() {
204+
// No Google Play services on iOS.
205+
return 0;
206+
}
207+
203208
} // namespace firebase_test_framework

0 commit comments

Comments
 (0)