Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move SetConsent test to the end to not break GetAnalyticsInstanceId tests. #1529

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 88 additions & 51 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,48 @@ 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();
TEST_F(FirebaseAnalyticsTest, TestSetConsent) {
// On Android, this test must be performed at the end, after all the tests for
// session ID and instance ID. This is because once you call SetConsent to
// deny consent on Android, calling it again to grant consent may not take
// effect until the app restarts, thus breaking any of those tests that are
// run after this one.
//
// If this test does happen to run earlier (due to randomizing test order, for
// example), the tests that could fail will be skipped (on Android).

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);
// 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
Loading