iOS/Tests/Shared/NotificationAttachment/NotificationAttachmentParserCamera.test.swift
Zac West 85b5ec4579
Support attachments which aren't downloaded in advance (#1610)
## Summary
Allows attachments to be loaded on-the-fly in the content extension if they are not present.

## Link to pull request in Documentation repository
Documentation: home-assistant/companion.home-assistant#510

## Any other notes
- Adds `lazy` as an option to the `attachment` dictionary. When true, this prevents the attachment from being downloaded except when displayed in the content extension.  
  
This is similar, but not identical, to hide-thumbnail. Hiding the thumbnail still uses the internal storage of the notifications which are limited to e.g. 50mb. However, we'd still want to pre-load those attachments even if the thumbnail is hidden.
- Retries non-lazy attachments which failed due to a network error when the content extension opens.
2021-05-01 12:58:30 -07:00

112 lines
3.6 KiB
Swift

import CoreServices
import Foundation
import PromiseKit
@testable import Shared
import XCTest
class NotificationAttachmentParserCameraTests: XCTestCase {
private typealias CameraError = NotificationAttachmentParserCamera.CameraError
private var parser: NotificationAttachmentParserCamera!
override func setUp() {
super.setUp()
parser = NotificationAttachmentParserCamera()
}
func testMissingEntityID() {
let content = UNMutableNotificationContent()
let promise = parser.attachmentInfo(from: content)
XCTAssertEqual(promise.wait(), .missing)
}
func testInvalidEntityID() {
let content = UNMutableNotificationContent()
content.userInfo["entity_id"] = "\\"
let promise = parser.attachmentInfo(from: content)
XCTAssertEqual(promise.wait(), .missing)
}
func testNonCameraEntityID() {
let content = UNMutableNotificationContent()
content.userInfo["entity_id"] = "light.bedroom"
let promise = parser.attachmentInfo(from: content)
XCTAssertEqual(promise.wait(), .missing)
}
func testAttachmentHidden() {
let content = UNMutableNotificationContent()
content.userInfo["entity_id"] = "camera.any"
content.userInfo["attachment"] = [
"hide-thumbnail": true,
]
let promise = parser.attachmentInfo(from: content)
guard let result = promise.wait().attachmentInfo else {
XCTFail("not an attachment")
return
}
XCTAssertEqual(result.url, URL(string: "/api/camera_proxy/camera.any"))
XCTAssertEqual(result.needsAuth, true)
XCTAssertEqual(result.typeHint, kUTTypeJPEG)
XCTAssertEqual(result.hideThumbnail, true)
}
func testAttachmentLazy() {
let content = UNMutableNotificationContent()
content.userInfo["entity_id"] = "camera.any"
content.userInfo["attachment"] = [
"lazy": true,
]
let promise = parser.attachmentInfo(from: content)
guard let result = promise.wait().attachmentInfo else {
XCTFail("not an attachment")
return
}
XCTAssertEqual(result.url, URL(string: "/api/camera_proxy/camera.any"))
XCTAssertEqual(result.needsAuth, true)
XCTAssertEqual(result.typeHint, kUTTypeJPEG)
XCTAssertEqual(result.hideThumbnail, nil)
XCTAssertEqual(result.lazy, true)
}
func testAttachmentNotHidden() {
let content = UNMutableNotificationContent()
content.userInfo["entity_id"] = "camera.any"
content.userInfo["attachment"] = [
"hide-thumbnail": false,
]
let promise = parser.attachmentInfo(from: content)
guard let result = promise.wait().attachmentInfo else {
XCTFail("not an attachment")
return
}
XCTAssertEqual(result.url, URL(string: "/api/camera_proxy/camera.any"))
XCTAssertEqual(result.needsAuth, true)
XCTAssertEqual(result.typeHint, kUTTypeJPEG)
XCTAssertEqual(result.hideThumbnail, false)
}
func testAttachmentInfo() {
let content = UNMutableNotificationContent()
content.userInfo["entity_id"] = "camera.any"
let promise = parser.attachmentInfo(from: content)
guard let result = promise.wait().attachmentInfo else {
XCTFail("not an attachment")
return
}
XCTAssertEqual(result.url, URL(string: "/api/camera_proxy/camera.any"))
XCTAssertEqual(result.needsAuth, true)
XCTAssertEqual(result.typeHint, kUTTypeJPEG)
XCTAssertEqual(result.hideThumbnail, nil)
}
}