adds first version of the app
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
import AppKit
|
||||
import PlakCore
|
||||
import PlakServices
|
||||
@testable import PlakUI
|
||||
import Testing
|
||||
|
||||
@MainActor
|
||||
@Suite struct ImageTests {
|
||||
private func pasteboard() -> NSPasteboard {
|
||||
let board = NSPasteboard(name: NSPasteboard.Name("plak-tests-\(UUID().uuidString)"))
|
||||
board.clearContents()
|
||||
return board
|
||||
}
|
||||
|
||||
private func attachments(_ page: TestPage) -> [(location: Int, image: ImageAttachment)] {
|
||||
guard let storage = page.textView.textStorage else { return [] }
|
||||
var found: [(Int, ImageAttachment)] = []
|
||||
storage.enumerateAttribute(.attachment, in: NSRange(location: 0, length: storage.length)) { value, range, _ in
|
||||
if let image = value as? ImageAttachment { found.append((range.location, image)) }
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
private func laidOutSize(_ page: TestPage, at location: Int) throws -> NSSize {
|
||||
let layout = try #require(page.textView.layoutManager)
|
||||
layout.ensureLayout(for: try #require(page.textView.textContainer))
|
||||
return layout.attachmentSize(forGlyphAt: layout.glyphIndexForCharacter(at: location))
|
||||
}
|
||||
|
||||
@Test("an image is never wider than the column and keeps its aspect ratio; a small image keeps its size")
|
||||
func scaling() throws {
|
||||
let page = try TestPage("\n\n\n",
|
||||
images: ["wide.png": NSSize(width: 2000, height: 500), "small.png": NSSize(width: 120, height: 80)])
|
||||
let column = try #require(page.textView.textContainer).size.width
|
||||
let wide = try laidOutSize(page, at: 0)
|
||||
#expect(wide.width <= column && wide.width >= column - 1)
|
||||
#expect(abs(wide.height - wide.width / 4) <= 1)
|
||||
#expect(try laidOutSize(page, at: 3) == NSSize(width: 120, height: 80))
|
||||
}
|
||||
|
||||
@Test("a missing file shows image missing in inkMuted and saves back as the same line")
|
||||
func missing() throws {
|
||||
let text = "before\n\nafter\n"
|
||||
let page = try TestPage(text)
|
||||
let image = try #require(attachments(page).first?.image)
|
||||
let cell = try #require(image.attachmentCell as? MissingImageCell)
|
||||
#expect(cell.text.string == "image missing")
|
||||
#expect(cell.text.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? NSColor == Theme.inkMuted)
|
||||
#expect(page.fileText == text)
|
||||
}
|
||||
|
||||
@Test("copy of a selection with an image gives its image line as plain text; pasting it gives the image again")
|
||||
func copyAndPaste() throws {
|
||||
let page = try TestPage("a\n\nb\n", images: ["a.png": NSSize(width: 10, height: 10)])
|
||||
let board = pasteboard()
|
||||
defer { board.releaseGlobally() }
|
||||
page.textView.setSelectedRange(NSRange(location: 0, length: page.textView.length))
|
||||
#expect(page.textView.writeSelection(to: board, types: [.string]))
|
||||
#expect(board.string(forType: .string) == "a\n\nb")
|
||||
#expect(board.types?.contains(.rtf) == false && board.types?.contains(.string) == true)
|
||||
|
||||
page.caretAtEnd()
|
||||
page.type("\n\n")
|
||||
page.textView.paste(from: board)
|
||||
#expect(attachments(page).map(\.image.path) == ["images/a.png", "images/a.png"])
|
||||
#expect(page.fileText.hasSuffix("\na\n\nb\n"))
|
||||
|
||||
board.clearContents()
|
||||
board.setString("", forType: .string)
|
||||
page.textView.paste(from: board)
|
||||
#expect(attachments(page).count == 2)
|
||||
#expect(page.textView.string.hasSuffix(""))
|
||||
}
|
||||
|
||||
@Test("a pasted image is written to images/ and lands at the caret on a line of its own, in a dated block")
|
||||
func pasteImage() throws {
|
||||
let page = try TestPage("one two\n")
|
||||
let source = page.folder.appendingPathComponent("shot.png")
|
||||
try TestPage.writeImage(source, size: NSSize(width: 8, height: 8))
|
||||
let board = pasteboard()
|
||||
defer { board.releaseGlobally() }
|
||||
board.setData(try Data(contentsOf: source), forType: .png)
|
||||
page.caret(3)
|
||||
page.textView.paste(from: board)
|
||||
#expect(page.textView.string == "one\n\u{FFFC}\n two")
|
||||
let image = try #require(attachments(page).first?.image)
|
||||
#expect(image.path.wholeMatch(of: /images\/\d{4}-\d{2}-\d{2}-\d{6}-[0-9a-f]{4}\.png/) != nil)
|
||||
#expect(FileManager.default.fileExists(atPath: page.folder.appendingPathComponent(image.path).path))
|
||||
#expect(!image.isMissing)
|
||||
#expect(page.textView.selectedRange().location == 6)
|
||||
}
|
||||
|
||||
@Test("dropped image files are copied with their type and each lands on its own line at the drop place")
|
||||
func dropFiles() throws {
|
||||
let page = try TestPage("one\n\ntwo\n")
|
||||
let first = page.folder.appendingPathComponent("drop/a.png"), second = page.folder.appendingPathComponent("drop/b.png")
|
||||
try TestPage.writeImage(first, size: NSSize(width: 4, height: 4))
|
||||
try TestPage.writeImage(second, size: NSSize(width: 4, height: 4))
|
||||
page.page.insertImages([.file(first), .file(second)], replacing: NSRange(location: 4, length: 0))
|
||||
#expect(page.textView.string == "one\n\u{FFFC}\n\u{FFFC}\n\ntwo")
|
||||
let lines = page.fileText.components(separatedBy: "\n")
|
||||
#expect(lines.count == 6 && lines[0] == "one" && lines[3] == "" && lines[4] == "two")
|
||||
#expect(lines[1].hasPrefix(" && lines[1].hasSuffix(".png)") && lines[2] != lines[1])
|
||||
}
|
||||
|
||||
@Test("⌘V is enabled for an image-only pasteboard, and paste(_:) puts PNG data and a copied file in as images")
|
||||
func pasteEntryPoint() throws {
|
||||
let page = try TestPage("one\n")
|
||||
let png = page.folder.appendingPathComponent("drop/shot.png")
|
||||
try TestPage.writeImage(png, size: NSSize(width: 6, height: 6))
|
||||
let item = NSMenuItem(title: "Paste", action: #selector(NSText.paste(_:)), keyEquivalent: "v")
|
||||
|
||||
let data = pasteboard()
|
||||
defer { data.releaseGlobally() }
|
||||
data.setData(try Data(contentsOf: png), forType: .png)
|
||||
page.textView.pasteboard = data
|
||||
#expect(data.availableType(from: page.textView.readablePasteboardTypes) != nil)
|
||||
#expect(page.textView.validateUserInterfaceItem(item))
|
||||
page.caretAtEnd()
|
||||
page.textView.paste(nil)
|
||||
|
||||
let file = pasteboard()
|
||||
defer { file.releaseGlobally() }
|
||||
file.writeObjects([png as NSURL])
|
||||
page.textView.pasteboard = file
|
||||
#expect(page.textView.validateUserInterfaceItem(item))
|
||||
page.textView.paste(nil)
|
||||
|
||||
let images = attachments(page).map(\.image)
|
||||
#expect(images.count == 2)
|
||||
#expect(images.last?.path.hasSuffix(".png") == true && images.first?.path != images.last?.path)
|
||||
for image in images {
|
||||
#expect(FileManager.default.fileExists(atPath: page.folder.appendingPathComponent(image.path).path))
|
||||
#expect(!image.isMissing)
|
||||
}
|
||||
#expect(page.textView.string == "one\n\u{FFFC}\n\u{FFFC}")
|
||||
}
|
||||
|
||||
@Test("the text view takes image file drags, and the drop path saves the file and inserts it")
|
||||
func dropEntryPoint() throws {
|
||||
let page = try TestPage("one\ntwo\n")
|
||||
let registered = Set(page.textView.registeredDraggedTypes)
|
||||
#expect(registered.isSuperset(of: [.fileURL, .png, .tiff, .string]))
|
||||
let png = page.folder.appendingPathComponent("drop/a.png")
|
||||
try TestPage.writeImage(png, size: NSSize(width: 4, height: 4))
|
||||
let drop = try #require(page.textView.onImages)
|
||||
drop([.file(png)], NSRange(location: 4, length: 0))
|
||||
let image = try #require(attachments(page).first?.image)
|
||||
#expect(page.textView.string == "one\n\u{FFFC}\ntwo")
|
||||
#expect(FileManager.default.fileExists(atPath: page.folder.appendingPathComponent(image.path).path))
|
||||
#expect(FileManager.default.fileExists(atPath: page.folder.appendingPathComponent("images").path))
|
||||
}
|
||||
|
||||
@Test("the drop place and the drop caret come from the point alone, without a drag session")
|
||||
func dropPlace() throws {
|
||||
let page = try TestPage("one\ntwo\n")
|
||||
let tv = page.textView
|
||||
let second = tv.lineRect(at: 4)
|
||||
let start = tv.imageDrop(at: NSPoint(x: second.minX + 1, y: second.midY))
|
||||
#expect(start.location == 4)
|
||||
#expect(start.caret == NSRect(x: round(second.minX) - 1, y: second.minY, width: 2, height: second.height))
|
||||
|
||||
let first = tv.lineRect(at: 0)
|
||||
let end = tv.imageDrop(at: NSPoint(x: first.maxX - 1, y: first.midY))
|
||||
#expect(end.location == 3)
|
||||
#expect(end.caret.minX > first.minX + 4 && end.caret.width == 2 && end.caret.minY == first.minY)
|
||||
#expect(end.location == min(tv.characterIndexForInsertion(at: NSPoint(x: first.maxX - 1, y: first.midY)), tv.length))
|
||||
|
||||
tv.showDropCaret(start.caret)
|
||||
#expect(tv.dropCaret == start.caret)
|
||||
tv.showDropCaret(nil)
|
||||
#expect(tv.dropCaret == nil)
|
||||
}
|
||||
|
||||
@Test("an image drop lands at once in a window that is not key, and leaves no drop caret")
|
||||
func dropInBackground() throws {
|
||||
let page = try TestPage("one\ntwo\n")
|
||||
let window = try #require(page.controller.window)
|
||||
window.makeFirstResponder(nil)
|
||||
#expect(!window.isKeyWindow && window.firstResponder !== page.textView)
|
||||
let png = page.folder.appendingPathComponent("drop/a.png")
|
||||
try TestPage.writeImage(png, size: NSSize(width: 4, height: 4))
|
||||
let line = page.textView.lineRect(at: 4)
|
||||
page.textView.showDropCaret(page.textView.dropCaretRect(at: 4))
|
||||
page.textView.dropImages([.file(png)], at: NSPoint(x: line.minX + 1, y: line.midY))
|
||||
#expect(page.textView.string == "one\n\u{FFFC}\ntwo")
|
||||
#expect(attachments(page).first.map { !$0.image.isMissing } == true)
|
||||
#expect(page.textView.dropCaret == nil)
|
||||
}
|
||||
|
||||
@Test("image files win over the icon TIFF that Finder adds, other files are left out")
|
||||
func pasteboardFiles() {
|
||||
let board = pasteboard()
|
||||
defer { board.releaseGlobally() }
|
||||
let image = URL(fileURLWithPath: "/tmp/a.png"), other = URL(fileURLWithPath: "/tmp/b.txt")
|
||||
board.writeObjects([image as NSURL, other as NSURL])
|
||||
board.addTypes([.tiff], owner: nil)
|
||||
board.setData(Data([1, 2, 3]), forType: .tiff)
|
||||
#expect(PasteboardImages.read(board) == [.file(image)])
|
||||
#expect(PasteboardImages.contains(board))
|
||||
}
|
||||
|
||||
@Test("raw image data is read, rich text and plain text are not images")
|
||||
func pasteboardData() {
|
||||
let board = pasteboard()
|
||||
defer { board.releaseGlobally() }
|
||||
board.setData(Data([9]), forType: .png)
|
||||
#expect(PasteboardImages.read(board) == [.data(Data([9]))])
|
||||
|
||||
board.clearContents()
|
||||
board.setString("plain", forType: .string)
|
||||
#expect(PasteboardImages.read(board).isEmpty)
|
||||
#expect(!PasteboardImages.contains(board))
|
||||
|
||||
board.clearContents()
|
||||
board.setData(Data("{\\rtf1 x}".utf8), forType: .rtf)
|
||||
board.setData(Data([9]), forType: .tiff)
|
||||
#expect(PasteboardImages.read(board).isEmpty)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user