Skip to content

Commit 902adf8

Browse files
Add Swift tests for Messaging (#9135)
1 parent ebfe3e3 commit 902adf8

File tree

5 files changed

+179
-21
lines changed

5 files changed

+179
-21
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2021 Google LLC
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+
// macOS requests a user password when accessing the Keychain for the first time,
18+
// so the tests may fail. Disable integration tests on macOS so far.
19+
// TODO: Configure the tests to run on macOS without requesting the keychain password.
20+
#if !os(OSX)
21+
22+
import FirebaseCore
23+
import FirebaseMessaging
24+
import XCTest
25+
26+
class FIRMessagingPubSubTest: XCTestCase {
27+
var app: FirebaseApp!
28+
var messaging: Messaging!
29+
30+
override class func setUp() {
31+
if FirebaseApp.app() == nil {
32+
FirebaseApp.configure()
33+
}
34+
}
35+
36+
override func setUpWithError() throws {
37+
messaging = try XCTUnwrap(Messaging.messaging())
38+
}
39+
40+
override func tearDown() {
41+
messaging = nil
42+
}
43+
44+
func testSubscribeTopic() {
45+
let expectation = self.expectation(description: "Successfully subscribe topic")
46+
assertDefaultToken()
47+
48+
messaging.subscribe(toTopic: "cat_video") { error in
49+
XCTAssertNil(error)
50+
expectation.fulfill()
51+
}
52+
wait(for: [expectation], timeout: 5)
53+
}
54+
55+
func testUnsubscribeTopic() {
56+
let expectation = self.expectation(description: "Successfully unsubscribe topic")
57+
assertDefaultToken()
58+
59+
messaging.unsubscribe(fromTopic: "cat_video") { error in
60+
XCTAssertNil(error)
61+
expectation.fulfill()
62+
}
63+
wait(for: [expectation], timeout: 5)
64+
}
65+
66+
func assertDefaultToken() {
67+
let expectation = self.expectation(description: "getToken")
68+
messaging.token { token, error in
69+
XCTAssertNil(error)
70+
XCTAssertNotNil(token)
71+
expectation.fulfill()
72+
}
73+
wait(for: [expectation], timeout: 5)
74+
}
75+
}
76+
#endif // !TARGET_OS_OSX

FirebaseMessaging/Tests/IntegrationTests/FIRMessagingTokenRefreshTests.swift

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
// so the tests may fail. Disable integration tests on macOS so far.
1919
// TODO: Configure the tests to run on macOS without requesting the keychain password.
2020
#if !os(OSX)
21-
2221
import FirebaseCore
2322
import FirebaseMessaging
2423
import XCTest
@@ -35,14 +34,16 @@
3534

3635
class FIRMessagingTokenRefreshTests: XCTestCase {
3736
var app: FirebaseApp!
38-
var messaging: Messaging?
37+
var messaging: Messaging!
3938

4039
override class func setUp() {
41-
FirebaseApp.configure()
40+
if FirebaseApp.app() == nil {
41+
FirebaseApp.configure()
42+
}
4243
}
4344

44-
override func setUp() {
45-
messaging = Messaging.messaging()
45+
override func setUpWithError() throws {
46+
messaging = try XCTUnwrap(Messaging.messaging())
4647
}
4748

4849
override func tearDown() {
@@ -59,12 +60,9 @@
5960
handler: nil)
6061

6162
let testDelegate = fakeAppDelegate()
62-
messaging?.delegate = testDelegate
63+
messaging.delegate = testDelegate
6364
testDelegate.delegateIsCalled = false
6465

65-
guard let messaging = self.messaging else {
66-
return
67-
}
6866
messaging.deleteFCMToken(forSenderID: tokenAuthorizedEntity(), completion: { error in
6967
XCTAssertNil(error)
7068
XCTAssertTrue(testDelegate.delegateIsCalled)
@@ -85,10 +83,6 @@
8583
let testDelegate = fakeAppDelegate()
8684
messaging?.delegate = testDelegate
8785
testDelegate.delegateIsCalled = false
88-
89-
guard let messaging = self.messaging else {
90-
return
91-
}
9286
messaging.deleteToken { error in
9387
XCTAssertNil(error)
9488
XCTAssertTrue(testDelegate.delegateIsCalled)
@@ -97,12 +91,31 @@
9791
wait(for: [expectation, notificationExpectation], timeout: 5)
9892
}
9993

94+
func testDeleteDataWithTokenRefreshDelegatesAndNotifications() {
95+
let expectation = self.expectation(description: "delegate method and notification are called")
96+
assertDefaultToken()
97+
98+
let notificationExpectation = self.expectation(forNotification: NSNotification.Name
99+
.MessagingRegistrationTokenRefreshed,
100+
object: nil,
101+
handler: nil)
102+
103+
let testDelegate = fakeAppDelegate()
104+
messaging?.delegate = testDelegate
105+
testDelegate.delegateIsCalled = false
106+
107+
messaging.deleteData { error in
108+
XCTAssertNil(error)
109+
XCTAssertTrue(testDelegate.delegateIsCalled)
110+
expectation.fulfill()
111+
}
112+
wait(for: [expectation, notificationExpectation], timeout: 5)
113+
}
114+
100115
// pragma mark - Helpers
101116
func assertTokenWithAuthorizedEntity() {
102117
let expectation = self.expectation(description: "tokenWithAuthorizedEntity")
103-
guard let messaging = self.messaging else {
104-
return
105-
}
118+
106119
messaging.retrieveFCMToken(forSenderID: tokenAuthorizedEntity()) { token, error in
107120
XCTAssertNil(error)
108121
XCTAssertNotNil(token)
@@ -113,9 +126,7 @@
113126

114127
func assertDefaultToken() {
115128
let expectation = self.expectation(description: "getToken")
116-
guard let messaging = self.messaging else {
117-
return
118-
}
129+
119130
messaging.token { token, error in
120131
XCTAssertNil(error)
121132
XCTAssertNotNil(token)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
import FirebaseCore
18+
import FirebaseMessaging
19+
import UserNotifications
20+
21+
// This file is a build-only test for the public Messaging Swift APIs not
22+
// exercised in the integration tests.
23+
func apis() {
24+
let messaging = Messaging.messaging()
25+
26+
if let _ = messaging.apnsToken {}
27+
28+
let apnsToken = Data()
29+
messaging.setAPNSToken(apnsToken, type: .prod)
30+
31+
let topic = "cat_video"
32+
messaging.subscribe(toTopic: topic)
33+
messaging.unsubscribe(fromTopic: topic)
34+
35+
messaging.appDidReceiveMessage([:])
36+
37+
if #available(macOS 10.14, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
38+
let serviceExtension = Messaging.serviceExtension()
39+
let content = UNMutableNotificationContent()
40+
serviceExtension.populateNotificationContent(content) { content in
41+
}
42+
serviceExtension.exportDeliveryMetricsToBigQuery(withMessageInfo: [:])
43+
}
44+
}
45+
46+
@available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
47+
func apiAsync() async throws {
48+
let messaging = Messaging.messaging()
49+
let topic = "cat_video"
50+
#if compiler(>=5.5) && canImport(_Concurrency)
51+
try await messaging.subscribe(toTopic: topic)
52+
53+
try await messaging.unsubscribe(fromTopic: topic)
54+
55+
try await messaging.token()
56+
57+
try await messaging.retrieveFCMToken(forSenderID: "fakeSenderID")
58+
59+
try await messaging.deleteToken()
60+
61+
try await messaging.deleteFCMToken(forSenderID: "fakeSenderID")
62+
63+
try await messaging.deleteData()
64+
#endif
65+
}

SwiftDashboard.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ tasks for additional Swift improvements.
1010
| | AB | An | ApC | ApD | Aut | Cor | Crs | DB | Fst | Fn | IAM | Ins | Msg | MLM | Prf | RC | Str |
1111
| :--- | :--- | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: |
1212
| **Swift Library** |||||||||| 1 ||||||||
13-
| **API Tests** |||||||| 3 | 2 || 2 || | 2 ||||
14-
| **async/await** || n/a |||||| 3 ||||| |||||
13+
| **API Tests** |||||||| 3 | 2 || 2 || 2 | 2 ||||
14+
| **async/await** || n/a |||||| 3 ||||| |||||
1515
| **Swift Errors** ||||| 4 |||||||||||| 5 |
1616
| **Codable** | n/a | n/a | n/a |n/a | n/a | n/a |n/a ||| 1 | n/a | n/a || n/a | n/a ||n/a |
1717
| **SwiftUI Lifecycle** | n/a || n/a ||| n/a |n/a | n/a | n/a | n/a | n/a | n/a || n/a | n/a | n/a |n/a |

scripts/build.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,12 @@ case "$product-$platform-$method" in
370370

371371
Messaging-*-xcodebuild)
372372
pod_gen FirebaseMessaging.podspec --platforms=ios
373+
374+
# Add GoogleService-Info.plist to generated Test Wrapper App.
375+
ruby ./scripts/update_xcode_target.rb gen/FirebaseMessaging/Pods/Pods.xcodeproj \
376+
AppHost-FirebaseMessaging-Unit-Tests \
377+
../../../FirebaseMessaging/Tests/IntegrationTests/Resources/GoogleService-Info.plist
378+
373379
RunXcodebuild \
374380
-workspace 'gen/FirebaseMessaging/FirebaseMessaging.xcworkspace' \
375381
-scheme "FirebaseMessaging-Unit-unit" \

0 commit comments

Comments
 (0)