From 4dc63f8d7cbf60417b88c6a77839ea32656627b9 Mon Sep 17 00:00:00 2001 From: Gil Date: Thu, 22 Feb 2018 08:26:22 -0800 Subject: Fix Firestore tests for M22 (#834) * Add FIRFirestoreTests to the Firestore Xcode project * Avoid waitForExpectations:timeout: This API was added in Xcode 8.3, but we still build production releases with Xcode 8.2. waitForExpectationsWithTimeout:handler: is available from Xcode 7.2. * Add AppForUnitTesting Add a utility for constructing a Firebase App for testing. * Handle the nil UID from FIRAuth * Avoid running CMake tests twice * Only build app_testing on Apple platforms * Revise test.sh messages --- .../test/firebase/firestore/auth/CMakeLists.txt | 1 + .../auth/firebase_credentials_provider_test.mm | 71 +++++++++------------- .../firebase/firestore/testutil/CMakeLists.txt | 35 +++++++++++ .../test/firebase/firestore/testutil/app_testing.h | 43 +++++++++++++ .../firebase/firestore/testutil/app_testing.mm | 48 +++++++++++++++ 5 files changed, 155 insertions(+), 43 deletions(-) create mode 100644 Firestore/core/test/firebase/firestore/testutil/CMakeLists.txt create mode 100644 Firestore/core/test/firebase/firestore/testutil/app_testing.h create mode 100644 Firestore/core/test/firebase/firestore/testutil/app_testing.mm (limited to 'Firestore/core/test') diff --git a/Firestore/core/test/firebase/firestore/auth/CMakeLists.txt b/Firestore/core/test/firebase/firestore/auth/CMakeLists.txt index f470bd7..a6aa2ef 100644 --- a/Firestore/core/test/firebase/firestore/auth/CMakeLists.txt +++ b/Firestore/core/test/firebase/firestore/auth/CMakeLists.txt @@ -30,5 +30,6 @@ if(APPLE) firebase_credentials_provider_test.mm DEPENDS firebase_firestore_auth_apple + firebase_firestore_testutil ) endif(APPLE) diff --git a/Firestore/core/test/firebase/firestore/auth/firebase_credentials_provider_test.mm b/Firestore/core/test/firebase/firestore/auth/firebase_credentials_provider_test.mm index e98d3d8..a6ccc4a 100644 --- a/Firestore/core/test/firebase/firestore/auth/firebase_credentials_provider_test.mm +++ b/Firestore/core/test/firebase/firestore/auth/firebase_credentials_provider_test.mm @@ -21,6 +21,7 @@ #import #include "Firestore/core/src/firebase/firestore/util/string_apple.h" +#include "Firestore/core/test/firebase/firestore/testutil/app_testing.h" #include "gtest/gtest.h" @@ -28,67 +29,51 @@ namespace firebase { namespace firestore { namespace auth { -// TODO(zxu123): Make this an integration test and get infos from environment. -// Set a .plist file here to enable the test-case. -static NSString* const kPlist = @""; - -class FirebaseCredentialsProviderTest : public ::testing::Test { - protected: - void SetUp() override { - app_ready_ = false; - if (![kPlist hasSuffix:@".plist"]) { - return; - } - - static dispatch_once_t once_token; - dispatch_once(&once_token, ^{ - FIROptions* options = [[FIROptions alloc] initWithContentsOfFile:kPlist]; - [FIRApp configureWithOptions:options]; - }); +FIRApp* AppWithFakeUid(NSString* _Nullable uid) { + FIRApp* app = testutil::AppForUnitTesting(); + app.getUIDImplementation = ^NSString* { + return uid; + }; + return app; +} - // Set getUID implementation. - FIRApp* default_app = [FIRApp defaultApp]; - default_app.getUIDImplementation = ^NSString* { - return @"I'm a fake uid."; - }; - app_ready_ = true; - } +TEST(FirebaseCredentialsProviderTest, GetTokenUnauthenticated) { + FIRApp* app = AppWithFakeUid(nil); - bool app_ready_; -}; + FirebaseCredentialsProvider credentials_provider(app); + credentials_provider.GetToken( + /*force_refresh=*/true, [](Token token, const absl::string_view error) { + EXPECT_EQ("", token.token()); + const User& user = token.user(); + EXPECT_EQ("", user.uid()); + EXPECT_FALSE(user.is_authenticated()); + EXPECT_EQ("", error) << error; + }); +} -// Set kPlist above before enable. -TEST_F(FirebaseCredentialsProviderTest, GetToken) { - if (!app_ready_) { - return; - } +TEST(FirebaseCredentialsProviderTest, GetToken) { + FIRApp* app = AppWithFakeUid(@"fake uid"); - FirebaseCredentialsProvider credentials_provider([FIRApp defaultApp]); + FirebaseCredentialsProvider credentials_provider(app); credentials_provider.GetToken( /*force_refresh=*/true, [](Token token, const absl::string_view error) { EXPECT_EQ("", token.token()); const User& user = token.user(); - EXPECT_EQ("I'm a fake uid.", user.uid()); + EXPECT_EQ("fake uid", user.uid()); EXPECT_TRUE(user.is_authenticated()); EXPECT_EQ("", error) << error; }); } -// Set kPlist above before enable. -TEST_F(FirebaseCredentialsProviderTest, SetListener) { - if (!app_ready_) { - return; - } +TEST(FirebaseCredentialsProviderTest, SetListener) { + FIRApp* app = AppWithFakeUid(@"fake uid"); - FirebaseCredentialsProvider credentials_provider([FIRApp defaultApp]); + FirebaseCredentialsProvider credentials_provider(app); credentials_provider.SetUserChangeListener([](User user) { - EXPECT_EQ("I'm a fake uid.", user.uid()); + EXPECT_EQ("fake uid", user.uid()); EXPECT_TRUE(user.is_authenticated()); }); - // TODO(wilhuff): We should wait for the above expectations to actually happen - // before continuing. - credentials_provider.SetUserChangeListener(nullptr); } diff --git a/Firestore/core/test/firebase/firestore/testutil/CMakeLists.txt b/Firestore/core/test/firebase/firestore/testutil/CMakeLists.txt new file mode 100644 index 0000000..b9fcd47 --- /dev/null +++ b/Firestore/core/test/firebase/firestore/testutil/CMakeLists.txt @@ -0,0 +1,35 @@ +# Copyright 2018 Google +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +cc_library( + firebase_firestore_testutil_apple + SOURCES + app_testing.h + app_testing.mm + DEPENDS + FirebaseCore + absl_strings +) + +if(APPLE) + list(APPEND TESTUTIL_DEPENDS firebase_firestore_testutil_apple) +endif() + +add_library( + firebase_firestore_testutil INTERFACE +) +target_link_libraries( + firebase_firestore_testutil INTERFACE + ${TESTUTIL_DEPENDS} +) diff --git a/Firestore/core/test/firebase/firestore/testutil/app_testing.h b/Firestore/core/test/firebase/firestore/testutil/app_testing.h new file mode 100644 index 0000000..d18ba4f --- /dev/null +++ b/Firestore/core/test/firebase/firestore/testutil/app_testing.h @@ -0,0 +1,43 @@ +/* + * Copyright 2018 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FIRESTORE_CORE_TEST_FIREBASE_FIRESTORE_TESTUTIL_APP_TESTING_H_ +#define FIRESTORE_CORE_TEST_FIREBASE_FIRESTORE_TESTUTIL_APP_TESTING_H_ + +#include "absl/strings/string_view.h" + +#if __OBJC__ + +@class FIRApp; + +namespace firebase { +namespace firestore { +namespace testutil { + +/** Creates a set of default Firebase Options for testing. */ +FIROptions* OptionsForUnitTesting( + const absl::string_view project_id = "project_id"); + +/** Creates a new Firebase App for testing. */ +FIRApp* AppForUnitTesting(const absl::string_view project_id = "project_id"); + +} // namespace testutil +} // namespace firestore +} // namespace firebase + +#endif // __OBJC__ + +#endif // FIRESTORE_CORE_TEST_FIREBASE_FIRESTORE_TESTUTIL_APP_TESTING_H_ diff --git a/Firestore/core/test/firebase/firestore/testutil/app_testing.mm b/Firestore/core/test/firebase/firestore/testutil/app_testing.mm new file mode 100644 index 0000000..21db0db --- /dev/null +++ b/Firestore/core/test/firebase/firestore/testutil/app_testing.mm @@ -0,0 +1,48 @@ +/* + * Copyright 2018 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#import +#import + +#include "Firestore/core/src/firebase/firestore/util/string_apple.h" +#include "Firestore/core/test/firebase/firestore/testutil/app_testing.h" + +namespace firebase { +namespace firestore { +namespace testutil { + +FIROptions* OptionsForUnitTesting(const absl::string_view project_id) { + FIROptions* options = + [[FIROptions alloc] initWithGoogleAppID:@"1:123:ios:123ab" + GCMSenderID:@"gcm_sender_id"]; + options.projectID = util::WrapNSString(project_id); + return options; +} + +FIRApp* AppForUnitTesting(const absl::string_view project_id) { + static int counter = 0; + + NSString* appName = + [NSString stringWithFormat:@"app_for_unit_testing_%d", counter++]; + FIROptions* options = OptionsForUnitTesting(project_id); + [FIRApp configureWithName:appName options:options]; + + return [FIRApp appNamed:appName]; +} + +} // namespace testutil +} // namespace firestore +} // namespace firebase -- cgit v1.2.3