Skip to content

Commit 1b16341

Browse files
authored
test: add test cases for AUNotificationService (#4)
* test: add test cases for AUNotificationService * test: fix unit test cases * test: rename AmplifyTest to TestData
1 parent 9c40fe4 commit 1b16341

File tree

3 files changed

+189
-1
lines changed

3 files changed

+189
-1
lines changed

Sources/AmplifyUtilsNotifications/AUNotificationService.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ open class AUNotificationService: UNNotificationServiceExtension {
2121
///
2222
/// Default: PinpointNotificationPayload
2323
open var payloadSchema: AUNotificationPayload.Type = PinpointNotificationPayload.self
24+
25+
/// Function for retrieve data from remote URL.
26+
///
27+
/// This is for testing only.
28+
///
29+
/// Default: Data.init(contentsOf:options:)
30+
var loadDataFromURL: (URL) throws -> Data = { try Data(contentsOf: $0) }
2431

2532
var contentHandler: ((UNNotificationContent) -> Void)?
2633
var bestAttemptContent: UNMutableNotificationContent?
@@ -65,7 +72,7 @@ open class AUNotificationService: UNNotificationServiceExtension {
6572
let mediaURLString = payloadData.remoteImageURL,
6673
let mediaType = mediaURLString.split(separator: ".").last,
6774
let mediaURL = URL(string: mediaURLString),
68-
let mediaData = try? Data(contentsOf: mediaURL) else {
75+
let mediaData = try? self.loadDataFromURL(mediaURL) else {
6976
return nil
7077
}
7178
os_log(.debug, "got image data")
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import XCTest
9+
@testable import AmplifyUtilsNotifications
10+
@testable import UserNotifications
11+
12+
final class AUNotificationServiceTests: XCTestCase {
13+
func testDidReceiveNotificationWithoutRemoteMediaURL() {
14+
let expect = expectation(description: "Did receive notification")
15+
let notificationService = AUNotificationService()
16+
let notificationContent = UNMutableNotificationContent()
17+
notificationContent.userInfo = [:]
18+
notificationContent.title = TestData.randomAlphanumeric()
19+
notificationContent.subtitle = TestData.randomAlphanumeric(length: 16)
20+
notificationContent.body = TestData.randomAlphanumeric(length: 64)
21+
22+
let notificationRequest = UNNotificationRequest(
23+
identifier: TestData.randomAlphanumeric(),
24+
content: notificationContent,
25+
trigger: nil
26+
)
27+
28+
notificationService.didReceive(notificationRequest) { content in
29+
XCTAssertEqual(content, notificationContent)
30+
expect.fulfill()
31+
}
32+
waitForExpectations(timeout: 1)
33+
}
34+
35+
func testDidReceiveNotificationWithRemoteMediaURL() {
36+
let expect = expectation(description: "Did receive notification")
37+
let mediaURL = "https://\(TestData.randomAlphabet()).com/\(TestData.randomAlphanumeric()).png"
38+
let mediaData = TestData.randomAlphanumeric(length: 64).data(using: .utf8)!
39+
40+
let notificationService = AUNotificationService()
41+
notificationService.loadDataFromURL = { url in
42+
XCTAssertEqual(URL(string: mediaURL), url)
43+
return mediaData
44+
}
45+
46+
let notificationContent = UNMutableNotificationContent()
47+
notificationContent.userInfo = [
48+
"data": [
49+
"media-url": mediaURL
50+
]
51+
]
52+
53+
let notificationRequest = UNNotificationRequest(
54+
identifier: TestData.randomAlphanumeric(),
55+
content: notificationContent,
56+
trigger: nil
57+
)
58+
59+
notificationService.didReceive(notificationRequest) { content in
60+
XCTAssertTrue(!content.attachments.isEmpty)
61+
let attachment = content.attachments.first!
62+
XCTAssertTrue(attachment.identifier.hasSuffix(".png"))
63+
let data = try? Data(contentsOf: attachment.url)
64+
XCTAssertEqual(mediaData, data)
65+
expect.fulfill()
66+
}
67+
waitForExpectations(timeout: 1)
68+
}
69+
70+
func testDidReceiveNotificationWithBrokenRemoteMediaURL() {
71+
let expect = expectation(description: "Did receive notification")
72+
let mediaURL = TestData.randomAlphanumeric()
73+
74+
let notificationService = AUNotificationService()
75+
let notificationContent = UNMutableNotificationContent()
76+
notificationContent.userInfo = [
77+
"data": [
78+
"media-url": mediaURL
79+
]
80+
]
81+
82+
let notificationRequest = UNNotificationRequest(
83+
identifier: TestData.randomAlphanumeric(),
84+
content: notificationContent,
85+
trigger: nil
86+
)
87+
88+
notificationService.didReceive(notificationRequest) { content in
89+
XCTAssertTrue(content.attachments.isEmpty)
90+
expect.fulfill()
91+
}
92+
waitForExpectations(timeout: 1)
93+
}
94+
95+
func testDidReceiveNotificationWithRemoteMediaURLAndLoadDataWithError() {
96+
struct NetworkError: Error { }
97+
98+
let expect = expectation(description: "Did receive notification")
99+
let mediaURL = "https://\(TestData.randomAlphabet()).com/\(TestData.randomAlphanumeric()).png"
100+
101+
let notificationService = AUNotificationService()
102+
notificationService.loadDataFromURL = { url in
103+
XCTAssertEqual(URL(string: mediaURL), url)
104+
throw NetworkError()
105+
}
106+
107+
let notificationContent = UNMutableNotificationContent()
108+
notificationContent.userInfo = [
109+
"data": [
110+
"media-url": mediaURL
111+
]
112+
]
113+
114+
let notificationRequest = UNNotificationRequest(
115+
identifier: TestData.randomAlphanumeric(),
116+
content: notificationContent,
117+
trigger: nil
118+
)
119+
120+
notificationService.didReceive(notificationRequest) { content in
121+
XCTAssertTrue(content.attachments.isEmpty)
122+
expect.fulfill()
123+
}
124+
waitForExpectations(timeout: 1)
125+
}
126+
127+
func testServiceExtensionTimeWillExpire() {
128+
let expect = expectation(description: "Service extension time will expire")
129+
let notificationService = AUNotificationService()
130+
let notificationContent = UNMutableNotificationContent()
131+
notificationContent.userInfo = [:]
132+
notificationContent.title = TestData.randomAlphanumeric()
133+
notificationContent.subtitle = TestData.randomAlphanumeric(length: 16)
134+
notificationContent.body = TestData.randomAlphanumeric(length: 64)
135+
notificationService.didReceive(
136+
UNNotificationRequest(
137+
identifier: TestData.randomAlphanumeric(),
138+
content: notificationContent,
139+
trigger: nil
140+
)
141+
) { _ in }
142+
143+
notificationService.contentHandler = { content in
144+
XCTAssertEqual(notificationContent, content)
145+
expect.fulfill()
146+
}
147+
148+
notificationService.serviceExtensionTimeWillExpire()
149+
waitForExpectations(timeout: 1)
150+
}
151+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Foundation
9+
10+
enum TestData { }
11+
12+
extension TestData {
13+
static func randomAlphabet(length: Int = 8) -> String {
14+
assert(length > 0)
15+
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
16+
return String((0..<length).map{ _ in letters.randomElement()! })
17+
}
18+
19+
static func randomNumeric(length: Int = 8) -> String {
20+
assert(length > 0)
21+
let letters = "0123456789"
22+
return String((0..<length).map{ _ in letters.randomElement()! })
23+
}
24+
25+
static func randomAlphanumeric(length: Int = 8) -> String {
26+
assert(length > 0)
27+
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
28+
return String((0..<length).map{ _ in letters.randomElement()! })
29+
}
30+
}

0 commit comments

Comments
 (0)