|
| 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 | +} |
0 commit comments