Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Nextcloud.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
AB7000042F70000100FE2776 /* NCShareDetailsGovernanceModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB7000032F70000100FE2776 /* NCShareDetailsGovernanceModel.swift */; };
AB7000062F70000100FE2776 /* NCGovernanceLabelSelectorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB7000052F70000100FE2776 /* NCGovernanceLabelSelectorView.swift */; };
AC13ADAF2026101300000002 /* NCMediaAdaptiveLayoutViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC13ADAF2026101300000001 /* NCMediaAdaptiveLayoutViewTests.swift */; };
B1E071012026102100000002 /* NCContextMenuViewerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1E071012026102100000001 /* NCContextMenuViewerTests.swift */; };
B1E071012026111000000002 /* NCViewerPhotoEditTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1E071012026111000000001 /* NCViewerPhotoEditTests.swift */; };
B1E071012026111000000004 /* MockViewerPhotoEditSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1E071012026111000000003 /* MockViewerPhotoEditSource.swift */; };
AC14ADAF2026101400000002 /* NCMediaAdaptiveControlsLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC14ADAF2026101400000001 /* NCMediaAdaptiveControlsLayout.swift */; };
AC14ADAF2026101400000012 /* NCMediaViewerPreviewLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC14ADAF2026101400000011 /* NCMediaViewerPreviewLoader.swift */; };
AF1A9B6427D0CA1E00F17A9E /* UIAlertController+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF1A9B6327D0CA1E00F17A9E /* UIAlertController+Extension.swift */; };
Expand Down Expand Up @@ -1411,6 +1414,9 @@
AB7000032F70000100FE2776 /* NCShareDetailsGovernanceModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NCShareDetailsGovernanceModel.swift; sourceTree = "<group>"; };
AB7000052F70000100FE2776 /* NCGovernanceLabelSelectorView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NCGovernanceLabelSelectorView.swift; sourceTree = "<group>"; };
AC13ADAF2026101300000001 /* NCMediaAdaptiveLayoutViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCMediaAdaptiveLayoutViewTests.swift; sourceTree = "<group>"; };
B1E071012026102100000001 /* NCContextMenuViewerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCContextMenuViewerTests.swift; sourceTree = "<group>"; };
B1E071012026111000000001 /* NCViewerPhotoEditTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCViewerPhotoEditTests.swift; sourceTree = "<group>"; };
B1E071012026111000000003 /* MockViewerPhotoEditSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockViewerPhotoEditSource.swift; sourceTree = "<group>"; };
AC14ADAF2026101400000001 /* NCMediaAdaptiveControlsLayout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCMediaAdaptiveControlsLayout.swift; sourceTree = "<group>"; };
AC14ADAF2026101400000011 /* NCMediaViewerPreviewLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCMediaViewerPreviewLoader.swift; sourceTree = "<group>"; };
AF1A9B6327D0CA1E00F17A9E /* UIAlertController+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIAlertController+Extension.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2321,6 +2327,9 @@
AB1028260000000000050300 /* NCShareUploadNamePlannerTests.swift */,
AB1028260000000000040000 /* NCShareLoaderItemProviderTests.swift */,
4B74B947E07444CC9CFE0A6C /* NCDocumentEditorSupportTests.swift */,
B1E071012026102100000001 /* NCContextMenuViewerTests.swift */,
B1E071012026111000000001 /* NCViewerPhotoEditTests.swift */,
B1E071012026111000000003 /* MockViewerPhotoEditSource.swift */,
);
path = NextcloudUnitTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -4703,6 +4712,9 @@
AB1028260000000000040001 /* NCShareLoaderItemProviderTests.swift in Sources */,
AB1028260000000000040002 /* NCShareLoaderItemProvider.swift in Sources */,
22BA14432CF44BDBA2E6C291 /* NCDocumentEditorSupportTests.swift in Sources */,
B1E071012026102100000002 /* NCContextMenuViewerTests.swift in Sources */,
B1E071012026111000000002 /* NCViewerPhotoEditTests.swift in Sources */,
B1E071012026111000000004 /* MockViewerPhotoEditSource.swift in Sources */,
F372087D2BAB4C0F006B5430 /* TestConstants.swift in Sources */,
F78E2D6C29AF02DB0024D4F3 /* Database.swift in Sources */,
);
Expand Down
54 changes: 54 additions & 0 deletions Tests/NextcloudUnitTests/MockViewerPhotoEditSource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-FileCopyrightText: Nextcloud GmbH
// SPDX-FileCopyrightText: 2026 Matt Van Horn
// SPDX-License-Identifier: GPL-3.0-or-later

import Foundation
@testable import Nextcloud

/// A viewer stand-in for photo editing tests. The download stays pending
/// until the test calls `finishDownload(error:)`, and the shown photo can be
/// changed while it waits. `present(_:)` records each editor it would open.
@MainActor
final class MockViewerPhotoEditSource: NCViewerPhotoEditSource {
var shownOcId: String?
var isOnScreen = true
private(set) var downloadRequests: [String] = []
private(set) var presentedOcIds: [String] = []
private var pendingDownloads: [CheckedContinuation<Void, Error>] = []

init(shownOcId: String?) {
self.shownOcId = shownOcId
}

var hasPendingDownload: Bool {
!pendingDownloads.isEmpty
}

func downloadOriginalForEditing(_ metadata: tableMetadata) async throws {
downloadRequests.append(metadata.ocId)
try await withCheckedThrowingContinuation { continuation in
pendingDownloads.append(continuation)
}
}

func finishDownload(error: Error? = nil) {
let continuations = pendingDownloads
pendingDownloads.removeAll()
for continuation in continuations {
if let error {
continuation.resume(throwing: error)
} else {
continuation.resume()
}
}
}

/// Records that the editor was opened for this photo.
func present(_ metadata: tableMetadata) {
presentedOcIds.append(metadata.ocId)
}

func isShowingMedia(ocId: String) -> Bool {
isOnScreen && shownOcId == ocId
}
}
131 changes: 131 additions & 0 deletions Tests/NextcloudUnitTests/NCContextMenuViewerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// SPDX-FileCopyrightText: Nextcloud GmbH
// SPDX-FileCopyrightText: 2026 Marino Faggiana
// SPDX-License-Identifier: GPL-3.0-or-later

import Testing
import UIKit
import NextcloudKit
@testable import Nextcloud

@Suite("Photo viewer edit menu", .serialized)
@MainActor
struct NCContextMenuViewerTests {
private let account = "viewer-edit-menu"

@Test("An online preview-only image offers one Edit action without downloading")
func previewOnlyImageOffersEdit() async throws {
try await performOnline {
let metadata = makeImage(fileName: "photo.jpg")
#expect(metadata.isImage)
#expect(metadata.isModifiableWithQuickLook)
#expect(metadata.session.isEmpty)
#expect(metadata.status == NCGlobal.shared.metadataStatusNormal)

let menu = try #require(viewMenu(for: metadata, webView: false))

#expect(editActions(in: menu).count == 1)
#expect(metadata.session.isEmpty)
#expect(metadata.status == NCGlobal.shared.metadataStatusNormal)
let stored = await NCManageDatabase.shared.getMetadataFromOcIdAsync(metadata.ocId)
#expect(stored?.ocId == nil)
}
}

@Test("Web view, non-images, and encrypted photos omit Edit")
func ineligibleItemsOmitEdit() async throws {
try await performOnline {
let webPhoto = makeImage(fileName: "web.jpg")
let webMenu = try #require(viewMenu(for: webPhoto, webView: true))
#expect(editActions(in: webMenu).isEmpty)

let video = makeImage(fileName: "clip.mov")
video.classFile = NKTypeClassFile.video.rawValue
video.contentType = "video/mp4"
let videoMenu = try #require(viewMenu(for: video, webView: false))
#expect(editActions(in: videoMenu).isEmpty)

let folder = encryptedFolder()
await NCManageDatabase.shared.addMetadataAsync(folder)
let photo = makeImage(fileName: "secret.jpg", serverUrl: folder.serverUrlFileName)
photo.e2eEncrypted = true
let isImage = photo.isImage
let quickLookEligible = photo.isModifiableWithQuickLook
let encryptedMenu = viewMenu(for: photo, webView: false)
await NCManageDatabase.shared.deleteMetadataAsync(ocId: folder.ocId)

#expect(isImage)
#expect(!quickLookEligible)
#expect(editActions(in: try #require(encryptedMenu)).isEmpty)
}
}

private func performOnline(_ body: () async throws -> Void) async throws {
let networking = NCNetworking.shared
let previousReachability = networking.networkReachability
let previousCapabilities = networking.capabilities[account]
networking.networkReachability = NKTypeReachability.reachableEthernetOrWiFi
networking.capabilities[account] = NKCapabilities.Capabilities()
defer {
networking.networkReachability = previousReachability
networking.capabilities[account] = previousCapabilities
}
try await body()
}

private func viewMenu(for metadata: tableMetadata, webView: Bool) -> UIMenu? {
NCContextMenuViewer(
metadata: metadata,
controller: NCMainTabBarController(),
viewController: nil,
webView: webView,
sender: nil
).viewMenu()
}

private func editActions(in menu: UIMenu) -> [UIAction] {
let title = NSLocalizedString("_edit_", comment: "")
return actions(in: menu).filter { $0.title == title }
}

private func actions(in element: UIMenuElement) -> [UIAction] {
if let action = element as? UIAction {
return [action]
}
guard let menu = element as? UIMenu else {
return []
}
return menu.children.flatMap(actions(in:))
}

private func makeImage(fileName: String, serverUrl: String? = nil) -> tableMetadata {
let metadata = tableMetadata()
metadata.account = account
metadata.ocId = "viewer-edit-\(fileName)"
metadata.fileName = fileName
metadata.fileNameView = fileName
metadata.classFile = NKTypeClassFile.image.rawValue
metadata.contentType = "image/jpeg"
metadata.size = 2048
metadata.urlBase = "https://example.com"
metadata.userId = "user"
metadata.serverUrl = serverUrl ?? "https://example.com/remote.php/dav/files/user/Photos"
metadata.serverUrlFileName = metadata.serverUrl + "/" + fileName
return metadata
}

private func encryptedFolder() -> tableMetadata {
let folder = tableMetadata()
folder.account = account
folder.ocId = "viewer-edit-e2ee-folder"
folder.fileName = "Secret"
folder.fileNameView = folder.fileName
folder.directory = true
folder.e2eEncrypted = true
folder.classFile = NKTypeClassFile.directory.rawValue
folder.urlBase = "https://example.com"
folder.userId = "user"
folder.serverUrl = "https://example.com/remote.php/dav/files/user"
folder.serverUrlFileName = folder.serverUrl + "/" + folder.fileName
return folder
}
}
Loading