|
| 1 | +/* |
| 2 | + * Copyright 2019 Google |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import FirebaseCore |
| 18 | +import FirebaseMessaging |
| 19 | +import XCTest |
| 20 | + |
| 21 | +class FIRMessagingInstanceTest: XCTestCase { |
| 22 | + |
| 23 | + func testSingleton_worksAfterDelete() { |
| 24 | + // This is an example of a functional test case. |
| 25 | + // Use XCTAssert and related functions to verify your tests produce the correct results. |
| 26 | + let options = FirebaseOptions(googleAppID: "1:123:ios:123abc", gcmSenderID: "valid-sender-id") |
| 27 | + FirebaseApp.configure(options: options) |
| 28 | + let original = Messaging.messaging() |
| 29 | + |
| 30 | + // Ensure the client is set up as expected. |
| 31 | + guard let isOrigClientSetup = original.value(forKey: "isClientSetup") as? Bool else { |
| 32 | + XCTFail("Could not get internal Messaging variable `isClientSetup`.") |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + XCTAssertTrue(isOrigClientSetup, "Property `isClientSetup` should be true after creation.") |
| 37 | + |
| 38 | + // Get and delete the default app. |
| 39 | + guard let defaultApp = FirebaseApp.app() else { |
| 40 | + XCTFail("Default app was not configured properly.") |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + // The delete API is synchronous, so the default app will be deleted afterwards. |
| 45 | + defaultApp.delete { (success) in |
| 46 | + XCTAssertTrue(success, "FirebaseApp deletion should be successful") |
| 47 | + } |
| 48 | + |
| 49 | + XCTAssertNil(FirebaseApp.app(), "The default app should be `nil` at this point.") |
| 50 | + |
| 51 | + // Re-configure the app to trigger Messaging to re-instantiate. |
| 52 | + FirebaseApp.configure(options: options) |
| 53 | + |
| 54 | + // Get another instance of Messaging, make sure it's not the same instance. |
| 55 | + let postDelete = Messaging.messaging() |
| 56 | + XCTAssertNotEqual(original, postDelete) |
| 57 | + |
| 58 | + // Ensure the new client is set up as expected. |
| 59 | + guard let isClientSetup = postDelete.value(forKey: "isClientSetup") as? Bool else { |
| 60 | + XCTFail("Could not get internal Messaging variable `isClientSetup`.") |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + XCTAssertTrue(isClientSetup, "Property `isClientSetup` should be true after creation.") |
| 65 | + } |
| 66 | +} |
0 commit comments