import Foundation import XCTest #if os(macOS) import AppKit #else import UIKit #endif @testable import DocsKit /// Every render makes new `NSTextList`s and `NSTextAttachment`s, which are /// equal only to themselves, so two renders of one projection never compared /// equal (nor, in the app, did the dictionaries the render JSON-encodes, /// whose key order varied); on iOS that redrew the whole text on every update and looped /// (EditorRenderLoopTests). `DocumentRenderReconciliation.isEquivalent` /// compares what they draw. @MainActor final class DocumentRenderEquivalenceTests: XCTestCase { private static let source = "data:image/png;base64,AAAA" private func projection(order: String = "3", source: String = DocumentRenderEquivalenceTests.source) throws -> CRDTProjection { // Several marks and block attributes: the render encodes them into // attributes (with sorted keys; in the app their order varied). let marks: [String: Any] = ["strong": [String: Any](), "em": [String: Any](), "link": ["href": "https://example.com", "title": "x"]] func text(_ insert: String) -> [String: Any] { ["id": UUID().uuidString, "type": "text", "runs": [["insert": insert, "marks": marks]]] } func paragraph(_ insert: String) -> [String: Any] { ["id": UUID().uuidString, "type": "paragraph", "children": [text(insert)]] } func item(_ insert: String) -> [String: Any] { ["id": UUID().uuidString, "type": "list_item", "children": [paragraph(insert)]] } let children: [[String: Any]] = [ ["id": "p", "type": "paragraph", "attrs": ["align": "center", "indent": "1"], "children": [text("Before.")]], ["id": "b", "type": "bullet_list", "children": [item("One"), item("Two")]], ["id": "o", "type": "ordered_list", "attrs": ["order": order], "children": [item("Three"), item("Four")]], ["id": "i", "type": "paragraph", "children": [["id": "img", "type": "image", "attrs": ["src": source, "alt": "A picture"]]]], ["id": "r", "type": "horizontal_rule"], paragraph("After."), ] let data = try JSONSerialization.data(withJSONObject: ["children": children, "canUndo": false, "canRedo": false]) return try JSONDecoder().decode(CRDTProjection.self, from: data) } private var picture: DocsPlatformImage { #if os(macOS) NSImage(size: NSSize(width: 64, height: 36), flipped: false) { rect in NSColor.blue.setFill(); rect.fill(); return true } #else UIGraphicsImageRenderer(size: CGSize(width: 64, height: 36)).image { context in UIColor.blue.setFill(); context.fill(CGRect(x: 0, y: 0, width: 64, height: 36)) } #endif } func testTwoRendersOfOneProjectionAreEquivalent() throws { let projection = try projection() let image = picture for images in [[:], [Self.source: image]] { let first = NativeTextProjection.render(projection, images: images) let second = NativeTextProjection.render(projection, images: images) XCTAssertFalse(first.isEqual(to: second), "the trap: lists and attachments are equal only to themselves") XCTAssertTrue(DocumentRenderReconciliation.isEquivalent(first, second)) } } func testAnImageThatLoadsIsAChange() throws { let projection = try projection() let placeholder = NativeTextProjection.render(projection) let loaded = NativeTextProjection.render(projection, images: [Self.source: picture]) XCTAssertEqual(placeholder.string, loaded.string) XCTAssertFalse(DocumentRenderReconciliation.isEquivalent(placeholder, loaded)) } func testAListsNumberingIsAChange() throws { let third = NativeTextProjection.render(try projection(order: "3")) let fifth = NativeTextProjection.render(try projection(order: "5")) XCTAssertEqual(third.string, fifth.string) XCTAssertFalse(DocumentRenderReconciliation.isEquivalent(third, fifth)) } func testAnyOtherAttributeIsAChange() throws { let base = NativeTextProjection.render(try projection()) let changed = NSMutableAttributedString(attributedString: NativeTextProjection.render(try projection())) #if os(macOS) changed.addAttribute(.foregroundColor, value: NSColor.red, range: NSRange(location: 0, length: 3)) #else changed.addAttribute(.foregroundColor, value: UIColor.red, range: NSRange(location: 0, length: 3)) #endif XCTAssertFalse(DocumentRenderReconciliation.isEquivalent(base, changed)) XCTAssertFalse(DocumentRenderReconciliation.isEquivalent(base, NSAttributedString(string: base.string))) XCTAssertFalse(DocumentRenderReconciliation.isEquivalent(NSAttributedString(string: "a"), NSAttributedString(string: "b"))) } }