Skip to content

Commit 9f5985a

Browse files
authored
Move SetConsent test to the end to not break GetAnalyticsInstanceId tests. (#1529)
* Move SetConsent test to the end, and ensure that GetAnalyticsInstanceId isn't tested after it's called on Android. * Add explanatory comment. * Format.
1 parent 27af7d6 commit 9f5985a

File tree

1 file changed

+91
-51
lines changed

1 file changed

+91
-51
lines changed

Diff for: analytics/integration_test/src/integration_test.cc

+91-51
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,22 @@ TEST_F(FirebaseAnalyticsTest, TestSetSessionTimeoutDuraction) {
100100
}
101101

102102
TEST_F(FirebaseAnalyticsTest, TestGetAnalyticsInstanceID) {
103+
// On Android, if SetConsent was tested, this test will fail, since the app
104+
// needs to be restarted after consent is denied or it won't generate a new
105+
// sessionID. To not break the tests, skip this test in that case.
106+
#if defined(__ANDROID__)
107+
// Log the Google Play services version for debugging in case this test fails.
108+
LogInfo("Google Play services version: %d", GetGooglePlayServicesVersion());
109+
if (did_test_setconsent_) {
110+
LogInfo(
111+
"Skipping %s after TestSetConsent, as the test may fail until the app "
112+
"is restarted.",
113+
::testing::UnitTest::GetInstance()->current_test_info()->name());
114+
GTEST_SKIP();
115+
return;
116+
}
117+
#endif
118+
103119
FLAKY_TEST_SECTION_BEGIN();
104120

105121
firebase::Future<std::string> future =
@@ -124,11 +140,11 @@ TEST_F(FirebaseAnalyticsTest, TestGetSessionID) {
124140
#if defined(__ANDROID__)
125141
// Log the Google Play services version for debugging in case this test fails.
126142
LogInfo("Google Play services version: %d", GetGooglePlayServicesVersion());
127-
128143
if (did_test_setconsent_) {
129144
LogInfo(
130-
"Skipping TestGetSessionID after TestSetConsent, as GetSessionId() "
131-
"will fail until the app is restarted.");
145+
"Skipping %s after TestSetConsent, as the test may fail until the app "
146+
"is restarted.",
147+
::testing::UnitTest::GetInstance()->current_test_info()->name());
132148
GTEST_SKIP();
133149
return;
134150
}
@@ -158,39 +174,39 @@ TEST_F(FirebaseAnalyticsTest, TestGetSessionID) {
158174
}
159175
}
160176

161-
TEST_F(FirebaseAnalyticsTest, TestSetConsent) {
162-
// Can't confirm that these do anything but just run them all to ensure the
163-
// app doesn't crash.
164-
std::map<firebase::analytics::ConsentType, firebase::analytics::ConsentStatus>
165-
consent_settings_allow = {
166-
{firebase::analytics::kConsentTypeAnalyticsStorage,
167-
firebase::analytics::kConsentStatusGranted},
168-
{firebase::analytics::kConsentTypeAdStorage,
169-
firebase::analytics::kConsentStatusGranted},
170-
{firebase::analytics::kConsentTypeAdUserData,
171-
firebase::analytics::kConsentStatusGranted},
172-
{firebase::analytics::kConsentTypeAdPersonalization,
173-
firebase::analytics::kConsentStatusGranted}};
174-
std::map<firebase::analytics::ConsentType, firebase::analytics::ConsentStatus>
175-
consent_settings_deny = {
176-
{firebase::analytics::kConsentTypeAnalyticsStorage,
177-
firebase::analytics::kConsentStatusDenied},
178-
{firebase::analytics::kConsentTypeAdStorage,
179-
firebase::analytics::kConsentStatusDenied},
180-
{firebase::analytics::kConsentTypeAdUserData,
181-
firebase::analytics::kConsentStatusDenied},
182-
{firebase::analytics::kConsentTypeAdPersonalization,
183-
firebase::analytics::kConsentStatusDenied}};
184-
std::map<firebase::analytics::ConsentType, firebase::analytics::ConsentStatus>
185-
consent_settings_empty;
186-
firebase::analytics::SetConsent(consent_settings_empty);
187-
ProcessEvents(1000);
188-
firebase::analytics::SetConsent(consent_settings_deny);
189-
ProcessEvents(1000);
190-
firebase::analytics::SetConsent(consent_settings_allow);
191-
ProcessEvents(1000);
177+
TEST_F(FirebaseAnalyticsTest, TestResettingGivesNewInstanceId) {
178+
// On Android, if SetConsent was tested, this test will fail, since the app
179+
// needs to be restarted after consent is denied or it won't generate a new
180+
// sessionID. To not break the tests, skip this test in that case.
181+
#if defined(__ANDROID__)
182+
// Log the Google Play services version for debugging in case this test fails.
183+
LogInfo("Google Play services version: %d", GetGooglePlayServicesVersion());
184+
if (did_test_setconsent_) {
185+
LogInfo(
186+
"Skipping %s after TestSetConsent, as the test may fail until the app "
187+
"is restarted.",
188+
::testing::UnitTest::GetInstance()->current_test_info()->name());
189+
GTEST_SKIP();
190+
return;
191+
}
192+
#endif
193+
FLAKY_TEST_SECTION_BEGIN();
192194

193-
did_test_setconsent_ = true;
195+
firebase::Future<std::string> future =
196+
firebase::analytics::GetAnalyticsInstanceId();
197+
WaitForCompletion(future, "GetAnalyticsInstanceId");
198+
EXPECT_FALSE(future.result()->empty());
199+
std::string instance_id = *future.result();
200+
201+
firebase::analytics::ResetAnalyticsData();
202+
203+
future = firebase::analytics::GetAnalyticsInstanceId();
204+
WaitForCompletion(future, "GetAnalyticsInstanceId after ResetAnalyticsData");
205+
std::string new_instance_id = *future.result();
206+
EXPECT_FALSE(future.result()->empty());
207+
EXPECT_NE(instance_id, new_instance_id);
208+
209+
FLAKY_TEST_SECTION_END();
194210
}
195211

196212
TEST_F(FirebaseAnalyticsTest, TestSetProperties) {
@@ -235,24 +251,48 @@ TEST_F(FirebaseAnalyticsTest, TestLogEventWithMultipleParameters) {
235251
sizeof(kLevelUpParameters) / sizeof(kLevelUpParameters[0]));
236252
}
237253

238-
TEST_F(FirebaseAnalyticsTest, TestResettingGivesNewInstanceId) {
239-
FLAKY_TEST_SECTION_BEGIN();
240-
241-
firebase::Future<std::string> future =
242-
firebase::analytics::GetAnalyticsInstanceId();
243-
WaitForCompletion(future, "GetAnalyticsInstanceId");
244-
EXPECT_FALSE(future.result()->empty());
245-
std::string instance_id = *future.result();
246-
247-
firebase::analytics::ResetAnalyticsData();
254+
TEST_F(FirebaseAnalyticsTest, TestSetConsent) {
255+
// On Android, this test must be performed at the end, after all the tests for
256+
// session ID and instance ID. This is because once you call SetConsent to
257+
// deny consent on Android, calling it again to grant consent may not take
258+
// effect until the app restarts, thus breaking any of those tests that are
259+
// run after this one.
260+
//
261+
// If this test does happen to run earlier (due to randomizing test order, for
262+
// example), the tests that could fail will be skipped (on Android).
248263

249-
future = firebase::analytics::GetAnalyticsInstanceId();
250-
WaitForCompletion(future, "GetAnalyticsInstanceId after ResetAnalyticsData");
251-
std::string new_instance_id = *future.result();
252-
EXPECT_FALSE(future.result()->empty());
253-
EXPECT_NE(instance_id, new_instance_id);
264+
// Can't confirm that these do anything but just run them all to ensure the
265+
// app doesn't crash.
266+
std::map<firebase::analytics::ConsentType, firebase::analytics::ConsentStatus>
267+
consent_settings_allow = {
268+
{firebase::analytics::kConsentTypeAnalyticsStorage,
269+
firebase::analytics::kConsentStatusGranted},
270+
{firebase::analytics::kConsentTypeAdStorage,
271+
firebase::analytics::kConsentStatusGranted},
272+
{firebase::analytics::kConsentTypeAdUserData,
273+
firebase::analytics::kConsentStatusGranted},
274+
{firebase::analytics::kConsentTypeAdPersonalization,
275+
firebase::analytics::kConsentStatusGranted}};
276+
std::map<firebase::analytics::ConsentType, firebase::analytics::ConsentStatus>
277+
consent_settings_deny = {
278+
{firebase::analytics::kConsentTypeAnalyticsStorage,
279+
firebase::analytics::kConsentStatusDenied},
280+
{firebase::analytics::kConsentTypeAdStorage,
281+
firebase::analytics::kConsentStatusDenied},
282+
{firebase::analytics::kConsentTypeAdUserData,
283+
firebase::analytics::kConsentStatusDenied},
284+
{firebase::analytics::kConsentTypeAdPersonalization,
285+
firebase::analytics::kConsentStatusDenied}};
286+
std::map<firebase::analytics::ConsentType, firebase::analytics::ConsentStatus>
287+
consent_settings_empty;
288+
firebase::analytics::SetConsent(consent_settings_empty);
289+
ProcessEvents(1000);
290+
firebase::analytics::SetConsent(consent_settings_deny);
291+
ProcessEvents(1000);
292+
firebase::analytics::SetConsent(consent_settings_allow);
293+
ProcessEvents(1000);
254294

255-
FLAKY_TEST_SECTION_END();
295+
did_test_setconsent_ = true;
256296
}
257297

258298
} // namespace firebase_testapp_automated

0 commit comments

Comments
 (0)