| 1 | import Foundation |
| 2 | import XCTest |
| 3 | #if os(macOS) |
| 4 | import AppKit |
| 5 | #else |
| 6 | import UIKit |
| 7 | #endif |
| 8 | @testable import DocsKit |
| 9 | |
| 10 | /// Every render makes new `NSTextList`s and `NSTextAttachment`s, which are |
| 11 | /// equal only to themselves, so two renders of one projection never compared |
| 12 | /// equal (nor, in the app, did the dictionaries the render JSON-encodes, |
| 13 | /// whose key order varied); on iOS that redrew the whole text on every update and looped |
| 14 | /// (EditorRenderLoopTests). `DocumentRenderReconciliation.isEquivalent` |
| 15 | /// compares what they draw. |
| 16 | @MainActor final class DocumentRenderEquivalenceTests: XCTestCase { |
| 17 | private static let source = "data:image/png;base64,AAAA" |
| 18 | |
| 19 | private func projection(order: String = "3", source: String = DocumentRenderEquivalenceTests.source) throws -> CRDTProjection { |
| 20 | // Several marks and block attributes: the render encodes them into |
| 21 | // attributes (with sorted keys; in the app their order varied). |
| 22 | let marks: [String: Any] = ["strong": [String: Any](), "em": [String: Any](), "link": ["href": "https://example.com", "title": "x"]] |
| 23 | func text(_ insert: String) -> [String: Any] { ["id": UUID().uuidString, "type": "text", "runs": [["insert": insert, "marks": marks]]] } |
| 24 | func paragraph(_ insert: String) -> [String: Any] { ["id": UUID().uuidString, "type": "paragraph", "children": [text(insert)]] } |
| 25 | func item(_ insert: String) -> [String: Any] { ["id": UUID().uuidString, "type": "list_item", "children": [paragraph(insert)]] } |
| 26 | let children: [[String: Any]] = [ |
| 27 | ["id": "p", "type": "paragraph", "attrs": ["align": "center", "indent": "1"], "children": [text("Before.")]], |
| 28 | ["id": "b", "type": "bullet_list", "children": [item("One"), item("Two")]], |
| 29 | ["id": "o", "type": "ordered_list", "attrs": ["order": order], "children": [item("Three"), item("Four")]], |
| 30 | ["id": "i", "type": "paragraph", "children": [["id": "img", "type": "image", "attrs": ["src": source, "alt": "A picture"]]]], |
| 31 | ["id": "r", "type": "horizontal_rule"], |
| 32 | paragraph("After."), |
| 33 | ] |
| 34 | let data = try JSONSerialization.data(withJSONObject: ["children": children, "canUndo": false, "canRedo": false]) |
| 35 | return try JSONDecoder().decode(CRDTProjection.self, from: data) |
| 36 | } |
| 37 | |
| 38 | private var picture: DocsPlatformImage { |
| 39 | #if os(macOS) |
| 40 | NSImage(size: NSSize(width: 64, height: 36), flipped: false) { rect in NSColor.blue.setFill(); rect.fill(); return true } |
| 41 | #else |
| 42 | UIGraphicsImageRenderer(size: CGSize(width: 64, height: 36)).image { context in UIColor.blue.setFill(); context.fill(CGRect(x: 0, y: 0, width: 64, height: 36)) } |
| 43 | #endif |
| 44 | } |
| 45 | |
| 46 | func testTwoRendersOfOneProjectionAreEquivalent() throws { |
| 47 | let projection = try projection() |
| 48 | let image = picture |
| 49 | for images in [[:], [Self.source: image]] { |
| 50 | let first = NativeTextProjection.render(projection, images: images) |
| 51 | let second = NativeTextProjection.render(projection, images: images) |
| 52 | XCTAssertFalse(first.isEqual(to: second), "the trap: lists and attachments are equal only to themselves") |
| 53 | XCTAssertTrue(DocumentRenderReconciliation.isEquivalent(first, second)) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func testAnImageThatLoadsIsAChange() throws { |
| 58 | let projection = try projection() |
| 59 | let placeholder = NativeTextProjection.render(projection) |
| 60 | let loaded = NativeTextProjection.render(projection, images: [Self.source: picture]) |
| 61 | XCTAssertEqual(placeholder.string, loaded.string) |
| 62 | XCTAssertFalse(DocumentRenderReconciliation.isEquivalent(placeholder, loaded)) |
| 63 | } |
| 64 | |
| 65 | func testAListsNumberingIsAChange() throws { |
| 66 | let third = NativeTextProjection.render(try projection(order: "3")) |
| 67 | let fifth = NativeTextProjection.render(try projection(order: "5")) |
| 68 | XCTAssertEqual(third.string, fifth.string) |
| 69 | XCTAssertFalse(DocumentRenderReconciliation.isEquivalent(third, fifth)) |
| 70 | } |
| 71 | |
| 72 | func testAnyOtherAttributeIsAChange() throws { |
| 73 | let base = NativeTextProjection.render(try projection()) |
| 74 | let changed = NSMutableAttributedString(attributedString: NativeTextProjection.render(try projection())) |
| 75 | #if os(macOS) |
| 76 | changed.addAttribute(.foregroundColor, value: NSColor.red, range: NSRange(location: 0, length: 3)) |
| 77 | #else |
| 78 | changed.addAttribute(.foregroundColor, value: UIColor.red, range: NSRange(location: 0, length: 3)) |
| 79 | #endif |
| 80 | XCTAssertFalse(DocumentRenderReconciliation.isEquivalent(base, changed)) |
| 81 | XCTAssertFalse(DocumentRenderReconciliation.isEquivalent(base, NSAttributedString(string: base.string))) |
| 82 | XCTAssertFalse(DocumentRenderReconciliation.isEquivalent(NSAttributedString(string: "a"), NSAttributedString(string: "b"))) |
| 83 | } |
| 84 | } |
| 85 | |