Skip to content

Commit

Permalink
Move SetConsent test to the end, and ensure that GetAnalyticsInstanceId
Browse files Browse the repository at this point in the history
isn't tested after it's called on Android.
  • Loading branch information
jonsimantov committed Jan 24, 2024
1 parent 27af7d6 commit dc42a9b
Showing 1 changed file with 80 additions and 52 deletions.
132 changes: 80 additions & 52 deletions analytics/integration_test/src/integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ TEST_F(FirebaseAnalyticsTest, TestSetSessionTimeoutDuraction) {
}

TEST_F(FirebaseAnalyticsTest, TestGetAnalyticsInstanceID) {
// On Android, if SetConsent was tested, this test will fail, since the app
// needs to be restarted after consent is denied or it won't generate a new
// sessionID. To not break the tests, skip this test in that case.
#if defined(__ANDROID__)
// Log the Google Play services version for debugging in case this test fails.
LogInfo("Google Play services version: %d", GetGooglePlayServicesVersion());
if (did_test_setconsent_) {
LogInfo(
"Skipping %s after TestSetConsent, as the test may fail until the app is restarted.",
::testing::UnitTest::GetInstance()->current_test_info()->name());
GTEST_SKIP();
return;
}
#endif

FLAKY_TEST_SECTION_BEGIN();

firebase::Future<std::string> future =
Expand All @@ -124,11 +139,10 @@ TEST_F(FirebaseAnalyticsTest, TestGetSessionID) {
#if defined(__ANDROID__)
// Log the Google Play services version for debugging in case this test fails.
LogInfo("Google Play services version: %d", GetGooglePlayServicesVersion());

if (did_test_setconsent_) {
LogInfo(
"Skipping TestGetSessionID after TestSetConsent, as GetSessionId() "
"will fail until the app is restarted.");
"Skipping %s after TestSetConsent, as the test may fail until the app is restarted.",
::testing::UnitTest::GetInstance()->current_test_info()->name());
GTEST_SKIP();
return;
}
Expand Down Expand Up @@ -158,39 +172,38 @@ TEST_F(FirebaseAnalyticsTest, TestGetSessionID) {
}
}

TEST_F(FirebaseAnalyticsTest, TestSetConsent) {
// Can't confirm that these do anything but just run them all to ensure the
// app doesn't crash.
std::map<firebase::analytics::ConsentType, firebase::analytics::ConsentStatus>
consent_settings_allow = {
{firebase::analytics::kConsentTypeAnalyticsStorage,
firebase::analytics::kConsentStatusGranted},
{firebase::analytics::kConsentTypeAdStorage,
firebase::analytics::kConsentStatusGranted},
{firebase::analytics::kConsentTypeAdUserData,
firebase::analytics::kConsentStatusGranted},
{firebase::analytics::kConsentTypeAdPersonalization,
firebase::analytics::kConsentStatusGranted}};
std::map<firebase::analytics::ConsentType, firebase::analytics::ConsentStatus>
consent_settings_deny = {
{firebase::analytics::kConsentTypeAnalyticsStorage,
firebase::analytics::kConsentStatusDenied},
{firebase::analytics::kConsentTypeAdStorage,
firebase::analytics::kConsentStatusDenied},
{firebase::analytics::kConsentTypeAdUserData,
firebase::analytics::kConsentStatusDenied},
{firebase::analytics::kConsentTypeAdPersonalization,
firebase::analytics::kConsentStatusDenied}};
std::map<firebase::analytics::ConsentType, firebase::analytics::ConsentStatus>
consent_settings_empty;
firebase::analytics::SetConsent(consent_settings_empty);
ProcessEvents(1000);
firebase::analytics::SetConsent(consent_settings_deny);
ProcessEvents(1000);
firebase::analytics::SetConsent(consent_settings_allow);
ProcessEvents(1000);
TEST_F(FirebaseAnalyticsTest, TestResettingGivesNewInstanceId) {
// On Android, if SetConsent was tested, this test will fail, since the app
// needs to be restarted after consent is denied or it won't generate a new
// sessionID. To not break the tests, skip this test in that case.
#if defined(__ANDROID__)
// Log the Google Play services version for debugging in case this test fails.
LogInfo("Google Play services version: %d", GetGooglePlayServicesVersion());
if (did_test_setconsent_) {
LogInfo(
"Skipping %s after TestSetConsent, as the test may fail until the app is restarted.",
::testing::UnitTest::GetInstance()->current_test_info()->name());
GTEST_SKIP();
return;
}
#endif
FLAKY_TEST_SECTION_BEGIN();

did_test_setconsent_ = true;
firebase::Future<std::string> future =
firebase::analytics::GetAnalyticsInstanceId();
WaitForCompletion(future, "GetAnalyticsInstanceId");
EXPECT_FALSE(future.result()->empty());
std::string instance_id = *future.result();

firebase::analytics::ResetAnalyticsData();

future = firebase::analytics::GetAnalyticsInstanceId();
WaitForCompletion(future, "GetAnalyticsInstanceId after ResetAnalyticsData");
std::string new_instance_id = *future.result();
EXPECT_FALSE(future.result()->empty());
EXPECT_NE(instance_id, new_instance_id);

FLAKY_TEST_SECTION_END();
}

TEST_F(FirebaseAnalyticsTest, TestSetProperties) {
Expand Down Expand Up @@ -235,24 +248,39 @@ TEST_F(FirebaseAnalyticsTest, TestLogEventWithMultipleParameters) {
sizeof(kLevelUpParameters) / sizeof(kLevelUpParameters[0]));
}

TEST_F(FirebaseAnalyticsTest, TestResettingGivesNewInstanceId) {
FLAKY_TEST_SECTION_BEGIN();

firebase::Future<std::string> future =
firebase::analytics::GetAnalyticsInstanceId();
WaitForCompletion(future, "GetAnalyticsInstanceId");
EXPECT_FALSE(future.result()->empty());
std::string instance_id = *future.result();

firebase::analytics::ResetAnalyticsData();

future = firebase::analytics::GetAnalyticsInstanceId();
WaitForCompletion(future, "GetAnalyticsInstanceId after ResetAnalyticsData");
std::string new_instance_id = *future.result();
EXPECT_FALSE(future.result()->empty());
EXPECT_NE(instance_id, new_instance_id);
TEST_F(FirebaseAnalyticsTest, TestSetConsent) {
// Can't confirm that these do anything but just run them all to ensure the
// app doesn't crash.
std::map<firebase::analytics::ConsentType, firebase::analytics::ConsentStatus>
consent_settings_allow = {
{firebase::analytics::kConsentTypeAnalyticsStorage,
firebase::analytics::kConsentStatusGranted},
{firebase::analytics::kConsentTypeAdStorage,
firebase::analytics::kConsentStatusGranted},
{firebase::analytics::kConsentTypeAdUserData,
firebase::analytics::kConsentStatusGranted},
{firebase::analytics::kConsentTypeAdPersonalization,
firebase::analytics::kConsentStatusGranted}};
std::map<firebase::analytics::ConsentType, firebase::analytics::ConsentStatus>
consent_settings_deny = {
{firebase::analytics::kConsentTypeAnalyticsStorage,
firebase::analytics::kConsentStatusDenied},
{firebase::analytics::kConsentTypeAdStorage,
firebase::analytics::kConsentStatusDenied},
{firebase::analytics::kConsentTypeAdUserData,
firebase::analytics::kConsentStatusDenied},
{firebase::analytics::kConsentTypeAdPersonalization,
firebase::analytics::kConsentStatusDenied}};
std::map<firebase::analytics::ConsentType, firebase::analytics::ConsentStatus>
consent_settings_empty;
firebase::analytics::SetConsent(consent_settings_empty);
ProcessEvents(1000);
firebase::analytics::SetConsent(consent_settings_deny);
ProcessEvents(1000);
firebase::analytics::SetConsent(consent_settings_allow);
ProcessEvents(1000);

FLAKY_TEST_SECTION_END();
did_test_setconsent_ = true;
}

} // namespace firebase_testapp_automated

0 comments on commit dc42a9b

Please sign in to comment.