Repository navigation
App (Desktop): register product user agents when an App is created #1988
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
Changes from all commits
cc82d1a
5293e26
b2c6d0f
3135cc3
ab4e3c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,13 +21,15 @@ | |
| #include <codecvt> | ||
| #include <cstdio> | ||
| #include <fstream> | ||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include "app/src/app_common.h" | ||
| #include "app/src/app_identifier.h" | ||
| #include "app/src/include/firebase/app.h" | ||
| #include "app/src/include/firebase/internal/platform.h" | ||
| #include "app/src/include/firebase/version.h" | ||
| #include "app/src/util.h" | ||
| #include "gmock/gmock.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
|
|
@@ -37,6 +39,17 @@ | |
| #include <unistd.h> | ||
| #endif | ||
|
|
||
| // A module that reports a library, used to test that libraries are registered | ||
| // when an App is created. | ||
| FIREBASE_APP_REGISTER_CALLBACKS( | ||
| library_test_module, { return ::firebase::kInitResultSuccess; }, | ||
| { | ||
| // Nothing to tear down. | ||
| }, | ||
| false); | ||
| FIREBASE_APP_REGISTER_LIBRARY(library_test_module, "fire-library-test", | ||
| "1.2.3"); | ||
|
|
||
| namespace firebase { | ||
|
|
||
| static bool RemoveDirectoryUtf8(std::string dir_utf8) { | ||
|
|
@@ -252,4 +265,53 @@ TEST(AppDesktopTest, TestLoadAppOptionsFromJsonConfigFileInInternationalPath) { | |
| EXPECT_TRUE(ChangeDirectoryUtf8(previous_directory)); | ||
| EXPECT_TRUE(RemoveDirectoryUtf8(new_dir)); | ||
| } | ||
|
|
||
| static const char kLibraryTestModule[] = "library_test_module"; | ||
| static const char kLibraryTestUserAgent[] = "fire-library-test/1.2.3"; | ||
|
|
||
| static std::unique_ptr<App> CreateAppForLibraryTest() { | ||
| AppOptions options; | ||
| options.set_app_id("1:1234567890:android:321abc456def7890"); | ||
| options.set_api_key("fake-api-key"); | ||
| options.set_project_id("fake-project-id"); | ||
| return std::unique_ptr<App>(App::Create(options)); | ||
| } | ||
|
|
||
| struct ScopedCallbackReset { | ||
| ~ScopedCallbackReset() { AppCallback::ResetAllToDefaults(); } | ||
| }; | ||
|
|
||
| TEST(AppDesktopTest, TestModuleLibraryRegisteredWhenAppCreated) { | ||
| EXPECT_FALSE(AppCallback::GetEnabledByName(kLibraryTestModule)); | ||
| std::unique_ptr<App> app = CreateAppForLibraryTest(); | ||
| ASSERT_NE(app, nullptr); | ||
|
|
||
| EXPECT_THAT(std::string(App::GetUserAgent()), | ||
| ::testing::HasSubstr(kLibraryTestUserAgent)); | ||
| } | ||
|
|
||
| TEST(AppDesktopTest, TestModuleLibraryNotRegisteredWhenModuleDisabled) { | ||
| ScopedCallbackReset reset_callbacks; | ||
| // The Unity SDK links every module and disables the ones that an application | ||
| // does not use. A disabled module must not be reported. | ||
| AppCallback::SetEnabledByName(kLibraryTestModule, false); | ||
| std::unique_ptr<App> app = CreateAppForLibraryTest(); | ||
| ASSERT_NE(app, nullptr); | ||
|
|
||
| EXPECT_THAT(std::string(App::GetUserAgent()), | ||
| ::testing::Not(::testing::HasSubstr(kLibraryTestUserAgent))); | ||
| } | ||
|
|
||
| TEST(AppDesktopTest, TestModuleLibraryRegisteredWhenModuleReenabled) { | ||
| ScopedCallbackReset reset_callbacks; | ||
| // This is the sequence the Unity SDK uses before it creates an App: disable | ||
| // every module, then enable the ones that are in use. | ||
| AppCallback::SetEnabledAll(false); | ||
| AppCallback::SetEnabledByName(kLibraryTestModule, true); | ||
| std::unique_ptr<App> app = CreateAppForLibraryTest(); | ||
| ASSERT_NE(app, nullptr); | ||
|
|
||
| EXPECT_THAT(std::string(App::GetUserAgent()), | ||
| ::testing::HasSubstr(kLibraryTestUserAgent)); | ||
| } | ||
|
Comment on lines
+314
to
+316
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test disables all callbacks using EXPECT_THAT(std::string(App::GetUserAgent()),
::testing::HasSubstr(kLibraryTestUserAgent));
AppCallback::SetEnabledAll(true);
} |
||
| } // namespace firebase | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent potential null pointer dereferences, it is safer to perform a null check on
callbackbefore callingSetLibraryon it.